1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Credentials\Ini; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\SDK; |
6
|
|
|
use AlibabaCloud\Client\Clients\Client; |
7
|
|
|
use AlibabaCloud\Client\Clients\AccessKeyClient; |
8
|
|
|
use AlibabaCloud\Client\Clients\RamRoleArnClient; |
9
|
|
|
use AlibabaCloud\Client\Clients\RsaKeyPairClient; |
10
|
|
|
use AlibabaCloud\Client\Clients\EcsRamRoleClient; |
11
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
12
|
|
|
use AlibabaCloud\Client\Clients\BearerTokenClient; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait CreateTrait |
16
|
|
|
* |
17
|
|
|
* @package AlibabaCloud\Client\Credentials\Ini |
18
|
|
|
* |
19
|
|
|
* @mixin IniCredential |
20
|
|
|
*/ |
21
|
|
|
trait CreateTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param string $clientName |
25
|
|
|
* @param array $credential |
26
|
|
|
* |
27
|
|
|
* @return Client|bool |
28
|
|
|
* @throws ClientException |
29
|
|
|
*/ |
30
|
26 |
|
protected function createClient($clientName, array $credential) |
31
|
|
|
{ |
32
|
26 |
|
if (!isset($credential['enable']) || !$credential['enable']) { |
33
|
2 |
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
24 |
|
if (!isset($credential['type'])) { |
37
|
2 |
|
$this->missingRequired('type', $clientName); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
22 |
|
return $this->createClientByType($clientName, $credential)->name($clientName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $clientName |
45
|
|
|
* @param array $credential |
46
|
|
|
* |
47
|
|
|
* @return AccessKeyClient|BearerTokenClient|EcsRamRoleClient|RamRoleArnClient|RsaKeyPairClient |
48
|
|
|
* @throws ClientException |
49
|
|
|
*/ |
50
|
28 |
|
private function createClientByType($clientName, array $credential) |
51
|
|
|
{ |
52
|
28 |
|
switch (\strtolower($credential['type'])) { |
53
|
28 |
|
case 'access_key': |
54
|
9 |
|
return $this->accessKeyClient($clientName, $credential); |
55
|
19 |
|
case 'ecs_ram_role': |
56
|
3 |
|
return $this->ecsRamRoleClient($clientName, $credential); |
57
|
16 |
|
case 'ram_role_arn': |
58
|
6 |
|
return $this->ramRoleArnClient($clientName, $credential); |
59
|
10 |
|
case 'bearer_token': |
60
|
3 |
|
return $this->bearerTokenClient($clientName, $credential); |
61
|
7 |
|
case 'rsa_key_pair': |
62
|
4 |
|
return $this->rsaKeyPairClient($clientName, $credential); |
63
|
3 |
|
default: |
64
|
3 |
|
throw new ClientException( |
65
|
3 |
|
"Invalid type '{$credential['type']}' for '$clientName' in {$this->filename}", |
66
|
|
|
SDK::INVALID_CREDENTIAL |
67
|
3 |
|
); |
68
|
3 |
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $credential |
73
|
|
|
* @param string $clientName |
74
|
|
|
* |
75
|
|
|
* @return AccessKeyClient |
76
|
|
|
* @throws ClientException |
77
|
|
|
*/ |
78
|
12 |
|
private function accessKeyClient($clientName, array $credential) |
79
|
|
|
{ |
80
|
12 |
|
if (!isset($credential['access_key_id'])) { |
81
|
3 |
|
$this->missingRequired('access_key_id', $clientName); |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
if (!isset($credential['access_key_secret'])) { |
85
|
2 |
|
$this->missingRequired('access_key_secret', $clientName); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
return new AccessKeyClient( |
89
|
7 |
|
$credential['access_key_id'], |
90
|
7 |
|
$credential['access_key_secret'] |
91
|
7 |
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $clientName |
96
|
|
|
* @param array $credential |
97
|
|
|
* |
98
|
|
|
* @return EcsRamRoleClient |
99
|
|
|
* @throws ClientException |
100
|
|
|
*/ |
101
|
5 |
|
private function ecsRamRoleClient($clientName, array $credential) |
102
|
|
|
{ |
103
|
5 |
|
if (!isset($credential['role_name'])) { |
104
|
3 |
|
$this->missingRequired('role_name', $clientName); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return new EcsRamRoleClient($credential['role_name']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $clientName |
112
|
|
|
* @param array $credential |
113
|
|
|
* |
114
|
|
|
* @return RamRoleArnClient |
115
|
|
|
* @throws ClientException |
116
|
|
|
*/ |
117
|
11 |
|
private function ramRoleArnClient($clientName, array $credential) |
118
|
|
|
{ |
119
|
11 |
|
if (!isset($credential['access_key_id'])) { |
120
|
3 |
|
$this->missingRequired('access_key_id', $clientName); |
121
|
|
|
} |
122
|
|
|
|
123
|
8 |
|
if (!isset($credential['access_key_secret'])) { |
124
|
2 |
|
$this->missingRequired('access_key_secret', $clientName); |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
if (!isset($credential['role_arn'])) { |
128
|
2 |
|
$this->missingRequired('role_arn', $clientName); |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
if (!isset($credential['role_session_name'])) { |
132
|
2 |
|
$this->missingRequired('role_session_name', $clientName); |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
return new RamRoleArnClient( |
136
|
2 |
|
$credential['access_key_id'], |
137
|
2 |
|
$credential['access_key_secret'], |
138
|
2 |
|
$credential['role_arn'], |
139
|
2 |
|
$credential['role_session_name'] |
140
|
2 |
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $clientName |
145
|
|
|
* @param array $credential |
146
|
|
|
* |
147
|
|
|
* @return BearerTokenClient |
148
|
|
|
* @throws ClientException |
149
|
|
|
*/ |
150
|
5 |
|
private function bearerTokenClient($clientName, array $credential) |
151
|
|
|
{ |
152
|
5 |
|
if (!isset($credential['bearer_token'])) { |
153
|
3 |
|
$this->missingRequired('bearer_token', $clientName); |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
return new BearerTokenClient($credential['bearer_token']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array $credential |
161
|
|
|
* @param string $clientName |
162
|
|
|
* |
163
|
|
|
* @return RsaKeyPairClient |
164
|
|
|
* @throws ClientException |
165
|
|
|
*/ |
166
|
7 |
|
private function rsaKeyPairClient($clientName, array $credential) |
167
|
|
|
{ |
168
|
7 |
|
if (!isset($credential['public_key_id'])) { |
169
|
3 |
|
$this->missingRequired('public_key_id', $clientName); |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
if (!isset($credential['private_key_file'])) { |
173
|
2 |
|
$this->missingRequired('private_key_file', $clientName); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
return new RsaKeyPairClient( |
177
|
2 |
|
$credential['public_key_id'], |
178
|
2 |
|
$credential['private_key_file'] |
179
|
2 |
|
); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|