Skip to content

Commit

Permalink
improve URICredentialsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 29, 2024
1 parent 626bc3a commit c6e412f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
6 changes: 3 additions & 3 deletions credentials/uri_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ func (e *URLCredentialsProvider) updateCredential() (err error) {
request.Method = "GET"
content, err := doAction(request, e.runtime)
if err != nil {
return fmt.Errorf("refresh Ecs sts token err: %s", err.Error())
return fmt.Errorf("get credentials from %s failed with error: %s", e.URL, err.Error())
}
var resp *URLResponse
err = json.Unmarshal(content, &resp)
if err != nil {
return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error())
return fmt.Errorf("get credentials from %s failed with error, json unmarshal fail: %s", e.URL, err.Error())
}
if resp.AccessKeyId == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" {
return fmt.Errorf("refresh Ecs sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyId, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration)
return fmt.Errorf("get credentials failed: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyId, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration)
}

expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration)
Expand Down
80 changes: 74 additions & 6 deletions credentials/uri_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,88 @@ import (
"github.com/stretchr/testify/assert"
)

func TestURLCredential_updateCredential(t *testing.T) {
func TestURLCredentialsProvider_updateCredential(t *testing.T) {
provider := newURLCredential("http://127.0.0.1")

origTestHookDo := hookDo
defer func() { hookDo = origTestHookDo }()
hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
return mockResponse(300, ``, errors.New("sdk test"))
}
}
_, err := provider.GetAccessKeyId()

cred, err := provider.GetCredential()
assert.NotNil(t, err)
assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error())
assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())
assert.Nil(t, cred)

assert.Equal(t, "credential_uri", *provider.GetType())
_, err = provider.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())

cred, err := provider.GetCredential()
assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error())
_, err = provider.GetAccessKeySecret()
assert.NotNil(t, err)
assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())

_, err = provider.GetSecurityToken()
assert.NotNil(t, err)
assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
return mockResponse(200, `invalid json`, nil)
}
}

cred, err = provider.GetCredential()
assert.NotNil(t, err)
assert.Equal(t, "get credentials from http://127.0.0.1 failed with error, json unmarshal fail: invalid character 'i' looking for beginning of value", err.Error())
assert.Nil(t, cred)

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
return mockResponse(200, `{}`, nil)
}
}

cred, err = provider.GetCredential()
assert.NotNil(t, err)
assert.Equal(t, "get credentials failed: AccessKeyId: , AccessKeySecret: , SecurityToken: , Expiration: ", err.Error())
assert.Nil(t, cred)

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
return mockResponse(200, `{"AccessKeyId":"akid", "AccessKeySecret":"aksecret","SecurityToken":"sts","Expiration":"2006-01-02T15:04:05Z"}`, nil)
}
}

cred, err = provider.GetCredential()
assert.Nil(t, err)
assert.NotNil(t, cred)
assert.Equal(t, "akid", *cred.AccessKeyId)
assert.Equal(t, "aksecret", *cred.AccessKeySecret)
assert.Equal(t, "sts", *cred.SecurityToken)

akid, err := provider.GetAccessKeyId()
assert.Nil(t, err)
assert.Equal(t, "akid", *akid)

aksecret, err := provider.GetAccessKeySecret()
assert.Nil(t, err)
assert.Equal(t, "aksecret", *aksecret)

sts, err := provider.GetSecurityToken()
assert.Nil(t, err)
assert.Equal(t, "sts", *sts)
}

func TestURLCredentialsProviderGetBearerToken(t *testing.T) {
provider := newURLCredential("http://127.0.0.1")
assert.Equal(t, "", *provider.GetBearerToken())
}

func TestURLCredentialsProviderGetType(t *testing.T) {
provider := newURLCredential("http://127.0.0.1")
assert.Equal(t, "credential_uri", *provider.GetType())
}

0 comments on commit c6e412f

Please sign in to comment.