|
| 1 | +/* |
| 2 | + * Copyright 2026 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package tls |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "k8s.io/klog/v2" |
| 26 | + |
| 27 | + ctrltls "github.com/openshift/controller-runtime-common/pkg/tls" |
| 28 | + libgocrypto "github.com/openshift/library-go/pkg/crypto" |
| 29 | + |
| 30 | + nropv1 "github.com/openshift-kni/numaresources-operator/api/v1" |
| 31 | + "github.com/openshift-kni/numaresources-operator/internal/podlist" |
| 32 | + e2eclient "github.com/openshift-kni/numaresources-operator/test/internal/clients" |
| 33 | + "github.com/openshift-kni/numaresources-operator/test/internal/objects" |
| 34 | + intls "github.com/openshift-kni/numaresources-operator/test/internal/tls" |
| 35 | + |
| 36 | + . "github.com/onsi/ginkgo/v2" |
| 37 | + . "github.com/onsi/gomega" |
| 38 | +) |
| 39 | + |
| 40 | +const schedulerSecurePort = "10259" |
| 41 | + |
| 42 | +var _ = Describe("TLS", func() { |
| 43 | + It("should reject TLS connections that are not compatible with the profile - negative test", func(ctx context.Context) { |
| 44 | + By("getting the current OCP TLS profile") |
| 45 | + tlsProfileSpec, err := ctrltls.FetchAPIServerTLSProfile(ctx, e2eclient.Client) |
| 46 | + Expect(err).ToNot(HaveOccurred(), "unable to get TLS profile from APIServer") |
| 47 | + |
| 48 | + tlsConfigFn, _ := ctrltls.NewTLSConfigFromProfile(tlsProfileSpec) |
| 49 | + tlsCfg := &tls.Config{} |
| 50 | + tlsConfigFn(tlsCfg) |
| 51 | + minVersion := tlsCfg.MinVersion |
| 52 | + klog.InfoS("current TLS minimum version", "version", libgocrypto.TLSVersionToNameOrDie(minVersion)) |
| 53 | + |
| 54 | + belowMinVersion, err := intls.TLSVersionBelow(minVersion) |
| 55 | + Expect(err).ToNot(HaveOccurred(), "failed to get TLS version below %s", libgocrypto.TLSVersionToNameOrDie(minVersion)) |
| 56 | + |
| 57 | + By("getting the scheduler deployment and pods") |
| 58 | + nroSchedObj := &nropv1.NUMAResourcesScheduler{} |
| 59 | + nroSchedKey := objects.NROSchedObjectKey() |
| 60 | + Expect(e2eclient.Client.Get(ctx, nroSchedKey, nroSchedObj)).To(Succeed(), "failed to get %q in the cluster", nroSchedKey.String()) |
| 61 | + |
| 62 | + deployment, err := podlist.With(e2eclient.Client).DeploymentByOwnerReference(ctx, nroSchedObj.GetUID()) |
| 63 | + Expect(err).ToNot(HaveOccurred(), "failed to get the deployment") |
| 64 | + Expect(deployment).ToNot(BeNil(), "scheduler deployment not found") |
| 65 | + |
| 66 | + pods, err := podlist.With(e2eclient.Client).ByDeployment(ctx, *deployment) |
| 67 | + Expect(err).ToNot(HaveOccurred(), "failed to get the pods") |
| 68 | + Expect(pods).ToNot(BeEmpty(), "no pods found for the deployment") |
| 69 | + |
| 70 | + schedulerPod := &pods[0] |
| 71 | + |
| 72 | + By(fmt.Sprintf("verifying that TLS connections at version %s are rejected by the server", tls.VersionName(belowMinVersion))) |
| 73 | + err = intls.ProbeMaxTLSVersion(e2eclient.K8sClient, schedulerPod, schedulerSecurePort, belowMinVersion) |
| 74 | + Expect(err).To(HaveOccurred(), "scheduler server should reject TLS connections capped at %s", tls.VersionName(belowMinVersion)) |
| 75 | + Expect(errors.Is(err, intls.ErrTLSHandshakeRejected)).To(BeTrue(), |
| 76 | + "expected TLS handshake rejection, got: %v", err) |
| 77 | + |
| 78 | + By(fmt.Sprintf("verifying that TLS connections with unsupported ciphers are rejected by the server")) |
| 79 | + if minVersion == tls.VersionTLS13 { |
| 80 | + klog.InfoS("TLS 1.3 is not configurable, so we cannot test unsupported ciphers") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + disallowedCipher := intls.FindDisallowedCipher(tlsProfileSpec.Ciphers) |
| 85 | + if disallowedCipher == "" { |
| 86 | + Skip("all known TLS 1.2 ciphers are in the allowed set, nothing to test") |
| 87 | + } |
| 88 | + klog.InfoS("testing with disallowed cipher", "cipher", disallowedCipher) |
| 89 | + err = intls.ProbeTLSCipher(e2eclient.K8sClient, schedulerPod, schedulerSecurePort, disallowedCipher) |
| 90 | + Expect(err).To(HaveOccurred(), "scheduler server should reject connections with disallowed cipher %s", disallowedCipher) |
| 91 | + Expect(errors.Is(err, intls.ErrTLSHandshakeRejected)).To(BeTrue(), "expected TLS handshake rejection for cipher %s, got: %v", disallowedCipher, err) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should adhere to openshift TLS profile - positive test", func(ctx context.Context) { |
| 95 | + By("getting the current OCP TLS profile") |
| 96 | + tlsProfileSpec, err := ctrltls.FetchAPIServerTLSProfile(ctx, e2eclient.Client) |
| 97 | + Expect(err).ToNot(HaveOccurred(), "unable to get TLS profile from APIServer") |
| 98 | + |
| 99 | + tlsConfigFn, _ := ctrltls.NewTLSConfigFromProfile(tlsProfileSpec) |
| 100 | + tlsCfg := &tls.Config{} |
| 101 | + tlsConfigFn(tlsCfg) |
| 102 | + minVersion := tlsCfg.MinVersion |
| 103 | + klog.InfoS("current TLS minimum version", "version", libgocrypto.TLSVersionToNameOrDie(minVersion)) |
| 104 | + |
| 105 | + By("getting the scheduler deployment and pods") |
| 106 | + nroSchedObj := &nropv1.NUMAResourcesScheduler{} |
| 107 | + nroSchedKey := objects.NROSchedObjectKey() |
| 108 | + Expect(e2eclient.Client.Get(ctx, nroSchedKey, nroSchedObj)).To(Succeed(), "failed to get %q in the cluster", nroSchedKey.String()) |
| 109 | + |
| 110 | + deployment, err := podlist.With(e2eclient.Client).DeploymentByOwnerReference(ctx, nroSchedObj.GetUID()) |
| 111 | + Expect(err).ToNot(HaveOccurred(), "failed to get the deployment") |
| 112 | + Expect(deployment).ToNot(BeNil(), "scheduler deployment not found") |
| 113 | + |
| 114 | + pods, err := podlist.With(e2eclient.Client).ByDeployment(ctx, *deployment) |
| 115 | + Expect(err).ToNot(HaveOccurred(), "failed to get the pods") |
| 116 | + Expect(pods).ToNot(BeEmpty(), "no pods found for the deployment") |
| 117 | + |
| 118 | + schedulerPod := &pods[0] |
| 119 | + |
| 120 | + By("probing the scheduler HTTPS endpoint to verify TLS connection is accepted") |
| 121 | + gotVersion, gotCipherID, err := intls.ProbeTLSSettings(e2eclient.K8sClient, schedulerPod, schedulerSecurePort) |
| 122 | + Expect(err).ToNot(HaveOccurred(), "failed to probe TLS settings on pod %q", schedulerPod.Name) |
| 123 | + |
| 124 | + gotVersionName := tls.VersionName(gotVersion) |
| 125 | + gotCipherName := tls.CipherSuiteName(gotCipherID) |
| 126 | + klog.InfoS("negotiated TLS settings", "version", gotVersionName, "cipher", gotCipherName) |
| 127 | + |
| 128 | + Expect(gotVersion).To(BeNumerically(">=", minVersion), "negotiated TLS version %s is below the expected minimum %s", gotVersionName, libgocrypto.TLSVersionToNameOrDie(minVersion)) |
| 129 | + |
| 130 | + // TLS 1.3 cipher suites are not configurable and won't appear in |
| 131 | + // the profile's list; only validate for TLS 1.2 and below. |
| 132 | + allowedCipherNames := libgocrypto.OpenSSLToIANACipherSuites(tlsProfileSpec.Ciphers) |
| 133 | + if gotVersion < tls.VersionTLS13 { |
| 134 | + Expect(gotCipherName).ToNot(BeEmpty(), "could not resolve negotiated cipher suite ID 0x%04x", gotCipherID) |
| 135 | + Expect(allowedCipherNames).To(ContainElement(gotCipherName), "negotiated cipher %s is not in the allowed set %v", gotCipherName, tlsProfileSpec.Ciphers) |
| 136 | + } |
| 137 | + }) |
| 138 | +}) |
0 commit comments