forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxd.go
408 lines (334 loc) · 9.63 KB
/
lxd.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
package lxd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/gorilla/websocket"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon-bakery.v2/httpbakery"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/logger"
neturl "net/url"
)
// ProtocolLXD represents a LXD API server
type ProtocolLXD struct {
server *api.Server
chConnected chan struct{}
eventListeners []*EventListener
eventListenersLock sync.Mutex
http *http.Client
httpCertificate string
httpHost string
httpUnixPath string
httpProtocol string
httpUserAgent string
bakeryClient *httpbakery.Client
bakeryInteractor []httpbakery.Interactor
requireAuthenticated bool
clusterTarget string
project string
}
// Disconnect gets rid of any background goroutines
func (r *ProtocolLXD) Disconnect() {
if r.chConnected != nil {
close(r.chConnected)
}
}
// GetConnectionInfo returns the basic connection information used to interact with the server
func (r *ProtocolLXD) GetConnectionInfo() (*ConnectionInfo, error) {
info := ConnectionInfo{}
info.Certificate = r.httpCertificate
info.Protocol = "lxd"
info.URL = r.httpHost
info.SocketPath = r.httpUnixPath
info.Project = r.project
if info.Project == "" {
info.Project = "default"
}
urls := []string{}
if r.httpProtocol == "https" {
urls = append(urls, r.httpHost)
}
if r.server != nil && len(r.server.Environment.Addresses) > 0 {
for _, addr := range r.server.Environment.Addresses {
if strings.HasPrefix(addr, ":") {
continue
}
url := fmt.Sprintf("https://%s", addr)
if !shared.StringInSlice(url, urls) {
urls = append(urls, url)
}
}
}
info.Addresses = urls
return &info, nil
}
// GetHTTPClient returns the http client used for the connection. This can be used to set custom http options.
func (r *ProtocolLXD) GetHTTPClient() (*http.Client, error) {
if r.http == nil {
return nil, fmt.Errorf("HTTP client isn't set, bad connection")
}
return r.http, nil
}
// Do performs a Request, using macaroon authentication if set.
func (r *ProtocolLXD) do(req *http.Request) (*http.Response, error) {
if r.bakeryClient != nil {
r.addMacaroonHeaders(req)
return r.bakeryClient.Do(req)
}
return r.http.Do(req)
}
func (r *ProtocolLXD) addMacaroonHeaders(req *http.Request) {
req.Header.Set(httpbakery.BakeryProtocolHeader, fmt.Sprint(bakery.LatestVersion))
for _, cookie := range r.http.Jar.Cookies(req.URL) {
req.AddCookie(cookie)
}
}
// RequireAuthenticated sets whether we expect to be authenticated with the server
func (r *ProtocolLXD) RequireAuthenticated(authenticated bool) {
r.requireAuthenticated = authenticated
}
// RawQuery allows directly querying the LXD API
//
// This should only be used by internal LXD tools.
func (r *ProtocolLXD) RawQuery(method string, path string, data interface{}, ETag string) (*api.Response, string, error) {
// Generate the URL
url := fmt.Sprintf("%s%s", r.httpHost, path)
return r.rawQuery(method, url, data, ETag)
}
// RawWebsocket allows directly connection to LXD API websockets
//
// This should only be used by internal LXD tools.
func (r *ProtocolLXD) RawWebsocket(path string) (*websocket.Conn, error) {
return r.websocket(path)
}
// RawOperation allows direct querying of a LXD API endpoint returning
// background operations.
func (r *ProtocolLXD) RawOperation(method string, path string, data interface{}, ETag string) (Operation, string, error) {
return r.queryOperation(method, path, data, ETag)
}
// Internal functions
func lxdParseResponse(resp *http.Response) (*api.Response, string, error) {
// Get the ETag
etag := resp.Header.Get("ETag")
// Decode the response
decoder := json.NewDecoder(resp.Body)
response := api.Response{}
err := decoder.Decode(&response)
if err != nil {
// Check the return value for a cleaner error
if resp.StatusCode != http.StatusOK {
return nil, "", fmt.Errorf("Failed to fetch %s: %s", resp.Request.URL.String(), resp.Status)
}
return nil, "", err
}
// Handle errors
if response.Type == api.ErrorResponse {
return nil, "", fmt.Errorf(response.Error)
}
return &response, etag, nil
}
func (r *ProtocolLXD) rawQuery(method string, url string, data interface{}, ETag string) (*api.Response, string, error) {
var req *http.Request
var err error
// Log the request
logger.Debug("Sending request to LXD",
"method", method,
"url", url,
"etag", ETag,
)
// Get a new HTTP request setup
if data != nil {
switch data := data.(type) {
case io.Reader:
// Some data to be sent along with the request
req, err = http.NewRequest(method, url, data)
if err != nil {
return nil, "", err
}
// Set the encoding accordingly
req.Header.Set("Content-Type", "application/octet-stream")
default:
// Encode the provided data
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(data)
if err != nil {
return nil, "", err
}
// Some data to be sent along with the request
// Use a reader since the request body needs to be seekable
req, err = http.NewRequest(method, url, bytes.NewReader(buf.Bytes()))
if err != nil {
return nil, "", err
}
// Set the encoding accordingly
req.Header.Set("Content-Type", "application/json")
// Log the data
logger.Debugf(logger.Pretty(data))
}
} else {
// No data to be sent along with the request
req, err = http.NewRequest(method, url, nil)
if err != nil {
return nil, "", err
}
}
// Set the user agent
if r.httpUserAgent != "" {
req.Header.Set("User-Agent", r.httpUserAgent)
}
// Set the ETag
if ETag != "" {
req.Header.Set("If-Match", ETag)
}
// Set the authentication header
if r.requireAuthenticated {
req.Header.Set("X-LXD-authenticated", "true")
}
// Send the request
resp, err := r.do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
return lxdParseResponse(resp)
}
func (r *ProtocolLXD) setQueryAttributes(uri string) (string, error) {
// Parse the full URI
fields, err := neturl.Parse(uri)
if err != nil {
return "", err
}
// Extract query fields and update for cluster targeting or project
values := fields.Query()
if r.clusterTarget != "" {
if values.Get("target") == "" {
values.Set("target", r.clusterTarget)
}
}
if r.project != "" {
if values.Get("project") == "" {
values.Set("project", r.project)
}
}
fields.RawQuery = values.Encode()
return fields.String(), nil
}
func (r *ProtocolLXD) query(method string, path string, data interface{}, ETag string) (*api.Response, string, error) {
// Generate the URL
url := fmt.Sprintf("%s/1.0%s", r.httpHost, path)
// Add project/target
url, err := r.setQueryAttributes(url)
if err != nil {
return nil, "", err
}
// Run the actual query
return r.rawQuery(method, url, data, ETag)
}
func (r *ProtocolLXD) queryStruct(method string, path string, data interface{}, ETag string, target interface{}) (string, error) {
resp, etag, err := r.query(method, path, data, ETag)
if err != nil {
return "", err
}
err = resp.MetadataAsStruct(&target)
if err != nil {
return "", err
}
// Log the data
logger.Debugf("Got response struct from LXD")
logger.Debugf(logger.Pretty(target))
return etag, nil
}
func (r *ProtocolLXD) queryOperation(method string, path string, data interface{}, ETag string) (Operation, string, error) {
// Attempt to setup an early event listener
listener, err := r.GetEvents()
if err != nil {
listener = nil
}
// Send the query
resp, etag, err := r.query(method, path, data, ETag)
if err != nil {
if listener != nil {
listener.Disconnect()
}
return nil, "", err
}
// Get to the operation
respOperation, err := resp.MetadataAsOperation()
if err != nil {
if listener != nil {
listener.Disconnect()
}
return nil, "", err
}
// Setup an Operation wrapper
op := operation{
Operation: *respOperation,
r: r,
listener: listener,
chActive: make(chan bool),
}
// Log the data
logger.Debugf("Got operation from LXD")
logger.Debugf(logger.Pretty(op.Operation))
return &op, etag, nil
}
func (r *ProtocolLXD) rawWebsocket(url string) (*websocket.Conn, error) {
// Grab the http transport handler
httpTransport := r.http.Transport.(*http.Transport)
// Setup a new websocket dialer based on it
dialer := websocket.Dialer{
NetDial: httpTransport.Dial,
TLSClientConfig: httpTransport.TLSClientConfig,
Proxy: httpTransport.Proxy,
}
// Set the user agent
headers := http.Header{}
if r.httpUserAgent != "" {
headers.Set("User-Agent", r.httpUserAgent)
}
if r.requireAuthenticated {
headers.Set("X-LXD-authenticated", "true")
}
// Set macaroon headers if needed
if r.bakeryClient != nil {
u, err := neturl.Parse(r.httpHost) // use the http url, not the ws one
if err != nil {
return nil, err
}
req := &http.Request{URL: u, Header: headers}
r.addMacaroonHeaders(req)
}
// Establish the connection
conn, _, err := dialer.Dial(url, headers)
if err != nil {
return nil, err
}
// Log the data
logger.Debugf("Connected to the websocket: %v", url)
return conn, err
}
func (r *ProtocolLXD) websocket(path string) (*websocket.Conn, error) {
// Generate the URL
var url string
if strings.HasPrefix(r.httpHost, "https://") {
url = fmt.Sprintf("wss://%s/1.0%s", strings.TrimPrefix(r.httpHost, "https://"), path)
} else {
url = fmt.Sprintf("ws://%s/1.0%s", strings.TrimPrefix(r.httpHost, "http://"), path)
}
return r.rawWebsocket(url)
}
func (r *ProtocolLXD) setupBakeryClient() {
r.bakeryClient = httpbakery.NewClient()
r.bakeryClient.Client = r.http
if r.bakeryInteractor != nil {
for _, interactor := range r.bakeryInteractor {
r.bakeryClient.AddInteractor(interactor)
}
}
}