Skip to content

Commit

Permalink
Add ProviderName
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 22, 2024
1 parent 13a5e3e commit e085793
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 36 deletions.
26 changes: 18 additions & 8 deletions sdk/auth/credentials/cli_profile_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *conf
WithRoleSessionName(p.RoleSessionName).
Build()
case "ChainableRamRoleArn":
previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile)
if err1 != nil {
err = fmt.Errorf("get source profile failed: %s", err1.Error())
var previousProvider CredentialsProvider
previousProvider, err = provider.getCredentialsProvider(conf, p.SourceProfile)
if err != nil {
err = fmt.Errorf("get source profile failed: %s", err.Error())
return
}
credentialsProvider, err = NewRAMRoleARNCredentialsProvider(previousProvider, p.RoleArn, p.RoleSessionName, p.DurationSeconds, "", p.StsRegion, "")
Expand All @@ -147,10 +148,9 @@ func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials
}

cfgPath := path.Join(homedir, ".aliyun/config.json")

conf, err1 := newConfigurationFromPath(cfgPath)
if err1 != nil {
err = err1
var conf *configuration
conf, err = newConfigurationFromPath(cfgPath)
if err != nil {
return
}

Expand All @@ -164,5 +164,15 @@ func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials
}
}

return provider.innerProvider.GetCredentials()
cc, err = provider.innerProvider.GetCredentials()
if err != nil {
return
}

cc.ProviderName = fmt.Sprintf("%s/%s", provider.GetProviderName(), provider.innerProvider.GetProviderName())
return
}

func (provider *CLIProfileCredentialsProvider) GetProviderName() string {
return "cli_provider"
}
17 changes: 15 additions & 2 deletions sdk/auth/credentials/cli_profile_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestCLIProfileCredentialsProvider_getCredentialsProvider(t *testing.T) {
assert.True(t, ok)
cc, err := akcp.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, cc, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: ""})
assert.Equal(t, cc, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "static_ak"})
// RamRoleArn
cp, err = provider.getCredentialsProvider(conf, "RamRoleArn")
assert.Nil(t, err)
Expand Down Expand Up @@ -184,9 +184,22 @@ func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
provider = NewCLIProfileCredentialsProviderBuilder().Build()
cc, err := provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", BearerToken: ""}, cc)
assert.Equal(t, &Credentials{
AccessKeyId: "akid",
AccessKeySecret: "secret",
SecurityToken: "",
BearerToken: "",
ProviderName: "cli_provider/static_ak",
}, cc)

provider = NewCLIProfileCredentialsProviderBuilder().WithProfileName("inexist").Build()
_, err = provider.GetCredentials()
assert.EqualError(t, err, "unable to get profile with 'inexist'")

// get credentials with RamRoleArn profile
// the previous credentials is invalid
provider = NewCLIProfileCredentialsProviderBuilder().WithProfileName("RamRoleArn").Build()
_, err = provider.GetCredentials()
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "InvalidAccessKeyId.NotFound")
}
39 changes: 38 additions & 1 deletion sdk/auth/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Credentials struct {
AccessKeySecret string
SecurityToken string
BearerToken string
ProviderName string
}

type do func(req *http.Request) (*http.Response, error)
Expand All @@ -80,6 +81,7 @@ var hookNewRequest = func(fn newReuqest) newReuqest {

type CredentialsProvider interface {
GetCredentials() (cc *Credentials, err error)
GetProviderName() string
}

type StaticAKCredentialsProvider struct {
Expand All @@ -98,10 +100,15 @@ func (provider *StaticAKCredentialsProvider) GetCredentials() (cc *Credentials,
cc = &Credentials{
AccessKeyId: provider.accessKeyId,
AccessKeySecret: provider.accessKeySecret,
ProviderName: provider.GetProviderName(),
}
return
}

func (provider *StaticAKCredentialsProvider) GetProviderName() string {
return "static_ak"
}

type StaticSTSCredentialsProvider struct {
accessKeyId string
accessKeySecret string
Expand All @@ -121,10 +128,15 @@ func (provider *StaticSTSCredentialsProvider) GetCredentials() (cc *Credentials,
AccessKeyId: provider.accessKeyId,
AccessKeySecret: provider.accessKeySecret,
SecurityToken: provider.securityToken,
ProviderName: provider.GetProviderName(),
}
return
}

func (provider *StaticSTSCredentialsProvider) GetProviderName() string {
return "static_sts"
}

type BearerTokenCredentialsProvider struct {
bearerToken string
}
Expand All @@ -137,11 +149,16 @@ func NewBearerTokenCredentialsProvider(bearerToken string) *BearerTokenCredentia

func (provider *BearerTokenCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
cc = &Credentials{
BearerToken: provider.bearerToken,
BearerToken: provider.bearerToken,
ProviderName: provider.GetProviderName(),
}
return
}

func (provider *BearerTokenCredentialsProvider) GetProviderName() string {
return "bearer_token"
}

// Deprecated: the RSA key pair credentials is deprecated
type RSAKeyPairCredentialsProvider struct {
PublicKeyId string
Expand Down Expand Up @@ -193,6 +210,7 @@ func (provider *RSAKeyPairCredentialsProvider) GetCredentials() (cc *Credentials
cc = &Credentials{
AccessKeyId: *provider.sessionAccessKey.SessionAccessKeyId,
AccessKeySecret: *provider.sessionAccessKey.SessionAccessKeySecret,
ProviderName: provider.GetProviderName(),
}
return
}
Expand Down Expand Up @@ -296,6 +314,10 @@ func (provider *RSAKeyPairCredentialsProvider) getCredentials() (sessionAK *sess
return
}

func (provider *RSAKeyPairCredentialsProvider) GetProviderName() string {
return "rsa_key_pair"
}

type RAMRoleARNCredentialsProvider struct {
credentialsProvider CredentialsProvider
roleArn string
Expand Down Expand Up @@ -482,10 +504,15 @@ func (provider *RAMRoleARNCredentialsProvider) GetCredentials() (cc *Credentials
AccessKeyId: provider.sessionCredentials.AccessKeyId,
AccessKeySecret: provider.sessionCredentials.AccessKeySecret,
SecurityToken: provider.sessionCredentials.SecurityToken,
ProviderName: fmt.Sprintf("%s/%s", provider.GetProviderName(), provider.credentialsProvider.GetProviderName()),
}
return
}

func (provider *RAMRoleARNCredentialsProvider) GetProviderName() string {
return "ram_role_arn"
}

type ECSRAMRoleCredentialsProvider struct {
roleName string
sessionCredentials *SessionCredentials
Expand Down Expand Up @@ -619,10 +646,15 @@ func (provider *ECSRAMRoleCredentialsProvider) GetCredentials() (cc *Credentials
AccessKeyId: provider.sessionCredentials.AccessKeyId,
AccessKeySecret: provider.sessionCredentials.AccessKeySecret,
SecurityToken: provider.sessionCredentials.SecurityToken,
ProviderName: provider.GetProviderName(),
}
return
}

func (provider *ECSRAMRoleCredentialsProvider) GetProviderName() string {
return "ecs_ram_role"
}

type OIDCCredentialsProvider struct {
oidcProviderARN string
oidcTokenFilePath string
Expand Down Expand Up @@ -853,6 +885,11 @@ func (provider *OIDCCredentialsProvider) GetCredentials() (cc *Credentials, err
AccessKeyId: provider.sessionCredentials.AccessKeyId,
AccessKeySecret: provider.sessionCredentials.AccessKeySecret,
SecurityToken: provider.sessionCredentials.SecurityToken,
ProviderName: provider.GetProviderName(),
}
return
}

func (provider *OIDCCredentialsProvider) GetProviderName() string {
return "oidc_role_arn"
}
3 changes: 3 additions & 0 deletions sdk/auth/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ func (p *errorCredentialsProvider) GetCredentials() (cc *Credentials, err error)
return
}

func (p *errorCredentialsProvider) GetProviderName() string {
return "error_mock"
}
func TestRAMRoleARNCredentialsProviderGetCredentials(t *testing.T) {
originDo := hookDo
defer func() { hookDo = originDo }()
Expand Down
13 changes: 12 additions & 1 deletion sdk/auth/credentials/default_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,35 @@ func NewDefaultCredentialsProvider() (provider *DefaultCredentialsProvider) {

func (provider *DefaultCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
if provider.lastUsedProvider != nil {
return provider.lastUsedProvider.GetCredentials()
cc, err = provider.lastUsedProvider.GetCredentials()
if err != nil {
return
}
cc.ProviderName = fmt.Sprintf("%s/%s", provider.GetProviderName(), provider.lastUsedProvider.GetProviderName())
return
}

errors := []string{}
for _, p := range provider.providerChain {
provider.lastUsedProvider = p
cc, err = p.GetCredentials()

if err != nil {
errors = append(errors, err.Error())
// 如果有错误,进入下一个获取过程
continue
}

if cc != nil {
cc.ProviderName = fmt.Sprintf("%s/%s", provider.GetProviderName(), p.GetProviderName())
return
}
}

err = fmt.Errorf("unable to get credentials from any of the providers in the chain: %s", strings.Join(errors, ", "))
return
}

func (provider *DefaultCredentialsProvider) GetProviderName() string {
return "default"
}
16 changes: 14 additions & 2 deletions sdk/auth/credentials/default_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,21 @@ func TestDefaultCredentialsProvider_GetCredentials(t *testing.T) {
assert.Len(t, provider.providerChain, 3)
cc, err := provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", BearerToken: ""}, cc)
assert.Equal(t, &Credentials{
AccessKeyId: "akid",
AccessKeySecret: "aksecret",
SecurityToken: "",
BearerToken: "",
ProviderName: "default/env",
}, cc)
// get again
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", BearerToken: ""}, cc)
assert.Equal(t, &Credentials{
AccessKeyId: "akid",
AccessKeySecret: "aksecret",
SecurityToken: "",
BearerToken: "",
ProviderName: "default/env",
}, cc)
}
21 changes: 10 additions & 11 deletions sdk/auth/credentials/env_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ func (provider *EnvironmentVariableCredentialsProvider) GetCredentials() (cc *Cr

securityToken := os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN")

if securityToken == "" {
cc = &Credentials{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
}
} else {
cc = &Credentials{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
SecurityToken: securityToken,
}
cc = &Credentials{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
SecurityToken: securityToken,
ProviderName: provider.GetProviderName(),
}

return
}

func (provider *EnvironmentVariableCredentialsProvider) GetProviderName() string {
return "env"
}
2 changes: 2 additions & 0 deletions sdk/auth/credentials/env_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ func TestEnvironmentVariableCredentialsProvider(t *testing.T) {
assert.Equal(t, "aksecret", cc.AccessKeySecret)
assert.Equal(t, "token", cc.SecurityToken)
assert.Equal(t, "", cc.BearerToken)

assert.Equal(t, "env", cc.ProviderName)
}
34 changes: 26 additions & 8 deletions sdk/auth/credentials/profile_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ func (provider *ProfileCredentialsProvider) getCredentialsProvider(ini *ini.File
return
}

func (provider *ProfileCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
if provider.innerProvider != nil {
return provider.innerProvider.GetCredentials()
}

func (provider *ProfileCredentialsProvider) getIni() (iniInfo *ini.File, err error) {
sharedCfgPath := os.Getenv("ALIBABA_CLOUD_CREDENTIALS_FILE")
if sharedCfgPath == "" {
homeDir := getHomePath()
Expand All @@ -113,16 +109,38 @@ func (provider *ProfileCredentialsProvider) GetCredentials() (cc *Credentials, e
sharedCfgPath = path.Join(homeDir, ".alibabacloud/credentials")
}

ini, err := ini.Load(sharedCfgPath)
iniInfo, err = ini.Load(sharedCfgPath)
if err != nil {
err = errors.New("ERROR: Can not open file" + err.Error())
return
}

provider.innerProvider, err = provider.getCredentialsProvider(ini)
return
}

func (provider *ProfileCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
if provider.innerProvider == nil {
var iniInfo *ini.File
iniInfo, err = provider.getIni()
if err != nil {
return
}

provider.innerProvider, err = provider.getCredentialsProvider(iniInfo)
if err != nil {
return
}
}

cc, err = provider.innerProvider.GetCredentials()
if err != nil {
return
}

return provider.innerProvider.GetCredentials()
cc.ProviderName = fmt.Sprintf("%s/%s", provider.GetProviderName(), provider.innerProvider.GetProviderName())
return
}

func (provider ProfileCredentialsProvider) GetProviderName() string {
return "profile"
}
Loading

0 comments on commit e085793

Please sign in to comment.