1 |
efrain |
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 |
* 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 |
|
|
|
17 |
/** @var StsClient */
|
|
|
18 |
private $client;
|
|
|
19 |
|
|
|
20 |
/** @var array */
|
|
|
21 |
private $assumeRoleParams;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* The constructor requires following configure parameters:
|
|
|
25 |
* - client: a StsClient
|
|
|
26 |
* - assume_role_params: Parameters used to make assumeRole call
|
|
|
27 |
*
|
|
|
28 |
* @param array $config Configuration options
|
|
|
29 |
* @throws \InvalidArgumentException
|
|
|
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 |
* Loads assume role credentials.
|
|
|
47 |
*
|
|
|
48 |
* @return PromiseInterface
|
|
|
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($result);
|
|
|
56 |
})->otherwise(function (\RuntimeException $exception) {
|
|
|
57 |
throw new CredentialsException(
|
|
|
58 |
"Error in retrieving assume role credentials.",
|
|
|
59 |
0,
|
|
|
60 |
$exception
|
|
|
61 |
);
|
|
|
62 |
});
|
|
|
63 |
}
|
|
|
64 |
}
|