| 1 |
<?php |
| 2 |
namespace Aws\Sts; |
| 3 |
|
| 4 |
use Aws\Arn\ArnParser; |
| 5 |
use Aws\AwsClient; |
| 6 |
use Aws\CacheInterface; |
| 7 |
use Aws\Credentials\Credentials; |
| 8 |
use Aws\HandlerList; |
| 9 |
use Aws\Middleware; |
| 10 |
use Aws\Result; |
| 11 |
use Aws\Retry\ConfigurationInterface as RetryConfigurationInterface; |
| 12 |
use Aws\Retry\ConfigurationProvider as RetryConfigurationProvider; |
| 13 |
use Aws\Retry\V3\OptIn as NewRetriesOptIn; |
| 14 |
use Aws\Retry\V3\RetryMiddleware as RetryV3Middleware; |
| 15 |
use Aws\RetryMiddleware; |
| 16 |
use Aws\Sts\RegionalEndpoints\ConfigurationProvider; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class StsClient extends AwsClient |
| 45 |
{ |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __construct(array $args) |
| 67 |
{ |
| 68 |
if ( |
| 69 |
!isset($args['sts_regional_endpoints']) |
| 70 |
|| $args['sts_regional_endpoints'] instanceof CacheInterface |
| 71 |
) { |
| 72 |
$args['sts_regional_endpoints'] = ConfigurationProvider::defaultProvider($args); |
| 73 |
} |
| 74 |
$this->addBuiltIns($args); |
| 75 |
parent::__construct($args); |
| 76 |
} |
| 77 |
|
| 78 |
public static function getArguments() |
| 79 |
{ |
| 80 |
$args = parent::getArguments(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if (NewRetriesOptIn::isEnabled()) { |
| 85 |
$args['retries']['fn'] = [__CLASS__, '_applyRetryConfig']; |
| 86 |
} |
| 87 |
return $args; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function _applyRetryConfig( |
| 95 |
$value, |
| 96 |
array &$args, |
| 97 |
HandlerList $list |
| 98 |
): void |
| 99 |
{ |
| 100 |
if (!$value) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$config = RetryConfigurationProvider::unwrap($value); |
| 105 |
|
| 106 |
if ($config->getMode() === 'legacy') { |
| 107 |
$decider = RetryMiddleware::createDefaultDecider($config->getMaxAttempts() - 1); |
| 108 |
$list->appendSign( |
| 109 |
Middleware::retry($decider, null, $args['stats']['retries']), |
| 110 |
'retry' |
| 111 |
); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$list->appendSign( |
| 116 |
RetryV3Middleware::wrap( |
| 117 |
$config, |
| 118 |
[ |
| 119 |
'collect_stats' => $args['stats']['retries'], |
| 120 |
'service' => $args['service'], |
| 121 |
'transient_error_codes' => ['IDPCommunicationError'], |
| 122 |
] |
| 123 |
), |
| 124 |
'retry' |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function createCredentials(Result $result, $source=null) |
| 137 |
{ |
| 138 |
if (!$result->hasKey('Credentials')) { |
| 139 |
throw new \InvalidArgumentException('Result contains no credentials'); |
| 140 |
} |
| 141 |
|
| 142 |
$accountId = null; |
| 143 |
if ($result->hasKey('AssumedRoleUser')) { |
| 144 |
$parsedArn = ArnParser::parse($result->get('AssumedRoleUser')['Arn']); |
| 145 |
$accountId = $parsedArn->getAccountId(); |
| 146 |
} elseif ($result->hasKey('FederatedUser')) { |
| 147 |
$parsedArn = ArnParser::parse($result->get('FederatedUser')['Arn']); |
| 148 |
$accountId = $parsedArn->getAccountId(); |
| 149 |
} |
| 150 |
|
| 151 |
$credentials = $result['Credentials']; |
| 152 |
$expiration = isset($credentials['Expiration']) && $credentials['Expiration'] instanceof \DateTimeInterface |
| 153 |
? (int) $credentials['Expiration']->format('U') |
| 154 |
: null; |
| 155 |
|
| 156 |
return new Credentials( |
| 157 |
$credentials['AccessKeyId'], |
| 158 |
$credentials['SecretAccessKey'], |
| 159 |
isset($credentials['SessionToken']) ? $credentials['SessionToken'] : null, |
| 160 |
$expiration, |
| 161 |
$accountId, |
| 162 |
$source |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private function addBuiltIns($args) |
| 172 |
{ |
| 173 |
$key = 'AWS::STS::UseGlobalEndpoint'; |
| 174 |
$result = $args['sts_regional_endpoints'] instanceof \Closure ? |
| 175 |
$args['sts_regional_endpoints']()->wait() : $args['sts_regional_endpoints']; |
| 176 |
|
| 177 |
if (is_string($result)) { |
| 178 |
if ($result === 'regional') { |
| 179 |
$value = false; |
| 180 |
} else if ($result === 'legacy') { |
| 181 |
$value = true; |
| 182 |
} else { |
| 183 |
return; |
| 184 |
} |
| 185 |
} else { |
| 186 |
if ($result->getEndpointsType() === 'regional') { |
| 187 |
$value = false; |
| 188 |
} else { |
| 189 |
$value = true; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
$this->clientBuiltIns[$key] = $value; |
| 194 |
} |
| 195 |
} |