问题
I completed the following tutorial (https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89) to set up self-signed SSL certificates to secure communications between a device and a server hosting a mongoDB database.
I can access the database from the server and from the device with the following command:
mongo --ssl --sslCAFile /path/to/CA.pem --sslPEMKeyFile /path/to/mongodb.pem --host IP:port
Error
When I try to connect to the database with a C++ program, I get a seg fault:
Segmentation fault (core dumped)
Output from GDB is
Program received signal SIGSEGV, Segmentation fault.
0x0000007fb7f6d6a8 in mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) () from /usr/local/lib/libmongocxx.so._noabi
C++ code
I instanciate the connection with:
mongocxx::instance instance{};
mongocxx::options::ssl ssl_opts;
ssl_opts.pem_file("/path/to/mongodb.pem");
// ssl_opts.allow_invalid_certificates(false); // I have tried this
mongocxx::options::client client_opts;
client_opts.ssl_opts(ssl_opts);
auto client = mongocxx::client{mongocxx::uri{"mongodb://user:pwd@IP:port/?authMechanism=MONGODB-X509&ssl=true"}, client_opts};
And compile with the following command:
c++ --std=c++11 main.cpp $(pkg-config --cflags --libs libmongocxx) -Wl,-rpath,/usr/local/lib
I have not been able to fix this error or find a solution online, any help would be much appreciated.
Additional Information
Versions:
Mongo c - 1.10.1
Mongo cxx - 3.3.0
Backtrace:
(gdb) thread apply all bt
Thread 1 (Thread 0x7fb7ff4000 (LWP 17800)):
#0 0x0000007fb7f6d6a8 in
mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&,
mongocxx::v_noabi::options::client const&) () from
/usr/local/lib/libmongocxx.so._noabi
#1 0x00000000004027d0 in main ()
回答1:
There's a known serious bug with passing options::ssl_opts
to the client constructor in the C++ driver version 3.3.0 causing the segfault. This is fixed in 3.3.1. It is highly recommended that you upgrade.
As a workaround for 3.3.0, you can pass the pem_file option through the URI string. The URI option "sslclientcertificatekeyfile"
corresponds to the options::ssl::pem_file
option. E.g:
auto uri = mongocxx::uri{"mongodb://localhost/?ssl=true&sslclientcertificatekeyfile=/path/to/mongodb.pem"};
But do upgrade to 3.3.1 if possible.
来源:https://stackoverflow.com/questions/52759683/mongocxx-fails-to-connect-to-mongodb-with-ssl