-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
472 lines (382 loc) · 12.5 KB
/
gateway.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
package net
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
"code.cloudfoundry.org/cli/version"
)
const (
JobFinished = "finished"
JobFailed = "failed"
DefaultPollingThrottle = 5 * time.Second
DefaultDialTimeout = 5 * time.Second
)
type JobResource struct {
Entity struct {
Status string
ErrorDetails struct {
Description string
} `json:"error_details"`
}
}
type AsyncResource struct {
Metadata struct {
URL string
}
}
type apiErrorHandler func(statusCode int, body []byte) error
type tokenRefresher interface {
RefreshAuthToken() (string, error)
}
type Request struct {
HTTPReq *http.Request
SeekableBody io.ReadSeeker
}
type Gateway struct {
authenticator tokenRefresher
errHandler apiErrorHandler
PollingEnabled bool
PollingThrottle time.Duration
trustedCerts []tls.Certificate
config coreconfig.Reader
warnings *[]string
Clock func() time.Time
transport *http.Transport
ui terminal.UI
logger trace.Printer
DialTimeout time.Duration
}
func (gateway *Gateway) AsyncTimeout() time.Duration {
if gateway.config.AsyncTimeout() > 0 {
return time.Duration(gateway.config.AsyncTimeout()) * time.Minute
}
return 0
}
func (gateway *Gateway) SetTokenRefresher(auth tokenRefresher) {
gateway.authenticator = auth
}
func (gateway Gateway) GetResource(url string, resource interface{}) (err error) {
request, err := gateway.NewRequest("GET", url, gateway.config.AccessToken(), nil)
if err != nil {
return
}
_, err = gateway.PerformRequestForJSONResponse(request, resource)
return
}
func (gateway Gateway) CreateResourceFromStruct(endpoint, url string, resource interface{}) error {
data, err := json.Marshal(resource)
if err != nil {
return err
}
return gateway.CreateResource(endpoint, url, bytes.NewReader(data))
}
func (gateway Gateway) UpdateResourceFromStruct(endpoint, apiURL string, resource interface{}) error {
data, err := json.Marshal(resource)
if err != nil {
return err
}
return gateway.UpdateResource(endpoint, apiURL, bytes.NewReader(data))
}
func (gateway Gateway) CreateResource(endpoint, apiURL string, body io.ReadSeeker, resource ...interface{}) error {
return gateway.createUpdateOrDeleteResource("POST", endpoint, apiURL, body, false, resource...)
}
func (gateway Gateway) UpdateResource(endpoint, apiURL string, body io.ReadSeeker, resource ...interface{}) error {
return gateway.createUpdateOrDeleteResource("PUT", endpoint, apiURL, body, false, resource...)
}
func (gateway Gateway) UpdateResourceSync(endpoint, apiURL string, body io.ReadSeeker, resource ...interface{}) error {
return gateway.createUpdateOrDeleteResource("PUT", endpoint, apiURL, body, true, resource...)
}
func (gateway Gateway) DeleteResourceSynchronously(endpoint, apiURL string) error {
return gateway.createUpdateOrDeleteResource("DELETE", endpoint, apiURL, nil, true, &AsyncResource{})
}
func (gateway Gateway) DeleteResource(endpoint, apiURL string) error {
return gateway.createUpdateOrDeleteResource("DELETE", endpoint, apiURL, nil, false, &AsyncResource{})
}
func (gateway Gateway) ListPaginatedResources(
target string,
path string,
resource interface{},
cb func(interface{}) bool,
) error {
for path != "" {
pagination := NewPaginatedResources(resource)
apiErr := gateway.GetResource(fmt.Sprintf("%s%s", target, path), &pagination)
if apiErr != nil {
return apiErr
}
resources, err := pagination.Resources()
if err != nil {
return fmt.Errorf("%s: %s", T("Error parsing JSON"), err.Error())
}
for _, resource := range resources {
if !cb(resource) {
return nil
}
}
path = pagination.NextURL
}
return nil
}
func (gateway Gateway) createUpdateOrDeleteResource(verb, endpoint, apiURL string, body io.ReadSeeker, sync bool, optionalResource ...interface{}) error {
var resource interface{}
if len(optionalResource) > 0 {
resource = optionalResource[0]
}
request, err := gateway.NewRequest(verb, endpoint+apiURL, gateway.config.AccessToken(), body)
if err != nil {
return err
}
if resource == nil {
_, err = gateway.PerformRequest(request)
return err
}
if gateway.PollingEnabled && !sync {
_, err = gateway.PerformPollingRequestForJSONResponse(endpoint, request, resource, gateway.AsyncTimeout())
return err
}
_, err = gateway.PerformRequestForJSONResponse(request, resource)
if err != nil {
return err
}
return nil
}
func (gateway Gateway) newRequest(request *http.Request, accessToken string, body io.ReadSeeker) *Request {
if accessToken != "" {
request.Header.Set("Authorization", accessToken)
}
request.Header.Set("accept", "application/json")
request.Header.Set("content-type", "application/json")
request.Header.Set("User-Agent", "go-cli "+version.VersionString()+" / "+runtime.GOOS)
return &Request{HTTPReq: request, SeekableBody: body}
}
func (gateway Gateway) NewRequestForFile(method, fullURL, accessToken string, body *os.File) (*Request, error) {
progressReader := NewProgressReader(body, gateway.ui, 5*time.Second)
_, _ = progressReader.Seek(0, 0)
fileStats, err := body.Stat()
if err != nil {
return nil, fmt.Errorf("%s: %s", T("Error getting file info"), err.Error())
}
request, err := http.NewRequest(method, fullURL, progressReader)
if err != nil {
return nil, fmt.Errorf("%s: %s", T("Error building request"), err.Error())
}
fileSize := fileStats.Size()
progressReader.SetTotalSize(fileSize)
request.ContentLength = fileSize
if err != nil {
return nil, fmt.Errorf("%s: %s", T("Error building request"), err.Error())
}
return gateway.newRequest(request, accessToken, progressReader), nil
}
func (gateway Gateway) NewRequest(method, path, accessToken string, body io.ReadSeeker) (*Request, error) {
request, err := http.NewRequest(method, path, body)
if err != nil {
return nil, fmt.Errorf("%s: %s", T("Error building request"), err.Error())
}
return gateway.newRequest(request, accessToken, body), nil
}
func (gateway Gateway) PerformRequest(request *Request) (*http.Response, error) {
return gateway.doRequestHandlingAuth(request)
}
func (gateway Gateway) performRequestForResponseBytes(request *Request) ([]byte, http.Header, *http.Response, error) {
rawResponse, err := gateway.doRequestHandlingAuth(request)
if err != nil {
return nil, nil, rawResponse, err
}
defer rawResponse.Body.Close()
bytes, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return bytes, nil, rawResponse, fmt.Errorf("%s: %s", T("Error reading response"), err.Error())
}
return bytes, rawResponse.Header, rawResponse, nil
}
func (gateway Gateway) PerformRequestForTextResponse(request *Request) (string, http.Header, error) {
bytes, headers, _, err := gateway.performRequestForResponseBytes(request)
return string(bytes), headers, err
}
func (gateway Gateway) PerformRequestForJSONResponse(request *Request, response interface{}) (http.Header, error) {
bytes, headers, rawResponse, err := gateway.performRequestForResponseBytes(request)
if err != nil {
if rawResponse != nil && rawResponse.Body != nil {
b, _ := ioutil.ReadAll(rawResponse.Body)
_ = json.Unmarshal(b, &response)
}
return headers, err
}
if rawResponse.StatusCode > 203 || strings.TrimSpace(string(bytes)) == "" {
return headers, nil
}
err = json.Unmarshal(bytes, &response)
if err != nil {
return headers, fmt.Errorf("%s: %s", T("Invalid JSON response from server"), err.Error())
}
return headers, nil
}
func (gateway Gateway) PerformPollingRequestForJSONResponse(endpoint string, request *Request, response interface{}, timeout time.Duration) (http.Header, error) {
query := request.HTTPReq.URL.Query()
query.Add("async", "true")
request.HTTPReq.URL.RawQuery = query.Encode()
bytes, headers, rawResponse, err := gateway.performRequestForResponseBytes(request)
if err != nil {
return headers, err
}
defer rawResponse.Body.Close()
if rawResponse.StatusCode > 203 || strings.TrimSpace(string(bytes)) == "" {
return headers, nil
}
err = json.Unmarshal(bytes, &response)
if err != nil {
return headers, fmt.Errorf("%s: %s", T("Invalid JSON response from server"), err.Error())
}
asyncResource := &AsyncResource{}
err = json.Unmarshal(bytes, &asyncResource)
if err != nil {
return headers, fmt.Errorf("%s: %s", T("Invalid async response from server"), err.Error())
}
jobURL := asyncResource.Metadata.URL
if jobURL == "" {
return headers, nil
}
if !strings.Contains(jobURL, "/jobs/") {
return headers, nil
}
err = gateway.waitForJob(endpoint+jobURL, request.HTTPReq.Header.Get("Authorization"), timeout)
return headers, err
}
func (gateway Gateway) Warnings() []string {
return *gateway.warnings
}
func (gateway Gateway) waitForJob(jobURL, accessToken string, timeout time.Duration) error {
startTime := gateway.Clock()
for true {
if gateway.Clock().Sub(startTime) > timeout && timeout != 0 {
return errors.NewAsyncTimeoutError(jobURL)
}
var request *Request
request, err := gateway.NewRequest("GET", jobURL, accessToken, nil)
response := &JobResource{}
_, err = gateway.PerformRequestForJSONResponse(request, response)
if err != nil {
return err
}
switch response.Entity.Status {
case JobFinished:
return nil
case JobFailed:
return errors.New(response.Entity.ErrorDetails.Description)
}
accessToken = request.HTTPReq.Header.Get("Authorization")
time.Sleep(gateway.PollingThrottle)
}
return nil
}
func (gateway Gateway) doRequestHandlingAuth(request *Request) (*http.Response, error) {
httpReq := request.HTTPReq
if request.SeekableBody != nil {
httpReq.Body = ioutil.NopCloser(request.SeekableBody)
}
// perform request
rawResponse, err := gateway.doRequestAndHandlerError(request)
if err == nil || gateway.authenticator == nil {
return rawResponse, err
}
switch err.(type) {
case *errors.InvalidTokenError:
// refresh the auth token
var newToken string
newToken, err = gateway.authenticator.RefreshAuthToken()
if err != nil {
return rawResponse, err
}
// reset the auth token and request body
httpReq.Header.Set("Authorization", newToken)
if request.SeekableBody != nil {
_, _ = request.SeekableBody.Seek(0, 0)
httpReq.Body = ioutil.NopCloser(request.SeekableBody)
}
// make the request again
rawResponse, err = gateway.doRequestAndHandlerError(request)
}
return rawResponse, err
}
func (gateway Gateway) doRequestAndHandlerError(request *Request) (*http.Response, error) {
rawResponse, err := gateway.doRequest(request.HTTPReq)
if err != nil {
return rawResponse, WrapNetworkErrors(request.HTTPReq.URL.Host, err)
}
if rawResponse.StatusCode > 299 {
defer rawResponse.Body.Close()
jsonBytes, _ := ioutil.ReadAll(rawResponse.Body)
rawResponse.Body = ioutil.NopCloser(bytes.NewBuffer(jsonBytes))
err = gateway.errHandler(rawResponse.StatusCode, jsonBytes)
}
return rawResponse, err
}
func (gateway Gateway) doRequest(request *http.Request) (*http.Response, error) {
var response *http.Response
var err error
if gateway.transport == nil {
makeHTTPTransport(&gateway)
}
httpClient := NewHTTPClient(gateway.transport, NewRequestDumper(gateway.logger))
httpClient.DumpRequest(request)
for i := 0; i < 3; i++ {
response, err = httpClient.Do(request)
if response == nil && err != nil {
continue
} else {
break
}
}
if err != nil {
return response, err
}
httpClient.DumpResponse(response)
rawWarnings := strings.Split(response.Header.Get("X-Cf-Warnings"), ",")
for _, rawWarning := range rawWarnings {
if rawWarning == "" {
continue
}
warning, _ := url.QueryUnescape(rawWarning)
*gateway.warnings = append(*gateway.warnings, warning)
}
return response, err
}
func makeHTTPTransport(gateway *Gateway) {
gateway.transport = &http.Transport{
DisableKeepAlives: true,
Dial: (&net.Dialer{
KeepAlive: 30 * time.Second,
Timeout: gateway.DialTimeout,
}).Dial,
TLSClientConfig: NewTLSConfig(gateway.trustedCerts, gateway.config.IsSSLDisabled()),
Proxy: http.ProxyFromEnvironment,
}
}
func dialTimeout(envDialTimeout string) time.Duration {
dialTimeout := DefaultDialTimeout
if timeout, err := strconv.Atoi(envDialTimeout); err == nil {
dialTimeout = time.Duration(timeout) * time.Second
}
return dialTimeout
}
func (gateway *Gateway) SetTrustedCerts(certificates []tls.Certificate) {
gateway.trustedCerts = certificates
makeHTTPTransport(gateway)
}