Skip to content

Commit 81d973e

Browse files
committed
add tls.backend field to specify gateway cert
1 parent b52a208 commit 81d973e

File tree

14 files changed

+1440
-853
lines changed

14 files changed

+1440
-853
lines changed

internal/controller/manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ func StartManager(cfg config.Config) error {
139139
GenericValidator: genericValidator,
140140
PolicyValidator: policyManager,
141141
},
142-
EventRecorder: recorder,
143-
MustExtractGVK: mustExtractGVK,
144-
PlusSecrets: plusSecrets,
142+
EventRecorder: recorder,
143+
MustExtractGVK: mustExtractGVK,
144+
PlusSecrets: plusSecrets,
145+
ExperimentalFeatures: cfg.ExperimentalFeatures,
145146
})
146147

147148
var handlerCollector handlerMetricsCollector = collectors.NewControllerNoopCollector()

internal/controller/nginx/config/base_http_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var baseHTTPTemplate = gotemplate.Must(gotemplate.New("baseHttp").Parse(baseHTTP
1212

1313
type httpConfig struct {
1414
DNSResolver *dataplane.DNSResolverConfig
15+
GatewaySecretID dataplane.SSLKeyPairID
1516
Includes []shared.Include
1617
NginxReadinessProbePort int32
1718
IPFamily shared.IPFamily
@@ -27,6 +28,7 @@ func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
2728
NginxReadinessProbePort: conf.BaseHTTPConfig.NginxReadinessProbePort,
2829
IPFamily: getIPFamily(conf.BaseHTTPConfig),
2930
DNSResolver: conf.BaseHTTPConfig.DNSResolver,
31+
GatewaySecretID: conf.BaseHTTPConfig.GatewaySecretID,
3032
}
3133

3234
results := make([]executeResult, 0, len(includes)+1)

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ server {
4848
}
4949
}
5050
51+
{{- if $.GatewaySecretID }}
52+
# Gateway Certificate
53+
proxy_ssl_certificate /etc/nginx/secrets/{{ $.GatewaySecretID }}.pem;
54+
proxy_ssl_certificate_key /etc/nginx/secrets/{{ $.GatewaySecretID }}.pem;
55+
{{- end }}
56+
5157
{{ range $i := .Includes -}}
5258
include {{ $i.Name }};
5359
{{ end -}}

internal/controller/nginx/config/base_http_config_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,49 @@ func TestExecuteBaseHttp_DNSResolver(t *testing.T) {
284284
})
285285
}
286286
}
287+
288+
func TestExecuteBaseHttp_GatewaySecretID(t *testing.T) {
289+
t.Parallel()
290+
291+
tests := []struct {
292+
name string
293+
expectedConfig string
294+
conf dataplane.Configuration
295+
}{
296+
{
297+
name: "with GatewaySecretID",
298+
conf: dataplane.Configuration{
299+
BaseHTTPConfig: dataplane.BaseHTTPConfig{
300+
GatewaySecretID: "client-secret",
301+
},
302+
},
303+
expectedConfig: "proxy_ssl_certificate /etc/nginx/secrets/client-secret.pem;" +
304+
"\nproxy_ssl_certificate_key /etc/nginx/secrets/client-secret.pem;",
305+
},
306+
{
307+
name: "without GatewaySecretID",
308+
conf: dataplane.Configuration{
309+
BaseHTTPConfig: dataplane.BaseHTTPConfig{
310+
GatewaySecretID: "",
311+
},
312+
},
313+
expectedConfig: "",
314+
},
315+
}
316+
317+
for _, test := range tests {
318+
t.Run(test.name, func(t *testing.T) {
319+
t.Parallel()
320+
g := NewWithT(t)
321+
322+
res := executeBaseHTTPConfig(test.conf)
323+
g.Expect(res).To(HaveLen(1))
324+
325+
httpConfig := string(res[0].data)
326+
327+
if test.expectedConfig != "" {
328+
g.Expect(httpConfig).To(ContainSubstring(test.expectedConfig))
329+
}
330+
})
331+
}
332+
}

internal/controller/state/change_processor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type ChangeProcessorConfig struct {
6363
GatewayCtlrName string
6464
// GatewayClassName is the name of the GatewayClass resource.
6565
GatewayClassName string
66+
// ExperimentalFeatures indicates whether experimental features are enabled.
67+
ExperimentalFeatures bool
6668
}
6769

6870
// ChangeProcessorImpl is an implementation of ChangeProcessor.
@@ -269,6 +271,7 @@ func (c *ChangeProcessorImpl) Process() *graph.Graph {
269271
c.cfg.PlusSecrets,
270272
c.cfg.Validators,
271273
c.cfg.Logger,
274+
c.cfg.ExperimentalFeatures,
272275
)
273276

274277
return c.latestGraph

internal/controller/state/conditions/conditions.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ const (
139139
// parametersRef resource is invalid.
140140
GatewayReasonParamsRefInvalid v1.GatewayConditionReason = "ParametersRefInvalid"
141141

142+
// GatewayReasonSecretRefInvalid is used with the "GatewayResolvedRefs" condition when the
143+
// secretRef resource is invalid.
144+
GatewayReasonSecretRefInvalid v1.GatewayConditionReason = "SecretRefInvalid"
145+
146+
// GatewayReasonSecretRefNotPermitted is used with the "GatewayResolvedRefs" condition when the
147+
// secretRef resource is not permitted by any ReferenceGrant.
148+
GatewayReasonSecretRefNotPermitted v1.GatewayConditionReason = "SecretRefNotPermitted"
149+
142150
// PolicyReasonAncestorLimitReached is used with the "PolicyAccepted" condition when a policy
143151
// cannot be applied because the ancestor status list has reached the maximum size of 16.
144152
PolicyReasonAncestorLimitReached v1alpha2.PolicyConditionReason = "AncestorLimitReached"
@@ -297,6 +305,27 @@ func NewGatewayClassUnsupportedVersion(recommendedVersion string) []Condition {
297305
}
298306
}
299307

308+
// NewGatewaySecretRefNotPermitted returns Condition that indicates that the Gateway references a TLS secret that is not
309+
// permitted by any ReferenceGrant.
310+
func NewGatewaySecretRefNotPermitted(msg string) Condition {
311+
return Condition{
312+
Type: string(GatewayReasonResolvedRefs),
313+
Status: metav1.ConditionFalse,
314+
Reason: string(GatewayReasonSecretRefNotPermitted),
315+
Message: msg,
316+
}
317+
}
318+
319+
// NewGatewaySecretRefInvalid returns Condition that indicates that the Gateway references a TLS secret that is invalid.
320+
func NewGatewaySecretRefInvalid(msg string) Condition {
321+
return Condition{
322+
Type: string(GatewayReasonResolvedRefs),
323+
Status: metav1.ConditionFalse,
324+
Reason: string(GatewayReasonSecretRefInvalid),
325+
Message: msg,
326+
}
327+
}
328+
300329
// NewGatewayClassConflict returns a Condition that indicates that the GatewayClass is not accepted
301330
// due to a conflict with another GatewayClass.
302331
func NewGatewayClassConflict() Condition {

internal/controller/state/dataplane/configuration.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ func BuildConfiguration(
8181
gateway,
8282
serviceResolver,
8383
g.ReferencedServices,
84-
baseHTTPConfig.IPFamily),
84+
baseHTTPConfig.IPFamily,
85+
),
8586
BackendGroups: backendGroups,
86-
SSLKeyPairs: buildSSLKeyPairs(g.ReferencedSecrets, gateway.Listeners),
87+
SSLKeyPairs: buildSSLKeyPairs(g.ReferencedSecrets, gateway),
8788
CertBundles: buildCertBundles(
8889
buildRefCertificateBundles(g.ReferencedSecrets, g.ReferencedCaCertConfigMaps),
8990
backendGroups,
@@ -248,14 +249,14 @@ func buildStreamUpstreams(
248249
}
249250

250251
// buildSSLKeyPairs builds the SSLKeyPairs from the Secrets. It will only include Secrets that are referenced by
251-
// valid listeners, so that we don't include unused Secrets in the configuration of the data plane.
252+
// valid gateway and its listeners, so that we don't include unused Secrets in the configuration of the data plane.
252253
func buildSSLKeyPairs(
253254
secrets map[types.NamespacedName]*graph.Secret,
254-
listeners []*graph.Listener,
255+
gateway *graph.Gateway,
255256
) map[SSLKeyPairID]SSLKeyPair {
256257
keyPairs := make(map[SSLKeyPairID]SSLKeyPair)
257258

258-
for _, l := range listeners {
259+
for _, l := range gateway.Listeners {
259260
if l.Valid && l.ResolvedSecret != nil {
260261
id := generateSSLKeyPairID(*l.ResolvedSecret)
261262
secret := secrets[*l.ResolvedSecret]
@@ -268,6 +269,15 @@ func buildSSLKeyPairs(
268269
}
269270
}
270271

272+
if gateway.Valid && gateway.SecretRef != nil {
273+
id := generateSSLKeyPairID(*gateway.SecretRef)
274+
secret := secrets[*gateway.SecretRef]
275+
keyPairs[id] = SSLKeyPair{
276+
Cert: secret.CertBundle.Cert.TLSCert,
277+
Key: secret.CertBundle.Cert.TLSPrivateKey,
278+
}
279+
}
280+
271281
return keyPairs
272282
}
273283

@@ -1019,6 +1029,10 @@ func buildBaseHTTPConfig(
10191029
NginxReadinessProbePort: DefaultNginxReadinessProbePort,
10201030
}
10211031

1032+
if gateway.Valid && gateway.SecretRef != nil {
1033+
baseConfig.GatewaySecretID = generateSSLKeyPairID(*gateway.SecretRef)
1034+
}
1035+
10221036
// safe to access EffectiveNginxProxy since we only call this function when the Gateway is not nil.
10231037
np := gateway.EffectiveNginxProxy
10241038
if np == nil {
@@ -1042,8 +1056,20 @@ func buildBaseHTTPConfig(
10421056
}
10431057
}
10441058

1059+
if port := getNginxReadinessProbePort(np); port != 0 {
1060+
baseConfig.NginxReadinessProbePort = port
1061+
}
1062+
10451063
baseConfig.RewriteClientIPSettings = buildRewriteClientIPConfig(np.RewriteClientIP)
10461064

1065+
baseConfig.DNSResolver = buildDNSResolverConfig(np.DNSResolver)
1066+
1067+
return baseConfig
1068+
}
1069+
1070+
func getNginxReadinessProbePort(np *graph.EffectiveNginxProxy) int32 {
1071+
var port int32
1072+
10471073
if np.Kubernetes != nil {
10481074
var containerSpec *ngfAPIv1alpha2.ContainerSpec
10491075
if np.Kubernetes.Deployment != nil {
@@ -1052,13 +1078,10 @@ func buildBaseHTTPConfig(
10521078
containerSpec = &np.Kubernetes.DaemonSet.Container
10531079
}
10541080
if containerSpec != nil && containerSpec.ReadinessProbe != nil && containerSpec.ReadinessProbe.Port != nil {
1055-
baseConfig.NginxReadinessProbePort = *containerSpec.ReadinessProbe.Port
1081+
port = *containerSpec.ReadinessProbe.Port
10561082
}
10571083
}
1058-
1059-
baseConfig.DNSResolver = buildDNSResolverConfig(np.DNSResolver)
1060-
1061-
return baseConfig
1084+
return port
10621085
}
10631086

10641087
// buildBaseStreamConfig generates the base stream context config that should be applied to all stream servers.

0 commit comments

Comments
 (0)