Reputation: 859
I'm attempting to write a script to deploy mongodb for customers using Powershell (whatever version comes with Server2016). I can install as a service and get it up and running just fine, but I want to also add authentication. Specifically I want to add an admin user and the database user.
Initially I create the mongod service with this powershell function:
function createMongoService([string] $mongoDbConfigPath, [string] $serviceName, [string] $storePath, [string] $authCmd)
{
$mongodexe = "$mongoInstallPath\bin\mongod.exe"
Write-Host "Activating Mongod as a service '$serviceName'"
"auth cmd = $authCmd"
& $mongodexe --config "$mongoDbConfigPath" --install --serviceName "$serviceName" --serviceDisplayName "Storfirst MongoDB" --port 27710 --dbpath "$storePath\db" $authCmd
if ($LASTEXITCODE -ne 0)
{
throw "Failed to start service"
}
}
In the initial case, $authCmd is "". Looking at the logs, I can confirm that the service is being started WITHOUT authentication.
2018-06-13T16:00:26.600-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-06-13T16:00:26.600-0700 I CONTROL [initandlisten] **
Read and write access to data and configuration is unrestricted.
Then I go to create the mongo user with this function:
function createAdminUser( [string] $mongoInstallPath, [string] $mongoAdminName, [string] $mongoAdminPass)
{
Write-Host "Creating admin user"
$createAdminCmd = `
"db.createUser( `
{ `
user: `"$mongoAdminName`",
pwd: `"$mongoAdminPass`",
roles: [ { role: `"userAdminAnyDatabase`", db: `"admin`" } ] `
} `
);"
"$createAdminCmd"
& "$mongoInstallPath\bin\mongo.exe" admin --port 27710 --eval "$createAdminCmd"
if ($LASTEXITCODE -ne 0)
{
throw "Failed to create admin user"
}
}
But this fails with the following error printed to screen (nothing about it is logged in the mongodb log). mongoAdminName is equal to administrator in this run, but it gives me the same results with the equivalent admin name if I use something else.
MongoDB shell version v3.4.15-49-g4ef027f98d connecting to: mongodb://127.0.0.1:27710/admin MongoDB server version: 3.4.15-49-g4ef027f98d 2018-06-13T16:10:00.125-0700 E QUERY [thread1] ReferenceError: administrator is not defined : @(shell eval):1:17
This happens even if I enter the expected command by hand in powershell. I use a similar command to create users on our custom mongodb docker container and have no problems with that, but I need a non-container solution in this case.
The plan is, once I've created the admin user, reexecute the createMongoService, this time with authCmd equal to "--auth", then use the admin user to create the database user with standard authentication practices. My questions are "whats going on here?!?" and "how do I fix this?"
Upvotes: 1
Views: 1703
Reputation: 151072
The error administrator is not defined
actually refers to an unquoted value present in the produced output. Basically that's the user
argument and if you change it to "bill"
then you get bill is not defined
. So it's not a "MongoDB error" but a JavaScript error of the interpreter.
This is because of the interpolation of double quotes inside double quotes, so the solution is to change the quote scheme instead:
$createAdminCmd =
"db.createUser(
{
user: '" + $mongoAdminName + "',
pwd: '" + $mongoAdminPass + "',
roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ]
}
);"
The alternate is to also escape the double quotes in a way that the JavaScript interpreter is happy with:
$createAdminCmd =
"db.createUser(
{
user: \`"$mongoAdminName\`",
pwd: \`"$mongoAdminPass\`",
roles: [ { role: \`"userAdminAnyDatabase\`", db: \`"admin\`" } ]
}
);"
Then the rest of it works just fine.
Remember that even though it "looks like" what you think you might just be typing in, from the perspective of the JavaScript interpreter which needs to "eval" the content before execution it's still "just a string". Therefore the same rules apply as they always do for JavaScript to interpolate a "string".
Upvotes: 2