mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.01k stars 1.43k forks source link

How to config with SSL option #2077

Closed yunier0525 closed 4 years ago

yunier0525 commented 4 years ago

Hi,

I have this command line that works fine: /# mongo --tls "mongodb://user:pass@host.com:27017/db" --tlsCAFile /var/www/.ssh/mongodb-certificate-develop.pem

The problem is when I try to setup the same config on laravel. With this config, I get "Exception Message:↵↵Authorization failure", after many tries I know that the problems is with ssl configuration.

database.php

'mongodb' =>
        [
            'driver' => 'mongodb',
            'dsn' => env('MONGO_DB_DSN'),
            'database' => env('MONGO_DB_DATABASE', 'db'),
            'options'  => [
                'tls' => true,
                'tlsCAFile' => '/var/www/.ssh/mongodb-certificate-develop.pem'
            ],
        ]

.env

MONGO_DB_DSN=mongodb://user:pass@host.com:27017/db
MONGO_DB_DATABASE=db

I read many comments with this respect but I can't find a solution.

What is the correct way to config ssl certificate ?

divine commented 4 years ago

Hi!

Are you using Documentdb?

Thanks!

yunier0525 commented 4 years ago

Hi,

Yes, I am using DocumentDB, but I ask the question because in MongoDB you can also configure SSL, so in that case how would I do it?

Thanks!

yunier0525 commented 4 years ago

Hi,

Now I disabled the TLS option on DocumentDB, and I get this results:

Command: Works fine mongo "mongodb://user:pass@host.com:27017/db"

PHP code: Work fine

$client = new MongoDB\Client('mongodb://user:pass@host.com:27017/db');
var_dump( $client->test);
MongoDB\Database {#4960
     databaseName: "test",
     manager: MongoDB\Driver\Manager {#4968},
     readConcern: MongoDB\Driver\ReadConcern {#4963},
     readPreference: MongoDB\Driver\ReadPreference {#4973
       +"mode": "primary",
     },
     typeMap: [
       "array" => "MongoDB\Model\BSONArray",
       "document" => "MongoDB\Model\BSONDocument",
       "root" => "MongoDB\Model\BSONDocument",
     ],
     writeConcern: MongoDB\Driver\WriteConcern {#4956},
   }

MongoDB Compass: Works fine

Laravel with laravel-mongodb: Don't work Error: Exception Message:↵↵Authorization failure Config: database.php

'mongodb' =>
        [
            'driver' => 'mongodb',
            'dsn' => env('MONGO_DB_DSN'),
            'database' => env('MONGO_DB_DATABASE', 'db'),
        ]

.env

MONGO_DB_DSN=mongodb://user:pass@host.com:27017/db
MONGO_DB_DATABASE=db
Smolevich commented 4 years ago

@yunier0525, Why do you use MONGO_DB_DATABASE=develop but database in MONGO_DB_DSN is db ?

yunier0525 commented 4 years ago

@yunier0525, Why do you use MONGO_DB_DATABASE=develop but database in MONGO_DB_DSN is db ?

Is dummy data, in my code the two values are the same. I'm updated the comment with de name of db.

divine commented 4 years ago

Hi,

Yes, I am using DocumentDB, but I ask the question because in MongoDB you can also configure SSL, so in that case how would I do it?

Thanks!

You've got an answer from me here https://github.com/jenssegers/laravel-mongodb/issues/2074, not sure why are you trying to ask same questions twice.

SSL option works with MongoDB but there are some problems with DocumentDB compatibility.

Please check this issue for possible solutions https://github.com/jenssegers/laravel-mongodb/issues/1709

yunier0525 commented 4 years ago

Hi @divine ,

I opened this question again cause I read every comment to this respect and I can't find a correct answer. I apologize to duplicate the question, but you close the issue assuming that the problem is resolved in others comments.

If you tell me DocumentDB is not compatible with laravel-mongodb ok, but give me a clear answer.

I think laravel-mongodb have a issue here, you decide if you wanna see the problem.

Regards.

divine commented 4 years ago

Hi @divine ,

I opened this question again cause I read every comment to this respect and I can't find a correct answer. I apologize to duplicate the question, but you close the issue assuming that the problem is resolved in others comments.

If you tell me DocumentDB is not compatible with laravel-mongodb ok, but give me a clear answer.

I think laravel-mongodb have a issue here, you decide if you wanna see the problem.

Regards.

It's already an open issue here https://github.com/jenssegers/laravel-mongodb/issues/1709 , there is no reason to create multiple new issues when there is one open.

See this for clarification regarding compatibility https://www.mongodb.com/atlas-vs-amazon-documentdb

Smolevich commented 4 years ago

@yunier0525, call this code and attach dump here, please

$client = new MongoDB\Client('mongodb://user:pass@host.com:27017/db');
$collection = $client->test->test_collection;
$result = $collection->insertOne( [ 'name' => 'Test #2'] );
$result = $collection->find( [ 'name' => 'Test #2' ] );
var_dump($result);
yunier0525 commented 4 years ago

@Smolevich

I get this error MongoDB/Driver/Exception/BulkWriteException with message 'Authorization failure' on $result = $collection->insertOne( [ 'name' => 'Test #2'] );

It seems to be a DB user permission issue.

Thanks

yunier0525 commented 4 years ago

Hi, I was able to successfully connect to AWS DocumentDB even with the certificates, in the end it was all permission issue.

I leave my final configuration as it was: database.php

'mongodb' => [ 'driver' => 'mongodb', 'dsn' => env('MONGO_DB_DSN'), 'database' => env('MONGO_DB_DATABASE', 'db'), 'options' => [ 'tls' => true, 'tlsCAFile' => '/var/www/.ssh/mongodb-certificate-develop.pem' ], ] .env

MONGO_DB_DSN=mongodb://user:pass@host.com:27017/db MONGO_DB_DATABASE=db

Thank you very much