How can I connect with X509 by putting all options in the connection string in node.js driver for mongodb?

白昼怎懂夜的黑 提交于 2021-02-05 09:20:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!