|
| 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 | +// Generated by Claude Sonnet 4.6 (1M context) |
| 17 | + |
| 18 | +package secrets_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 33 | + |
| 34 | + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 35 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 36 | + "github.com/devfile/devworkspace-operator/pkg/secrets" |
| 37 | +) |
| 38 | + |
| 39 | +func TestSecrets(t *testing.T) { |
| 40 | + RegisterFailHandler(Fail) |
| 41 | + RunSpecs(t, "Secrets Suite") |
| 42 | +} |
| 43 | + |
| 44 | +var _ = Describe("Backup secrets", func() { |
| 45 | + var ( |
| 46 | + ctx context.Context |
| 47 | + scheme *runtime.Scheme |
| 48 | + ) |
| 49 | + |
| 50 | + const ( |
| 51 | + operatorNamespace = "devworkspace-controller" |
| 52 | + workspaceNamespace = "user-namespace" |
| 53 | + configuredAuthSecretName = "quay-backup-auth" |
| 54 | + ) |
| 55 | + |
| 56 | + BeforeEach(func() { |
| 57 | + ctx = context.Background() |
| 58 | + |
| 59 | + scheme = runtime.NewScheme() |
| 60 | + Expect(dw.AddToScheme(scheme)).To(Succeed()) |
| 61 | + Expect(corev1.AddToScheme(scheme)).To(Succeed()) |
| 62 | + Expect(controllerv1alpha1.AddToScheme(scheme)).To(Succeed()) |
| 63 | + }) |
| 64 | + |
| 65 | + // buildFakeClient creates a fake client with the given objects pre-populated. |
| 66 | + buildFakeClient := func(objs ...client.Object) client.Client { |
| 67 | + return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() |
| 68 | + } |
| 69 | + |
| 70 | + // buildWorkspace creates a minimal DevWorkspace for use in tests. |
| 71 | + buildWorkspace := func(name, namespace string) *dw.DevWorkspace { |
| 72 | + return &dw.DevWorkspace{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: name, |
| 75 | + Namespace: namespace, |
| 76 | + UID: "test-uid", |
| 77 | + }, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // buildConfig creates an OperatorConfiguration with the given registry auth secret name. |
| 82 | + buildConfig := func(authSecretName string) *controllerv1alpha1.OperatorConfiguration { |
| 83 | + return &controllerv1alpha1.OperatorConfiguration{ |
| 84 | + Workspace: &controllerv1alpha1.WorkspaceConfig{ |
| 85 | + BackupCronJob: &controllerv1alpha1.BackupCronJobConfig{ |
| 86 | + Registry: &controllerv1alpha1.RegistryConfig{ |
| 87 | + Path: "my-registry:5000", |
| 88 | + AuthSecret: authSecretName, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // buildSecret creates a secret with the given name, namespace and data. |
| 96 | + buildSecret := func(name, namespace string, data map[string][]byte) *corev1.Secret { |
| 97 | + return &corev1.Secret{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: name, |
| 100 | + Namespace: namespace, |
| 101 | + }, |
| 102 | + Data: data, |
| 103 | + Type: corev1.SecretTypeDockerConfigJson, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + log := zap.New(zap.UseDevMode(true)).WithName("SecretsTest") |
| 108 | + |
| 109 | + Context("CopySecret", func() { |
| 110 | + It("creates the copy using the configured secret name, not devworkspace-backup-registry-auth", func() { |
| 111 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 112 | + sourceSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{ |
| 113 | + ".dockerconfigjson": []byte(`{"auths":{}}`), |
| 114 | + }) |
| 115 | + fakeClient := buildFakeClient(workspace) |
| 116 | + |
| 117 | + By("calling CopySecret with the configured secret name") |
| 118 | + result, err := secrets.CopySecret(ctx, fakeClient, workspace, sourceSecret, configuredAuthSecretName, scheme, log) |
| 119 | + Expect(err).NotTo(HaveOccurred()) |
| 120 | + // The first sync creates the object and returns desiredSecret (NotInSyncError path) |
| 121 | + Expect(result).NotTo(BeNil()) |
| 122 | + Expect(result.Name).To(Equal(configuredAuthSecretName)) |
| 123 | + Expect(result.Namespace).To(Equal(workspaceNamespace)) |
| 124 | + |
| 125 | + By("verifying the secret was created in the workspace namespace with the correct name") |
| 126 | + clusterSecret := &corev1.Secret{} |
| 127 | + Expect(fakeClient.Get(ctx, types.NamespacedName{ |
| 128 | + Name: configuredAuthSecretName, |
| 129 | + Namespace: workspaceNamespace, |
| 130 | + }, clusterSecret)).To(Succeed()) |
| 131 | + Expect(clusterSecret.Name).To(Equal(configuredAuthSecretName)) |
| 132 | + Expect(clusterSecret.Data).To(Equal(sourceSecret.Data)) |
| 133 | + |
| 134 | + By("verifying the hardcoded default name was NOT used") |
| 135 | + legacySecret := &corev1.Secret{} |
| 136 | + err = fakeClient.Get(ctx, types.NamespacedName{ |
| 137 | + Name: "devworkspace-backup-registry-auth", |
| 138 | + Namespace: workspaceNamespace, |
| 139 | + }, legacySecret) |
| 140 | + Expect(client.IgnoreNotFound(err)).To(Succeed()) |
| 141 | + Expect(err).To(HaveOccurred(), "legacy hardcoded name must not be used") |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + Context("HandleRegistryAuthSecret", func() { |
| 146 | + It("returns secret directly when it exists in the workspace namespace with the configured name", func() { |
| 147 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 148 | + config := buildConfig(configuredAuthSecretName) |
| 149 | + // Secret already exists in workspace namespace |
| 150 | + localSecret := buildSecret(configuredAuthSecretName, workspaceNamespace, map[string][]byte{ |
| 151 | + ".dockerconfigjson": []byte(`{"auths":{"local":{}}}`), |
| 152 | + }) |
| 153 | + fakeClient := buildFakeClient(workspace, localSecret) |
| 154 | + |
| 155 | + By("calling HandleRegistryAuthSecret with operator namespace") |
| 156 | + result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log) |
| 157 | + Expect(err).NotTo(HaveOccurred()) |
| 158 | + Expect(result).NotTo(BeNil()) |
| 159 | + Expect(result.Name).To(Equal(configuredAuthSecretName)) |
| 160 | + Expect(result.Namespace).To(Equal(workspaceNamespace)) |
| 161 | + }) |
| 162 | + |
| 163 | + It("copies secret from operator namespace to workspace namespace with the configured name", func() { |
| 164 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 165 | + config := buildConfig(configuredAuthSecretName) |
| 166 | + // Secret only exists in the operator namespace |
| 167 | + operatorSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{ |
| 168 | + ".dockerconfigjson": []byte(`{"auths":{"operator":{}}}`), |
| 169 | + }) |
| 170 | + fakeClient := buildFakeClient(workspace, operatorSecret) |
| 171 | + |
| 172 | + By("calling HandleRegistryAuthSecret — secret missing from workspace NS, present in operator NS") |
| 173 | + result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log) |
| 174 | + Expect(err).NotTo(HaveOccurred()) |
| 175 | + Expect(result).NotTo(BeNil()) |
| 176 | + // The result should use the configured name |
| 177 | + Expect(result.Name).To(Equal(configuredAuthSecretName)) |
| 178 | + Expect(result.Namespace).To(Equal(workspaceNamespace)) |
| 179 | + |
| 180 | + By("verifying the copy exists in workspace namespace with the configured name") |
| 181 | + clusterSecret := &corev1.Secret{} |
| 182 | + Expect(fakeClient.Get(ctx, types.NamespacedName{ |
| 183 | + Name: configuredAuthSecretName, |
| 184 | + Namespace: workspaceNamespace, |
| 185 | + }, clusterSecret)).To(Succeed()) |
| 186 | + |
| 187 | + By("verifying the legacy hardcoded name was NOT created") |
| 188 | + legacySecret := &corev1.Secret{} |
| 189 | + err = fakeClient.Get(ctx, types.NamespacedName{ |
| 190 | + Name: "devworkspace-backup-registry-auth", |
| 191 | + Namespace: workspaceNamespace, |
| 192 | + }, legacySecret) |
| 193 | + Expect(client.IgnoreNotFound(err)).To(Succeed()) |
| 194 | + Expect(err).To(HaveOccurred(), "legacy hardcoded name must not be created") |
| 195 | + }) |
| 196 | + |
| 197 | + It("returns nil, nil when operator namespace is given but secret is in neither namespace", func() { |
| 198 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 199 | + config := buildConfig(configuredAuthSecretName) |
| 200 | + fakeClient := buildFakeClient(workspace) // No secrets pre-created |
| 201 | + |
| 202 | + result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log) |
| 203 | + // Secret not found in operator namespace returns an error (not found) |
| 204 | + // The function logs the error and returns it |
| 205 | + Expect(err).To(HaveOccurred()) |
| 206 | + Expect(result).To(BeNil()) |
| 207 | + }) |
| 208 | + }) |
| 209 | + |
| 210 | + Context("GetNamespaceRegistryAuthSecret", func() { |
| 211 | + It("end-to-end: with operatorConfigNamespace, copies secret from operator NS and returns it with configured name", func() { |
| 212 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 213 | + config := buildConfig(configuredAuthSecretName) |
| 214 | + // Secret only in operator namespace |
| 215 | + operatorSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{ |
| 216 | + ".dockerconfigjson": []byte(`{"auths":{"e2e":{}}}`), |
| 217 | + }) |
| 218 | + fakeClient := buildFakeClient(workspace, operatorSecret) |
| 219 | + |
| 220 | + By("calling GetNamespaceRegistryAuthSecret with a non-empty operatorConfigNamespace") |
| 221 | + result, err := secrets.GetNamespaceRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log) |
| 222 | + Expect(err).NotTo(HaveOccurred()) |
| 223 | + Expect(result).NotTo(BeNil()) |
| 224 | + Expect(result.Name).To(Equal(configuredAuthSecretName)) |
| 225 | + Expect(result.Namespace).To(Equal(workspaceNamespace)) |
| 226 | + |
| 227 | + By("verifying the copy exists with the configured name in the workspace namespace") |
| 228 | + clusterSecret := &corev1.Secret{} |
| 229 | + Expect(fakeClient.Get(ctx, types.NamespacedName{ |
| 230 | + Name: configuredAuthSecretName, |
| 231 | + Namespace: workspaceNamespace, |
| 232 | + }, clusterSecret)).To(Succeed()) |
| 233 | + }) |
| 234 | + |
| 235 | + It("legacy no-op case: with operatorConfigNamespace empty and secret in workspace NS, returns it directly", func() { |
| 236 | + workspace := buildWorkspace("my-workspace", workspaceNamespace) |
| 237 | + config := buildConfig(configuredAuthSecretName) |
| 238 | + // Secret already exists in workspace namespace |
| 239 | + localSecret := buildSecret(configuredAuthSecretName, workspaceNamespace, map[string][]byte{ |
| 240 | + ".dockerconfigjson": []byte(`{"auths":{"local":{}}}`), |
| 241 | + }) |
| 242 | + fakeClient := buildFakeClient(workspace, localSecret) |
| 243 | + |
| 244 | + By("calling GetNamespaceRegistryAuthSecret with empty operatorConfigNamespace (legacy behavior)") |
| 245 | + result, err := secrets.GetNamespaceRegistryAuthSecret(ctx, fakeClient, workspace, config, "", scheme, log) |
| 246 | + Expect(err).NotTo(HaveOccurred()) |
| 247 | + Expect(result).NotTo(BeNil()) |
| 248 | + Expect(result.Name).To(Equal(configuredAuthSecretName)) |
| 249 | + Expect(result.Namespace).To(Equal(workspaceNamespace)) |
| 250 | + }) |
| 251 | + }) |
| 252 | +}) |
0 commit comments