|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Red Hat, Inc. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +// Package tlssetup builds TLS options for controller-runtime servers from the |
| 17 | +// OpenShift APIServer TLS profile. |
| 18 | +package tlssetup |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "crypto/tls" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + configv1 "github.com/openshift/api/config/v1" |
| 26 | + ostls "github.com/openshift/controller-runtime-common/pkg/tls" |
| 27 | + k8sruntime "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "k8s.io/client-go/rest" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 31 | + |
| 32 | + "github.com/devfile/devworkspace-operator/pkg/infrastructure" |
| 33 | +) |
| 34 | + |
| 35 | +// ServerTLS holds TLS options and the initial APIServer profile/adherence policy for watching. |
| 36 | +type ServerTLS struct { |
| 37 | + TLSOpts []func(*tls.Config) |
| 38 | + InitialTLSProfileSpec configv1.TLSProfileSpec |
| 39 | + InitialTLSAdherencePolicy configv1.TLSAdherencePolicy |
| 40 | +} |
| 41 | + |
| 42 | +// ShouldHonorClusterTLSProfile returns true when the component must honor the cluster |
| 43 | +// TLS profile. Unknown values return true for forward compatibility. |
| 44 | +func ShouldHonorClusterTLSProfile(adherence configv1.TLSAdherencePolicy) bool { |
| 45 | + switch adherence { |
| 46 | + case "", configv1.TLSAdherencePolicyLegacyAdheringComponentsOnly: |
| 47 | + return false |
| 48 | + default: |
| 49 | + // StrictAllComponents or unknown future value → honor the profile |
| 50 | + return true |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// BuildServerTLSOptions fetches TLS settings from the OpenShift API server. |
| 55 | +// Only applies the cluster profile when the tlsAdherence policy requires it. |
| 56 | +func BuildServerTLSOptions(ctx context.Context, cfg *rest.Config, scheme *k8sruntime.Scheme, log logr.Logger) (ServerTLS, error) { |
| 57 | + var result ServerTLS |
| 58 | + |
| 59 | + if !infrastructure.IsOpenShift() { |
| 60 | + log.Info("Not running on OpenShift; using Go default TLS configuration") |
| 61 | + return result, nil |
| 62 | + } |
| 63 | + |
| 64 | + bootstrapClient, err := client.New(cfg, client.Options{Scheme: scheme}) |
| 65 | + if err != nil { |
| 66 | + log.Error(err, "Failed to create bootstrap client for TLS profile fetch; using Go default TLS configuration") |
| 67 | + return result, nil |
| 68 | + } |
| 69 | + |
| 70 | + profile, err := ostls.FetchAPIServerTLSProfile(ctx, bootstrapClient) |
| 71 | + if err != nil { |
| 72 | + log.Error(err, "Failed to fetch TLS profile from APIServer; using Go default TLS configuration") |
| 73 | + return result, nil |
| 74 | + } |
| 75 | + |
| 76 | + adherence, err := ostls.FetchAPIServerTLSAdherencePolicy(ctx, bootstrapClient) |
| 77 | + if err != nil { |
| 78 | + log.Error(err, "Failed to fetch TLS adherence policy from APIServer; using Go default TLS configuration") |
| 79 | + return result, nil |
| 80 | + } |
| 81 | + |
| 82 | + result.InitialTLSProfileSpec = profile |
| 83 | + result.InitialTLSAdherencePolicy = adherence |
| 84 | + |
| 85 | + // Check if we should honor the cluster TLS profile |
| 86 | + if !ShouldHonorClusterTLSProfile(adherence) { |
| 87 | + log.Info("TLS adherence policy does not require strict adherence; using Go default TLS configuration", |
| 88 | + "policy", adherence) |
| 89 | + return result, nil |
| 90 | + } |
| 91 | + |
| 92 | + // Apply the cluster TLS profile |
| 93 | + tlsConfigFn, unsupported := ostls.NewTLSConfigFromProfile(profile) |
| 94 | + if len(unsupported) > 0 { |
| 95 | + log.Info("TLS profile contains ciphers unsupported by Go; they will be ignored", |
| 96 | + "unsupportedCiphers", unsupported) |
| 97 | + } |
| 98 | + |
| 99 | + result.TLSOpts = []func(*tls.Config){tlsConfigFn} |
| 100 | + |
| 101 | + log.Info("Applying cluster TLS profile to metrics and webhook servers", |
| 102 | + "minTLSVersion", profile.MinTLSVersion, |
| 103 | + "cipherCount", len(profile.Ciphers), |
| 104 | + "adherencePolicy", adherence) |
| 105 | + |
| 106 | + return result, nil |
| 107 | +} |
| 108 | + |
| 109 | +// RegisterSecurityProfileWatcher watches the APIServer TLS profile and adherence policy. |
| 110 | +// Calls onCancel to trigger restart when either changes. No-op on non-OpenShift. |
| 111 | +func RegisterSecurityProfileWatcher(mgr manager.Manager, serverTLS ServerTLS, onCancel context.CancelFunc, log logr.Logger) error { |
| 112 | + if !infrastructure.IsOpenShift() { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + // Only set up the watcher if we successfully fetched the initial profile |
| 117 | + if len(serverTLS.TLSOpts) == 0 { |
| 118 | + log.Info("Skipping TLS profile watcher (profile not applied)") |
| 119 | + return nil |
| 120 | + } |
| 121 | + |
| 122 | + watcher := &ostls.SecurityProfileWatcher{ |
| 123 | + Client: mgr.GetClient(), |
| 124 | + InitialTLSProfileSpec: serverTLS.InitialTLSProfileSpec, |
| 125 | + InitialTLSAdherencePolicy: serverTLS.InitialTLSAdherencePolicy, |
| 126 | + OnProfileChange: func(_ context.Context, old, new configv1.TLSProfileSpec) { |
| 127 | + log.Info("TLS security profile changed; initiating graceful restart", |
| 128 | + "oldMinTLSVersion", old.MinTLSVersion, |
| 129 | + "newMinTLSVersion", new.MinTLSVersion) |
| 130 | + onCancel() |
| 131 | + }, |
| 132 | + OnAdherencePolicyChange: func(_ context.Context, old, new configv1.TLSAdherencePolicy) { |
| 133 | + log.Info("TLS adherence policy changed; initiating graceful restart", |
| 134 | + "old", old, |
| 135 | + "new", new) |
| 136 | + onCancel() |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + return watcher.SetupWithManager(mgr) |
| 141 | +} |
0 commit comments