-
Notifications
You must be signed in to change notification settings - Fork 29
/
client.go
657 lines (568 loc) · 17.1 KB
/
client.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
package client
import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/ciscoecosystem/aci-go-client/container"
)
const authPayload = `{
"aaaUser" : {
"attributes" : {
"name" : "%s",
"pwd" : "%s"
}
}
}`
// Used authAppPayload to authenticate against the APIC using:
// AppName, App Certificate DN, Signed Request
const authAppPayload = `{
"aaaAppToken" : {
"attributes" : {
"appName" : "%s"
}
}
}`
// Default timeout for NGINX in ACI is 90 Seconds.
// Allow the client to set a shorter or longer time depending on their
// environment
const DefaultReqTimeoutVal int = 100
const DefaultBackoffMinDelay int = 4
const DefaultBackoffMaxDelay int = 60
const DefaultBackoffDelayFactor float64 = 3
const DefaultMOURL = "/api/node/mo"
// Client is the main entry point
type Client struct {
BaseURL *url.URL
MOURL string
httpClient *http.Client
AuthToken *Auth
l sync.Mutex
username string
password string
privatekey string
adminCert string
insecure bool
reqTimeoutSet bool
reqTimeoutVal uint32
proxyUrl string
proxyCreds string
preserveBaseUrlRef bool
skipLoggingPayload bool
appUserName string
ValidateRelationDn bool
maxRetries int
backoffMinDelay int
backoffMaxDelay int
backoffDelayFactor float64
*ServiceManager
}
// singleton implementation of a client
var clientImpl *Client
type Option func(*Client)
func Insecure(insecure bool) Option {
return func(client *Client) {
client.insecure = insecure
}
}
func MoURL(moURL string) Option {
return func(sm *Client) {
sm.MOURL = moURL
}
}
func Password(password string) Option {
return func(client *Client) {
client.password = password
}
}
func PrivateKey(privatekey string) Option {
return func(client *Client) {
client.privatekey = privatekey
}
}
func AdminCert(adminCert string) Option {
return func(client *Client) {
client.adminCert = adminCert
}
}
func AppUserName(appUserName string) Option {
return func(client *Client) {
client.appUserName = appUserName
}
}
func ProxyUrl(pUrl string) Option {
return func(client *Client) {
client.proxyUrl = pUrl
}
}
func ProxyCreds(pcreds string) Option {
return func(client *Client) {
client.proxyCreds = pcreds
}
}
func ValidateRelationDn(validateRelationDn bool) Option {
return func(client *Client) {
client.ValidateRelationDn = validateRelationDn
}
}
func MaxRetries(maxRetries int) Option {
return func(client *Client) {
client.maxRetries = maxRetries
}
}
func BackoffMinDelay(backoffMinDelay int) Option {
return func(client *Client) {
client.backoffMinDelay = backoffMinDelay
}
}
func BackoffMaxDelay(backoffMaxDelay int) Option {
return func(client *Client) {
client.backoffMaxDelay = backoffMaxDelay
}
}
func BackoffDelayFactor(backoffDelayFactor float64) Option {
return func(client *Client) {
client.backoffDelayFactor = backoffDelayFactor
}
}
// HttpClient option: allows for caller to set 'httpClient' with 'Transport'.
// When this option is set 'client.proxyUrl' option is ignored.
func HttpClient(httpcl *http.Client) Option {
return func(client *Client) {
client.httpClient = httpcl
}
}
func SkipLoggingPayload(skipLoggingPayload bool) Option {
return func(client *Client) {
client.skipLoggingPayload = skipLoggingPayload
}
}
func PreserveBaseUrlRef(preserveBaseUrlRef bool) Option {
return func(client *Client) {
client.preserveBaseUrlRef = preserveBaseUrlRef
}
}
func ReqTimeout(timeout uint32) Option {
return func(client *Client) {
client.reqTimeoutSet = true
client.reqTimeoutVal = timeout
}
}
func initClient(clientUrl, username string, options ...Option) *Client {
var transport *http.Transport
bUrl, err := url.Parse(clientUrl)
if err != nil {
// cannot move forward if url is undefined
log.Fatal(err)
}
client := &Client{
BaseURL: bUrl,
username: username,
MOURL: DefaultMOURL,
}
for _, option := range options {
option(client)
}
if client.httpClient == nil {
transport = client.useInsecureHTTPClient(client.insecure)
if client.proxyUrl != "" {
transport = client.configProxy(transport)
}
client.httpClient = &http.Client{
Transport: transport,
}
}
var timeout time.Duration
if client.reqTimeoutSet {
timeout = time.Second * time.Duration(client.reqTimeoutVal)
} else {
timeout = time.Second * time.Duration(DefaultReqTimeoutVal)
}
client.httpClient.Timeout = timeout
client.ServiceManager = NewServiceManager(client.MOURL, client)
return client
}
// GetClient returns a singleton
func GetClient(clientUrl, username string, options ...Option) *Client {
if clientImpl == nil {
clientImpl = initClient(clientUrl, username, options...)
} else {
// making sure it is the same client
bUrl, err := url.Parse(clientUrl)
if err != nil {
// cannot move forward if url is undefined
log.Fatal(err)
}
if bUrl != clientImpl.BaseURL {
clientImpl = initClient(clientUrl, username, options...)
}
}
return clientImpl
}
// NewClient returns a new Instance of the client - allowing for simultaneous connections to the same APIC
func NewClient(clientUrl, username string, options ...Option) *Client {
// making sure it is the same client
_, err := url.Parse(clientUrl)
if err != nil {
// cannot move forward if url is undefined
log.Fatal(err)
}
// initClient always returns a new struct, so always create a new pointer to allow for
// multiple object instances
newClientImpl := initClient(clientUrl, username, options...)
return newClientImpl
}
func (c *Client) configProxy(transport *http.Transport) *http.Transport {
log.Printf("[DEBUG]: Using Proxy Server: %s ", c.proxyUrl)
pUrl, err := url.Parse(c.proxyUrl)
if err != nil {
log.Fatal(err)
}
transport.Proxy = http.ProxyURL(pUrl)
if c.proxyCreds != "" {
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(c.proxyCreds))
transport.ProxyConnectHeader = http.Header{}
transport.ProxyConnectHeader.Add("Proxy-Authorization", basicAuth)
}
return transport
}
func (c *Client) useInsecureHTTPClient(insecure bool) *http.Transport {
// proxyUrl, _ := url.Parse("http://10.0.1.167:3128")
transport := http.DefaultTransport.(*http.Transport)
// transport := &http.Transport{
// TLSClientConfig: &tls.Config{
// CipherSuites: []uint16{
// tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
// tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// },
// PreferServerCipherSuites: true,
// InsecureSkipVerify: insecure,
// MinVersion: tls.VersionTLS11,
// MaxVersion: tls.VersionTLS12,
// },
// }
transport.TLSClientConfig = &tls.Config{
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
},
PreferServerCipherSuites: true,
InsecureSkipVerify: insecure,
MinVersion: tls.VersionTLS11,
MaxVersion: tls.VersionTLS12,
}
return transport
}
// Takes raw payload and does the http request
// Used for login request
// passwords with special chars have issues when using container
// for encoding/decoding
func (c *Client) MakeRestRequestRaw(method string, rpath string, payload []byte, authenticated bool) (*http.Request, error) {
pathURL, err := url.Parse(rpath)
if err != nil {
return nil, err
}
fURL, err := url.Parse(c.BaseURL.String())
if err != nil {
return nil, err
}
if c.preserveBaseUrlRef {
// Default is false for preserveBaseUrlRef - matching original behavior to strip out BaseURL
fURLStr := fURL.String() + pathURL.String()
fURL, err = url.Parse(fURLStr)
if err != nil {
return nil, err
}
} else {
// Original behavior to strip down BaseURL
fURL = fURL.ResolveReference(pathURL)
}
var req *http.Request
log.Printf("[DEBUG] BaseURL: %s, pathURL: %s, fURL: %s", c.BaseURL.String(), pathURL.String(), fURL.String())
if method == "GET" {
req, err = http.NewRequest(method, fURL.String(), nil)
} else {
req, err = http.NewRequest(method, fURL.String(), bytes.NewBuffer(payload))
}
if err != nil {
return nil, err
}
if c.skipLoggingPayload {
log.Printf("HTTP request %s %s", method, rpath)
} else {
log.Printf("HTTP request %s %s %v", method, rpath, req)
}
if authenticated {
req, err = c.InjectAuthenticationHeader(req, rpath)
if err != nil {
return req, err
}
}
if !c.skipLoggingPayload {
log.Printf("HTTP request after injection %s %s %v", method, rpath, req)
}
return req, nil
}
func (c *Client) MakeRestRequest(method string, rpath string, body *container.Container, authenticated bool) (*http.Request, error) {
pathURL, err := url.Parse(rpath)
if err != nil {
return nil, err
}
fURL, err := url.Parse(c.BaseURL.String())
if err != nil {
return nil, err
}
if c.preserveBaseUrlRef {
// Default is false for preserveBaseUrlRef - matching original behavior to strip out BaseURL
fURLStr := fURL.String() + pathURL.String()
fURL, err = url.Parse(fURLStr)
if err != nil {
return nil, err
}
} else {
// Original behavior to strip down BaseURL
fURL = fURL.ResolveReference(pathURL)
}
var req *http.Request
log.Printf("[DEBUG] BaseURL: %s, pathURL: %s, fURL: %s", c.BaseURL.String(), pathURL.String(), fURL.String())
if method == "GET" {
req, err = http.NewRequest(method, fURL.String(), nil)
} else {
req, err = http.NewRequest(method, fURL.String(), bytes.NewBuffer((body.Bytes())))
}
if err != nil {
return nil, err
}
if c.skipLoggingPayload {
log.Printf("HTTP request %s %s", method, rpath)
} else {
log.Printf("HTTP request %s %s %v", method, rpath, req)
}
if authenticated {
req, err = c.InjectAuthenticationHeader(req, rpath)
if err != nil {
return req, err
}
}
if !c.skipLoggingPayload {
log.Printf("HTTP request after injection %s %s %v", method, rpath, req)
}
return req, nil
}
// Authenticate is used to
func (c *Client) Authenticate() error {
method := "POST"
path := "/api/aaaLogin.json"
authenticated := false
// Adding the follwing replace allows support for (1) Login Domains, where login is in the format of: apic#LOCAL\admin2
// (2) escapes out the password to support scenarios where the user password includes backslashes
escUserName := strings.ReplaceAll(c.username, `\`, `\\`)
escPwd := strings.ReplaceAll(c.password, `\`, `\\`)
body := []byte(fmt.Sprintf(authPayload, escUserName, escPwd))
if c.appUserName != "" {
path = "/api/requestAppToken.json"
body = []byte(fmt.Sprintf(authAppPayload, c.appUserName))
authenticated = true
}
c.skipLoggingPayload = true
req, err := c.MakeRestRequestRaw(method, path, body, authenticated)
if err != nil {
return err
}
obj, _, err := c.Do(req)
c.skipLoggingPayload = false
if err != nil {
log.Printf("[DEBUG] Authentication ERROR: %s", err)
return err
}
if obj == nil {
return errors.New("Empty response")
}
err = CheckForErrors(obj, method, c.skipLoggingPayload)
if err != nil {
return err
}
token := obj.S("imdata").Index(0).S("aaaLogin", "attributes", "token").String()
creationTimeStr := stripQuotes(obj.S("imdata").Index(0).S("aaaLogin", "attributes", "creationTime").String())
refreshTimeStr := stripQuotes(obj.S("imdata").Index(0).S("aaaLogin", "attributes", "refreshTimeoutSeconds").String())
creationTimeInt, err := StrtoInt(creationTimeStr, 10, 64)
if err != nil {
return err
}
refreshTimeInt, err := StrtoInt(refreshTimeStr, 10, 64)
if err != nil {
return err
}
if token == "" {
return errors.New("Invalid Username or Password")
}
if c.AuthToken == nil {
c.AuthToken = &Auth{}
}
c.AuthToken.Token = stripQuotes(token)
c.AuthToken.apicCreatedAt = time.Unix(creationTimeInt, 0)
c.AuthToken.realCreatedAt = time.Now()
c.AuthToken.CalculateExpiry(refreshTimeInt)
c.AuthToken.CaclulateOffset()
return nil
}
func StrtoInt(s string, startIndex int, bitSize int) (int64, error) {
return strconv.ParseInt(s, startIndex, bitSize)
}
func (c *Client) Do(req *http.Request) (*container.Container, *http.Response, error) {
log.Printf("[DEBUG] Begining Do method %s", req.URL.String())
// retain the request body across multiple attempts
var body []byte
if req.Body != nil {
body, _ = ioutil.ReadAll(req.Body)
}
for attempts := 0; ; attempts++ {
log.Printf("[TRACE] HTTP Request Method and URL: %s %s", req.Method, req.URL.String())
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if !c.skipLoggingPayload {
log.Printf("[TRACE] HTTP Request Body: %v", req.Body)
}
resp, err := c.httpClient.Do(req)
if err != nil {
if ok := c.backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Connection error occured: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
return nil, nil, err
} else {
log.Printf("[ERROR] HTTP Connection failed: %s, retries: %v", err, attempts)
continue
}
}
if !c.skipLoggingPayload {
log.Printf("[TRACE] HTTP Response: %d %s %v", resp.StatusCode, resp.Status, resp)
} else {
log.Printf("[TRACE] HTTP Response: %d %s", resp.StatusCode, resp.Status)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyStr := string(bodyBytes)
resp.Body.Close()
if !c.skipLoggingPayload {
log.Printf("[DEBUG] HTTP response unique string %s %s %s", req.Method, req.URL.String(), bodyStr)
}
if (resp.StatusCode < 500 || resp.StatusCode > 504) && resp.StatusCode != 405 {
obj, err := container.ParseJSON(bodyBytes)
if err != nil {
log.Printf("[ERROR] Error occured while json parsing %+v", err)
log.Printf("[DEBUG] Exit from Do method")
return nil, resp, err
}
log.Printf("[DEBUG] Exit from Do method")
return obj, resp, nil
} else {
if ok := c.backoff(attempts); !ok {
obj, err := container.ParseJSON(bodyBytes)
if err != nil {
log.Printf("[ERROR] Error occured while json parsing %+v with HTTP StatusCode 405, 500-504", err)
log.Printf("[DEBUG] Exit from Do method")
return nil, resp, err
}
log.Printf("[DEBUG] Exit from Do method")
return obj, resp, nil
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", resp.StatusCode, attempts)
continue
}
}
}
}
func (c *Client) DoRaw(req *http.Request) (*http.Response, error) {
log.Printf("[DEBUG] Begining DoRaw method %s", req.URL.String())
// retain the request body across multiple attempts
var body []byte
if req.Body != nil {
body, _ = ioutil.ReadAll(req.Body)
}
for attempts := 0; ; attempts++ {
log.Printf("[TRACE] HTTP Request Method and URL: %s %s", req.Method, req.URL.String())
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if !c.skipLoggingPayload {
log.Printf("[TRACE] HTTP Request Body: %v", req.Body)
}
resp, err := c.httpClient.Do(req)
if err != nil {
if ok := c.backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Connection error occured: %+v", err)
log.Printf("[DEBUG] Exit from DoRaw method")
return nil, err
} else {
log.Printf("[ERROR] HTTP Connection failed: %s, retries: %v", err, attempts)
continue
}
}
if !c.skipLoggingPayload {
log.Printf("[TRACE] HTTP Response: %d %s %v", resp.StatusCode, resp.Status, resp)
} else {
log.Printf("[TRACE] HTTP Response: %d %s", resp.StatusCode, resp.Status)
}
if (resp.StatusCode < 500 || resp.StatusCode > 504) && resp.StatusCode != 405 {
log.Printf("[DEBUG] Exit from DoRaw method")
return resp, nil
} else {
if ok := c.backoff(attempts); !ok {
log.Printf("[DEBUG] Exit from DoRaw method")
return resp, nil
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", resp.StatusCode, attempts)
continue
}
}
}
}
func stripQuotes(word string) string {
if strings.HasPrefix(word, "\"") && strings.HasSuffix(word, "\"") {
return strings.TrimSuffix(strings.TrimPrefix(word, "\""), "\"")
}
return word
}
func (c *Client) backoff(attempts int) bool {
log.Printf("[DEBUG] Begining backoff method: attempts %v on %v", attempts, c.maxRetries)
if attempts >= c.maxRetries {
log.Printf("[DEBUG] Exit from backoff method with return value false")
return false
}
minDelay := time.Duration(DefaultBackoffMinDelay) * time.Second
if c.backoffMinDelay != 0 {
minDelay = time.Duration(c.backoffMinDelay) * time.Second
}
maxDelay := time.Duration(DefaultBackoffMaxDelay) * time.Second
if c.backoffMaxDelay != 0 {
maxDelay = time.Duration(c.backoffMaxDelay) * time.Second
}
factor := DefaultBackoffDelayFactor
if c.backoffDelayFactor != 0 {
factor = c.backoffDelayFactor
}
min := float64(minDelay)
backoff := min * math.Pow(factor, float64(attempts))
if backoff > float64(maxDelay) {
backoff = float64(maxDelay)
}
backoff = (rand.Float64()/2+0.5)*(backoff-min) + min
backoffDuration := time.Duration(backoff)
log.Printf("[TRACE] Starting sleeping for %v", backoffDuration.Round(time.Second))
time.Sleep(backoffDuration)
log.Printf("[DEBUG] Exit from backoff method with return value true")
return true
}