Connection code snippets for MongoDB drivers using x.509 authentication
https://www.mongodb.com/docs/manual/tutorial/manage-mongodb-processes/#start-mongod-processes
https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/
db.getSiblingDB("$external").runCommand(
{
createUser: "CN=ChrisChoClient,OU=TestClientCertificateOrgUnit,O=TestClientCertificateOrg,L=TestClientCertificateLocality,ST=TestClientCertificateState,C=US",
roles: [
{ role: "readWrite", db: "test" },
{ role: "userAdminAnyDatabase", db: "admin" }
],
writeConcern: { w: "majority" , wtimeout: 5000 }
}
)
Record your user values since your client certificate will need to match them.
https://www.mongodb.com/docs/manual/tutorial/manage-mongodb-processes/#stop-mongod-processes
Create hostnames in /etc/hosts
. e.g. 127.0.0.1 localmongo1
-
create openssl server certificate When creating "openssl-test-server.cnf", fill in the DNS.1 and IP.1 settings with the local hostname you set up in /etc/hosts. E.g. "DNS.1 = localmongo1" and "IP.1 = 127.0.0.1". Delete DNS.2 and IP.2. Make sure all the certificate values match the client certificate / user values except for either the Organization (O) or Organization Unit (OU).
mongod --dbpath=/Users/chris.cho/dev/mongo_data/6.0.0 --replSet \"myRS\" --tlsMode requireTLS --tlsCertificateKeyFile test-server1.pem --tlsCAFile test-ca.pem --bind_ip localmongo1 --tlsAllowInvalidCertificates
mongosh --tls --host localmongo1 --tlsCertificateKeyFile test-client.pem --tlsCAFile test-ca.pem --authenticationMechanism MONGODB-X509 --authenticationDatabase='$external' --tlsAllowInvalidCertificates
Required for specific drivers.
$ cat mongodb-test-client.crt mongodb-test-ia.crt > client-bundle.crt
$ openssl pkcs12 -export -out client-certificate.pfx -inkey mongodb-test-client.key -in client-bundle.crt
<choose a password>
$ keytool -importkeystore -destkeystore client.keystore -srckeystore client-certificate.pfx -srcstoretype pkcs12 -alias client-cert
$ keytool -list -keystore client.keystore
$ keytool -import -alias server-cert -file test-server1.pem -keystore client.truststore
$ keytool -import -alias client-cert -file test-client.pem -keystore client.truststore