|
| 1 | +/* |
| 2 | +Copyright 2023 Red Hat |
| 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 | +package functional |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/google/uuid" |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + "github.com/openstack-k8s-operators/lib-common/modules/common/service" |
| 23 | + "github.com/openstack-k8s-operators/lib-common/modules/common/tls" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | +) |
| 27 | + |
| 28 | +var _ = Describe("tls package", func() { |
| 29 | + var namespace string |
| 30 | + |
| 31 | + BeforeEach(func() { |
| 32 | + // NOTE(gibi): We need to create a unique namespace for each test run |
| 33 | + // as namespaces cannot be deleted in a locally running envtest. See |
| 34 | + // https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation |
| 35 | + namespace = uuid.New().String() |
| 36 | + th.CreateNamespace(namespace) |
| 37 | + // We still request the delete of the Namespace to properly cleanup if |
| 38 | + // we run the test in an existing cluster. |
| 39 | + DeferCleanup(th.DeleteNamespace, namespace) |
| 40 | + |
| 41 | + }) |
| 42 | + |
| 43 | + It("validates CA cert secret", func() { |
| 44 | + sname := types.NamespacedName{ |
| 45 | + Name: "ca", |
| 46 | + Namespace: namespace, |
| 47 | + } |
| 48 | + th.CreateEmptySecret(sname) |
| 49 | + |
| 50 | + // validate bad ca cert secret |
| 51 | + _, ctrlResult, err := tls.ValidateCACertSecret(th.Ctx, cClient, sname) |
| 52 | + Expect(err).To(HaveOccurred()) |
| 53 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 54 | + |
| 55 | + // update ca cert secret with good data |
| 56 | + th.UpdateSecret(sname, tls.CABundleKey, []byte("foo")) |
| 57 | + hash, ctrlResult, err := tls.ValidateCACertSecret(th.Ctx, cClient, sname) |
| 58 | + Expect(err).ShouldNot(HaveOccurred()) |
| 59 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 60 | + Expect(hash).To(BeIdenticalTo("n56fh645hfbh687hc9h678h87h64bh598h577hch5d6h5c9h5d4h74h84h5f4hfch6dh678h547h9bhbchb6h89h5c4h68dhc9h664h557h595h5c5q")) |
| 61 | + }) |
| 62 | + |
| 63 | + It("validates service cert secret", func() { |
| 64 | + sname := types.NamespacedName{ |
| 65 | + Name: "cert", |
| 66 | + Namespace: namespace, |
| 67 | + } |
| 68 | + |
| 69 | + // create bad cert secret |
| 70 | + th.CreateEmptySecret(sname) |
| 71 | + |
| 72 | + // validate bad cert secret |
| 73 | + s := &tls.Service{ |
| 74 | + SecretName: sname.Name, |
| 75 | + } |
| 76 | + _, ctrlResult, err := s.ValidateCertSecret(th.Ctx, h, namespace) |
| 77 | + Expect(err).To(HaveOccurred()) |
| 78 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 79 | + |
| 80 | + // update cert secret with cert, still key missing |
| 81 | + th.UpdateSecret(sname, tls.CertKey, []byte("cert")) |
| 82 | + _, ctrlResult, err = s.ValidateCertSecret(th.Ctx, h, namespace) |
| 83 | + Expect(err).To(HaveOccurred()) |
| 84 | + Expect(err.Error()).To(ContainSubstring("field tls.key not found in Secret")) |
| 85 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 86 | + |
| 87 | + // update cert secret with key to be a good cert secret |
| 88 | + th.UpdateSecret(sname, tls.PrivateKey, []byte("key")) |
| 89 | + |
| 90 | + // validate good cert secret |
| 91 | + hash, ctrlResult, err := s.ValidateCertSecret(th.Ctx, h, namespace) |
| 92 | + Expect(err).ShouldNot(HaveOccurred()) |
| 93 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 94 | + Expect(hash).To(BeIdenticalTo("n547h97h5cfh587h56ch594h79hd4h96h5cfh565h587h569h688h666h685h67ch7fhfbh664h5f9h694h564h9ch645h675h665h78h7h87h566hb6q")) |
| 95 | + }) |
| 96 | + |
| 97 | + It("validates endpoint certs secrets", func() { |
| 98 | + sname := types.NamespacedName{ |
| 99 | + Name: "cert", |
| 100 | + Namespace: namespace, |
| 101 | + } |
| 102 | + // create bad cert secret |
| 103 | + th.CreateSecret(sname, map[string][]byte{ |
| 104 | + tls.PrivateKey: []byte("key"), |
| 105 | + }) |
| 106 | + |
| 107 | + endpointCfgs := map[service.Endpoint]tls.Service{} |
| 108 | + |
| 109 | + // validate empty service map |
| 110 | + _, ctrlResult, err := tls.ValidateEndpointCerts(th.Ctx, h, namespace, endpointCfgs) |
| 111 | + Expect(err).ToNot(HaveOccurred()) |
| 112 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 113 | + |
| 114 | + endpointCfgs[service.EndpointInternal] = tls.Service{ |
| 115 | + SecretName: sname.Name, |
| 116 | + } |
| 117 | + endpointCfgs[service.EndpointPublic] = tls.Service{ |
| 118 | + SecretName: sname.Name, |
| 119 | + } |
| 120 | + |
| 121 | + // validate service map with bad cert secret |
| 122 | + _, ctrlResult, err = tls.ValidateEndpointCerts(th.Ctx, h, namespace, endpointCfgs) |
| 123 | + Expect(err).To(HaveOccurred()) |
| 124 | + Expect(err.Error()).To(ContainSubstring("field tls.crt not found in Secret")) |
| 125 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 126 | + |
| 127 | + // update cert secret to have missing private key |
| 128 | + th.UpdateSecret(sname, tls.CertKey, []byte("cert")) |
| 129 | + |
| 130 | + // validate service map with good cert secret |
| 131 | + hash, ctrlResult, err := tls.ValidateEndpointCerts(th.Ctx, h, namespace, endpointCfgs) |
| 132 | + Expect(err).ShouldNot(HaveOccurred()) |
| 133 | + Expect(ctrlResult).To(BeIdenticalTo(ctrl.Result{})) |
| 134 | + Expect(hash).To(BeIdenticalTo("n5d7h65dh5d5h569hffh66ch568h95h686h58fhcfh586h5b8hc6hd7h65bh56bh55bh656hfh5f7h84h54bh65dh5c9h8ch64bh64bhdfh8ch589h54bq")) |
| 135 | + }) |
| 136 | +}) |
0 commit comments