-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.go
executable file
·762 lines (655 loc) · 19.7 KB
/
api.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
Copyright (c) 2019 Dell Inc, or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
log "github.com/akutz/gournal"
"github.com/sirupsen/logrus"
"github.com/PuerkitoBio/goquery"
"github.com/dell/goisilon/api/json"
)
const (
headerKeyContentType = "Content-Type"
headerValContentTypeJSON = "application/json"
headerValContentTypeBinaryOctetStream = "binary/octet-stream"
headerKeyContentLength = "Content-Length"
defaultVolumesPath = "/ifs/volumes"
defaultVolumesPathPermissions = "0777"
defaultIgnoreUnresolvableHosts = false
headerISISessToken = "Cookie"
headerISICSRFToken = "X-CSRF-Token" // #nosec, G101 False positive since no hard-coded CSRF token
headerISIReferer = "Referer"
isiSessCsrfToken = "Set-Cookie"
authTypeBasic = 0
authTypeSessionBased = 1
)
var (
debug, _ = strconv.ParseBool(os.Getenv("GOISILON_DEBUG"))
errNewClient = errors.New("missing endpoint, username, or password")
)
// Client is an API client.
type Client interface {
// Do sends an HTTP request to the OneFS API.
Do(
ctx context.Context,
method, path, id string,
params OrderedValues,
body, resp interface{}) error
// DoWithHeaders sends an HTTP request to the OneFS API.
DoWithHeaders(
ctx context.Context,
method, path, id string,
params OrderedValues, headers map[string]string,
body, resp interface{}) error
// Get sends an HTTP request using the GET method to the OneFS API.
Get(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
resp interface{}) error
// Post sends an HTTP request using the POST method to the OneFS API.
Post(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
body, resp interface{}) error
// Put sends an HTTP request using the PUT method to the OneFS API.
Put(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
body, resp interface{}) error
// Delete sends an HTTP request using the DELETE method to the OneFS API.
Delete(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
resp interface{}) error
// APIVersion returns the API version.
APIVersion() uint8
// User returns the user name used to access the OneFS API.
User() string
// Group returns the group name used to access the OneFS API.
Group() string
// VolumesPath returns the client's configured volumes path.
VolumesPath() string
// VolumePath returns the path to a volume with the provided name.
VolumePath(name string) string
// SetAuthToken sets the Auth token/Cookie for the HTTP client
SetAuthToken(token string)
// SetCSRFToken sets the Auth token for the HTTP client
SetCSRFToken(csrf string)
// SetReferer sets the Referer header
SetReferer(referer string)
// GetAuthToken gets the Auth token/Cookie for the HTTP client
GetAuthToken() string
// GetCSRFToken gets the CSRF token for the HTTP client
GetCSRFToken() string
// GetReferer gets the Referer header
GetReferer() string
}
type client struct {
http *http.Client
hostname string
username string
groupname string
password string
volumePath string
volumePathPermissions string
ignoreUnresolvableHosts bool
apiVersion uint8
apiMinorVersion uint8
verboseLogging VerboseType
sessionCredentials session
authType uint8
}
type session struct {
sessionCookies string
sessionCSRF string
referer string
}
type setupConnection struct {
Services []string `json:"services"`
Username string `json:"username"`
Password string `json:"password"`
}
type VerboseType uint
const (
Verbose_High VerboseType = 0
Verbose_Medium VerboseType = 1
Verbose_Low VerboseType = 2
)
type apiVerResponse struct {
Latest *string `json:"latest"`
}
// Error is an API error.
type Error struct {
Code string `json:"code"`
Field string `json:"field"`
Message string `json:"message"`
}
// JSONError is a JSON response with one or more errors.
type JSONError struct {
StatusCode int
Err []Error `json:"errors"`
}
// HTMLError is an HTML response with one or more errors.
type HTMLError struct {
StatusCode int
Message string
}
// ClientOptions are options for the API client.
type ClientOptions struct {
// Insecure is a flag that indicates whether or not to supress SSL errors.
Insecure bool
// VolumesPath is the location on the Isilon server where volumes are
// stored.
VolumesPath string
// VolumesPathPermissions is the directory permissions for VolumesPath
VolumesPathPermissions string
// IgnoreUnresolvableHosts is the unresolvable hosts param from platform
IgnoreUnresolvableHosts bool
// Timeout specifies a time limit for requests made by this client.
Timeout time.Duration
}
// New returns a new API client.
func New(
ctx context.Context,
hostname, username, password, groupname string,
verboseLogging uint, authType uint8,
opts *ClientOptions,
) (Client, error) {
if hostname == "" || username == "" || password == "" {
return nil, errNewClient
}
if authType != authTypeBasic && authType != authTypeSessionBased {
log.Warn(ctx, "AuthType can be 0 or 1. Setting it to default value 0")
authType = authTypeBasic
}
c := &client{
hostname: hostname,
username: username,
groupname: groupname,
password: password,
volumePath: defaultVolumesPath,
volumePathPermissions: defaultVolumesPathPermissions,
ignoreUnresolvableHosts: defaultIgnoreUnresolvableHosts,
verboseLogging: VerboseType(verboseLogging),
authType: authType,
}
c.http = &http.Client{}
if opts != nil {
if opts.VolumesPath != "" {
c.volumePath = opts.VolumesPath
}
if opts.VolumesPathPermissions != "" {
c.volumePathPermissions = opts.VolumesPathPermissions
}
if opts.IgnoreUnresolvableHosts {
c.ignoreUnresolvableHosts = opts.IgnoreUnresolvableHosts
}
if opts.Timeout != 0 {
c.http.Timeout = opts.Timeout
}
log.Debug(ctx, "opts.Insecure : '%v'", opts.Insecure)
if opts.Insecure {
c.http.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // #nosec,G402
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
CipherSuites: GetSecuredCipherSuites(),
},
}
} else {
pool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
c.http.Transport = &http.Transport{
TLSClientConfig: &tls.Config{ //nolint:gosec,G402
RootCAs: pool,
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
CipherSuites: GetSecuredCipherSuites(),
},
}
}
}
if c.authType == authTypeSessionBased {
_ = c.authenticate(ctx, username, password, hostname)
}
resp := &apiVerResponse{}
if err := c.Get(ctx, "/platform/latest", "", nil, nil, resp); err != nil &&
!strings.HasPrefix(err.Error(), "json: ") {
return nil, err
}
if resp.Latest != nil {
s := *resp.Latest
c.apiMinorVersion = 0
if i := strings.Index(s, "."); i != -1 {
ms := s[i+1:]
m, err := strconv.ParseUint(ms, 10, 8)
if err != nil {
return nil, err
}
c.apiMinorVersion = uint8(m)
s = s[:i]
}
i, err := strconv.ParseUint(s, 10, 8)
if err != nil {
return nil, err
}
c.apiVersion = uint8(i)
} else {
c.apiVersion = 2
}
if c.apiVersion < 3 {
return nil, errors.New("OneFS releases older than 8.0 are no longer supported")
}
return c, nil
}
func (c *client) Get(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
resp interface{},
) error {
return c.executeWithRetryAuthenticate(
ctx, http.MethodGet, path, id, params, headers, nil, resp)
}
func (c *client) Post(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
body, resp interface{},
) error {
return c.executeWithRetryAuthenticate(
ctx, http.MethodPost, path, id, params, headers, body, resp)
}
func (c *client) Put(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
body, resp interface{},
) error {
return c.executeWithRetryAuthenticate(
ctx, http.MethodPut, path, id, params, headers, body, resp)
}
func (c *client) Delete(
ctx context.Context,
path, id string,
params OrderedValues, headers map[string]string,
resp interface{},
) error {
return c.executeWithRetryAuthenticate(
ctx, http.MethodDelete, path, id, params, headers, nil, resp)
}
func (c *client) Do(
ctx context.Context,
method, path, id string,
params OrderedValues,
body, resp interface{},
) error {
return c.executeWithRetryAuthenticate(ctx, method, path, id, params, nil, body, resp)
}
func beginsWithSlash(s string) bool {
return s[0] == '/'
}
func endsWithSlash(s string) bool {
return s[len(s)-1] == '/'
}
func (c *client) DoWithHeaders(
ctx context.Context,
method, uri, id string,
params OrderedValues, headers map[string]string,
body, resp interface{},
) error {
res, _, err := c.DoAndGetResponseBody(
ctx, method, uri, id, params, headers, body)
if err != nil {
return err
}
defer func() {
if err := res.Body.Close(); err != nil {
logrus.Printf("Error closing HTTP response: %s", err.Error())
}
}()
logResponse(ctx, res, c.verboseLogging)
// parse the response
switch {
case res == nil:
return nil
case res.StatusCode >= 200 && res.StatusCode <= 299:
if resp == nil {
return nil
}
dec := json.NewDecoder(res.Body)
if err = dec.Decode(resp); err != nil && err != io.EOF {
return err
}
default:
return parseJSONHTMLError(res)
}
return nil
}
func (c *client) DoAndGetResponseBody(
ctx context.Context,
method, uri, id string,
params OrderedValues, headers map[string]string,
body interface{},
) (*http.Response, bool, error) {
var (
err error
req *http.Request
res *http.Response
ubf = &bytes.Buffer{}
lid = len(id)
luri = len(uri)
hostnameEndsWithSlash = endsWithSlash(c.hostname)
uriBeginsWithSlash = beginsWithSlash(uri)
uriEndsWithSlash = endsWithSlash(uri)
)
ubf.WriteString(c.hostname)
if !hostnameEndsWithSlash && (luri > 0 || lid > 0) {
ubf.WriteString("/")
}
if luri > 0 {
if uriBeginsWithSlash {
ubf.WriteString(uri[1:])
} else {
ubf.WriteString(uri)
}
if !uriEndsWithSlash {
ubf.WriteString("/")
}
}
if lid > 0 {
ubf.WriteString(id)
}
// add parameters to the URI
if len(params) > 0 {
ubf.WriteByte('?')
if err := params.EncodeTo(ubf); err != nil {
return nil, false, err
}
}
u, err := url.Parse(ubf.String())
if err != nil {
return nil, false, err
}
var isContentTypeSet bool
// marshal the message body (assumes json format)
if body != nil {
if r, ok := body.(io.ReadCloser); ok {
req, err = http.NewRequest(method, u.String(), r)
defer func() {
if err := r.Close(); err != nil {
logrus.Printf("Error closing HTTP response: %s", err.Error())
}
}()
if v, ok := headers[headerKeyContentType]; ok {
req.Header.Set(headerKeyContentType, v)
} else {
req.Header.Set(
headerKeyContentType, headerValContentTypeBinaryOctetStream)
}
isContentTypeSet = true
// Avoid chunked encoding
if _, ok := headers[headerKeyContentLength]; ok {
req.TransferEncoding = []string{"native"}
}
} else {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
if err = enc.Encode(body); err != nil {
return nil, false, err
}
req, err = http.NewRequest(method, u.String(), buf)
if v, ok := headers[headerKeyContentType]; ok {
req.Header.Set(headerKeyContentType, v)
} else {
req.Header.Set(headerKeyContentType, headerValContentTypeJSON)
}
isContentTypeSet = true
}
} else {
req, err = http.NewRequest(method, u.String(), nil)
}
if err != nil {
return nil, false, err
}
if !isContentTypeSet {
isContentTypeSet = req.Header.Get(headerKeyContentType) != ""
}
// add headers to the request
if len(headers) > 0 {
for header, value := range headers {
if header == headerKeyContentType && isContentTypeSet {
continue
}
req.Header.Add(header, value)
}
}
if c.authType == authTypeBasic {
req.SetBasicAuth(c.username, c.password)
} else {
if c.GetAuthToken() != "" {
req.Header.Set(headerISISessToken, c.GetAuthToken())
req.Header.Set(headerISIReferer, c.GetReferer())
req.Header.Set(headerISICSRFToken, c.GetCSRFToken())
}
}
var (
isDebugLog bool
logReqBuf = &bytes.Buffer{}
)
if lvl, ok := ctx.Value(
log.LevelKey()).(log.Level); ok && lvl >= log.DebugLevel {
isDebugLog = true
}
logRequest(ctx, logReqBuf, req, c.verboseLogging)
log.Debug(ctx, logReqBuf.String())
// send the request
req = req.WithContext(ctx)
if res, err = c.http.Do(req); err != nil {
return nil, isDebugLog, err
}
return res, isDebugLog, err
}
func (c *client) APIVersion() uint8 {
return c.apiVersion
}
func (c *client) User() string {
return c.username
}
func (c *client) Group() string {
return c.groupname
}
func (c *client) VolumesPath() string {
return c.volumePath
}
func (c *client) VolumePath(volumeName string) string {
return path.Join(c.volumePath, volumeName)
}
func (err *JSONError) Error() string {
return err.Err[0].Message
}
func (err *HTMLError) Error() string {
return err.Message
}
func (c *client) SetAuthToken(cookie string) {
c.sessionCredentials.sessionCookies = cookie
}
func (c *client) SetCSRFToken(csrf string) {
c.sessionCredentials.sessionCSRF = csrf
}
func (c *client) SetReferer(referer string) {
c.sessionCredentials.referer = referer
}
func (c *client) GetAuthToken() string {
return c.sessionCredentials.sessionCookies
}
func (c *client) GetCSRFToken() string {
return c.sessionCredentials.sessionCSRF
}
func (c *client) GetReferer() string {
return c.sessionCredentials.referer
}
func parseJSONHTMLError(r *http.Response) error {
// check the content type of the response
if r.Header.Get("Content-Type") == "text/html" {
htmlError := &HTMLError{}
// decode HTML error response
doc, err := goquery.NewDocumentFromReader(r.Body)
if err != nil {
return err
}
htmlError.StatusCode = r.StatusCode
if h1 := doc.Find("h1"); h1 != nil {
htmlError.Message = h1.Text()
}
if strings.TrimSpace(htmlError.Message) == "" && doc.Find("title") != nil {
htmlError.Message = doc.Find("title").Text()
}
return htmlError
}
jsonErr := &JSONError{}
// decode JSON error response
err := json.NewDecoder(r.Body).Decode(jsonErr)
if err != nil {
return err
}
jsonErr.StatusCode = r.StatusCode
if len(jsonErr.Err) > 0 && jsonErr.Err[0].Message == "" {
jsonErr.Err[0].Message = r.Status
}
return jsonErr
}
// Authenticate make a REST API call [/session/1/session] to PowerScale to authenticate the given credentials.
// The response contains the session Cookie, X-CSRF-Token and the client uses it for further communication.
func (c *client) authenticate(ctx context.Context, username string, password string, endpoint string) error {
headers := make(map[string]string, 1)
headers[headerKeyContentType] = headerValContentTypeJSON
data := &setupConnection{Services: []string{"platform", "namespace"}, Username: username, Password: password}
resp, _, err := c.DoAndGetResponseBody(ctx, http.MethodPost, "/session/1/session", "", nil, headers, data)
if err != nil {
return fmt.Errorf("Authentication error: %v", err)
}
if resp != nil {
log.Debug(ctx, "Authentication response code: %d", resp.StatusCode)
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.Printf("Error closing HTTP response: %s", err.Error())
}
}()
switch {
case resp.StatusCode == 201:
{
log.Debug(ctx, "Authentication successful")
}
case resp.StatusCode == 401:
{
log.Debug(ctx, "Response Code %v", resp)
return fmt.Errorf("authentication failed. unable to login to powerscale. verify username and password")
}
default:
return fmt.Errorf("authenticate error. response-")
}
headerRes := strings.Join(resp.Header.Values(isiSessCsrfToken), " ")
startIndex, endIndex, matchStrLen := FetchValueIndexForKey(headerRes, "isisessid=", ";")
if startIndex < 0 || endIndex < 0 {
return fmt.Errorf("Session ID not retrieved")
}
c.SetAuthToken(headerRes[startIndex : startIndex+matchStrLen+endIndex])
startIndex, endIndex, matchStrLen = FetchValueIndexForKey(headerRes, "isicsrf=", ";")
if startIndex < 0 || endIndex < 0 {
log.Warn(ctx, "Anti-CSRF Token not retrieved")
} else {
c.SetCSRFToken(headerRes[startIndex+matchStrLen : startIndex+matchStrLen+endIndex])
}
c.SetReferer(endpoint)
} else {
log.Error(ctx, "Authenticate error: Nil response received")
}
return nil
}
// executeWithRetryAuthenticate re-authenticates when session credentials become invalid due to time-out or requests exceed.
// it retries the same operation after performing authentication.
func (c *client) executeWithRetryAuthenticate(ctx context.Context, method, uri string, id string, params OrderedValues, headers map[string]string, body, resp interface{}) error {
err := c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
if c.authType == authTypeBasic {
return err
}
if err == nil {
log.Debug(ctx, "Execution successful on Method: %v, URI: %v", method, uri)
return nil
}
switch e := err.(type) {
case *JSONError:
if e.StatusCode == 401 {
log.Debug(ctx, "Authentication failed. Trying to re-authenticate")
if err := c.authenticate(ctx, c.username, c.password, c.hostname); err != nil {
return fmt.Errorf("authentication failure due to: %v", err)
}
return c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
} else {
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v JSON Error: %+v", method, uri, err, e)
}
case *HTMLError:
if e.StatusCode == 401 {
log.Debug(ctx, "Authentication failed. Trying to re-authenticate")
if err := c.authenticate(ctx, c.username, c.password, c.hostname); err != nil {
return fmt.Errorf("authentication failure due to: %v", err)
}
return c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
} else {
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v HTML Error: %+v", method, uri, err, e)
}
default:
log.Error(ctx, "Error is not a type of \"*JSONError or *HTMLError\". Error:", err)
}
return err
}
func FetchValueIndexForKey(l string, match string, sep string) (int, int, int) {
startIndex, endIndex := -1, -1
if strings.Contains(l, match) {
startIndex = strings.Index(l, match)
if startIndex != -1 && sep != "" {
endIndex = strings.Index(l[startIndex+len(match):], sep)
}
}
return startIndex, endIndex, len(match)
}
// GetSecuredCipherSuites returns a set of secure cipher suites.
func GetSecuredCipherSuites() (suites []uint16) {
securedSuite := tls.CipherSuites()
for _, v := range securedSuite {
suites = append(suites, v.ID)
}
return suites
}