/
session.go
498 lines (420 loc) · 15.5 KB
/
session.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
/*
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 client provides Go bindings to the Traffic Ops RPC API.
package client
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httptrace"
"strconv"
"strings"
"sync"
"time"
tc "github.com/apache/trafficcontrol/lib/go-tc"
"golang.org/x/net/publicsuffix"
)
// Session ...
type Session struct {
UserName string
Password string
URL string
Client *http.Client
cache map[string]CacheEntry
cacheMutex *sync.RWMutex
useCache bool
UserAgentStr string
}
func NewSession(user, password, url, userAgent string, client *http.Client, useCache bool) *Session {
return &Session{
UserName: user,
Password: password,
URL: url,
Client: client,
cache: map[string]CacheEntry{},
cacheMutex: &sync.RWMutex{},
useCache: useCache,
UserAgentStr: userAgent,
}
}
const DefaultTimeout = time.Second * time.Duration(30)
// HTTPError is returned on Update Session failure.
type HTTPError struct {
HTTPStatusCode int
HTTPStatus string
URL string
Body string
}
// Error implements the error interface for our customer error type.
func (e *HTTPError) Error() string {
return fmt.Sprintf("%s[%d] - Error requesting Traffic Ops %s %s", e.HTTPStatus, e.HTTPStatusCode, e.URL, e.Body)
}
// CacheEntry ...
type CacheEntry struct {
Entered int64
Bytes []byte
RemoteAddr net.Addr
}
// TODO JvD
const tmPollingInterval = 60
// loginCreds gathers login credentials for Traffic Ops.
func loginCreds(toUser string, toPasswd string) ([]byte, error) {
credentials := tc.UserCredentials{
Username: toUser,
Password: toPasswd,
}
js, err := json.Marshal(credentials)
if err != nil {
err := fmt.Errorf("Error creating login json: %v", err)
return nil, err
}
return js, nil
}
// loginToken gathers token login credentials for Traffic Ops.
func loginToken(token string) ([]byte, error) {
form := tc.UserToken{
Token: token,
}
j, e := json.Marshal(form)
if e != nil {
e := fmt.Errorf("Error creating token login json: %v", e)
return nil, e
}
return j, nil
}
// login tries to log in to Traffic Ops, and set the auth cookie in the Session. Returns the IP address of the remote Traffic Ops.
func (to *Session) login() (net.Addr, error) {
credentials, err := loginCreds(to.UserName, to.Password)
if err != nil {
return nil, errors.New("creating login credentials: " + err.Error())
}
path := apiBase + "/user/login"
resp, remoteAddr, err := to.RawRequest("POST", path, credentials)
resp, remoteAddr, err = to.ErrUnlessOK(resp, remoteAddr, err, path)
if err != nil {
return remoteAddr, errors.New("requesting: " + err.Error())
}
defer resp.Body.Close()
var alerts tc.Alerts
if err := json.NewDecoder(resp.Body).Decode(&alerts); err != nil {
return remoteAddr, errors.New("decoding response JSON: " + err.Error())
}
success := false
for _, alert := range alerts.Alerts {
if alert.Level == "success" && alert.Text == "Successfully logged in." {
success = true
break
}
}
if !success {
return remoteAddr, fmt.Errorf("Login failed, alerts string: %+v", alerts)
}
return remoteAddr, nil
}
func (to *Session) loginWithToken(token []byte) (net.Addr, error) {
path := apiBase + "/user/login/token"
resp, remoteAddr, err := to.RawRequest(http.MethodPost, path, token)
resp, remoteAddr, err = to.ErrUnlessOK(resp, remoteAddr, err, path)
if err != nil {
return remoteAddr, fmt.Errorf("requesting: %v", err)
}
defer resp.Body.Close()
var alerts tc.Alerts
if err := json.NewDecoder(resp.Body).Decode(&alerts); err != nil {
return remoteAddr, fmt.Errorf("decoding response JSON: %v", err)
}
for _, alert := range alerts.Alerts {
if alert.Level == tc.SuccessLevel.String() && alert.Text == "Successfully logged in." {
return remoteAddr, nil
}
}
return remoteAddr, fmt.Errorf("Login failed, alerts string: %+v", alerts)
}
// logout of Traffic Ops
func (to *Session) logout() (net.Addr, error) {
credentials, err := loginCreds(to.UserName, to.Password)
if err != nil {
return nil, errors.New("creating login credentials: " + err.Error())
}
path := apiBase + "/user/logout"
resp, remoteAddr, err := to.RawRequest("POST", path, credentials)
resp, remoteAddr, err = to.ErrUnlessOK(resp, remoteAddr, err, path)
if err != nil {
return remoteAddr, errors.New("requesting: " + err.Error())
}
defer resp.Body.Close()
var alerts tc.Alerts
if err := json.NewDecoder(resp.Body).Decode(&alerts); err != nil {
return remoteAddr, errors.New("decoding response JSON: " + err.Error())
}
success := false
for _, alert := range alerts.Alerts {
if alert.Level == "success" && alert.Text == "Successfully logged in." {
success = true
break
}
}
if !success {
return remoteAddr, fmt.Errorf("Logout failed, alerts string: %+v", alerts)
}
return remoteAddr, nil
}
// Login to traffic_ops, the response should set the cookie for this session
// automatically. Start with
// to := traffic_ops.Login("user", "passwd", true)
// subsequent calls like to.GetData("datadeliveryservice") will be authenticated.
// Returns the logged in client, the remote address of Traffic Ops which was translated and used to log in, and any error. If the error is not nil, the remote address may or may not be nil, depending whether the error occurred before the login request.
func LoginWithAgent(toURL string, toUser string, toPasswd string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) (*Session, net.Addr, error) {
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
return nil, nil, err
}
to := NewSession(toUser, toPasswd, toURL, userAgent, &http.Client{
Timeout: requestTimeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
Jar: jar,
}, useCache)
remoteAddr, err := to.login()
if err != nil {
return nil, remoteAddr, errors.New("logging in: " + err.Error())
}
return to, remoteAddr, nil
}
func LoginWithToken(toURL string, token string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) (*Session, net.Addr, error) {
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
return nil, nil, err
}
client := http.Client{
Timeout: requestTimeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
Jar: jar,
}
to := NewSession("", "", toURL, userAgent, &client, useCache)
tBts, err := loginToken(token)
if err != nil {
return nil, nil, fmt.Errorf("logging in: %v", err)
}
remoteAddr, err := to.loginWithToken(tBts)
if err != nil {
return nil, remoteAddr, fmt.Errorf("logging in: %v", err)
}
return to, remoteAddr, nil
}
// Logout of traffic_ops
func LogoutWithAgent(toURL string, toUser string, toPasswd string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) (*Session, net.Addr, error) {
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
return nil, nil, err
}
to := NewSession(toUser, toPasswd, toURL, userAgent, &http.Client{
Timeout: requestTimeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
Jar: jar,
}, useCache)
remoteAddr, err := to.logout()
if err != nil {
return nil, remoteAddr, errors.New("logging out: " + err.Error())
}
return to, remoteAddr, nil
}
// NewNoAuthSession returns a new Session without logging in
// this can be used for querying unauthenticated endpoints without requiring a login
func NewNoAuthSession(toURL string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) *Session {
return NewSession("", "", toURL, userAgent, &http.Client{
Timeout: requestTimeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
}, useCache)
}
// ErrUnlessOk returns nil and an error if the given Response's status code is anything but 200 OK. This includes reading the Response.Body and Closing it. Otherwise, the given response and error are returned unchanged.
func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err error, path string) (*http.Response, net.Addr, error) {
if err != nil {
return resp, remoteAddr, err
}
if resp.StatusCode < 300 {
return resp, remoteAddr, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotImplemented {
return nil, remoteAddr, errors.New("Traffic Ops Server returned 'Not Implemented', this client is probably newer than Traffic Ops, and you probably need to either upgrade Traffic Ops, or use a client whose version matches your Traffic Ops version.")
}
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil, remoteAddr, readErr
}
return nil, remoteAddr, errors.New(resp.Status + "[" + strconv.Itoa(resp.StatusCode) + "] - Error requesting Traffic Ops " + to.getURL(path) + " " + string(body))
}
func (to *Session) getURL(path string) string { return to.URL + path }
// request performs the HTTP request to Traffic Ops, trying to refresh the cookie if an Unauthorized or Forbidden code is received. It only tries once. If the login fails, the original Unauthorized/Forbidden response is returned. If the login succeeds and the subsequent re-request fails, the re-request's response is returned even if it's another Unauthorized/Forbidden.
// Returns the response, the remote address of the Traffic Ops instance used, and any error.
// The returned net.Addr is guaranteed to be either nil or valid, even if the returned error is not nil. Callers are encouraged to check and use the net.Addr if an error is returned, and use the remote address in their own error messages. This violates the Go idiom that a non-nil error implies all other values are undefined, but it's more straightforward than alternatives like typecasting.
func (to *Session) request(method, path string, body []byte) (*http.Response, net.Addr, error) {
r, remoteAddr, err := to.RawRequest(method, path, body)
if err != nil {
return r, remoteAddr, err
}
if r.StatusCode != http.StatusUnauthorized && r.StatusCode != http.StatusForbidden {
return to.ErrUnlessOK(r, remoteAddr, err, path)
}
if _, lerr := to.login(); lerr != nil {
return to.ErrUnlessOK(r, remoteAddr, err, path) // if re-logging-in fails, return the original request's response
}
// return second request, even if it's another Unauthorized or Forbidden.
r, remoteAddr, err = to.RawRequest(method, path, body)
return to.ErrUnlessOK(r, remoteAddr, err, path)
}
// RawRequest performs the actual HTTP request to Traffic Ops, simply, without trying to refresh the cookie if an Unauthorized code is returned.
// Returns the response, the remote address of the Traffic Ops instance used, and any error.
// The returned net.Addr is guaranteed to be either nil or valid, even if the returned error is not nil. Callers are encouraged to check and use the net.Addr if an error is returned, and use the remote address in their own error messages. This violates the Go idiom that a non-nil error implies all other values are undefined, but it's more straightforward than alternatives like typecasting.
func (to *Session) RawRequest(method, path string, body []byte) (*http.Response, net.Addr, error) {
url := to.getURL(path)
var req *http.Request
var err error
remoteAddr := net.Addr(nil)
if body != nil {
req, err = http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, remoteAddr, err
}
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(method, url, nil)
if err != nil {
return nil, remoteAddr, err
}
}
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
remoteAddr = connInfo.Conn.RemoteAddr()
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
req.Header.Set("User-Agent", to.UserAgentStr)
resp, err := to.Client.Do(req)
if err != nil {
return nil, remoteAddr, err
}
return resp, remoteAddr, nil
}
type ReqInf struct {
CacheHitStatus CacheHitStatus
RemoteAddr net.Addr
}
type CacheHitStatus string
const CacheHitStatusHit = CacheHitStatus("hit")
const CacheHitStatusExpired = CacheHitStatus("expired")
const CacheHitStatusMiss = CacheHitStatus("miss")
const CacheHitStatusInvalid = CacheHitStatus("")
func (s CacheHitStatus) String() string {
return string(s)
}
func StringToCacheHitStatus(s string) CacheHitStatus {
s = strings.ToLower(s)
switch s {
case "hit":
return CacheHitStatusHit
case "expired":
return CacheHitStatusExpired
case "miss":
return CacheHitStatusMiss
default:
return CacheHitStatusInvalid
}
}
// setCache Sets the given cache key and value. This is threadsafe for multiple goroutines.
func (to *Session) setCache(path string, entry CacheEntry) {
if !to.useCache {
return
}
to.cacheMutex.Lock()
defer to.cacheMutex.Unlock()
to.cache[path] = entry
}
// getCache gets the cache value at the given key, or false if it doesn't exist. This is threadsafe for multiple goroutines.
func (to *Session) getCache(path string) (CacheEntry, bool) {
to.cacheMutex.RLock()
defer to.cacheMutex.RUnlock()
cacheEntry, ok := to.cache[path]
return cacheEntry, ok
}
//if cacheEntry, ok := to.Cache[path]; ok {
// getBytesWithTTL gets the path, and caches in the session. Returns bytes from the cache, if found and the TTL isn't expired. Otherwise, gets it and store it in cache
func (to *Session) getBytesWithTTL(path string, ttl int64) ([]byte, ReqInf, error) {
var body []byte
var err error
var cacheHitStatus CacheHitStatus
var remoteAddr net.Addr
getFresh := false
if cacheEntry, ok := to.getCache(path); ok {
if cacheEntry.Entered > time.Now().Unix()-ttl {
cacheHitStatus = CacheHitStatusHit
body = cacheEntry.Bytes
remoteAddr = cacheEntry.RemoteAddr
} else {
cacheHitStatus = CacheHitStatusExpired
getFresh = true
}
} else {
cacheHitStatus = CacheHitStatusMiss
getFresh = true
}
if getFresh {
body, remoteAddr, err = to.getBytes(path)
if err != nil {
return nil, ReqInf{CacheHitStatus: CacheHitStatusInvalid, RemoteAddr: remoteAddr}, err
}
newEntry := CacheEntry{
Entered: time.Now().Unix(),
Bytes: body,
RemoteAddr: remoteAddr,
}
to.setCache(path, newEntry)
}
return body, ReqInf{CacheHitStatus: cacheHitStatus, RemoteAddr: remoteAddr}, nil
}
// GetBytes - get []bytes array for a certain path on the to session.
// returns the raw body, the remote address the Traffic Ops URL resolved to, or any error. If the error is not nil, the RemoteAddr may or may not be nil, depending whether the error occurred before the request was executed.
func (to *Session) getBytes(path string) ([]byte, net.Addr, error) {
resp, remoteAddr, err := to.request("GET", path, nil)
if err != nil {
return nil, remoteAddr, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, remoteAddr, err
}
return body, remoteAddr, nil
}