Skip to content

CLOUDP-299865: Correctly Cleanup Connection Secrets #2141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/translation/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ func (ds *ProductionAtlasDeployments) ListDeploymentNames(ctx context.Context, p
return deploymentNames, nil
}

flex, _, err := ds.flexAPI.ListFlexClusters(ctx, projectID).Execute()
if err != nil {
return nil, fmt.Errorf("failed to list flex clusters for project %s: %w", projectID, err)
}
for _, d := range flex.GetResults() {
name := pointer.GetOrDefault(d.Name, "")
if name != "" {
deploymentNames = append(deploymentNames, name)
}
}

serverless, _, err := ds.serverlessAPI.ListServerlessInstances(ctx, projectID).Execute()
if err != nil {
return nil, fmt.Errorf("failed to list serverless deployments for project %s: %w", projectID, err)
Expand Down
48 changes: 45 additions & 3 deletions test/int/databaseuser_unprotected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
})
})

It("Remove stale secrets", Label("user-gc-secrets"), func() {
It("Correctly removes stale secrets", Label("user-gc-secrets"), func() {
secondTestDeployment := &akov2.AtlasDeployment{}

By("Creating a second deployment", func() {
Expand Down Expand Up @@ -474,6 +474,26 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser1)).Should(Succeed())
})

By("Creating a second database user", func() {
passwordSecret := buildPasswordSecret(testNamespace.Name, "user-password-secret-2", DBUserPassword)
Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed())

testDBUser2 = akov2.NewDBUser(testNamespace.Name, dbUserName2, dbUserName2, projectName).
WithPasswordSecret("user-password-secret-2").
WithRole("readWriteAnyDatabase", "admin", "")
Expect(k8sClient.Create(context.Background(), testDBUser2)).To(Succeed())

Eventually(func() bool {
return resources.CheckCondition(k8sClient, testDBUser2, api.TrueCondition(api.ReadyType))
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())

validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser2)
validateSecret(k8sClient, *testProject, *secondTestDeployment, *testDBUser2)

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser2)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser2)).Should(Succeed())
})

By("Renaming username, new user is added and stale secrets are removed", func() {
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(testDBUser1), testDBUser1)).To(Succeed())
oldName := testDBUser1.Spec.Username
Expand All @@ -493,7 +513,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Execute()
Expect(err).To(HaveOccurred())

checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 2)
checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 4)
secret := validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-new-user",
kube.NormalizeIdentifier(testProject.Spec.Name),
Expand All @@ -504,9 +524,21 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
kube.NormalizeIdentifier(testProject.Spec.Name),
kube.NormalizeIdentifier(secondTestDeployment.GetDeploymentName()),
)))
secret = validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser2)
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-db-user2",
kube.NormalizeIdentifier(testProject.Spec.Name),
kube.NormalizeIdentifier(testDeployment.GetDeploymentName()),
)))
secret = validateSecret(k8sClient, *testProject, *secondTestDeployment, *testDBUser2)
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-db-user2",
kube.NormalizeIdentifier(testProject.Spec.Name),
kube.NormalizeIdentifier(secondTestDeployment.GetDeploymentName()),
)))

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser1)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser2)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser2)).Should(Succeed())
})

By("Scoping user to one cluster, a stale secret is removed", func() {
Expand All @@ -520,11 +552,21 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())

checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 1)
testDBUser2 = testDBUser2.ClearScopes().WithScope(akov2.DeploymentScopeType, testDeployment.GetDeploymentName())
Expect(k8sClient.Update(context.Background(), testDBUser2)).To(Succeed())

Eventually(func() bool {
return resources.CheckCondition(k8sClient, testDBUser2, api.TrueCondition(api.ReadyType))
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())

checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 2)
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser2)

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser1)).ShouldNot(Succeed())
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser2)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser2)).ShouldNot(Succeed())
})

By("Deleting second deployment", func() {
Expand Down
Loading