Skip to content

Commit a6bfa29

Browse files
authored
[SymfonyBundle] feat: Add support for MaxResults on SSMVault (#1672)
feat: Add support for MaxResults on SSMVault To reduce the number of requests sent to SSM
1 parent 718f9a6 commit a6bfa29

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66

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

910
## 1.12.0
1011

src/DependencyInjection/AsyncAwsExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void
4848
* enabled: bool,
4949
* client: string|null,
5050
* path: string|null,
51+
* max_results: int|null,
5152
* recursive: bool,
5253
* cache: array{enabled: bool, pool: string, ttl: int},
5354
* },
@@ -215,6 +216,7 @@ private function addServiceDefinition(ContainerBuilder $container, string $name,
215216
* client: string|null,
216217
* path: string|null,
217218
* recursive: bool,
219+
* max_results: int|null,
218220
* cache: array{enabled: bool, pool: string, ttl: int},
219221
* },
220222
* clients: array<string, array{type: string, ...}>,
@@ -257,6 +259,7 @@ private function registerEnvLoader(ContainerBuilder $container, array $config):
257259
new Reference('async_aws.client.' . $client),
258260
$config['secrets']['path'],
259261
$config['secrets']['recursive'],
262+
$config['secrets']['max_results'],
260263
]);
261264

262265
if ($config['secrets']['cache']['enabled']) {

src/DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4848
->children()
4949
->scalarNode('path')->info('Path to the parameters.')->defaultNull()->end()
5050
->booleanNode('recursive')->info('Retrieve all parameters within a hierarchy.')->defaultValue(true)->end()
51+
->integerNode('max_results')->info('The maximum number of items for each ssm call. Maximum value of 50.')->min(1)->max(50)->defaultNull()->end()
5152
->scalarNode('client')->info('Name of the SSM client. When null, use the default SSM configuration.')->defaultNull()->end()
5253
->arrayNode('cache')
5354
->canBeEnabled()

src/Secrets/SsmVault.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ class SsmVault implements EnvVarLoaderInterface
2323
*/
2424
private $recursive;
2525

26-
public function __construct(SsmClient $client, ?string $path, bool $recursive)
26+
/**
27+
* @var int|null
28+
*/
29+
private $maxResults;
30+
31+
public function __construct(SsmClient $client, ?string $path, bool $recursive, ?int $maxResults = null)
2732
{
2833
$this->client = $client;
2934
$this->path = $path ?? '/';
3035
$this->recursive = $recursive;
36+
$this->maxResults = $maxResults;
3137
}
3238

3339
public function loadEnvVars(): array
@@ -36,6 +42,7 @@ public function loadEnvVars(): array
3642
'Path' => $this->path,
3743
'Recursive' => $this->recursive,
3844
'WithDecryption' => true,
45+
'MaxResults' => $this->maxResults,
3946
]);
4047

4148
$secrets = [];

tests/Unit/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testDefaultValues(): void
3939
'pool' => 'cache.system',
4040
'ttl' => 600,
4141
],
42+
'max_results' => null,
4243
],
4344
]);
4445
}

tests/Unit/Secrets/SsmVaultTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ public function provideParameters(): iterable
3636
yield 'remove trailing' => ['/my_app/', '/my_app/foo', ['FOO' => 'value']];
3737
yield 'recursive' => [null, '/foo/bar', ['FOO_BAR' => 'value']];
3838
}
39+
40+
public function testMaxResults()
41+
{
42+
$maxResults = 50;
43+
$client = $this->createMock(SsmClient::class);
44+
$ssmVault = new SsmVault($client, '/', true, $maxResults);
45+
46+
$client->expects(self::once())
47+
->method('getParametersByPath')
48+
->with([
49+
'Path' => '/',
50+
'Recursive' => true,
51+
'WithDecryption' => true,
52+
'MaxResults' => $maxResults,
53+
])
54+
->willReturn(ResultMockFactory::create(GetParametersByPathResult::class, ['Parameters' => [new Parameter(['Name' => 'foo', 'Value' => 'value'])]]));
55+
56+
$ssmVault->loadEnvVars();
57+
}
3958
}

0 commit comments

Comments
 (0)