forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
352 lines (323 loc) · 11.3 KB
/
http.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
package tcclient
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
// "net/http/httputil"
"net/url"
"reflect"
"time"
"github.com/cenkalti/backoff/v3"
"github.com/taskcluster/httpbackoff/v3"
tcurls "github.com/taskcluster/taskcluster-lib-urls"
hawk "github.com/tent/hawk-go"
)
var debug = false
func init() {
if _, ok := os.LookupEnv("TASKCLUSTER_DEBUG"); ok {
debug = true
}
}
var defaultBackoff httpbackoff.Client = httpbackoff.Client{
BackOffSettings: backoff.NewExponentialBackOff(),
}
// CallSummary provides information about the underlying http request and
// response issued for a given API call.
type CallSummary struct {
HTTPRequest *http.Request
// Keep a copy of request body in addition to the *http.Request, since
// accessing the Body via the *http.Request object, you get a io.ReadCloser
// - and after the request has been made, the body will have been read, and
// the data lost... This way, it is still available after the api call
// returns.
HTTPRequestBody string
// The Go Type which is marshaled into json and used as the http request
// body.
HTTPRequestObject interface{}
HTTPResponse *http.Response
// Keep a copy of response body in addition to the *http.Response, since
// accessing the Body via the *http.Response object, you get a
// io.ReadCloser - and after the response has been read once (to unmarshal
// json into native go types) the data is lost... This way, it is still
// available after the api call returns.
HTTPResponseBody string
// Keep a record of how many http requests were attempted
Attempts int
}
func (cs *CallSummary) String() string {
s := "\nCALL SUMMARY\n============\n"
if req := cs.HTTPRequest; req != nil {
s += fmt.Sprintf("Method: %v\n", req.Method)
if debug {
if req.URL != nil {
s += fmt.Sprintf("URL: %v\n", req.URL)
}
s += fmt.Sprintf("Request Headers:\n%#v\n", req.Header)
s += fmt.Sprintf("Request Body:\n%v\n", cs.HTTPRequestBody)
if resp := cs.HTTPResponse; resp != nil {
s += fmt.Sprintf("Response Headers:\n%#v\n", cs.HTTPResponse.Header)
}
} else {
if req.URL != nil {
s += fmt.Sprintf("Service: %s:%s\n", req.URL.Hostname(), req.URL.Port())
}
}
}
s += fmt.Sprintf("Response Body:\n%v\n", cs.HTTPResponseBody)
s += fmt.Sprintf("Attempts: %v", cs.Attempts)
return s
}
type APICall struct {
Client *Client
Route string
QueryString url.Values
Payload io.Reader
}
// ReducedHTTPClient is the interface that wraps the functionality of
// http.Client that we actually use in Client.APICall.
type ReducedHTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// defaultHTTPClient is the HTTP Client used to make requests if none are
// defined in the client.
// A single object is created and used because http.Client is thread-safe when
// making multiple requests in various goroutines.
var defaultHTTPClient ReducedHTTPClient = &http.Client{
// do not follow redirects
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
// utility function to create a URL object based on given data
func setURL(client *Client, route string, query url.Values) (u *url.URL, err error) {
URL := tcurls.API(client.RootURL, client.ServiceName, client.APIVersion, route)
u, err = url.Parse(URL)
if err != nil {
return nil, fmt.Errorf("Cannot parse url: '%v', is RootURL (%v) set correctly?\n%v\n", URL, client.RootURL, err)
}
if query != nil {
u.RawQuery = query.Encode()
}
return
}
// Request is the underlying method that makes a raw API request, without
// performing any json marshaling/unmarshaling of requests/responses. It is
// useful if you wish to handle raw payloads and/or raw http response bodies,
// rather than calling APICall which translates []byte to/from go types.
func (client *Client) Request(rawPayload []byte, method, route string, query url.Values) (*CallSummary, error) {
callSummary := new(CallSummary)
callSummary.HTTPRequestBody = string(rawPayload)
// function to perform http request - we call this using backoff library to
// have exponential backoff in case of intermittent failures (e.g. network
// blips or HTTP 5xx errors)
httpCall := func() (*http.Response, error, error) {
ioReader := bytes.NewReader(rawPayload)
u, err := setURL(client, route, query)
if err != nil {
return nil, nil, fmt.Errorf("apiCall url cannot be parsed:\n%v\n", err)
}
callSummary.HTTPRequest, err = http.NewRequest(method, u.String(), ioReader)
if err != nil {
return nil, nil, fmt.Errorf("Internal error: apiCall url cannot be parsed although thought to be valid: '%v', is the RootURL (%v) set correctly?\n%v\n", u.String(), client.RootURL, err)
}
if len(rawPayload) > 0 {
callSummary.HTTPRequest.Header.Set("Content-Type", "application/json")
}
// Refresh Authorization header with each call...
// Only authenticate if client library user wishes to.
if client.Authenticate {
err = client.Credentials.SignRequest(callSummary.HTTPRequest)
if err != nil {
return nil, nil, err
}
}
// Set context if one is given
if client.Context != nil {
callSummary.HTTPRequest = callSummary.HTTPRequest.WithContext(client.Context)
}
var resp *http.Response
if client.HTTPClient != nil {
resp, err = client.HTTPClient.Do(callSummary.HTTPRequest)
} else {
resp, err = defaultHTTPClient.Do(callSummary.HTTPRequest)
}
// return cancelled error, if context was cancelled
if client.Context != nil && client.Context.Err() != nil {
return nil, nil, client.Context.Err()
}
// b, e := httputil.DumpResponse(resp, true)
// if e == nil {
// fmt.Println(string(b))
// }
return resp, err, nil
}
// Make HTTP API calls using an exponential backoff algorithm...
var err error
callSummary.HTTPResponse, callSummary.Attempts, err = defaultBackoff.Retry(httpCall)
// read response into memory, so that we can return the body
if callSummary.HTTPResponse != nil {
body, err2 := ioutil.ReadAll(callSummary.HTTPResponse.Body)
if err2 == nil {
callSummary.HTTPResponseBody = string(body)
}
}
return callSummary, err
}
// SignRequest will add an Authorization header
func (c *Credentials) SignRequest(req *http.Request) (err error) {
// s, err := c.SignHeader(req.Method, req.URL.String(), hash)
// req.Header.Set("Authorization", s)
// return err
credentials := &hawk.Credentials{
ID: c.ClientID,
Key: c.AccessToken,
Hash: sha256.New,
}
reqAuth := hawk.NewRequestAuth(req, credentials, 0)
reqAuth.Ext, err = getExtHeader(c)
if err != nil {
return fmt.Errorf("Internal error: was not able to generate hawk ext header from provided credentials:\n%s\n%s", c, err)
}
req.Header.Set("Authorization", reqAuth.RequestHeader())
return nil
}
type APICallException struct {
CallSummary *CallSummary
RootCause error
}
func (err *APICallException) Error() string {
return err.CallSummary.String() + "\n" + err.RootCause.Error()
}
// APICall is the generic REST API calling method which performs all REST API
// calls for this library. Each auto-generated REST API method simply is a
// wrapper around this method, calling it with specific specific arguments.
func (client *Client) APICall(payload interface{}, method, route string, result interface{}, query url.Values) (interface{}, *CallSummary, error) {
rawPayload := []byte{}
var err error
if reflect.ValueOf(payload).IsValid() && !reflect.ValueOf(payload).IsNil() {
rawPayload, err = json.Marshal(payload)
if err != nil {
cs := &CallSummary{
HTTPRequestObject: payload,
}
return result,
cs,
&APICallException{
CallSummary: cs,
RootCause: err,
}
}
}
callSummary, err := client.Request(rawPayload, method, route, query)
callSummary.HTTPRequestObject = payload
if err != nil {
// If context failed during this request, then we should just return that error
if client.Context != nil && client.Context.Err() != nil {
return result, callSummary, client.Context.Err()
}
return result,
callSummary,
&APICallException{
CallSummary: callSummary,
RootCause: err,
}
}
// if result is passed in as nil, it means the API defines no response body
// json
if reflect.ValueOf(result).IsValid() && !reflect.ValueOf(result).IsNil() {
err = json.Unmarshal([]byte(callSummary.HTTPResponseBody), &result)
}
if err != nil {
return result,
callSummary,
&APICallException{
CallSummary: callSummary,
RootCause: err,
}
}
return result, callSummary, nil
}
// SignedURL creates a signed URL using the given Client, where route is
// either a url path relative to `<RootURL>/api/<serviceName>/<apiVersion>` or
// a fully qualified URL. query is the set of query string parameters, if any,
// and duration is the amount of time that the signed URL should remain valid
// for. The full-URL form permits signing URLs that are not even on the given
// RootURL.
func (client *Client) SignedURL(route string, query url.Values, duration time.Duration) (u *url.URL, err error) {
u, err = url.Parse(route)
if err != nil || u.Host == "" {
u, err = setURL(client, route, query)
if err != nil {
return
}
}
credentials := &hawk.Credentials{
ID: client.Credentials.ClientID,
Key: client.Credentials.AccessToken,
Hash: sha256.New,
}
reqAuth, err := hawk.NewURLAuth(u.String(), credentials, duration)
if err != nil {
return
}
reqAuth.Ext, err = getExtHeader(client.Credentials)
if err != nil {
return
}
bewitSignature := reqAuth.Bewit()
if query == nil {
query = url.Values{}
}
query.Set("bewit", bewitSignature)
u.RawQuery = query.Encode()
return
}
// getExtHeader generates the hawk ext header based on the authorizedScopes and
// the certificate used in the case of temporary credentials. The header is a
// base64 encoded json object with a "certificate" property set to the
// certificate of the temporary credentials and a "authorizedScopes" property
// set to the array of authorizedScopes, if provided. If either "certificate"
// or "authorizedScopes" is not supplied, they will be omitted from the json
// result. If neither are provided, an empty string is returned, rather than a
// base64 encoded representation of "null" or "{}". Hawk interprets the empty
// string as meaning the ext header is not needed.
//
// See:
// * https://docs.taskcluster.net/docs/manual/design/apis/hawk/authorized-scopes
// * https://docs.taskcluster.net/docs/manual/design/apis/hawk/temporary-credentials
func getExtHeader(credentials *Credentials) (header string, err error) {
ext := &ExtHeader{}
if credentials.Certificate != "" {
certObj := new(Certificate)
err = json.Unmarshal([]byte(credentials.Certificate), certObj)
if err != nil {
return "", err
}
ext.Certificate = certObj
}
if credentials.AuthorizedScopes != nil {
ext.AuthorizedScopes = &credentials.AuthorizedScopes
}
extJSON, err := json.Marshal(ext)
if err != nil {
return "", err
}
if string(extJSON) != "{}" {
return base64.StdEncoding.EncodeToString(extJSON), nil
}
return "", nil
}
// ExtHeader represents the authentication/authorization data that is contained
// in the ext field inside the base64 decoded `Authorization` HTTP header in
// outgoing Hawk HTTP requests.
type ExtHeader struct {
Certificate *Certificate `json:"certificate,omitempty"`
// use pointer to slice to distinguish between nil slice and empty slice
AuthorizedScopes *[]string `json:"authorizedScopes,omitempty"`
}