|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/1password/kubernetes-secrets-injector/pkg/testhelper/defaults" |
| 8 | + "github.com/1password/kubernetes-secrets-injector/pkg/testhelper/kind" |
| 9 | + "github.com/1password/kubernetes-secrets-injector/pkg/testhelper/kube" |
| 10 | + "github.com/1password/kubernetes-secrets-injector/pkg/testhelper/system" |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + imageName = "1password/kubernetes-secrets-injector:latest" |
| 17 | +) |
| 18 | + |
| 19 | +var kubeClient *kube.Kube |
| 20 | + |
| 21 | +var _ = Describe("Kubernetes Secrets Injector e2e", Ordered, func() { |
| 22 | + ctx := context.Background() |
| 23 | + |
| 24 | + BeforeAll(func() { |
| 25 | + kubeClient = kube.NewKubeClient(&kube.Config{ |
| 26 | + Namespace: "default", |
| 27 | + ManifestsDir: filepath.Join("manifests"), |
| 28 | + TestConfig: &kube.TestConfig{ |
| 29 | + Timeout: defaults.E2ETimeout, |
| 30 | + Interval: defaults.E2EInterval, |
| 31 | + }, |
| 32 | + }) |
| 33 | + |
| 34 | + By("Building injector image") |
| 35 | + _, err := system.Run("make", "docker-build") |
| 36 | + Expect(err).NotTo(HaveOccurred()) |
| 37 | + |
| 38 | + By("Building app-example image") |
| 39 | + root, err := system.GetProjectRoot() |
| 40 | + Expect(err).NotTo(HaveOccurred()) |
| 41 | + clientDir := filepath.Join(root, "test", "e2e", "client") |
| 42 | + _, err = system.Run("docker", "build", "-t", "app-example:latest", clientDir) |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + |
| 45 | + kind.LoadImageToKind(imageName) |
| 46 | + kind.LoadImageToKind("app-example:latest") |
| 47 | + |
| 48 | + kubeClient.Secret("op-credentials").CreateOpCredentials(ctx) |
| 49 | + kubeClient.Secret("op-credentials").CheckIfExists(ctx) |
| 50 | + |
| 51 | + kubeClient.Secret("onepassword-token").CreateFromEnvVar(ctx, "OP_CONNECT_TOKEN") |
| 52 | + kubeClient.Secret("onepassword-token").CheckIfExists(ctx) |
| 53 | + |
| 54 | + kubeClient.Secret("onepassword-service-account-token").CreateFromEnvVar(ctx, "OP_SERVICE_ACCOUNT_TOKEN") |
| 55 | + kubeClient.Secret("onepassword-service-account-token").CheckIfExists(ctx) |
| 56 | + |
| 57 | + kubeClient.Namespace("default").LabelNamespace(ctx, map[string]string{ |
| 58 | + "secrets-injection": "enabled", |
| 59 | + }) |
| 60 | + |
| 61 | + _, err = system.Run("make", "deploy") |
| 62 | + Expect(err).NotTo(HaveOccurred()) |
| 63 | + |
| 64 | + kubeClient.Pod(map[string]string{"app": "secrets-injector"}).WaitingForRunningPod(ctx) |
| 65 | + |
| 66 | + //time.Sleep(5 * time.Second) |
| 67 | + kubeClient.Webhook("secrets-injector-webhook-config").WaitForWebhookToBeRegistered(ctx) |
| 68 | + |
| 69 | + By("Deploy test app") |
| 70 | + yamlPath := filepath.Join(root, "test", "e2e", "manifests", "client.yaml") |
| 71 | + _, err = system.Run("kubectl", "apply", "-f", yamlPath) |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + |
| 74 | + kubeClient.Pod(map[string]string{"app": "app-example"}).WaitingForRunningPod(ctx) |
| 75 | + }) |
| 76 | + |
| 77 | + //Context("Use Injector with Connect", func() { |
| 78 | + // BeforeAll(func() { |
| 79 | + // kubeClient.Pod(map[string]string{"app": "onepassword-connect"}).WaitingForRunningPod(ctx) |
| 80 | + // }) |
| 81 | + // |
| 82 | + // runCommonTestCases(ctx) |
| 83 | + //}) |
| 84 | + |
| 85 | + Context("Use the operator with Service Account", func() { |
| 86 | + //BeforeAll(func() { |
| 87 | + // // Update test-app to use Service Account |
| 88 | + // kubeClient.Deployment("test-app").PatchEnvVars(ctx, []corev1.EnvVar{ |
| 89 | + // { |
| 90 | + // Name: "OP_SERVICE_ACCOUNT_TOKEN", |
| 91 | + // ValueFrom: &corev1.EnvVarSource{ |
| 92 | + // SecretKeyRef: &corev1.SecretKeySelector{ |
| 93 | + // LocalObjectReference: corev1.LocalObjectReference{ |
| 94 | + // Name: "onepassword-service-account-token", |
| 95 | + // }, |
| 96 | + // Key: "token", |
| 97 | + // }, |
| 98 | + // }, |
| 99 | + // }, |
| 100 | + // }, []string{"OP_CONNECT_HOST", "OP_CONNECT_TOKEN"}) |
| 101 | + //}) |
| 102 | + |
| 103 | + runCommonTestCases(ctx) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +// runCommonTestCases contains test cases that are common to both Connect and Service Account authentication methods. |
| 108 | +func runCommonTestCases(ctx context.Context) { |
| 109 | + It("Should inject env variables into test app container", func() { |
| 110 | + kubeClient.Pod(map[string]string{ |
| 111 | + "app": "app-example", |
| 112 | + }).VerifyWebhookInjection(ctx) |
| 113 | + |
| 114 | + kubeClient.Pod(map[string]string{ |
| 115 | + "app": "app-example", |
| 116 | + }).VerifySecretsInjected(ctx) |
| 117 | + }) |
| 118 | +} |
0 commit comments