How to reliable check if client is really valid

1,116 views
Skip to first unread message

henning...@celeraone.com

unread,
Jun 27, 2016, 10:26:08 AM6/27/16
to mongodb-user
I have the follwing code
const auto uri_str = "mongodb://wrong:pa...@127.0.0.1:27017/unittest";
//const auto uri_str = "mongodb://foo:b...@127.0.0.1:27017/unittest";

const auto uri = mongocxx::uri{ uri_str };
auto client = mongocxx::client(uri);

if (!client)
{
    std
::cerr << "Couldn't connect to DB." << std::endl;
}
else
{
    std
::cerr << "Connected to DB!" << std::endl;
}
I try to find out if the client is a vlid client in terms of the authentication of the user. In the example i used two different user which give me the same result. Both result in "Connected to DB!", but the first user is not valid. In the old mongo db c++ driver it was possible to check authentication with auth() on a ScopedDBConnection, but this is not possible anymore. How can i test if the client is valid with the provided URI?

I tried to call the authentication command via the run_command() function as follows
bsoncxx::builder::stream::document auth;
auth
<< "authenticate"
     
<< bsoncxx::builder::stream::open_document
     
<< "user" << "foo"
     
<< "pwd" << "bar"
     
//<< "digestPassword" << false
     
//<< "mechanism" << "MONGODB-CR"
     
<< bsoncxx::builder::stream::close_document;

std
::cout << bsoncxx::to_json(auth.view()) << std::endl;

try
{
   
auto result = db.run_command(auth.view());
    std
::cout << bsoncxx::to_json(result) << std::endl;
}
catch (const mongocxx::exception & e)
{
    std
::cerr << "mongocxx::exception - " << e.what() << std::endl;
}
But this always gives me the following error "field missing/wrong type in received authenticate command: generic server error". The documentation for the authentication command is a bit unclear in terms of how the BSON should look like. https://docs.mongodb.com/manual/reference/method/db.auth/#db.auth

Can you tell me how to reliable check if a connection to the database with the supplied information was successful or not and how to call the correctly call the authentication command.

Best regards
Henning

David Golden

unread,
Jun 30, 2016, 11:23:22 AM6/30/16
to mongodb-user
Generally, the current generation of drivers are designed to succeed during client construction, regardless of the state of servers, so that application startup isn't interrupted by a (possibly transient) server or network error.  Errors (such as authentication problems) are thrown when an operation can't complete.  In your case, try an operation that requires authentication.

Regards,
David
Message has been deleted

henning...@celeraone.com

unread,
Jul 1, 2016, 4:09:36 AM7/1/16
to mongodb-user
bool MDB::connected() const
{
   
Logger log(LOGGER_NAME);

   
if (!client)
   
{
        log
.e() << "MongoDB connection is not initialized/valid: "
               
<< uri << " : " << database
               
<< std::endl;
       
return false;
   
}

   
try
   
{
        bsoncxx
::builder::stream::document ping;
        ping
<< "ping" << 1;
       
auto db = (**client)[database];
       
auto result = db.run_command(ping.view());

       
if (result.view()["ok"].get_double() != 1)
       
{
            log
.e() << "Ping to database failed: "
                   
<< uri << " : " << database << std::endl;
           
return false;

       
}
   
}
   
catch (const mongocxx::exception & e)
   
{

        log
.e() << "Run command failed: "
               
<< uri << " : " << database << std::endl;
       
return false;
   
}

   
return true;
}

I know that the authentication is part of the URI, but imagine you have a client in which you are logged in as an admin. Now you want to create a user and than test its access rights. Therefore an authentication method would be good.

David Golden

unread,
Jul 1, 2016, 9:44:08 AM7/1/16
to mongodb-user
In that circumstance, I recommend creating a new client with the new credentials and trying an operation that requires auth.

Reply all
Reply to author
Forward
0 new messages