| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\ApiProvider; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Api\DocModel; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Auth\AuthSelectionMiddleware; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\Auth\AuthSchemeResolverInterface; |
| 10 |
use Dudlewebs\WPMCS\s3\Aws\EndpointDiscovery\EndpointDiscoveryMiddleware; |
| 11 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\EndpointProviderV2; |
| 12 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\EndpointV2Middleware; |
| 13 |
use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException; |
| 14 |
use Dudlewebs\WPMCS\s3\Aws\Signature\SignatureProvider; |
| 15 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 16 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 17 |
/** |
| 18 |
* Default AWS client implementation |
| 19 |
*/ |
| 20 |
class AwsClient implements AwsClientInterface |
| 21 |
{ |
| 22 |
use AwsClientTrait; |
| 23 |
/** @var array */ |
| 24 |
private $aliases; |
| 25 |
/** @var array */ |
| 26 |
private $config; |
| 27 |
/** @var string */ |
| 28 |
private $region; |
| 29 |
/** @var string */ |
| 30 |
private $signingRegionSet; |
| 31 |
/** @var string */ |
| 32 |
private $endpoint; |
| 33 |
/** @var Service */ |
| 34 |
private $api; |
| 35 |
/** @var callable */ |
| 36 |
private $signatureProvider; |
| 37 |
/** @var AuthSchemeResolverInterface */ |
| 38 |
private $authSchemeResolver; |
| 39 |
/** @var callable */ |
| 40 |
private $credentialProvider; |
| 41 |
/** @var callable */ |
| 42 |
private $tokenProvider; |
| 43 |
/** @var HandlerList */ |
| 44 |
private $handlerList; |
| 45 |
/** @var array*/ |
| 46 |
private $defaultRequestOptions; |
| 47 |
/** @var array*/ |
| 48 |
private $clientContextParams = []; |
| 49 |
/** @var array*/ |
| 50 |
protected $clientBuiltIns = []; |
| 51 |
/** @var EndpointProviderV2 | callable */ |
| 52 |
protected $endpointProvider; |
| 53 |
/** @var callable */ |
| 54 |
protected $serializer; |
| 55 |
/** |
| 56 |
* Get an array of client constructor arguments used by the client. |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public static function getArguments() |
| 61 |
{ |
| 62 |
return ClientResolver::getDefaultArguments(); |
| 63 |
} |
| 64 |
/** |
| 65 |
* The client constructor accepts the following options: |
| 66 |
* |
| 67 |
* - api_provider: (callable) An optional PHP callable that accepts a |
| 68 |
* type, service, and version argument, and returns an array of |
| 69 |
* corresponding configuration data. The type value can be one of api, |
| 70 |
* waiter, or paginator. |
| 71 |
* - credentials: |
| 72 |
* (Aws\Credentials\CredentialsInterface|array|bool|callable) Specifies |
| 73 |
* the credentials used to sign requests. Provide an |
| 74 |
* Aws\Credentials\CredentialsInterface object, an associative array of |
| 75 |
* "key", "secret", and an optional "token" key, `false` to use null |
| 76 |
* credentials, or a callable credentials provider used to create |
| 77 |
* credentials or return null. See Aws\Credentials\CredentialProvider for |
| 78 |
* a list of built-in credentials providers. If no credentials are |
| 79 |
* provided, the SDK will attempt to load them from the environment. |
| 80 |
* - token: |
| 81 |
* (Aws\Token\TokenInterface|array|bool|callable) Specifies |
| 82 |
* the token used to authorize requests. Provide an |
| 83 |
* Aws\Token\TokenInterface object, an associative array of |
| 84 |
* "token" and an optional "expires" key, `false` to use no |
| 85 |
* token, or a callable token provider used to create a |
| 86 |
* token or return null. See Aws\Token\TokenProvider for |
| 87 |
* a list of built-in token providers. If no token is |
| 88 |
* provided, the SDK will attempt to load one from the environment. |
| 89 |
* - csm: |
| 90 |
* (Aws\ClientSideMonitoring\ConfigurationInterface|array|callable) Specifies |
| 91 |
* the credentials used to sign requests. Provide an |
| 92 |
* Aws\ClientSideMonitoring\ConfigurationInterface object, a callable |
| 93 |
* configuration provider used to create client-side monitoring configuration, |
| 94 |
* `false` to disable csm, or an associative array with the following keys: |
| 95 |
* enabled: (bool) Set to true to enable client-side monitoring, defaults |
| 96 |
* to false; host: (string) the host location to send monitoring events to, |
| 97 |
* defaults to 127.0.0.1; port: (int) The port used for the host connection, |
| 98 |
* defaults to 31000; client_id: (string) An identifier for this project |
| 99 |
* - debug: (bool|array) Set to true to display debug information when |
| 100 |
* sending requests. Alternatively, you can provide an associative array |
| 101 |
* with the following keys: logfn: (callable) Function that is invoked |
| 102 |
* with log messages; stream_size: (int) When the size of a stream is |
| 103 |
* greater than this number, the stream data will not be logged (set to |
| 104 |
* "0" to not log any stream data); scrub_auth: (bool) Set to false to |
| 105 |
* disable the scrubbing of auth data from the logged messages; http: |
| 106 |
* (bool) Set to false to disable the "debug" feature of lower level HTTP |
| 107 |
* adapters (e.g., verbose curl output). |
| 108 |
* - stats: (bool|array) Set to true to gather transfer statistics on |
| 109 |
* requests sent. Alternatively, you can provide an associative array with |
| 110 |
* the following keys: retries: (bool) Set to false to disable reporting |
| 111 |
* on retries attempted; http: (bool) Set to true to enable collecting |
| 112 |
* statistics from lower level HTTP adapters (e.g., values returned in |
| 113 |
* GuzzleHttp\TransferStats). HTTP handlers must support an |
| 114 |
* `http_stats_receiver` option for this to have an effect; timer: (bool) |
| 115 |
* Set to true to enable a command timer that reports the total wall clock |
| 116 |
* time spent on an operation in seconds. |
| 117 |
* - disable_host_prefix_injection: (bool) Set to true to disable host prefix |
| 118 |
* injection logic for services that use it. This disables the entire |
| 119 |
* prefix injection, including the portions supplied by user-defined |
| 120 |
* parameters. Setting this flag will have no effect on services that do |
| 121 |
* not use host prefix injection. |
| 122 |
* - endpoint: (string) The full URI of the webservice. This is only |
| 123 |
* required when connecting to a custom endpoint (e.g., a local version |
| 124 |
* of S3). |
| 125 |
* - endpoint_discovery: (Aws\EndpointDiscovery\ConfigurationInterface, |
| 126 |
* Aws\CacheInterface, array, callable) Settings for endpoint discovery. |
| 127 |
* Provide an instance of Aws\EndpointDiscovery\ConfigurationInterface, |
| 128 |
* an instance Aws\CacheInterface, a callable that provides a promise for |
| 129 |
* a Configuration object, or an associative array with the following |
| 130 |
* keys: enabled: (bool) Set to true to enable endpoint discovery, false |
| 131 |
* to explicitly disable it, defaults to false; cache_limit: (int) The |
| 132 |
* maximum number of keys in the endpoints cache, defaults to 1000. |
| 133 |
* - endpoint_provider: (callable) An optional PHP callable that |
| 134 |
* accepts a hash of options including a "service" and "region" key and |
| 135 |
* returns NULL or a hash of endpoint data, of which the "endpoint" key |
| 136 |
* is required. See Aws\Endpoint\EndpointProvider for a list of built-in |
| 137 |
* providers. |
| 138 |
* - handler: (callable) A handler that accepts a command object, |
| 139 |
* request object and returns a promise that is fulfilled with an |
| 140 |
* Aws\ResultInterface object or rejected with an |
| 141 |
* Aws\Exception\AwsException. A handler does not accept a next handler |
| 142 |
* as it is terminal and expected to fulfill a command. If no handler is |
| 143 |
* provided, a default Guzzle handler will be utilized. |
| 144 |
* - http: (array, default=array(0)) Set to an array of SDK request |
| 145 |
* options to apply to each request (e.g., proxy, verify, etc.). |
| 146 |
* - http_handler: (callable) An HTTP handler is a function that |
| 147 |
* accepts a PSR-7 request object and returns a promise that is fulfilled |
| 148 |
* with a PSR-7 response object or rejected with an array of exception |
| 149 |
* data. NOTE: This option supersedes any provided "handler" option. |
| 150 |
* - idempotency_auto_fill: (bool|callable) Set to false to disable SDK to |
| 151 |
* populate parameters that enabled 'idempotencyToken' trait with a random |
| 152 |
* UUID v4 value on your behalf. Using default value 'true' still allows |
| 153 |
* parameter value to be overwritten when provided. Note: auto-fill only |
| 154 |
* works when cryptographically secure random bytes generator functions |
| 155 |
* (random_bytes, openssl_random_pseudo_bytes or mcrypt_create_iv) can be |
| 156 |
* found. You may also provide a callable source of random bytes. |
| 157 |
* - profile: (string) Allows you to specify which profile to use when |
| 158 |
* credentials are created from the AWS credentials file in your HOME |
| 159 |
* directory. This setting overrides the AWS_PROFILE environment |
| 160 |
* variable. Note: Specifying "profile" will cause the "credentials" key |
| 161 |
* to be ignored. |
| 162 |
* - region: (string, required) Region to connect to. See |
| 163 |
* http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of |
| 164 |
* available regions. |
| 165 |
* - retries: (int, Aws\Retry\ConfigurationInterface, Aws\CacheInterface, |
| 166 |
* array, callable) Configures the retry mode and maximum number of |
| 167 |
* allowed retries for a client (pass 0 to disable retries). Provide an |
| 168 |
* integer for 'legacy' mode with the specified number of retries. |
| 169 |
* Otherwise provide an instance of Aws\Retry\ConfigurationInterface, an |
| 170 |
* instance of Aws\CacheInterface, a callable function, or an array with |
| 171 |
* the following keys: mode: (string) Set to 'legacy', 'standard' (uses |
| 172 |
* retry quota management), or 'adapative' (an experimental mode that adds |
| 173 |
* client-side rate limiting to standard mode); max_attempts (int) The |
| 174 |
* maximum number of attempts for a given request. |
| 175 |
* - scheme: (string, default=string(5) "https") URI scheme to use when |
| 176 |
* connecting connect. The SDK will utilize "https" endpoints (i.e., |
| 177 |
* utilize SSL/TLS connections) by default. You can attempt to connect to |
| 178 |
* a service over an unencrypted "http" endpoint by setting ``scheme`` to |
| 179 |
* "http". |
| 180 |
* - signature_provider: (callable) A callable that accepts a signature |
| 181 |
* version name (e.g., "v4"), a service name, and region, and |
| 182 |
* returns a SignatureInterface object or null. This provider is used to |
| 183 |
* create signers utilized by the client. See |
| 184 |
* Aws\Signature\SignatureProvider for a list of built-in providers |
| 185 |
* - signature_version: (string) A string representing a custom |
| 186 |
* signature version to use with a service (e.g., v4). Note that |
| 187 |
* per/operation signature version MAY override this requested signature |
| 188 |
* version. |
| 189 |
* - use_aws_shared_config_files: (bool, default=bool(true)) Set to false to |
| 190 |
* disable checking for shared config file in '~/.aws/config' and |
| 191 |
* '~/.aws/credentials'. This will override the AWS_CONFIG_FILE |
| 192 |
* environment variable. |
| 193 |
* - validate: (bool, default=bool(true)) Set to false to disable |
| 194 |
* client-side parameter validation. |
| 195 |
* - version: (string, required) The version of the webservice to |
| 196 |
* utilize (e.g., 2006-03-01). |
| 197 |
* - account_id_endpoint_mode: (string, default(preferred)) this option |
| 198 |
* decides whether credentials should resolve an accountId value, |
| 199 |
* which is going to be used as part of the endpoint resolution. |
| 200 |
* The valid values for this option are: |
| 201 |
* - preferred: when this value is set then, a warning is logged when |
| 202 |
* accountId is empty in the resolved identity. |
| 203 |
* - required: when this value is set then, an exception is thrown when |
| 204 |
* accountId is empty in the resolved identity. |
| 205 |
* - disabled: when this value is set then, the validation for if accountId |
| 206 |
* was resolved or not, is ignored. |
| 207 |
* - ua_append: (string, array) To pass custom user agent parameters. |
| 208 |
* - app_id: (string) an optional application specific identifier that can be set. |
| 209 |
* When set it will be appended to the User-Agent header of every request |
| 210 |
* in the form of App/{AppId}. This variable is sourced from environment |
| 211 |
* variable AWS_SDK_UA_APP_ID or the shared config profile attribute sdk_ua_app_id. |
| 212 |
* See https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html for |
| 213 |
* more information on environment variables and shared config settings. |
| 214 |
* |
| 215 |
* @param array $args Client configuration arguments. |
| 216 |
* |
| 217 |
* @throws \InvalidArgumentException if any required options are missing or |
| 218 |
* the service is not supported. |
| 219 |
*/ |
| 220 |
public function __construct(array $args) |
| 221 |
{ |
| 222 |
list($service, $exceptionClass) = $this->parseClass(); |
| 223 |
if (!isset($args['service'])) { |
| 224 |
$args['service'] = manifest($service)['endpoint']; |
| 225 |
} |
| 226 |
if (!isset($args['exception_class'])) { |
| 227 |
$args['exception_class'] = $exceptionClass; |
| 228 |
} |
| 229 |
$this->handlerList = new HandlerList(); |
| 230 |
$resolver = new ClientResolver(static::getArguments()); |
| 231 |
$config = $resolver->resolve($args, $this->handlerList); |
| 232 |
$this->api = $config['api']; |
| 233 |
$this->signatureProvider = $config['signature_provider']; |
| 234 |
$this->authSchemeResolver = $config['auth_scheme_resolver']; |
| 235 |
$this->endpoint = new Uri($config['endpoint']); |
| 236 |
$this->credentialProvider = $config['credentials']; |
| 237 |
$this->tokenProvider = $config['token']; |
| 238 |
$this->region = $config['region'] ?? null; |
| 239 |
$this->signingRegionSet = $config['sigv4a_signing_region_set'] ?? null; |
| 240 |
$this->config = $config['config']; |
| 241 |
$this->setClientBuiltIns($args, $config); |
| 242 |
$this->clientContextParams = $this->setClientContextParams($args); |
| 243 |
$this->defaultRequestOptions = $config['http']; |
| 244 |
$this->endpointProvider = $config['endpoint_provider']; |
| 245 |
$this->serializer = $config['serializer']; |
| 246 |
$this->addSignatureMiddleware($args); |
| 247 |
$this->addInvocationId(); |
| 248 |
$this->addEndpointParameterMiddleware($args); |
| 249 |
$this->addEndpointDiscoveryMiddleware($config, $args); |
| 250 |
$this->addRequestCompressionMiddleware($config); |
| 251 |
$this->loadAliases(); |
| 252 |
$this->addStreamRequestPayload(); |
| 253 |
$this->addRecursionDetection(); |
| 254 |
if ($this->isUseEndpointV2()) { |
| 255 |
$this->addEndpointV2Middleware(); |
| 256 |
} |
| 257 |
$this->addAuthSelectionMiddleware($config['config']); |
| 258 |
if (!\is_null($this->api->getMetadata('awsQueryCompatible'))) { |
| 259 |
$this->addQueryCompatibleInputMiddleware($this->api); |
| 260 |
$this->addQueryModeHeader(); |
| 261 |
} |
| 262 |
if (isset($args['with_resolved'])) { |
| 263 |
$args['with_resolved']($config); |
| 264 |
} |
| 265 |
$this->addUserAgentMiddleware($config); |
| 266 |
} |
| 267 |
public function getHandlerList() |
| 268 |
{ |
| 269 |
return $this->handlerList; |
| 270 |
} |
| 271 |
public function getConfig($option = null) |
| 272 |
{ |
| 273 |
return $option === null ? $this->config : $this->config[$option] ?? null; |
| 274 |
} |
| 275 |
public function getCredentials() |
| 276 |
{ |
| 277 |
$fn = $this->credentialProvider; |
| 278 |
return $fn(); |
| 279 |
} |
| 280 |
public function getToken() |
| 281 |
{ |
| 282 |
$fn = $this->tokenProvider; |
| 283 |
return $fn(); |
| 284 |
} |
| 285 |
public function getEndpoint() |
| 286 |
{ |
| 287 |
return $this->endpoint; |
| 288 |
} |
| 289 |
public function getRegion() |
| 290 |
{ |
| 291 |
return $this->region; |
| 292 |
} |
| 293 |
public function getApi() |
| 294 |
{ |
| 295 |
return $this->api; |
| 296 |
} |
| 297 |
public function getCommand($name, array $args = []) |
| 298 |
{ |
| 299 |
// Fail fast if the command cannot be found in the description. |
| 300 |
if (!isset($this->getApi()['operations'][$name])) { |
| 301 |
$name = \ucfirst($name); |
| 302 |
if (!isset($this->getApi()['operations'][$name])) { |
| 303 |
throw new \InvalidArgumentException("Operation not found: {$name}"); |
| 304 |
} |
| 305 |
} |
| 306 |
if (!isset($args['@http'])) { |
| 307 |
$args['@http'] = $this->defaultRequestOptions; |
| 308 |
} else { |
| 309 |
$args['@http'] += $this->defaultRequestOptions; |
| 310 |
} |
| 311 |
return new Command($name, $args, clone $this->getHandlerList()); |
| 312 |
} |
| 313 |
public function getEndpointProvider() |
| 314 |
{ |
| 315 |
return $this->endpointProvider; |
| 316 |
} |
| 317 |
/** |
| 318 |
* Provides the set of service context parameter |
| 319 |
* key-value pairs used for endpoint resolution. |
| 320 |
* |
| 321 |
* @return array |
| 322 |
*/ |
| 323 |
public function getClientContextParams() |
| 324 |
{ |
| 325 |
return $this->clientContextParams; |
| 326 |
} |
| 327 |
/** |
| 328 |
* Provides the set of built-in keys and values |
| 329 |
* used for endpoint resolution |
| 330 |
* |
| 331 |
* @return array |
| 332 |
*/ |
| 333 |
public function getClientBuiltIns() |
| 334 |
{ |
| 335 |
return $this->clientBuiltIns; |
| 336 |
} |
| 337 |
public function __sleep() |
| 338 |
{ |
| 339 |
throw new \RuntimeException('Instances of ' . static::class . ' cannot be serialized'); |
| 340 |
} |
| 341 |
/** |
| 342 |
* Get the signature_provider function of the client. |
| 343 |
* |
| 344 |
* @return callable |
| 345 |
*/ |
| 346 |
public final function getSignatureProvider() |
| 347 |
{ |
| 348 |
return $this->signatureProvider; |
| 349 |
} |
| 350 |
/** |
| 351 |
* Parse the class name and setup the custom exception class of the client |
| 352 |
* and return the "service" name of the client and "exception_class". |
| 353 |
* |
| 354 |
* @return array |
| 355 |
*/ |
| 356 |
private function parseClass() |
| 357 |
{ |
| 358 |
$klass = \get_class($this); |
| 359 |
if ($klass === __CLASS__) { |
| 360 |
return ['', AwsException::class]; |
| 361 |
} |
| 362 |
$service = \substr($klass, \strrpos($klass, '\\') + 1, -6); |
| 363 |
return [\strtolower($service), "Dudlewebs\\WPMCS\\s3\\Aws\\{$service}\\Exception\\{$service}Exception"]; |
| 364 |
} |
| 365 |
private function addEndpointParameterMiddleware($args) |
| 366 |
{ |
| 367 |
if (empty($args['disable_host_prefix_injection'])) { |
| 368 |
$list = $this->getHandlerList(); |
| 369 |
$list->appendBuild(EndpointParameterMiddleware::wrap($this->api), 'endpoint_parameter'); |
| 370 |
} |
| 371 |
} |
| 372 |
private function addEndpointDiscoveryMiddleware($config, $args) |
| 373 |
{ |
| 374 |
$list = $this->getHandlerList(); |
| 375 |
if (!isset($args['endpoint'])) { |
| 376 |
$list->appendBuild(EndpointDiscoveryMiddleware::wrap($this, $args, $config['endpoint_discovery']), 'EndpointDiscoveryMiddleware'); |
| 377 |
} |
| 378 |
} |
| 379 |
private function addSignatureMiddleware(array $args) |
| 380 |
{ |
| 381 |
$api = $this->getApi(); |
| 382 |
$provider = $this->signatureProvider; |
| 383 |
$signatureVersion = $this->config['signature_version']; |
| 384 |
$name = $this->config['signing_name']; |
| 385 |
$region = $this->config['signing_region']; |
| 386 |
$signingRegionSet = $this->signingRegionSet; |
| 387 |
if (isset($args['signature_version']) || isset($this->config['configured_signature_version'])) { |
| 388 |
$configuredSignatureVersion = \true; |
| 389 |
} else { |
| 390 |
$configuredSignatureVersion = \false; |
| 391 |
} |
| 392 |
$resolver = static function (CommandInterface $command) use($api, $provider, $name, $region, $signatureVersion, $configuredSignatureVersion, $signingRegionSet) { |
| 393 |
if (!$configuredSignatureVersion) { |
| 394 |
if (!empty($command['@context']['signing_region'])) { |
| 395 |
$region = $command['@context']['signing_region']; |
| 396 |
} |
| 397 |
if (!empty($command['@context']['signing_service'])) { |
| 398 |
$name = $command['@context']['signing_service']; |
| 399 |
} |
| 400 |
if (!empty($command['@context']['signature_version'])) { |
| 401 |
$signatureVersion = $command['@context']['signature_version']; |
| 402 |
} |
| 403 |
$authType = $api->getOperation($command->getName())['authtype']; |
| 404 |
switch ($authType) { |
| 405 |
case 'none': |
| 406 |
$signatureVersion = 'anonymous'; |
| 407 |
break; |
| 408 |
case 'v4-unsigned-body': |
| 409 |
$signatureVersion = 'v4-unsigned-body'; |
| 410 |
break; |
| 411 |
case 'bearer': |
| 412 |
$signatureVersion = 'bearer'; |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
if ($signatureVersion === 'v4a') { |
| 417 |
$commandSigningRegionSet = !empty($command['@context']['signing_region_set']) ? \implode(', ', $command['@context']['signing_region_set']) : null; |
| 418 |
$region = $signingRegionSet ?? $commandSigningRegionSet ?? $region; |
| 419 |
} |
| 420 |
// Capture signature metric |
| 421 |
$command->getMetricsBuilder()->identifyMetricByValueAndAppend('signature', $signatureVersion); |
| 422 |
return SignatureProvider::resolve($provider, $signatureVersion, $name, $region); |
| 423 |
}; |
| 424 |
$this->handlerList->appendSign(Middleware::signer($this->credentialProvider, $resolver, $this->tokenProvider, $this->getConfig()), 'signer'); |
| 425 |
} |
| 426 |
private function addRequestCompressionMiddleware($config) |
| 427 |
{ |
| 428 |
if (empty($config['disable_request_compression'])) { |
| 429 |
$list = $this->getHandlerList(); |
| 430 |
$list->appendBuild(RequestCompressionMiddleware::wrap($config), 'request-compression'); |
| 431 |
} |
| 432 |
} |
| 433 |
private function addQueryCompatibleInputMiddleware(Service $api) |
| 434 |
{ |
| 435 |
$list = $this->getHandlerList(); |
| 436 |
$list->appendValidate(QueryCompatibleInputMiddleware::wrap($api), 'query-compatible-input'); |
| 437 |
} |
| 438 |
private function addQueryModeHeader() : void |
| 439 |
{ |
| 440 |
$list = $this->getHandlerList(); |
| 441 |
$list->appendBuild(Middleware::mapRequest(function (RequestInterface $r) { |
| 442 |
return $r->withHeader('x-amzn-query-mode', "true"); |
| 443 |
}), 'x-amzn-query-mode-header'); |
| 444 |
} |
| 445 |
private function addInvocationId() |
| 446 |
{ |
| 447 |
// Add invocation id to each request |
| 448 |
$this->handlerList->prependSign(Middleware::invocationId(), 'invocation-id'); |
| 449 |
} |
| 450 |
private function loadAliases($file = null) |
| 451 |
{ |
| 452 |
if (!isset($this->aliases)) { |
| 453 |
if (\is_null($file)) { |
| 454 |
$file = __DIR__ . '/data/aliases.json'; |
| 455 |
} |
| 456 |
$aliases = \Dudlewebs\WPMCS\s3\Aws\load_compiled_json($file); |
| 457 |
$serviceId = $this->api->getServiceId(); |
| 458 |
$version = $this->getApi()->getApiVersion(); |
| 459 |
if (!empty($aliases['operations'][$serviceId][$version])) { |
| 460 |
$this->aliases = \array_flip($aliases['operations'][$serviceId][$version]); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
private function addStreamRequestPayload() |
| 465 |
{ |
| 466 |
$streamRequestPayloadMiddleware = StreamRequestPayloadMiddleware::wrap($this->api); |
| 467 |
$this->handlerList->prependSign($streamRequestPayloadMiddleware, 'StreamRequestPayloadMiddleware'); |
| 468 |
} |
| 469 |
private function addRecursionDetection() |
| 470 |
{ |
| 471 |
// Add recursion detection header to requests |
| 472 |
// originating in supported Lambda runtimes |
| 473 |
$this->handlerList->appendBuild(Middleware::recursionDetection(), 'recursion-detection'); |
| 474 |
} |
| 475 |
private function addAuthSelectionMiddleware(array $args) |
| 476 |
{ |
| 477 |
$list = $this->getHandlerList(); |
| 478 |
$list->prependBuild(AuthSelectionMiddleware::wrap($this->authSchemeResolver, $this->getApi(), $args['auth_scheme_preference'] ?? null), 'auth-selection'); |
| 479 |
} |
| 480 |
private function addEndpointV2Middleware() |
| 481 |
{ |
| 482 |
$list = $this->getHandlerList(); |
| 483 |
$endpointArgs = $this->getEndpointProviderArgs(); |
| 484 |
$list->prependBuild(EndpointV2Middleware::wrap($this->endpointProvider, $this->getApi(), $endpointArgs, $this->credentialProvider), 'endpoint-resolution'); |
| 485 |
} |
| 486 |
/** |
| 487 |
* Appends the user agent middleware. |
| 488 |
* This middleware MUST be appended after the |
| 489 |
* signature middleware `addSignatureMiddleware`, |
| 490 |
* so that metrics around signatures are properly |
| 491 |
* captured. |
| 492 |
* |
| 493 |
* @param $args |
| 494 |
* @return void |
| 495 |
*/ |
| 496 |
private function addUserAgentMiddleware($args) |
| 497 |
{ |
| 498 |
$this->getHandlerList()->appendSign(UserAgentMiddleware::wrap($args), 'user-agent'); |
| 499 |
} |
| 500 |
/** |
| 501 |
* Retrieves client context param definition from service model, |
| 502 |
* creates mapping of client context param names with client-provided |
| 503 |
* values. |
| 504 |
* |
| 505 |
* @return array |
| 506 |
*/ |
| 507 |
private function setClientContextParams($args) |
| 508 |
{ |
| 509 |
$api = $this->getApi(); |
| 510 |
$resolvedParams = []; |
| 511 |
if (!empty($paramDefinitions = $api->getClientContextParams())) { |
| 512 |
foreach ($paramDefinitions as $paramName => $paramValue) { |
| 513 |
if (isset($args[$paramName])) { |
| 514 |
$resolvedParams[$paramName] = $args[$paramName]; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
return $resolvedParams; |
| 519 |
} |
| 520 |
/** |
| 521 |
* Retrieves and sets default values used for endpoint resolution. |
| 522 |
*/ |
| 523 |
private function setClientBuiltIns($args, $resolvedConfig) |
| 524 |
{ |
| 525 |
$builtIns = []; |
| 526 |
$config = $resolvedConfig['config']; |
| 527 |
$service = $args['service']; |
| 528 |
$builtIns['SDK::Endpoint'] = null; |
| 529 |
if (!empty($args['endpoint'])) { |
| 530 |
$builtIns['SDK::Endpoint'] = $args['endpoint']; |
| 531 |
} elseif (isset($config['configured_endpoint_url'])) { |
| 532 |
$builtIns['SDK::Endpoint'] = (string) $this->getEndpoint(); |
| 533 |
} |
| 534 |
$builtIns['AWS::Region'] = $this->getRegion(); |
| 535 |
$builtIns['AWS::UseFIPS'] = $config['use_fips_endpoint']->isUseFipsEndpoint(); |
| 536 |
$builtIns['AWS::UseDualStack'] = $config['use_dual_stack_endpoint']->isUseDualstackEndpoint(); |
| 537 |
if ($service === 's3' || $service === 's3control') { |
| 538 |
$builtIns['AWS::S3::UseArnRegion'] = $config['use_arn_region']->isUseArnRegion(); |
| 539 |
} |
| 540 |
if ($service === 's3') { |
| 541 |
$builtIns['AWS::S3::UseArnRegion'] = $config['use_arn_region']->isUseArnRegion(); |
| 542 |
$builtIns['AWS::S3::Accelerate'] = $config['use_accelerate_endpoint']; |
| 543 |
$builtIns['AWS::S3::ForcePathStyle'] = $config['use_path_style_endpoint']; |
| 544 |
$builtIns['AWS::S3::DisableMultiRegionAccessPoints'] = $config['disable_multiregion_access_points']; |
| 545 |
} |
| 546 |
$builtIns['AWS::Auth::AccountIdEndpointMode'] = $resolvedConfig['account_id_endpoint_mode']; |
| 547 |
$this->clientBuiltIns += $builtIns; |
| 548 |
} |
| 549 |
/** |
| 550 |
* Retrieves arguments to be used in endpoint resolution. |
| 551 |
* |
| 552 |
* @return array |
| 553 |
*/ |
| 554 |
public function getEndpointProviderArgs() |
| 555 |
{ |
| 556 |
return $this->normalizeEndpointProviderArgs(); |
| 557 |
} |
| 558 |
/** |
| 559 |
* Combines built-in and client context parameter values in |
| 560 |
* order of specificity. Client context parameter values supersede |
| 561 |
* built-in values. |
| 562 |
* |
| 563 |
* @return array |
| 564 |
*/ |
| 565 |
private function normalizeEndpointProviderArgs() |
| 566 |
{ |
| 567 |
$normalizedBuiltIns = []; |
| 568 |
foreach ($this->clientBuiltIns as $name => $value) { |
| 569 |
$normalizedName = \explode('::', $name); |
| 570 |
$normalizedName = $normalizedName[\count($normalizedName) - 1]; |
| 571 |
$normalizedBuiltIns[$normalizedName] = $value; |
| 572 |
} |
| 573 |
return \array_merge($normalizedBuiltIns, $this->getClientContextParams()); |
| 574 |
} |
| 575 |
protected function isUseEndpointV2() |
| 576 |
{ |
| 577 |
return $this->endpointProvider instanceof EndpointProviderV2; |
| 578 |
} |
| 579 |
public static function emitDeprecationWarning() |
| 580 |
{ |
| 581 |
\trigger_error("This method is deprecated. It will be removed in an upcoming release.", \E_USER_DEPRECATED); |
| 582 |
$phpVersion = \PHP_VERSION_ID; |
| 583 |
if ($phpVersion < 70205) { |
| 584 |
$phpVersionString = \phpversion(); |
| 585 |
@\trigger_error("This installation of the SDK is using PHP version" . " {$phpVersionString}, which will be deprecated on August" . " 15th, 2023. Please upgrade your PHP version to a minimum of" . " 7.2.5 before then to continue receiving updates to the AWS" . " SDK for PHP. To disable this warning, set" . " suppress_php_deprecation_warning to true on the client constructor" . " or set the environment variable AWS_SUPPRESS_PHP_DEPRECATION_WARNING" . " to true.", \E_USER_DEPRECATED); |
| 586 |
} |
| 587 |
} |
| 588 |
/** |
| 589 |
* Returns a service model and doc model with any necessary changes |
| 590 |
* applied. |
| 591 |
* |
| 592 |
* @param array $api Array of service data being documented. |
| 593 |
* @param array $docs Array of doc model data. |
| 594 |
* |
| 595 |
* @return array Tuple containing a [Service, DocModel] |
| 596 |
* |
| 597 |
* @internal This should only used to document the service API. |
| 598 |
* @codeCoverageIgnore |
| 599 |
*/ |
| 600 |
public static function applyDocFilters(array $api, array $docs) |
| 601 |
{ |
| 602 |
$aliases = \Dudlewebs\WPMCS\s3\Aws\load_compiled_json(__DIR__ . '/data/aliases.json'); |
| 603 |
$serviceId = $api['metadata']['serviceId'] ?? ''; |
| 604 |
$version = $api['metadata']['apiVersion']; |
| 605 |
// Replace names for any operations with SDK aliases |
| 606 |
if (!empty($aliases['operations'][$serviceId][$version])) { |
| 607 |
foreach ($aliases['operations'][$serviceId][$version] as $op => $alias) { |
| 608 |
$api['operations'][$alias] = $api['operations'][$op]; |
| 609 |
$docs['operations'][$alias] = $docs['operations'][$op]; |
| 610 |
unset($api['operations'][$op], $docs['operations'][$op]); |
| 611 |
} |
| 612 |
} |
| 613 |
\ksort($api['operations']); |
| 614 |
return [new Service($api, ApiProvider::defaultProvider()), new DocModel($docs)]; |
| 615 |
} |
| 616 |
/** |
| 617 |
* @deprecated |
| 618 |
* @return static |
| 619 |
*/ |
| 620 |
public static function factory(array $config = []) |
| 621 |
{ |
| 622 |
return new static($config); |
| 623 |
} |
| 624 |
} |
| 625 |
|