Skip to content

Commit

Permalink
add nil check to apikey suppliedIn (#6733)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Ryan authored Oct 30, 2024
1 parent fc573ef commit e9561e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/apis/configuration/validation/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,20 @@ func validateOIDC(oidc *v1.OIDC, fieldPath *field.Path) field.ErrorList {

func validateAPIKey(apiKey *v1.APIKey, fieldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}

if apiKey == nil {
allErrs = append(allErrs, field.Required(fieldPath, "apiKey cannot be nil"))
return allErrs
}

if apiKey.SuppliedIn == nil {
allErrs = append(allErrs, field.Required(fieldPath.Child("suppliedIn"), "suppliedIn cannot be nil"))
return allErrs
}

if apiKey.SuppliedIn.Query == nil && apiKey.SuppliedIn.Header == nil {
msg := "at least one query or header name must be provided"
allErrs = append(allErrs, field.Required(fieldPath.Child("SuppliedIn"), msg))
allErrs = append(allErrs, field.Required(fieldPath.Child("suppliedIn"), msg))
}

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

if apiKey.ClientSecret == "" {
allErrs = append(allErrs, field.Required(fieldPath.Child("clientSecret"), ""))
allErrs = append(allErrs, field.Required(fieldPath.Child("clientSecret"), "clientSecret cannot be empty"))
} else {
allErrs = append(allErrs, validateSecretName(apiKey.ClientSecret, fieldPath.Child("clientSecret"))...)
}

allErrs = append(allErrs, validateSecretName(apiKey.ClientSecret, fieldPath.Child("clientSecret"))...)

return allErrs
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/configuration/validation/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,16 @@ func TestValidateAPIKeyPolicy_FailsOnInvalidInput(t *testing.T) {
},
msg: "invalid secret name",
},
{
apiKey: &v1.APIKey{
ClientSecret: "secret_1",
},
msg: "no suppliedIn provided",
},

{
apiKey: nil, msg: "no apikey provided",
},
}

for _, test := range tests {
Expand Down

0 comments on commit e9561e9

Please sign in to comment.