Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move basic auth policy validation to CRD #7257

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/crd/bases/k8s.nginx.org_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,16 @@ spec:
description: BasicAuth holds HTTP Basic authentication configuration
properties:
realm:
description: The realm for basic authentication
pattern: ^([^"$\\]|\\[^$])*$
type: string
secret:
description: The name of the Kubernetes secret that stores the
Htpasswd configuration
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
required:
- secret
type: object
egressMTLS:
description: EgressMTLS defines an Egress MTLS policy.
Expand Down
7 changes: 7 additions & 0 deletions deploy/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,16 @@ spec:
description: BasicAuth holds HTTP Basic authentication configuration
properties:
realm:
description: The realm for basic authentication
pattern: ^([^"$\\]|\\[^$])*$
type: string
secret:
description: The name of the Kubernetes secret that stores the
Htpasswd configuration
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
required:
- secret
type: object
egressMTLS:
description: EgressMTLS defines an Egress MTLS policy.
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/configuration/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,13 @@ type JWTAuth struct {

// BasicAuth holds HTTP Basic authentication configuration
type BasicAuth struct {
Realm string `json:"realm"`
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Pattern=`^([^"$\\]|\\[^$])*$`
// The realm for basic authentication
Realm string `json:"realm,omitempty"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
// The name of the Kubernetes secret that stores the Htpasswd configuration
Secret string `json:"secret"`
}

Expand Down
13 changes: 0 additions & 13 deletions pkg/apis/configuration/validation/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func validatePolicySpec(spec *v1.PolicySpec, fieldPath *field.Path, isPlus, enab
}

if spec.BasicAuth != nil {
allErrs = append(allErrs, validateBasic(spec.BasicAuth, fieldPath.Child("basicAuth"))...)
fieldCount++
}

Expand Down Expand Up @@ -206,18 +205,6 @@ func validateJWT(jwt *v1.JWTAuth, fieldPath *field.Path) field.ErrorList {
return allErrs
}

func validateBasic(basic *v1.BasicAuth, fieldPath *field.Path) field.ErrorList {
if basic.Secret == "" {
return field.ErrorList{field.Required(fieldPath.Child("secret"), "")}
}

allErrs := field.ErrorList{}
if basic.Realm != "" {
allErrs = append(allErrs, validateRealm(basic.Realm, fieldPath.Child("realm"))...)
}
return append(allErrs, validateSecretName(basic.Secret, fieldPath.Child("secret"))...)
}

func validateIngressMTLS(ingressMTLS *v1.IngressMTLS, fieldPath *field.Path) field.ErrorList {
if ingressMTLS.ClientCertSecret == "" {
return field.ErrorList{field.Required(fieldPath.Child("clientCertSecret"), "")}
Expand Down
18 changes: 0 additions & 18 deletions pkg/apis/configuration/validation/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1982,24 +1982,6 @@ func TestValidateWAF_FailsOnInvalidApPolicy(t *testing.T) {
}
}

func TestValidateBasic_PassesOnNotEmptySecret(t *testing.T) {
t.Parallel()

errList := validateBasic(&v1.BasicAuth{Realm: "", Secret: "secret"}, field.NewPath("secret"))
if len(errList) != 0 {
t.Errorf("want no errors, got %v", errList)
}
}

func TestValidateBasic_FailsOnMissingSecret(t *testing.T) {
t.Parallel()

errList := validateBasic(&v1.BasicAuth{Realm: "realm", Secret: ""}, field.NewPath("secret"))
if len(errList) == 0 {
t.Error("want error on invalid input")
}
}

func TestValidateWAF_FailsOnPresentBothApLogBundleAndApLogConf(t *testing.T) {
t.Parallel()

Expand Down
Loading