I am logging in to the test
database using the following credentials
username : testUser
password : pass
The user has only readWrite
access to the database. I want to change the current user's password to pass123
.
I tried both the below options
db.changeUserPassword("testUser", "pass123")
db.updateUser("testUser",{pwd: "pass123"})
Both failed with following errors respectively
Error: not authorized on test to execute command
Error: Updating user failed: not authorized on test to execute command { updateUser: "testUser", pwd: "xxx" }
Is there any way to change the current user's password without the help of admin roles?
UPDATE
It makes sense if the user is not allowed to change other user's password. But why the user is restricted to change his/her own password.
How to handle the below scenario?
Consider If I company policy forcing all the database users to change their password for every 30 days, as per the solution the user has to ask the admin to change the password.
ANSWER:
In the admin database, create a new role with changeOwnPassword
action.
use admin
db.createRole(
{ role: "changeOwnPasswordRole",
privileges: [
{
resource: { db: "", collection: ""},
actions: [ "changeOwnPassword" ]
}
],
roles: []
}
)
create a new user with changeOwnPasswordRole
role and along with other roles
use test
db.createUser(
{
user:"user123",
pwd:"12345678",
roles:[ "readWrite", { role:"changeOwnPasswordRole", db:"admin" } ]
}
)
login the above user credentials
Use the below command to update own password
db.updateUser("user123",{pwd: "pass123"})
Change Your Password and Custom Data
How to change my mongoDB user password as non administrator?
These link helped me with the above solution, not the link marked as duplicate.
I have read How do I change a MongoDB user's password? and it's helpful but doesn't address how to change own password. Please remove the DUPLICATE flag.