forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
401 lines (342 loc) · 7.83 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
package rc
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"runtime"
"time"
"github.com/concourse/fly/ui"
"github.com/concourse/fly/version"
"github.com/concourse/go-concourse/concourse"
"golang.org/x/oauth2"
)
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 fly -t %s sync\n", ui.Embolden(e.flyVersion), ui.Embolden(e.atcVersion), e.targetName)
}
type Target interface {
Client() concourse.Client
Team() concourse.Team
CACert() string
Validate() error
ValidateWithWarningOnly() error
TLSConfig() *tls.Config
URL() string
Token() *TargetToken
TokenAuthorization() (string, bool)
}
type target struct {
name TargetName
teamName string
caCert string
tlsConfig *tls.Config
client concourse.Client
url string
token *TargetToken
}
func newTarget(
name TargetName,
teamName string,
url string,
token *TargetToken,
caCert string,
caCertPool *x509.CertPool,
insecure bool,
client concourse.Client,
) *target {
tlsConfig := &tls.Config{
InsecureSkipVerify: insecure,
RootCAs: caCertPool,
}
return &target{
name: name,
teamName: teamName,
url: url,
token: token,
caCert: caCert,
tlsConfig: tlsConfig,
client: client,
}
}
func LoadTarget(selectedTarget TargetName) (Target, error) {
targetProps, err := selectTarget(selectedTarget)
if err != nil {
return nil, err
}
caCertPool, err := loadCACertPool(targetProps.CACert)
if err != nil {
return nil, err
}
httpClient := defaultHttpClient(targetProps.Token, targetProps.Insecure, caCertPool)
client := concourse.NewClient(targetProps.API, httpClient)
return newTarget(
selectedTarget,
targetProps.TeamName,
targetProps.API,
targetProps.Token,
targetProps.CACert,
caCertPool,
targetProps.Insecure,
client,
), nil
}
func LoadTargetWithInsecure(
selectedTarget TargetName,
teamName string,
commandInsecure bool,
caCert string,
) (Target, error) {
targetProps, err := selectTarget(selectedTarget)
if err != nil {
return nil, err
}
if teamName == "" {
teamName = targetProps.TeamName
}
if caCert == "" {
caCert = targetProps.CACert
}
if commandInsecure {
caCert = ""
}
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
httpClient := defaultHttpClient(targetProps.Token, commandInsecure, caCertPool)
return newTarget(
selectedTarget,
teamName,
targetProps.API,
targetProps.Token,
caCert,
caCertPool,
targetProps.Insecure,
concourse.NewClient(targetProps.API, httpClient),
), nil
}
func NewUnauthenticatedTarget(
name TargetName,
url string,
teamName string,
insecure bool,
caCert string,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
httpClient := unauthenticatedHttpClient(insecure, caCertPool)
client := concourse.NewClient(url, httpClient)
return newTarget(
name,
teamName,
url,
nil,
caCert,
caCertPool,
insecure,
client,
), nil
}
func NewBasicAuthTarget(
name TargetName,
url string,
teamName string,
insecure bool,
username string,
password string,
caCert string,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
httpClient := basicAuthHttpClient(username, password, insecure, caCertPool)
client := concourse.NewClient(url, httpClient)
return newTarget(
name,
teamName,
url,
nil,
caCert,
caCertPool,
insecure,
client,
), nil
}
func NewNoAuthTarget(
name TargetName,
url string,
teamName string,
insecure bool,
caCert string,
) (Target, error) {
caCertPool, err := loadCACertPool(caCert)
if err != nil {
return nil, err
}
httpClient := &http.Client{Transport: transport(insecure, caCertPool)}
client := concourse.NewClient(url, httpClient)
return newTarget(
name,
teamName,
url,
nil,
caCert,
caCertPool,
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) CACert() string {
return t.caCert
}
func (t *target) TLSConfig() *tls.Config {
return t.tlsConfig
}
func (t *target) URL() string {
return t.url
}
func (t *target) Token() *TargetToken {
return t.token
}
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) validate(allowVersionMismatch bool) error {
info, err := t.client.GetInfo()
if err != nil {
return err
}
if info.Version == version.Version || version.IsDev(version.Version) {
return nil
}
atcMajor, atcMinor, atcPatch, err := version.GetSemver(info.Version)
if err != nil {
return err
}
flyMajor, flyMinor, flyPatch, err := version.GetSemver(version.Version)
if err != nil {
return err
}
if !allowVersionMismatch && (atcMajor != flyMajor || atcMinor != flyMinor) {
return NewErrVersionMismatch(version.Version, 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(version.Version, info.Version, t.name).Error()))
}
return nil
}
func unauthenticatedHttpClient(insecure bool, caCertPool *x509.CertPool) *http.Client {
return &http.Client{
Transport: transport(insecure, caCertPool),
}
}
func defaultHttpClient(token *TargetToken, insecure bool, caCertPool *x509.CertPool) *http.Client {
var oAuthToken *oauth2.Token
if token != nil {
oAuthToken = &oauth2.Token{
TokenType: token.Type,
AccessToken: token.Value,
}
}
transport := transport(insecure, caCertPool)
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 basicAuthHttpClient(
username string,
password string,
insecure bool,
caCertPool *x509.CertPool,
) *http.Client {
return &http.Client{
Transport: basicAuthTransport{
username: username,
password: password,
base: transport(insecure, caCertPool),
},
}
}
func transport(insecure bool, caCertPool *x509.CertPool) http.RoundTripper {
var transport http.RoundTripper
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
RootCAs: caCertPool,
},
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)
}