Skip to content

Commit

Permalink
backport of commit 9019203 (#23492)
Browse files Browse the repository at this point in the history
Co-authored-by: Conor McCullough <[email protected]>
  • Loading branch information
1 parent c9f0387 commit 1e137ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/23240.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
mongo-db: allow non-admin database for root credential rotation
```
2 changes: 1 addition & 1 deletion plugins/database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (m *MongoDB) changeUserPassword(ctx context.Context, username, password str
}

database := cs.Database
if username == m.Username || database == "" {
if database == "" {
database = "admin"
}

Expand Down
59 changes: 58 additions & 1 deletion plugins/database/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"go.mongodb.org/mongo-driver/mongo/readpref"
)

const mongoAdminRole = `{ "db": "admin", "roles": [ { "role": "readWrite" } ] }`
const (
mongoAdminRole = `{ "db": "admin", "roles": [ { "role": "readWrite" } ] }`
mongoTestDBAdminRole = `{ "db": "test", "roles": [ { "role": "readWrite" } ] }`
)

func TestMongoDB_Initialize(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
Expand Down Expand Up @@ -119,13 +122,34 @@ func TestNewUser_usernameTemplate(t *testing.T) {

expectedUsernameRegex: "^[A-Z0-9]{2}_[0-9]{10}_TESTROLENAMEWITHMANYCHARACTERS_TOKEN$",
},
"admin in test database username template": {
usernameTemplate: "",

newUserReq: dbplugin.NewUserRequest{
UsernameConfig: dbplugin.UsernameMetadata{
DisplayName: "token",
RoleName: "testrolenamewithmanycharacters",
},
Statements: dbplugin.Statements{
Commands: []string{mongoTestDBAdminRole},
},
Password: "98yq3thgnakjsfhjkl",
Expiration: time.Now().Add(time.Minute),
},

expectedUsernameRegex: "^v-token-testrolenamewit-[a-zA-Z0-9]{20}-[0-9]{10}$",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()

if name == "admin in test database username template" {
connURL = connURL + "/test?authSource=test"
}

db := new()
defer dbtesting.AssertClose(t, db)

Expand Down Expand Up @@ -290,6 +314,39 @@ func TestMongoDB_UpdateUser_Password(t *testing.T) {
assertCredsExist(t, dbUser, newPassword, connURL)
}

func TestMongoDB_RotateRoot_NonAdminDB(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()

connURL = connURL + "/test?authSource=test"
db := new()
defer dbtesting.AssertClose(t, db)

initReq := dbplugin.InitializeRequest{
Config: map[string]interface{}{
"connection_url": connURL,
},
VerifyConnection: true,
}
dbtesting.AssertInitialize(t, db, initReq)

dbUser := "testmongouser"
startingPassword := "password"
createDBUser(t, connURL, "test", dbUser, startingPassword)

newPassword := "myreallysecurecredentials"

updateReq := dbplugin.UpdateUserRequest{
Username: dbUser,
Password: &dbplugin.ChangePassword{
NewPassword: newPassword,
},
}
dbtesting.AssertUpdateUser(t, db, updateReq)

assertCredsExist(t, dbUser, newPassword, connURL)
}

func TestGetTLSAuth(t *testing.T) {
ca := certhelpers.NewCert(t,
certhelpers.CommonName("certificate authority"),
Expand Down

0 comments on commit 1e137ae

Please sign in to comment.