/
login.go
340 lines (301 loc) 路 8.74 KB
/
login.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package commands
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/clilogger"
"github.com/scaleway/scaleway-cli/pkg/config"
"github.com/scaleway/scaleway-cli/pkg/scwversion"
)
// LoginArgs are arguments passed to `RunLogin`
type LoginArgs struct {
Organization string
Token string
SSHKey string
SkipSSHKey bool
}
// selectKey allows to choice a key in ~/.ssh
func selectKey(args *LoginArgs) error {
home, err := config.GetHomeDir()
if err != nil {
return err
}
dir := filepath.Join(home, ".ssh")
files, err := ioutil.ReadDir(dir)
if err != nil {
return fmt.Errorf("Unable to open your ~/.ssh: %v", err)
}
var pubs []string
for i := range files {
if filepath.Ext(files[i].Name()) == ".pub" {
pubs = append(pubs, files[i].Name())
}
}
if len(pubs) == 0 {
return nil
}
fmt.Println("Do you want to upload an SSH key ?")
fmt.Println("[0] I don't want to upload a key !")
for i := range pubs {
fmt.Printf("[%d] %s\n", i+1, pubs[i])
}
for {
if err := promptUser("Which [id]: ", &args.SSHKey, true); err != nil {
return err
}
id, err := strconv.ParseUint(strings.TrimSpace(args.SSHKey), 10, 32)
if err != nil {
fmt.Println(err)
continue
}
if int(id) > len(pubs) {
fmt.Println("Out of range id must be lower than", len(pubs))
continue
}
args.SSHKey = ""
if id == 0 {
break
}
buff, err := ioutil.ReadFile(filepath.Join(dir, pubs[id-1]))
if err != nil {
return fmt.Errorf("Unable to open your key: %v", err)
}
args.SSHKey = string(buff[:])
break
}
return nil
}
func postToken(connect api.ScalewayConnectInterface) (*http.Response, error) {
FakeConnection, err := api.NewScalewayAPI("", "", scwversion.UserAgent(), "", clilogger.SetupLogger)
if err != nil {
return nil, fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err)
}
FakeConnection.SetPassword(connect.GetPassword())
resp, err := FakeConnection.PostResponse(api.AccountAPI, "tokens", connect)
return resp, err
}
func getToken(connect api.ScalewayConnect) (string, error) {
var resp *http.Response
var err error
resp, err = postToken(&connect)
if err != nil {
return "", err
}
defer resp.Body.Close()
// HTTP/403 error: check if the error is a 2FA_error. If it is, we need to
// provide a 2FA_token or a 2FA_backup_code to complete authentication.
if resp.StatusCode == 403 {
var data api.ScalewayAPIError
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return "", err
}
if data.Type == "2FA_error" {
authToken := ""
if err = promptUser("Two-Factor auth token (or backup code): ", &authToken, true); err != nil {
return "", err
}
// If the input contains a letter, consider it is a backup code.
// Otherwise consider it's an OTP.
isBackupCode, err := regexp.MatchString("[a-zA-Z]", authToken)
if err != nil {
return "", err
}
authToken = strings.Trim(authToken, "\r\n")
if isBackupCode {
resp, err = postToken(
&api.ScalewayConnectByBackupCode{
ScalewayConnect: connect,
TwoFABackupCode: authToken})
} else {
resp, err = postToken(
&api.ScalewayConnectByOTP{
ScalewayConnect: connect,
TwoFAToken: authToken})
}
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
if isBackupCode {
return "", fmt.Errorf("[%d] Invalid backup code. Maybe it has already been used?", resp.StatusCode)
}
return "", fmt.Errorf("[%d] Invalid OTP token", resp.StatusCode)
}
}
}
// Either non HTTP/403 error or HTTP/403 error different from "2FA_error".
if resp.StatusCode != 201 {
return "", fmt.Errorf("[%d] maybe your email or your password is not valid", resp.StatusCode)
}
var data api.ScalewayConnectResponse
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return "", err
}
return data.Token.ID, nil
}
func getOrganization(token string, email string) (string, error) {
FakeConnection, err := api.NewScalewayAPI("", token, scwversion.UserAgent(), "", clilogger.SetupLogger)
if err != nil {
return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err)
}
data, err := FakeConnection.GetOrganization()
if err != nil {
return "", err
}
orgaID := ""
for _, orga := range data.Organizations {
for _, user := range orga.Users {
if user.Email == email {
for i := range user.Organizations {
if user.Organizations[i].Name != "OCS" {
orgaID = user.Organizations[i].ID
goto exit
}
}
}
}
}
if orgaID == "" {
return "", fmt.Errorf("Unable to find your organization")
}
exit:
return orgaID, nil
}
func connectAPI() (string, string, error) {
email := ""
password := ""
orga := ""
token := ""
hostname, err := os.Hostname()
if err != nil {
return "", "", fmt.Errorf("unable to get your Hostname %v", err)
}
if err = promptUser("Login (cloud.scaleway.com): ", &email, true); err != nil {
return "", "", err
}
if err = promptUser("Password: ", &password, false); err != nil {
return "", "", err
}
connect := api.ScalewayConnect{
Email: strings.Trim(email, "\r\n"),
Password: strings.Trim(password, "\r\n"),
Expires: false,
Description: strings.Join([]string{"scw", hostname}, "-"),
}
token, err = getToken(connect)
if err != nil {
return "", "", err
}
orga, err = getOrganization(token, connect.Email)
if err != nil {
return "", "", err
}
return orga, token, nil
}
// uploadSSHKeys uploads an SSH Key
func uploadSSHKeys(apiConnection *api.ScalewayAPI, newKey string) {
user, err := apiConnection.GetUser()
if err != nil {
logrus.Errorf("Unable to contact ScalewayAPI: %s", err)
} else {
user.SSHPublicKeys = append(user.SSHPublicKeys, api.ScalewayKeyDefinition{Key: strings.Trim(newKey, "\n")})
SSHKeys := api.ScalewayUserPatchSSHKeyDefinition{
SSHPublicKeys: user.SSHPublicKeys,
}
for i := range SSHKeys.SSHPublicKeys {
SSHKeys.SSHPublicKeys[i].Fingerprint = ""
}
userID, err := apiConnection.GetUserID()
if err != nil {
logrus.Errorf("Unable to get userID: %s", err)
} else {
if err = apiConnection.PatchUserSSHKey(userID, SSHKeys); err != nil {
logrus.Errorf("Unable to patch SSHkey: %v", err)
}
}
}
}
// RunLogin is the handler for 'scw login'
func RunLogin(ctx CommandContext, args LoginArgs) error {
if config, cfgErr := config.GetConfig(ctx.ConfigPath); cfgErr == nil {
if TestConnection, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "", clilogger.SetupLogger); err == nil {
if user, err := TestConnection.GetUser(); err == nil {
fmt.Println("You are already logged as", user.Fullname)
}
}
}
if args.Organization == "" || args.Token == "" {
var err error
args.Organization, args.Token, err = connectAPI()
if err != nil {
return err
}
}
cfg := &config.Config{
Organization: strings.Trim(args.Organization, "\n"),
Token: strings.Trim(args.Token, "\n"),
}
apiConnection, err := api.NewScalewayAPI(cfg.Organization, cfg.Token, scwversion.UserAgent(), "", clilogger.SetupLogger)
if err != nil {
return fmt.Errorf("Unable to create ScalewayAPI: %s", err)
}
err = apiConnection.CheckCredentials()
if err != nil {
return fmt.Errorf("Unable to contact ScalewayAPI: %s", err)
}
if !args.SkipSSHKey {
if err = selectKey(&args); err != nil {
logrus.Errorf("Unable to select a key: %v", err)
} else {
if args.SSHKey != "" {
uploadSSHKeys(apiConnection, args.SSHKey)
}
}
}
name := "."
user, err := apiConnection.GetUser()
if err == nil {
name = "as " + user.Fullname + "."
}
fmt.Println("")
fmt.Println("You are now authenticated on Scaleway.com", name)
fmt.Println("You can list your existing servers using `scw ps` or create a new one using `scw run ubuntu-bionic`.")
fmt.Println("You can get a list of all available commands using `scw -h` and get more usage examples on github.com/scaleway/scaleway-cli.")
fmt.Println("Happy cloud riding.")
return cfg.Save(ctx.ConfigPath)
}
func promptUser(prompt string, output *string, echo bool) error {
// FIXME: should use stdin/stdout from command context
fmt.Fprintf(os.Stdout, prompt)
os.Stdout.Sync()
if !echo {
b, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return fmt.Errorf("Unable to prompt for password: %s", err)
}
*output = string(b)
fmt.Fprintf(os.Stdout, "\n")
} else {
reader := bufio.NewReader(os.Stdin)
*output, _ = reader.ReadString('\n')
}
return nil
}