Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit e95497e

Browse files
authored
CLOUDP-74820: change in scram credential secret name generation (#234)
This PR changes the logic for SCRAM credential secret name generation for MongoDB users (to accommodate for non DNS1123 complying user name). Now a user is explicitly asked to provide a DNS1123 complying "secret name" which is being validated as opposed to the current operator behavior where it errors out during runtime when an invalid name is being generated for SCRAM secret(based on user name).
1 parent d89d4c7 commit e95497e

File tree

13 files changed

+153
-34
lines changed

13 files changed

+153
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ tags
7676
.history
7777
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
7878
*mypy_cache
79+
venv/
80+
local-config.json
7981
.idea
8082
vendor
8183
__pycache__

deploy/crds/mongodb.com_mongodb_crd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,16 @@ spec:
157157
- name
158158
type: object
159159
type: array
160+
scramCredentialsSecretName:
161+
description: ScramCredentialsSecretName appended by string "scram-credentials" is the name of the secret object
162+
created by the mongoDB operator for storing SCRAM credentials
163+
type: string
164+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
160165
required:
161166
- name
162167
- passwordSecretRef
163168
- roles
169+
- scramCredentialsSecretName
164170
type: object
165171
type: array
166172
version:

deploy/crds/mongodb.com_v1_mongodb_cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ spec:
1919
db: admin
2020
- name: userAdminAnyDatabase
2121
db: admin
22+
scramCredentialsSecretName: my-scram

deploy/crds/mongodb.com_v1_mongodb_openshift_cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ spec:
1919
db: admin
2020
- name: userAdminAnyDatabase
2121
db: admin
22+
scramCredentialsSecretName: my-scram
2223
statefulSet:
2324
spec:
2425
template:

deploy/crds/mongodb.com_v1_mongodb_scram_cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ spec:
2020
db: admin
2121
- name: userAdminAnyDatabase
2222
db: admin
23+
scramCredentialsSecretName: my-scram
2324

2425
# the user credentials will be generated from this secret
2526
# once the credentials are generated, this secret is no longer required

deploy/crds/mongodb.com_v1_mongodb_tls_cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ spec:
2525
db: admin
2626
- name: userAdminAnyDatabase
2727
db: admin
28+
scramCredentialsSecretName: my-scram

docs/users.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ You cannot disable SCRAM authentication.
3838
| `spec.users.db` | string | Database that the user authenticates against. Defaults to `admin`. | No |
3939
| `spec.users.passwordSecretRef.name` | string | Name of the secret that contains the user's plain text password. | Yes|
4040
| `spec.users.passwordSecretRef.key` | string| Key in the secret that corresponds to the value of the user's password. Defaults to `password`. | No |
41+
| `spec.users.scramCredentialsSecretName` | string| ScramCredentialsSecretName appended by string "scram-credentials" is the name of the secret object created by the operator for storing SCRAM credentials for the user. The name should comply with [DNS1123 subdomain](https://tools.ietf.org/html/rfc1123). Also, please make sure the name is unique among `users`. | Yes |
4142
| `spec.users.roles` | array of objects | Configures roles assigned to the user. | Yes |
4243
| `spec.users.roles.role.name` | string | Name of the role. Valid values are [built-in roles](https://docs.mongodb.com/manual/reference/built-in-roles/#built-in-roles). | Yes |
4344
| `spec.users.roles.role.db` | string | Database that the role applies to. | Yes |

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/mongodb/mongodb-kubernetes-operator
22

3-
go 1.13
3+
go 1.14
44

55
require (
66
github.com/Azure/go-autorest v14.0.1+incompatible // indirect
@@ -15,6 +15,7 @@ require (
1515
github.com/klauspost/compress v1.9.8 // indirect
1616
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
1717
github.com/operator-framework/operator-sdk v0.17.0
18+
github.com/pkg/errors v0.9.1
1819
github.com/prometheus/procfs v0.0.11 // indirect
1920
github.com/rogpeppe/go-internal v1.5.2 // indirect
2021
github.com/spf13/cobra v0.0.7 // indirect

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ type MongoDBUser struct {
118118

119119
// Roles is an array of roles assigned to this user
120120
Roles []Role `json:"roles"`
121+
122+
// ScramCredentialsSecretName appended by string "scram-credentials" is the name of the secret object created by the mongoDB operator for storing SCRAM credentials
123+
// +kubebuilder:validation:Pattern="^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$"
124+
ScramCredentialsSecretName string `json:"scramCredentialsSecretName"`
121125
}
122126

123127
func (m MongoDBUser) GetPasswordSecretKey() string {
@@ -135,6 +139,12 @@ func (m MongoDBUser) GetUserName() string {
135139
return m.Name
136140
}
137141

142+
// GetScramCredentialsSecretName gets the final SCRAM credentials secret-name by appending the user provided
143+
// scramsCredentialSecretName with "scram-credentials"
144+
func (m MongoDBUser) GetScramCredentialsSecretName() string {
145+
return fmt.Sprintf("%s-%s", m.ScramCredentialsSecretName, "scram-credentials")
146+
}
147+
138148
// SecretKeyReference is a reference to the secret containing the user's password
139149
type SecretKeyReference struct {
140150
// Name is the name of the secret storing this user's password

pkg/apis/mongodb/v1/mongodb_types_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,75 @@ func TestGetFCV(t *testing.T) {
2828
assert.Equal(t, "4.2", mdb.GetFCV())
2929
}
3030

31+
func TestGetScramCredentialsSecretName(t *testing.T) {
32+
testusers := []struct {
33+
in MongoDBUser
34+
exp string
35+
}{
36+
{
37+
MongoDBUser{
38+
Name: "mdb-0",
39+
DB: "admin",
40+
Roles: []Role{
41+
// roles on testing db for general connectivity
42+
{
43+
DB: "testing",
44+
Name: "readWrite",
45+
},
46+
{
47+
DB: "testing",
48+
Name: "clusterAdmin",
49+
},
50+
// admin roles for reading FCV
51+
{
52+
DB: "admin",
53+
Name: "readWrite",
54+
},
55+
{
56+
DB: "admin",
57+
Name: "clusterAdmin",
58+
},
59+
},
60+
ScramCredentialsSecretName: "scram-credential-secret-name-0",
61+
},
62+
"scram-credential-secret-name-0-scram-credentials",
63+
},
64+
{
65+
MongoDBUser{
66+
Name: "mdb-1",
67+
DB: "admin",
68+
Roles: []Role{
69+
// roles on testing db for general connectivity
70+
{
71+
DB: "testing",
72+
Name: "readWrite",
73+
},
74+
{
75+
DB: "testing",
76+
Name: "clusterAdmin",
77+
},
78+
// admin roles for reading FCV
79+
{
80+
DB: "admin",
81+
Name: "readWrite",
82+
},
83+
{
84+
DB: "admin",
85+
Name: "clusterAdmin",
86+
},
87+
},
88+
ScramCredentialsSecretName: "scram-credential-secret-name-1",
89+
},
90+
"scram-credential-secret-name-1-scram-credentials",
91+
},
92+
}
93+
94+
for _, tt := range testusers {
95+
assert.Equal(t, tt.exp, tt.in.GetScramCredentialsSecretName())
96+
}
97+
98+
}
99+
31100
func newReplicaSet(members int, name, namespace string) MongoDB {
32101
return MongoDB{
33102
TypeMeta: metav1.TypeMeta{},

0 commit comments

Comments
 (0)