-
Notifications
You must be signed in to change notification settings - Fork 787
/
auth_config.go
431 lines (400 loc) · 10.4 KB
/
auth_config.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package auth
import (
"fmt"
"io"
"net/url"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
func (c *AuthConfig) FindUserAuths(serverURL string) []*UserAuth {
for _, server := range c.Servers {
if urlsEqual(server.URL, serverURL) {
return server.Users
}
}
return []*UserAuth{}
}
func (c *AuthConfig) GetOrCreateUserAuth(url string, username string) *UserAuth {
user := c.FindUserAuth(url, username)
if user != nil {
return user
}
server := c.GetOrCreateServer(url)
if server.Users == nil {
server.Users = []*UserAuth{}
}
user = &UserAuth{
Username: username,
}
server.Users = append(server.Users, user)
for _, user := range server.Users {
if user.Username == username {
return user
}
}
return nil
}
// FindUserAuth finds the auth for the given user name
// if no username is specified and there is only one auth then return that else nil
func (c *AuthConfig) FindUserAuth(serverURL string, username string) *UserAuth {
auths := c.FindUserAuths(serverURL)
if username == "" {
if len(auths) == 1 {
return auths[0]
} else {
return nil
}
}
for _, auth := range auths {
if auth.Username == username {
return auth
}
}
return nil
}
func (c *AuthConfig) IndexOfServerName(name string) int {
for i, server := range c.Servers {
if server.Name == name {
return i
}
}
return -1
}
func (c *AuthConfig) SetUserAuth(url string, auth *UserAuth) {
username := auth.Username
for i, server := range c.Servers {
if urlsEqual(server.URL, url) {
for j, a := range server.Users {
if a.Username == auth.Username {
c.Servers[i].Users[j] = auth
c.Servers[i].CurrentUser = username
return
}
}
c.Servers[i].Users = append(c.Servers[i].Users, auth)
c.Servers[i].CurrentUser = username
return
}
}
c.Servers = append(c.Servers, &AuthServer{
URL: url,
Users: []*UserAuth{auth},
CurrentUser: username,
})
}
func urlsEqual(url1, url2 string) bool {
return url1 == url2 || strings.TrimSuffix(url1, "/") == strings.TrimSuffix(url2, "/")
}
// GetServerByName returns the server for the given URL or null if its not found
func (c *AuthConfig) GetServer(url string) *AuthServer {
for _, s := range c.Servers {
if urlsEqual(s.URL, url) {
return s
}
}
return nil
}
// GetServerByName returns the server for the given name or null if its not found
func (c *AuthConfig) GetServerByName(name string) *AuthServer {
for _, s := range c.Servers {
if s.Name == name {
return s
}
}
return nil
}
// GetServerByKind returns the server for the given kind or null if its not found
func (c *AuthConfig) GetServerByKind(kind string) *AuthServer {
for _, s := range c.Servers {
if s.Kind == kind && s.URL == c.CurrentServer {
return s
}
}
return nil
}
//DeleteServer deletes the server for the given URL and updates the current server
//if is the same with the deleted server
func (c *AuthConfig) DeleteServer(url string) {
for i, s := range c.Servers {
if urlsEqual(s.URL, url) {
c.Servers = append(c.Servers[:i], c.Servers[i+1:]...)
}
}
if urlsEqual(c.CurrentServer, url) && len(c.Servers) > 0 {
c.CurrentServer = c.Servers[0].URL
} else {
c.CurrentServer = ""
}
}
func (c *AuthConfig) CurrentUser(server *AuthServer, inCluster bool) *UserAuth {
if urlsEqual(c.PipeLineServer, server.URL) && inCluster {
return server.GetUserAuth(c.PipeLineUsername)
}
return server.CurrentAuth()
}
func (c *AuthConfig) GetOrCreateServer(url string) *AuthServer {
name := ""
kind := ""
return c.GetOrCreateServerName(url, name, kind)
}
func (c *AuthConfig) GetOrCreateServerName(url string, name string, kind string) *AuthServer {
s := c.GetServer(url)
if s == nil {
if name == "" {
// lets default the name to the server URL
name = urlHostName(url)
}
if c.Servers == nil {
c.Servers = []*AuthServer{}
}
s = &AuthServer{
URL: url,
Users: []*UserAuth{},
Name: name,
Kind: kind,
}
c.Servers = append(c.Servers, s)
}
if s.Kind == "" {
s.Kind = kind
}
return s
}
func (c *AuthConfig) AddServer(server *AuthServer) {
s := c.GetServer(server.URL)
if s == nil {
if c.Servers == nil {
c.Servers = []*AuthServer{}
}
c.Servers = append(c.Servers, server)
} else {
s.Users = append(s.Users, server.Users...)
}
}
func urlHostName(rawUrl string) string {
u, err := url.Parse(rawUrl)
if err == nil {
return u.Host
}
idx := strings.Index(rawUrl, "://")
if idx > 0 {
rawUrl = rawUrl[idx+3:]
}
return strings.TrimSuffix(rawUrl, "/")
}
func (c *AuthConfig) PickServer(message string, batchMode bool, in terminal.FileReader, out terminal.FileWriter, outErr io.Writer) (*AuthServer, error) {
if c.Servers == nil || len(c.Servers) == 0 {
return nil, fmt.Errorf("No servers available!")
}
if len(c.Servers) == 1 {
return c.Servers[0], nil
}
urls := []string{}
for _, s := range c.Servers {
urls = append(urls, s.URL)
}
url := ""
if len(urls) > 1 {
if batchMode {
url = c.CurrentServer
} else {
prompt := &survey.Select{
Message: message,
Options: urls,
}
surveyOpts := survey.WithStdio(in, out, outErr)
err := survey.AskOne(prompt, &url, survey.Required, surveyOpts)
if err != nil {
return nil, err
}
}
}
for _, s := range c.Servers {
if urlsEqual(s.URL, url) {
return s, nil
}
}
return nil, fmt.Errorf("Could not find server for URL %s", url)
}
// PickServerAuth Pick the servers auth
func (c *AuthConfig) PickServerUserAuth(server *AuthServer, message string, batchMode bool, org string, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) (*UserAuth, error) {
url := server.URL
userAuths := c.FindUserAuths(url)
surveyOpts := survey.WithStdio(in, out, errOut)
if len(userAuths) == 1 {
auth := userAuths[0]
if batchMode {
return auth, nil
}
confirm := &survey.Confirm{
Message: fmt.Sprintf("Do you wish to use %s as the %s", auth.Username, message),
Default: true,
}
flag := false
err := survey.AskOne(confirm, &flag, nil, surveyOpts)
if err != nil {
return auth, err
}
if flag {
return auth, nil
}
// lets create a new user name
prompt := &survey.Input{
Message: message,
}
username := ""
err = survey.AskOne(prompt, &username, nil, surveyOpts)
if err != nil {
return auth, err
}
return c.GetOrCreateUserAuth(url, username), nil
}
if len(userAuths) > 1 {
// If in batchmode select the user auth based on the org passed, or default to the first auth.
if batchMode {
for i, x := range userAuths {
if x.Username == org {
return userAuths[i], nil
}
}
return userAuths[0], nil
}
usernames := []string{}
m := map[string]*UserAuth{}
for _, ua := range userAuths {
name := ua.Username
usernames = append(usernames, name)
m[name] = ua
}
username := ""
prompt := &survey.Select{
Message: message,
Options: usernames,
}
err := survey.AskOne(prompt, &username, survey.Required, surveyOpts)
if err != nil {
return &UserAuth{}, err
}
answer := m[username]
if answer == nil {
return nil, fmt.Errorf("no username chosen")
}
return answer, nil
}
return &UserAuth{}, nil
}
// PrintUserFn prints the use name
type PrintUserFn func(username string) error
// EditUserAuth Lets the user input/edit the user auth
func (c *AuthConfig) EditUserAuth(serverLabel string, auth *UserAuth, defaultUserName string, editUser, batchMode bool, fn PrintUserFn, in terminal.FileReader, out terminal.FileWriter, outErr io.Writer) error {
// default the user name if its empty
defaultUsername := c.DefaultUsername
if defaultUsername == "" {
defaultUsername = defaultUserName
}
if auth.Username == "" {
auth.Username = defaultUsername
}
if batchMode {
if auth.Username == "" {
return fmt.Errorf("Running in batch mode and no default Git username found")
}
if auth.ApiToken == "" {
return fmt.Errorf("Running in batch mode and no default API token found")
}
return nil
}
var err error
if editUser || auth.Username == "" {
auth.Username, err = util.PickValue(serverLabel+" user name:", auth.Username, true, "", in, out, outErr)
if err != nil {
return err
}
}
if fn != nil {
err := fn(auth.Username)
if err != nil {
return err
}
}
auth.ApiToken, err = util.PickPassword("API Token:", "", in, out, outErr)
return err
}
// GetServerNames returns the name of the server currently in the configuration
func (c *AuthConfig) GetServerNames() []string {
answer := []string{}
for _, server := range c.Servers {
name := server.Name
if name != "" {
answer = append(answer, name)
}
}
sort.Strings(answer)
return answer
}
// GetServerURLs returns the server URLs currently in the configuration
func (c *AuthConfig) GetServerURLs() []string {
answer := []string{}
for _, server := range c.Servers {
u := server.URL
if u != "" {
answer = append(answer, u)
}
}
sort.Strings(answer)
return answer
}
// PickOrCreateServer picks the server to use defaulting to the current server
func (c *AuthConfig) PickOrCreateServer(fallbackServerURL string, serverURL string, message string, batchMode bool, in terminal.FileReader, out terminal.FileWriter, outErr io.Writer) (*AuthServer, error) {
servers := c.Servers
if len(servers) == 0 {
if serverURL != "" {
return c.GetOrCreateServer(serverURL), nil
}
return c.GetOrCreateServer(fallbackServerURL), nil
}
// lets let the user pick which server to use defaulting to the current server
names := []string{}
teamServerMissing := true
for _, s := range servers {
u := s.URL
if u != "" {
names = append(names, u)
}
if u == serverURL {
teamServerMissing = false
}
}
if teamServerMissing && serverURL != "" {
names = append(names, serverURL)
}
if len(names) == 1 {
return c.GetOrCreateServer(names[0]), nil
}
defaultValue := serverURL
if defaultValue == "" {
defaultValue = c.CurrentServer
}
if defaultValue == "" {
defaultValue = names[0]
}
if batchMode {
return c.GetOrCreateServer(defaultValue), nil
}
name, err := util.PickRequiredNameWithDefault(names, message, defaultValue, "", in, out, outErr)
if err != nil {
return nil, err
}
if name == "" {
return nil, fmt.Errorf("no server URL chosen")
}
return c.GetOrCreateServer(name), nil
}
// UpdatePipelineServer updates the pipeline server in the configuration
func (c *AuthConfig) UpdatePipelineServer(server *AuthServer, user *UserAuth) {
c.PipeLineServer = server.URL
c.PipeLineUsername = user.Username
}