Skip to content

Commit f7d9744

Browse files
authored
fix(service-account): add nil pointer checks for cmd outputs (#635)
relates to STACKITCLI-114
1 parent ff37f2a commit f7d9744

File tree

10 files changed

+285
-7
lines changed

10 files changed

+285
-7
lines changed

internal/cmd/service-account/create/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7474
return fmt.Errorf("create service account: %w", err)
7575
}
7676

77-
return outputResult(p, model, projectLabel, resp)
77+
return outputResult(p, model.OutputFormat, projectLabel, resp)
7878
},
7979
}
8080
configureFlags(cmd)
@@ -119,8 +119,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
119119
return req
120120
}
121121

122-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, serviceAccount *serviceaccount.ServiceAccount) error {
123-
switch model.OutputFormat {
122+
func outputResult(p *print.Printer, outputFormat, projectLabel string, serviceAccount *serviceaccount.ServiceAccount) error {
123+
if serviceAccount == nil {
124+
return fmt.Errorf("service account is nil")
125+
}
126+
127+
switch outputFormat {
124128
case print.JSONOutputFormat:
125129
details, err := json.MarshalIndent(serviceAccount, "", " ")
126130
if err != nil {

internal/cmd/service-account/create/create_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,38 @@ func TestBuildRequest(t *testing.T) {
187187
})
188188
}
189189
}
190+
191+
func TestOutputResult(t *testing.T) {
192+
type args struct {
193+
outputFormat string
194+
projectLabel string
195+
serviceAccount *serviceaccount.ServiceAccount
196+
}
197+
tests := []struct {
198+
name string
199+
args args
200+
wantErr bool
201+
}{
202+
{
203+
name: "empty",
204+
args: args{},
205+
wantErr: true,
206+
},
207+
{
208+
name: "empty service account",
209+
args: args{
210+
serviceAccount: &serviceaccount.ServiceAccount{},
211+
},
212+
wantErr: false,
213+
},
214+
}
215+
p := print.NewPrinter()
216+
p.Cmd = NewCmd(p)
217+
for _, tt := range tests {
218+
t.Run(tt.name, func(t *testing.T) {
219+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.serviceAccount); (err != nil) != tt.wantErr {
220+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
221+
}
222+
})
223+
}
224+
}

internal/cmd/service-account/get-jwks/get_jwks_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,43 @@ func TestBuildRequest(t *testing.T) {
137137
})
138138
}
139139
}
140+
141+
func TestOutputResult(t *testing.T) {
142+
type args struct {
143+
serviceAccounts []serviceaccount.JWK
144+
}
145+
tests := []struct {
146+
name string
147+
args args
148+
wantErr bool
149+
}{
150+
{
151+
name: "empty",
152+
args: args{},
153+
wantErr: false,
154+
},
155+
{
156+
name: "empty service accounts slice",
157+
args: args{
158+
serviceAccounts: []serviceaccount.JWK{},
159+
},
160+
wantErr: false,
161+
},
162+
{
163+
name: "empty service account in service accounts slice",
164+
args: args{
165+
serviceAccounts: []serviceaccount.JWK{{}},
166+
},
167+
wantErr: false,
168+
},
169+
}
170+
p := print.NewPrinter()
171+
p.Cmd = NewCmd(p)
172+
for _, tt := range tests {
173+
t.Run(tt.name, func(t *testing.T) {
174+
if err := outputResult(p, tt.args.serviceAccounts); (err != nil) != tt.wantErr {
175+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
176+
}
177+
})
178+
}
179+
}

internal/cmd/service-account/key/describe/describe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
115115
}
116116

117117
func outputResult(p *print.Printer, key *serviceaccount.GetServiceAccountKeyResponse) error {
118+
if key == nil {
119+
return fmt.Errorf("key is nil")
120+
}
121+
118122
marshaledKey, err := json.MarshalIndent(key, "", " ")
119123
if err != nil {
120124
return fmt.Errorf("marshal service account key: %w", err)

internal/cmd/service-account/key/describe/describe_test.go

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

internal/cmd/service-account/key/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+
keys []serviceaccount.ServiceAccountKeyListResponse
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 keys slice",
217+
args: args{
218+
keys: []serviceaccount.ServiceAccountKeyListResponse{},
219+
},
220+
wantErr: false,
221+
},
222+
{
223+
name: "empty key in keys slice",
224+
args: args{
225+
keys: []serviceaccount.ServiceAccountKeyListResponse{{}},
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.keys); (err != nil) != tt.wantErr {
235+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
236+
}
237+
})
238+
}
239+
}

internal/cmd/service-account/list/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,44 @@ func TestBuildRequest(t *testing.T) {
186186
})
187187
}
188188
}
189+
190+
func TestOutputResult(t *testing.T) {
191+
type args struct {
192+
outputFormat string
193+
serviceAccounts []serviceaccount.ServiceAccount
194+
}
195+
tests := []struct {
196+
name string
197+
args args
198+
wantErr bool
199+
}{
200+
{
201+
name: "empty",
202+
args: args{},
203+
wantErr: false,
204+
},
205+
{
206+
name: "empty service accounts slice",
207+
args: args{
208+
serviceAccounts: []serviceaccount.ServiceAccount{},
209+
},
210+
wantErr: false,
211+
},
212+
{
213+
name: "empty service account in service accounts slice",
214+
args: args{
215+
serviceAccounts: []serviceaccount.ServiceAccount{{}},
216+
},
217+
wantErr: false,
218+
},
219+
}
220+
p := print.NewPrinter()
221+
p.Cmd = NewCmd(p)
222+
for _, tt := range tests {
223+
t.Run(tt.name, func(t *testing.T) {
224+
if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccounts); (err != nil) != tt.wantErr {
225+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
226+
}
227+
})
228+
}
229+
}

internal/cmd/service-account/token/create/create.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7878
return fmt.Errorf("create access token: %w", err)
7979
}
8080

81-
return outputResult(p, model, token)
81+
return outputResult(p, model.OutputFormat, model.ServiceAccountEmail, token)
8282
},
8383
}
8484

@@ -142,8 +142,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
142142
return req
143143
}
144144

145-
func outputResult(p *print.Printer, model *inputModel, token *serviceaccount.AccessToken) error {
146-
switch model.OutputFormat {
145+
func outputResult(p *print.Printer, outputFormat, serviceAccountEmail string, token *serviceaccount.AccessToken) error {
146+
if token == nil {
147+
return fmt.Errorf("token is nil")
148+
}
149+
150+
switch outputFormat {
147151
case print.JSONOutputFormat:
148152
details, err := json.MarshalIndent(token, "", " ")
149153
if err != nil {
@@ -161,7 +165,7 @@ func outputResult(p *print.Printer, model *inputModel, token *serviceaccount.Acc
161165

162166
return nil
163167
default:
164-
p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, utils.PtrString(token.Id))
168+
p.Outputf("Created access token for service account %s. Token ID: %s\n\n", serviceAccountEmail, utils.PtrString(token.Id))
165169
p.Outputf("Valid until: %s\n", utils.PtrString(token.ValidUntil))
166170
p.Outputf("Token: %s\n", utils.PtrString(token.Token))
167171
return nil

internal/cmd/service-account/token/create/create_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,38 @@ func TestBuildRequest(t *testing.T) {
193193
})
194194
}
195195
}
196+
197+
func TestOutputResult(t *testing.T) {
198+
type args struct {
199+
outputFormat string
200+
serviceAccountEmail string
201+
token *serviceaccount.AccessToken
202+
}
203+
tests := []struct {
204+
name string
205+
args args
206+
wantErr bool
207+
}{
208+
{
209+
name: "empty",
210+
args: args{},
211+
wantErr: true,
212+
},
213+
{
214+
name: "empty token",
215+
args: args{
216+
token: &serviceaccount.AccessToken{},
217+
},
218+
wantErr: false,
219+
},
220+
}
221+
p := print.NewPrinter()
222+
p.Cmd = NewCmd(p)
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccountEmail, tt.args.token); (err != nil) != tt.wantErr {
226+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
227+
}
228+
})
229+
}
230+
}

internal/cmd/service-account/token/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+
tokensMetadata []serviceaccount.AccessTokenMetadata
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 tokens metadata slice",
217+
args: args{
218+
tokensMetadata: []serviceaccount.AccessTokenMetadata{},
219+
},
220+
wantErr: false,
221+
},
222+
{
223+
name: "empty token metadata in tokens metadata slice",
224+
args: args{
225+
tokensMetadata: []serviceaccount.AccessTokenMetadata{{}},
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.tokensMetadata); (err != nil) != tt.wantErr {
235+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
236+
}
237+
})
238+
}
239+
}

0 commit comments

Comments
 (0)