-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiclient.go
1274 lines (1143 loc) · 37.7 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
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"bufio"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/gorilla/websocket"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/utils/v4"
"github.com/juju/utils/v4/parallel"
"gopkg.in/retry.v1"
"github.com/juju/juju/api/base"
"github.com/juju/juju/core/facades"
"github.com/juju/juju/core/network"
jujuversion "github.com/juju/juju/core/version"
jujuhttp "github.com/juju/juju/internal/http"
internallogger "github.com/juju/juju/internal/logger"
jujuproxy "github.com/juju/juju/internal/proxy"
proxy "github.com/juju/juju/internal/proxy/config"
"github.com/juju/juju/rpc"
"github.com/juju/juju/rpc/jsoncodec"
"github.com/juju/juju/rpc/params"
)
// 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
// modelRoot is the prefix that all model API paths begin with.
const modelRoot = "/model/"
var logger = internallogger.GetLogger("juju.api")
type rpcConnection interface {
Call(ctx context.Context, req rpc.Request, params, response interface{}) error
Dead() <-chan struct{}
Close() error
}
// 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.MachineHostPorts
// CACert holds the certificate of the remote server.
CACert string
// FollowRedirect is set to true for cases like JAAS where the client
// needs to automatically follow the redirect to the new controller.
FollowRedirect bool
// ControllerTag uniquely identifies the controller being redirected to.
ControllerTag names.ControllerTag
// An optional alias for the controller the model got redirected to.
// It can be used by the client to present the user with a more
// meaningful juju login -c XYZ command
ControllerAlias string
}
func (e *RedirectError) Error() string {
return "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(ctx context.Context, info *Info, opts DialOpts) (Connection, error) {
if err := info.Validate(); err != nil {
return nil, errors.Annotate(err, "validating info for opening an API connection")
}
if opts.Clock == nil {
opts.Clock = clock.WallClock
}
dialCtx := ctx
if opts.Timeout > 0 {
ctx1, cancel := utils.ContextWithTimeout(dialCtx, opts.Clock, opts.Timeout)
defer cancel()
dialCtx = ctx1
}
dialResult, err := dialAPI(dialCtx, info, opts)
if err != nil {
return nil, errors.Trace(err)
}
client := rpc.NewConn(jsoncodec.New(dialResult.conn), nil)
client.Start(ctx)
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
}
// 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: dialResult.addr,
primary: jujuhttp.NewHTTPTLSTransport(jujuhttp.TransportConfig{
TLSConfig: dialResult.tlsConfig,
}),
fallback: http.DefaultTransport,
}
host := PerferredHost(info)
if host == "" {
host = dialResult.addr
}
pingerFacadeVersions := facadeVersions["Pinger"]
if len(pingerFacadeVersions) == 0 {
return nil, errors.Errorf("pinger facade version is required")
}
loginProvider := opts.LoginProvider
// TODO (alesstimec, wallyworld): login provider should be constructed outside
// of this function and always passed in as part of dial opts. Also Info
// does not need to hold the authentication related data anymore. Until that
// is refactored we fall back to using the user-pass login provider
// with information from Info.
if loginProvider == nil {
loginProvider = NewLegacyLoginProvider(info.Tag, info.Password, info.Nonce, info.Macaroons, bakeryClient, CookieURLFromHost(host))
}
c := &conn{
client: client,
conn: dialResult.conn,
clock: opts.Clock,
addr: dialResult.addr,
ipAddr: dialResult.ipAddr,
cookieURL: CookieURLFromHost(host),
pingerFacadeVersion: pingerFacadeVersions[len(pingerFacadeVersions)-1],
serverScheme: "https",
serverRootAddress: dialResult.addr,
// We keep the login provider around to provide auth headers
// when doing HTTP requests.
// If login fails, we discard the connection.
loginProvider: loginProvider,
tlsConfig: dialResult.tlsConfig,
bakeryClient: bakeryClient,
modelTag: info.ModelTag,
proxier: dialResult.proxier,
}
if !info.SkipLogin {
if err := loginWithContext(dialCtx, c, loginProvider); err != nil {
dialResult.conn.Close()
return nil, errors.Trace(err)
}
}
c.broken = make(chan struct{})
c.closed = make(chan struct{})
go (&monitor{
clock: opts.Clock,
ping: c.ping,
pingPeriod: PingPeriod,
pingTimeout: pingTimeout,
closed: c.closed,
dead: client.Dead(),
broken: c.broken,
}).run()
return c, nil
}
// CookieURLFromHost creates a url.URL from a given host.
func CookieURLFromHost(host string) *url.URL {
return &url.URL{
Scheme: "https",
Host: host,
Path: "/",
}
}
// PerferredHost returns the SNI hostname or controller name for the cookie URL
// so that it is stable when used with a HA controller cluster.
func PerferredHost(info *Info) string {
if info == nil {
return ""
}
host := info.SNIHostName
if host == "" && info.ControllerUUID != "" {
host = info.ControllerUUID
}
return host
}
// loginWithContext wraps conn.Login with code that terminates
// if the context is cancelled.
// TODO(rogpeppe) pass Context into Login (and all API calls) so
// that this becomes unnecessary.
func loginWithContext(ctx context.Context, c *conn, loginProvider LoginProvider) error {
if loginProvider == nil {
return errors.New("login provider not specified")
}
result := make(chan error, 1)
go func() {
loginResult, err := loginProvider.Login(ctx, c)
if err != nil {
result <- err
return
}
result <- c.setLoginResult(loginResult)
}()
select {
case err := <-result:
return errors.Trace(err)
case <-ctx.Done():
return errors.Annotatef(ctx.Err(), "cannot log in")
}
}
// 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. The stream
// returned will apply a 30-second write deadline, so WriteJSON should
// only be called from one goroutine.
func (c *conn) ConnectStream(ctx context.Context, path string, attrs url.Values) (base.Stream, error) {
path, err := apiPath(c.modelTag.Id(), path)
if err != nil {
return nil, errors.Trace(err)
}
conn, err := c.connectStreamWithRetry(ctx, path, attrs, nil)
if err != nil {
return nil, errors.Trace(err)
}
return conn, nil
}
// ConnectControllerStream creates a stream connection to an API path
// that isn't prefixed with /model/uuid - the target model (if the
// endpoint needs one) can be specified in the headers. The stream
// returned will apply a 30-second write deadline, so WriteJSON should
// only be called from one goroutine.
func (c *conn) ConnectControllerStream(ctx context.Context, path string, attrs url.Values, headers http.Header) (base.Stream, error) {
if !strings.HasPrefix(path, "/") {
return nil, errors.Errorf("path %q is not absolute", path)
}
if strings.HasPrefix(path, modelRoot) {
return nil, errors.Errorf("path %q is model-specific", path)
}
conn, err := c.connectStreamWithRetry(ctx, path, attrs, headers)
if err != nil {
return nil, errors.Trace(err)
}
return conn, nil
}
func (c *conn) connectStreamWithRetry(ctx context.Context, path string, attrs url.Values, headers http.Header) (base.Stream, error) {
if !c.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 := c.connectStream(path, attrs, headers)
if err == nil {
return conn, err
}
if params.ErrCode(err) != params.CodeDischargeRequired {
return nil, errors.Trace(err)
}
if err := c.bakeryClient.HandleError(ctx, c.cookieURL, bakeryError(err)); err != nil {
return nil, errors.Trace(err)
}
// Try again with the discharged macaroon.
conn, err = c.connectStream(path, attrs, headers)
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 (c *conn) connectStream(path string, attrs url.Values, extraHeaders http.Header) (base.Stream, error) {
target := url.URL{
Scheme: "wss",
Host: c.addr,
Path: path,
RawQuery: attrs.Encode(),
}
// TODO(macgreagoir) IPv6. Ubuntu still always provides IPv4 loopback,
// and when/if this changes localhost should resolve to IPv6 loopback
// in any case (lp:1644009). Review.
dialer := &websocket.Dialer{
Proxy: proxy.DefaultConfig.GetProxy,
TLSClientConfig: c.tlsConfig,
}
requestHeader, err := c.loginProvider.AuthHeader()
if err != nil {
return nil, errors.Trace(err)
}
requestHeader.Set(params.JujuClientVersion, jujuversion.Current.String())
for header, values := range extraHeaders {
for _, value := range values {
requestHeader.Add(header, value)
}
}
connection, err := WebsocketDial(dialer, target.String(), requestHeader)
if err != nil {
return nil, err
}
if err := readInitialStreamError(connection); err != nil {
connection.Close()
return nil, errors.Trace(err)
}
return connection, nil
}
// readInitialStreamError reads the initial error response
// from a stream connection and returns it.
func readInitialStreamError(ws base.Stream) 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).
messageType, reader, err := ws.NextReader()
if err != nil {
return errors.Annotate(err, "unable to get reader")
}
if messageType != websocket.TextMessage {
return errors.Errorf("unexpected message type %v", messageType)
}
line, err := bufio.NewReader(reader).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
}
// apiEndpoint returns a URL that refers to the given API slash-prefixed
// endpoint path and query parameters.
func (c *conn) apiEndpoint(path, query string) (*url.URL, error) {
path, err := apiPath(c.modelTag.Id(), path)
if err != nil {
return nil, errors.Trace(err)
}
return &url.URL{
Scheme: c.serverScheme,
Host: c.Addr(),
Path: path,
RawQuery: query,
}, nil
}
// ControllerAPIURL returns the URL to use to connect to the controller API.
func ControllerAPIURL(addr string, port int) string {
hp := net.JoinHostPort(addr, strconv.Itoa(port))
urlStr, _ := url.QueryUnescape(apiURL(hp, "").String())
return urlStr
}
func apiURL(addr, model string) *url.URL {
path, _ := apiPath(model, "/api")
return &url.URL{
Scheme: "wss",
Host: addr,
Path: path,
}
}
// ping implements calls the Pinger.ping facade.
func (c *conn) ping(ctx context.Context) error {
return c.APICall(ctx, "Pinger", c.pingerFacadeVersion, "", "Ping", nil, nil)
}
// apiPath returns the given API endpoint path relative
// to the given model string.
func apiPath(model, path string) (string, error) {
if !strings.HasPrefix(path, "/") {
return "", errors.Errorf("cannot make API path from non-slash-prefixed path %q", path)
}
if model == "" {
return path, nil
}
return modelRoot + model + path, nil
}
// dialResult holds a dialed connection, the URL
// and TLS configuration used to connect to it.
type dialResult struct {
conn jsoncodec.JSONConn
addr string
urlStr string
ipAddr string
proxier jujuproxy.Proxier
tlsConfig *tls.Config
}
// Close implements io.Closer by closing the websocket
// connection. It is implemented so that a *dialResult
// value can be used as the result of a parallel.Try.
func (c *dialResult) Close() error {
return c.conn.Close()
}
// dialOpts holds the original dial options
// but adds some information for the local dial logic.
type dialOpts struct {
DialOpts
sniHostName string
// certPool holds a cert pool containing the CACert
// if there is one.
certPool *x509.CertPool
}
// 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(ctx context.Context, info *Info, opts0 DialOpts) (*dialResult, error) {
if len(info.Addrs) == 0 {
return nil, errors.New("no API addresses to connect to")
}
addrs := info.Addrs[:]
if info.Proxier != nil {
if err := info.Proxier.Start(ctx); err != nil {
return nil, errors.Annotate(err, "starting proxy for api connection")
}
logger.Debugf("starting proxier for connection")
switch p := info.Proxier.(type) {
case jujuproxy.TunnelProxier:
logger.Debugf("tunnel proxy in use at %s on port %s", p.Host(), p.Port())
addrs = []string{
net.JoinHostPort(p.Host(), p.Port()),
}
default:
info.Proxier.Stop()
return nil, errors.New("unknown proxier provided")
}
}
opts := dialOpts{
DialOpts: opts0,
sniHostName: info.SNIHostName,
}
if info.CACert != "" {
certPool, err := CreateCertPool(info.CACert)
if err != nil {
return nil, errors.Annotate(err, "cert pool creation failed")
}
opts.certPool = certPool
}
// Set opts.DialWebsocket and opts.Clock here rather than in open because
// some tests call dialAPI directly.
if opts.DialWebsocket == nil {
opts.DialWebsocket = gorillaDialWebsocket
}
if opts.IPAddrResolver == nil {
opts.IPAddrResolver = net.DefaultResolver
}
if opts.Clock == nil {
opts.Clock = clock.WallClock
}
if opts.DNSCache == nil {
opts.DNSCache = nopDNSCache{}
}
path, err := apiPath(info.ModelTag.Id(), "/api")
if err != nil {
return nil, errors.Trace(err)
}
// Encourage load balancing by shuffling controller addresses.
rand.Shuffle(len(addrs), func(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] })
if opts.VerifyCA != nil {
if err := verifyCAMulti(ctx, addrs, &opts); err != nil {
return nil, err
}
}
if opts.DialTimeout > 0 {
ctx1, cancel := utils.ContextWithTimeout(ctx, opts.Clock, opts.DialTimeout)
defer cancel()
ctx = ctx1
}
dialInfo, err := dialWebsocketMulti(ctx, addrs, path, opts)
if err != nil {
return nil, errors.Trace(err)
}
logger.Infof("connection established to %q", dialInfo.urlStr)
dialInfo.proxier = info.Proxier
return dialInfo, nil
}
// gorillaDialWebsocket makes a websocket connection using the
// gorilla websocket package. The ipAddr parameter holds the
// actual IP address that will be contacted - the host in urlStr
// is used only for TLS verification when tlsConfig.ServerName
// is empty.
func gorillaDialWebsocket(ctx context.Context, urlStr string, tlsConfig *tls.Config, ipAddr string) (jsoncodec.JSONConn, error) {
url, err := url.Parse(urlStr)
if err != nil {
return nil, errors.Trace(err)
}
// TODO(rogpeppe) We'd like to set Deadline here
// but that would break lots of tests that rely on
// setting a zero timeout.
netDialer := net.Dialer{}
dialer := &websocket.Dialer{
NetDial: func(netw, addr string) (net.Conn, error) {
if addr == url.Host {
// Use pre-resolved IP address. The address
// may be different if a proxy is in use.
addr = ipAddr
}
return netDialer.DialContext(ctx, netw, addr)
},
Proxy: proxy.DefaultConfig.GetProxy,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: tlsConfig,
}
// Note: no extra headers.
c, resp, err := dialer.Dial(urlStr, nil)
if err != nil {
if err == websocket.ErrBadHandshake {
// If ErrBadHandshake is returned, a non-nil response
// is returned so the client can react to auth errors
// (for example).
defer resp.Body.Close()
body, readErr := io.ReadAll(resp.Body)
if readErr == nil {
err = errors.Errorf(
"%s (%s)",
strings.TrimSpace(string(body)),
http.StatusText(resp.StatusCode),
)
}
}
return nil, errors.Trace(err)
}
return jsoncodec.NewWebsocketConn(c), nil
}
type resolvedAddress struct {
host string
ip string
port string
}
type addressProvider struct {
dnsCache DNSCache
ipAddrResolver IPAddrResolver
// A pool of host addresses to be resolved to one or more IP addresses.
addrPool []string
// A pool of host addresses that got resolved via the DNS cache; these
// are kept separate so we can attempt to resolve them without the DNS
// cache when we run out of entries in AddrPool.
cachedAddrPool []string
resolvedAddrs []*resolvedAddress
}
func newAddressProvider(initialAddrs []string, dnsCache DNSCache, ipAddrResolver IPAddrResolver) *addressProvider {
return &addressProvider{
dnsCache: dnsCache,
ipAddrResolver: ipAddrResolver,
addrPool: initialAddrs,
}
}
// next returns back either a successfully resolved address or the error that
// occurred while attempting to resolve the next address candidate. Calls to
// next return io.EOF to indicate that no more addresses are available.
func (ap *addressProvider) next(ctx context.Context) (*resolvedAddress, error) {
if len(ap.resolvedAddrs) == 0 {
// If we have ran out of addresses to resolve but we have
// resolved some via the DNS cache, make another pass for
// those with an empty DNS cache to refresh any stale entries.
if len(ap.addrPool) == 0 && len(ap.cachedAddrPool) > 0 {
ap.addrPool = ap.cachedAddrPool
ap.cachedAddrPool = nil
ap.dnsCache = emptyDNSCache{ap.dnsCache}
}
// Resolve the next host from the address pool
if len(ap.addrPool) != 0 {
next := ap.addrPool[0]
ap.addrPool = ap.addrPool[1:]
host, port, err := net.SplitHostPort(next)
if err != nil {
return nil, errors.Errorf("invalid address %q: %v", next, err)
}
ips := ap.dnsCache.Lookup(host)
if len(ips) > 0 {
ap.cachedAddrPool = append(ap.cachedAddrPool, next)
} else if isNumericHost(host) {
ips = []string{host}
} else {
var err error
ips, err = lookupIPAddr(ctx, host, ap.ipAddrResolver)
if err != nil {
return nil, errors.Errorf("cannot resolve %q: %v", host, err)
}
ap.dnsCache.Add(host, ips)
logger.Debugf("looked up %v -> %v", host, ips)
}
for _, ip := range ips {
ap.resolvedAddrs = append(ap.resolvedAddrs, &resolvedAddress{
host: next,
ip: ip,
port: port,
})
}
}
}
// Ran out of resolved addresses and cached addresses
if len(ap.resolvedAddrs) == 0 {
return nil, io.EOF
}
next := ap.resolvedAddrs[0]
ap.resolvedAddrs = ap.resolvedAddrs[1:]
return next, nil
}
// caRetrieveRes is an adaptor for returning CA certificate lookup results via
// calls to parallel.Try.
type caRetrieveRes struct {
host string
endpoint string
caCert *x509.Certificate
}
func (caRetrieveRes) Close() error { return nil }
// verifyCAMulti attempts to establish a TLS connection with one of the
// provided addresses, retrieve the CA certificate and validate it using the
// system root CAs. If that is not possible, the certificate verification will
// be delegated to the VerifyCA implementation specified in opts.DialOpts.
//
// If VerifyCA does not return an error, the CA cert is assumed to be trusted
// and will be appended to opt's certificate pool allowing secure websocket
// connections to proceed without certificate verification errors. Otherwise,
// the error reported by VerifyCA is returned back to the caller.
//
// For load-balancing purposes, all addresses are tested concurrently with the
// first retrieved CA cert being used for the verification tests. In addition,
// apart from the initial TLS handshake with the remote server, no other data
// is exchanged with the remote server.
func verifyCAMulti(ctx context.Context, addrs []string, opts *dialOpts) error {
dOpts := opts.DialOpts
if dOpts.DialTimeout > 0 {
ctx1, cancel := utils.ContextWithTimeout(ctx, dOpts.Clock, dOpts.DialTimeout)
defer cancel()
ctx = ctx1
}
try := parallel.NewTry(0, nil)
defer try.Kill()
addrProvider := newAddressProvider(addrs, opts.DNSCache, opts.IPAddrResolver)
tryRetrieveCaCertFn := func(ctx context.Context, addr *resolvedAddress) func(<-chan struct{}) (io.Closer, error) {
ipStr := net.JoinHostPort(addr.ip, addr.port)
return func(<-chan struct{}) (io.Closer, error) {
caCert, err := retrieveCACert(ctx, ipStr)
if err != nil {
return nil, err
}
return caRetrieveRes{
host: addr.host,
endpoint: ipStr,
caCert: caCert,
}, nil
}
}
for {
resolvedAddr, err := addrProvider.next(ctx)
if err == io.EOF {
break
} else if err != nil {
recordTryError(try, err)
continue
}
err = try.Start(tryRetrieveCaCertFn(ctx, resolvedAddr))
if err == parallel.ErrStopped {
break
} else if err != nil {
continue
}
select {
case <-opts.Clock.After(dOpts.DialAddressInterval):
case <-try.Dead():
}
}
try.Close()
// If we are unable to fetch the CA either because it is not presented
// by the remote server OR due to an unsuccessful connection attempt
// we should skip the verification path and dial the server as if no
// VerifyCA implementation was provided.
result, err := try.Result()
if err != nil || result == nil {
logger.Debugf("unable to retrieve CA cert from remote host; skipping CA verification")
return nil
}
// Try to verify CA cert using the system roots. If the verification
// succeeds then we are done; tls connections will work out of the box.
res := result.(caRetrieveRes)
if _, err = res.caCert.Verify(x509.VerifyOptions{}); err == nil {
logger.Debugf("remote CA certificate trusted by system roots")
return nil
}
// Invoke the CA verifier; if the CA should be trusted, append it to
// the dialOpts certPool and proceed with the actual connection attempt.
err = opts.VerifyCA(res.host, res.endpoint, res.caCert)
if err == nil {
if opts.certPool == nil {
opts.certPool = x509.NewCertPool()
}
opts.certPool.AddCert(res.caCert)
}
return err
}
// retrieveCACert establishes an insecure TLS connection to addr and attempts
// to retrieve the CA cert presented by the server. If no CA cert is presented,
// retrieveCACert will returns nil, nil.
func retrieveCACert(ctx context.Context, addr string) (*x509.Certificate, error) {
netConn, err := new(net.Dialer).DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
conn := tls.Client(netConn, &tls.Config{InsecureSkipVerify: true})
if err = conn.Handshake(); err != nil {
_ = netConn.Close()
return nil, err
}
defer func() {
_ = conn.Close()
_ = netConn.Close()
}()
for _, cert := range conn.ConnectionState().PeerCertificates {
if cert.IsCA {
return cert, nil
}
}
return nil, errors.New("no CA certificate presented by remote server")
}
// 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(ctx context.Context, addrs []string, path string, opts dialOpts) (*dialResult, error) {
// Prioritise non-dial errors over the normal "connection refused".
isDialError := func(err error) bool {
netErr, ok := errors.Cause(err).(*net.OpError)
if !ok {
return false
}
return netErr.Op == "dial"
}
combine := func(initial, other error) error {
if initial == nil || isDialError(initial) {
return other
}
if isDialError(other) {
return initial
}
return other
}
// Dial all addresses at reasonable intervals.
try := parallel.NewTry(0, combine)
defer try.Kill()
// Make a context that's cancelled when the try
// completes so that (for example) a slow DNS
// query will be cancelled if a previous try succeeds.
ctx, cancel := context.WithCancel(ctx)
go func() {
<-try.Dead()
cancel()
}()
tried := make(map[string]bool)
addrProvider := newAddressProvider(addrs, opts.DNSCache, opts.IPAddrResolver)
for {
resolvedAddr, err := addrProvider.next(ctx)
if err == io.EOF {
break
} else if err != nil {
recordTryError(try, err)
continue
}
ipStr := net.JoinHostPort(resolvedAddr.ip, resolvedAddr.port)
if tried[ipStr] {
continue
}
tried[ipStr] = true
err = startDialWebsocket(ctx, try, ipStr, resolvedAddr.host, path, opts)
if err == parallel.ErrStopped {
break
}
if err != nil {
return nil, errors.Trace(err)
}
select {
case <-opts.Clock.After(opts.DialAddressInterval):
case <-try.Dead():
}
}
try.Close()
result, err := try.Result()
if err != nil {
return nil, errors.Trace(err)
}
return result.(*dialResult), nil
}
func lookupIPAddr(ctx context.Context, host string, resolver IPAddrResolver) ([]string, error) {
addrs, err := resolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, errors.Trace(err)
}
ips := make([]string, 0, len(addrs))
for _, addr := range addrs {
if addr.Zone != "" {
// Ignore IPv6 zone. Hopefully this shouldn't
// cause any problems in practice.
logger.Infof("ignoring IP address with zone %q", addr)
continue
}
ips = append(ips, addr.IP.String())
}
return ips, nil
}
// recordTryError starts a try that just returns the given error.
// This is so that we can use the usual Try error combination
// logic even for errors that happen before we start a try.
func recordTryError(try *parallel.Try, err error) {
logger.Infof("%v", err)
_ = try.Start(func(_ <-chan struct{}) (io.Closer, error) {
return nil, errors.Trace(err)
})
}
var oneAttempt = retry.LimitCount(1, retry.Regular{
Min: 1,
})
// startDialWebsocket starts websocket connection to a single address
// on the given try instance.
func startDialWebsocket(ctx context.Context, try *parallel.Try, ipAddr, addr, path string, opts dialOpts) error {
var openAttempt retry.Strategy
if opts.RetryDelay > 0 {
openAttempt = retry.Regular{
Total: opts.Timeout,
Delay: opts.RetryDelay,
Min: int(opts.Timeout / opts.RetryDelay),
}
} else {
// Zero retry delay implies exactly one try.
openAttempt = oneAttempt
}
d := dialer{
ctx: ctx,
openAttempt: openAttempt,
serverName: opts.sniHostName,
ipAddr: ipAddr,
urlStr: "wss://" + addr + path,
addr: addr,
opts: opts,
}
return try.Start(d.dial)
}
type dialer struct {
ctx context.Context
openAttempt retry.Strategy
// serverName holds the SNI name to use
// when connecting with a public certificate.
serverName string
// addr holds the host:port that is being dialed.
addr string
// addr holds the ipaddr:port (one of the addresses
// that addr resolves to) that is being dialed.
ipAddr string
// urlStr holds the URL that is being dialed.
urlStr string
// opts holds the dial options.
opts dialOpts
}
// dial implements the function value expected by Try.Start
// by dialing the websocket as specified in d and retrying
// when appropriate.
func (d dialer) dial(_ <-chan struct{}) (io.Closer, error) {
a := retry.StartWithCancel(d.openAttempt, d.opts.Clock, d.ctx.Done())
var lastErr error = nil
for a.Next() {
conn, tlsConfig, err := d.dial1()
if err == nil {
return &dialResult{
conn: conn,
addr: d.addr,