/
configure.go
223 lines (195 loc) · 5.74 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
// Copyright 1999-2019 Alibaba Group Holding Limited
//
// 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 (
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/i18n"
)
var hookLoadConfiguration = func(fn func(path string, w io.Writer) (Configuration, error)) func(path string, w io.Writer) (Configuration, error) {
return fn
}
var hookSaveConfiguration = func(fn func(config Configuration) error) func(config Configuration) error {
return fn
}
func NewConfigureCommand() *cli.Command {
c := &cli.Command{
Name: "configure",
Short: i18n.T(
"configure credential and settings",
"配置身份认证和其他信息"),
Usage: "configure --mode <AuthenticateMode> --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 := hookLoadConfiguration(LoadConfiguration)(GetConfigPath()+"/"+configFile, ctx.Writer())
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 RsaKeyPair:
cp.Mode = RsaKeyPair
configureRsaKeyPair(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)
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, "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)
cp.ExpiredSeconds = 900
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 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 ReadInput(defaultValue string) string {
var s string
fmt.Scanf("%s\n", &s)
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))
}
}