From 413499f73c053667ffd57b37afbd7afb14b0e8de Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Fri, 16 Aug 2024 13:46:48 +0800 Subject: [PATCH] enable default provider --- integration/core_test.go | 5 +++-- integration/credential_test.go | 24 +++++++++++++++++++++--- sdk/auth/credential.go | 5 +++++ sdk/auth/credential_test.go | 7 +++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/integration/core_test.go b/integration/core_test.go index adda15b6b1..cf7cbabc79 100644 --- a/integration/core_test.go +++ b/integration/core_test.go @@ -309,14 +309,15 @@ func SkipTest_HTTPProxy(t *testing.T) { assert.Equal(t, "test", resp.GetHttpContentString()) originEnv := os.Getenv("HTTP_PROXY") + defer func() { + os.Setenv("HTTP_PROXY", originEnv) + }() domain = strings.Replace(ts.URL, "http://", "", 1) os.Setenv("HTTP_PROXY", fmt.Sprintf("http://someuser:somepassword@%s", domain)) resp, err = client.ProcessCommonRequest(request) assert.Nil(t, err) assert.Equal(t, 200, resp.GetHttpStatus()) assert.Equal(t, "sdktest", resp.GetHttpContentString()) - - os.Setenv("HTTP_PROXY", originEnv) } func Test_DdoscooWithServiceCode(t *testing.T) { diff --git a/integration/credential_test.go b/integration/credential_test.go index 6c617f87d5..92a86ccdd5 100644 --- a/integration/credential_test.go +++ b/integration/credential_test.go @@ -9,7 +9,6 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" - "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/provider" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" "github.com/aliyun/alibaba-cloud-sdk-go/services/sts" @@ -63,8 +62,15 @@ func Test_DescribeRegionsWithRPCrequestWithArn(t *testing.T) { } func TestDescribeRegionsWithProviderAndAk(t *testing.T) { - os.Setenv(provider.ENVAccessKeyID, os.Getenv("ACCESS_KEY_ID")) - os.Setenv(provider.ENVAccessKeySecret, os.Getenv("ACCESS_KEY_SECRET")) + originAKID := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") + originAKSecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") + defer func() { + os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", originAKID) + os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", originAKSecret) + }() + + os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", os.Getenv("ACCESS_KEY_ID")) + os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", os.Getenv("ACCESS_KEY_SECRET")) request := requests.NewCommonRequest() request.Version = "2014-05-26" request.Product = "Ecs" @@ -156,3 +162,15 @@ func TestGetCallerIdentityWithOIDCCredentialsProvider(t *testing.T) { assert.NotZero(t, response.RoleId) assert.NotZero(t, response.AccountId) } + +func TestGetCallerIdentityWithDefaultCredentialsProvider(t *testing.T) { + config := sdk.NewConfig().WithScheme("HTTPS") + client, err := sts.NewClientWithOptions("cn-hangzhou", config, nil) + assert.Nil(t, err) + request := sts.CreateGetCallerIdentityRequest() + response, err := client.GetCallerIdentity(request) + assert.Nil(t, err) + assert.True(t, response.IsSuccess()) + assert.NotZero(t, response.RoleId) + assert.NotZero(t, response.AccountId) +} diff --git a/sdk/auth/credential.go b/sdk/auth/credential.go index 6023a7f4f7..b2ab1d6380 100644 --- a/sdk/auth/credential.go +++ b/sdk/auth/credential.go @@ -26,6 +26,11 @@ type Credential interface { } func ToCredentialsProvider(credential Credential) (provider credentials.CredentialsProvider, err error) { + if credential == nil { + provider = credentials.NewDefaultCredentialsProvider() + return + } + switch instance := credential.(type) { case *credentials.AccessKeyCredential: { diff --git a/sdk/auth/credential_test.go b/sdk/auth/credential_test.go index 0269b1c452..5c349381d9 100644 --- a/sdk/auth/credential_test.go +++ b/sdk/auth/credential_test.go @@ -57,6 +57,13 @@ func TestToCredentialsProvider(t *testing.T) { _, ok = p.(*credentials.OIDCCredentialsProvider) assert.True(t, ok) + // Default credentials provider + p, err = ToCredentialsProvider(nil) + assert.Nil(t, err) + _, ok = p.(*credentials.DefaultCredentialsProvider) + assert.True(t, ok) + + // unsupported _, err = ToCredentialsProvider("string") assert.NotNil(t, err) assert.Equal(t, "[SDK.UnsupportedCredential] Specified credential (type = string) is not supported, please check", err.Error())