Skip to content

Commit 08bfab3

Browse files
committed
feat: support inner credentials client for RAM role arn
1 parent 11db11a commit 08bfab3

File tree

7 files changed

+125
-84
lines changed

7 files changed

+125
-84
lines changed

src/Clients/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Client
3232
/**
3333
* @var CredentialsInterface|AccessKeyCredential|BearerTokenCredential|StsCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
3434
*/
35-
private $credential;
35+
protected $credential;
3636

3737
/**
3838
* @var SignatureInterface

src/Clients/RamRoleArnClient.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ public function __construct($accessKeyId, $accessKeySecret, $roleArn, $roleSessi
3030
new ShaHmac1Signature()
3131
);
3232
}
33+
34+
/**
35+
* @param string $clientName
36+
*
37+
* @return $this
38+
* @throws ClientException
39+
*/
40+
public function withCredentialClient($clientName)
41+
{
42+
$this->credential = $this->credential->withClient($clientName);
43+
44+
return $this;
45+
}
3346
}

src/Credentials/Providers/RamRoleArnProvider.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use AlibabaCloud\Client\Exception\ClientException;
1111
use AlibabaCloud\Client\Exception\ServerException;
1212
use AlibabaCloud\Client\Credentials\Requests\AssumeRole;
13+
use AlibabaCloud\Client\Filter\CredentialFilter;
1314

1415
/**
1516
* Class RamRoleArnProvider
@@ -68,11 +69,15 @@ private function request($timeout, $connectTimeout)
6869
{
6970
$clientName = __CLASS__ . \uniqid('ak', true);
7071
$credential = $this->client->getCredential();
71-
72-
AlibabaCloud::accessKeyClient(
73-
$credential->getAccessKeyId(),
74-
$credential->getAccessKeySecret()
75-
)->name($clientName);
72+
if (!is_null($credential->getClient())) {
73+
$clientName = $credential->getClient();
74+
} else {
75+
CredentialFilter::AccessKey($credential->getAccessKeyId(), $credential->getAccessKeySecret());
76+
AlibabaCloud::accessKeyClient(
77+
$credential->getAccessKeyId(),
78+
$credential->getAccessKeySecret()
79+
)->name($clientName);
80+
}
7681

7782
return (new AssumeRole($credential))
7883
->client($clientName)

src/Credentials/RamRoleArnCredential.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace AlibabaCloud\Client\Credentials;
44

5-
use AlibabaCloud\Client\Filter\CredentialFilter;
65
use AlibabaCloud\Client\Exception\ClientException;
76

87
/**
@@ -13,6 +12,11 @@
1312
class RamRoleArnCredential implements CredentialsInterface
1413
{
1514

15+
/**
16+
* @var string
17+
*/
18+
private $client;
19+
1620
/**
1721
* @var string
1822
*/
@@ -51,15 +55,26 @@ class RamRoleArnCredential implements CredentialsInterface
5155
*/
5256
public function __construct($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName, $policy = '')
5357
{
54-
CredentialFilter::AccessKey($accessKeyId, $accessKeySecret);
55-
5658
$this->accessKeyId = $accessKeyId;
5759
$this->accessKeySecret = $accessKeySecret;
5860
$this->roleArn = $roleArn;
5961
$this->roleSessionName = $roleSessionName;
6062
$this->policy = $policy;
6163
}
6264

65+
/**
66+
* @param string $clientName
67+
*
68+
* @return $this
69+
* @throws ClientException
70+
*/
71+
public function withClient($clientName)
72+
{
73+
$this->client = $clientName;
74+
75+
return $this;
76+
}
77+
6378
/**
6479
* @return string
6580
*/
@@ -76,6 +91,14 @@ public function getAccessKeySecret()
7691
return $this->accessKeySecret;
7792
}
7893

94+
/**
95+
* @return string
96+
*/
97+
public function getClient()
98+
{
99+
return $this->client;
100+
}
101+
79102
/**
80103
* @return string
81104
*/
@@ -105,6 +128,6 @@ public function getPolicy()
105128
*/
106129
public function __toString()
107130
{
108-
return "$this->accessKeyId#$this->accessKeySecret#$this->roleArn#$this->roleSessionName";
131+
return "$this->accessKeyId#$this->accessKeySecret#$this->client#$this->roleArn#$this->roleSessionName";
109132
}
110133
}

tests/Unit/Credentials/Providers/ProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function key()
7878
],
7979
[
8080
new RamRoleArnClient('foo', 'bar', 'arn', 'name'),
81-
'foo#bar#arn#name',
81+
'foo#bar##arn#name',
8282
],
8383
[
8484
new RsaKeyPairClient('foo', VirtualRsaKeyPairCredential::ok()),

tests/Unit/Credentials/Providers/RamRoleArnProviderTest.php

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,78 @@ protected function finalize()
3131
AlibabaCloud::cancelMock();
3232
}
3333

34+
/**
35+
* @throws ClientException
36+
*/
37+
public function testAccessKeyIdEmpty()
38+
{
39+
$this->expectException(ClientException::class);
40+
$this->expectExceptionMessage('AccessKey ID cannot be empty');
41+
// Setup
42+
$client = new RamRoleArnClient(
43+
'',
44+
'access_key_secret',
45+
'role_arn',
46+
'role_session_name'
47+
);
48+
$provider = new RamRoleArnProvider($client);
49+
$provider->get();
50+
}
51+
52+
/**
53+
* @throws ClientException
54+
*/
55+
public function testAccessKeyIdFormat()
56+
{
57+
$this->expectException(ClientException::class);
58+
$this->expectExceptionMessage('AccessKey ID must be a string');
59+
// Setup
60+
$client = new RamRoleArnClient(
61+
null,
62+
'access_key_secret',
63+
'role_arn',
64+
'role_session_name'
65+
);
66+
$provider = new RamRoleArnProvider($client);
67+
$provider->get();
68+
}
69+
70+
/**
71+
* @throws ClientException
72+
*/
73+
public function testAccessKeySecretEmpty()
74+
{
75+
$this->expectException(ClientException::class);
76+
$this->expectExceptionMessage('AccessKey Secret cannot be empty');
77+
// Setup
78+
$client = new RamRoleArnClient(
79+
'access_key_id',
80+
'',
81+
'role_arn',
82+
'role_session_name'
83+
);
84+
$provider = new RamRoleArnProvider($client);
85+
$provider->get();
86+
}
87+
88+
/**
89+
* @throws ClientException
90+
*/
91+
public function testAccessKeySecretFormat()
92+
{
93+
$this->expectException(ClientException::class);
94+
$this->expectExceptionMessage('AccessKey Secret must be a string');
95+
// Setup
96+
$client = new RamRoleArnClient(
97+
'access_key_id',
98+
null,
99+
'role_arn',
100+
'role_session_name'
101+
);
102+
$provider = new RamRoleArnProvider($client);
103+
$provider->get();
104+
}
105+
34106
/**
35107
* @throws ClientException
36108
*/
@@ -54,7 +126,6 @@ public function testGet()
54126
} catch (ServerException $e) {
55127
self::assertEquals('InvalidAccessKeyId.NotFound', $e->getErrorCode());
56128
}
57-
58129
}
59130

60131
/**
@@ -91,7 +162,6 @@ public function testGetInCache()
91162

92163
// Assert
93164
self::assertInstanceOf(StsCredential::class, $actual);
94-
95165
}
96166

97167
/**
@@ -108,7 +178,6 @@ public function testNoCredentials()
108178

109179
$provider = new RamRoleArnProvider($client);
110180
$provider->get();
111-
112181
}
113182

114183
/**
@@ -140,7 +209,5 @@ public function testOk()
140209
$provider = new RamRoleArnProvider($client);
141210
$credential = $provider->get();
142211
self::assertInstanceOf(StsCredential::class, $credential);
143-
144212
}
145-
146213
}

tests/Unit/Credentials/RamRoleArnCredentialTest.php

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,76 +38,9 @@ public function testConstruct()
3838
$this->assertEquals($sessionName, $credential->getRoleSessionName());
3939
$this->assertEquals($policy, $credential->getPolicy());
4040
$this->assertEquals(
41-
"$accessKeyId#$accessKeySecret#$arn#$sessionName",
41+
"$accessKeyId#$accessKeySecret##$arn#$sessionName",
4242
(string)$credential
4343
);
4444
}
4545

46-
/**
47-
* @throws ClientException
48-
*/
49-
public function testAccessKeyIdEmpty()
50-
{
51-
$this->expectException(ClientException::class);
52-
$this->expectExceptionMessage('AccessKey ID cannot be empty');
53-
// Setup
54-
$accessKeyId = '';
55-
$accessKeySecret = 'access_key_secret';
56-
$arn = 'role_arn';
57-
$sessionName = 'role_session_name';
58-
59-
// Test
60-
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
61-
}
62-
63-
/**
64-
* @throws ClientException
65-
*/
66-
public function testAccessKeyIdFormat()
67-
{
68-
$this->expectException(ClientException::class);
69-
$this->expectExceptionMessage('AccessKey ID must be a string');
70-
// Setup
71-
$accessKeyId = null;
72-
$accessKeySecret = 'access_key_secret';
73-
$arn = 'role_arn';
74-
$sessionName = 'role_session_name';
75-
76-
// Test
77-
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
78-
}
79-
80-
/**
81-
* @throws ClientException
82-
*/
83-
public function testAccessKeySecretEmpty()
84-
{
85-
$this->expectException(ClientException::class);
86-
$this->expectExceptionMessage('AccessKey Secret cannot be empty');
87-
// Setup
88-
$accessKeyId = 'access_key_id';
89-
$accessKeySecret = '';
90-
$arn = 'role_arn';
91-
$sessionName = 'role_session_name';
92-
93-
// Test
94-
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
95-
}
96-
97-
/**
98-
* @throws ClientException
99-
*/
100-
public function testAccessKeySecretFormat()
101-
{
102-
$this->expectException(ClientException::class);
103-
$this->expectExceptionMessage('AccessKey Secret must be a string');
104-
// Setup
105-
$accessKeyId = 'access_key_id';
106-
$accessKeySecret = null;
107-
$arn = 'role_arn';
108-
$sessionName = 'role_session_name';
109-
110-
// Test
111-
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
112-
}
11346
}

0 commit comments

Comments
 (0)