-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathconfigure.go
300 lines (266 loc) · 8.35 KB
/
configure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/i18n"
)
var hookLoadConfiguration = func(fn func(path string) (*Configuration, error)) func(path string) (*Configuration, error) {
return fn
}
var hookSaveConfiguration = func(fn func(config *Configuration) error) func(config *Configuration) error {
return fn
}
func loadConfiguration() (*Configuration, error) {
return hookLoadConfiguration(LoadConfiguration)(GetConfigPath() + "/" + configFile)
}
func NewConfigureCommand() *cli.Command {
c := &cli.Command{
Name: "configure",
Short: i18n.T(
"configure credential and settings",
"配置身份认证和其他信息"),
Usage: "configure --mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName|ChainableRamRoleArn} --profile <profileName>",
Run: func(ctx *cli.Context, args []string) error {
if len(args) > 0 {
return cli.NewInvalidCommandError(args[0], ctx)
}
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
mode, _ := ModeFlag(ctx.Flags()).GetValue()
return doConfigure(ctx, profileName, mode)
},
}
c.AddSubCommand(NewConfigureGetCommand())
c.AddSubCommand(NewConfigureSetCommand())
c.AddSubCommand(NewConfigureListCommand())
c.AddSubCommand(NewConfigureDeleteCommand())
return c
}
func doConfigure(ctx *cli.Context, profileName string, mode string) error {
w := ctx.Writer()
conf, err := loadConfiguration()
if err != nil {
return err
}
if profileName == "" {
if conf.CurrentProfile == "" {
profileName = "default"
} else {
profileName = conf.CurrentProfile
originMode := string(conf.GetCurrentProfile(ctx).Mode)
if mode == "" {
mode = originMode
} else if mode != originMode {
cli.Printf(w, "Warning: You are changing the authentication type of profile '%s' from '%s' to '%s'\n", profileName, originMode, mode)
}
}
}
if mode == "" {
mode = "AK"
}
cp, ok := conf.GetProfile(profileName)
if !ok {
cp = conf.NewProfile(profileName)
}
cli.Printf(w, "Configuring profile '%s' in '%s' authenticate mode...\n", profileName, mode)
if mode != "" {
switch AuthenticateMode(mode) {
case AK:
cp.Mode = AK
configureAK(w, &cp)
case StsToken:
cp.Mode = StsToken
configureStsToken(w, &cp)
case RamRoleArn:
cp.Mode = RamRoleArn
configureRamRoleArn(w, &cp)
case EcsRamRole:
cp.Mode = EcsRamRole
configureEcsRamRole(w, &cp)
case RamRoleArnWithEcs:
cp.Mode = RamRoleArnWithEcs
configureRamRoleArnWithEcs(w, &cp)
case ChainableRamRoleArn:
cp.Mode = ChainableRamRoleArn
configureChainableRamRoleArn(w, &cp)
case RsaKeyPair:
cp.Mode = RsaKeyPair
configureRsaKeyPair(w, &cp)
case External:
cp.Mode = External
configureExternal(w, &cp)
case CredentialsURI:
cp.Mode = CredentialsURI
configureCredentialsURI(w, &cp)
default:
return fmt.Errorf("unexcepted authenticate mode: %s", mode)
}
} else {
configureAK(w, &cp)
}
//
// configure common
cli.Printf(w, "Default Region Id [%s]: ", cp.RegionId)
cp.RegionId = ReadInput(cp.RegionId)
cli.Printf(w, "Default Output Format [%s]: json (Only support json)\n", cp.OutputFormat)
// cp.OutputFormat = ReadInput(cp.OutputFormat)
cp.OutputFormat = "json"
cli.Printf(w, "Default Language [zh|en] %s: ", cp.Language)
cp.Language = ReadInput(cp.Language)
if cp.Language != "zh" && cp.Language != "en" {
cp.Language = "en"
}
//fmt.Printf("User site: [china|international|japan] %s", cp.Site)
//cp.Site = ReadInput(cp.Site)
cli.Printf(w, "Saving profile[%s] ...", profileName)
conf.PutProfile(cp)
conf.CurrentProfile = cp.Name
err = hookSaveConfiguration(SaveConfiguration)(conf)
// cp 要在下文的 DoHello 中使用,所以 需要建立 parent 的关系
cp.parent = conf
if err != nil {
return err
}
cli.Printf(w, "Done.\n")
DoHello(ctx, &cp)
return nil
}
func configureAK(w io.Writer, cp *Profile) error {
cli.Printf(w, "Access Key Id [%s]: ", MosaicString(cp.AccessKeyId, 3))
cp.AccessKeyId = ReadInput(cp.AccessKeyId)
cli.Printf(w, "Access Key Secret [%s]: ", MosaicString(cp.AccessKeySecret, 3))
cp.AccessKeySecret = ReadInput(cp.AccessKeySecret)
return nil
}
func configureStsToken(w io.Writer, cp *Profile) error {
err := configureAK(w, cp)
if err != nil {
return err
}
cli.Printf(w, "Sts Token [%s]: ", cp.StsToken)
cp.StsToken = ReadInput(cp.StsToken)
return nil
}
func configureRamRoleArn(w io.Writer, cp *Profile) error {
err := configureAK(w, cp)
if err != nil {
return err
}
cli.Printf(w, "Sts Region [%s]: ", cp.StsRegion)
cp.StsRegion = ReadInput(cp.StsRegion)
cli.Printf(w, "Ram Role Arn [%s]: ", cp.RamRoleArn)
cp.RamRoleArn = ReadInput(cp.RamRoleArn)
cli.Printf(w, "Role Session Name [%s]: ", cp.RoleSessionName)
cp.RoleSessionName = ReadInput(cp.RoleSessionName)
if cp.ExpiredSeconds == 0 {
cp.ExpiredSeconds = 900
}
cli.Printf(w, "Expired Seconds [%v]: ", cp.ExpiredSeconds)
cp.ExpiredSeconds, _ = strconv.Atoi(ReadInput(strconv.Itoa(cp.ExpiredSeconds)))
return nil
}
func configureEcsRamRole(w io.Writer, cp *Profile) error {
cli.Printf(w, "Ecs Ram Role [%s]: ", cp.RamRoleName)
cp.RamRoleName = ReadInput(cp.RamRoleName)
return nil
}
func configureRamRoleArnWithEcs(w io.Writer, cp *Profile) error {
cli.Printf(w, "Ecs Ram Role [%s]: ", cp.RamRoleName)
cp.RamRoleName = ReadInput(cp.RamRoleName)
cli.Printf(w, "Sts Region [%s]: ", cp.StsRegion)
cp.StsRegion = ReadInput(cp.StsRegion)
cli.Printf(w, "Ram Role Arn [%s]: ", cp.RamRoleArn)
cp.RamRoleArn = ReadInput(cp.RamRoleArn)
cli.Printf(w, "Role Session Name [%s]: ", cp.RoleSessionName)
cp.RoleSessionName = ReadInput(cp.RoleSessionName)
if cp.ExpiredSeconds == 0 {
cp.ExpiredSeconds = 900
}
cli.Printf(w, "Expired Seconds [%v]: ", cp.ExpiredSeconds)
cp.ExpiredSeconds, _ = strconv.Atoi(ReadInput(strconv.Itoa(cp.ExpiredSeconds)))
return nil
}
func configureChainableRamRoleArn(w io.Writer, cp *Profile) error {
cli.Printf(w, "Source Profile [%s]: ", cp.SourceProfile)
cp.SourceProfile = ReadInput(cp.SourceProfile)
cli.Printf(w, "Sts Region [%s]: ", cp.StsRegion)
cp.StsRegion = ReadInput(cp.StsRegion)
cli.Printf(w, "Ram Role Arn [%s]: ", cp.RamRoleArn)
cp.RamRoleArn = ReadInput(cp.RamRoleArn)
cli.Printf(w, "Role Session Name [%s]: ", cp.RoleSessionName)
cp.RoleSessionName = ReadInput(cp.RoleSessionName)
if cp.ExpiredSeconds == 0 {
cp.ExpiredSeconds = 900
}
cli.Printf(w, "Expired Seconds [%v]: ", cp.ExpiredSeconds)
cp.ExpiredSeconds, _ = strconv.Atoi(ReadInput(strconv.Itoa(cp.ExpiredSeconds)))
return nil
}
func configureRsaKeyPair(w io.Writer, cp *Profile) error {
cli.Printf(w, "Rsa Private Key File: ")
keyFile := ReadInput("")
buf, err := ioutil.ReadFile(keyFile)
if err != nil {
return fmt.Errorf("read key file %s failed %v", keyFile, err)
}
cp.PrivateKey = string(buf)
cli.Printf(w, "Rsa Key Pair Name: ")
cp.KeyPairName = ReadInput("")
cp.ExpiredSeconds = 900
return nil
}
func configureExternal(w io.Writer, cp *Profile) error {
cli.Printf(w, "Process Command [%s]: ", cp.ProcessCommand)
cp.ProcessCommand = ReadInput(cp.ProcessCommand)
return nil
}
func configureCredentialsURI(w io.Writer, cp *Profile) error {
cli.Printf(w, "Credentials URI [%s]: ", cp.CredentialsURI)
cp.CredentialsURI = ReadInput(cp.CredentialsURI)
return nil
}
func ReadInput(defaultValue string) string {
var s string
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
s = scanner.Text()
}
if s == "" {
return defaultValue
}
return s
}
func MosaicString(s string, lastChars int) string {
r := len(s) - lastChars
if r > 0 {
return strings.Repeat("*", r) + s[r:]
} else {
return strings.Repeat("*", len(s))
}
}
func GetLastChars(s string, lastChars int) string {
r := len(s) - lastChars
if r > 0 {
return s[r:]
} else {
return strings.Repeat("*", len(s))
}
}