|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + g "github.com/onsi/ginkgo/v2" |
| 10 | + o "github.com/onsi/gomega" |
| 11 | + |
| 12 | + exutil "github.com/openshift/origin/test/extended/util" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + e2e "k8s.io/kubernetes/test/e2e/framework" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = g.Describe(`[sig-storage][CSI][Jira:"Storage"] CSI driver operator secure certificates`, func() { |
| 19 | + defer g.GinkgoRecover() |
| 20 | + oc := exutil.NewCLI("csi-cert-check") |
| 21 | + |
| 22 | + g.BeforeEach(func() { |
| 23 | + isMicroShift, err := exutil.IsMicroShiftCluster(oc.AdminKubeClient()) |
| 24 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 25 | + if isMicroShift { |
| 26 | + g.Skip("Not supported on MicroShift") |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + g.It("csi driver operators should use service CA signed certificates by default", func() { |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + g.By("Verifying the storage cluster operator is healthy") |
| 34 | + WaitForCSOHealthy(oc) |
| 35 | + |
| 36 | + g.By("Listing CSI driver operator pods") |
| 37 | + allPods, err := oc.AdminKubeClient().CoreV1().Pods(CSINamespace).List(ctx, metav1.ListOptions{}) |
| 38 | + o.Expect(err).NotTo(o.HaveOccurred(), "failed to list pods in namespace %s", CSINamespace) |
| 39 | + |
| 40 | + checked := 0 |
| 41 | + var failures []string |
| 42 | + |
| 43 | + for _, pod := range allPods.Items { |
| 44 | + operatorContainer := getCsiDriverOperatorContainerName(pod) |
| 45 | + if operatorContainer == "" { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + g.By(fmt.Sprintf("Checking logs of %s for secure certificate usage", pod.Name)) |
| 50 | + logStream, err := oc.AdminKubeClient().CoreV1().Pods(CSINamespace).GetLogs(pod.Name, &corev1.PodLogOptions{ |
| 51 | + Container: operatorContainer, |
| 52 | + }).Stream(ctx) |
| 53 | + if err != nil { |
| 54 | + e2e.Logf("Failed to get logs for pod %s, skipping: %v", pod.Name, err) |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + logBytes, err := io.ReadAll(logStream) |
| 59 | + logStream.Close() |
| 60 | + o.Expect(err).NotTo(o.HaveOccurred(), "failed to read logs for pod %s", pod.Name) |
| 61 | + |
| 62 | + logOutput := string(logBytes) |
| 63 | + if strings.Contains(logOutput, "Using insecure, self-signed certificates") { |
| 64 | + failures = append(failures, fmt.Sprintf("%s (pod %s): insecure self-signed certificates detected", operatorContainer, pod.Name)) |
| 65 | + } |
| 66 | + if !strings.Contains(logOutput, "Using service-serving-cert provided certificates") { |
| 67 | + failures = append(failures, fmt.Sprintf("%s (pod %s): secure cert log not found", operatorContainer, pod.Name)) |
| 68 | + } |
| 69 | + checked++ |
| 70 | + } |
| 71 | + |
| 72 | + if checked == 0 { |
| 73 | + g.Skip(fmt.Sprintf("No CSI driver operator pods found on platform %q", e2e.TestContext.Provider)) |
| 74 | + } |
| 75 | + o.Expect(failures).To(o.BeEmpty(), |
| 76 | + "CSI driver operators not using service CA signed certificates:\n%s", strings.Join(failures, "\n")) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +// Returns the name of the CSI driver operator |
| 81 | +// container in the pod, or "" if the pod is not a CSI driver operator. |
| 82 | +func getCsiDriverOperatorContainerName(pod corev1.Pod) string { |
| 83 | + for _, c := range pod.Spec.Containers { |
| 84 | + if strings.HasSuffix(c.Name, "-csi-driver-operator") { |
| 85 | + return c.Name |
| 86 | + } |
| 87 | + } |
| 88 | + return "" |
| 89 | +} |
0 commit comments