forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
310 lines (263 loc) · 6.63 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
package commands
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"net"
"github.com/concourse/atc"
"github.com/concourse/fly/rc"
"github.com/concourse/go-concourse/concourse"
"github.com/vito/go-interact/interact"
)
type LoginCommand struct {
ATCURL string `short:"c" long:"concourse-url" description:"Concourse URL to authenticate with"`
Insecure bool `short:"k" long:"insecure" description:"Skip verification of the endpoint's SSL certificate"`
Username string `short:"u" long:"username" description:"Username for basic auth"`
Password string `short:"p" long:"password" description:"Password for basic auth"`
TeamName string `short:"n" long:"team-name" description:"Team to authenticate with" default:"main"`
CACert atc.PathFlag `long:"ca-cert" description:"Path to Concourse PEM-encoded CA certificate file."`
}
func (command *LoginCommand) Execute(args []string) error {
if Fly.Target == "" {
return errors.New("name for the target must be specified (--target/-t)")
}
var target rc.Target
var err error
var caCert string
if command.CACert != "" {
caCertBytes, err := ioutil.ReadFile(string(command.CACert))
if err != nil {
return err
}
caCert = string(caCertBytes)
}
if command.ATCURL != "" {
target, err = rc.NewUnauthenticatedTarget(
Fly.Target,
command.ATCURL,
command.TeamName,
command.Insecure,
caCert,
)
} else {
target, err = rc.LoadTargetWithInsecure(
Fly.Target,
command.TeamName,
command.Insecure,
caCert,
)
}
if err != nil {
return err
}
err = target.ValidateWithWarningOnly()
if err != nil {
return err
}
authMethods, err := target.Team().ListAuthMethods()
if err != nil {
return err
}
var chosenMethod atc.AuthMethod
if command.Username != "" && command.Password != "" {
for _, method := range authMethods {
if method.Type == atc.AuthTypeBasic {
chosenMethod = method
break
}
}
if chosenMethod.Type == "" {
return errors.New("basic auth is not available")
}
} else {
switch len(authMethods) {
case 0:
target, err := rc.NewNoAuthTarget(
Fly.Target,
target.Client().URL(),
command.TeamName,
command.Insecure,
target.CACert(),
)
if err != nil {
return err
}
token, err := target.Team().AuthToken()
if err != nil {
return err
}
return command.saveTarget(
target.Client().URL(),
&rc.TargetToken{
Type: token.Type,
Value: token.Value,
},
target.CACert(),
)
case 1:
chosenMethod = authMethods[0]
default:
choices := make([]interact.Choice, len(authMethods))
for i, method := range authMethods {
choices[i] = interact.Choice{
Display: method.DisplayName,
Value: method,
}
}
err = interact.NewInteraction("choose an auth method", choices...).Resolve(&chosenMethod)
if err != nil {
return err
}
}
}
client := target.Client()
token, err := command.loginWith(chosenMethod, client, caCert, target.Client().URL())
if err != nil {
return err
}
return command.saveTarget(
client.URL(),
&rc.TargetToken{
Type: token.Type,
Value: token.Value,
},
target.CACert(),
)
}
func listenForTokenCallback(tokenChannel chan string, errorChannel chan error, portChannel chan string, targetUrl string) {
s := &http.Server{
Addr: "127.0.0.1:0",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenChannel <- r.FormValue("token")
http.Redirect(w, r, fmt.Sprintf("%s/public/fly_success", targetUrl), http.StatusTemporaryRedirect)
}),
}
err := listenAndServeWithPort(s, portChannel)
if err != nil {
errorChannel <- err
}
}
func (command *LoginCommand) loginWith(
method atc.AuthMethod,
client concourse.Client,
caCert string,
targetUrl string,
) (*atc.AuthToken, error) {
var token atc.AuthToken
switch method.Type {
case atc.AuthTypeOAuth:
var tokenStr string
stdinChannel := make(chan string)
tokenChannel := make(chan string)
errorChannel := make(chan error)
portChannel := make(chan string)
go listenForTokenCallback(tokenChannel, errorChannel, portChannel, targetUrl)
port := <-portChannel
fmt.Println("navigate to the following URL in your browser:")
fmt.Println("")
fmt.Printf(" %s&fly_local_port=%s\n", method.AuthURL, port)
fmt.Println("")
go waitForTokenInput(stdinChannel, errorChannel)
select {
case tokenStrMsg := <-tokenChannel:
tokenStr = tokenStrMsg
case tokenStrMsg := <-stdinChannel:
tokenStr = tokenStrMsg
case errorMsg := <-errorChannel:
return nil, errorMsg
}
segments := strings.SplitN(tokenStr, " ", 2)
token.Type = segments[0]
token.Value = segments[1]
case atc.AuthTypeBasic:
var username string
if command.Username != "" {
username = command.Username
} else {
err := interact.NewInteraction("username").Resolve(interact.Required(&username))
if err != nil {
return nil, err
}
}
var password string
if command.Password != "" {
password = command.Password
} else {
var interactivePassword interact.Password
err := interact.NewInteraction("password").Resolve(interact.Required(&interactivePassword))
if err != nil {
return nil, err
}
password = string(interactivePassword)
}
target, err := rc.NewBasicAuthTarget(
Fly.Target,
client.URL(),
command.TeamName,
command.Insecure,
username,
password,
caCert,
)
if err != nil {
return nil, err
}
token, err = target.Team().AuthToken()
if err != nil {
return nil, err
}
}
return &token, nil
}
func waitForTokenInput(tokenChannel chan string, errorChannel chan error) {
for {
fmt.Printf("or enter token manually: ")
var tokenType string
var tokenValue string
count, err := fmt.Scanf("%s %s", &tokenType, &tokenValue)
if err != nil {
if count != 2 {
fmt.Println("token must be of the format 'TYPE VALUE', e.g. 'Bearer ...'")
continue
}
errorChannel <- err
return
}
tokenChannel <- tokenType + " " + tokenValue
break
}
}
func (command *LoginCommand) saveTarget(url string, token *rc.TargetToken, caCert string) error {
err := rc.SaveTarget(
Fly.Target,
url,
command.Insecure,
command.TeamName,
&rc.TargetToken{
Type: token.Type,
Value: token.Value,
},
caCert,
)
if err != nil {
return err
}
fmt.Println("")
fmt.Println("target saved")
return nil
}
func listenAndServeWithPort(srv *http.Server, portChannel chan string) error {
addr := srv.Addr
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
_, port, err := net.SplitHostPort(ln.Addr().String())
portChannel <- port
return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
}
type tcpKeepAliveListener struct {
*net.TCPListener
}