Skip to content

Commit ad8272f

Browse files
committed
feat: use knative.dev/pkg/tls for queue-proxy TLS configuration
Replace the hardcoded tls.VersionTLS13 in queue-proxy's TLS server with the shared knative.dev/pkg/tls package, allowing TLS settings to be configured via QUEUE_PROXY_TLS_MIN_VERSION, QUEUE_PROXY_TLS_MAX_VERSION, QUEUE_PROXY_TLS_CIPHER_SUITES, and QUEUE_PROXY_TLS_CURVE_PREFERENCES environment variables. The default remains TLS 1.3 when no env var is set. Add four new keys to the config-deployment ConfigMap (queue-sidecar-tls-min-version, queue-sidecar-tls-max-version, queue-sidecar-tls-cipher-suites, queue-sidecar-tls-curve-preferences) and forward them as QUEUE_PROXY_TLS_* environment variables in makeQueueContainer. This allows cluster admins to configure the queue-proxy's TLS server via the same ConfigMap used for other queue-proxy settings (like queue-sidecar-rootca), since the operator cannot inject env vars into the dynamically created sidecar via manifestival. Signed-off-by: Mikhail Fedosin <mfedosin@redhat.com>
1 parent 42495d4 commit ad8272f

File tree

7 files changed

+123
-30
lines changed

7 files changed

+123
-30
lines changed

config/core/configmaps/deployment.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ metadata:
2222
app.kubernetes.io/component: controller
2323
app.kubernetes.io/version: devel
2424
annotations:
25-
knative.dev/example-checksum: "b99000ec"
25+
knative.dev/example-checksum: "555b4826"
2626
data:
2727
# This is the Go import path for the binary that is containerized
2828
# and substituted here.
@@ -92,6 +92,25 @@ data:
9292
# If omitted, or empty, no rootCA is added to the golang rootCAs
9393
queue-sidecar-rootca: ""
9494
95+
# Sets the minimum TLS version for the queue proxy sidecar's TLS server.
96+
# Accepted values: "1.2", "1.3". Default is "1.3" if not specified.
97+
queue-sidecar-tls-min-version: ""
98+
99+
# Sets the maximum TLS version for the queue proxy sidecar's TLS server.
100+
# Accepted values: "1.2", "1.3". If omitted, the Go default is used.
101+
queue-sidecar-tls-max-version: ""
102+
103+
# Sets the cipher suites for the queue proxy sidecar's TLS server.
104+
# Comma-separated list of cipher suite names (e.g. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256").
105+
# If omitted, the Go default cipher suites are used.
106+
# Note: cipher suites are not configurable in TLS 1.3.
107+
queue-sidecar-tls-cipher-suites: ""
108+
109+
# Sets the elliptic curve preferences for the queue proxy sidecar's TLS server.
110+
# Comma-separated list of curve names (e.g. "X25519,CurveP256").
111+
# If omitted, the Go default curves are used.
112+
queue-sidecar-tls-curve-preferences: ""
113+
95114
# If set, it automatically configures pod anti-affinity requirements for all Knative services.
96115
# It employs the `preferredDuringSchedulingIgnoredDuringExecution` weighted pod affinity term,
97116
# aligning with the Knative revision label. It yields the configuration below in all workloads' deployments:

pkg/deployment/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ const (
7474
queueSidecarTokenAudiencesKey = "queue-sidecar-token-audiences"
7575
queueSidecarRooCAKey = "queue-sidecar-rootca"
7676

77+
// queueSidecar TLS configuration keys.
78+
queueSidecarTLSMinVersionKey = "queue-sidecar-tls-min-version"
79+
queueSidecarTLSMaxVersionKey = "queue-sidecar-tls-max-version"
80+
queueSidecarTLSCipherSuitesKey = "queue-sidecar-tls-cipher-suites"
81+
queueSidecarTLSCurvePreferencesKey = "queue-sidecar-tls-curve-preferences"
82+
7783
defaultAffinityTypeKey = "default-affinity-type"
7884
defaultAffinityTypeValue = PreferSpreadRevisionOverNodes
7985

@@ -202,6 +208,11 @@ func NewConfigFromMap(configMap map[string]string) (*Config, error) {
202208
cm.AsStringSet(queueSidecarTokenAudiencesKey, &nc.QueueSidecarTokenAudiences),
203209
cm.AsString(queueSidecarRooCAKey, &nc.QueueSidecarRootCA),
204210

211+
cm.AsString(queueSidecarTLSMinVersionKey, &nc.QueueSidecarTLSMinVersion),
212+
cm.AsString(queueSidecarTLSMaxVersionKey, &nc.QueueSidecarTLSMaxVersion),
213+
cm.AsString(queueSidecarTLSCipherSuitesKey, &nc.QueueSidecarTLSCipherSuites),
214+
cm.AsString(queueSidecarTLSCurvePreferencesKey, &nc.QueueSidecarTLSCurvePreferences),
215+
205216
cm.AsString(RuntimeClassNameKey, &runtimeClassNames),
206217

207218
cm.AsBool(podIsAlwaysSchedulableKey, &nc.PodIsAlwaysSchedulable),
@@ -308,6 +319,18 @@ type Config struct {
308319
// QueueSidecarRootCA is a root certificate to be trusted by the queue proxy sidecar qpoptions.
309320
QueueSidecarRootCA string
310321

322+
// QueueSidecarTLSMinVersion is the minimum TLS version for the queue proxy sidecar (e.g. "1.2", "1.3").
323+
QueueSidecarTLSMinVersion string
324+
325+
// QueueSidecarTLSMaxVersion is the maximum TLS version for the queue proxy sidecar (e.g. "1.2", "1.3").
326+
QueueSidecarTLSMaxVersion string
327+
328+
// QueueSidecarTLSCipherSuites is a comma-separated list of cipher suites for the queue proxy sidecar.
329+
QueueSidecarTLSCipherSuites string
330+
331+
// QueueSidecarTLSCurvePreferences is a comma-separated list of elliptic curves for the queue proxy sidecar.
332+
QueueSidecarTLSCurvePreferences string
333+
311334
// DefaultAffinityType is a string that controls what affinity rules will be automatically
312335
// applied to the PodSpec of all Knative services.
313336
DefaultAffinityType AffinityType

pkg/deployment/config_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,28 @@ kata:
472472
podIsAlwaysSchedulableKey: "true",
473473
QueueSidecarImageKey: defaultSidecarImage,
474474
},
475+
}, {
476+
name: "controller configuration with queue sidecar TLS settings",
477+
wantConfig: &Config{
478+
RegistriesSkippingTagResolving: sets.New("kind.local", "ko.local", "dev.local"),
479+
DigestResolutionTimeout: digestResolutionTimeoutDefault,
480+
QueueSidecarImage: defaultSidecarImage,
481+
QueueSidecarCPURequest: &QueueSidecarCPURequestDefault,
482+
QueueSidecarTokenAudiences: sets.New(""),
483+
ProgressDeadline: ProgressDeadlineDefault,
484+
DefaultAffinityType: defaultAffinityTypeValue,
485+
QueueSidecarTLSMinVersion: "1.2",
486+
QueueSidecarTLSMaxVersion: "1.3",
487+
QueueSidecarTLSCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
488+
QueueSidecarTLSCurvePreferences: "X25519,CurveP256",
489+
},
490+
data: map[string]string{
491+
QueueSidecarImageKey: defaultSidecarImage,
492+
queueSidecarTLSMinVersionKey: "1.2",
493+
queueSidecarTLSMaxVersionKey: "1.3",
494+
queueSidecarTLSCipherSuitesKey: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
495+
queueSidecarTLSCurvePreferencesKey: "X25519,CurveP256",
496+
},
475497
}}
476498

477499
for _, tt := range configTests {

pkg/queue/sharedmain/main.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package sharedmain
1818

1919
import (
2020
"context"
21-
"crypto/tls"
2221
"encoding/json"
2322
"errors"
2423
"fmt"
@@ -31,6 +30,7 @@ import (
3130

3231
netproxy "knative.dev/networking/pkg/http/proxy"
3332
pkghandler "knative.dev/pkg/network/handlers"
33+
knativetls "knative.dev/pkg/tls"
3434
"knative.dev/serving/pkg/activator"
3535

3636
"github.com/kelseyhightower/envconfig"
@@ -265,23 +265,10 @@ func Main(opts ...Option) error {
265265
httpServers["profile"] = pprof.Server
266266
}
267267

268-
tlsServers := make(map[string]*http.Server)
268+
var tlsServer *http.Server
269269
var certWatcher *certificate.CertWatcher
270270
var err error
271271

272-
if tlsEnabled {
273-
tlsServers["main"] = mainServer(":"+env.QueueServingTLSPort, mainHandler)
274-
// Keep admin server on HTTP even with TLS enabled since it's only accessed locally by kubelet
275-
276-
certWatcher, err = certificate.NewCertWatcher(certPath, keyPath, 1*time.Minute, logger)
277-
if err != nil {
278-
logger.Fatal("failed to create certWatcher", zap.Error(err))
279-
}
280-
defer certWatcher.Stop()
281-
}
282-
283-
logger.Info("Starting queue-proxy")
284-
285272
errCh := make(chan error)
286273
for name, server := range httpServers {
287274
go func(name string, s *http.Server) {
@@ -292,20 +279,34 @@ func Main(opts ...Option) error {
292279
}
293280
}(name, server)
294281
}
295-
for name, server := range tlsServers {
296-
go func(name string, s *http.Server) {
297-
logger.Info("Starting tls server ", name, s.Addr)
298-
s.TLSConfig = &tls.Config{
299-
GetCertificate: certWatcher.GetCertificate,
300-
MinVersion: tls.VersionTLS13,
301-
}
282+
283+
if tlsEnabled {
284+
tlsServer = mainServer(":"+env.QueueServingTLSPort, mainHandler)
285+
// Keep admin server on HTTP even with TLS enabled since it's only accessed locally by kubelet
286+
287+
certWatcher, err = certificate.NewCertWatcher(certPath, keyPath, 1*time.Minute, logger)
288+
if err != nil {
289+
logger.Fatal("failed to create certWatcher", zap.Error(err))
290+
}
291+
defer certWatcher.Stop()
292+
293+
tlsCfg, err := knativetls.DefaultConfigFromEnv("QUEUE_PROXY_")
294+
if err != nil {
295+
logger.Fatalw("Failed to read TLS configuration from environment", zap.Error(err))
296+
}
297+
go func() {
298+
logger.Info("Starting tls server main ", tlsServer.Addr)
299+
tlsServer.TLSConfig = tlsCfg
300+
tlsServer.TLSConfig.GetCertificate = certWatcher.GetCertificate
302301
// Don't forward ErrServerClosed as that indicates we're already shutting down.
303-
if err := s.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
304-
errCh <- fmt.Errorf("%s server failed to serve: %w", name, err)
302+
if err := tlsServer.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
303+
errCh <- fmt.Errorf("main tls server failed to serve: %w", err)
305304
}
306-
}(name, server)
305+
}()
307306
}
308307

308+
logger.Info("Starting queue-proxy")
309+
309310
// Blocks until we actually receive a TERM signal or one of the servers
310311
// exits unexpectedly. We fold both signals together because we only want
311312
// to act on the first of those to reach here.
@@ -326,10 +327,10 @@ func Main(opts ...Option) error {
326327
logger.Errorw("Failed to shutdown server", zap.String("server", name), zap.Error(err))
327328
}
328329
}
329-
for name, srv := range tlsServers {
330-
logger.Info("Shutting down server: ", name)
331-
if err := srv.Shutdown(ctx); err != nil {
332-
logger.Errorw("Failed to shutdown server", zap.String("server", name), zap.Error(err))
330+
if tlsServer != nil {
331+
logger.Info("Shutting down server: main tls")
332+
if err := tlsServer.Shutdown(ctx); err != nil {
333+
logger.Errorw("Failed to shutdown server", zap.String("server", "main tls"), zap.Error(err))
333334
}
334335
}
335336

pkg/reconciler/revision/resources/deploy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ var (
158158
}, {
159159
Name: "ROOT_CA",
160160
Value: "",
161+
}, {
162+
Name: "QUEUE_PROXY_TLS_MIN_VERSION",
163+
Value: "",
164+
}, {
165+
Name: "QUEUE_PROXY_TLS_MAX_VERSION",
166+
Value: "",
167+
}, {
168+
Name: "QUEUE_PROXY_TLS_CIPHER_SUITES",
169+
Value: "",
170+
}, {
171+
Name: "QUEUE_PROXY_TLS_CURVE_PREFERENCES",
172+
Value: "",
161173
}, {
162174
Name: "ENABLE_MULTI_CONTAINER_PROBES",
163175
Value: "false",

pkg/reconciler/revision/resources/queue.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,18 @@ func makeQueueContainer(rev *v1.Revision, cfg *config.Config) (*corev1.Container
428428
}, {
429429
Name: "ROOT_CA",
430430
Value: cfg.Deployment.QueueSidecarRootCA,
431+
}, {
432+
Name: "QUEUE_PROXY_TLS_MIN_VERSION",
433+
Value: cfg.Deployment.QueueSidecarTLSMinVersion,
434+
}, {
435+
Name: "QUEUE_PROXY_TLS_MAX_VERSION",
436+
Value: cfg.Deployment.QueueSidecarTLSMaxVersion,
437+
}, {
438+
Name: "QUEUE_PROXY_TLS_CIPHER_SUITES",
439+
Value: cfg.Deployment.QueueSidecarTLSCipherSuites,
440+
}, {
441+
Name: "QUEUE_PROXY_TLS_CURVE_PREFERENCES",
442+
Value: cfg.Deployment.QueueSidecarTLSCurvePreferences,
431443
}, {
432444
Name: "ENABLE_MULTI_CONTAINER_PROBES",
433445
Value: strconv.FormatBool(multiContainerProbingEnabled),

pkg/reconciler/revision/resources/queue_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,10 @@ var defaultEnv = map[string]string{
11221122
"SYSTEM_NAMESPACE": system.Namespace(),
11231123
"USER_PORT": strconv.Itoa(v1.DefaultUserPort),
11241124
"ROOT_CA": "",
1125+
"QUEUE_PROXY_TLS_MIN_VERSION": "",
1126+
"QUEUE_PROXY_TLS_MAX_VERSION": "",
1127+
"QUEUE_PROXY_TLS_CIPHER_SUITES": "",
1128+
"QUEUE_PROXY_TLS_CURVE_PREFERENCES": "",
11251129
"ENABLE_MULTI_CONTAINER_PROBES": "false",
11261130
"OBSERVABILITY_CONFIG": `{"tracing":{},"metrics":{},"runtime":{},"requestMetrics":{}}`,
11271131
}

0 commit comments

Comments
 (0)