Skip to content

Commit 644f337

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

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

src/Clients/Client.php

+1-1
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

+13
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

+10-5
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

+27-4
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

+1-1
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/RamRoleArnCredentialTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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
}

0 commit comments

Comments
 (0)