Skip to content

Commit

Permalink
added authSource in the configuration options
Browse files Browse the repository at this point in the history
adapted dev and test environment
readme update
  • Loading branch information
Alessandro Galli committed Nov 10, 2017
1 parent d608d1b commit afcb1e5
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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();

Expand Down
21 changes: 17 additions & 4 deletions src/Models/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -61,6 +66,14 @@ public function getPassword(): string
return $this->password;
}

/**
* @return null|string
*/
public function getAuthSource()
{
return $this->authSource;
}

/**
* @return array
*/
Expand Down
11 changes: 9 additions & 2 deletions src/Services/ClientRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]
);
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Explain/ExplainQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash

docker-compose start
docker exec -ti mb_php bin/phpunit -c phpunit.xml.dist
2 changes: 1 addition & 1 deletion tests/Functional/Capsule/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/TestApp/config_test_32_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ framework:
mongo_db_bundle:
clients:
test_client:
username: root
password: rootPass
authSource: admin
hosts:
- { host: mongo, port: 27017 }

Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/TestApp/config_test_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ framework:
mongo_db_bundle:
clients:
test_client:
username: root
password: rootPass
authSource: admin
hosts:
- { host: mongo, port: 27017 }

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function test_full_configuration_process()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => null,
'ssl' => false,
'connectTimeoutMS' => null,
Expand Down Expand Up @@ -65,6 +66,7 @@ public function test_options_configuration_process()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => 'testReplica',
'ssl' => true,
'connectTimeoutMS' => 3000,
Expand Down Expand Up @@ -94,6 +96,7 @@ public function test_data_collection_disabled_configuration_process()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => 'testReplica',
'ssl' => true,
'connectTimeoutMS' => 3000,
Expand Down Expand Up @@ -123,6 +126,7 @@ public function test_multiple_connections_configuration_process()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => null,
'ssl' => false,
'connectTimeoutMS' => null,
Expand All @@ -135,6 +139,7 @@ public function test_multiple_connections_configuration_process()
],
'username' => 'mee',
'password' => 'zod',
'authSource' => null,
'replicaSet' => null,
'ssl' => false,
'connectTimeoutMS' => null,
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Models/ClientConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function test_construction_with_options(array $options, array $expectedOp
'localhost:27017',
'',
'',
null,
$options
);

Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Services/ClientRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function test_client_connection_url_generation_singlehost()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => 'testReplica',
'ssl' => true,
'connectTimeoutMS' => 3000,
Expand All @@ -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()
{
Expand All @@ -51,6 +52,7 @@ public function test_client_connection_url_generation_multyhost()
],
'username' => 'foo',
'password' => 'bar',
'authSource' => null,
'replicaSet' => 'testReplica',
'ssl' => true,
'connectTimeoutMS' => 3000,
Expand All @@ -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']);
}
Expand Down

0 comments on commit afcb1e5

Please sign in to comment.