@@ -48,7 +48,7 @@ if [ -f "$MONGO_CONF" ]; then
48
48
else
49
49
# Check if net section already exists
50
50
if grep -q " net:" " $MONGO_CONF " && ! grep -q " tls:" " $MONGO_CONF " ; then
51
- # Add TLS configuration under existing net section
51
+ # Add TLS configuration under existing net section (requiring client certificates)
52
52
echo " Adding TLS configuration to existing net section..."
53
53
sudo sed -i ' /net:/a\ tls:\n mode: requireTLS\n certificateKeyFile: ' " $CERT_FILE " ' \n CAFile: ' " $CA_FILE " ' ' " $MONGO_CONF "
54
54
124
124
125
125
# Verify MongoDB is running with TLS
126
126
echo " Waiting for MongoDB to start completely..."
127
- sleep 5
127
+
128
+ sleep 10
129
+
128
130
if sudo systemctl is-active --quiet mongod; then
129
131
echo " ✅ MongoDB restarted successfully with TLS configuration"
130
132
@@ -138,14 +140,20 @@ if sudo systemctl is-active --quiet mongod; then
138
140
MONGO_PORT=$( jq -r ' .mongo_port' " $CONFIG_FILE " )
139
141
140
142
if command -v mongosh & > /dev/null; then
141
- # Try with localhost
142
- echo " Attempting to verify TLS using localhost "
143
- if sudo mongosh --host localhost --port $MONGO_PORT --tls -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval " db.adminCommand({ getParameter: 1, tlsMode: 1 })" 2> /dev/null | grep -q " requireTLS" ; then
144
- echo " ✅ MongoDB TLS mode verified using localhost : requireTLS is active"
143
+ # Try with domain name and client certificate
144
+ echo " Attempting to verify TLS using domain name "
145
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval " db.adminCommand({ getParameter: 1, tlsMode: 1 })" 2> /dev/null | grep -q " requireTLS" ; then
146
+ echo " ✅ MongoDB TLS mode verified using domain name : requireTLS is active"
145
147
else
146
148
echo " ⚠️ WARNING: MongoDB is running but TLS mode could not be verified."
147
- echo " Please check manually with:"
148
- echo " mongosh --host localhost --port $MONGO_PORT --tls -u $DB_USERNAME -p <password> --authenticationDatabase admin --eval \" db.adminCommand({ getParameter: 1, tlsMode: 1 })\" "
149
+ echo " This is expected because client certificates are required."
150
+ echo " "
151
+ echo " IMPORTANT: To connect to MongoDB, you will need:"
152
+ echo " 1. A client certificate signed by your CA"
153
+ echo " 2. Connect using the domain name that matches your server certificate"
154
+ echo " "
155
+ echo " Example connection command:"
156
+ echo " mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p <password> --authenticationDatabase admin"
149
157
fi
150
158
else
151
159
echo " ⚠️ mongosh not available to verify TLS configuration."
@@ -204,12 +212,12 @@ if [ -f "$CONFIG_FILE" ]; then
204
212
fi
205
213
206
214
# Check if the node is already initialized (part of a replica set)
207
- 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
215
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " JSON.stringify(rs.status())" 2> /dev/null | grep -q ' "ok":1' ; then
208
216
IS_INITIALIZED=true
209
217
echo " This node is already initialized as part of a replica set."
210
218
211
219
# Now check if it's primary
212
- 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
220
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " JSON.stringify(rs.isMaster())" 2> /dev/null | grep -q ' "ismaster":true' ; then
213
221
IS_PRIMARY=true
214
222
echo " This node is the primary."
215
223
else
@@ -226,7 +234,7 @@ if [ -f "$CONFIG_FILE" ]; then
226
234
echo " Initializing replica set with domain name..."
227
235
228
236
# Initialize the replica set with the domain name instead of localhost
229
- if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "
237
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "
230
238
rs.initiate({
231
239
_id: '$REPLICA_SET ',
232
240
members: [{ _id: 0, host: '$DOMAIN :$MONGO_PORT ' }]
@@ -236,25 +244,25 @@ if [ -f "$CONFIG_FILE" ]; then
236
244
237
245
# Verify the initialization
238
246
echo " Verifying replica set configuration..."
239
- mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " rs.conf().members.forEach(function(m) { print(m.host); })"
247
+ mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " rs.conf().members.forEach(function(m) { print(m.host); })"
240
248
else
241
249
echo " ❌ ERROR: Failed to initialize replica set. You may need to initialize it manually."
242
250
echo " Manual initialization command:"
243
- echo " mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval \" rs.initiate({ _id: '$REPLICA_SET ', members: [{ _id: 0, host: '$DOMAIN :$MONGO_PORT ' }] })\" "
251
+ echo " mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval \" rs.initiate({ _id: '$REPLICA_SET ', members: [{ _id: 0, host: '$DOMAIN :$MONGO_PORT ' }] })\" "
244
252
fi
245
253
elif [ " $IS_INITIALIZED " = true ]; then
246
254
# This is an already initialized primary node, check if we need to update the configuration
247
255
echo " Checking if replica set configuration needs to be updated..."
248
256
249
257
# Get current replica set configuration
250
258
TEMP_FILE=$( mktemp)
251
- if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " JSON.stringify(rs.conf())" > $TEMP_FILE 2> /dev/null; then
259
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " JSON.stringify(rs.conf())" > $TEMP_FILE 2> /dev/null; then
252
260
# Check if any member is using localhost
253
261
if grep -q " localhost" $TEMP_FILE ; then
254
262
echo " Found localhost in replica set configuration. Updating to use domain name..."
255
263
256
264
# Update replica set configuration to use domain name
257
- if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "
265
+ if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "
258
266
var config = rs.conf();
259
267
for (var i = 0; i < config.members.length; i++) {
260
268
if (config.members[i].host.includes('localhost')) {
@@ -268,11 +276,11 @@ if [ -f "$CONFIG_FILE" ]; then
268
276
269
277
# Verify the update
270
278
echo " Verifying updated configuration..."
271
- mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " rs.conf().members.forEach(function(m) { print(m.host); })"
279
+ mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval " rs.conf().members.forEach(function(m) { print(m.host); })"
272
280
else
273
281
echo " ⚠️ WARNING: Failed to update replica set configuration. You may need to update it manually."
274
282
echo " Manual update command:"
275
- echo " mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval \" var config = rs.conf(); config.members[0].host = '$DOMAIN :$MONGO_PORT '; rs.reconfig(config);\" "
283
+ echo " mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval \" var config = rs.conf(); config.members[0].host = '$DOMAIN :$MONGO_PORT '; rs.reconfig(config);\" "
276
284
fi
277
285
else
278
286
echo " Replica set configuration already using domain name. No update needed."
309
317
310
318
echo " ✅ TLS configuration complete"
311
319
echo " MongoDB is now configured to use TLS with the certificate at $CERT_FILE "
312
- echo " Clients will need to connect using TLS"
320
+ echo " "
321
+ echo " IMPORTANT: Client certificates are required for connections"
322
+ echo " To connect to MongoDB, you will need:"
323
+ echo " 1. A client certificate signed by your CA"
324
+ echo " 2. Connect using the domain name that matches your server certificate"
325
+ echo " "
326
+ echo " Example connection command:"
327
+ echo " mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p <password> --authenticationDatabase admin"
0 commit comments