| 1 |
<?php |
| 2 |
namespace Aws\Credentials; |
| 3 |
|
| 4 |
use Aws\Exception\CredentialsException; |
| 5 |
use Aws\Result; |
| 6 |
use Aws\Sts\StsClient; |
| 7 |
use GuzzleHttp\Promise\PromiseInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class AssumeRoleCredentialProvider |
| 14 |
{ |
| 15 |
const ERROR_MSG = "Missing required 'AssumeRoleCredentialProvider' configuration option: "; |
| 16 |
|
| 17 |
|
| 18 |
private $client; |
| 19 |
|
| 20 |
|
| 21 |
private $assumeRoleParams; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function __construct(array $config = []) |
| 32 |
{ |
| 33 |
if (!isset($config['assume_role_params'])) { |
| 34 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'assume_role_params'."); |
| 35 |
} |
| 36 |
|
| 37 |
if (!isset($config['client'])) { |
| 38 |
throw new \InvalidArgumentException(self::ERROR_MSG . "'client'."); |
| 39 |
} |
| 40 |
|
| 41 |
$this->client = $config['client']; |
| 42 |
$this->assumeRoleParams = $config['assume_role_params']; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __invoke() |
| 51 |
{ |
| 52 |
$client = $this->client; |
| 53 |
return $client->assumeRoleAsync($this->assumeRoleParams) |
| 54 |
->then(function (Result $result) { |
| 55 |
return $this->client->createCredentials( |
| 56 |
$result, |
| 57 |
CredentialSources::STS_ASSUME_ROLE |
| 58 |
); |
| 59 |
})->otherwise(function (\RuntimeException $exception) { |
| 60 |
throw new CredentialsException( |
| 61 |
"Error in retrieving assume role credentials.", |
| 62 |
0, |
| 63 |
$exception |
| 64 |
); |
| 65 |
}); |
| 66 |
} |
| 67 |
} |