forked from concourse/concourse
/
target.go
620 lines (521 loc) · 13.1 KB
/
target.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package rc
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"runtime"
"time"
conc "github.com/pf-qiu/concourse/v6"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/fly/ui"
"github.com/pf-qiu/concourse/v6/fly/version"
"github.com/pf-qiu/concourse/v6/go-concourse/concourse"
semisemanticversion "github.com/cppforlife/go-semi-semantic/version"
"golang.org/x/oauth2"
)
var LocalVersion = conc.Version
func init() {
ver, found := os.LookupEnv("FAKE_FLY_VERSION")
if found {
LocalVersion = ver
}
}
type ErrVersionMismatch struct {
flyVersion string
atcVersion string
targetName TargetName
}
func NewErrVersionMismatch(flyVersion string, atcVersion string, targetName TargetName) ErrVersionMismatch {
return ErrVersionMismatch{
flyVersion: flyVersion,
atcVersion: atcVersion,
targetName: targetName,
}
}
func (e ErrVersionMismatch) Error() string {
return fmt.Sprintf(
"fly version (%s) is out of sync with the target (%s). to sync up, run the following:\n\n %s -t %s sync\n",
ui.Embolden(e.flyVersion), ui.Embolden(e.atcVersion), os.Args[0], e.targetName)
}
//go:generate counterfeiter . Target
type Target interface {
Client() concourse.Client
Team() concourse.Team
FindTeam(string) (concourse.Team, error)
CACert() string
ClientCertPath() string
ClientKeyPath() string
ClientCertificate() []tls.Certificate
Validate() error
ValidateWithWarningOnly() error
TLSConfig() *tls.Config
URL() string
WorkerVersion() (string, error)
IsWorkerVersionCompatible(string) (bool, error)
Token() *TargetToken
TokenAuthorization() (string, bool)
Version() (string, error)
}
type target struct {
name TargetName
teamName string
caCert string
clientCertPath string
clientKeyPath string
clientCertificate []tls.Certificate
tlsConfig *tls.Config
client concourse.Client
url string
token *TargetToken
info atc.Info
}
func NewTarget(
name TargetName,
teamName string,
url string,
token *TargetToken,
caCert string,
caCertPool *x509.CertPool,
clientCertPath string,
clientKeyPath string,
clientCertificate []tls.Certificate,
insecure bool,
client concourse.Client,
) *target {
tlsConfig := &tls.Config{
InsecureSkipVerify: insecure,
RootCAs: caCertPool,
Certificates: clientCertificate,
}
return &target{
name: name,
teamName: teamName,
url: url,
token: token,
caCert: caCert,
clientCertPath: clientCertPath,
clientKeyPath: clientKeyPath,
clientCertificate: clientCertificate,
tlsConfig: tlsConfig,
client: client,
}
}
func LoadTargetFromURL(url, team string, tracing bool) (Target, TargetName, error) {
flyTargets, err := LoadTargets()
if err != nil {
return nil, "", err
}
for name, props := range flyTargets {
if props.API == url && props.TeamName == team {
target, err := LoadTarget(name, tracing)
return target, name, err
}
}
return nil, "", ErrNoTargetFromURL
}
func LoadTarget(selectedTarget TargetName, tracing bool) (Target, error) {
var clientCertificate []tls.Certificate
targetProps, err := selectTarget(selectedTarget)
if err != nil {
return nil, err
}
caCertPool, err := loadCACertPool(targetProps.CACert)
if err != nil {
return nil, err
}
clientCertificate, err = loadClientCertificate(targetProps.ClientCertPath, targetProps.ClientKeyPath)
if err != nil {
return nil, err
}
httpClient := defaultHttpClient(targetProps.Token, targetProps.Insecure, caCertPool, clientCertificate)
client := concourse.NewClient(targetProps.API, httpClient, tracing)
return NewTarget(
selectedTarget,
targetProps.TeamName,
targetProps.API,
targetProps.Token,
targetProps.CACert,
caCertPool,
targetProps.ClientCertPath,
targetProps.ClientKeyPath,
clientCertificate,
targetProps.Insecure,
client,
), nil
}
func LoadUnauthenticatedTarget(
selectedTarget TargetName,
teamName string,
insecure bool,
caCert string,
clientCertPath string,
clientKeyPath string,
tracing bool,
) (Target, error) {
targetProps, err := selectTarget(selectedTarget)
if err != nil {
return nil, err
}
if teamName == "" {
teamName = targetProps.TeamName
}
if caCert == "" {
caCert = targetProps.CACert
}
if insecure {
caCert = ""
}
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
var clientCertificate []tls.Certificate
if clientCertPath == "" && clientKeyPath == "" {
clientCertPath = targetProps.ClientCertPath
clientKeyPath = targetProps.ClientKeyPath
}
clientCertificate, err = loadClientCertificate(clientCertPath, clientKeyPath)
if err != nil {
return nil, err
}
httpClient := &http.Client{Transport: transport(insecure, caCertPool, clientCertificate)}
return NewTarget(
selectedTarget,
teamName,
targetProps.API,
targetProps.Token,
caCert,
caCertPool,
clientCertPath,
clientKeyPath,
clientCertificate,
targetProps.Insecure,
concourse.NewClient(targetProps.API, httpClient, tracing),
), nil
}
func NewUnauthenticatedTarget(
name TargetName,
url string,
teamName string,
insecure bool,
caCert string,
clientCertPath string,
clientKeyPath string,
tracing bool,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
var clientCertificate []tls.Certificate
clientCertificate, err = loadClientCertificate(clientCertPath, clientKeyPath)
if err != nil {
return nil, err
}
httpClient := &http.Client{Transport: transport(insecure, caCertPool, clientCertificate)}
client := concourse.NewClient(url, httpClient, tracing)
return NewTarget(
name,
teamName,
url,
nil,
caCert,
caCertPool,
clientCertPath,
clientKeyPath,
clientCertificate,
insecure,
client,
), nil
}
func NewAuthenticatedTarget(
name TargetName,
url string,
teamName string,
insecure bool,
token *TargetToken,
caCert string,
clientCertPath string,
clientKeyPath string,
tracing bool,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
var clientCertificate []tls.Certificate
clientCertificate, err = loadClientCertificate(clientCertPath, clientKeyPath)
if err != nil {
return nil, err
}
httpClient := defaultHttpClient(token, insecure, caCertPool, clientCertificate)
client := concourse.NewClient(url, httpClient, tracing)
return NewTarget(
name,
teamName,
url,
token,
caCert,
caCertPool,
clientCertPath,
clientKeyPath,
clientCertificate,
insecure,
client,
), nil
}
func NewBasicAuthTarget(
name TargetName,
url string,
teamName string,
insecure bool,
username string,
password string,
caCert string,
clientCertPath string,
clientKeyPath string,
tracing bool,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
var clientCertificate []tls.Certificate
clientCertificate, err = loadClientCertificate(clientCertPath, clientKeyPath)
if err != nil {
return nil, err
}
httpClient := basicAuthHttpClient(username, password, insecure, caCertPool, clientCertificate)
client := concourse.NewClient(url, httpClient, tracing)
return NewTarget(
name,
teamName,
url,
nil,
caCert,
caCertPool,
clientCertPath,
clientKeyPath,
clientCertificate,
insecure,
client,
), nil
}
func (t *target) Client() concourse.Client {
return t.client
}
func (t *target) Team() concourse.Team {
return t.client.Team(t.teamName)
}
func (t *target) FindTeam(teamName string) (concourse.Team, error) {
return t.client.FindTeam(teamName)
}
func (t *target) CACert() string {
return t.caCert
}
func (t *target) TLSConfig() *tls.Config {
return t.tlsConfig
}
func (t *target) ClientCertPath() string {
return t.clientCertPath
}
func (t *target) ClientKeyPath() string {
return t.clientKeyPath
}
func (t *target) ClientCertificate() []tls.Certificate {
return t.clientCertificate
}
func (t *target) URL() string {
return t.url
}
func (t *target) Token() *TargetToken {
return t.token
}
func (t *target) Version() (string, error) {
info, err := t.getInfo()
if err != nil {
return "", err
}
return info.Version, nil
}
func (t *target) WorkerVersion() (string, error) {
info, err := t.getInfo()
if err != nil {
return "", err
}
return info.WorkerVersion, nil
}
func (t *target) TokenAuthorization() (string, bool) {
if t.token == nil || (t.token.Type == "" && t.token.Value == "") {
return "", false
}
return t.token.Type + " " + t.token.Value, true
}
func (t *target) ValidateWithWarningOnly() error {
return t.validate(true)
}
func (t *target) Validate() error {
return t.validate(false)
}
func (t *target) IsWorkerVersionCompatible(workerVersion string) (bool, error) {
info, err := t.getInfo()
if err != nil {
return false, err
}
if info.WorkerVersion == "" {
return true, nil
}
if workerVersion == "" {
return false, nil
}
workerV, err := semisemanticversion.NewVersionFromString(workerVersion)
if err != nil {
return false, err
}
infoV, err := semisemanticversion.NewVersionFromString(info.WorkerVersion)
if err != nil {
return false, err
}
if workerV.Release.Components[0].Compare(infoV.Release.Components[0]) != 0 {
return false, nil
}
if workerV.Release.Components[1].Compare(infoV.Release.Components[1]) == -1 {
return false, nil
}
return true, nil
}
func (t *target) validate(allowVersionMismatch bool) error {
info, err := t.getInfo()
if err != nil {
return err
}
if info.Version == LocalVersion || version.IsDev(LocalVersion) {
return nil
}
atcMajor, atcMinor, atcPatch, err := version.GetSemver(info.Version)
if err != nil {
return err
}
flyMajor, flyMinor, flyPatch, err := version.GetSemver(LocalVersion)
if err != nil {
return err
}
if !allowVersionMismatch && (atcMajor != flyMajor || atcMinor != flyMinor) {
return NewErrVersionMismatch(LocalVersion, info.Version, t.name)
}
if atcMajor != flyMajor || atcMinor != flyMinor || atcPatch != flyPatch {
fmt.Fprintln(ui.Stderr, ui.WarningColor("WARNING:\n"))
fmt.Fprintln(ui.Stderr, ui.WarningColor(NewErrVersionMismatch(LocalVersion, info.Version, t.name).Error()))
}
return nil
}
func (t *target) getInfo() (atc.Info, error) {
if (t.info != atc.Info{}) {
return t.info, nil
}
var err error
t.info, err = t.client.GetInfo()
return t.info, err
}
func defaultHttpClient(token *TargetToken, insecure bool, caCertPool *x509.CertPool, clientCertificate []tls.Certificate) *http.Client {
var oAuthToken *oauth2.Token
if token != nil {
oAuthToken = &oauth2.Token{
TokenType: token.Type,
AccessToken: token.Value,
}
}
transport := transport(insecure, caCertPool, clientCertificate)
if token != nil {
transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(oAuthToken),
Base: transport,
}
}
return &http.Client{Transport: transport}
}
func loadCACertPool(caCert string) (cert *x509.CertPool, err error) {
if caCert == "" {
return nil, nil
}
// TODO: remove else block once we switch to go 1.8
// x509.SystemCertPool is not supported in go 1.7 on Windows
// see: https://github.com/golang/go/issues/16736
var pool *x509.CertPool
if runtime.GOOS != "windows" {
var err error
pool, err = x509.SystemCertPool()
if err != nil {
return nil, err
}
} else {
pool = x509.NewCertPool()
}
ok := pool.AppendCertsFromPEM([]byte(caCert))
if !ok {
return nil, errors.New("CA Cert not valid")
}
return pool, nil
}
func loadClientCertificate(clientCertificateLocation string, clientKeyLocation string) (cert []tls.Certificate, err error) {
if clientCertificateLocation == "" {
if clientKeyLocation != "" {
err = errors.New("A client key may not be declared without defining a client certificate")
return []tls.Certificate{}, err
}
return []tls.Certificate{}, nil
}
if clientCertificateLocation != "" && clientKeyLocation == "" {
err = errors.New("A client certificate may not be declared without defining a client key")
return []tls.Certificate{}, err
}
clientCertData, err := tls.LoadX509KeyPair(clientCertificateLocation, clientKeyLocation)
if err != nil {
return []tls.Certificate{}, err
}
cert = []tls.Certificate{clientCertData}
return cert, nil
}
func basicAuthHttpClient(
username string,
password string,
insecure bool,
caCertPool *x509.CertPool,
clientCertificate []tls.Certificate,
) *http.Client {
return &http.Client{
Transport: basicAuthTransport{
username: username,
password: password,
base: transport(insecure, caCertPool, clientCertificate),
},
}
}
func transport(insecure bool, caCertPool *x509.CertPool, clientCertificate []tls.Certificate) http.RoundTripper {
var transport http.RoundTripper
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
RootCAs: caCertPool,
Certificates: clientCertificate,
},
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
Proxy: http.ProxyFromEnvironment,
}
return transport
}
type basicAuthTransport struct {
username string
password string
base http.RoundTripper
}
func (t basicAuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.SetBasicAuth(t.username, t.password)
return t.base.RoundTrip(r)
}