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 5bba668
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions sdk/auth/credentials/cli_profile_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@ func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials

return provider.innerProvider.GetCredentials()
}

func (provider *CLIProfileCredentialsProvider) GetProviderName() string {
return "cli_provider"
}
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
4 changes: 4 additions & 0 deletions sdk/auth/credentials/default_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ func (provider *DefaultCredentialsProvider) GetCredentials() (cc *Credentials, e
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"
}
4 changes: 4 additions & 0 deletions sdk/auth/credentials/env_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func (provider *EnvironmentVariableCredentialsProvider) GetCredentials() (cc *Cr
}
return
}

func (provider *EnvironmentVariableCredentialsProvider) GetProviderName() string {
return "env"
}
4 changes: 4 additions & 0 deletions sdk/auth/credentials/profile_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ func (provider *ProfileCredentialsProvider) GetCredentials() (cc *Credentials, e

return provider.innerProvider.GetCredentials()
}

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

0 comments on commit 5bba668

Please sign in to comment.