coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

Problem with authentication. #121

Closed BorisBabiy closed 11 years ago

BorisBabiy commented 11 years ago

Hi guys, I have a problem, we use Shanty Mongo with Zend Framework 1.12, and I want add authentication.

Server Ubuntu 12.04, PHP Mongo 1.4.1 stable, mongod 2.2.4

Here is Zend configuration example: ; Shanty Mongo setup hosts.0.host = "test.host.com:27017" hosts.1.host = "test.host.com:27117" hosts.2.host = "test.host.com:27217" database = "db_test" replicaSet = true username = "user" password = "12345"

We run 3 instance of mongod on different ports for replicaset.

If I add user to my db_test it's work fine. But when I add user to admin database I can't connect anymore. I try to remove/add users, try different combinations, but when I add user to admin database I can connect only from cli shell.

BorisBabiy commented 11 years ago

Maybe It's can be helpful:

I add small script: /* Configure logging */ MongoLog::setModule( MongoLog::ALL ); MongoLog::setLevel( MongoLog::ALL );

$m = new MongoClient(); // connect $db = $m->selectDB("db_test"); $db->authenticate('user','12345');

When I add user to db_test it's work: PHP Notice: CON INFO: authenticate: start in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: hash=md5(user:mongo:12345) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: key=md5(...) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: read from header: 36 in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: data_size: 77 in /home/boris/mongo.php on line 9 PHP Notice: CON INFO: authentication successful in /home/boris/mongo.php on line 9

but after this, I remove user from db_test, and add user with same credentials to admin database, and got this: PHP Notice: CON INFO: authenticate: start in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: hash=md5(user:mongo:12345) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: key=md5(...) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: read from header: 36 in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: data_size: 40 in /home/boris/mongo.php on line 9 PHP Notice: CON WARN: authentication failed in /home/boris/mongo.php on line 9

if I had the same user in both databases, I think they should be the same I get this: PHP Notice: CON INFO: authenticate: start in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: hash=md5(user:mongo:12345) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: authenticate: key=md5(...) = ... in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: read from header: 36 in /home/boris/mongo.php on line 9 PHP Notice: CON FINE: send_packet: data_size: 77 in /home/boris/mongo.php on line 9 PHP Notice: CON INFO: authentication successful in /home/boris/mongo.php on line 9

But connection with Shanty doesn't work anyway.

BorisBabiy commented 11 years ago

I figured out this. Maybe it's will be heplful for some one on future, Shanty-Mongo require username, password and port in array of servers. It's should looks like:

; Shanty Mongo Config hosts.0.host = '127.0.0.1' hosts.0.port = '27017' hosts.0.username = 'root' hosts.0.password = 'pass' hosts.1.host = '127.0.0.1' hosts.1.port = '27018' hosts.1.username = 'root' hosts.1.password = 'pass' hosts.2.host = '127.0.0.1' hosts.2.port = '27019' hosts.2.username = 'root' hosts.2.password = 'pass' database = 'database_test' replicaSet = true Also what I do: in mongo: 1. edit config file, enable auth (auth = true) 2. create user in admin database 3. connect as new user 4. create user in target database in ZF config update config file, as I describe above.

Also some times I get error: Fatal error: php_network_getaddresses, I think it's because I have some trouble with DNS or php trying resolve address: mongo://root:pass@127.0.0.1:27017,root:pass@127.0.0.1:27018,root:pass@127.0.0.1:27019/database_test