Skip to content

Commit 67a00ab

Browse files
authored
bugfix: sts credential provider endpoint mode (#3198)
1 parent 5722f6e commit 67a00ab

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"type": "bugfix",
4+
"category": "Credentials",
5+
"description": "Fixes condition where certain STS credential providers call the regional `us-east-1` endpoint by default."
6+
}
7+
]

src/Credentials/AssumeRoleWithWebIdentityCredentialProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AssumeRoleWithWebIdentityCredentialProvider
3636

3737
/** @var integer */
3838
private $tokenFileReadAttempts;
39+
3940
/** @var string */
4041
private $source;
4142

@@ -72,14 +73,17 @@ public function __construct(array $config = [])
7273
$this->tokenFileReadAttempts = 0;
7374
$this->session = $config['SessionName']
7475
?? 'aws-sdk-php-' . round(microtime(true) * 1000);
75-
$region = $config['region'] ?? 'us-east-1';
76+
$region = $config['region'] ?? null;
7677
if (isset($config['client'])) {
7778
$this->client = $config['client'];
7879
} else {
7980
$this->client = new StsClient([
8081
'credentials' => false,
81-
'region' => $region,
82-
'version' => 'latest'
82+
'region' => $region ?? 'us-east-1',
83+
'version' => 'latest',
84+
'sts_regional_endpoints' => $region
85+
? 'regional'
86+
: 'legacy'
8387
]);
8488
}
8589

src/Credentials/CredentialProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,7 @@ private static function loadRoleProfile(
708708
}
709709

710710
if (empty($stsClient)) {
711-
$sourceRegion = isset($profiles[$sourceProfileName]['region'])
712-
? $profiles[$sourceProfileName]['region']
713-
: 'us-east-1';
711+
$sourceRegion = $profiles[$sourceProfileName]['region'] ?? null;
714712
$config['preferStaticCredentials'] = true;
715713
$sourceCredentials = null;
716714
if (!empty($roleProfile['source_profile'])){
@@ -725,8 +723,11 @@ private static function loadRoleProfile(
725723
}
726724
$stsClient = new StsClient([
727725
'credentials' => $sourceCredentials,
728-
'region' => $sourceRegion,
726+
'region' => $sourceRegion ?? 'us-east-1',
729727
'version' => '2011-06-15',
728+
'sts_regional_endpoints' => $sourceRegion
729+
? 'regional'
730+
: 'legacy'
730731
]);
731732
}
732733

tests/Credentials/AssumeRoleWithWebIdentityCredentialProviderTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Aws\Credentials\AssumeRoleWithWebIdentityCredentialProvider;
77
use Aws\Credentials\Credentials;
88
use Aws\Exception\AwsException;
9+
use Aws\Middleware;
910
use Aws\Result;
1011
use Aws\Sts\StsClient;
1112
use Aws\Sts\Exception\StsException;
@@ -396,4 +397,85 @@ public function testCanDisableInvalidIdentityTokenRetries()
396397
unlink($tokenPath);
397398
}
398399
}
400+
401+
/**
402+
* @dataProvider endpointConfigurationProvider
403+
*/
404+
public function testEndpointConfigurationBasedOnRegion(
405+
?string $region,
406+
string $expectedEndpoint,
407+
string $description
408+
): void
409+
{
410+
$tokenFile = tempnam(sys_get_temp_dir(), 'token');
411+
file_put_contents($tokenFile, 'test-token-content');
412+
413+
$config = [
414+
'RoleArn' => self::SAMPLE_ROLE_ARN,
415+
'WebIdentityTokenFile' => $tokenFile,
416+
];
417+
418+
if ($region !== null) {
419+
$config['region'] = $region;
420+
}
421+
422+
423+
$provider = new AssumeRoleWithWebIdentityCredentialProvider($config);
424+
$reflection = new \ReflectionClass($provider);
425+
$clientProperty = $reflection->getProperty('client');
426+
$stsClient = $clientProperty->getValue($provider);
427+
428+
$capturedEndpoint = null;
429+
$stsClient->getHandlerList()->appendBuild(
430+
Middleware::tap(
431+
function ($cmd, $req) use (&$capturedEndpoint) {
432+
$capturedEndpoint = (string) $req->getUri();
433+
}
434+
)
435+
);
436+
437+
$stsClient->getHandlerList()->setHandler(
438+
function ($c, $r) {
439+
$result = [
440+
'Credentials' => [
441+
'AccessKeyId' => 'foo',
442+
'SecretAccessKey' => 'bar',
443+
'SessionToken' => 'baz',
444+
'Expiration' => DateTimeResult::fromEpoch(time() + 10)
445+
],
446+
'AssumedRoleUser' => [
447+
'AssumedRoleId' => 'ARXXXXXXXXXXXXXXXXXXX:test_session',
448+
'Arn' => self::SAMPLE_ROLE_ARN . "/test_session"
449+
]
450+
];
451+
return Promise\Create::promiseFor(new Result($result));
452+
}
453+
);
454+
455+
$provider()->wait();
456+
457+
$this->assertEquals(
458+
$expectedEndpoint,
459+
$capturedEndpoint,
460+
"Failed asserting endpoint for: {$description}"
461+
);
462+
463+
unlink($tokenFile);
464+
}
465+
466+
public function endpointConfigurationProvider(): array
467+
{
468+
return [
469+
'explicit us-east-1 uses regional endpoint' => [
470+
'region' => 'us-east-1',
471+
'expectedEndpoint' => 'https://sts.us-east-1.amazonaws.com/',
472+
'description' => 'explicit us-east-1'
473+
],
474+
'no region defaults to us-east-1 with global endpoint' => [
475+
'region' => null,
476+
'expectedEndpoint' => 'https://sts.amazonaws.com/',
477+
'description' => 'default region'
478+
]
479+
];
480+
}
399481
}

0 commit comments

Comments
 (0)