Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support inner credentials client for RAM role arn #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Clients/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Client
/**
* @var CredentialsInterface|AccessKeyCredential|BearerTokenCredential|StsCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
*/
private $credential;
protected $credential;

/**
* @var SignatureInterface
Expand Down
13 changes: 13 additions & 0 deletions src/Clients/RamRoleArnClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ public function __construct($accessKeyId, $accessKeySecret, $roleArn, $roleSessi
new ShaHmac1Signature()
);
}

/**
* @param string $clientName
*
* @return $this
* @throws ClientException
*/
public function withCredentialClient($clientName)
{
$this->credential = $this->credential->withClient($clientName);

return $this;
}
}
15 changes: 10 additions & 5 deletions src/Credentials/Providers/RamRoleArnProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Credentials\Requests\AssumeRole;
use AlibabaCloud\Client\Filter\CredentialFilter;

/**
* Class RamRoleArnProvider
Expand Down Expand Up @@ -68,11 +69,15 @@ private function request($timeout, $connectTimeout)
{
$clientName = __CLASS__ . \uniqid('ak', true);
$credential = $this->client->getCredential();

AlibabaCloud::accessKeyClient(
$credential->getAccessKeyId(),
$credential->getAccessKeySecret()
)->name($clientName);
if (!is_null($credential->getClient())) {
$clientName = $credential->getClient();
} else {
CredentialFilter::AccessKey($credential->getAccessKeyId(), $credential->getAccessKeySecret());
AlibabaCloud::accessKeyClient(
$credential->getAccessKeyId(),
$credential->getAccessKeySecret()
)->name($clientName);
}

return (new AssumeRole($credential))
->client($clientName)
Expand Down
31 changes: 27 additions & 4 deletions src/Credentials/RamRoleArnCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AlibabaCloud\Client\Credentials;

use AlibabaCloud\Client\Filter\CredentialFilter;
use AlibabaCloud\Client\Exception\ClientException;

/**
Expand All @@ -13,6 +12,11 @@
class RamRoleArnCredential implements CredentialsInterface
{

/**
* @var string
*/
private $client;

/**
* @var string
*/
Expand Down Expand Up @@ -51,15 +55,26 @@ class RamRoleArnCredential implements CredentialsInterface
*/
public function __construct($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName, $policy = '')
{
CredentialFilter::AccessKey($accessKeyId, $accessKeySecret);

$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->roleArn = $roleArn;
$this->roleSessionName = $roleSessionName;
$this->policy = $policy;
}

/**
* @param string $clientName
*
* @return $this
* @throws ClientException
*/
public function withClient($clientName)
{
$this->client = $clientName;

return $this;
}

/**
* @return string
*/
Expand All @@ -76,6 +91,14 @@ public function getAccessKeySecret()
return $this->accessKeySecret;
}

/**
* @return string
*/
public function getClient()
{
return $this->client;
}

/**
* @return string
*/
Expand Down Expand Up @@ -105,6 +128,6 @@ public function getPolicy()
*/
public function __toString()
{
return "$this->accessKeyId#$this->accessKeySecret#$this->roleArn#$this->roleSessionName";
return "$this->accessKeyId#$this->accessKeySecret#$this->client#$this->roleArn#$this->roleSessionName";
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Credentials/Providers/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function key()
],
[
new RamRoleArnClient('foo', 'bar', 'arn', 'name'),
'foo#bar#arn#name',
'foo#bar##arn#name',
],
[
new RsaKeyPairClient('foo', VirtualRsaKeyPairCredential::ok()),
Expand Down
77 changes: 72 additions & 5 deletions tests/Unit/Credentials/Providers/RamRoleArnProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,78 @@ protected function finalize()
AlibabaCloud::cancelMock();
}

/**
* @throws ClientException
*/
public function testAccessKeyIdEmpty()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey ID cannot be empty');
// Setup
$client = new RamRoleArnClient(
'',
'access_key_secret',
'role_arn',
'role_session_name'
);
$provider = new RamRoleArnProvider($client);
$provider->get();
}

/**
* @throws ClientException
*/
public function testAccessKeyIdFormat()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey ID must be a string');
// Setup
$client = new RamRoleArnClient(
null,
'access_key_secret',
'role_arn',
'role_session_name'
);
$provider = new RamRoleArnProvider($client);
$provider->get();
}

/**
* @throws ClientException
*/
public function testAccessKeySecretEmpty()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey Secret cannot be empty');
// Setup
$client = new RamRoleArnClient(
'access_key_id',
'',
'role_arn',
'role_session_name'
);
$provider = new RamRoleArnProvider($client);
$provider->get();
}

/**
* @throws ClientException
*/
public function testAccessKeySecretFormat()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey Secret must be a string');
// Setup
$client = new RamRoleArnClient(
'access_key_id',
null,
'role_arn',
'role_session_name'
);
$provider = new RamRoleArnProvider($client);
$provider->get();
}

/**
* @throws ClientException
*/
Expand All @@ -54,7 +126,6 @@ public function testGet()
} catch (ServerException $e) {
self::assertEquals('InvalidAccessKeyId.NotFound', $e->getErrorCode());
}

}

/**
Expand Down Expand Up @@ -91,7 +162,6 @@ public function testGetInCache()

// Assert
self::assertInstanceOf(StsCredential::class, $actual);

}

/**
Expand All @@ -108,7 +178,6 @@ public function testNoCredentials()

$provider = new RamRoleArnProvider($client);
$provider->get();

}

/**
Expand Down Expand Up @@ -140,7 +209,5 @@ public function testOk()
$provider = new RamRoleArnProvider($client);
$credential = $provider->get();
self::assertInstanceOf(StsCredential::class, $credential);

}

}
69 changes: 1 addition & 68 deletions tests/Unit/Credentials/RamRoleArnCredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,76 +38,9 @@ public function testConstruct()
$this->assertEquals($sessionName, $credential->getRoleSessionName());
$this->assertEquals($policy, $credential->getPolicy());
$this->assertEquals(
"$accessKeyId#$accessKeySecret#$arn#$sessionName",
"$accessKeyId#$accessKeySecret##$arn#$sessionName",
(string)$credential
);
}

/**
* @throws ClientException
*/
public function testAccessKeyIdEmpty()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey ID cannot be empty');
// Setup
$accessKeyId = '';
$accessKeySecret = 'access_key_secret';
$arn = 'role_arn';
$sessionName = 'role_session_name';

// Test
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
}

/**
* @throws ClientException
*/
public function testAccessKeyIdFormat()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey ID must be a string');
// Setup
$accessKeyId = null;
$accessKeySecret = 'access_key_secret';
$arn = 'role_arn';
$sessionName = 'role_session_name';

// Test
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
}

/**
* @throws ClientException
*/
public function testAccessKeySecretEmpty()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey Secret cannot be empty');
// Setup
$accessKeyId = 'access_key_id';
$accessKeySecret = '';
$arn = 'role_arn';
$sessionName = 'role_session_name';

// Test
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
}

/**
* @throws ClientException
*/
public function testAccessKeySecretFormat()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('AccessKey Secret must be a string');
// Setup
$accessKeyId = 'access_key_id';
$accessKeySecret = null;
$arn = 'role_arn';
$sessionName = 'role_session_name';

// Test
new RamRoleArnCredential($accessKeyId, $accessKeySecret, $arn, $sessionName);
}
}
Loading