-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiclient.go
702 lines (632 loc) · 20.9 KB
/
apiclient.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
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"bufio"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/retry"
"github.com/juju/utils"
"github.com/juju/utils/clock"
"github.com/juju/utils/parallel"
"github.com/juju/version"
"golang.org/x/net/websocket"
"gopkg.in/juju/names.v2"
"gopkg.in/macaroon-bakery.v1/httpbakery"
"gopkg.in/macaroon.v1"
"github.com/juju/juju/api/base"
"github.com/juju/juju/apiserver/observer"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/network"
"github.com/juju/juju/rpc"
"github.com/juju/juju/rpc/jsoncodec"
)
// PingPeriod defines how often the internal connection health check
// will run.
const PingPeriod = 1 * time.Minute
// pingTimeout defines how long a health check can take before we
// consider it to have failed.
const pingTimeout = 30 * time.Second
var logger = loggo.GetLogger("juju.api")
type rpcConnection interface {
Call(req rpc.Request, params, response interface{}) error
Dead() <-chan struct{}
Close() error
}
// state is the internal implementation of the Connection interface.
type state struct {
client rpcConnection
conn *websocket.Conn
clock clock.Clock
// addr is the address used to connect to the API server.
addr string
// cookieURL is the URL that HTTP cookies for the API
// will be associated with (specifically macaroon auth cookies).
cookieURL *url.URL
// modelTag holds the model tag.
// It is empty if there is no model tag associated with the connection.
modelTag names.ModelTag
// controllerTag holds the controller's tag once we're connected.
controllerTag names.ControllerTag
// serverVersion holds the version of the API server that we are
// connected to. It is possible that this version is 0 if the
// server does not report this during login.
serverVersion version.Number
// hostPorts is the API server addresses returned from Login,
// which the client may cache and use for failover.
hostPorts [][]network.HostPort
// facadeVersions holds the versions of all facades as reported by
// Login
facadeVersions map[string][]int
// pingFacadeVersion is the version to use for the pinger. This is lazily
// set at initialization to avoid a race in our tests. See
// http://pad.lv/1614732 for more details regarding the race.
pingerFacadeVersion int
// authTag holds the authenticated entity's tag after login.
authTag names.Tag
// mpdelAccess holds the access level of the user to the connected model.
modelAccess string
// controllerAccess holds the access level of the user to the connected controller.
controllerAccess string
// broken is a channel that gets closed when the connection is
// broken.
broken chan struct{}
// closed is a channel that gets closed when State.Close is called.
closed chan struct{}
// loggedIn holds whether the client has successfully logged
// in. It's a int32 so that the atomic package can be used to
// access it safely.
loggedIn int32
// tag, password, macaroons and nonce hold the cached login
// credentials. These are only valid if loggedIn is 1.
tag string
password string
macaroons []macaroon.Slice
nonce string
// serverRootAddress holds the cached API server address and port used
// to login.
serverRootAddress string
// serverScheme is the URI scheme of the API Server
serverScheme string
// tlsConfig holds the TLS config appropriate for making SSL
// connections to the API endpoints.
tlsConfig *tls.Config
// certPool holds the cert pool that is used to authenticate the tls
// connections to the API.
certPool *x509.CertPool
// bakeryClient holds the client that will be used to
// authorize macaroon based login requests.
bakeryClient *httpbakery.Client
}
// RedirectError is returned from Open when the controller
// needs to inform the client that the model is hosted
// on a different set of API addresses.
type RedirectError struct {
// Servers holds the sets of addresses of the redirected
// servers.
Servers [][]network.HostPort
// CACert holds the certificate of the remote server.
CACert string
}
func (e *RedirectError) Error() string {
return fmt.Sprintf("redirection to alternative server required")
}
// Open establishes a connection to the API server using the Info
// given, returning a State instance which can be used to make API
// requests.
//
// If the model is hosted on a different server, Open
// will return an error with a *RedirectError cause
// holding the details of another server to connect to.
//
// See Connect for details of the connection mechanics.
func Open(info *Info, opts DialOpts) (Connection, error) {
return open(info, opts, clock.WallClock)
}
// open is the unexported version of open that also includes
// an explicit clock instance argument.
func open(
info *Info,
opts DialOpts,
clock clock.Clock,
) (Connection, error) {
if err := info.Validate(); err != nil {
return nil, errors.Annotate(err, "validating info for opening an API connection")
}
if clock == nil {
return nil, errors.NotValidf("nil clock")
}
conn, tlsConfig, err := dialAPI(info, opts)
if err != nil {
return nil, errors.Trace(err)
}
client := rpc.NewConn(jsoncodec.NewWebsocket(conn), observer.None())
client.Start()
bakeryClient := opts.BakeryClient
if bakeryClient == nil {
bakeryClient = httpbakery.NewClient()
} else {
// Make a copy of the bakery client and its HTTP client
c := *opts.BakeryClient
bakeryClient = &c
httpc := *bakeryClient.Client
bakeryClient.Client = &httpc
}
apiHost := conn.Config().Location.Host
// Technically when there's no CACert, we don't need this
// machinery, because we could just use http.DefaultTransport
// for everything, but it's easier just to leave it in place.
bakeryClient.Client.Transport = &hostSwitchingTransport{
primaryHost: apiHost,
primary: utils.NewHttpTLSTransport(tlsConfig),
fallback: http.DefaultTransport,
}
st := &state{
client: client,
conn: conn,
clock: clock,
addr: apiHost,
cookieURL: &url.URL{
Scheme: "https",
Host: conn.Config().Location.Host,
Path: "/",
},
pingerFacadeVersion: facadeVersions["Pinger"],
serverScheme: "https",
serverRootAddress: conn.Config().Location.Host,
// We populate the username and password before
// login because, when doing HTTP requests, we'll want
// to use the same username and password for authenticating
// those. If login fails, we discard the connection.
tag: tagToString(info.Tag),
password: info.Password,
macaroons: info.Macaroons,
nonce: info.Nonce,
tlsConfig: tlsConfig,
bakeryClient: bakeryClient,
modelTag: info.ModelTag,
}
if !info.SkipLogin {
if err := st.Login(info.Tag, info.Password, info.Nonce, info.Macaroons); err != nil {
conn.Close()
return nil, errors.Trace(err)
}
}
st.broken = make(chan struct{})
st.closed = make(chan struct{})
go (&monitor{
clock: clock,
ping: st.Ping,
pingPeriod: PingPeriod,
pingTimeout: pingTimeout,
closed: st.closed,
dead: client.Dead(),
broken: st.broken,
}).run()
return st, nil
}
// hostSwitchingTransport provides an http.RoundTripper
// that chooses an actual RoundTripper to use
// depending on the destination host.
//
// This makes it possible to use a different set of root
// CAs for the API and all other hosts.
type hostSwitchingTransport struct {
primaryHost string
primary http.RoundTripper
fallback http.RoundTripper
}
// RoundTrip implements http.RoundTripper.RoundTrip.
func (t *hostSwitchingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL.Host == t.primaryHost {
return t.primary.RoundTrip(req)
}
return t.fallback.RoundTrip(req)
}
// ConnectStream implements StreamConnector.ConnectStream.
func (st *state) ConnectStream(path string, attrs url.Values) (base.Stream, error) {
if !st.isLoggedIn() {
return nil, errors.New("cannot use ConnectStream without logging in")
}
// We use the standard "macaraq" macaroon authentication dance here.
// That is, we attach any macaroons we have to the initial request,
// and if that succeeds, all's good. If it fails with a DischargeRequired
// error, the response will contain a macaroon that, when discharged,
// may allow access, so we discharge it (using bakery.Client.HandleError)
// and try the request again.
conn, err := st.connectStream(path, attrs)
if err == nil {
return conn, err
}
if params.ErrCode(err) != params.CodeDischargeRequired {
return nil, errors.Trace(err)
}
if err := st.bakeryClient.HandleError(st.cookieURL, bakeryError(err)); err != nil {
return nil, errors.Trace(err)
}
// Try again with the discharged macaroon.
conn, err = st.connectStream(path, attrs)
if err != nil {
return nil, errors.Trace(err)
}
return conn, nil
}
// connectStream is the internal version of ConnectStream. It differs from
// ConnectStream only in that it will not retry the connection if it encounters
// discharge-required error.
func (st *state) connectStream(path string, attrs url.Values) (base.Stream, error) {
path, err := apiPath(st.modelTag, path)
if err != nil {
return nil, errors.Trace(err)
}
target := url.URL{
Scheme: "wss",
Host: st.addr,
Path: path,
RawQuery: attrs.Encode(),
}
cfg, err := websocket.NewConfig(target.String(), "http://localhost/")
if st.tag != "" {
cfg.Header = utils.BasicAuthHeader(st.tag, st.password)
}
if st.nonce != "" {
cfg.Header.Set(params.MachineNonceHeader, st.nonce)
}
// Add any cookies because they will not be sent to websocket
// connections by default.
st.addCookiesToHeader(cfg.Header)
cfg.TlsConfig = st.tlsConfig
connection, err := websocketDialConfig(cfg)
if err != nil {
return nil, err
}
if err := readInitialStreamError(connection); err != nil {
return nil, errors.Trace(err)
}
return connection, nil
}
// readInitialStreamError reads the initial error response
// from a stream connection and returns it.
func readInitialStreamError(conn io.Reader) error {
// We can use bufio here because the websocket guarantees that a
// single read will not read more than a single frame; there is
// no guarantee that a single read might not read less than the
// whole frame though, so using a single Read call is not
// correct. By using ReadSlice rather than ReadBytes, we
// guarantee that the error can't be too big (>4096 bytes).
line, err := bufio.NewReader(conn).ReadSlice('\n')
if err != nil {
return errors.Annotate(err, "unable to read initial response")
}
var errResult params.ErrorResult
if err := json.Unmarshal(line, &errResult); err != nil {
return errors.Annotate(err, "unable to unmarshal initial response")
}
if errResult.Error != nil {
return errResult.Error
}
return nil
}
// addCookiesToHeader adds any cookies associated with the
// API host to the given header. This is necessary because
// otherwise cookies are not sent to websocket endpoints.
func (st *state) addCookiesToHeader(h http.Header) {
// net/http only allows adding cookies to a request,
// but when it sends a request to a non-http endpoint,
// it doesn't add the cookies, so make a request, starting
// with the given header, add the cookies to use, then
// throw away the request but keep the header.
req := &http.Request{
Header: h,
}
cookies := st.bakeryClient.Client.Jar.Cookies(st.cookieURL)
for _, c := range cookies {
req.AddCookie(c)
}
}
// apiEndpoint returns a URL that refers to the given API slash-prefixed
// endpoint path and query parameters.
func (st *state) apiEndpoint(path, query string) (*url.URL, error) {
path, err := apiPath(st.modelTag, path)
if err != nil {
return nil, errors.Trace(err)
}
return &url.URL{
Scheme: st.serverScheme,
Host: st.Addr(),
Path: path,
RawQuery: query,
}, nil
}
// Ping implements api.Connection.
func (s *state) Ping() error {
return s.APICall("Pinger", s.pingerFacadeVersion, "", "Ping", nil, nil)
}
// apiPath returns the given API endpoint path relative
// to the given model tag.
func apiPath(modelTag names.ModelTag, path string) (string, error) {
if !strings.HasPrefix(path, "/") {
return "", errors.Errorf("cannot make API path from non-slash-prefixed path %q", path)
}
modelUUID := modelTag.Id()
if modelUUID == "" {
return path, nil
}
return "/model/" + modelUUID + path, nil
}
// tagToString returns the value of a tag's String method, or "" if the tag is nil.
func tagToString(tag names.Tag) string {
if tag == nil {
return ""
}
return tag.String()
}
// dialAPI establishes a websocket connection to the RPC
// API websocket on the API server using Info. If multiple API addresses
// are provided in Info they will be tried concurrently - the first successful
// connection wins.
//
// It also returns the TLS configuration that it has derived from the Info.
func dialAPI(info *Info, opts DialOpts) (*websocket.Conn, *tls.Config, error) {
// Set opts.DialWebsocket here rather than in open because
// some tests call dialAPI directly.
if opts.DialWebsocket == nil {
opts.DialWebsocket = websocket.DialConfig
}
if len(info.Addrs) == 0 {
return nil, nil, errors.New("no API addresses to connect to")
}
tlsConfig := utils.SecureTLSConfig()
tlsConfig.InsecureSkipVerify = opts.InsecureSkipVerify
if info.CACert != "" {
// We want to be specific here (rather than just using "anything".
// See commit 7fc118f015d8480dfad7831788e4b8c0432205e8 (PR 899).
tlsConfig.ServerName = "juju-apiserver"
certPool, err := CreateCertPool(info.CACert)
if err != nil {
return nil, nil, errors.Annotate(err, "cert pool creation failed")
}
tlsConfig.RootCAs = certPool
} else {
// No CA certificate so use the SNI host name for all
// connections (if SNIHostName is empty, the host
// name in the address will be used as usual).
tlsConfig.ServerName = info.SNIHostName
}
path, err := apiPath(info.ModelTag, "/api")
if err != nil {
return nil, nil, errors.Trace(err)
}
conn, err := dialWebsocketMulti(info.Addrs, path, tlsConfig, opts)
if err != nil {
return nil, nil, errors.Trace(err)
}
logger.Infof("connection established to %q", conn.RemoteAddr())
return conn, tlsConfig, nil
}
// dialWebsocketMulti dials a websocket with one of the provided addresses, the
// specified URL path, TLS configuration, and dial options. Each of the
// specified addresses will be attempted concurrently, and the first
// successful connection will be returned.
func dialWebsocketMulti(addrs []string, path string, tlsConfig *tls.Config, opts DialOpts) (*websocket.Conn, error) {
// Dial all addresses at reasonable intervals.
try := parallel.NewTry(0, nil)
defer try.Kill()
for _, addr := range addrs {
err := startDialWebsocket(try, addr, path, opts, tlsConfig)
if err == parallel.ErrStopped {
break
}
if err != nil {
return nil, errors.Trace(err)
}
select {
case <-time.After(opts.DialAddressInterval):
case <-try.Dead():
}
}
try.Close()
result, err := try.Result()
if err != nil {
return nil, errors.Trace(err)
}
return result.(*websocket.Conn), nil
}
// startDialWebsocket starts websocket connection to a single address
// on the given try instance.
func startDialWebsocket(try *parallel.Try, addr, path string, opts DialOpts, tlsConfig *tls.Config) error {
// origin is required by the WebSocket API, used for "origin policy"
// in websockets. We pass localhost to satisfy the API; it is
// inconsequential to us.
const origin = "http://localhost/"
cfg, err := websocket.NewConfig("wss://"+addr+path, origin)
if err != nil {
return errors.Trace(err)
}
cfg.TlsConfig = tlsConfig
return try.Start(newWebsocketDialer(cfg, opts))
}
// newWebsocketDialer0 returns a function that dials the websocket represented
// by the given configuration with the given dial options, suitable for passing
// to utils/parallel.Try.Start.
func newWebsocketDialer(cfg *websocket.Config, opts DialOpts) func(<-chan struct{}) (io.Closer, error) {
// TODO(katco): 2016-08-09: lp:1611427
openAttempt := utils.AttemptStrategy{
Total: opts.Timeout,
Delay: opts.RetryDelay,
}
return func(stop <-chan struct{}) (io.Closer, error) {
for a := openAttempt.Start(); a.Next(); {
select {
case <-stop:
return nil, parallel.ErrStopped
default:
}
logger.Infof("dialing %q", cfg.Location)
conn, err := opts.DialWebsocket(cfg)
if err == nil {
return conn, nil
}
if !a.HasNext() || isX509Error(err) {
// We won't reconnect when there's an X509 error
// because we're not going to succeed if we retry
// in that case.
logger.Infof("error dialing %q: %v", cfg.Location, err)
return nil, errors.Annotatef(err, "unable to connect to API")
}
}
panic("unreachable")
}
}
// isX509Error reports whether the given websocket error
// results from an X509 problem.
func isX509Error(err error) bool {
wsErr, ok := errors.Cause(err).(*websocket.DialError)
if !ok {
return false
}
switch wsErr.Err.(type) {
case x509.HostnameError,
x509.InsecureAlgorithmError,
x509.UnhandledCriticalExtension,
x509.UnknownAuthorityError,
x509.ConstraintViolationError,
x509.SystemRootsError:
return true
}
switch err {
case x509.ErrUnsupportedAlgorithm,
x509.IncorrectPasswordError:
return true
}
return false
}
type hasErrorCode interface {
ErrorCode() string
}
// APICall places a call to the remote machine.
//
// This fills out the rpc.Request on the given facade, version for a given
// object id, and the specific RPC method. It marshalls the Arguments, and will
// unmarshall the result into the response object that is supplied.
func (s *state) APICall(facade string, version int, id, method string, args, response interface{}) error {
retrySpec := retry.CallArgs{
Func: func() error {
return s.client.Call(rpc.Request{
Type: facade,
Version: version,
Id: id,
Action: method,
}, args, response)
},
IsFatalError: func(err error) bool {
err = errors.Cause(err)
ec, ok := err.(hasErrorCode)
if !ok {
return true
}
return ec.ErrorCode() != params.CodeRetry
},
Delay: 100 * time.Millisecond,
MaxDelay: 1500 * time.Millisecond,
MaxDuration: 10 * time.Second,
BackoffFunc: retry.DoubleDelay,
Clock: s.clock,
}
err := retry.Call(retrySpec)
return errors.Trace(err)
}
func (s *state) Close() error {
err := s.client.Close()
select {
case <-s.closed:
default:
close(s.closed)
}
<-s.broken
return err
}
// Broken implements api.Connection.
func (s *state) Broken() <-chan struct{} {
return s.broken
}
// IsBroken implements api.Connection.
func (s *state) IsBroken() bool {
select {
case <-s.broken:
return true
default:
}
if err := s.Ping(); err != nil {
logger.Debugf("connection ping failed: %v", err)
return true
}
return false
}
// Addr returns the address used to connect to the API server.
func (s *state) Addr() string {
return s.addr
}
// ModelTag implements base.APICaller.ModelTag.
func (s *state) ModelTag() (names.ModelTag, bool) {
return s.modelTag, s.modelTag.Id() != ""
}
// ControllerTag implements base.APICaller.ControllerTag.
func (s *state) ControllerTag() names.ControllerTag {
return s.controllerTag
}
// APIHostPorts returns addresses that may be used to connect
// to the API server, including the address used to connect.
//
// The addresses are scoped (public, cloud-internal, etc.), so
// the client may choose which addresses to attempt. For the
// Juju CLI, all addresses must be attempted, as the CLI may
// be invoked both within and outside the model (think
// private clouds).
func (s *state) APIHostPorts() [][]network.HostPort {
// NOTE: We're making a copy of s.hostPorts before returning it,
// for safety.
hostPorts := make([][]network.HostPort, len(s.hostPorts))
for i, server := range s.hostPorts {
hostPorts[i] = append([]network.HostPort{}, server...)
}
return hostPorts
}
// AllFacadeVersions returns what versions we know about for all facades
func (s *state) AllFacadeVersions() map[string][]int {
facades := make(map[string][]int, len(s.facadeVersions))
for name, versions := range s.facadeVersions {
facades[name] = append([]int{}, versions...)
}
return facades
}
// BestFacadeVersion compares the versions of facades that we know about, and
// the versions available from the server, and reports back what version is the
// 'best available' to use.
// TODO(jam) this is the eventual implementation of what version of a given
// Facade we will want to use. It needs to line up the versions that the server
// reports to us, with the versions that our client knows how to use.
func (s *state) BestFacadeVersion(facade string) int {
return bestVersion(facadeVersions[facade], s.facadeVersions[facade])
}
// serverRoot returns the cached API server address and port used
// to login, prefixed with "<URI scheme>://" (usually https).
func (s *state) serverRoot() string {
return s.serverScheme + "://" + s.serverRootAddress
}
func (s *state) isLoggedIn() bool {
return atomic.LoadInt32(&s.loggedIn) == 1
}
func (s *state) setLoggedIn() {
atomic.StoreInt32(&s.loggedIn, 1)
}