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