1
|
|
|
package providers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"io/ioutil" |
7
|
|
|
"os" |
8
|
|
|
"path" |
9
|
|
|
|
10
|
|
|
"github.com/aliyun/credentials-go/credentials/internal/utils" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
type CLIProfileCredentialsProvider struct { |
14
|
|
|
profileName string |
15
|
|
|
innerProvider CredentialsProvider |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
type CLIProfileCredentialsProviderBuilder struct { |
19
|
|
|
provider *CLIProfileCredentialsProvider |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
func (b *CLIProfileCredentialsProviderBuilder) WithProfileName(profileName string) *CLIProfileCredentialsProviderBuilder { |
23
|
|
|
b.provider.profileName = profileName |
24
|
|
|
return b |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
func (b *CLIProfileCredentialsProviderBuilder) Build() *CLIProfileCredentialsProvider { |
28
|
|
|
// 优先级: |
29
|
|
|
// 1. 使用显示指定的 profileName |
30
|
|
|
// 2. 使用环境变量(ALIBABA_CLOUD_PROFILE)制定的 profileName |
31
|
|
|
// 3. 使用 CLI 配置中的当前 profileName |
32
|
|
|
if b.provider.profileName == "" { |
33
|
|
|
b.provider.profileName = os.Getenv("ALIBABA_CLOUD_PROFILE") |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return b.provider |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
func NewCLIProfileCredentialsProviderBuilder() *CLIProfileCredentialsProviderBuilder { |
40
|
|
|
return &CLIProfileCredentialsProviderBuilder{ |
41
|
|
|
provider: &CLIProfileCredentialsProvider{}, |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
type profile struct { |
46
|
|
|
Name string `json:"name"` |
47
|
|
|
Mode string `json:"mode"` |
48
|
|
|
AccessKeyID string `json:"access_key_id"` |
49
|
|
|
AccessKeySecret string `json:"access_key_secret"` |
50
|
|
|
RegionID string `json:"region_id"` |
51
|
|
|
RoleArn string `json:"ram_role_arn"` |
52
|
|
|
RoleSessionName string `json:"ram_session_name"` |
53
|
|
|
DurationSeconds int `json:"expired_seconds"` |
54
|
|
|
StsRegion string `json:"sts_region"` |
55
|
|
|
SourceProfile string `json:"source_profile"` |
56
|
|
|
RoleName string `json:"ram_role_name"` |
57
|
|
|
OIDCTokenFile string `json:"oidc_token_file"` |
58
|
|
|
OIDCProviderARN string `json:"oidc_provider_arn"` |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
type configuration struct { |
62
|
|
|
Current string `json:"current"` |
63
|
|
|
Profiles []*profile `json:"profiles"` |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
func newConfigurationFromPath(cfgPath string) (conf *configuration, err error) { |
67
|
|
|
bytes, err := ioutil.ReadFile(cfgPath) |
68
|
|
|
if err != nil { |
69
|
|
|
err = fmt.Errorf("reading aliyun cli config from '%s' failed %v", cfgPath, err) |
70
|
|
|
return |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
conf = &configuration{} |
74
|
|
|
|
75
|
|
|
err = json.Unmarshal(bytes, conf) |
76
|
|
|
if err != nil { |
77
|
|
|
err = fmt.Errorf("unmarshal aliyun cli config from '%s' failed: %s", cfgPath, string(bytes)) |
78
|
|
|
return |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if conf.Profiles == nil || len(conf.Profiles) == 0 { |
82
|
|
|
err = fmt.Errorf("no any configured profiles in '%s'", cfgPath) |
83
|
|
|
return |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
func (conf *configuration) getProfile(name string) (profile *profile, err error) { |
90
|
|
|
for _, p := range conf.Profiles { |
91
|
|
|
if p.Name == name { |
92
|
|
|
profile = p |
93
|
|
|
return |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
err = fmt.Errorf("unable to get profile with '%s'", name) |
98
|
|
|
return |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *configuration, profileName string) (credentialsProvider CredentialsProvider, err error) { |
102
|
|
|
p, err := conf.getProfile(profileName) |
103
|
|
|
if err != nil { |
104
|
|
|
return |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
switch p.Mode { |
108
|
|
|
case "AK": |
109
|
|
|
credentialsProvider, err = NewStaticAKCredentialsProviderBuilder(). |
110
|
|
|
WithAccessKeyId(p.AccessKeyID). |
111
|
|
|
WithAccessKeySecret(p.AccessKeySecret). |
112
|
|
|
Build() |
113
|
|
|
case "RamRoleArn": |
114
|
|
|
previousProvider, err1 := NewStaticAKCredentialsProviderBuilder(). |
115
|
|
|
WithAccessKeyId(p.AccessKeyID). |
116
|
|
|
WithAccessKeySecret(p.AccessKeySecret). |
117
|
|
|
Build() |
118
|
|
|
if err1 != nil { |
119
|
|
|
return nil, err1 |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
123
|
|
|
WithCredentialsProvider(previousProvider). |
124
|
|
|
WithRoleArn(p.RoleArn). |
125
|
|
|
WithRoleSessionName(p.RoleSessionName). |
126
|
|
|
WithDurationSeconds(p.DurationSeconds). |
127
|
|
|
WithStsRegionId(p.StsRegion). |
128
|
|
|
Build() |
129
|
|
|
case "EcsRamRole": |
130
|
|
|
credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(p.RoleName).Build() |
131
|
|
|
case "OIDC": |
132
|
|
|
credentialsProvider, err = NewOIDCCredentialsProviderBuilder(). |
133
|
|
|
WithOIDCTokenFilePath(p.OIDCTokenFile). |
134
|
|
|
WithOIDCProviderARN(p.OIDCProviderARN). |
135
|
|
|
WithRoleArn(p.RoleArn). |
136
|
|
|
WithStsRegionId(p.StsRegion). |
137
|
|
|
WithDurationSeconds(p.DurationSeconds). |
138
|
|
|
WithRoleSessionName(p.RoleSessionName). |
139
|
|
|
Build() |
140
|
|
|
case "ChainableRamRoleArn": |
141
|
|
|
previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile) |
142
|
|
|
if err1 != nil { |
143
|
|
|
err = fmt.Errorf("get source profile failed: %s", err1.Error()) |
144
|
|
|
return |
145
|
|
|
} |
146
|
|
|
credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
147
|
|
|
WithCredentialsProvider(previousProvider). |
148
|
|
|
WithRoleArn(p.RoleArn). |
149
|
|
|
WithRoleSessionName(p.RoleSessionName). |
150
|
|
|
WithDurationSeconds(p.DurationSeconds). |
151
|
|
|
WithStsRegionId(p.StsRegion). |
152
|
|
|
Build() |
153
|
|
|
default: |
154
|
|
|
err = fmt.Errorf("unsupported profile mode '%s'", p.Mode) |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// 默认设置为 GetHomePath,测试时便于 mock |
161
|
|
|
var getHomePath = utils.GetHomePath |
162
|
|
|
|
163
|
|
|
func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials, err error) { |
164
|
|
|
if provider.innerProvider == nil { |
165
|
|
|
homedir := getHomePath() |
166
|
|
|
if homedir == "" { |
167
|
|
|
err = fmt.Errorf("cannot found home dir") |
168
|
|
|
return |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
cfgPath := path.Join(homedir, ".aliyun/config.json") |
172
|
|
|
|
173
|
|
|
conf, err1 := newConfigurationFromPath(cfgPath) |
174
|
|
|
if err1 != nil { |
175
|
|
|
err = err1 |
176
|
|
|
return |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if provider.profileName == "" { |
180
|
|
|
provider.profileName = conf.Current |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
provider.innerProvider, err = provider.getCredentialsProvider(conf, provider.profileName) |
184
|
|
|
if err != nil { |
185
|
|
|
return |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
innerCC, err := provider.innerProvider.GetCredentials() |
190
|
|
|
if err != nil { |
191
|
|
|
return |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
cc = &Credentials{ |
195
|
|
|
AccessKeyId: innerCC.AccessKeyId, |
196
|
|
|
AccessKeySecret: innerCC.AccessKeySecret, |
197
|
|
|
SecurityToken: innerCC.SecurityToken, |
198
|
|
|
ProviderName: fmt.Sprintf("%s/%s", provider.GetProviderName(), provider.innerProvider.GetProviderName()), |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
func (provider *CLIProfileCredentialsProvider) GetProviderName() string { |
205
|
|
|
return "cli_profile" |
206
|
|
|
} |
207
|
|
|
|