Skip to content

Commit 40eadff

Browse files
Revert "enhancement: user agent 2.1 (#3001)" (#3046)
This reverts commit c9b20ac.
1 parent c9b20ac commit 40eadff

37 files changed

+448
-3335
lines changed

src/AwsClient.php

+10-35
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ public function __construct(array $args)
282282
if (isset($args['with_resolved'])) {
283283
$args['with_resolved']($config);
284284
}
285-
$this->addUserAgentMiddleware($config);
286285
}
287286

288287
public function getHandlerList()
@@ -450,7 +449,7 @@ private function addSignatureMiddleware(array $args)
450449
}
451450

452451
$resolver = static function (
453-
CommandInterface $command
452+
CommandInterface $c
454453
) use (
455454
$api,
456455
$provider,
@@ -461,17 +460,17 @@ private function addSignatureMiddleware(array $args)
461460
$signingRegionSet
462461
) {
463462
if (!$configuredSignatureVersion) {
464-
if (!empty($command['@context']['signing_region'])) {
465-
$region = $command['@context']['signing_region'];
463+
if (!empty($c['@context']['signing_region'])) {
464+
$region = $c['@context']['signing_region'];
466465
}
467-
if (!empty($command['@context']['signing_service'])) {
468-
$name = $command['@context']['signing_service'];
466+
if (!empty($c['@context']['signing_service'])) {
467+
$name = $c['@context']['signing_service'];
469468
}
470-
if (!empty($command['@context']['signature_version'])) {
471-
$signatureVersion = $command['@context']['signature_version'];
469+
if (!empty($c['@context']['signature_version'])) {
470+
$signatureVersion = $c['@context']['signature_version'];
472471
}
473472

474-
$authType = $api->getOperation($command->getName())['authtype'];
473+
$authType = $api->getOperation($c->getName())['authtype'];
475474
switch ($authType){
476475
case 'none':
477476
$signatureVersion = 'anonymous';
@@ -486,21 +485,15 @@ private function addSignatureMiddleware(array $args)
486485
}
487486

488487
if ($signatureVersion === 'v4a') {
489-
$commandSigningRegionSet = !empty($command['@context']['signing_region_set'])
490-
? implode(', ', $command['@context']['signing_region_set'])
488+
$commandSigningRegionSet = !empty($c['@context']['signing_region_set'])
489+
? implode(', ', $c['@context']['signing_region_set'])
491490
: null;
492491

493492
$region = $signingRegionSet
494493
?? $commandSigningRegionSet
495494
?? $region;
496495
}
497496

498-
// Capture signature metric
499-
$command->getMetricsBuilder()->identifyMetricByValueAndAppend(
500-
'signature',
501-
$signatureVersion
502-
);
503-
504497
return SignatureProvider::resolve($provider, $signatureVersion, $name, $region);
505498
};
506499
$this->handlerList->appendSign(
@@ -618,24 +611,6 @@ private function addEndpointV2Middleware()
618611
);
619612
}
620613

621-
/**
622-
* Appends the user agent middleware.
623-
* This middleware MUST be appended after the
624-
* signature middleware `addSignatureMiddleware`,
625-
* so that metrics around signatures are properly
626-
* captured.
627-
*
628-
* @param $args
629-
* @return void
630-
*/
631-
private function addUserAgentMiddleware($args)
632-
{
633-
$this->getHandlerList()->appendSign(
634-
UserAgentMiddleware::wrap($args),
635-
'user-agent'
636-
);
637-
}
638-
639614
/**
640615
* Retrieves client context param definition from service model,
641616
* creates mapping of client context param names with client-provided

src/ClientResolver.php

+83-1
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,66 @@ public static function _default_app_id(array $args)
993993

994994
public static function _apply_user_agent($inputUserAgent, array &$args, HandlerList $list)
995995
{
996+
// Add SDK version
997+
$userAgent = ['aws-sdk-php/' . Sdk::VERSION];
998+
999+
// User Agent Metadata
1000+
$userAgent[] = 'ua/2.0';
1001+
1002+
// If on HHVM add the HHVM version
1003+
if (defined('HHVM_VERSION')) {
1004+
$userAgent []= 'HHVM/' . HHVM_VERSION;
1005+
}
1006+
1007+
// Add OS version
1008+
$disabledFunctions = explode(',', ini_get('disable_functions'));
1009+
if (function_exists('php_uname')
1010+
&& !in_array('php_uname', $disabledFunctions, true)
1011+
) {
1012+
$osName = "OS/" . php_uname('s') . '#' . php_uname('r');
1013+
if (!empty($osName)) {
1014+
$userAgent []= $osName;
1015+
}
1016+
}
1017+
1018+
// Add the language version
1019+
$userAgent []= 'lang/php#' . phpversion();
1020+
1021+
// Add exec environment if present
1022+
if ($executionEnvironment = getenv('AWS_EXECUTION_ENV')) {
1023+
$userAgent []= $executionEnvironment;
1024+
}
1025+
9961026
// Add endpoint discovery if set
997-
$userAgent = [];
1027+
if (isset($args['endpoint_discovery'])) {
1028+
if (($args['endpoint_discovery'] instanceof \Aws\EndpointDiscovery\Configuration
1029+
&& $args['endpoint_discovery']->isEnabled())
1030+
) {
1031+
$userAgent []= 'cfg/endpoint-discovery';
1032+
} elseif (is_array($args['endpoint_discovery'])
1033+
&& isset($args['endpoint_discovery']['enabled'])
1034+
&& $args['endpoint_discovery']['enabled']
1035+
) {
1036+
$userAgent []= 'cfg/endpoint-discovery';
1037+
}
1038+
}
1039+
1040+
// Add retry mode if set
1041+
if (isset($args['retries'])) {
1042+
if ($args['retries'] instanceof \Aws\Retry\Configuration) {
1043+
$userAgent []= 'cfg/retry-mode#' . $args["retries"]->getMode();
1044+
} elseif (is_array($args['retries'])
1045+
&& isset($args["retries"]["mode"])
1046+
) {
1047+
$userAgent []= 'cfg/retry-mode#' . $args["retries"]["mode"];
1048+
}
1049+
}
1050+
1051+
// AppID Metadata
1052+
if (!empty($args['app_id'])) {
1053+
$userAgent[] = 'app/' . $args['app_id'];
1054+
}
1055+
9981056
// Add the input to the end
9991057
if ($inputUserAgent){
10001058
if (!is_array($inputUserAgent)) {
@@ -1005,6 +1063,30 @@ public static function _apply_user_agent($inputUserAgent, array &$args, HandlerL
10051063
}
10061064

10071065
$args['ua_append'] = $userAgent;
1066+
1067+
$list->appendBuild(static function (callable $handler) use ($userAgent) {
1068+
return function (
1069+
CommandInterface $command,
1070+
RequestInterface $request
1071+
) use ($handler, $userAgent) {
1072+
return $handler(
1073+
$command,
1074+
$request->withHeader(
1075+
'X-Amz-User-Agent',
1076+
implode(' ', array_merge(
1077+
$userAgent,
1078+
$request->getHeader('X-Amz-User-Agent')
1079+
))
1080+
)->withHeader(
1081+
'User-Agent',
1082+
implode(' ', array_merge(
1083+
$userAgent,
1084+
$request->getHeader('User-Agent')
1085+
))
1086+
)
1087+
);
1088+
};
1089+
});
10081090
}
10091091

10101092
public static function _apply_endpoint($value, array &$args, HandlerList $list)

src/Command.php

+2-23
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class Command implements CommandInterface
1414
/** @var HandlerList */
1515
private $handlerList;
1616

17-
/** @var array */
17+
/** @var Array */
1818
private $authSchemes;
1919

20-
/** @var MetricsBuilder */
21-
private $metricsBuilder;
22-
2320
/**
2421
* Accepts an associative array of command options, including:
2522
*
@@ -29,12 +26,7 @@ class Command implements CommandInterface
2926
* @param array $args Arguments to pass to the command
3027
* @param HandlerList $list Handler list
3128
*/
32-
public function __construct(
33-
$name,
34-
array $args = [],
35-
?HandlerList $list = null,
36-
?MetricsBuilder $metricsBuilder = null
37-
)
29+
public function __construct($name, array $args = [], ?HandlerList $list = null)
3830
{
3931
$this->name = $name;
4032
$this->data = $args;
@@ -46,7 +38,6 @@ public function __construct(
4638
if (!isset($this->data['@context'])) {
4739
$this->data['@context'] = [];
4840
}
49-
$this->metricsBuilder = $metricsBuilder ?: new MetricsBuilder();
5041
}
5142

5243
public function __clone()
@@ -119,16 +110,4 @@ public function get($name)
119110
{
120111
return $this[$name];
121112
}
122-
123-
/**
124-
* Returns the metrics builder instance tied up to this command.
125-
*
126-
* @internal
127-
*
128-
* @return MetricsBuilder
129-
*/
130-
public function getMetricsBuilder(): MetricsBuilder
131-
{
132-
return $this->metricsBuilder;
133-
}
134113
}

src/Credentials/AssumeRoleCredentialProvider.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public function __invoke()
5252
$client = $this->client;
5353
return $client->assumeRoleAsync($this->assumeRoleParams)
5454
->then(function (Result $result) {
55-
return $this->client->createCredentials(
56-
$result,
57-
CredentialSources::STS_ASSUME_ROLE
58-
);
55+
return $this->client->createCredentials($result);
5956
})->otherwise(function (\RuntimeException $exception) {
6057
throw new CredentialsException(
6158
"Error in retrieving assume role credentials.",

src/Credentials/AssumeRoleWithWebIdentityCredentialProvider.php

+10-14
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ class AssumeRoleWithWebIdentityCredentialProvider
3636

3737
/** @var integer */
3838
private $tokenFileReadAttempts;
39-
/** @var string */
40-
private $source;
4139

4240
/**
4341
* The constructor attempts to load config from environment variables.
4442
* If not set, the following config options are used:
4543
* - WebIdentityTokenFile: full path of token filename
4644
* - RoleArn: arn of role to be assumed
4745
* - SessionName: (optional) set by SDK if not provided
48-
* - source: To identify if the provider was sourced by a profile or
49-
* from environment definition. Default will be `sts_web_id_token`.
5046
*
5147
* @param array $config Configuration options
5248
* @throws \InvalidArgumentException
@@ -70,9 +66,15 @@ public function __construct(array $config = [])
7066
$this->retries = (int) getenv(self::ENV_RETRIES) ?: (isset($config['retries']) ? $config['retries'] : 3);
7167
$this->authenticationAttempts = 0;
7268
$this->tokenFileReadAttempts = 0;
73-
$this->session = $config['SessionName']
74-
?? 'aws-sdk-php-' . round(microtime(true) * 1000);
75-
$region = $config['region'] ?? 'us-east-1';
69+
70+
$this->session = isset($config['SessionName'])
71+
? $config['SessionName']
72+
: 'aws-sdk-php-' . round(microtime(true) * 1000);
73+
74+
$region = isset($config['region'])
75+
? $config['region']
76+
: 'us-east-1';
77+
7678
if (isset($config['client'])) {
7779
$this->client = $config['client'];
7880
} else {
@@ -82,9 +84,6 @@ public function __construct(array $config = [])
8284
'version' => 'latest'
8385
]);
8486
}
85-
86-
$this->source = $config['source']
87-
?? CredentialSources::STS_WEB_ID_TOKEN;
8887
}
8988

9089
/**
@@ -161,10 +160,7 @@ public function __invoke()
161160
$this->authenticationAttempts++;
162161
}
163162

164-
yield $this->client->createCredentials(
165-
$result,
166-
$this->source
167-
);
163+
yield $this->client->createCredentials($result);
168164
});
169165
}
170166
}

src/Credentials/CredentialProvider.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ public static function env()
302302
$secret,
303303
$token,
304304
null,
305-
$accountId,
306-
CredentialSources::ENVIRONMENT
305+
$accountId
307306
)
308307
);
309308
}
@@ -418,8 +417,7 @@ public static function assumeRoleWithWebIdentityCredentialProvider(array $config
418417
'WebIdentityTokenFile' => $tokenFromEnv,
419418
'SessionName' => $sessionName,
420419
'client' => $stsClient,
421-
'region' => $region,
422-
'source' => CredentialSources::ENVIRONMENT_STS_WEB_ID_TOKEN
420+
'region' => $region
423421
]);
424422

425423
return $provider();
@@ -448,8 +446,7 @@ public static function assumeRoleWithWebIdentityCredentialProvider(array $config
448446
'WebIdentityTokenFile' => $profile['web_identity_token_file'],
449447
'SessionName' => $sessionName,
450448
'client' => $stsClient,
451-
'region' => $region,
452-
'source' => CredentialSources::PROFILE_STS_WEB_ID_TOKEN
449+
'region' => $region
453450
]);
454451

455452
return $provider();
@@ -556,8 +553,7 @@ public static function ini($profile = null, $filename = null, array $config = []
556553
$data[$profile]['aws_secret_access_key'],
557554
$data[$profile]['aws_session_token'],
558555
null,
559-
$data[$profile]['aws_account_id'] ?? null,
560-
CredentialSources::PROFILE
556+
!empty($data[$profile]['aws_account_id']) ? $data[$profile]['aws_account_id'] : null
561557
)
562558
);
563559
};
@@ -645,8 +641,7 @@ public static function process($profile = null, $filename = null)
645641
$processData['SecretAccessKey'],
646642
$processData['SessionToken'],
647643
$expires,
648-
$accountId,
649-
CredentialSources::PROFILE_PROCESS
644+
$accountId
650645
)
651646
);
652647
};
@@ -729,10 +724,7 @@ private static function loadRoleProfile(
729724
'RoleArn' => $roleArn,
730725
'RoleSessionName' => $roleSessionName
731726
]);
732-
$credentials = $stsClient->createCredentials(
733-
$result,
734-
CredentialSources::STS_ASSUME_ROLE
735-
);
727+
$credentials = $stsClient->createCredentials($result);
736728

737729
return Promise\Create::promiseFor($credentials);
738730
}
@@ -926,8 +918,7 @@ private static function getSsoCredentials($profiles, $ssoProfileName, $filename,
926918
$ssoCredentials['secretAccessKey'],
927919
$ssoCredentials['sessionToken'],
928920
$expiration,
929-
$ssoProfile['sso_account_id'],
930-
CredentialSources::PROFILE_SSO
921+
$ssoProfile['sso_account_id']
931922
)
932923
);
933924
}
@@ -987,8 +978,7 @@ private static function getSsoCredentialsLegacy($profiles, $ssoProfileName, $fil
987978
$ssoCredentials['secretAccessKey'],
988979
$ssoCredentials['sessionToken'],
989980
$expiration,
990-
$ssoProfile['sso_account_id'],
991-
CredentialSources::PROFILE_SSO_LEGACY
981+
$ssoProfile['sso_account_id']
992982
)
993983
);
994984
}

0 commit comments

Comments
 (0)