| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | use Dudlewebs\WPMCS\s3\Aws\EndpointV2\EndpointV2Middleware; |
| 13 | 13 | use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException; |
| 14 | 14 | use Dudlewebs\WPMCS\s3\Aws\Signature\SignatureProvider; |
| 15 | 15 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 16 | +use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; | |
| 16 | 17 | /** |
| 17 | 18 | * Default AWS client implementation |
| 18 | 19 | */ |
| 19 | 20 | class AwsClient implements AwsClientInterface |
| @@ -252,15 +253,17 @@ | ||
| 252 | 253 | $this->addRecursionDetection(); |
| 253 | 254 | if ($this->isUseEndpointV2()) { |
| 254 | 255 | $this->addEndpointV2Middleware(); |
| 255 | 256 | } |
| 256 | - $this->addAuthSelectionMiddleware(); | |
| 257 | + $this->addAuthSelectionMiddleware($config['config']); | |
| 257 | 258 | if (!\is_null($this->api->getMetadata('awsQueryCompatible'))) { |
| 258 | 259 | $this->addQueryCompatibleInputMiddleware($this->api); |
| 260 | + $this->addQueryModeHeader(); | |
| 259 | 261 | } |
| 260 | 262 | if (isset($args['with_resolved'])) { |
| 261 | 263 | $args['with_resolved']($config); |
| 262 | 264 | } |
| 265 | + $this->addUserAgentMiddleware($config); | |
| 263 | 266 | } |
| 264 | 267 | public function getHandlerList() |
| 265 | 268 | { |
| 266 | 269 | return $this->handlerList; |
| @@ -273,8 +276,13 @@ | ||
| 273 | 276 | { |
| 274 | 277 | $fn = $this->credentialProvider; |
| 275 | 278 | return $fn(); |
| 276 | 279 | } |
| 280 | + public function getToken() | |
| 281 | + { | |
| 282 | + $fn = $this->tokenProvider; | |
| 283 | + return $fn(); | |
| 284 | + } | |
| 277 | 285 | public function getEndpoint() |
| 278 | 286 | { |
| 279 | 287 | return $this->endpoint; |
| 280 | 288 | } |
| @@ -380,20 +388,20 @@ | ||
| 380 | 388 | $configuredSignatureVersion = \true; |
| 381 | 389 | } else { |
| 382 | 390 | $configuredSignatureVersion = \false; |
| 383 | 391 | } |
| 384 | - $resolver = static function (CommandInterface $c) use($api, $provider, $name, $region, $signatureVersion, $configuredSignatureVersion, $signingRegionSet) { | |
| 392 | + $resolver = static function (CommandInterface $command) use($api, $provider, $name, $region, $signatureVersion, $configuredSignatureVersion, $signingRegionSet) { | |
| 385 | 393 | if (!$configuredSignatureVersion) { |
| 386 | - if (!empty($c['@context']['signing_region'])) { | |
| 387 | - $region = $c['@context']['signing_region']; | |
| 394 | + if (!empty($command['@context']['signing_region'])) { | |
| 395 | + $region = $command['@context']['signing_region']; | |
| 388 | 396 | } |
| 389 | - if (!empty($c['@context']['signing_service'])) { | |
| 390 | - $name = $c['@context']['signing_service']; | |
| 397 | + if (!empty($command['@context']['signing_service'])) { | |
| 398 | + $name = $command['@context']['signing_service']; | |
| 391 | 399 | } |
| 392 | - if (!empty($c['@context']['signature_version'])) { | |
| 393 | - $signatureVersion = $c['@context']['signature_version']; | |
| 400 | + if (!empty($command['@context']['signature_version'])) { | |
| 401 | + $signatureVersion = $command['@context']['signature_version']; | |
| 394 | 402 | } |
| 395 | - $authType = $api->getOperation($c->getName())['authtype']; | |
| 403 | + $authType = $api->getOperation($command->getName())['authtype']; | |
| 396 | 404 | switch ($authType) { |
| 397 | 405 | case 'none': |
| 398 | 406 | $signatureVersion = 'anonymous'; |
| 399 | 407 | break; |
| @@ -405,11 +413,13 @@ | ||
| 405 | 413 | break; |
| 406 | 414 | } |
| 407 | 415 | } |
| 408 | 416 | if ($signatureVersion === 'v4a') { |
| 409 | - $commandSigningRegionSet = !empty($c['@context']['signing_region_set']) ? \implode(', ', $c['@context']['signing_region_set']) : null; | |
| 417 | + $commandSigningRegionSet = !empty($command['@context']['signing_region_set']) ? \implode(', ', $command['@context']['signing_region_set']) : null; | |
| 410 | 418 | $region = $signingRegionSet ?? $commandSigningRegionSet ?? $region; |
| 411 | 419 | } |
| 420 | + // Capture signature metric | |
| 421 | + $command->getMetricsBuilder()->identifyMetricByValueAndAppend('signature', $signatureVersion); | |
| 412 | 422 | return SignatureProvider::resolve($provider, $signatureVersion, $name, $region); |
| 413 | 423 | }; |
| 414 | 424 | $this->handlerList->appendSign(Middleware::signer($this->credentialProvider, $resolver, $this->tokenProvider, $this->getConfig()), 'signer'); |
| 415 | 425 | } |
| @@ -424,8 +434,15 @@ | ||
| 424 | 434 | { |
| 425 | 435 | $list = $this->getHandlerList(); |
| 426 | 436 | $list->appendValidate(QueryCompatibleInputMiddleware::wrap($api), 'query-compatible-input'); |
| 427 | 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 | + } | |
| 428 | 445 | private function addInvocationId() |
| 429 | 446 | { |
| 430 | 447 | // Add invocation id to each request |
| 431 | 448 | $this->handlerList->prependSign(Middleware::invocationId(), 'invocation-id'); |
| @@ -454,12 +471,12 @@ | ||
| 454 | 471 | // Add recursion detection header to requests |
| 455 | 472 | // originating in supported Lambda runtimes |
| 456 | 473 | $this->handlerList->appendBuild(Middleware::recursionDetection(), 'recursion-detection'); |
| 457 | 474 | } |
| 458 | - private function addAuthSelectionMiddleware() | |
| 475 | + private function addAuthSelectionMiddleware(array $args) | |
| 459 | 476 | { |
| 460 | 477 | $list = $this->getHandlerList(); |
| 461 | - $list->prependBuild(AuthSelectionMiddleware::wrap($this->authSchemeResolver, $this->getApi()), 'auth-selection'); | |
| 478 | + $list->prependBuild(AuthSelectionMiddleware::wrap($this->authSchemeResolver, $this->getApi(), $args['auth_scheme_preference'] ?? null), 'auth-selection'); | |
| 462 | 479 | } |
| 463 | 480 | private function addEndpointV2Middleware() |
| 464 | 481 | { |
| 465 | 482 | $list = $this->getHandlerList(); |
| @@ -466,8 +483,22 @@ | ||
| 466 | 483 | $endpointArgs = $this->getEndpointProviderArgs(); |
| 467 | 484 | $list->prependBuild(EndpointV2Middleware::wrap($this->endpointProvider, $this->getApi(), $endpointArgs, $this->credentialProvider), 'endpoint-resolution'); |
| 468 | 485 | } |
| 469 | 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 | + /** | |
| 470 | 501 | * Retrieves client context param definition from service model, |
| 471 | 502 | * creates mapping of client context param names with client-provided |
| 472 | 503 | * values. |
| 473 | 504 | * |
| @@ -546,8 +577,9 @@ | ||
| 546 | 577 | return $this->endpointProvider instanceof EndpointProviderV2; |
| 547 | 578 | } |
| 548 | 579 | public static function emitDeprecationWarning() |
| 549 | 580 | { |
| 581 | + \trigger_error("This method is deprecated. It will be removed in an upcoming release.", \E_USER_DEPRECATED); | |
| 550 | 582 | $phpVersion = \PHP_VERSION_ID; |
| 551 | 583 | if ($phpVersion < 70205) { |
| 552 | 584 | $phpVersionString = \phpversion(); |
| 553 | 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); |