| 1 |
<?php |
| 2 |
namespace Aws\Credentials; |
| 3 |
|
| 4 |
use Aws\Exception\AwsException; |
| 5 |
use Aws\Exception\CredentialsException; |
| 6 |
use Aws\Result; |
| 7 |
use Aws\Sts\StsClient; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class AssumeRoleWithWebIdentityCredentialProvider |
| 15 |
{ |
| 16 |
const ERROR_MSG = "Missing required 'AssumeRoleWithWebIdentityCredentialProvider' configuration option: "; |
| 17 |
const ENV_RETRIES = 'AWS_METADATA_SERVICE_NUM_ATTEMPTS'; |
| 18 |
|
| 19 |
|
| 20 |
private $tokenFile; |
| 21 |
|
| 22 |
|
| 23 |
private $arn; |
| 24 |
|
| 25 |
|
| 26 |
private $session; |
| 27 |
|
| 28 |
|
| 29 |
private $client; |
| 30 |
|
| 31 |
|
| 32 |
private $retries; |
| 33 |
|
| 34 |
|
| 35 |
private $authenticationAttempts; |
| 36 |
|
| 37 |
|
| 38 |
private $tokenFileReadAttempts; |
| 39 |
|
| 40 |
|
| 41 |
private $source; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct(array $config = []) |
| 56 |
{ |
| 57 |
if (!isset($config['RoleArn'])) { |
| 58 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'RoleArn'."); |
| 59 |
} |
| 60 |
$this->arn = $config['RoleArn']; |
| 61 |
|
| 62 |
if (!isset($config['WebIdentityTokenFile'])) { |
| 63 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'WebIdentityTokenFile'."); |
| 64 |
} |
| 65 |
$this->tokenFile = $config['WebIdentityTokenFile']; |
| 66 |
|
| 67 |
if (!preg_match("/^\w\:|^\/|^\\\/", $this->tokenFile)) { |
| 68 |
throw new \InvalidArgumentException("'WebIdentityTokenFile' must be an absolute path."); |
| 69 |
} |
| 70 |
|
| 71 |
$this->retries = (int) getenv(self::ENV_RETRIES) ?: (isset($config['retries']) ? $config['retries'] : 3); |
| 72 |
$this->authenticationAttempts = 0; |
| 73 |
$this->tokenFileReadAttempts = 0; |
| 74 |
$this->session = $config['SessionName'] |
| 75 |
?? 'aws-sdk-php-' . round(microtime(true) * 1000); |
| 76 |
|
| 77 |
if (isset($config['client'])) { |
| 78 |
$this->client = $config['client']; |
| 79 |
} else { |
| 80 |
$region = $config['region'] |
| 81 |
?? getEnv(CredentialProvider::ENV_REGION) |
| 82 |
?: null; |
| 83 |
|
| 84 |
$this->client = $this->createDefaultStsClient($region); |
| 85 |
} |
| 86 |
|
| 87 |
$this->source = $config['source'] |
| 88 |
?? CredentialSources::STS_WEB_ID_TOKEN; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function __invoke() |
| 97 |
{ |
| 98 |
return Promise\Coroutine::of(function () { |
| 99 |
$client = $this->client; |
| 100 |
$result = null; |
| 101 |
while ($result == null) { |
| 102 |
try { |
| 103 |
$token = @file_get_contents($this->tokenFile); |
| 104 |
if (false === $token) { |
| 105 |
clearstatcache(true, dirname($this->tokenFile) . "/" . readlink($this->tokenFile)); |
| 106 |
clearstatcache(true, dirname($this->tokenFile) . "/" . dirname(readlink($this->tokenFile))); |
| 107 |
clearstatcache(true, $this->tokenFile); |
| 108 |
if (!@is_readable($this->tokenFile)) { |
| 109 |
throw new CredentialsException( |
| 110 |
"Unreadable tokenfile at location {$this->tokenFile}" |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
$token = @file_get_contents($this->tokenFile); |
| 115 |
} |
| 116 |
if (empty($token)) { |
| 117 |
if ($this->tokenFileReadAttempts < $this->retries) { |
| 118 |
sleep((int) pow(1.2, $this->tokenFileReadAttempts)); |
| 119 |
$this->tokenFileReadAttempts++; |
| 120 |
continue; |
| 121 |
} |
| 122 |
throw new CredentialsException("InvalidIdentityToken from file: {$this->tokenFile}"); |
| 123 |
} |
| 124 |
} catch (\Exception $exception) { |
| 125 |
throw new CredentialsException( |
| 126 |
"Error reading WebIdentityTokenFile from " . $this->tokenFile, |
| 127 |
0, |
| 128 |
$exception |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
$assumeParams = [ |
| 133 |
'RoleArn' => $this->arn, |
| 134 |
'RoleSessionName' => $this->session, |
| 135 |
'WebIdentityToken' => $token |
| 136 |
]; |
| 137 |
|
| 138 |
try { |
| 139 |
$result = $client->assumeRoleWithWebIdentity($assumeParams); |
| 140 |
} catch (AwsException $e) { |
| 141 |
if ($e->getAwsErrorCode() == 'InvalidIdentityToken') { |
| 142 |
if ($this->authenticationAttempts < $this->retries) { |
| 143 |
sleep((int) pow(1.2, $this->authenticationAttempts)); |
| 144 |
} else { |
| 145 |
throw new CredentialsException( |
| 146 |
"InvalidIdentityToken, retries exhausted" |
| 147 |
); |
| 148 |
} |
| 149 |
} else { |
| 150 |
throw new CredentialsException( |
| 151 |
"Error assuming role from web identity credentials", |
| 152 |
0, |
| 153 |
$e |
| 154 |
); |
| 155 |
} |
| 156 |
} catch (\Exception $e) { |
| 157 |
throw new CredentialsException( |
| 158 |
"Error retrieving web identity credentials: " . $e->getMessage() |
| 159 |
. " (" . $e->getCode() . ")" |
| 160 |
); |
| 161 |
} |
| 162 |
$this->authenticationAttempts++; |
| 163 |
} |
| 164 |
|
| 165 |
yield $this->client->createCredentials( |
| 166 |
$result, |
| 167 |
$this->source |
| 168 |
); |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
private function createDefaultStsClient( |
| 178 |
?string $region |
| 179 |
): StsClient |
| 180 |
{ |
| 181 |
if (empty($region)) { |
| 182 |
$region = CredentialProvider::FALLBACK_REGION; |
| 183 |
trigger_error( |
| 184 |
'NOTICE: STS client created without explicit `region` configuration.' . PHP_EOL |
| 185 |
. "Defaulting to {$region}. This fallback behavior may be removed." . PHP_EOL |
| 186 |
. 'To avoid potential disruptions, configure a region using one of the following methods:' . PHP_EOL |
| 187 |
. '(1) Pass `region` in the `$config` array when calling the provider,' . PHP_EOL |
| 188 |
. '(2) Set the `AWS_REGION` environment variable.' . PHP_EOL |
| 189 |
. 'OR provide an STS client in the `$config` array when creating the provider as `client`.' . PHP_EOL |
| 190 |
. 'See: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/assume-role-with-web-identity-provider.html' |
| 191 |
. PHP_EOL, |
| 192 |
E_USER_NOTICE |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
return new StsClient([ |
| 197 |
'credentials' => false, |
| 198 |
'region' => $region |
| 199 |
]); |
| 200 |
} |
| 201 |
} |