Skip to content

Commit 3e9181d

Browse files
authored
fix(secrets-manager): add nil pointer checks for cmd outputs (#633)
relates to STACKITCLI-113
1 parent 3f4d14a commit 3e9181d

File tree

9 files changed

+267
-16
lines changed

9 files changed

+267
-16
lines changed

internal/cmd/secrets-manager/instance/create/create.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ If you want to retry configuring the ACLs, you can do it via:
9494
}
9595
}
9696

97-
return outputResult(p, model, projectLabel, instanceId, resp)
97+
return outputResult(p, model.OutputFormat, projectLabel, instanceId, resp)
9898
},
9999
}
100100
configureFlags(cmd)
@@ -157,18 +157,22 @@ func buildUpdateACLsRequest(ctx context.Context, model *inputModel, instanceId s
157157
return req
158158
}
159159

160-
func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *secretsmanager.Instance) error {
161-
switch model.OutputFormat {
160+
func outputResult(p *print.Printer, outputFormat, projectLabel, instanceId string, instance *secretsmanager.Instance) error {
161+
if instance == nil {
162+
return fmt.Errorf("instance is nil")
163+
}
164+
165+
switch outputFormat {
162166
case print.JSONOutputFormat:
163-
details, err := json.MarshalIndent(resp, "", " ")
167+
details, err := json.MarshalIndent(instance, "", " ")
164168
if err != nil {
165169
return fmt.Errorf("marshal Secrets Manager instance: %w", err)
166170
}
167171
p.Outputln(string(details))
168172

169173
return nil
170174
case print.YAMLOutputFormat:
171-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
175+
details, err := yaml.MarshalWithOptions(instance, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
172176
if err != nil {
173177
return fmt.Errorf("marshal Secrets Manager instance: %w", err)
174178
}

internal/cmd/secrets-manager/instance/create/create_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,39 @@ func TestBuildCreateACLRequests(t *testing.T) {
302302
})
303303
}
304304
}
305+
306+
func TestOutputResult(t *testing.T) {
307+
type args struct {
308+
outputFormat string
309+
projectLabel string
310+
instanceId string
311+
instance *secretsmanager.Instance
312+
}
313+
tests := []struct {
314+
name string
315+
args args
316+
wantErr bool
317+
}{
318+
{
319+
name: "empty",
320+
args: args{},
321+
wantErr: true,
322+
},
323+
{
324+
name: "empty instance",
325+
args: args{
326+
instance: &secretsmanager.Instance{},
327+
},
328+
wantErr: false,
329+
},
330+
}
331+
p := print.NewPrinter()
332+
p.Cmd = NewCmd(p)
333+
for _, tt := range tests {
334+
t.Run(tt.name, func(t *testing.T) {
335+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instanceId, tt.args.instance); (err != nil) != tt.wantErr {
336+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
337+
}
338+
})
339+
}
340+
}

internal/cmd/secrets-manager/instance/describe/describe.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ func buildListACLsRequest(ctx context.Context, model *inputModel, apiClient *sec
111111
}
112112

113113
func outputResult(p *print.Printer, outputFormat string, instance *secretsmanager.Instance, aclList *secretsmanager.ListACLsResponse) error {
114+
if instance == nil {
115+
return fmt.Errorf("instance is nil")
116+
} else if aclList == nil {
117+
return fmt.Errorf("aclList is nil")
118+
}
119+
114120
output := struct {
115121
*secretsmanager.Instance
116122
*secretsmanager.ListACLsResponse
@@ -148,7 +154,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *secretsmanage
148154
table.AddRow("CREATION DATE", utils.PtrString(instance.CreationStartDate))
149155
table.AddSeparator()
150156
// Only show ACL if it's present and not empty
151-
if aclList != nil && aclList.Acls != nil && len(*aclList.Acls) > 0 {
157+
if aclList.Acls != nil && len(*aclList.Acls) > 0 {
152158
var cidrs []string
153159

154160
for _, acl := range *aclList.Acls {

internal/cmd/secrets-manager/instance/describe/describe_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,53 @@ func TestBuildGetACLsRequest(t *testing.T) {
252252
})
253253
}
254254
}
255+
256+
func TestOutputResult(t *testing.T) {
257+
type args struct {
258+
outputFormat string
259+
instance *secretsmanager.Instance
260+
aclList *secretsmanager.ListACLsResponse
261+
}
262+
tests := []struct {
263+
name string
264+
args args
265+
wantErr bool
266+
}{
267+
{
268+
name: "empty",
269+
args: args{},
270+
wantErr: true,
271+
},
272+
{
273+
name: "missing acl",
274+
args: args{
275+
aclList: &secretsmanager.ListACLsResponse{},
276+
},
277+
wantErr: true,
278+
},
279+
{
280+
name: "missing instance",
281+
args: args{
282+
instance: &secretsmanager.Instance{},
283+
},
284+
wantErr: true,
285+
},
286+
{
287+
name: "empty instance and empty acl",
288+
args: args{
289+
instance: &secretsmanager.Instance{},
290+
aclList: &secretsmanager.ListACLsResponse{},
291+
},
292+
wantErr: false,
293+
},
294+
}
295+
p := print.NewPrinter()
296+
p.Cmd = NewCmd(p)
297+
for _, tt := range tests {
298+
t.Run(tt.name, func(t *testing.T) {
299+
if err := outputResult(p, tt.args.outputFormat, tt.args.instance, tt.args.aclList); (err != nil) != tt.wantErr {
300+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
301+
}
302+
})
303+
}
304+
}

internal/cmd/secrets-manager/instance/list/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,44 @@ func TestBuildRequest(t *testing.T) {
196196
})
197197
}
198198
}
199+
200+
func TestOutputResult(t *testing.T) {
201+
type args struct {
202+
outputFormat string
203+
instances []secretsmanager.Instance
204+
}
205+
tests := []struct {
206+
name string
207+
args args
208+
wantErr bool
209+
}{
210+
{
211+
name: "empty",
212+
args: args{},
213+
wantErr: false,
214+
},
215+
{
216+
name: "empty instances slice",
217+
args: args{
218+
instances: []secretsmanager.Instance{},
219+
},
220+
wantErr: false,
221+
},
222+
{
223+
name: "empty instance in instances slice",
224+
args: args{
225+
instances: []secretsmanager.Instance{{}},
226+
},
227+
wantErr: false,
228+
},
229+
}
230+
p := print.NewPrinter()
231+
p.Cmd = NewCmd(p)
232+
for _, tt := range tests {
233+
t.Run(tt.name, func(t *testing.T) {
234+
if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
235+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
236+
}
237+
})
238+
}
239+
}

internal/cmd/secrets-manager/user/create/create.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8686
return fmt.Errorf("create Secrets Manager user: %w", err)
8787
}
8888

89-
return outputResult(p, model, instanceLabel, resp)
89+
return outputResult(p, model.OutputFormat, instanceLabel, resp)
9090
},
9191
}
9292

@@ -137,30 +137,34 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana
137137
return req
138138
}
139139

140-
func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *secretsmanager.User) error {
141-
switch model.OutputFormat {
140+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, user *secretsmanager.User) error {
141+
if user == nil {
142+
return fmt.Errorf("user is nil")
143+
}
144+
145+
switch outputFormat {
142146
case print.JSONOutputFormat:
143-
details, err := json.MarshalIndent(resp, "", " ")
147+
details, err := json.MarshalIndent(user, "", " ")
144148
if err != nil {
145149
return fmt.Errorf("marshal Secrets Manager user: %w", err)
146150
}
147151
p.Outputln(string(details))
148152

149153
return nil
150154
case print.YAMLOutputFormat:
151-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
155+
details, err := yaml.MarshalWithOptions(user, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
152156
if err != nil {
153157
return fmt.Errorf("marshal Secrets Manager user: %w", err)
154158
}
155159
p.Outputln(string(details))
156160

157161
return nil
158162
default:
159-
p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, utils.PtrString(resp.Id))
160-
p.Outputf("Username: %s\n", utils.PtrString(resp.Username))
161-
p.Outputf("Password: %s\n", utils.PtrString(resp.Password))
162-
p.Outputf("Description: %s\n", utils.PtrString(resp.Description))
163-
p.Outputf("Write Access: %s\n", utils.PtrString(resp.Write))
163+
p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, utils.PtrString(user.Id))
164+
p.Outputf("Username: %s\n", utils.PtrString(user.Username))
165+
p.Outputf("Password: %s\n", utils.PtrString(user.Password))
166+
p.Outputf("Description: %s\n", utils.PtrString(user.Description))
167+
p.Outputf("Write Access: %s\n", utils.PtrString(user.Write))
164168

165169
return nil
166170
}

internal/cmd/secrets-manager/user/create/create_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,38 @@ func TestBuildRequest(t *testing.T) {
225225
})
226226
}
227227
}
228+
229+
func TestOutputResult(t *testing.T) {
230+
type args struct {
231+
outputFormat string
232+
instanceLabel string
233+
user *secretsmanager.User
234+
}
235+
tests := []struct {
236+
name string
237+
args args
238+
wantErr bool
239+
}{
240+
{
241+
name: "empty",
242+
args: args{},
243+
wantErr: true,
244+
},
245+
{
246+
name: "empty user",
247+
args: args{
248+
user: &secretsmanager.User{},
249+
},
250+
wantErr: false,
251+
},
252+
}
253+
p := print.NewPrinter()
254+
p.Cmd = NewCmd(p)
255+
for _, tt := range tests {
256+
t.Run(tt.name, func(t *testing.T) {
257+
if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr {
258+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
259+
}
260+
})
261+
}
262+
}

internal/cmd/secrets-manager/user/describe/describe_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,37 @@ func TestBuildRequest(t *testing.T) {
243243
})
244244
}
245245
}
246+
247+
func TestOutputResult(t *testing.T) {
248+
type args struct {
249+
outputFormat string
250+
user secretsmanager.User
251+
}
252+
tests := []struct {
253+
name string
254+
args args
255+
wantErr bool
256+
}{
257+
{
258+
name: "empty",
259+
args: args{},
260+
wantErr: false,
261+
},
262+
{
263+
name: "empty user",
264+
args: args{
265+
user: secretsmanager.User{},
266+
},
267+
wantErr: false,
268+
},
269+
}
270+
p := print.NewPrinter()
271+
p.Cmd = NewCmd(p)
272+
for _, tt := range tests {
273+
t.Run(tt.name, func(t *testing.T) {
274+
if err := outputResult(p, tt.args.outputFormat, tt.args.user); (err != nil) != tt.wantErr {
275+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
276+
}
277+
})
278+
}
279+
}

internal/cmd/secrets-manager/user/list/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,44 @@ func TestBuildRequest(t *testing.T) {
203203
})
204204
}
205205
}
206+
207+
func TestOutputResult(t *testing.T) {
208+
type args struct {
209+
outputFormat string
210+
users []secretsmanager.User
211+
}
212+
tests := []struct {
213+
name string
214+
args args
215+
wantErr bool
216+
}{
217+
{
218+
name: "empty",
219+
args: args{},
220+
wantErr: false,
221+
},
222+
{
223+
name: "empty users slice",
224+
args: args{
225+
users: []secretsmanager.User{},
226+
},
227+
wantErr: false,
228+
},
229+
{
230+
name: "empty user in users slice",
231+
args: args{
232+
users: []secretsmanager.User{{}},
233+
},
234+
wantErr: false,
235+
},
236+
}
237+
p := print.NewPrinter()
238+
p.Cmd = NewCmd(p)
239+
for _, tt := range tests {
240+
t.Run(tt.name, func(t *testing.T) {
241+
if err := outputResult(p, tt.args.outputFormat, tt.args.users); (err != nil) != tt.wantErr {
242+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
243+
}
244+
})
245+
}
246+
}

0 commit comments

Comments
 (0)