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