Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save siddhantprateek/a80b8ee13678bc14e9c93c4eefa14bd9 to your computer and use it in GitHub Desktop.

Select an option

Save siddhantprateek/a80b8ee13678bc14e9c93c4eefa14bd9 to your computer and use it in GitHub Desktop.
Generate self-signed SSL certificates for MongoDb server and client

Make PEM containig a public key certificate and its associated private key

openssl req -newkey rsa:2048 -new -x509 -days 3650 -nodes -out mongo.crt -keyout mongo.key
cat mongo.key mongo.crt > mongo.pem

Edit /etc/mongod.conf, network interfaces section

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1
  ssl:
    mode: allowSSL
    PEMKeyFile: /etc/ssl/mongo.pem
    #CAFile: /etc/ssl/mongo.crt

Check for startup config errors

sudo mongod --config /etc/mongod.conf

Restart mongo

sudo service mongod restart

Test-connect

mongo --ssl --sslAllowInvalidHostnames --sslAllowInvalidCertificates

NodeJs, mongo connection options

{ 
  "sslValidate": false,
  "sslKey": fs.readFileSync('/etc/ssl/mongodb.pem'),
  "sslCert": fs.readFileSync('/etc/ssl/mongodb-cert.crt')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment