Skip to content

Commit 8f5547d

Browse files
committed
ensure that authentication is enabled after admin
user is created
1 parent 60aed80 commit 8f5547d

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

bootstrap.sh

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if [ ! -f "$MONGO_KEYFILE" ]; then
7878
chown mongodb:mongodb "$MONGO_KEYFILE"
7979
fi
8080

81-
# NOTE: Update mongod.conf.
81+
# NOTE: First create a MongoDB config without authentication
8282
cat <<EOF | sudo tee $MONGO_CONF
8383
storage:
8484
dbPath: /var/lib/mongodb
@@ -89,9 +89,6 @@ systemLog:
8989
net:
9090
port: $MONGO_PORT
9191
bindIp: 127.0.0.1
92-
security:
93-
authorization: enabled
94-
keyFile: $MONGO_KEYFILE
9592
replication:
9693
replSetName: $REPLICA_SET
9794
EOF
@@ -112,7 +109,46 @@ if [ "$ROLE" == "primary" ]; then
112109
fi
113110

114111
# NOTE: Create admin user.
115-
mongosh --port $MONGO_PORT --eval "db.getSiblingDB('admin').createUser({ user: '$DB_USERNAME', pwd: '$DB_PASSWORD', roles: [ { role: 'root', db: 'admin' } ] })" || echo "Admin user may already exist."
112+
echo "Creating admin user..."
113+
if mongosh --port $MONGO_PORT --eval "db.getSiblingDB('admin').createUser({ user: '$DB_USERNAME', pwd: '$DB_PASSWORD', roles: [ { role: 'root', db: 'admin' } ] })"; then
114+
echo "✅ Admin user created successfully"
115+
else
116+
echo "❌ Failed to create admin user"
117+
exit 1
118+
fi
119+
120+
# NOTE: Now update the config to enable authentication
121+
echo "Enabling authentication in MongoDB configuration..."
122+
cat <<EOF | sudo tee $MONGO_CONF
123+
storage:
124+
dbPath: /var/lib/mongodb
125+
systemLog:
126+
destination: file
127+
path: $LOG_FILE
128+
logAppend: true
129+
net:
130+
port: $MONGO_PORT
131+
bindIp: 127.0.0.1
132+
security:
133+
authorization: enabled
134+
keyFile: $MONGO_KEYFILE
135+
replication:
136+
replSetName: $REPLICA_SET
137+
EOF
138+
139+
# Restart MongoDB with authentication enabled
140+
echo "Restarting MongoDB with authentication enabled..."
141+
sudo systemctl restart mongod
142+
sleep 5
143+
144+
# Verify we can connect with authentication
145+
echo "Verifying authentication..."
146+
if mongosh --port $MONGO_PORT -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')"; then
147+
echo "✅ Authentication working correctly"
148+
else
149+
echo "❌ Authentication verification failed"
150+
exit 1
151+
fi
116152

117153
# NOTE: Setup log rotation.
118154
cat <<EOF | sudo tee /etc/logrotate.d/mongod

provision_ssl.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ if [ -f "$CONFIG_FILE" ]; then
178178
fi
179179

180180
# Check if the node is already initialized (part of a replica set)
181-
if mongosh --host localhost --port $MONGO_PORT --quiet --eval "JSON.stringify(rs.status())" 2>/dev/null | grep -q '"ok":1'; then
181+
if mongosh --host localhost --port $MONGO_PORT -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" 2>/dev/null | grep -q '"ok":1'; then
182182
IS_INITIALIZED=true
183183
echo "This node is already initialized as part of a replica set."
184184

185185
# Now check if it's primary
186-
if mongosh --host localhost --port $MONGO_PORT --quiet --eval "JSON.stringify(rs.isMaster())" 2>/dev/null | grep -q '"ismaster":true'; then
186+
if mongosh --host localhost --port $MONGO_PORT -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.isMaster())" 2>/dev/null | grep -q '"ismaster":true'; then
187187
IS_PRIMARY=true
188188
echo "This node is the primary."
189189
else

0 commit comments

Comments
 (0)