-
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, and when this option is not disabled then, it prepends a middleware that resolves the identity, from the provided credential provider and, it validates the account id based on the account_id_endpoint_mode option. When an identity is resolved by this middleware then, this identity is carry over in a command property bag called @context, which is then reused by the signer middleware, and any other middleware that needs to resolve identity. This is done for avoiding having to resolve identity multiple times in a single request. The property is: $command['@context']['resolved_identity'];
- Loading branch information
1 parent
76d1165
commit 0876457
Showing
34 changed files
with
1,651 additions
and
20 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,79 @@ | ||
<?php | ||
namespace Aws; | ||
|
||
use Aws\Exception\CredentialsException; | ||
|
||
/** | ||
* This middleware class resolves the identity from a credentials provider callable function | ||
* and determine whether an account should have been resolved. When this middleware resolves | ||
* identity then, the identity is included in the $command context bag property "$command['@context]" | ||
* as resolved_identity. Example $command['@context]['resolved_identity'] = $resolvedIdentity, and | ||
* when this property is set then, the signer middleware gives preference to use that resolved identity | ||
* instead of resolving the provided credentials provider by the client. This is done to avoid having to | ||
* resolve credentials more than once per request. | ||
*/ | ||
class AccountIdEndpointMiddleware | ||
{ | ||
/** | ||
* @var callable $nextHandler | ||
*/ | ||
private $nextHandler; | ||
/** | ||
* @var string $accountIdEndpointMode | ||
*/ | ||
private $accountIdEndpointMode; | ||
/** | ||
* @var callable $credentialsProvider | ||
*/ | ||
private $credentialsProvider; | ||
|
||
/** | ||
* @param callable $nextHandler | ||
* @param string $accountIdEndpointMode | ||
* @param callable $credentialsProvider | ||
*/ | ||
public function __construct($nextHandler, $accountIdEndpointMode, $credentialsProvider) | ||
{ | ||
$this->nextHandler = $nextHandler; | ||
$this->accountIdEndpointMode = $accountIdEndpointMode; | ||
$this->credentialsProvider = $credentialsProvider; | ||
} | ||
|
||
/** | ||
* This method wraps a new instance of the AccountIdEndpointMiddleware. | ||
* | ||
* @param string $accountIddEndpointMode | ||
* @param callable $credentialsProvider | ||
* @return callable | ||
*/ | ||
public static function wrap($accountIddEndpointMode, $credentialsProvider): callable | ||
{ | ||
return function (callable $handler) use ($accountIddEndpointMode, $credentialsProvider) { | ||
return new self($handler, $accountIddEndpointMode, $credentialsProvider); | ||
}; | ||
} | ||
|
||
public function __invoke($command) | ||
{ | ||
$nextHandler = $this->nextHandler; | ||
$fnCredentialsProvider = $this->credentialsProvider; | ||
$resolvedIdentity = $fnCredentialsProvider()->wait(); | ||
if (empty($resolvedIdentity->getAccountId())) { | ||
$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 'required': | ||
throw new CredentialsException($message('required')); | ||
case 'preferred': | ||
error_log('Warning: ' . $message('preferred'), E_WARNING|E_NOTICE); | ||
break; | ||
} | ||
} | ||
|
||
$command['@context']['resolved_identity'] = $resolvedIdentity; | ||
|
||
return $nextHandler($command); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,6 @@ public function isExpired(); | |
* @return array | ||
*/ | ||
public function toArray(); | ||
|
||
public function getAccountId(); | ||
} |
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.