| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Credentials; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Exception\CredentialsException; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Result; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Sts\StsClient; |
| 8 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 9 |
/** |
| 10 |
* Credential provider that provides credentials via assuming a role |
| 11 |
* More Information, see: http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sts-2011-06-15.html#assumerole |
| 12 |
*/ |
| 13 |
class AssumeRoleCredentialProvider |
| 14 |
{ |
| 15 |
const ERROR_MSG = "Missing required 'AssumeRoleCredentialProvider' configuration option: "; |
| 16 |
/** @var StsClient */ |
| 17 |
private $client; |
| 18 |
/** @var array */ |
| 19 |
private $assumeRoleParams; |
| 20 |
/** |
| 21 |
* The constructor requires following configure parameters: |
| 22 |
* - client: a StsClient |
| 23 |
* - assume_role_params: Parameters used to make assumeRole call |
| 24 |
* |
| 25 |
* @param array $config Configuration options |
| 26 |
* @throws \InvalidArgumentException |
| 27 |
*/ |
| 28 |
public function __construct(array $config = []) |
| 29 |
{ |
| 30 |
if (!isset($config['assume_role_params'])) { |
| 31 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'assume_role_params'."); |
| 32 |
} |
| 33 |
if (!isset($config['client'])) { |
| 34 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'client'."); |
| 35 |
} |
| 36 |
$this->client = $config['client']; |
| 37 |
$this->assumeRoleParams = $config['assume_role_params']; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Loads assume role credentials. |
| 41 |
* |
| 42 |
* @return PromiseInterface |
| 43 |
*/ |
| 44 |
public function __invoke() |
| 45 |
{ |
| 46 |
$client = $this->client; |
| 47 |
return $client->assumeRoleAsync($this->assumeRoleParams)->then(function (Result $result) { |
| 48 |
return $this->client->createCredentials($result); |
| 49 |
})->otherwise(function (\RuntimeException $exception) { |
| 50 |
throw new CredentialsException("Error in retrieving assume role credentials.", 0, $exception); |
| 51 |
}); |
| 52 |
} |
| 53 |
} |
| 54 |
|