Skip to content

feat: support profileFile in cli_profile provider #132

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

Merged
merged 1 commit into from
Mar 10, 2025
Merged
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
4 changes: 2 additions & 2 deletions credentials/internal/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDoGet(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "text/html; charset=utf-8", res.Headers["Content-Type"])
assert.Equal(t, "text/html;charset=UTF-8", res.Headers["Content-Type"])

req = &Request{
Method: "GET",
Expand All @@ -61,7 +61,7 @@ func TestDoGet(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "text/html; charset=utf-8", res.Headers["Content-Type"])
assert.Equal(t, "text/html;charset=UTF-8", res.Headers["Content-Type"])
}

func TestDoPost(t *testing.T) {
Expand Down
15 changes: 14 additions & 1 deletion credentials/providers/cli_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type CLIProfileCredentialsProvider struct {
profileFile string
profileName string
innerProvider CredentialsProvider
}
Expand All @@ -21,12 +22,24 @@ type CLIProfileCredentialsProviderBuilder struct {
provider *CLIProfileCredentialsProvider
}

func (b *CLIProfileCredentialsProviderBuilder) WithProfileFile(profileFile string) *CLIProfileCredentialsProviderBuilder {
b.provider.profileFile = profileFile
return b
}

func (b *CLIProfileCredentialsProviderBuilder) WithProfileName(profileName string) *CLIProfileCredentialsProviderBuilder {
b.provider.profileName = profileName
return b
}

func (b *CLIProfileCredentialsProviderBuilder) Build() (provider *CLIProfileCredentialsProvider, err error) {
// 优先级:
// 1. 使用显示指定的 profileFile
// 2. 使用环境变量(ALIBABA_CLOUD_CONFIG_FILE)指定的 profileFile
// 3. 兜底使用 path.Join(homeDir, ".aliyun/config") 作为 profileFile
if b.provider.profileFile == "" {
b.provider.profileFile = os.Getenv("ALIBABA_CLOUD_CONFIG_FILE")
}
// 优先级:
// 1. 使用显示指定的 profileName
// 2. 使用环境变量(ALIBABA_CLOUD_PROFILE)制定的 profileName
Expand Down Expand Up @@ -181,7 +194,7 @@ var getHomePath = utils.GetHomePath

func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
if provider.innerProvider == nil {
cfgPath := os.Getenv("ALIBABA_CLOUD_CONFIG_FILE")
cfgPath := provider.profileFile
if cfgPath == "" {
homeDir := getHomePath()
if homeDir == "" {
Expand Down
11 changes: 11 additions & 0 deletions credentials/providers/cli_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,23 @@ func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
_, err = provider.GetCredentials()
assert.EqualError(t, err, "reading aliyun cli config from '/path/invalid/home/dir/.aliyun/config.json' failed open /path/invalid/home/dir/.aliyun/config.json: no such file or directory")

// testcase: specify credentials file
provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileFile("/path/to/config.invalid").Build()
assert.Nil(t, err)
_, err = provider.GetCredentials()
assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config.invalid' failed open /path/to/config.invalid: no such file or directory")

// testcase: specify credentials file with env
os.Setenv("ALIBABA_CLOUD_CONFIG_FILE", "/path/to/config.invalid")
provider, err = NewCLIProfileCredentialsProviderBuilder().Build()
assert.Nil(t, err)
_, err = provider.GetCredentials()
assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config.invalid' failed open /path/to/config.invalid: no such file or directory")

provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileFile("/path/to/config1.invalid").Build()
assert.Nil(t, err)
_, err = provider.GetCredentials()
assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config1.invalid' failed open /path/to/config1.invalid: no such file or directory")
os.Unsetenv("ALIBABA_CLOUD_CONFIG_FILE")

getHomePath = func() string {
Expand Down
Loading