I am Trying to connect to mongoDb using x509Authentication. I have generated the self signed certificates. Even I have provided the exact path to the Certificates in my code still I am getting bellow error in mongodb server:
{"t":{"$date":"2020-09-14T15:14:20.108+05:30"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn5","msg":"connection ended","attr":{"remote":"192.168.43.20:62215","connectionCount":1}} {"t":{"$date":"2020-09-14T15:14:47.154+05:30"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn4","msg":"connection ended","attr":{"remote":"192.168.43.20:62187","connectionCount":0}} {"t":{"$date":"2020-09-14T17:03:04.641+05:30"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"connection accepted","attr":{"remote":"192.168.43.20:62813","sessionId":6,"connectionCount":1}} {"t":{"$date":"2020-09-14T17:03:04.720+05:30"},"s":"E", "c":"NETWORK", "id":23280, "ctx":"conn6","msg":"No SSL certificate provided by peer; connection rejected"} {"t":{"$date":"2020-09-14T17:03:04.720+05:30"},"s":"I", "c":"NETWORK", "id":22988, "ctx":"conn6","msg":"Error receiving request from client. Ending connection from remote","attr":{"error":{"code":141,"codeName":"SSLHandshakeFailed","errmsg":"no SSL certificate provided by peer; connection rejected"},"remote":"192.168.43.20:62813","connectionId":6}} {"t":{"$date":"2020-09-14T17:03:04.721+05:30"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn6","msg":"connection ended","attr":{"remote":"192.168.43.20:62813","connectionCount":0}}
My Code:-
std::string ca_file = "D:/Certs/my.crt"; std::string pem_file = "D:/Certs/user.pem";
/*if (FILE *file = fopen(ca_file.c_str(), "r")) {
fclose(file);
}
if (FILE *file = fopen(pem_file.c_str(), "r")) {
fclose(file);
}*/
std::fstream filestream;
filestream.open(ca_file);
if (filestream.fail())
{
std::cout << "certificate not passed";
}
else
{
std::cout << "certificate passed"<<std::endl;
}
tls_options.ca_file(ca_file);
tls_options.ca_dir("D:/Certs/");
tls_options.pem_file(pem_file);
tls_options.pem_password("1234");
tls_options.allow_invalid_certificates(true);
client_options.tls_opts(tls_options);
m_cpp_client = NULL;
try
{
mongocxx::instance inst{};
mongocxx::client conn{ mongocxx::uri{"mongodb://192.168.43.20:27017/?authMechanism=MONGODB-X509&tls=true"},client_options};
// auto collection = conn["test"]["restaurants"];
mongocxx::database db = conn["admin"];
auto cursor1 = db.list_collections();
for (const bsoncxx::document::view& doc : cursor1)
{
bsoncxx::document::element ele = doc["name"];
std::string name = ele.get_utf8().value.to_string();
std::cout << name << std::endl;
}
}
Please let me know if you can help me