Skip to content

Commit 77aaffc

Browse files
authored
CLOUDP-300256: Allow unset db-user scope (#2120)
* Allow unset db-user scope by sending an empty set
1 parent a631328 commit 77aaffc

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

internal/controller/atlasdatabaseuser/databaseuser_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ func TestDbuLifeCycle(t *testing.T) {
651651
Name: "user-pass",
652652
},
653653
DatabaseName: "admin",
654+
Scopes: []akov2.ScopeSpec{},
654655
},
655656
},
656657
nil,
@@ -1160,6 +1161,7 @@ func TestUpdate(t *testing.T) {
11601161
Name: "user-pass",
11611162
},
11621163
DatabaseName: "admin",
1164+
Scopes: []akov2.ScopeSpec{},
11631165
},
11641166
},
11651167
dbUserService: func() dbuser.AtlasUsersService {

internal/translation/dbuser/conversion.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func normalize(spec *akov2.AtlasDatabaseUserSpec) error {
8585
a.Name+string(a.Type),
8686
b.Name+string(b.Type))
8787
})
88+
if spec.Scopes == nil {
89+
spec.Scopes = []akov2.ScopeSpec{}
90+
}
8891
if spec.DeleteAfterDate != "" { // enforce date format
8992
operatorDeleteDate, err := timeutil.ParseISO8601(spec.DeleteAfterDate)
9093
if err != nil {
@@ -178,7 +181,7 @@ func rolesToAtlas(roles []akov2.RoleSpec) *[]admin.DatabaseUserRole {
178181

179182
func scopesToAtlas(scopes []akov2.ScopeSpec) *[]admin.UserScope {
180183
if len(scopes) == 0 {
181-
return nil
184+
return &[]admin.UserScope{}
182185
}
183186
atlasScopes := []admin.UserScope{}
184187
for _, scope := range scopes {
@@ -199,9 +202,9 @@ func dateFromAtlas(date *time.Time) string {
199202

200203
func scopesFromAtlas(scopes []admin.UserScope) ([]akov2.ScopeSpec, error) {
201204
if len(scopes) == 0 {
202-
return nil, nil
205+
return []akov2.ScopeSpec{}, nil
203206
}
204-
specScopes := []akov2.ScopeSpec{}
207+
specScopes := make([]akov2.ScopeSpec, 0, len(scopes))
205208
for _, scope := range scopes {
206209
scopeType, err := scopeTypeFromAtlas(scope.Type)
207210
if err != nil {

internal/translation/dbuser/conversion_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestNewUser(t *testing.T) {
4646
{
4747
title: "Empty spec returns empty user",
4848
spec: &akov2.AtlasDatabaseUserSpec{},
49-
expectedUser: &dbuser.User{AtlasDatabaseUserSpec: &akov2.AtlasDatabaseUserSpec{}},
49+
expectedUser: &dbuser.User{AtlasDatabaseUserSpec: &akov2.AtlasDatabaseUserSpec{Scopes: []akov2.ScopeSpec{}}},
5050
},
5151

5252
{
@@ -490,5 +490,6 @@ func defaultTestSpec() *akov2.AtlasDatabaseUserSpec {
490490
return &akov2.AtlasDatabaseUserSpec{
491491
DatabaseName: testDB,
492492
Username: testUsername,
493+
Scopes: []akov2.ScopeSpec{},
493494
}
494495
}

internal/translation/dbuser/dbuser_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,15 @@ func TestAtlasUsersGet(t *testing.T) {
7373
mockUsersAPI.EXPECT().GetDatabaseUserExecute(admin.GetDatabaseUserApiRequest{ApiService: mockUsersAPI}).Return(
7474
expectedUser, &http.Response{StatusCode: http.StatusOK}, nil)
7575
},
76-
expectedUser: &User{AtlasDatabaseUserSpec: &akov2.AtlasDatabaseUserSpec{DatabaseName: db, Username: username}, ProjectID: projectID},
77-
expectedErr: nil,
76+
expectedUser: &User{
77+
ProjectID: projectID,
78+
AtlasDatabaseUserSpec: &akov2.AtlasDatabaseUserSpec{
79+
DatabaseName: db,
80+
Username: username,
81+
Scopes: []akov2.ScopeSpec{},
82+
},
83+
},
84+
expectedErr: nil,
7885
},
7986
{
8087
name: "User not found",

internal/translation/dbuser/internal_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func defaultTestUser() *User {
116116
AtlasDatabaseUserSpec: &akov2.AtlasDatabaseUserSpec{
117117
DatabaseName: testDB,
118118
Username: testUsername,
119+
Scopes: []akov2.ScopeSpec{},
119120
},
120121
Password: testPassword,
121122
ProjectID: testProjectID,

test/int/databaseuser_unprotected_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,13 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
279279
It("Adds connection secret when new deployment is created", Label("user-add-secret"), func() {
280280
secondDeployment := &akov2.AtlasDeployment{}
281281

282-
By("Creating a database user", func() {
282+
By("Creating a database user for existing deployment only", func() {
283283
passwordSecret := buildPasswordSecret(testNamespace.Name, UserPasswordSecret, DBUserPassword)
284284
Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed())
285285

286286
testDBUser1 = akov2.NewDBUser(testNamespace.Name, dbUserName1, dbUserName1, projectName).
287287
WithPasswordSecret(UserPasswordSecret).
288+
WithScope(akov2.DeploymentScopeType, testDeployment.GetDeploymentName()).
288289
WithRole("readWriteAnyDatabase", "admin", "")
289290
Expect(k8sClient.Create(context.Background(), testDBUser1)).To(Succeed())
290291

@@ -306,7 +307,25 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
306307
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
307308
})
308309

309-
By("Validating connection secrets were created", func() {
310+
By("Validating connection secrets for second deployment were not created", func() {
311+
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
312+
313+
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
314+
Expect(tryConnect(testProject.ID(), *secondDeployment, *testDBUser1)).ShouldNot(Succeed())
315+
})
316+
317+
By("Removing database user scope for first deployment", func() {
318+
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(testDBUser1), testDBUser1)).Should(Succeed())
319+
testDBUser1.Spec.Scopes = nil
320+
321+
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
322+
323+
Eventually(func() bool {
324+
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
325+
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
326+
})
327+
328+
By("Validating connection secrets for both deployments were created", func() {
310329
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
311330
validateSecret(k8sClient, *testProject, *secondDeployment, *testDBUser1)
312331

0 commit comments

Comments
 (0)