diff --git a/README.MD b/README.MD index d7e33c0..2b7adeb 100644 --- a/README.MD +++ b/README.MD @@ -58,6 +58,8 @@ mongo_db_bundle: - { host: host2 } username: '' password: '' + authSource: '' # the database name associated with the user’s credentials, defaults to connection + database_name if not specified replicaSet: '' # default null (no replica) (experimental) ssl: false connectTimeoutMS: 3000 # default null (no timeout) diff --git a/docker-compose.yml b/docker-compose.yml index 43ecf1d..43068bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,8 +13,7 @@ services: image: mongo:3.4.2 ports: - 27017:27017 - entrypoint: - - mongod - - --quiet - - --logpath=/dev/null + environment: + - MONGO_INITDB_ROOT_USERNAME=root + - MONGO_INITDB_ROOT_PASSWORD=rootPass container_name: mb_mongo diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d008686..e7a5cfe 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -22,13 +22,13 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->root('mongo_db_bundle'); $rootNode ->children() - ->booleanNode('data_collection')->defaultTrue()->info('Use to disable Data Collection if needed') + ->booleanNode('data_collection')->defaultTrue()->info('Disables Data Collection if needed') ->end() ->arrayNode('clients')->isRequired()->requiresAtLeastOneElement() ->useAttributeAsKey('name') ->prototype('array') ->children() - ->arrayNode('hosts')->info('Your MongoDB hosts addresses and ports') + ->arrayNode('hosts')->info('Hosts addresses and ports') ->prototype('array') ->children() ->scalarNode('host')->isRequired()->end() @@ -38,6 +38,7 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('username')->defaultValue('')->end() ->scalarNode('password')->defaultValue('')->end() + ->scalarNode('authSource')->defaultValue(null)->info('Database name associated with the user’s credentials')->end() ->scalarNode('readPreference') ->defaultValue('primaryPreferred') ->validate() @@ -54,8 +55,8 @@ public function getConfigTreeBuilder() ->useAttributeAsKey('name') ->prototype('array') ->children() - ->scalarNode('client_name')->isRequired()->info('Your defined client name')->end() - ->scalarNode('database_name')->isRequired()->info('Your MongoDB database name')->end() + ->scalarNode('client_name')->isRequired()->info('Desired client name')->end() + ->scalarNode('database_name')->isRequired()->info('Database name')->end() ->end() ->end(); diff --git a/src/Models/ClientConfiguration.php b/src/Models/ClientConfiguration.php index d0740a6..69d70aa 100644 --- a/src/Models/ClientConfiguration.php +++ b/src/Models/ClientConfiguration.php @@ -16,25 +16,30 @@ final class ClientConfiguration private $password; /** @var array */ private $options; + /** @var null|string */ + private $authSource; /** * ClientConfiguration constructor. * - * @param string $hosts - * @param string $username - * @param string $password - * @param array $options + * @param string $hosts + * @param string $username + * @param string $password + * @param string|null $authSource + * @param array $options */ public function __construct( string $hosts, string $username = '', string $password = '', + string $authSource = null, array $options = [] ) { $this->hosts = $hosts; $this->username = $username; $this->password = $password; $this->options = $options; + $this->authSource = $authSource; } /** @@ -61,6 +66,14 @@ public function getPassword(): string return $this->password; } + /** + * @return null|string + */ + public function getAuthSource() + { + return $this->authSource; + } + /** * @return array */ diff --git a/src/Services/ClientRegistry.php b/src/Services/ClientRegistry.php index f35a3ec..9bc82cb 100644 --- a/src/Services/ClientRegistry.php +++ b/src/Services/ClientRegistry.php @@ -69,11 +69,12 @@ private function buildClientConfiguration(array $conf): ClientConfiguration $this->buildConnectionUri($conf['hosts']), $conf['username'], $conf['password'], + $conf['authSource'], [ 'replicaSet' => $conf['replicaSet'], 'ssl' => $conf['ssl'], 'connectTimeoutMS' => $conf['connectTimeoutMS'], - 'readPreference' => $conf['readPreference'], + 'readPreference' => $conf['readPreference'] ] ); } @@ -128,7 +129,13 @@ public function getClient(string $name, string $databaseName = null): Client if (!isset($this->clients[$clientKey])) { $conf = $this->configurations[$name]; $uri = sprintf('mongodb://%s', $conf->getHosts()); - $options = array_merge(['database' => $databaseName], $conf->getOptions()); + $options = array_merge( + [ + 'database' => $databaseName, + 'authSource' => $conf->getAuthSource() ?? $databaseName + ], + $conf->getOptions() + ); $this->clients[$clientKey] = $this->buildClient($name, $uri, $options, []); $this->eventDispatcher->dispatch( diff --git a/src/Services/Explain/ExplainQueryService.php b/src/Services/Explain/ExplainQueryService.php index a2de4c7..5fd61da 100644 --- a/src/Services/Explain/ExplainQueryService.php +++ b/src/Services/Explain/ExplainQueryService.php @@ -59,7 +59,7 @@ public function execute(Query $query, string $verbosity = self::VERBOSITY_ALL_PL ); }; - $manager = $this->clientRegistry->getClient($query->getClient())->__debugInfo()['manager']; + $manager = $this->clientRegistry->getClient($query->getClient(),$query->getDatabase())->__debugInfo()['manager']; return $manager ->executeCommand( diff --git a/test.sh b/test.sh index 4057445..16c7689 100755 --- a/test.sh +++ b/test.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash +docker-compose start docker exec -ti mb_php bin/phpunit -c phpunit.xml.dist diff --git a/tests/Functional/Capsule/CollectionTest.php b/tests/Functional/Capsule/CollectionTest.php index 15a2980..ff2f3fa 100644 --- a/tests/Functional/Capsule/CollectionTest.php +++ b/tests/Functional/Capsule/CollectionTest.php @@ -14,7 +14,7 @@ private function getManager(): Manager /** @var \Facile\MongoDbBundle\Services\ClientRegistry $reg */ $reg = $this->getContainer()->get('mongo.client_registry'); /** @var \MongoDB\Client $client */ - $client = $reg->getClient('test_client'); + $client = $reg->getClient('test_client', 'testdb'); return $client->__debugInfo()['manager']; } diff --git a/tests/Functional/TestApp/config_test_32_docker.yml b/tests/Functional/TestApp/config_test_32_docker.yml index 7ffdb7b..49cc209 100644 --- a/tests/Functional/TestApp/config_test_32_docker.yml +++ b/tests/Functional/TestApp/config_test_32_docker.yml @@ -6,6 +6,9 @@ framework: mongo_db_bundle: clients: test_client: + username: root + password: rootPass + authSource: admin hosts: - { host: mongo, port: 27017 } diff --git a/tests/Functional/TestApp/config_test_docker.yml b/tests/Functional/TestApp/config_test_docker.yml index df4fee1..e39041d 100644 --- a/tests/Functional/TestApp/config_test_docker.yml +++ b/tests/Functional/TestApp/config_test_docker.yml @@ -4,6 +4,9 @@ framework: mongo_db_bundle: clients: test_client: + username: root + password: rootPass + authSource: admin hosts: - { host: mongo, port: 27017 } diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index c364c3c..9e7f3a4 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -36,6 +36,7 @@ public function test_full_configuration_process() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => null, 'ssl' => false, 'connectTimeoutMS' => null, @@ -65,6 +66,7 @@ public function test_options_configuration_process() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => 'testReplica', 'ssl' => true, 'connectTimeoutMS' => 3000, @@ -94,6 +96,7 @@ public function test_data_collection_disabled_configuration_process() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => 'testReplica', 'ssl' => true, 'connectTimeoutMS' => 3000, @@ -123,6 +126,7 @@ public function test_multiple_connections_configuration_process() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => null, 'ssl' => false, 'connectTimeoutMS' => null, @@ -135,6 +139,7 @@ public function test_multiple_connections_configuration_process() ], 'username' => 'mee', 'password' => 'zod', + 'authSource' => null, 'replicaSet' => null, 'ssl' => false, 'connectTimeoutMS' => null, diff --git a/tests/Unit/Models/ClientConfigurationTest.php b/tests/Unit/Models/ClientConfigurationTest.php index db6a1e2..4bf3122 100644 --- a/tests/Unit/Models/ClientConfigurationTest.php +++ b/tests/Unit/Models/ClientConfigurationTest.php @@ -61,6 +61,7 @@ public function test_construction_with_options(array $options, array $expectedOp 'localhost:27017', '', '', + null, $options ); diff --git a/tests/Unit/Services/ClientRegistryTest.php b/tests/Unit/Services/ClientRegistryTest.php index b681484..f506b5d 100644 --- a/tests/Unit/Services/ClientRegistryTest.php +++ b/tests/Unit/Services/ClientRegistryTest.php @@ -22,6 +22,7 @@ public function test_client_connection_url_generation_singlehost() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => 'testReplica', 'ssl' => true, 'connectTimeoutMS' => 3000, @@ -30,11 +31,11 @@ public function test_client_connection_url_generation_singlehost() ]; $registry->addClientsConfigurations($testConf); - $client = $registry->getClient('test_client'); + $client = $registry->getClient('test_client', 'testdb'); $this->assertEquals('mongodb://host1:8080',$client->__debugInfo()['uri']); - $this->assertEquals(['test_client'], $registry->getClientNames()); + $this->assertEquals(['test_client.testdb'], $registry->getClientNames()); } public function test_client_connection_url_generation_multyhost() { @@ -51,6 +52,7 @@ public function test_client_connection_url_generation_multyhost() ], 'username' => 'foo', 'password' => 'bar', + 'authSource' => null, 'replicaSet' => 'testReplica', 'ssl' => true, 'connectTimeoutMS' => 3000, @@ -59,7 +61,7 @@ public function test_client_connection_url_generation_multyhost() ]; $registry->addClientsConfigurations($testConf); - $client = $registry->getClient('test_client'); + $client = $registry->getClient('test_client', 'testdb'); $this->assertEquals('mongodb://host1:8080,host2:8081',$client->__debugInfo()['uri']); }