Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Sts/StsClient.php

raw symbols SCIP
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 * This client is used to interact with the **AWS Security Token Service (AWS STS)**.
20 *
21 * @method \Aws\Result assumeRole(array $args = [])
22 * @method \GuzzleHttp\Promise\Promise assumeRoleAsync(array $args = [])
23 * @method \Aws\Result assumeRoleWithSAML(array $args = [])
24 * @method \GuzzleHttp\Promise\Promise assumeRoleWithSAMLAsync(array $args = [])
25 * @method \Aws\Result assumeRoleWithWebIdentity(array $args = [])
26 * @method \GuzzleHttp\Promise\Promise assumeRoleWithWebIdentityAsync(array $args = [])
27 * @method \Aws\Result assumeRoot(array $args = [])
28 * @method \GuzzleHttp\Promise\Promise assumeRootAsync(array $args = [])
29 * @method \Aws\Result decodeAuthorizationMessage(array $args = [])
30 * @method \GuzzleHttp\Promise\Promise decodeAuthorizationMessageAsync(array $args = [])
31 * @method \Aws\Result getAccessKeyInfo(array $args = [])
32 * @method \GuzzleHttp\Promise\Promise getAccessKeyInfoAsync(array $args = [])
33 * @method \Aws\Result getCallerIdentity(array $args = [])
34 * @method \GuzzleHttp\Promise\Promise getCallerIdentityAsync(array $args = [])
35 * @method \Aws\Result getDelegatedAccessToken(array $args = [])
36 * @method \GuzzleHttp\Promise\Promise getDelegatedAccessTokenAsync(array $args = [])
37 * @method \Aws\Result getFederationToken(array $args = [])
38 * @method \GuzzleHttp\Promise\Promise getFederationTokenAsync(array $args = [])
39 * @method \Aws\Result getSessionToken(array $args = [])
40 * @method \GuzzleHttp\Promise\Promise getSessionTokenAsync(array $args = [])
41 * @method \Aws\Result getWebIdentityToken(array $args = [])
42 * @method \GuzzleHttp\Promise\Promise getWebIdentityTokenAsync(array $args = [])
43 */
44 class StsClient extends AwsClient
45 {
46
47 /**
48 * {@inheritdoc}
49 *
50 * In addition to the options available to
51 * {@see \Aws\AwsClient::__construct}, StsClient accepts the following
52 * options:
53 *
54 * - sts_regional_endpoints:
55 * (Aws\Sts\RegionalEndpoints\ConfigurationInterface|Aws\CacheInterface\|callable|string|array)
56 * Specifies whether to use regional or legacy endpoints for legacy regions.
57 * Provide an Aws\Sts\RegionalEndpoints\ConfigurationInterface object, an
58 * instance of Aws\CacheInterface, a callable configuration provider used
59 * to create endpoint configuration, a string value of `legacy` or
60 * `regional`, or an associative array with the following keys:
61 * endpoint_types (string) Set to `legacy` or `regional`, defaults to
62 * `legacy`
63 *
64 * @param array $args
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 // Off-path STS keeps the default ClientResolver retry handling. The
82 // override below adds IDPCommunicationError as a transient error and
83 // is only registered when the AWS_NEW_RETRIES_2026 flag is on.
84 if (NewRetriesOptIn::isEnabled()) {
85 $args['retries']['fn'] = [__CLASS__, '_applyRetryConfig'];
86 }
87 return $args;
88 }
89
90 /**
91 * @internal Only invoked when AWS_NEW_RETRIES_2026=true. The off-path
92 * uses the default ClientResolver::_apply_retries.
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 * Creates credentials from the result of an STS operations
130 *
131 * @param Result $result Result of an STS operation
132 *
133 * @return Credentials
134 * @throws \InvalidArgumentException if the result contains no credentials
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 * Adds service-specific client built-in value
168 *
169 * @return void
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 }