I am having trouble running the eval command when a database is secured. It seems that I can execute commands on the mongo CLI when the database is unsecured, but as soon as I set the --auth flag, it stops working here.
Here are repro steps:
# Start from a new database:
mongod &
mongo
db.eval( function(x) { return x + x; }, 3 );
# This returns 6 as expected
# Create a user with superuser privileges, then shutdown and exit:
use admin
db.createUser(
{
user: "devops",
pwd: "devops",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "clusterAdmin", db: "admin" }
]
}
)
db.shutdownServer()
exit
# Restart mongod with authentication turned on:
mongod --auth --setParameter enableLocalhostAuthBypass=0 &
# Log back into mongo CLI, authenticate, and try the eval command:
mongo
use admin
db.auth("devops", "devops")
db.eval( function(x) { return x + x; }, 3 );
# At this point, I see the following error message:
"errmsg" : "not authorized on admin to execute command { $eval: function (x) { return x + x; }, args: [ 3.0 ] }",
What am I missing here? Based on the documentation on
MongoDB roles, I thought I had already given my user the "superuser" roles, so I am not sure what additional permissions I can grant the "devops" user.
My application actually invokes the
DB#eval method in the mongo-java-driver, and that results in the same error message.
I appreciate any help anyone can provide. Thanks in advance.
Best Regards,
Will Chan