-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiclient.go
366 lines (321 loc) · 10.1 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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"strings"
"time"
"code.google.com/p/go.net/websocket"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils"
"github.com/juju/utils/parallel"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cert"
"github.com/juju/juju/network"
"github.com/juju/juju/rpc"
"github.com/juju/juju/rpc/jsoncodec"
)
var logger = loggo.GetLogger("juju.api")
// PingPeriod defines how often the internal connection health check
// will run. It's a variable so it can be changed in tests.
var PingPeriod = 1 * time.Minute
type State struct {
client *rpc.Conn
conn *websocket.Conn
// addr is the address used to connect to the API server.
addr string
// environTag holds the environment tag once we're connected
environTag string
// 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
// authTag holds the authenticated entity's tag after login.
authTag names.Tag
// 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{}
// tag and password hold the cached login credentials.
tag string
password string
// serverRoot holds the cached API server address and port we used
// to login, with a https:// prefix.
serverRoot string
// certPool holds the cert pool that is used to authenticate the tls
// connections to the API.
certPool *x509.CertPool
}
// Info encapsulates information about a server holding juju state and
// can be used to make a connection to it.
type Info struct {
// Addrs holds the addresses of the state servers.
Addrs []string
// CACert holds the CA certificate that will be used
// to validate the state server's certificate, in PEM format.
CACert string
// Tag holds the name of the entity that is connecting.
// If this is nil, and the password are empty, no login attempt will be made.
// (this is to allow tests to access the API to check that operations
// fail when not logged in).
Tag names.Tag
// Password holds the password for the administrator or connecting entity.
Password string
// Nonce holds the nonce used when provisioning the machine. Used
// only by the machine agent.
Nonce string `yaml:",omitempty"`
// EnvironTag holds the environ tag for the environment we are
// trying to connect to.
EnvironTag names.EnvironTag
}
// DialOpts holds configuration parameters that control the
// Dialing behavior when connecting to a state server.
type DialOpts struct {
// DialAddressInterval is the amount of time to wait
// before starting to dial another address.
DialAddressInterval time.Duration
// Timeout is the amount of time to wait contacting
// a state server.
Timeout time.Duration
// RetryDelay is the amount of time to wait between
// unsucssful connection attempts.
RetryDelay time.Duration
}
// DefaultDialOpts returns a DialOpts representing the default
// parameters for contacting a state server.
func DefaultDialOpts() DialOpts {
return DialOpts{
DialAddressInterval: 50 * time.Millisecond,
Timeout: 10 * time.Minute,
RetryDelay: 2 * time.Second,
}
}
func Open(info *Info, opts DialOpts) (*State, error) {
if len(info.Addrs) == 0 {
return nil, fmt.Errorf("no API addresses to connect to")
}
pool := x509.NewCertPool()
xcert, err := cert.ParseCert(info.CACert)
if err != nil {
return nil, err
}
pool.AddCert(xcert)
var environUUID string
if info.EnvironTag.Id() != "" {
environUUID = info.EnvironTag.Id()
}
// Dial all addresses at reasonable intervals.
try := parallel.NewTry(0, nil)
defer try.Kill()
var addrs []string
for _, addr := range info.Addrs {
if strings.HasPrefix(addr, "localhost:") {
addrs = append(addrs, addr)
break
}
}
if len(addrs) == 0 {
addrs = info.Addrs
}
for _, addr := range addrs {
err := dialWebsocket(addr, environUUID, opts, pool, try)
if err == parallel.ErrStopped {
break
}
if err != nil {
return nil, err
}
select {
case <-time.After(opts.DialAddressInterval):
case <-try.Dead():
}
}
try.Close()
result, err := try.Result()
if err != nil {
return nil, err
}
conn := result.(*websocket.Conn)
logger.Infof("connection established to %q", conn.RemoteAddr())
client := rpc.NewConn(jsoncodec.NewWebsocket(conn), nil)
client.Start()
st := &State{
client: client,
conn: conn,
addr: conn.Config().Location.Host,
serverRoot: "https://" + conn.Config().Location.Host,
// why are the contents of the tag (username and password) written into the
// state structure BEFORE login ?!?
tag: toString(info.Tag),
password: info.Password,
certPool: pool,
}
if info.Tag != nil || info.Password != "" {
if err := st.Login(info.Tag.String(), info.Password, info.Nonce); err != nil {
conn.Close()
return nil, err
}
}
st.broken = make(chan struct{})
st.closed = make(chan struct{})
go st.heartbeatMonitor()
return st, nil
}
// toString returns the value of a tag's String method, or "" if the tag is nil.
func toString(tag names.Tag) string {
if tag == nil {
return ""
}
return tag.String()
}
func dialWebsocket(addr, environUUID string, opts DialOpts, rootCAs *x509.CertPool, try *parallel.Try) error {
cfg, err := setUpWebsocket(addr, environUUID, rootCAs)
if err != nil {
return err
}
return try.Start(newWebsocketDialer(cfg, opts))
}
func setUpWebsocket(addr, environUUID string, rootCAs *x509.CertPool) (*websocket.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/"
tail := "/"
if environUUID != "" {
tail = "/environment/" + environUUID + "/api"
}
cfg, err := websocket.NewConfig("wss://"+addr+tail, origin)
if err != nil {
return nil, err
}
cfg.TlsConfig = &tls.Config{
RootCAs: rootCAs,
ServerName: "juju-apiserver",
}
return cfg, nil
}
// newWebsocketDialer returns a function that
// can be passed to utils/parallel.Try.Start.
func newWebsocketDialer(cfg *websocket.Config, opts DialOpts) func(<-chan struct{}) (io.Closer, error) {
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 := websocket.DialConfig(cfg)
if err == nil {
return conn, nil
}
if a.HasNext() {
logger.Debugf("error dialing %q, will retry: %v", cfg.Location, err)
} else {
logger.Infof("error dialing %q: %v", cfg.Location, err)
return nil, fmt.Errorf("unable to connect to %q", cfg.Location)
}
}
panic("unreachable")
}
}
func (s *State) heartbeatMonitor() {
for {
if err := s.Ping(); err != nil {
close(s.broken)
return
}
select {
case <-time.After(PingPeriod):
case <-s.closed:
}
}
}
func (s *State) Ping() error {
return s.APICall("Pinger", s.BestFacadeVersion("Pinger"), "", "Ping", nil, nil)
}
// 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 {
err := s.client.Call(rpc.Request{
Type: facade,
Version: version,
Id: id,
Action: method,
}, args, response)
return params.ClientError(err)
}
func (s *State) Close() error {
err := s.client.Close()
select {
case <-s.closed:
default:
close(s.closed)
}
<-s.broken
return err
}
// Broken returns a channel that's closed when the connection is broken.
func (s *State) Broken() <-chan struct{} {
return s.broken
}
// RPCClient returns the RPC client for the state, so that testing
// functions can tickle parts of the API that the conventional entry
// points don't reach. This is exported for testing purposes only.
func (s *State) RPCClient() *rpc.Conn {
return s.client
}
// Addr returns the address used to connect to the API server.
func (s *State) Addr() string {
return s.addr
}
// EnvironTag returns the tag of the environment we are connected to.
func (s *State) EnvironTag() (names.EnvironTag, error) {
return names.ParseEnvironTag(s.environTag)
}
// 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 environment (think
// private clouds).
func (s *State) APIHostPorts() [][]network.HostPort {
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])
}