-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use internal static_ak/static_sts credentials provider
- Loading branch information
1 parent
3589f7b
commit cd6b32d
Showing
11 changed files
with
386 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package providers | ||
|
||
// 下一版本 Credentials 包 | ||
// - 分离 bearer token | ||
// - 从 config 传递迁移到真正的 credentials provider 模式 | ||
// - 删除 GetAccessKeyId()/GetAccessKeySecret()/GetSecurityToken() 方法,只保留 GetCredentials() | ||
|
||
// The credentials struct | ||
type Credentials struct { | ||
AccessKeyId string | ||
AccessKeySecret string | ||
SecurityToken string | ||
ProviderName string | ||
} | ||
|
||
// The credentials provider interface, return credentials and provider name | ||
type CredentialsProvider interface { | ||
// Get credentials | ||
GetCredentials() (*Credentials, error) | ||
// Get credentials provider name | ||
GetProviderName() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package providers | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
) | ||
|
||
type StaticAKCredentialsProvider struct { | ||
accessKeyId string | ||
accessKeySecret string | ||
} | ||
|
||
type StaticAKCredentialsProviderBuilder struct { | ||
provider *StaticAKCredentialsProvider | ||
} | ||
|
||
func NewStaticAKCredentialsProviderBuilder() *StaticAKCredentialsProviderBuilder { | ||
return &StaticAKCredentialsProviderBuilder{ | ||
provider: &StaticAKCredentialsProvider{}, | ||
} | ||
} | ||
|
||
func (builder *StaticAKCredentialsProviderBuilder) WithAccessKeyId(accessKeyId string) *StaticAKCredentialsProviderBuilder { | ||
builder.provider.accessKeyId = accessKeyId | ||
return builder | ||
} | ||
|
||
func (builder *StaticAKCredentialsProviderBuilder) WithAccessKeySecret(accessKeySecret string) *StaticAKCredentialsProviderBuilder { | ||
builder.provider.accessKeySecret = accessKeySecret | ||
return builder | ||
} | ||
|
||
func (builder *StaticAKCredentialsProviderBuilder) Build() (provider *StaticAKCredentialsProvider, err error) { | ||
if builder.provider.accessKeyId == "" { | ||
builder.provider.accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") | ||
} | ||
|
||
if builder.provider.accessKeyId == "" { | ||
err = errors.New("the access key id is empty") | ||
return | ||
} | ||
|
||
if builder.provider.accessKeySecret == "" { | ||
builder.provider.accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") | ||
} | ||
|
||
if builder.provider.accessKeySecret == "" { | ||
err = errors.New("the access key secret is empty") | ||
return | ||
} | ||
|
||
provider = builder.provider | ||
return | ||
} | ||
|
||
func (provider *StaticAKCredentialsProvider) GetCredentials() (cc *Credentials, err error) { | ||
cc = &Credentials{ | ||
AccessKeyId: provider.accessKeyId, | ||
AccessKeySecret: provider.accessKeySecret, | ||
ProviderName: provider.GetProviderName(), | ||
} | ||
return | ||
} | ||
|
||
func (provider *StaticAKCredentialsProvider) GetProviderName() string { | ||
return "static_ak" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package providers | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStaticAKCredentialsProvider(t *testing.T) { | ||
_, err := NewStaticAKCredentialsProviderBuilder(). | ||
Build() | ||
assert.EqualError(t, err, "the access key id is empty") | ||
|
||
_, err = NewStaticAKCredentialsProviderBuilder(). | ||
WithAccessKeyId("akid"). | ||
Build() | ||
assert.EqualError(t, err, "the access key secret is empty") | ||
|
||
provider, err := NewStaticAKCredentialsProviderBuilder(). | ||
WithAccessKeyId("accessKeyId"). | ||
WithAccessKeySecret("accessKeySecret"). | ||
Build() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "static_ak", provider.GetProviderName()) | ||
|
||
cred, err := provider.GetCredentials() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "accessKeyId", cred.AccessKeyId) | ||
assert.Equal(t, "accessKeySecret", cred.AccessKeySecret) | ||
assert.Equal(t, "", cred.SecurityToken) | ||
assert.Equal(t, "static_ak", cred.ProviderName) | ||
} | ||
|
||
func TestStaticAKCredentialsProviderWithEnv(t *testing.T) { | ||
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", "akid_from_env") | ||
os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "aksecret_from_env") | ||
provider, err := NewStaticAKCredentialsProviderBuilder(). | ||
Build() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "static_ak", provider.GetProviderName()) | ||
|
||
cred, err := provider.GetCredentials() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "akid_from_env", cred.AccessKeyId) | ||
assert.Equal(t, "aksecret_from_env", cred.AccessKeySecret) | ||
assert.Equal(t, "", cred.SecurityToken) | ||
assert.Equal(t, "static_ak", cred.ProviderName) | ||
} |
Oops, something went wrong.