Skip to content

Commit 6639d4e

Browse files
author
Jan Sternagel
committed
addressing nil pointer derferences
1 parent 78871c2 commit 6639d4e

File tree

14 files changed

+99
-50
lines changed

14 files changed

+99
-50
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8989

9090
// Call API
9191
req, _ := buildRequest(ctx, model, apiClient)
92-
key, err := req.Execute()
92+
resp, err := req.Execute()
9393
if err != nil {
9494
return fmt.Errorf("create KMS key: %w", err)
9595
}
@@ -98,14 +98,14 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
9898
if !model.Async {
9999
s := spinner.New(params.Printer)
100100
s.Start("Creating key")
101-
_, err = wait.CreateOrUpdateKeyWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.KeyRingId, *key.Id).WaitWithContext(ctx)
101+
_, err = wait.CreateOrUpdateKeyWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.KeyRingId, *resp.Id).WaitWithContext(ctx)
102102
if err != nil {
103103
return fmt.Errorf("wait for KMS key creation: %w", err)
104104
}
105105
s.Stop()
106106
}
107107

108-
return outputResult(params.Printer, model.OutputFormat, projectLabel, key)
108+
return outputResult(params.Printer, model.OutputFormat, projectLabel, resp)
109109
},
110110
}
111111
configureFlags(cmd)

internal/cmd/beta/kms/key/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func configureFlags(cmd *cobra.Command) {
132132

133133
func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
134134
if resp == nil {
135-
return fmt.Errorf("response from 'GetKeyExecute()' is nil")
135+
return fmt.Errorf("response is nil")
136136
}
137137

138138
switch outputFormat {

internal/cmd/beta/kms/key/importKey/importKey.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8383

8484
// Call API
8585
req, _ := buildRequest(ctx, model, apiClient)
86-
keyVersion, err := req.Execute()
86+
resp, err := req.Execute()
8787
if err != nil {
8888
return fmt.Errorf("import KMS key: %w", err)
8989
}
9090

91-
return outputResult(params.Printer, model.OutputFormat, keyRingName, keyName, keyVersion)
91+
return outputResult(params.Printer, model.OutputFormat, keyRingName, keyName, resp)
9292
},
9393
}
9494
configureFlags(cmd)

internal/cmd/beta/kms/key/list/list.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6363
return fmt.Errorf("get KMS Keys: %w", err)
6464
}
6565

66-
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyRingId, *resp.Keys)
66+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyRingId, resp)
6767
},
6868
}
6969

@@ -105,11 +105,13 @@ func configureFlags(cmd *cobra.Command) {
105105
cobra.CheckErr(err)
106106
}
107107

108-
func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, keys []kms.Key) error {
109-
if keys == nil {
110-
return fmt.Errorf("response was an empty list")
108+
func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, resp *kms.KeyList) error {
109+
if resp == nil || resp.Keys == nil {
110+
return fmt.Errorf("response was nil / empty")
111111
}
112112

113+
keys := *resp.Keys
114+
113115
switch outputFormat {
114116
case print.JSONOutputFormat:
115117
details, err := json.MarshalIndent(keys, "", " ")

internal/cmd/beta/kms/key/list/list_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,37 +200,44 @@ func TestBuildRequest(t *testing.T) {
200200
func TestOutputResult(t *testing.T) {
201201
tests := []struct {
202202
description string
203-
keys []kms.Key
203+
resp *kms.KeyList
204204
projectId string
205205
keyRingId string
206206
outputFormat string
207207
wantErr bool
208208
}{
209209
{
210210
description: "nil response",
211-
keys: nil,
211+
resp: nil,
212+
projectId: uuid.NewString(),
213+
keyRingId: uuid.NewString(),
214+
wantErr: true,
215+
},
216+
{
217+
description: "empty response",
218+
resp: &kms.KeyList{},
212219
projectId: uuid.NewString(),
213220
keyRingId: uuid.NewString(),
214221
wantErr: true,
215222
},
216223
{
217224
description: "default output",
218-
keys: []kms.Key{},
225+
resp: &kms.KeyList{Keys: &[]kms.Key{}},
219226
projectId: uuid.NewString(),
220227
keyRingId: uuid.NewString(),
221228
wantErr: false,
222229
},
223230
{
224231
description: "json output",
225-
keys: []kms.Key{},
232+
resp: &kms.KeyList{Keys: &[]kms.Key{}},
226233
projectId: uuid.NewString(),
227234
keyRingId: uuid.NewString(),
228235
outputFormat: print.JSONOutputFormat,
229236
wantErr: false,
230237
},
231238
{
232239
description: "yaml output",
233-
keys: []kms.Key{},
240+
resp: &kms.KeyList{Keys: &[]kms.Key{}},
234241
projectId: uuid.NewString(),
235242
keyRingId: uuid.NewString(),
236243
outputFormat: print.YAMLOutputFormat,
@@ -242,7 +249,7 @@ func TestOutputResult(t *testing.T) {
242249
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
243250
for _, tt := range tests {
244251
t.Run(tt.description, func(t *testing.T) {
245-
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRingId, tt.keys)
252+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRingId, tt.resp)
246253
if (err != nil) != tt.wantErr {
247254
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
248255
}

internal/cmd/beta/kms/key/restore/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func configureFlags(cmd *cobra.Command) {
131131

132132
func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
133133
if resp == nil {
134-
return fmt.Errorf("response from 'GetKeyExecute()' is nil")
134+
return fmt.Errorf("response is nil")
135135
}
136136

137137
switch outputFormat {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5757
return fmt.Errorf("get KMS key rings: %w", err)
5858
}
5959

60-
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, *resp.KeyRings)
60+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, resp)
6161
},
6262
}
6363

@@ -91,11 +91,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
9191
return req
9292
}
9393

94-
func outputResult(p *print.Printer, outputFormat, projectId string, keyRings []kms.KeyRing) error {
95-
if keyRings == nil {
96-
return fmt.Errorf("response was nil")
94+
func outputResult(p *print.Printer, outputFormat, projectId string, resp *kms.KeyRingList) error {
95+
if resp == nil || resp.KeyRings == nil {
96+
return fmt.Errorf("response was nil / empty")
9797
}
9898

99+
keyRings := *resp.KeyRings
100+
99101
switch outputFormat {
100102
case print.JSONOutputFormat:
101103
details, err := json.MarshalIndent(keyRings, "", " ")

internal/cmd/beta/kms/keyring/list/list_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,36 +174,43 @@ func TestOutputResult(t *testing.T) {
174174
tests := []struct {
175175
description string
176176
projectId string
177-
keyRings []kms.KeyRing
177+
resp *kms.KeyRingList
178178
outputFormat string
179179
projectLabel string
180180
wantErr bool
181181
}{
182182
{
183183
description: "nil response",
184-
keyRings: nil,
184+
resp: nil,
185+
projectId: uuid.NewString(),
186+
projectLabel: "my-project",
187+
wantErr: true,
188+
},
189+
{
190+
description: "empty response",
191+
resp: &kms.KeyRingList{},
185192
projectId: uuid.NewString(),
186193
projectLabel: "my-project",
187194
wantErr: true,
188195
},
189196
{
190197
description: "default output",
191198
projectId: uuid.NewString(),
192-
keyRings: []kms.KeyRing{},
199+
resp: &kms.KeyRingList{KeyRings: &[]kms.KeyRing{}},
193200
projectLabel: "my-project",
194201
wantErr: false,
195202
},
196203
{
197204
description: "json output",
198205
projectId: uuid.NewString(),
199-
keyRings: []kms.KeyRing{},
206+
resp: &kms.KeyRingList{KeyRings: &[]kms.KeyRing{}},
200207
outputFormat: print.JSONOutputFormat,
201208
wantErr: false,
202209
},
203210
{
204211
description: "yaml output",
205212
projectId: uuid.NewString(),
206-
keyRings: []kms.KeyRing{},
213+
resp: &kms.KeyRingList{KeyRings: &[]kms.KeyRing{}},
207214
outputFormat: print.YAMLOutputFormat,
208215
wantErr: false,
209216
},
@@ -213,7 +220,7 @@ func TestOutputResult(t *testing.T) {
213220
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
214221
for _, tt := range tests {
215222
t.Run(tt.description, func(t *testing.T) {
216-
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRings)
223+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.resp)
217224
if (err != nil) != tt.wantErr {
218225
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
219226
}

internal/cmd/beta/kms/version/list/list.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6565
return fmt.Errorf("get key version: %w", err)
6666
}
6767

68-
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyId, *resp.Versions)
68+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyId, resp)
6969
},
7070
}
7171

@@ -101,7 +101,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
101101
return apiClient.ListVersions(ctx, model.ProjectId, model.Region, model.KeyRingId, model.KeyId)
102102
}
103103

104-
func outputResult(p *print.Printer, outputFormat, projectId, keyId string, versions []kms.Version) error {
104+
func outputResult(p *print.Printer, outputFormat, projectId, keyId string, resp *kms.VersionList) error {
105+
if resp == nil || resp.Versions == nil {
106+
return fmt.Errorf("response is nil / empty")
107+
}
108+
versions := *resp.Versions
109+
105110
switch outputFormat {
106111
case print.JSONOutputFormat:
107112
details, err := json.MarshalIndent(versions, "", " ")
@@ -125,8 +130,7 @@ func outputResult(p *print.Printer, outputFormat, projectId, keyId string, versi
125130
table := tables.NewTable()
126131
table.SetHeader("ID", "NUMBER", "CREATED AT", "DESTROY DATE", "STATUS")
127132

128-
for i := range versions {
129-
version := versions[i]
133+
for _, version := range versions {
130134
table.AddRow(
131135
utils.PtrString(version.KeyId),
132136
utils.PtrString(version.Number),

internal/cmd/beta/kms/version/list/list_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,32 +232,38 @@ func TestOutputResult(t *testing.T) {
232232
description string
233233
projectId string
234234
keyId string
235-
versions []kms.Version
235+
resp *kms.VersionList
236236
outputFormat string
237237
projectLabel string
238238
wantErr bool
239239
}{
240+
{
241+
description: "nil response",
242+
resp: nil,
243+
projectLabel: "my-project",
244+
wantErr: true,
245+
},
240246
{
241247
description: "empty default",
242-
versions: nil,
248+
resp: &kms.VersionList{},
243249
projectLabel: "my-project",
244-
wantErr: false,
250+
wantErr: true,
245251
},
246252
{
247253
description: "default output",
248-
versions: []kms.Version{},
254+
resp: &kms.VersionList{Versions: &[]kms.Version{}},
249255
projectLabel: "my-project",
250256
wantErr: false,
251257
},
252258
{
253259
description: "json output",
254-
versions: []kms.Version{},
260+
resp: &kms.VersionList{Versions: &[]kms.Version{}},
255261
outputFormat: print.JSONOutputFormat,
256262
wantErr: false,
257263
},
258264
{
259265
description: "yaml output",
260-
versions: []kms.Version{},
266+
resp: &kms.VersionList{Versions: &[]kms.Version{}},
261267
outputFormat: print.YAMLOutputFormat,
262268
wantErr: false,
263269
},
@@ -267,7 +273,7 @@ func TestOutputResult(t *testing.T) {
267273
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
268274
for _, tt := range tests {
269275
t.Run(tt.description, func(t *testing.T) {
270-
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyId, tt.versions)
276+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyId, tt.resp)
271277
if (err != nil) != tt.wantErr {
272278
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
273279
}

0 commit comments

Comments
 (0)