Skip to content

Commit bfe2279

Browse files
fix: honor use_aws_shared_config_files (#3027)
1 parent c640230 commit bfe2279

File tree

3 files changed

+151
-40
lines changed

3 files changed

+151
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"type": "feature",
4+
"category": "",
5+
"description": "Standarize how config from env->ini is resolved."
6+
}
7+
]

src/ClientResolver.php

+57-39
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class ClientResolver
4747
/** @var array */
4848
private $argDefinitions;
4949

50+
/**
51+
* When using this option as default please make sure that, your config
52+
* has at least one data type defined in `valid` otherwise it will be
53+
* defaulted to `string`. Also, the default value will be the falsy value
54+
* based on the resolved data type. For example, the default for `string`
55+
* will be `''` and for bool will be `false`.
56+
*
57+
* @var string
58+
*/
59+
const DEFAULT_FROM_ENV_INI = [
60+
__CLASS__,
61+
'_resolve_from_env_ini'
62+
];
63+
5064
/** @var array Map of types to a corresponding function */
5165
private static $typeMap = [
5266
'resource' => 'is_resource',
@@ -91,7 +105,7 @@ class ClientResolver
91105
'valid' => ['bool'],
92106
'doc' => 'Set to true to disable endpoint urls configured using `AWS_ENDPOINT_URL` and `endpoint_url` shared config option.',
93107
'fn' => [__CLASS__, '_apply_ignore_configured_endpoint_urls'],
94-
'default' => [__CLASS__, '_default_ignore_configured_endpoint_urls'],
108+
'default' => self::DEFAULT_FROM_ENV_INI,
95109
],
96110
'endpoint' => [
97111
'type' => 'value',
@@ -105,7 +119,7 @@ class ClientResolver
105119
'valid' => ['string'],
106120
'doc' => 'Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.',
107121
'fn' => [__CLASS__, '_apply_region'],
108-
'default' => [__CLASS__, '_default_region']
122+
'default' => self::DEFAULT_FROM_ENV_INI
109123
],
110124
'version' => [
111125
'type' => 'value',
@@ -244,7 +258,7 @@ class ClientResolver
244258
'valid' => ['bool', 'callable'],
245259
'doc' => 'Set to true to disable request compression for supported operations',
246260
'fn' => [__CLASS__, '_apply_disable_request_compression'],
247-
'default' => [__CLASS__, '_default_disable_request_compression'],
261+
'default' => self::DEFAULT_FROM_ENV_INI,
248262
],
249263
'request_min_compression_size_bytes' => [
250264
'type' => 'value',
@@ -324,10 +338,10 @@ class ClientResolver
324338
],
325339
'sigv4a_signing_region_set' => [
326340
'type' => 'value',
327-
'valid' => ['array', 'string'],
341+
'valid' => ['string', 'array'],
328342
'doc' => 'A comma-delimited list of supported regions sent in sigv4a requests.',
329343
'fn' => [__CLASS__, '_apply_sigv4a_signing_region_set'],
330-
'default' => [__CLASS__, '_default_sigv4a_signing_region_set']
344+
'default' => self::DEFAULT_FROM_ENV_INI
331345
]
332346
];
333347

@@ -397,7 +411,15 @@ public function resolve(array $args, HandlerList $list)
397411
|| $a['default'] instanceof \Closure
398412
)
399413
) {
400-
$args[$key] = $a['default']($args);
414+
if ($a['default'] === self::DEFAULT_FROM_ENV_INI) {
415+
$args[$key] = $a['default'](
416+
$key,
417+
$a['valid'][0] ?? 'string',
418+
$args
419+
);
420+
} else {
421+
$args[$key] = $a['default']($args);
422+
}
401423
} else {
402424
$args[$key] = $a['default'];
403425
}
@@ -590,15 +612,6 @@ public static function _apply_disable_request_compression($value, array &$args)
590612
$args['config']['disable_request_compression'] = $value;
591613
}
592614

593-
public static function _default_disable_request_compression(array &$args) {
594-
return ConfigurationResolver::resolve(
595-
'disable_request_compression',
596-
false,
597-
'bool',
598-
$args
599-
);
600-
}
601-
602615
public static function _apply_min_compression_size($value, array &$args) {
603616
if (is_callable($value)) {
604617
$value = $value();
@@ -1259,16 +1272,6 @@ public static function _apply_suppress_php_deprecation_warning($value, &$args)
12591272
}
12601273
}
12611274

1262-
public static function _default_ignore_configured_endpoint_urls(array &$args)
1263-
{
1264-
return ConfigurationResolver::resolve(
1265-
'ignore_configured_endpoint_urls',
1266-
false,
1267-
'bool',
1268-
$args
1269-
);
1270-
}
1271-
12721275
public static function _default_endpoint(array &$args)
12731276
{
12741277
if ($args['config']['ignore_configured_endpoint_urls']
@@ -1318,15 +1321,6 @@ public static function _apply_sigv4a_signing_region_set($value, array &$args)
13181321
}
13191322
}
13201323

1321-
public static function _default_sigv4a_signing_region_set(array &$args)
1322-
{
1323-
return ConfigurationResolver::resolve(
1324-
'sigv4a_signing_region_set',
1325-
'',
1326-
'string'
1327-
);
1328-
}
1329-
13301324
public static function _apply_region($value, array &$args)
13311325
{
13321326
if (empty($value)) {
@@ -1335,11 +1329,6 @@ public static function _apply_region($value, array &$args)
13351329
$args['region'] = $value;
13361330
}
13371331

1338-
public static function _default_region(&$args)
1339-
{
1340-
return ConfigurationResolver::resolve('region', '', 'string');
1341-
}
1342-
13431332
public static function _missing_region(array $args)
13441333
{
13451334
$service = $args['service'] ?? '';
@@ -1356,6 +1345,35 @@ public static function _missing_region(array $args)
13561345
throw new IAE($msg);
13571346
}
13581347

1348+
/**
1349+
* Resolves a value from env or config.
1350+
*
1351+
* @param $key
1352+
* @param $expectedType
1353+
* @param $args
1354+
*
1355+
* @return mixed|string
1356+
*/
1357+
private static function _resolve_from_env_ini(
1358+
string $key,
1359+
string $expectedType,
1360+
array $args
1361+
) {
1362+
static $typeDefaultMap = [
1363+
'int' => 0,
1364+
'bool' => false,
1365+
'boolean' => false,
1366+
'string' => '',
1367+
];
1368+
1369+
return ConfigurationResolver::resolve(
1370+
$key,
1371+
$typeDefaultMap[$expectedType] ?? '',
1372+
$expectedType,
1373+
$args
1374+
);
1375+
}
1376+
13591377
/**
13601378
* Extracts client options for the endpoint provider to its own array
13611379
*

tests/ClientResolverTest.php

+87-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
namespace Aws\Test;
33

44
use Aws\Api\Service;
5-
use Aws\Auth\AuthSchemeResolver;
65
use Aws\Auth\AuthSchemeResolverInterface;
76
use Aws\ClientResolver;
87
use Aws\ClientSideMonitoring\Configuration;
98
use Aws\ClientSideMonitoring\ConfigurationProvider;
109
use Aws\CommandInterface;
10+
use Aws\Configuration\ConfigurationResolver;
1111
use Aws\Credentials\CredentialProvider;
1212
use Aws\Credentials\Credentials;
1313
use Aws\Credentials\CredentialsInterface;
@@ -1696,4 +1696,90 @@ public function testEmitsDeprecationWarning()
16961696
putenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING=true');
16971697
}
16981698
}
1699+
1700+
/**
1701+
* Tests the flag `use_aws_shared_config_files` is applied to the method
1702+
* for resolving a default value for a config.
1703+
*
1704+
* @return void
1705+
*/
1706+
public function testResolveFromEnvIniUseAwsSharedFiles(): void
1707+
{
1708+
// The config being tested
1709+
$configKey = 'foo-config-key';
1710+
$configValue = 'foo-config-value';
1711+
// Populate config file
1712+
$tempDir = sys_get_temp_dir();
1713+
$awsDir = $tempDir . "/.aws";
1714+
if (!is_dir($awsDir)) {
1715+
mkdir($awsDir, 0777, true);
1716+
}
1717+
$configFile = $awsDir . "/config";
1718+
$configData = <<<EOF
1719+
[default]
1720+
$configKey=$configValue
1721+
EOF;
1722+
file_put_contents($configFile, $configData);
1723+
$currentEnvConfigFile = getenv(ConfigurationResolver::ENV_CONFIG_FILE);
1724+
putenv(ConfigurationResolver::ENV_CONFIG_FILE . "=" . $configFile);
1725+
1726+
try {
1727+
$resolver = new ClientResolver([
1728+
$configKey => [
1729+
'type' => 'value',
1730+
'valid' => ['string'],
1731+
'fn' => function ($value, array &$args) use ($configKey) {
1732+
if (empty($value)) {
1733+
$args[$configKey] = null;
1734+
1735+
return;
1736+
}
1737+
1738+
$args[$configKey] = $value;
1739+
},
1740+
'default' => ClientResolver::DEFAULT_FROM_ENV_INI
1741+
]
1742+
]);
1743+
$testCases = [
1744+
[
1745+
'args' => [
1746+
'use_aws_shared_config_files' => true
1747+
],
1748+
'expected' => $configValue
1749+
],
1750+
[
1751+
'args' => [
1752+
'use_aws_shared_config_files' => false
1753+
],
1754+
'expected' => null
1755+
]
1756+
];
1757+
foreach ($testCases as $case) {
1758+
$resolvedArgs = $resolver->resolve(
1759+
$case['args'],
1760+
new HandlerList()
1761+
);
1762+
if ($case['expected']) {
1763+
$this->assertEquals(
1764+
$case['expected'],
1765+
$resolvedArgs[$configKey]
1766+
);
1767+
} else {
1768+
$this->assertNull($resolvedArgs[$configKey]);
1769+
}
1770+
}
1771+
} finally {
1772+
unlink($configFile);
1773+
rmdir($awsDir);
1774+
if ($currentEnvConfigFile) {
1775+
putenv(
1776+
ConfigurationResolver::ENV_CONFIG_FILE
1777+
. "="
1778+
. $currentEnvConfigFile
1779+
);
1780+
} else {
1781+
putenv(ConfigurationResolver::ENV_CONFIG_FILE);
1782+
}
1783+
}
1784+
}
16991785
}

0 commit comments

Comments
 (0)