1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Credentials\Providers; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\SDK; |
6
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
7
|
|
|
use AlibabaCloud\Client\Result\Result; |
8
|
|
|
use AlibabaCloud\Client\Request\Request; |
9
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
10
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
11
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
12
|
|
|
use AlibabaCloud\Client\Credentials\Requests\AssumeRole; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class RamRoleArnProvider |
16
|
|
|
* |
17
|
|
|
* @package AlibabaCloud\Client\Credentials\Providers |
18
|
|
|
*/ |
19
|
|
|
class RamRoleArnProvider extends Provider |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get credential. |
24
|
|
|
* |
25
|
|
|
* |
26
|
|
|
* @param int $timeout |
27
|
|
|
* @param int $connectTimeout |
28
|
|
|
* |
29
|
|
|
* @return StsCredential |
30
|
|
|
* @throws ClientException |
31
|
|
|
* @throws ServerException |
32
|
|
|
*/ |
33
|
7 |
|
public function get($timeout = Request::TIMEOUT, $connectTimeout = Request::CONNECT_TIMEOUT) |
34
|
|
|
{ |
35
|
7 |
|
$credential = $this->getCredentialsInCache(); |
36
|
|
|
|
37
|
7 |
|
if (null === $credential) { |
38
|
6 |
|
$result = $this->request($timeout, $connectTimeout); |
39
|
|
|
|
40
|
2 |
|
if (!isset($result['Credentials']['AccessKeyId'], |
41
|
2 |
|
$result['Credentials']['AccessKeySecret'], |
42
|
2 |
|
$result['Credentials']['SecurityToken'])) { |
43
|
1 |
|
throw new ServerException($result, $this->error, SDK::INVALID_CREDENTIAL); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$credential = $result['Credentials']; |
47
|
1 |
|
$this->cache($credential); |
|
|
|
|
48
|
1 |
|
} |
49
|
|
|
|
50
|
2 |
|
return new StsCredential( |
51
|
2 |
|
$credential['AccessKeyId'], |
52
|
2 |
|
$credential['AccessKeySecret'], |
53
|
2 |
|
$credential['SecurityToken'] |
54
|
2 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get credentials by request. |
59
|
|
|
* |
60
|
|
|
* @param $timeout |
61
|
|
|
* @param $connectTimeout |
62
|
|
|
* |
63
|
|
|
* @return Result |
64
|
|
|
* @throws ClientException |
65
|
|
|
* @throws ServerException |
66
|
|
|
*/ |
67
|
6 |
|
private function request($timeout, $connectTimeout) |
68
|
|
|
{ |
69
|
6 |
|
$clientName = __CLASS__ . \uniqid('ak', true); |
70
|
6 |
|
$credential = $this->client->getCredential(); |
71
|
|
|
|
72
|
6 |
|
AlibabaCloud::accessKeyClient( |
73
|
6 |
|
$credential->getAccessKeyId(), |
|
|
|
|
74
|
6 |
|
$credential->getAccessKeySecret() |
|
|
|
|
75
|
6 |
|
)->name($clientName); |
76
|
|
|
|
77
|
6 |
|
return (new AssumeRole($credential)) |
|
|
|
|
78
|
6 |
|
->client($clientName) |
79
|
6 |
|
->timeout($timeout) |
80
|
6 |
|
->connectTimeout($connectTimeout) |
81
|
6 |
|
->debug($this->client->isDebug()) |
82
|
6 |
|
->request(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|