Skip to content

Commit 77c6d96

Browse files
author
Jan Sternagel
committed
nil pointer safe
1 parent 26490c9 commit 77c6d96

File tree

1 file changed

+16
-10
lines changed
  • internal/cmd/beta/kms/keyring/create

1 file changed

+16
-10
lines changed

internal/cmd/beta/kms/keyring/create/create.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ type inputModel struct {
3737
func NewCmd(params *params.CmdParams) *cobra.Command {
3838
cmd := &cobra.Command{
3939
Use: "create",
40-
Short: "Creates a KMS Key Ring",
41-
Long: "Creates a KMS Key Ring.",
40+
Short: "Creates a KMS key ring",
41+
Long: "Creates a KMS key ring.",
4242
Args: args.NoArgs,
4343
Example: examples.Build(
4444
examples.NewExample(
4545
`Create a KMS key ring`,
4646
"$ stakit beta kms keyring create --name my-keyring"),
4747
examples.NewExample(
48-
`Create a KMS Key ring with a description`,
48+
`Create a KMS key ring with a description`,
4949
"$ stakit beta kms keyring create --name my-keyring --description my-description"),
5050
),
5151
RunE: func(cmd *cobra.Command, _ []string) error {
@@ -68,7 +68,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6868
}
6969

7070
if !model.AssumeYes {
71-
prompt := fmt.Sprintf("Are you sure you want to create a KMS Key Ring for project %q?", projectLabel)
71+
prompt := fmt.Sprintf("Are you sure you want to create a KMS key ring for project %q?", projectLabel)
7272
err = params.Printer.PromptForConfirmation(prompt)
7373
if err != nil {
7474
return err
@@ -80,8 +80,14 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8080

8181
keyRing, err := req.Execute()
8282
if err != nil {
83-
return fmt.Errorf("create KMS Key Ring: %w", err)
83+
return fmt.Errorf("create KMS key ring: %w", err)
8484
}
85+
86+
// Prevent potential nil pointer dereference
87+
if keyRing == nil || keyRing.Id == nil {
88+
return fmt.Errorf("API call succeeded but returned an invalid response (missing key ring ID)")
89+
}
90+
8591
keyRingId := *keyRing.Id
8692

8793
// Wait for async operation, if async mode not enabled
@@ -90,7 +96,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
9096
s.Start("Creating instance")
9197
_, err = wait.CreateKeyRingWaitHandler(ctx, apiClient, model.ProjectId, model.Region, keyRingId).WaitWithContext(ctx)
9298
if err != nil {
93-
return fmt.Errorf("wait for KMS Key Ring creation: %w", err)
99+
return fmt.Errorf("wait for KMS key ring creation: %w", err)
94100
}
95101
s.Stop()
96102
}
@@ -159,15 +165,15 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
159165
case print.JSONOutputFormat:
160166
details, err := json.MarshalIndent(resp, "", " ")
161167
if err != nil {
162-
return fmt.Errorf("marshal KMS Keyring: %w", err)
168+
return fmt.Errorf("marshal KMS key ring: %w", err)
163169
}
164170
p.Outputln(string(details))
165171
return nil
166172

167173
case print.YAMLOutputFormat:
168174
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
169175
if err != nil {
170-
return fmt.Errorf("marshal KMS Keyring: %w", err)
176+
return fmt.Errorf("marshal KMS key ring: %w", err)
171177
}
172178
p.Outputln(string(details))
173179
return nil
@@ -179,8 +185,8 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
179185
}
180186

181187
func configureFlags(cmd *cobra.Command) {
182-
cmd.Flags().String(keyRingNameFlag, "", "Name of the KMS Key Ring")
183-
cmd.Flags().String(descriptionFlag, "", "Optinal description of the Key Ring")
188+
cmd.Flags().String(keyRingNameFlag, "", "Name of the KMS key ring")
189+
cmd.Flags().String(descriptionFlag, "", "Optional description of the key ring")
184190

185191
err := flags.MarkFlagsRequired(cmd, keyRingNameFlag)
186192
cobra.CheckErr(err)

0 commit comments

Comments
 (0)