Configuring Syracuse for MongoDB X509 Authentication
To enable X509 authentication for MongoDB, you need:
Make sure you add the Certificate entity instance (including the client certificate and the client private key file), as well as the
CA Certificate entity instance (including the CA certificate) to the MongoDB database before switching to X509 authentication.
Example: Assuming these instances are named "mongoclient" and "mongoca", you have to make the following adjustments to the collaboration
section of nodelocal.js.
exports.config = {// ...collaboration: {// ..mongoX509Cert: ["mongoclient", "mongoca"],}// ..}
In this example, you should find the following files on the certificate path of the Syracuse instance:
Alternatively, you can also enable X509 authentification with mongoX509Cert:true
, which allows you to use the raw certificate in mongoOtps.server.cert or mongoOtps.server.replSet.
Source from the Internet:
Configure X509 client authentication
The first step is to generate a CA certificate, if necessary. It is used to generate all MongoDB related certificates.
To do so, enter the following command in OpenSSL:
openssl req -x509 -days 365 -newkey rsa:2048 -out [CA_CERTIFICATE] -keyout [CA_KEY]
The MongoDB server requires a server certificate with the following characteristics:
Enter the following command in OpenSSL to generate the certificate request and sign the certificate:
openssl req -newkey rsa:2048 -keyout server-key.pem -out server-req.csropenssl x509 -req -days 360 -in server-req.csr -CA [CA_CERTIFICATE] -CAkey [CA_KEY] -CAcreateserial -out server-cert.crt
openssl rsa -in server-key.pem > server-key-no.pem
copy server-cert.crt+server-key-no.pem [SERVER_CERT_KEY]
Note: The subject must contain "O", "OU" or "C", as well as extra fields "keyUsage" and "extendedKeyUsage". The "O", "OU" and "C" values must not be identical to the corresponding values of the server certificate.
Caution: Do not use special characters (including blank spaces) in the subject as they might cause the authentication to fail.
To generate the MongoDB client certificate:
[mongoClient]keyUsage = digitalSignatureextendedKeyUsage = clientAuth
openssl req -newkey rsa:2048 -keyout [CLIENT_KEY] -out client-req.csropenssl x509 -req -days 360 -in client-req.csr -extensions mongoClient -extfile [CLIENT_SSL_CONFIG] -CA [CA_CERTIFICATE] -CAkey [CA_KEY] -CAcreateserial -out [CLIENT_CERTIFICATE]
For the first invocation of the MongoDB server without SSL, enter the following command:
mongod --port [PORT] [--logpath [LOGFILE]] [--smallfiles] [--dbpath [PATH_FOR_MONGODB]]
openssl x509 -in [CLIENT_CERTIFICATE] -inform PEM -subject -nameopt RFC2253 -noout
Subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
mongo [--port [PORT]] [--host [HOSTNAME]]
db.getSiblingDB("$external").runCommand({createUser: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry",roles: [{ role: 'readWrite', db: 'syracuse' },{ role: 'userAdminAnyDatabase', db: 'admin' }],writeConcern: { w: "majority" , wtimeout: 5000 }})
For the invocation of the MongoDB server with the X509 authentication, enter the following command:
mongod [--clusterAuthMode x509] --sslMode requireSSL --sslPEMKeyFile [SERVER_CERT_KEY] --sslCAFile [CA_CERTIFICATE] --port [PORT] [--logpath [LOGFILE]] [--smallfiles] [--dbpath [PATH_FOR_MONGODB]]
Note:
--clusterAuthMode
option is only necessary for replica sets or shared clusters, not for standalone MongoDB servers.Run the following command to test the configuration:
mongo --ssl --sslPEMKeyFile [CLIENT_CERT_KEY] --sslCAFile [CA_CERTIFICATE] --host [HOSTNAME] --port [PORT] -u [DN] --authenticationMechanism=MONGODB-X509 --authenticationDatabase='$external'
Note:
Below is a simple Javascript client to test the connection (Source: mongodb.github.io/tutorial):
var MongoClient = require('mongodb').MongoClient,fs = require('fs');// Read the cert and keyvar cert = fs.readFileSync([CLIENT_CERTIFICATE]);var key = fs.readFileSync([CLIENT_KEY]);var ca = fs.readFileSync([CA_CERTIFICATE]);// User name (obtained from cert - please adjust it)var userName = "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US";// Connect using the MONGODB-X509 authentication mechanismMongoClient.connect('mongodb://'+encodeURIComponent(userName)+'@[HOSTNAME]:[PORT]/syracuse?authMechanism=MONGODB-X509&ssl=true', {server:{sslKey:key,sslValidate: true,checkServerIdentity:false, // optionalsslCert:cert,sslCA:[ca]},sslKey:key,sslCert:cert,}, function(err, db) {if (err) console.log("Error", err);if(db) { console.log("Closing"); db.close();}});