diff --git a/src/Credentials/EcsCredentialProvider.php b/src/Credentials/EcsCredentialProvider.php index 0d8c11928d..e95b2b005b 100644 --- a/src/Credentials/EcsCredentialProvider.php +++ b/src/Credentials/EcsCredentialProvider.php @@ -1,6 +1,7 @@ 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'], diff --git a/tests/Credentials/EcsCredentialProviderTest.php b/tests/Credentials/EcsCredentialProviderTest.php index 68d2e84f85..62998025c5 100644 --- a/tests/Credentials/EcsCredentialProviderTest.php +++ b/tests/Credentials/EcsCredentialProviderTest.php @@ -353,6 +353,39 @@ public function testResolveCredentialsWithAccountId() } + public function testResolveCredentialsWithAccountIdFromArn() + { + $testAccountId = 'foo'; + $testArn = "arn:aws:iam::$testAccountId:role/role_name"; + $expiration = time() + 1000; + $testHandler = function (RequestInterface $_) use ($expiration, $testArn) { + $jsonResponse = << $testHandler + ]); + try { + /** @var Credentials $credentials */ + $credentials = $provider()->wait(); + $this->assertSame('foo', $credentials->getAccessKeyId()); + $this->assertSame('foo', $credentials->getSecretKey()); + $this->assertSame('bazz', $credentials->getSecurityToken()); + $this->assertSame($expiration, $credentials->getExpiration()); + $this->assertSame($testAccountId, $credentials->getAccountId()); + } catch (GuzzleException $e) { + self::fail($e->getMessage()); + } + } + /** * @dataProvider successTestCases *