Skip to content

Commit bf432d3

Browse files
committed
use x509 auth for replicas
1 parent 733c5b1 commit bf432d3

File tree

3 files changed

+96
-11
lines changed

3 files changed

+96
-11
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ The monitoring script sets up email alerts and a monitoring endpoint for your Mo
297297

298298
## Managing Replica Sets
299299

300-
If you're setting up a replica set with multiple nodes, you'll need to add secondary nodes to the replica set.
300+
This deployment uses x509 certificate authentication for replica set members, providing enhanced security compared to the traditional keyFile authentication method.
301301

302302
1. **Set up secondary nodes**:
303303

@@ -311,17 +311,39 @@ If you're setting up a replica set with multiple nodes, you'll need to add secon
311311
sudo mkdir -p /etc/ssl/mongodb
312312
sudo cp /path/to/your/certificate.pem /etc/ssl/mongodb/certificate.pem
313313
sudo cp /path/to/your/ca_certificate.pem /etc/ssl/mongodb/certificate_authority.pem
314+
sudo cp /path/to/your/replicas.pem /etc/ssl/mongodb/replicas.pem
314315
sudo chmod 600 /etc/ssl/mongodb/certificate.pem
315316
sudo chmod 600 /etc/ssl/mongodb/certificate_authority.pem
317+
sudo chmod 600 /etc/ssl/mongodb/replicas.pem
316318
sudo chown mongodb:mongodb /etc/ssl/mongodb/certificate.pem
317319
sudo chown mongodb:mongodb /etc/ssl/mongodb/certificate_authority.pem
320+
sudo chown mongodb:mongodb /etc/ssl/mongodb/replicas.pem
318321

319322
# Configure TLS and set up monitoring
320323
./provision_ssl.sh
321324
./monitoring.sh secondary-node-domain.com
322325
```
323326

324327
The `provision_ssl.sh` script will automatically get the domain name from your config.json file and use it to configure the replica set.
328+
329+
**Note about x509 Authentication**: This deployment uses x509 certificate authentication for replica set members instead of the traditional keyFile method. The x509 certificates provide stronger security and are more flexible for certificate rotation.
330+
331+
The replica certificate should be placed at `/etc/ssl/mongodb/replicas.pem` on each node. This file must contain both the certificate and its private key in PEM format, similar to the server certificate. The certificate should be signed by the same CA as the server certificate.
332+
333+
Example of generating a replica certificate:
334+
```bash
335+
# Generate private key
336+
openssl genrsa -out replicas.key 2048
337+
338+
# Generate CSR (Certificate Signing Request)
339+
openssl req -new -key replicas.key -out replicas.csr -subj "/CN=mongodb-replicas"
340+
341+
# Sign with your CA
342+
openssl x509 -req -in replicas.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out replicas.crt -days 7300 -sha256
343+
344+
# Combine certificate and key into a single PEM file
345+
cat replicas.crt replicas.key > replicas.pem
346+
```
325347

326348
2. **Add secondary nodes to the replica set**:
327349

bootstrap.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ if [ -z "$MONGO_PORT" ] || [ "$MONGO_PORT" == "null" ]; then
5454
fi
5555
MONGO_VERSION=8.0
5656
MONGO_CONF="/etc/mongod.conf"
57-
MONGO_KEYFILE="/etc/mongo-keyfile"
5857
LOG_FILE="/var/log/mongodb/mongod.log"
5958
BACKUP_SCRIPT="/usr/local/bin/mongo_backup.sh"
59+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
6060

6161
# NOTE: Install MongoDB 8.0.
6262
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
@@ -71,12 +71,9 @@ echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gp
7171
sudo apt update
7272
sudo apt install -y mongodb-org
7373

74-
# NOTE: Create Mongo keyfile if missing.
75-
if [ ! -f "$MONGO_KEYFILE" ]; then
76-
echo "$REPLICA_SET_KEY" > "$MONGO_KEYFILE"
77-
chmod 400 "$MONGO_KEYFILE"
78-
chown mongodb:mongodb "$MONGO_KEYFILE"
79-
fi
74+
# NOTE: We'll use x509 certificates for internal authentication instead of keyFile
75+
echo "MongoDB will use x509 certificates for internal authentication"
76+
echo "Make sure to place your replica certificate at $REPLICA_CERT"
8077

8178
# NOTE: First create a MongoDB config without authentication and without replication
8279
cat <<EOF | sudo tee $MONGO_CONF
@@ -142,7 +139,6 @@ net:
142139
bindIp: 127.0.0.1
143140
security:
144141
authorization: enabled
145-
keyFile: $MONGO_KEYFILE
146142
EOF
147143

148144
# Restart MongoDB with authentication enabled
@@ -351,5 +347,6 @@ echo "Next steps:"
351347
echo "1. Place your private CA certificates at:"
352348
echo " - /etc/ssl/mongodb/certificate.pem"
353349
echo " - /etc/ssl/mongodb/certificate_authority.pem"
350+
echo " - /etc/ssl/mongodb/replicas.pem (for x509 authentication between replica set members)"
354351
echo "2. Run ./provision_ssl.sh to configure MongoDB to use the certificates."
355352
echo "3. Run ./monitoring.sh $DOMAIN to set up monitoring and alerts."

provision_ssl.sh

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ if [ -f "$MONGO_CONF" ]; then
5050
if grep -q "net:" "$MONGO_CONF" && ! grep -q " tls:" "$MONGO_CONF"; then
5151
# Add TLS configuration under existing net section (requiring client certificates)
5252
echo "Adding TLS configuration to existing net section..."
53-
sudo sed -i '/net:/a\ tls:\n mode: requireTLS\n certificateKeyFile: '"$CERT_FILE"'\n CAFile: '"$CA_FILE"'' "$MONGO_CONF"
53+
54+
# Check if replica certificate exists
55+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
56+
if [ -f "$REPLICA_CERT" ]; then
57+
echo "Using replica certificate for x509 authentication..."
58+
sudo sed -i '/net:/a\ tls:\n mode: requireTLS\n certificateKeyFile: '"$CERT_FILE"'\n CAFile: '"$CA_FILE"'\n clusterFile: '"$REPLICA_CERT"'' "$MONGO_CONF"
59+
else
60+
echo "Replica certificate not found, using standard TLS configuration..."
61+
sudo sed -i '/net:/a\ tls:\n mode: requireTLS\n certificateKeyFile: '"$CERT_FILE"'\n CAFile: '"$CA_FILE"'' "$MONGO_CONF"
62+
fi
5463

5564
# Update bindIp to listen on all interfaces
5665
echo "Updating bindIp to listen on all interfaces..."
@@ -61,6 +70,17 @@ if [ -f "$MONGO_CONF" ]; then
6170
sudo sed -i '/tls:/,/[a-z]/ s|certificateKeyFile:.*|certificateKeyFile: '"$CERT_FILE"'|' "$MONGO_CONF"
6271
sudo sed -i '/tls:/,/[a-z]/ s|CAFile:.*|CAFile: '"$CA_FILE"'|' "$MONGO_CONF"
6372

73+
# Check if replica certificate exists
74+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
75+
if [ -f "$REPLICA_CERT" ]; then
76+
echo "Using replica certificate for x509 authentication..."
77+
if grep -q "clusterFile:" "$MONGO_CONF"; then
78+
sudo sed -i '/clusterFile:/c\ clusterFile: '"$REPLICA_CERT"'' "$MONGO_CONF"
79+
else
80+
sudo sed -i '/CAFile:/a\ clusterFile: '"$REPLICA_CERT"'' "$MONGO_CONF"
81+
fi
82+
fi
83+
6484
# Remove any relaxed security settings if they exist
6585
sudo sed -i '/allowConnectionsWithoutCertificates:/d' "$MONGO_CONF"
6686
sudo sed -i '/allowInvalidHostnames:/d' "$MONGO_CONF"
@@ -72,7 +92,16 @@ if [ -f "$MONGO_CONF" ]; then
7292
elif ! grep -q "net:" "$MONGO_CONF"; then
7393
# Add net section with TLS configuration
7494
echo "Adding new net section with TLS configuration..."
75-
echo -e "\nnet:\n bindIp: 0.0.0.0\n tls:\n mode: requireTLS\n certificateKeyFile: $CERT_FILE\n CAFile: $CA_FILE" | sudo tee -a "$MONGO_CONF"
95+
96+
# Check if replica certificate exists
97+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
98+
if [ -f "$REPLICA_CERT" ]; then
99+
echo "Using replica certificate for x509 authentication..."
100+
echo -e "\nnet:\n bindIp: 0.0.0.0\n tls:\n mode: requireTLS\n certificateKeyFile: $CERT_FILE\n CAFile: $CA_FILE\n clusterFile: $REPLICA_CERT" | sudo tee -a "$MONGO_CONF"
101+
else
102+
echo "Replica certificate not found, using standard TLS configuration..."
103+
echo -e "\nnet:\n bindIp: 0.0.0.0\n tls:\n mode: requireTLS\n certificateKeyFile: $CERT_FILE\n CAFile: $CA_FILE" | sudo tee -a "$MONGO_CONF"
104+
fi
76105
fi
77106

78107
# Remove any old SSL configuration if it exists
@@ -82,6 +111,43 @@ if [ -f "$MONGO_CONF" ]; then
82111
fi
83112
fi
84113

114+
# Check for replica certificate
115+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
116+
if [ ! -f "$REPLICA_CERT" ]; then
117+
echo "⚠️ WARNING: Replica certificate not found at $REPLICA_CERT"
118+
echo "Please ensure the replica certificate is placed at $REPLICA_CERT before running this script."
119+
echo "This certificate is required for x509 authentication between replica set members."
120+
else
121+
echo "✅ Replica certificate found at $REPLICA_CERT"
122+
# Ensure proper permissions
123+
sudo chmod 600 "$REPLICA_CERT"
124+
sudo chown mongodb:mongodb "$REPLICA_CERT"
125+
fi
126+
127+
# Configure x509 authentication for replica set members
128+
REPLICA_CERT="/etc/ssl/mongodb/replicas.pem"
129+
if [ -f "$REPLICA_CERT" ]; then
130+
echo "Configuring x509 authentication for replica set members..."
131+
132+
# Check if security section exists
133+
if grep -q "security:" "$MONGO_CONF"; then
134+
# Add x509 authentication to existing security section
135+
if ! grep -q "clusterAuthMode:" "$MONGO_CONF"; then
136+
sudo sed -i '/security:/a\ clusterAuthMode: x509' "$MONGO_CONF"
137+
else
138+
sudo sed -i '/clusterAuthMode:/c\ clusterAuthMode: x509' "$MONGO_CONF"
139+
fi
140+
else
141+
# Add security section with x509 authentication
142+
echo -e "\nsecurity:\n authorization: enabled\n clusterAuthMode: x509" | sudo tee -a "$MONGO_CONF"
143+
fi
144+
145+
echo "✅ x509 authentication configured for replica set members"
146+
else
147+
echo "⚠️ WARNING: Replica certificate not found at $REPLICA_CERT"
148+
echo "x509 authentication for replica set members will not be configured."
149+
fi
150+
85151
# Add or update replication section
86152
if grep -q "replication:" "$MONGO_CONF"; then
87153
echo "Updating existing replication configuration..."

0 commit comments

Comments
 (0)