From 4c0d77f908fb0af6fc17e966168c408076b29229 Mon Sep 17 00:00:00 2001 From: Yenfry Herrera Feliz Date: Wed, 6 Mar 2024 09:49:27 -0800 Subject: [PATCH] fix: set use_aws_shared_config_files opts-in This change makes the ConfigurationResolver to load configuration from aws shared config files based on the configuration `use_aws_shared_config_files` either provided as provider arguments or as service client arguments. --- src/Credentials/InstanceProfileProvider.php | 16 +++++++++++----- .../Credentials/InstanceProfileProviderTest.php | 7 +++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Credentials/InstanceProfileProvider.php b/src/Credentials/InstanceProfileProvider.php index d7e5a912bb..1fb5712b2f 100644 --- a/src/Credentials/InstanceProfileProvider.php +++ b/src/Credentials/InstanceProfileProvider.php @@ -60,6 +60,9 @@ class InstanceProfileProvider /** @var string */ private $endpointMode; + /** @var array */ + private $config; + /** * The constructor accepts the following options: * @@ -68,10 +71,12 @@ class InstanceProfileProvider * - retries: Optional number of retries to be attempted. * - ec2_metadata_v1_disabled: Optional for disabling the fallback to IMDSv1. * - endpoint: Optional for overriding the default endpoint to be used for fetching credentials. - * The value must contain a valid URI scheme. If the URI scheme is not https, it must - * resolve to a loopback address. + * The value must contain a valid URI scheme. If the URI scheme is not https, it must + * resolve to a loopback address. * - endpoint_mode: Optional for overriding the default endpoint mode (IPv4|IPv6) to be used for * resolving the default endpoint. + * - use_aws_shared_config_files: Decides whether the shared config file should be considered when + * using the ConfigurationResolver::resolve method. * * @param array $config Configuration options. */ @@ -88,6 +93,7 @@ public function __construct(array $config = []) } $this->endpointMode = $config[self::CFG_EC2_METADATA_SERVICE_ENDPOINT_MODE] ?? null; + $this->config = $config; } /** @@ -344,7 +350,7 @@ private function shouldFallbackToIMDSv1(): bool self::CFG_EC2_METADATA_V1_DISABLED, self::DEFAULT_AWS_EC2_METADATA_V1_DISABLED, 'bool', - ['use_aws_shared_config_files' => true] + $this->config ) ) ?? self::DEFAULT_AWS_EC2_METADATA_V1_DISABLED; @@ -369,7 +375,7 @@ private function resolveEndpoint(): string self::CFG_EC2_METADATA_SERVICE_ENDPOINT, $this->getDefaultEndpoint(), 'string', - ['use_aws_shared_config_files' => true] + $this->config ); } @@ -420,7 +426,7 @@ private function resolveEndpointMode(): string self::CFG_EC2_METADATA_SERVICE_ENDPOINT_MODE, self::ENDPOINT_MODE_IPv4, 'string', - ['use_aws_shared_config_files' => true] + $this->config ); } diff --git a/tests/Credentials/InstanceProfileProviderTest.php b/tests/Credentials/InstanceProfileProviderTest.php index da21465dd4..c39b8588ff 100644 --- a/tests/Credentials/InstanceProfileProviderTest.php +++ b/tests/Credentials/InstanceProfileProviderTest.php @@ -1340,6 +1340,7 @@ private function fetchMockedCredentialsAndAlwaysExpectAToken($config=[]) { return Promise\Create::rejectionFor(['exception' => new \Exception('Unexpected error!')]); }; + $config['use_aws_shared_config_files'] = true; $provider = new InstanceProfileProvider(array_merge(($config ?? []), ['client' => $mockHandler])); try { $provider()->wait(); @@ -1385,7 +1386,8 @@ public function testEndpointModeResolution($endpointModeClientConfig, $endpointM default: $this->fail("The expected value for endpoint_mode should be either one of the following options[" . InstanceProfileProvider::ENDPOINT_MODE_IPv4 . ', ' . InstanceProfileProvider::ENDPOINT_MODE_IPv6 . "]"); } - }) + }), + 'use_aws_shared_config_files' => true ]; if (!is_null($endpointModeClientConfig)) { $providerConfig[InstanceProfileProvider::CFG_EC2_METADATA_SERVICE_ENDPOINT_MODE] = $endpointModeClientConfig; @@ -1498,7 +1500,8 @@ public function testEndpointResolution($endpointMode, $endpointEnv, $endpointCon 'client' => $this->getClientForEndpointTesting(function ($uri) use ($expectedEndpoint) { $endpoint = $uri->getScheme() . '://' . $uri->getHost(); $this->assertSame($expectedEndpoint, $endpoint); - }) + }), + 'use_aws_shared_config_files' => true ]; $deferredTasks = []; if (!is_null($endpointEnv)) {