in src/Credentials/EcsCredentialProvider.php [70:128]
public function __invoke()
{
$this->attempts = 0;
$uri = $this->getEcsUri();
if ($this->isCompatibleUri($uri)) {
return Promise\Coroutine::of(function () {
$client = $this->client;
$request = new Request('GET', $this->getEcsUri());
$headers = $this->getHeadersForAuthToken();
$credentials = null;
while ($credentials === null) {
$credentials = (yield $client(
$request,
[
'timeout' => $this->timeout,
'proxy' => '',
'headers' => $headers,
]
)->then(function (ResponseInterface $response) {
$result = $this->decodeResult((string)$response->getBody());
if (!isset($result['AccountId']) && isset($result['RoleArn'])) {
try {
$parsedArn = new Arn($result['RoleArn']);
$result['AccountId'] = $parsedArn->getAccountId();
} catch (\Exception $e) {
// AccountId will be null
}
}
return new Credentials(
$result['AccessKeyId'],
$result['SecretAccessKey'],
$result['Token'],
strtotime($result['Expiration']),
$result['AccountId'] ?? null,
CredentialSources::ECS
);
})->otherwise(function ($reason) {
$reason = is_array($reason) ? $reason['exception'] : $reason;
$isRetryable = $reason instanceof ConnectException;
if ($isRetryable && ($this->attempts < $this->retries)) {
sleep((int)pow(1.2, $this->attempts));
} else {
$msg = $reason->getMessage();
throw new CredentialsException(
sprintf('Error retrieving credentials from container metadata after attempt %d/%d (%s)', $this->attempts, $this->retries, $msg)
);
}
}));
$this->attempts++;
}
yield $credentials;
});
}
throw new CredentialsException("Uri '{$uri}' contains an unsupported host.");
}