Skip to content

Commit

Permalink
enable default provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 16, 2024
1 parent a87c82d commit 413499f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
5 changes: 3 additions & 2 deletions integration/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 21 additions & 3 deletions integration/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions sdk/auth/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down
7 changes: 7 additions & 0 deletions sdk/auth/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 413499f

Please sign in to comment.