Skip to content

Commit

Permalink
feat: add disableIMDSv1 setter
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and JacksonTian committed Sep 12, 2024
1 parent f2abf11 commit e541552
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func main(){
SetType("ecs_ram_role").
// `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests
SetRoleName("RoleName").
// `EnableIMDSv2` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_ECS_IMDSV2_ENABLE
SetEnableIMDSv2(true)
// `DisableIMDSv1` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_IMDSV1_DISABLE
SetDisableIMDSv1(true)

provider, err := credentials.NewCredential(config)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ func main(){
SetType("ecs_ram_role").
// `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests
SetRoleName("RoleName").
// `EnableIMDSv2` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_ECS_IMDSV2_ENABLE
SetEnableIMDSv2(true)
// `DisableIMDSv1` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_IMDSV1_DISABLE
SetDisableIMDSv1(true)

provider, err := credentials.NewCredential(config)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions credentials/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (s *Config) SetEnableIMDSv2(v bool) *Config {
return s
}

func (s *Config) SetDisableIMDSv1(v bool) *Config {
s.DisableIMDSv1 = &v
return s
}

func (s *Config) SetMetadataTokenDuration(v int) *Config {
s.MetadataTokenDuration = &v
return s
Expand Down
10 changes: 10 additions & 0 deletions credentials/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ func TestNewCredentialWithECSRAMRole(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, cred)

config.SetDisableIMDSv1(false)
cred, err = NewCredential(config)
assert.Nil(t, err)
assert.NotNil(t, cred)

config.SetEnableIMDSv2(true)
cred, err = NewCredential(config)
assert.Nil(t, err)
assert.NotNil(t, cred)

config.SetDisableIMDSv1(true)
cred, err = NewCredential(config)
assert.Nil(t, err)
assert.NotNil(t, cred)

config.SetEnableIMDSv2(true)
config.SetMetadataTokenDuration(180)
cred, err = NewCredential(config)
Expand Down

0 comments on commit e541552

Please sign in to comment.