问题
I'm using the keys available in mongodb docs for a self-signed certificate using X509 authentication with a Node.js Driver.
When trying to connect to the database I get the following error:
MongoNetworkError: failed to connect to server [pedro.com:57040] on first connect [MongoNetworkError: unable to verify the first certificate]
Despite that error, I am sure that the connection works as I am using Studio 3T and everything is up and running by using the same certificates.
So my question is... how can I programmatically connect to a database using x509 certificate by writing all the options in the connection string? Perhaps the keys that I am giving are wrong? Or some are missing?
db: `mongodb://CN=pedro.com,OU=IT,O=Polygon,L=Setubal,ST=Setubal,C=PT@pedro.com:57040,pedro.com:57041
,pedro.com:57042/testBO?
ssl=true&authMechanism=MONGODB-X509
&sslPEMKeyFile=${fs.readFileSync(
`${Helpers.appRoot(Env.get('DB_SSL_CERT'))}`
)}
&sslCAFile=${fs.readFileSync(
`${Helpers.appRoot(Env.get('DB_SSL_CA'))}`
)}&replicaSet=testReplica`
回答1:
You need to ensure both server and client can verify each other's certificates. The two verifications are independent and use different options.
For server side see https://github.com/mongodb/mongo-ruby-driver/blob/master/.evergreen/run-tests.sh#L74, for client side see https://github.com/mongodb/mongo-ruby-driver/blob/master/.evergreen/run-tests.sh#L91. URI options are standardized across drivers.
Also see https://github.com/mongodb/mongo-ruby-driver/blob/master/spec/README.md#x509-authentication.
sslPEMKeyFile
is a legacy node driver option (assuming it is valid), the tls* options take paths to files and not file contents.
来源:https://stackoverflow.com/questions/63188502/how-can-i-connect-with-x509-by-putting-all-options-in-the-connection-string-in-n