Skip to content

Commit

Permalink
feat: add isEnableIMDSv2 && update IMDSv2 doc for ecs ram role
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed May 24, 2024
1 parent 22dc2e4 commit e345fbe
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
fail-fast: false
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
Expand Down
11 changes: 7 additions & 4 deletions README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ $ramRoleArn->getPolicy();

use AlibabaCloud\Credentials\Credential;

$ecsRamRole = new Credential([
'type' => 'ecs_ram_role',
'role_name' => '<role_name>',
$config = new Credential\Config([
'type' => 'ecs_ram_role',
'roleName' => '<role_name>',
'enableIMDSv2' => true,
]);
$ecsRamRole = new Credential($config);
$ecsRamRole->getRoleName();
// Note: `role_name` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
// Note: `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
// Note: `enableIMDSv2` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_ECS_IMDSV2_ENABLE
```

#### RsaKeyPair
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ By specifying the role name, the credential will be able to automatically reques

use AlibabaCloud\Credentials\Credential;

$ecsRamRole = new Credential([
'type' => 'ecs_ram_role',
'role_name' => '<role_name>',
$config = new Credential\Config([
'type' => 'ecs_ram_role',
'roleName' => '<role_name>',
'enableIMDSv2' => true,
]);
$ecsRamRole = new Credential($config);
$ecsRamRole->getRoleName();
// Note: `role_name` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
// Note: `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
// Note: `enableIMDSv2` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_ECS_IMDSV2_ENABLE
```

#### RsaKeyPair
Expand Down
16 changes: 13 additions & 3 deletions src/EcsRamRoleCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class EcsRamRoleCredential implements CredentialsInterface
*
* @param $role_name
*/
public function __construct($role_name = null, $enable_IMDS_v2 = false, $metadata_token_duration = 21600 )
public function __construct($role_name = null, $enable_imdsv2 = false, $metadata_token_duration = 21600 )
{
Filter::roleName($role_name);

$this->roleName = $role_name;

Filter::enableIMDSv2($enable_IMDS_v2);
Filter::enableIMDSv2($enable_imdsv2);

$this->enableIMDSv2 = $enable_IMDS_v2;
$this->enableIMDSv2 = $enable_imdsv2;

Filter::metadataTokenDuration($metadata_token_duration);

Expand Down Expand Up @@ -102,6 +102,16 @@ public function getRoleNameFromMeta()
return $role_name;
}

/**
* @return bool
* @throws GuzzleException
* @throws Exception
*/
public function isEnableIMDSv2()
{
return $this->enableIMDSv2;
}

/**
* @return string
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public static function roleName($role_name)
/**
* @param boolean|null $enable_IMDS_v2
*/
public static function enableIMDSv2($enable_IMDS_v2)
public static function enableIMDSv2($enable_imds_v2)
{
if (!is_bool($enable_IMDS_v2)) {
throw new InvalidArgumentException('enable_IMDS_v2 must be a string');
if (!is_bool($enable_imds_v2)) {
throw new InvalidArgumentException('enable_imds_v2 must be a string');
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Feature/CredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public function testAccessKey()
public function testEcsRamRoleCredential()
{
$config = new Credential\Config([
'type' => 'ecs_ram_role',
'roleName' => 'foo',
'type' => 'ecs_ram_role',
'roleName' => 'foo',
'enableIMDSv2' => true,
]);
$credential = new Credential($config);

// Assert
$this->assertEquals('foo', $credential->getRoleName());
$this->assertEquals('ecs_ram_role', $credential->getType());
$this->assertTrue($credential->isEnableIMDSv2());
$credential->getAccessKeySecret();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function exceptionCases()
'role_name' => 'test',
'enableIMDSv2' => 'false',
],
'enable_IMDS_v2 must be a string',
'enable_imds_v2 must be a string',
],

[
Expand Down

0 comments on commit e345fbe

Please sign in to comment.