-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: account id endpoint resolution support
This change add account_id as part of the identity resolution, from the different credentials provider. It also validates whether an account should have been resolved based on the configure option account_id_endpoint_mode. The way this is done is by using lazy resolvers. We wrap the credentials provider into a custom lazy resolver that will avoid resolving credentials more than once, which means that in that credentials lazy resolver the value will be resolved once, and it will be returned everytime the credentials provider is consumed/invoked. For accountId builts-in, we also use a lazy resolver which holds the validation for wheter account_id value should have been resolved as part of the resolved identity. This accountId built-ins lazy resolver is resolved from endpoint resolution.
- Loading branch information
1 parent
76d1165
commit 89aec5e
Showing
14 changed files
with
331 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Aws; | ||
|
||
use Aws\Exception\AccountIdNotFoundException; | ||
use GuzzleHttp\Promise\FulfilledPromise; | ||
use GuzzleHttp\Promise\Promise; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
class AccountIdLazyResolver implements LazyResolver | ||
{ | ||
const ACCOUNT_ID_ENDPOINT_MODE_DISABLED = 'disabled'; | ||
const ACCOUNT_ID_ENDPOINT_MODE_REQUIRED = 'required'; | ||
const ACCOUNT_ID_ENDPOINT_MODE_PREFERRED = 'preferred'; | ||
|
||
/** | ||
* @var LazyResolver $credentialsProvider | ||
*/ | ||
private $credentialsProvider; | ||
/** | ||
* @var string $accountIdEndpointMode | ||
*/ | ||
private $accountIdEndpointMode; | ||
|
||
public function __construct(LazyResolver $credentialsProvider, $accountIdEndpointMode) | ||
{ | ||
$this->credentialsProvider = $credentialsProvider; | ||
$this->accountIdEndpointMode = $accountIdEndpointMode; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolve(bool $force = false): mixed | ||
{ | ||
$identity = $this->credentialsProvider->resolve(); | ||
$accountId = $identity->getAccountId(); | ||
if (empty($accountId)) { | ||
$message = function ($mode) { | ||
return "It is ${mode} to resolve an account id based on the 'account_id_endpoint_mode' configuration. \n- If you are using credentials from a shared ini file, please make sure you have configured the property aws_account_id. \n- If you are using credentials defined in environment variables please make sure you have set AWS_ACCOUNT_ID. \n- Otherwise, if you are supplying credentials as part of client constructor parameters, please make sure you have set the property account_id.\n If you prefer to not use account id endpoint resolution then, please make account_id_endpoint_mode to be disabled by either providing it explicitly in the client, defining a config property in your shared config file account_id_endpoint_mode, or by setting an environment variable called AWS_ACCOUNT_ID_ENDPOINT_MODE, and the value for any of those source should be 'disabled' if the desire is to disable this behavior."; | ||
}; | ||
|
||
switch ($this->accountIdEndpointMode) { | ||
case self::ACCOUNT_ID_ENDPOINT_MODE_REQUIRED: | ||
throw new AccountIdNotFoundException($message('required')); | ||
case self::ACCOUNT_ID_ENDPOINT_MODE_PREFERRED: | ||
trigger_error($message('preferred'), E_USER_WARNING); | ||
return null; | ||
case self::ACCOUNT_ID_ENDPOINT_MODE_DISABLED: | ||
return null; | ||
default: | ||
throw new \RuntimeException("Unrecognized account_id_endpoint_mode value " . $this->accountIdEndpointMode."\n Valid Values are: [" . implode(', ', [self::ACCOUNT_ID_ENDPOINT_MODE_DISABLED, self::ACCOUNT_ID_ENDPOINT_MODE_PREFERRED, self::ACCOUNT_ID_ENDPOINT_MODE_REQUIRED]) . "]"); | ||
} | ||
} | ||
|
||
return $accountId; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function isResolved(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.