Skip to content

Commit 9c6556f

Browse files
authored
Move timeout to ContainerProvider::__construct() (#571)
* Move timeout to ContainerProvider::__construct() This will fix #546 * Dont use hard coded timeout
1 parent fd40ca5 commit 9c6556f

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/Configuration.php

-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ final class Configuration
2626
public const OPTION_WEB_IDENTITY_TOKEN_FILE = 'webIdentityTokenFile';
2727
public const OPTION_ROLE_SESSION_NAME = 'roleSessionName';
2828
public const OPTION_CONTAINER_CREDENTIALS_RELATIVE_URI = 'containerCredentialsRelativeUri';
29-
public const OPTION_METADATA_SERVICE_TIMEOUT = 'metadataServiceTimeout';
3029

3130
private const AVAILABLE_OPTIONS = [
3231
self::OPTION_REGION => true,
@@ -40,7 +39,6 @@ final class Configuration
4039
self::OPTION_ROLE_ARN => true,
4140
self::OPTION_WEB_IDENTITY_TOKEN_FILE => true,
4241
self::OPTION_ROLE_SESSION_NAME => true,
43-
self::OPTION_METADATA_SERVICE_TIMEOUT => true,
4442
self::OPTION_CONTAINER_CREDENTIALS_RELATIVE_URI => true,
4543
];
4644

@@ -61,7 +59,6 @@ final class Configuration
6159
self::OPTION_ROLE_SESSION_NAME => 'AWS_ROLE_SESSION_NAME',
6260
],
6361
[self::OPTION_CONTAINER_CREDENTIALS_RELATIVE_URI => 'AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'],
64-
[self::OPTION_METADATA_SERVICE_TIMEOUT => 'AWS_METADATA_SERVICE_TIMEOUT'],
6562
];
6663

6764
private const DEFAULT_OPTIONS = [
@@ -71,7 +68,6 @@ final class Configuration
7168
self::OPTION_SHARED_CONFIG_FILE => '~/.aws/config',
7269
// https://docs.aws.amazon.com/general/latest/gr/rande.html
7370
self::OPTION_ENDPOINT => 'https://%service%.%region%.amazonaws.com',
74-
self::OPTION_METADATA_SERVICE_TIMEOUT => 1.0,
7571
];
7672

7773
private $data = [];

src/Credentials/ContainerProvider.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@ final class ContainerProvider implements CredentialProvider
2626

2727
private $httpClient;
2828

29-
public function __construct(?HttpClientInterface $httpClient = null, ?LoggerInterface $logger = null)
29+
private $timeout;
30+
31+
public function __construct(?HttpClientInterface $httpClient = null, ?LoggerInterface $logger = null, float $timeout = 1.0)
3032
{
3133
$this->logger = $logger ?? new NullLogger();
3234
$this->httpClient = $httpClient ?? HttpClient::create();
35+
$this->timeout = $timeout;
3336
}
3437

3538
public function getCredentials(Configuration $configuration): ?Credentials
3639
{
37-
$timeout = $configuration->get(Configuration::OPTION_METADATA_SERVICE_TIMEOUT);
3840
$relativeUri = $configuration->get(Configuration::OPTION_CONTAINER_CREDENTIALS_RELATIVE_URI);
3941
// introduces an early exit if the env variable is not set.
4042
if (empty($relativeUri)) {
4143
return null;
4244
}
45+
4346
// fetch credentials from ecs endpoint
4447
try {
45-
$response = $this->httpClient->request('GET', self::ENDPOINT . $relativeUri, ['timeout' => $timeout]);
48+
$response = $this->httpClient->request('GET', self::ENDPOINT . $relativeUri, ['timeout' => $this->timeout]);
4649
$result = $response->toArray();
4750
} catch (DecodingExceptionInterface $e) {
4851
$this->logger->info('Failed to decode Credentials.', ['exception' => $e]);

src/Credentials/InstanceProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(?HttpClientInterface $httpClient = null, ?LoggerInte
4242

4343
public function getCredentials(Configuration $configuration): ?Credentials
4444
{
45-
// fetch current Profile
45+
// Fetch current Profile
4646
try {
4747
$response = $this->httpClient->request('GET', self::ENDPOINT, ['timeout' => $this->timeout]);
4848
$profile = $response->getContent();
@@ -56,9 +56,9 @@ public function getCredentials(Configuration $configuration): ?Credentials
5656
return null;
5757
}
5858

59-
// fetch credentials from profile
59+
// Fetch credentials from profile
6060
try {
61-
$response = $this->httpClient->request('GET', self::ENDPOINT . '/' . $profile, ['timeout' => 1.0]);
61+
$response = $this->httpClient->request('GET', self::ENDPOINT . '/' . $profile, ['timeout' => $this->timeout]);
6262
$result = $this->toArray($response);
6363

6464
if ('Success' !== $result['Code']) {

0 commit comments

Comments
 (0)