Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to define a default value for undefined parameters #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Secrets.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static function loadSecretEnvironmentVariables(?SsmClient $ssmClient = nu
return self::retrieveParametersFromSsm($ssmClient, array_values($ssmNames));
});

// Remove default values from parameter names
$ssmNames = array_map(static fn($name) => explode(',', $name, 2)[0], $ssmNames);

foreach ($parameters as $parameterName => $parameterValue) {
$envVar = array_search($parameterName, $ssmNames, true);
$_SERVER[$envVar] = $_ENV[$envVar] = $parameterValue;
Expand Down Expand Up @@ -99,6 +102,16 @@ private static function retrieveParametersFromSsm(?SsmClient $ssmClient, array $
/** @var array<string, string> $parameters Map of parameter name -> value */
$parameters = [];
$parametersNotFound = [];
// Store default values for parameters
$parametersDefaults = array_reduce($ssmNames, static function ($carry, $item) {
[ $paramName, $defaultValue ] = explode(',', $item) + [ null, null ];

return $paramName !== null && $defaultValue !== null
? array_merge($carry, [ $paramName => $defaultValue ])
: $carry;
}, []);
// Remove default values from parameter names for querying SSM
$ssmNames = array_map(static fn($name) => explode(',', $name, 2)[0], $ssmNames);

// The API only accepts up to 10 parameters at a time, so we batch the calls
foreach (array_chunk($ssmNames, 10) as $batchOfSsmNames) {
Expand All @@ -124,6 +137,20 @@ private static function retrieveParametersFromSsm(?SsmClient $ssmClient, array $
$parametersNotFound = array_merge($parametersNotFound, $result->getInvalidParameters());
}

// check any of the invalid parameters has a default value
$parametersNotFound = array_filter($parametersNotFound, static function($parameter) use (&$parameters, $parametersDefaults): bool {
// check if the parameter has a default value
if (array_key_exists($parameter, $parametersDefaults)) {
// load default value
$parameters[$parameter] = $parametersDefaults[$parameter];

// remove it from the not found list
return false;
}

return true;
});

if (count($parametersNotFound) > 0) {
throw new RuntimeException('The following SSM parameters could not be found: ' . implode(', ', $parametersNotFound));
}
Expand Down
21 changes: 21 additions & 0 deletions tests/SecretsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ public function test caches parameters to call SSM only once(): void
$this->assertSame('foobar', getenv('SOME_VARIABLE'));
}

public function test that parameter can have a default value(): void
{
putenv('SOME_VARIABLE_WITH_DEFAULT=bref-ssm:/some/undefined-parameter,default-value');

$ssmClient = $this->getMockBuilder(SsmClient::class)
->disableOriginalConstructor()
->getMock();
$result = ResultMockFactory::create(GetParametersResult::class, [
'InvalidParameters' => [
'/some/undefined-parameter',
],
]);
$ssmClient->method('getParameters')
->willReturn($result);

Secrets::loadSecretEnvironmentVariables($ssmClient);

// Check that the variable has the default value
$this->assertSame('default-value', getenv('SOME_VARIABLE_WITH_DEFAULT'));
}

public function test throws a clear error message on missing permissions(): void
{
putenv('SOME_VARIABLE=bref-ssm:/app/test');
Expand Down