Skip to content

Commit

Permalink
[SymfonyBundle] feat: Add support for MaxResults on SSMVault (#1672)
Browse files Browse the repository at this point in the history
feat: Add support for MaxResults on SSMVault

To reduce the number of requests sent to SSM
  • Loading branch information
tyx committed Feb 22, 2024
1 parent 125a4b9 commit 970c5a7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/integration/symfony-bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async_aws:
secrets: ~
# path: /parameters/my-project
# recursive: true
# max_results: 20
# client: app-secret
# cache:
# pool: cache.app
Expand Down
1 change: 1 addition & 0 deletions src/Integration/Symfony/Bundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Adding `async-aws/s3` 2.0, `async-aws/sqs` 2.0, `async-aws/ssm` 2.0 in dev dependencies
- Adding `max_results` option in `secrets` configuration

## 1.12.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void
* enabled: bool,
* client: string|null,
* path: string|null,
* max_results: int|null,
* recursive: bool,
* cache: array{enabled: bool, pool: string, ttl: int},
* },
Expand Down Expand Up @@ -215,6 +216,7 @@ private function addServiceDefinition(ContainerBuilder $container, string $name,
* client: string|null,
* path: string|null,
* recursive: bool,
* max_results: int|null,
* cache: array{enabled: bool, pool: string, ttl: int},
* },
* clients: array<string, array{type: string, ...}>,
Expand Down Expand Up @@ -257,6 +259,7 @@ private function registerEnvLoader(ContainerBuilder $container, array $config):
new Reference('async_aws.client.' . $client),
$config['secrets']['path'],
$config['secrets']['recursive'],
$config['secrets']['max_results'],
]);

if ($config['secrets']['cache']['enabled']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('path')->info('Path to the parameters.')->defaultNull()->end()
->booleanNode('recursive')->info('Retrieve all parameters within a hierarchy.')->defaultValue(true)->end()
->integerNode('max_results')->info('The maximum number of items for each ssm call. Maximum value of 50.')->min(1)->max(50)->defaultNull()->end()
->scalarNode('client')->info('Name of the SSM client. When null, use the default SSM configuration.')->defaultNull()->end()
->arrayNode('cache')
->canBeEnabled()
Expand Down
9 changes: 8 additions & 1 deletion src/Integration/Symfony/Bundle/src/Secrets/SsmVault.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ class SsmVault implements EnvVarLoaderInterface
*/
private $recursive;

public function __construct(SsmClient $client, ?string $path, bool $recursive)
/**
* @var int|null
*/
private $maxResults;

public function __construct(SsmClient $client, ?string $path, bool $recursive, ?int $maxResults = null)
{
$this->client = $client;
$this->path = $path ?? '/';
$this->recursive = $recursive;
$this->maxResults = $maxResults;
}

public function loadEnvVars(): array
Expand All @@ -36,6 +42,7 @@ public function loadEnvVars(): array
'Path' => $this->path,
'Recursive' => $this->recursive,
'WithDecryption' => true,
'MaxResults' => $this->maxResults,
]);

$secrets = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testDefaultValues(): void
'pool' => 'cache.system',
'ttl' => 600,
],
'max_results' => null,
],
]);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Integration/Symfony/Bundle/tests/Unit/Secrets/SsmVaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ public function provideParameters(): iterable
yield 'remove trailing' => ['/my_app/', '/my_app/foo', ['FOO' => 'value']];
yield 'recursive' => [null, '/foo/bar', ['FOO_BAR' => 'value']];
}

public function testMaxResults()
{
$maxResults = 50;
$client = $this->createMock(SsmClient::class);
$ssmVault = new SsmVault($client, '/', true, $maxResults);

$client->expects(self::once())
->method('getParametersByPath')
->with([
'Path' => '/',
'Recursive' => true,
'WithDecryption' => true,
'MaxResults' => $maxResults,
])
->willReturn(ResultMockFactory::create(GetParametersByPathResult::class, ['Parameters' => [new Parameter(['Name' => 'foo', 'Value' => 'value'])]]));

$ssmVault->loadEnvVars();
}
}

0 comments on commit 970c5a7

Please sign in to comment.