Skip to content

Commit e9561e9

Browse files
author
Jim Ryan
authored
add nil check to apikey suppliedIn (#6733)
1 parent fc573ef commit e9561e9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

pkg/apis/configuration/validation/policy.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,20 @@ func validateOIDC(oidc *v1.OIDC, fieldPath *field.Path) field.ErrorList {
294294

295295
func validateAPIKey(apiKey *v1.APIKey, fieldPath *field.Path) field.ErrorList {
296296
allErrs := field.ErrorList{}
297+
298+
if apiKey == nil {
299+
allErrs = append(allErrs, field.Required(fieldPath, "apiKey cannot be nil"))
300+
return allErrs
301+
}
302+
303+
if apiKey.SuppliedIn == nil {
304+
allErrs = append(allErrs, field.Required(fieldPath.Child("suppliedIn"), "suppliedIn cannot be nil"))
305+
return allErrs
306+
}
307+
297308
if apiKey.SuppliedIn.Query == nil && apiKey.SuppliedIn.Header == nil {
298309
msg := "at least one query or header name must be provided"
299-
allErrs = append(allErrs, field.Required(fieldPath.Child("SuppliedIn"), msg))
310+
allErrs = append(allErrs, field.Required(fieldPath.Child("suppliedIn"), msg))
300311
}
301312

302313
if apiKey.SuppliedIn.Header != nil {
@@ -316,11 +327,11 @@ func validateAPIKey(apiKey *v1.APIKey, fieldPath *field.Path) field.ErrorList {
316327
}
317328

318329
if apiKey.ClientSecret == "" {
319-
allErrs = append(allErrs, field.Required(fieldPath.Child("clientSecret"), ""))
330+
allErrs = append(allErrs, field.Required(fieldPath.Child("clientSecret"), "clientSecret cannot be empty"))
331+
} else {
332+
allErrs = append(allErrs, validateSecretName(apiKey.ClientSecret, fieldPath.Child("clientSecret"))...)
320333
}
321334

322-
allErrs = append(allErrs, validateSecretName(apiKey.ClientSecret, fieldPath.Child("clientSecret"))...)
323-
324335
return allErrs
325336
}
326337

pkg/apis/configuration/validation/policy_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,16 @@ func TestValidateAPIKeyPolicy_FailsOnInvalidInput(t *testing.T) {
16801680
},
16811681
msg: "invalid secret name",
16821682
},
1683+
{
1684+
apiKey: &v1.APIKey{
1685+
ClientSecret: "secret_1",
1686+
},
1687+
msg: "no suppliedIn provided",
1688+
},
1689+
1690+
{
1691+
apiKey: nil, msg: "no apikey provided",
1692+
},
16831693
}
16841694

16851695
for _, test := range tests {

0 commit comments

Comments
 (0)