Skip to content

Commit e7776e4

Browse files
committed
metrics: Introduce override flags for TLS configuration to support HyperShift
Introducing new flags based on a recommendation from the centralized TLS config enhancement [1] to support HyperShift > When these flags are set by the CPO, they take precedence over any > value the component would read from > apiservers.config.openshift.io/cluster. When they are not set, the > component falls back to its normal behavior of watching the cluster config. [1]: https://github.com/openshift/enhancements/blob/master/enhancements/security/centralized-tls-config.md
1 parent 1d7a70d commit e7776e4

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

cmd/cluster-version-operator/start.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"strings"
56

67
"github.com/spf13/cobra"
78

9+
cliflag "k8s.io/component-base/cli/flag"
810
"k8s.io/klog/v2"
911

1012
"github.com/openshift/cluster-version-operator/pkg/start"
@@ -37,6 +39,8 @@ func init() {
3739
cmd.PersistentFlags().StringVar(&opts.ReleaseImage, "release-image", opts.ReleaseImage, "The Openshift release image url.")
3840
cmd.PersistentFlags().StringVar(&opts.MetricsOptions.ServingCertFile, "serving-cert-file", opts.MetricsOptions.ServingCertFile, "The X.509 certificate file for serving metrics over HTTPS. You must set both --serving-cert-file and --serving-key-file unless you set --listen empty.")
3941
cmd.PersistentFlags().StringVar(&opts.MetricsOptions.ServingKeyFile, "serving-key-file", opts.MetricsOptions.ServingKeyFile, "The X.509 key file for serving metrics over HTTPS. You must set both --serving-cert-file and --serving-key-file unless you set --listen empty.")
42+
cmd.PersistentFlags().StringVar(&opts.MetricsOptions.TLSMinVersionOverride, "tls-min-version", opts.MetricsOptions.TLSMinVersionOverride, "Minimum TLS version supported. When set, overrides the value from the central TLS profile. Possible values: "+strings.Join(cliflag.TLSPossibleVersions(), ", "))
43+
cmd.PersistentFlags().StringSliceVar(&opts.MetricsOptions.TLSCipherSuitesOverride, "tls-cipher-suites", opts.MetricsOptions.TLSCipherSuitesOverride, "Comma-separated list of cipher suites for the server. When set, overrides the value from the central TLS profile. Accepts the cipher suite names defined by Go's crypto/tls package.")
4044
cmd.PersistentFlags().StringVar(&opts.PromQLTarget.CABundleFile, "metrics-ca-bundle-file", opts.PromQLTarget.CABundleFile, "The service CA bundle file containing one or more X.509 certificate files for validating certificates generated from the service CA for the respective remote PromQL query service.")
4145
cmd.PersistentFlags().StringVar(&opts.PromQLTarget.BearerTokenFile, "metrics-token-file", opts.PromQLTarget.BearerTokenFile, "The bearer token file used to access the remote PromQL query service.")
4246
cmd.PersistentFlags().StringVar(&opts.PromQLTarget.KubeSvc.Namespace, "metrics-namespace", opts.PromQLTarget.KubeSvc.Namespace, "The name of the namespace where the the remote PromQL query service resides. Must be specified when --use-dns-for-services is disabled.")

pkg/cvo/metrics.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/client-go/rest"
2525
"k8s.io/client-go/tools/cache"
2626
"k8s.io/client-go/tools/record"
27+
cliflag "k8s.io/component-base/cli/flag"
2728
"k8s.io/klog/v2"
2829

2930
configv1 "github.com/openshift/api/config/v1"
@@ -326,6 +327,53 @@ type MetricsOptions struct {
326327

327328
DisableAuthentication bool
328329
DisableAuthorization bool
330+
331+
// TLSMinVersionOverride is the minimum TLS version supported.
332+
// When set, it takes precedence over the central TLS profile.
333+
TLSMinVersionOverride string
334+
335+
// TLSCipherSuitesOverride is the list of allowed cipher suites for the server.
336+
// When set, it takes precedence over the central TLS profile.
337+
TLSCipherSuitesOverride []string
338+
}
339+
340+
// ValidateTLSOptions validates the TLS configuration options.
341+
func (o *MetricsOptions) ValidateTLSOptions() error {
342+
if o.TLSMinVersionOverride != "" {
343+
if _, err := cliflag.TLSVersion(o.TLSMinVersionOverride); err != nil {
344+
return fmt.Errorf("invalid --tls-min-version %q: %w (valid values: %v)", o.TLSMinVersionOverride, err, cliflag.TLSPossibleVersions())
345+
}
346+
}
347+
348+
if len(o.TLSCipherSuitesOverride) > 0 {
349+
if _, err := cliflag.TLSCipherSuites(o.TLSCipherSuitesOverride); err != nil {
350+
return fmt.Errorf("invalid --tls-cipher-suites: %w", err)
351+
}
352+
}
353+
354+
return nil
355+
}
356+
357+
// ApplyTLSOptions applies the TLS configuration options to the provided tls.Config.
358+
// When flags are set, they override the corresponding settings from the central TLS profile.
359+
func (o *MetricsOptions) ApplyTLSOptions(config *tls.Config) error {
360+
if o.TLSMinVersionOverride != "" {
361+
minVersion, err := cliflag.TLSVersion(o.TLSMinVersionOverride)
362+
if err != nil {
363+
return fmt.Errorf("invalid --tls-min-version %q: %w", o.TLSMinVersionOverride, err)
364+
}
365+
config.MinVersion = minVersion
366+
}
367+
368+
if len(o.TLSCipherSuitesOverride) > 0 {
369+
cipherSuites, err := cliflag.TLSCipherSuites(o.TLSCipherSuitesOverride)
370+
if err != nil {
371+
return fmt.Errorf("invalid --tls-cipher-suites: %w", err)
372+
}
373+
config.CipherSuites = cipherSuites
374+
}
375+
376+
return nil
329377
}
330378

331379
// RunMetrics launches an HTTPS server bound to listenAddress serving
@@ -344,6 +392,17 @@ func RunMetrics(runContext context.Context, shutdownContext context.Context, res
344392
return errors.New("invalid configuration: cannot enable authorization without authentication")
345393
}
346394

395+
if err := options.ValidateTLSOptions(); err != nil {
396+
return fmt.Errorf("invalid TLS configuration: %w", err)
397+
}
398+
399+
if options.TLSMinVersionOverride != "" {
400+
klog.Infof("TLS min version flag set to %s, will override central TLS profile", options.TLSMinVersionOverride)
401+
}
402+
if len(options.TLSCipherSuitesOverride) > 0 {
403+
klog.Infof("TLS cipher suites flag set to %v, will override central TLS profile", options.TLSCipherSuitesOverride)
404+
}
405+
347406
// Prepare synchronization for to-be created go routines
348407
metricsContext, metricsContextCancel := context.WithCancel(runContext)
349408
defer metricsContextCancel()
@@ -474,6 +533,11 @@ func RunMetrics(runContext context.Context, shutdownContext context.Context, res
474533
lastApplier = applier
475534
applier.applyTLSProfile(config)
476535

536+
// Then apply flag-based overrides
537+
if err := options.ApplyTLSOptions(config); err != nil {
538+
return nil, fmt.Errorf("failed to apply TLS options: %w", err)
539+
}
540+
477541
return config, nil
478542
},
479543
})

0 commit comments

Comments
 (0)