forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
604 lines (527 loc) · 16.9 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
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
package chef
import (
"bytes"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"path"
"strings"
"time"
)
// ChefVersion that we pretend to emulate
const ChefVersion = "14.0.0"
// Body wraps io.Reader and adds methods for calculating hashes and detecting content
type Body struct {
io.Reader
}
// AuthConfig representing a client and a private key used for encryption
// This is embedded in the Client type
type AuthConfig struct {
PrivateKey *rsa.PrivateKey
ClientName string
AuthenticationVersion string
}
// Client is vessel for public methods used against the chef-server
type Client struct {
Auth *AuthConfig
BaseURL *url.URL
client *http.Client
ACLs *ACLService
Associations *AssociationService
AuthenticateUser *AuthenticateUserService
Clients *ApiClientService
Containers *ContainerService
CookbookArtifacts *CBAService
Cookbooks *CookbookService
DataBags *DataBagService
Environments *EnvironmentService
Groups *GroupService
License *LicenseService
Nodes *NodeService
Organizations *OrganizationService
Policies *PolicyService
PolicyGroups *PolicyGroupService
Principals *PrincipalService
RequiredRecipe *RequiredRecipeService
Roles *RoleService
Sandboxes *SandboxService
Search *SearchService
Stats *StatsService
Status *StatusService
Universe *UniverseService
UpdatedSince *UpdatedSinceService
Users *UserService
}
// Config contains the configuration options for a chef client. This structure is used primarily in the NewClient() constructor in order to setup a proper client object
type Config struct {
// This should be the user ID on the chef server
Name string
// This is the plain text private Key for the user
Key string
// BaseURL is the chef server URL used to connect to. If using orgs you should include your org in the url and terminate the url with a "/"
BaseURL string
// When set to false (default) this will enable SSL Cert Verification. If you need to disable Cert Verification set to true
SkipSSL bool
// RootCAs is a reference to x509.CertPool for TLS
RootCAs *x509.CertPool
// Time to wait in seconds before giving up on a request to the server
Timeout int
// Authentication Protocol Version
AuthenticationVersion string
}
/*
An ErrorResponse reports one or more errors caused by an API request.
Thanks to https://github.com/google/go-github
The Response structure includes:
Status string
StatusCode int
*/
type ErrorResponse struct {
Response *http.Response // HTTP response that caused this error
// extracted error message converted to string if possible
ErrorMsg string
// json body raw byte stream from an error
ErrorText []byte
}
type ErrorMsg struct {
Error interface{} `json:"error"`
}
// Buffer creates a byte.Buffer copy from a io.Reader resets read on reader to 0,0
func (body *Body) Buffer() *bytes.Buffer {
var b bytes.Buffer
if body.Reader == nil {
return &b
}
b.ReadFrom(body.Reader)
_, err := body.Reader.(io.Seeker).Seek(0, 0)
if err != nil {
log.Fatal(err)
}
return &b
}
// Hash calculates the body content hash
func (body *Body) Hash() (h string) {
b := body.Buffer()
// empty buffs should return a empty string
if b.Len() == 0 {
h = HashStr("")
}
h = HashStr(b.String())
return
}
// Hash256 calculates the body content hash
func (body *Body) Hash256() (h string) {
b := body.Buffer()
// empty buffs should return a empty string
if b.Len() == 0 {
h = HashStr256("")
}
h = HashStr256(b.String())
return
}
// ContentType returns the content-type string of Body as detected by http.DetectContentType()
func (body *Body) ContentType() string {
if json.Unmarshal(body.Buffer().Bytes(), &struct{}{}) == nil {
return "application/json"
}
return http.DetectContentType(body.Buffer().Bytes())
}
// Error implements the error interface method for ErrorResponse
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d",
r.Response.Request.Method, r.Response.Request.URL,
r.Response.StatusCode)
}
// StatusCode returns the status code from the http response embedded in the ErrorResponse
func (r *ErrorResponse) StatusCode() int {
return r.Response.StatusCode
}
// StatusMsg returns the error msg string from the http response. The message is a best
// effort value and depends on the Chef Server json return format
func (r *ErrorResponse) StatusMsg() string {
return r.ErrorMsg
}
// StatusText returns the raw json response included in the http response
func (r *ErrorResponse) StatusText() []byte {
return r.ErrorText
}
// StatusMethod returns the method used from the http response embedded in the ErrorResponse
func (r *ErrorResponse) StatusMethod() string {
return r.Response.Request.Method
}
// StatusURL returns the URL used from the http response embedded in the ErrorResponse
func (r *ErrorResponse) StatusURL() *url.URL {
return r.Response.Request.URL
}
// NewClient is the client generator used to instantiate a client for talking to a chef-server
// It is a simple constructor for the Client struct intended as a easy interface for issuing
// signed requests
func NewClient(cfg *Config) (*Client, error) {
// Verify Config settings
// Authentication version = 1.0 or 1.3, default to 1.0
cfg.VerifyVersion()
pk, err := PrivateKeyFromString([]byte(cfg.Key))
if err != nil {
return nil, err
}
baseUrl, _ := url.Parse(cfg.BaseURL)
tlsConfig := &tls.Config{InsecureSkipVerify: cfg.SkipSSL}
if cfg.RootCAs != nil {
tlsConfig.RootCAs = cfg.RootCAs
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: tlsConfig,
TLSHandshakeTimeout: 10 * time.Second,
}
c := &Client{
Auth: &AuthConfig{
PrivateKey: pk,
ClientName: cfg.Name,
AuthenticationVersion: cfg.AuthenticationVersion,
},
client: &http.Client{
Transport: tr,
Timeout: time.Duration(cfg.Timeout) * time.Second,
},
BaseURL: baseUrl,
}
c.ACLs = &ACLService{client: c}
c.AuthenticateUser = &AuthenticateUserService{client: c}
c.Associations = &AssociationService{client: c}
c.Clients = &ApiClientService{client: c}
c.Containers = &ContainerService{client: c}
c.Cookbooks = &CookbookService{client: c}
c.CookbookArtifacts = &CBAService{client: c}
c.DataBags = &DataBagService{client: c}
c.Environments = &EnvironmentService{client: c}
c.Groups = &GroupService{client: c}
c.License = &LicenseService{client: c}
c.Nodes = &NodeService{client: c}
c.Organizations = &OrganizationService{client: c}
c.Policies = &PolicyService{client: c}
c.PolicyGroups = &PolicyGroupService{client: c}
c.RequiredRecipe = &RequiredRecipeService{client: c}
c.Principals = &PrincipalService{client: c}
c.Roles = &RoleService{client: c}
c.Sandboxes = &SandboxService{client: c}
c.Search = &SearchService{client: c}
c.Stats = &StatsService{client: c}
c.Status = &StatusService{client: c}
c.UpdatedSince = &UpdatedSinceService{client: c}
c.Universe = &UniverseService{client: c}
c.Users = &UserService{client: c}
return c, nil
}
func (cfg *Config) VerifyVersion() (err error) {
if cfg.AuthenticationVersion != "1.3" {
cfg.AuthenticationVersion = "1.0"
}
return
}
// basicRequestDecoder performs a request on an endpoint, and decodes the response into the passed in Type
// basicRequestDecoder is the same code as magic RequestDecoder with the addition of a generated Authentication: Basic header
// to the http request
func (c *Client) basicRequestDecoder(method, path string, body io.Reader, v interface{}, user string, password string) error {
req, err := c.NewRequest(method, path, body)
if err != nil {
return err
}
basicAuthHeader(req, user, password)
debug("\n\nRequest: %+v \n", req)
res, err := c.Do(req, v)
if res != nil {
defer res.Body.Close()
}
debug("Response: %+v\n", res)
if err != nil {
return err
}
return err
}
// magicRequestDecoder performs a request on an endpoint, and decodes the response into the passed in Type
func (c *Client) magicRequestDecoder(method, path string, body io.Reader, v interface{}) error {
req, err := c.NewRequest(method, path, body)
if err != nil {
return err
}
debug("\n\nRequest: %+v \n", req)
res, err := c.Do(req, v)
if res != nil {
defer res.Body.Close()
}
debug("Response: %+v\n", res)
if err != nil {
return err
}
return err
}
// NewRequest returns a signed request suitable for the chef server
func (c *Client) NewRequest(method string, requestUrl string, body io.Reader) (*http.Request, error) {
relativeUrl, err := url.Parse(requestUrl)
if err != nil {
return nil, err
}
u := c.BaseURL.ResolveReference(relativeUrl)
// NewRequest uses a new value object of body
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
// parse and encode Querystring Values
values := req.URL.Query()
req.URL.RawQuery = values.Encode()
debug("Encoded url %+v\n", u)
myBody := &Body{body}
if body != nil {
// Detect Content-type
req.Header.Set("Content-Type", myBody.ContentType())
}
// Calculate the body hash
if c.Auth.AuthenticationVersion == "1.3" {
req.Header.Set("X-Ops-Content-Hash", myBody.Hash256())
} else {
req.Header.Set("X-Ops-Content-Hash", myBody.Hash())
}
err = c.Auth.SignRequest(req)
if err != nil {
return nil, err
}
return req, nil
}
// basicAuth does base64 encoding of a user and password
func basicAuth(user string, password string) string {
creds := user + ":" + password
return base64.StdEncoding.EncodeToString([]byte(creds))
}
// basicAuthHeader adds an Authentication Basic header to the request
// The user and password values should be clear text. They will be
// base64 encoded for the header.
func basicAuthHeader(r *http.Request, user string, password string) {
r.Header.Add("authorization", "Basic "+basicAuth(user, password))
}
// CheckResponse receives a pointer to a http.Response and generates an Error via unmarshalling
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
debug("Response Error Body: %+v\n", string(data))
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
errorResponse.ErrorText = data
errorResponse.ErrorMsg = extractErrorMsg(data)
}
return errorResponse
}
// extractErrorMsg makes a best faith effort to extract the error message text
// from the response body returned from the Chef Server. Error messages are
// typically formatted in a json body as {"error": ["msg"]}
func extractErrorMsg(data []byte) string {
errorMsg := &ErrorMsg{}
json.Unmarshal(data, errorMsg)
switch t := errorMsg.Error.(type) {
case []interface{}:
// Return the string as a byte stream
var rmsg string
for _, val := range t {
switch inval := val.(type) {
case string:
rmsg = rmsg + inval + "\n"
default:
debug("Unknown type %+v data %+v\n", inval, val)
}
return strings.TrimSpace(rmsg)
}
default:
debug("Unknown type %+v data %+v msg %+v\n", t, string(data), errorMsg.Error)
}
return ""
}
// ChefError tries to unwind a chef client err return embedded in an error
// Unwinding allows easy access the StatusCode, StatusMethod and StatusURL functions
func ChefError(err error) (cerr *ErrorResponse, nerr error) {
if err == nil {
return cerr, err
}
if cerr, ok := err.(*ErrorResponse); ok {
return cerr, err
}
return cerr, err
}
// Do is used either internally via our magic request shite or a user may use it
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
// BUG(fujin) tightly coupled
err = CheckResponse(res)
if err != nil {
return res, err
}
var resBuf bytes.Buffer
resTee := io.TeeReader(res.Body, &resBuf)
// no response interface specified
if v == nil {
if debug_on() {
// show the response body as a string
resbody, _ := ioutil.ReadAll(resTee)
debug("Response body: %+v\n", string(resbody))
}
debug("No response body requested\n")
return res, nil
}
// response interface, v, is an io writer
if w, ok := v.(io.Writer); ok {
debug("Response output desired is an io Writer\n")
_, err = io.Copy(w, resTee)
return res, err
}
// response content-type specifies JSON encoded - decode it
if hasJsonContentType(res) {
err = json.NewDecoder(resTee).Decode(v)
if debug_on() {
// show the response body as a string
resbody, _ := ioutil.ReadAll(&resBuf)
debug("Response body: %+v\n", string(resbody))
}
debug("Response body specifies content as JSON: %+v Err:\n", v, err)
if err != nil {
return res, err
}
return res, nil
}
// response interface, v, is type string and the content is plain text
if _, ok := v.(*string); ok && hasTextContentType(res) {
resbody, _ := ioutil.ReadAll(resTee)
if err != nil {
return res, err
}
out := string(resbody)
debug("Response body parsed as string: %+v\n", out)
*v.(*string) = out
return res, nil
}
// Default response: Content-Type is not JSON. Assume v is a struct and decode the response as json
err = json.NewDecoder(resTee).Decode(v)
if debug_on() {
// show the response body as a string
resbody, _ := ioutil.ReadAll(&resBuf)
debug("Response body: %+v\n", string(resbody))
}
debug("Response body defaulted to JSON parsing: %+v Err:\n", v, err)
if err != nil {
return res, err
}
return res, nil
}
func hasJsonContentType(res *http.Response) bool {
contentType := res.Header.Get("Content-Type")
return contentType == "application/json"
}
func hasTextContentType(res *http.Response) bool {
contentType := res.Header.Get("Content-Type")
return contentType == "text/plain"
}
// SignRequest modifies headers of an http.Request
func (ac AuthConfig) SignRequest(request *http.Request) error {
var request_headers []string
var endpoint string
if request.URL.Path != "" {
endpoint = path.Clean(request.URL.Path)
request.URL.Path = endpoint
} else {
endpoint = request.URL.Path
}
vals := map[string]string{
"Method": request.Method,
"Accept": "application/json",
"X-Chef-Version": ChefVersion,
"X-Ops-Server-API-Version": "1",
"X-Ops-Timestamp": time.Now().UTC().Format(time.RFC3339),
"X-Ops-Content-Hash": request.Header.Get("X-Ops-Content-Hash"),
"X-Ops-UserId": ac.ClientName,
}
if ac.AuthenticationVersion == "1.3" {
vals["Path"] = endpoint
vals["X-Ops-Sign"] = "version=1.3"
request_headers = []string{"Method", "Path", "Accept", "X-Chef-Version", "X-Ops-Server-API-Version", "X-Ops-Timestamp", "X-Ops-UserId", "X-Ops-Sign"}
} else {
vals["Hashed Path"] = HashStr(endpoint)
vals["X-Ops-Sign"] = "algorithm=sha1;version=1.0"
request_headers = []string{"Method", "Accept", "X-Chef-Version", "X-Ops-Server-API-Version", "X-Ops-Timestamp", "X-Ops-UserId", "X-Ops-Sign"}
}
// Add the vals to the request
for _, key := range request_headers {
request.Header.Set(key, vals[key])
}
content := ac.SignatureContent(vals)
// generate signed string of headers
var signature []byte
var err error
if ac.AuthenticationVersion == "1.3" {
signature, err = GenerateDigestSignature(ac.PrivateKey, content)
if err != nil {
fmt.Printf("Error from signature %+v\n", err)
return err
}
} else {
signature, err = GenerateSignature(ac.PrivateKey, content)
if err != nil {
return err
}
}
// THIS IS CHEF PROTOCOL SPECIFIC
// Signature is made up of n 60 length chunks
base64sig := Base64BlockEncode(signature, 60)
// roll over the auth slice and add the apropriate header
for index, value := range base64sig {
request.Header.Set(fmt.Sprintf("X-Ops-Authorization-%d", index+1), string(value))
}
return nil
}
func (ac AuthConfig) SignatureContent(vals map[string]string) (content string) {
// sanitize the path for the chef-server
// chef-server doesn't support '//' in the Hash Path.
// The signature is very particular, the exact headers and the order they are included in the signature matter
var signed_headers []string
if ac.AuthenticationVersion == "1.3" {
signed_headers = []string{"Method", "Path", "X-Ops-Content-Hash", "X-Ops-Sign", "X-Ops-Timestamp",
"X-Ops-UserId", "X-Ops-Server-API-Version"}
} else {
signed_headers = []string{"Method", "Hashed Path", "X-Ops-Content-Hash", "X-Ops-Timestamp", "X-Ops-UserId"}
}
for _, key := range signed_headers {
content += fmt.Sprintf("%s:%s\n", key, vals[key])
}
content = strings.TrimSuffix(content, "\n")
return
}
// PrivateKeyFromString parses an RSA private key from a string
func PrivateKeyFromString(key []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, fmt.Errorf("private key block size invalid")
}
rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsaKey, nil
}