Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support sts type in profile provider #642

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sdk/auth/credentials/provider/profile_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ func (p *ProfileProvider) Resolve() (auth.Credential, error) {
data += scan.Text() + "\n"
}
return credentials.NewRsaKeyPairCredential(privateKey, value1.String(), 3600), nil
case "sts":
value1, err1 := section.GetKey("access_key_id")
value2, err2 := section.GetKey("access_key_secret")
value3, err3 := section.GetKey("security_token")
if err1 != nil || err2 != nil || err3 != nil {
return nil, errors.New("ERROR: Failed to get value")
}
if value1.String() == "" || value2.String() == "" || value3.String() == "" {
return nil, errors.New("ERROR: Value can't be empty")
}
return credentials.NewStsTokenCredential(value1.String(), value2.String(), value3.String()), nil
default:
return nil, errors.New("ERROR: Failed to get credential")
}
Expand Down
33 changes: 33 additions & 0 deletions sdk/auth/credentials/provider/profile_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ private_key_file = ./pk_error.pem
type = error_type
public_key_id = publicKeyId
private_key_file = ./pk_error.pem

[nosts]
type = sts
access_key_secret = bar
security_token = security_token

[emptysts]
type = sts
access_key_id =
access_key_secret = bar
security_token = security_token

[sts]
type = sts
access_key_id = foo
access_key_secret = bar
security_token = security_token
`
var privatekey = `this is privatekey`

Expand Down Expand Up @@ -239,4 +256,20 @@ func TestProfileProvider(t *testing.T) {
c, err = p.Resolve()
assert.Nil(t, c)
assert.True(t, strings.Contains(err.Error(), "ERROR: Failed to get credential"))

// testcase, normal StsToken
p = provider.NewProfileProvider("sts")
c, err = p.Resolve()
assert.Equal(t, credentials.NewStsTokenCredential("foo", "bar", "security_token"), c)
assert.Nil(t, err)
//testcase, key does not exist
p = provider.NewProfileProvider("nosts")
c, err = p.Resolve()
assert.Nil(t, c)
assert.True(t, strings.Contains(err.Error(), "ERROR: Failed to get value"))
//testcase, value is empty
p = provider.NewProfileProvider("emptysts")
c, err = p.Resolve()
assert.Nil(t, c)
assert.True(t, strings.Contains(err.Error(), "ERROR: Value can't be empty"))
}