Skip to content

Commit 78032d5

Browse files
fix: account_id resolution in ecs (#3058)
Co-authored-by: Sean O'Brien <[email protected]>
1 parent dc9ac0a commit 78032d5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Credentials/EcsCredentialProvider.php

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Aws\Credentials;
33

4+
use Aws\Arn\Arn;
45
use Aws\Exception\CredentialsException;
56
use GuzzleHttp\Exception\ConnectException;
67
use GuzzleHttp\Exception\GuzzleException;
@@ -86,6 +87,15 @@ public function __invoke()
8687
]
8788
)->then(function (ResponseInterface $response) {
8889
$result = $this->decodeResult((string)$response->getBody());
90+
if (!isset($result['AccountId']) && isset($result['RoleArn'])) {
91+
try {
92+
$parsedArn = new Arn($result['RoleArn']);
93+
$result['AccountId'] = $parsedArn->getAccountId();
94+
} catch (\Exception $e) {
95+
// AccountId will be null
96+
}
97+
}
98+
8999
return new Credentials(
90100
$result['AccessKeyId'],
91101
$result['SecretAccessKey'],

tests/Credentials/EcsCredentialProviderTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,39 @@ public function testResolveCredentialsWithAccountId()
353353

354354
}
355355

356+
public function testResolveCredentialsWithAccountIdFromArn()
357+
{
358+
$testAccountId = 'foo';
359+
$testArn = "arn:aws:iam::$testAccountId:role/role_name";
360+
$expiration = time() + 1000;
361+
$testHandler = function (RequestInterface $_) use ($expiration, $testArn) {
362+
$jsonResponse = <<<EOF
363+
{
364+
"AccessKeyId": "foo",
365+
"SecretAccessKey": "foo",
366+
"Token": "bazz",
367+
"Expiration": "@$expiration",
368+
"RoleArn": "$testArn"
369+
}
370+
EOF;
371+
return Promise\Create::promiseFor(new Response(200, [], $jsonResponse));
372+
};
373+
$provider = new EcsCredentialProvider([
374+
'client' => $testHandler
375+
]);
376+
try {
377+
/** @var Credentials $credentials */
378+
$credentials = $provider()->wait();
379+
$this->assertSame('foo', $credentials->getAccessKeyId());
380+
$this->assertSame('foo', $credentials->getSecretKey());
381+
$this->assertSame('bazz', $credentials->getSecurityToken());
382+
$this->assertSame($expiration, $credentials->getExpiration());
383+
$this->assertSame($testAccountId, $credentials->getAccountId());
384+
} catch (GuzzleException $e) {
385+
self::fail($e->getMessage());
386+
}
387+
}
388+
356389
/**
357390
* @dataProvider successTestCases
358391
*

0 commit comments

Comments
 (0)