Skip to content

Commit 70c14e8

Browse files
committed
CLOUDP-349078: custom roles regression tests
1 parent 582d248 commit 70c14e8

File tree

6 files changed

+308
-89
lines changed

6 files changed

+308
-89
lines changed

controllers/operator/common_controller_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,98 @@ func TestDontSendNilPrivileges(t *testing.T) {
406406
assert.NotNil(t, roles[0].Privileges)
407407
}
408408

409+
func TestCheckEmptyStringsInPrivilegesEquivalentToNotPassingFields(t *testing.T) {
410+
ctx := context.Background()
411+
412+
roleWithEmptyStrings := mdbv1.MongoDBRole{
413+
Role: "withEmptyStrings",
414+
Db: "admin",
415+
Roles: []mdbv1.InheritedRole{{
416+
Db: "admin",
417+
Role: "read",
418+
}},
419+
Privileges: []mdbv1.Privilege{
420+
{
421+
Resource: mdbv1.Resource{
422+
Db: "config",
423+
Collection: "", // Explicit empty string
424+
},
425+
Actions: []string{"find", "update", "insert", "remove"},
426+
},
427+
{
428+
Resource: mdbv1.Resource{
429+
Db: "users",
430+
Collection: "usersCollection",
431+
},
432+
Actions: []string{"update", "insert", "remove"},
433+
},
434+
{
435+
Resource: mdbv1.Resource{
436+
Db: "", // Explicit empty string
437+
Collection: "", // Explicit empty string
438+
},
439+
Actions: []string{"find"},
440+
},
441+
},
442+
}
443+
444+
// Role without empty strings (fields omitted, which should result in empty strings for string types)
445+
roleWithoutEmptyStrings := mdbv1.MongoDBRole{
446+
Role: "withoutEmptyFields",
447+
Db: "admin",
448+
Roles: []mdbv1.InheritedRole{{
449+
Db: "admin",
450+
Role: "read",
451+
}},
452+
Privileges: []mdbv1.Privilege{
453+
{
454+
Resource: mdbv1.Resource{
455+
Db: "config",
456+
// field not set, should pass ""
457+
},
458+
Actions: []string{"find", "update", "insert", "remove"},
459+
},
460+
{
461+
Resource: mdbv1.Resource{
462+
Db: "users",
463+
Collection: "usersCollection",
464+
},
465+
Actions: []string{"update", "insert", "remove"},
466+
},
467+
{
468+
Resource: mdbv1.Resource{
469+
// fields not set, should be passed as empty strings
470+
},
471+
Actions: []string{"find"},
472+
},
473+
},
474+
}
475+
476+
rs := DefaultReplicaSetBuilder().SetRoles([]mdbv1.MongoDBRole{roleWithEmptyStrings, roleWithoutEmptyStrings}).Build()
477+
kubeClient, omConnectionFactory := mock.NewDefaultFakeClient()
478+
controller := NewReconcileCommonController(ctx, kubeClient)
479+
mockOm, _ := prepareConnection(ctx, controller, omConnectionFactory.GetConnectionFunc, t)
480+
481+
controller.ensureRoles(ctx, rs.Spec.DbCommonSpec, true, mockOm, kube.ObjectKeyFromApiObject(rs), zap.S())
482+
483+
ac, err := mockOm.ReadAutomationConfig()
484+
assert.NoError(t, err)
485+
roles, ok := ac.Deployment["roles"].([]mdbv1.MongoDBRole)
486+
assert.True(t, ok)
487+
require.Len(t, roles, 2)
488+
489+
assert.Equal(t, "config", roles[0].Privileges[0].Resource.Db)
490+
assert.Equal(t, "", roles[0].Privileges[0].Resource.Collection)
491+
492+
assert.Equal(t, "users", roles[0].Privileges[1].Resource.Db)
493+
assert.Equal(t, "usersCollection", roles[0].Privileges[1].Resource.Collection)
494+
495+
assert.Equal(t, "", roles[0].Privileges[2].Resource.Db)
496+
assert.Equal(t, "", roles[0].Privileges[2].Resource.Collection)
497+
498+
assert.True(t, reflect.DeepEqual(roles[0].Privileges, roles[1].Privileges))
499+
}
500+
409501
func TestSecretWatcherWithAllResources(t *testing.T) {
410502
ctx := context.Background()
411503
caName := "custom-ca"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: mongodb.com/v1
2+
kind: ClusterMongoDBRole
3+
metadata:
4+
name: customrole-with-empty-strings
5+
spec:
6+
role: "myClusterwideAdminWithEmptyStrings"
7+
db: "admin"
8+
roles:
9+
- db: "admin"
10+
role: "read"
11+
privileges:
12+
- resource:
13+
db: "config"
14+
collection: ""
15+
actions:
16+
- "find"
17+
- "update"
18+
- "insert"
19+
- "remove"
20+
- resource:
21+
db: "users"
22+
collection: "usersCollection"
23+
actions:
24+
- "update"
25+
- "insert"
26+
- "remove"
27+
- resource:
28+
db: ""
29+
collection: ""
30+
actions:
31+
- "find"
32+
authenticationRestrictions:
33+
- clientSource:
34+
- "127.0.0.0/8"
35+
serverAddress:
36+
- "10.0.0.0/8"

docker/mongodb-kubernetes-tests/tests/authentication/fixtures/cluster-mongodb-role.yaml renamed to docker/mongodb-kubernetes-tests/tests/authentication/fixtures/cluster-mongodb-role-without-empty-strings.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
apiVersion: mongodb.com/v1
22
kind: ClusterMongoDBRole
33
metadata:
4-
name: test-customrole
4+
name: customrole-without-empty-strings
55
spec:
6-
role: "test-customrole"
6+
role: "myClusterwideAdminWithoutEmptyStrings"
77
db: "admin"
88
roles:
99
- db: "admin"
10-
role: "root"
10+
role: "read"
1111
privileges:
1212
- resource:
13-
db: "admin"
14-
collection: "system.users"
13+
db: "config"
1514
actions:
1615
- "find"
1716
- "update"
17+
- "insert"
18+
- "remove"
1819
- resource:
19-
db: "admin"
20-
collection: "system.roles"
20+
db: "users"
21+
collection: "usersCollection"
2122
actions:
22-
- "find"
2323
- "update"
24+
- "insert"
25+
- "remove"
26+
- resource: {}
27+
actions:
28+
- "find"
2429
authenticationRestrictions:
2530
- clientSource:
2631
- "127.0.0.0/8"

docker/mongodb-kubernetes-tests/tests/authentication/fixtures/replica-set-scram.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
name: my-project
1313
credentials: my-credentials
1414
logLevel: DEBUG
15-
persistent: false
15+
persistent: true
1616
security:
1717
authentication:
1818
agents:

0 commit comments

Comments
 (0)