@@ -2,9 +2,11 @@ package graph
22
33import (
44 "fmt"
5+ "slices"
56
67 "k8s.io/apimachinery/pkg/types"
78 "k8s.io/apimachinery/pkg/util/validation/field"
9+ v1 "sigs.k8s.io/gateway-api/apis/v1"
810 "sigs.k8s.io/gateway-api/apis/v1alpha3"
911
1012 "github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
@@ -31,6 +33,7 @@ type BackendTLSPolicy struct {
3133func processBackendTLSPolicies (
3234 backendTLSPolicies map [types.NamespacedName ]* v1alpha3.BackendTLSPolicy ,
3335 configMapResolver * configMapResolver ,
36+ secretResolver * secretResolver ,
3437 ctlrName string ,
3538 gateway * Gateway ,
3639) map [types.NamespacedName ]* BackendTLSPolicy {
@@ -42,7 +45,7 @@ func processBackendTLSPolicies(
4245 for nsname , backendTLSPolicy := range backendTLSPolicies {
4346 var caCertRef types.NamespacedName
4447
45- valid , ignored , conds := validateBackendTLSPolicy (backendTLSPolicy , configMapResolver , ctlrName )
48+ valid , ignored , conds := validateBackendTLSPolicy (backendTLSPolicy , configMapResolver , secretResolver , ctlrName )
4649
4750 if valid && ! ignored && backendTLSPolicy .Spec .Validation .CACertificateRefs != nil {
4851 caCertRef = types.NamespacedName {
@@ -68,6 +71,7 @@ func processBackendTLSPolicies(
6871func validateBackendTLSPolicy (
6972 backendTLSPolicy * v1alpha3.BackendTLSPolicy ,
7073 configMapResolver * configMapResolver ,
74+ secretResolver * secretResolver ,
7175 ctlrName string ,
7276) (valid , ignored bool , conds []conditions.Condition ) {
7377 valid = true
@@ -93,7 +97,7 @@ func validateBackendTLSPolicy(
9397 conds = append (conds , staticConds .NewPolicyInvalid (msg ))
9498
9599 case len (caCertRefs ) > 0 :
96- if err := validateBackendTLSCACertRef (backendTLSPolicy , configMapResolver ); err != nil {
100+ if err := validateBackendTLSCACertRef (backendTLSPolicy , configMapResolver , secretResolver ); err != nil {
97101 valid = false
98102 conds = append (conds , staticConds .NewPolicyInvalid (
99103 fmt .Sprintf ("invalid CACertificateRef: %s" , err .Error ())))
@@ -124,30 +128,42 @@ func validateBackendTLSHostname(btp *v1alpha3.BackendTLSPolicy) error {
124128 return nil
125129}
126130
127- func validateBackendTLSCACertRef (btp * v1alpha3.BackendTLSPolicy , configMapResolver * configMapResolver ) error {
131+ func validateBackendTLSCACertRef (btp * v1alpha3.BackendTLSPolicy , configMapResolver * configMapResolver , secretResolver * secretResolver ) error {
128132 if len (btp .Spec .Validation .CACertificateRefs ) != 1 {
129133 path := field .NewPath ("tls.cacertrefs" )
130134 valErr := field .TooMany (path , len (btp .Spec .Validation .CACertificateRefs ), 1 )
131135 return valErr
132136 }
133- if btp .Spec .Validation .CACertificateRefs [0 ].Kind != "ConfigMap" {
137+
138+ selectedCertRef := btp .Spec .Validation .CACertificateRefs [0 ]
139+ allowedCaCertKinds := []v1.Kind {"ConfigMap" , "Secret" }
140+
141+ if slices .Contains (allowedCaCertKinds , selectedCertRef .Kind ) {
134142 path := field .NewPath ("tls.cacertrefs[0].kind" )
135- valErr := field .NotSupported (path , btp .Spec .Validation .CACertificateRefs [0 ].Kind , [] string { "ConfigMap" } )
143+ valErr := field .NotSupported (path , btp .Spec .Validation .CACertificateRefs [0 ].Kind , allowedCaCertKinds )
136144 return valErr
137145 }
138- if btp . Spec . Validation . CACertificateRefs [ 0 ] .Group != "" &&
139- btp . Spec . Validation . CACertificateRefs [ 0 ] .Group != "core" {
146+ if selectedCertRef .Group != "" &&
147+ selectedCertRef .Group != "core" {
140148 path := field .NewPath ("tls.cacertrefs[0].group" )
141- valErr := field .NotSupported (path , btp . Spec . Validation . CACertificateRefs [ 0 ] .Group , []string {"" , "core" })
149+ valErr := field .NotSupported (path , selectedCertRef .Group , []string {"" , "core" })
142150 return valErr
143151 }
144152 nsName := types.NamespacedName {
145153 Namespace : btp .Namespace ,
146- Name : string (btp . Spec . Validation . CACertificateRefs [ 0 ] .Name ),
154+ Name : string (selectedCertRef .Name ),
147155 }
148- if err := configMapResolver .resolve (nsName ); err != nil {
149- path := field .NewPath ("tls.cacertrefs[0]" )
150- return field .Invalid (path , btp .Spec .Validation .CACertificateRefs [0 ], err .Error ())
156+
157+ if selectedCertRef .Kind == "ConfigMap" {
158+ if err := configMapResolver .resolve (nsName ); err != nil {
159+ path := field .NewPath ("tls.cacertrefs[0]" )
160+ return field .Invalid (path , selectedCertRef , err .Error ())
161+ }
162+ } else if selectedCertRef .Kind == "Secret" {
163+ if err := secretResolver .resolve (nsName ); err != nil {
164+ path := field .NewPath ("tls.cacertrefs[0]" )
165+ return field .Invalid (path , selectedCertRef , err .Error ())
166+ }
151167 }
152168 return nil
153169}
0 commit comments