-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
582 lines (486 loc) · 14.8 KB
/
driver.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
package triton
import (
"bytes"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/mcnflag"
)
const (
driverName = "triton"
TritonDefaultDockerPort = 2376
TritonDefaultCloudapiDomain = "api.joyent.com"
)
var CreateHack = false
type Driver struct {
*drivers.BaseDriver
CloudApiURL string
DockerApiURL string
DataCenter string
Account string
PrivateKey string
SkipTlsVerify bool
}
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "triton-url",
Usage: "Triton cloudapi URL",
Value: "",
EnvVar: "SDC_URL",
},
mcnflag.StringFlag{
Name: "triton-datacenter",
Usage: "Triton datacenter name",
Value: "us-east-1",
EnvVar: "SDC_DC",
},
mcnflag.StringFlag{
Name: "triton-account",
Usage: "Triton account name",
Value: "",
EnvVar: "SDC_ACCOUNT",
},
mcnflag.StringFlag{
Name: "triton-key",
Usage: "SSH private key for Triton authentication",
Value: "",
EnvVar: "SDC_KEY",
},
mcnflag.BoolFlag{
Name: "triton-skip-tls-verify",
Usage: "Skip tls verification 'true' or 'false' (defaults to 'false')",
EnvVar: "SDC_SKIP_TLS_VERIFY",
},
}
}
func NewDriver(machineName string, storePath string) Driver {
return Driver{
BaseDriver: &drivers.BaseDriver{
MachineName: machineName,
StorePath: storePath,
},
}
}
/* --------------------------------------------------------- */
/* Implement the drivers.Driver interface. */
/* --------------------------------------------------------- */
// AuthorizePort authorizes a port for machine access
//func (d *Driver) AuthorizePort(ports []*drivers.Port) error {
// return nil
//}
// Create a host using the driver's config
func (d *Driver) Create() error {
CreateHack = true
log.Infof("Generating %s user certificates - you will be prompted for", driverName)
log.Infof("your SSH private key password (if it's password protected).")
err := d.RegisterWithSdcCloudApi()
if err != nil {
return err
}
err = d.GenerateCertificates()
if err != nil {
return err
}
log.Infof("Success!")
return nil
}
// DeauthorizePort removes a port for machine access
//func (d *Driver) DeauthorizePort(ports []*drivers.Port) error {
// return nil
//}
// DriverName returns the name of the driver as it is registered
func (d *Driver) DriverName() string {
/**
* Overriding the driver name to avoid SSH provisioning - issue #886
*
* We only need override the driver name in the 'create' step, so this
* approximately checks if this is a create.
*/
if CreateHack {
return "none"
}
return driverName
}
// GetIP returns an IP or hostname that this host is available at
// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net
func (d *Driver) GetIP() (string, error) {
// DockerApiURL looks like: 'tcp://foo.bar:2376'
u, err := url.Parse(d.DockerApiURL)
if err != nil {
return "", err
}
return strings.Split(u.Host, ":")[0], nil
}
// GetMachineName returns the name of the machine
func (d *Driver) GetMachineName() string {
return d.MachineName
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return "", fmt.Errorf("SSH is not available for the triton driver")
}
// GetSSHPort returns port for use with ssh
func (d *Driver) GetSSHPort() (int, error) {
return 22, nil
}
// GetSSHUsername returns username for use with ssh
func (d *Driver) GetSSHUsername() string {
return d.Account
}
// GetSSHKeyPath returns key path for use with ssh
func (d *Driver) GetSSHKeyPath() string {
return d.PrivateKey
}
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
func (d *Driver) GetURL() (string, error) {
return d.DockerApiURL, nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
u := d.GetHttpsURL()
resp, err := client.Get(fmt.Sprintf("%s/_ping", u))
if err != nil {
return state.Error, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if string(body[:]) == "OK" {
return state.Running, nil
}
return state.None, nil
}
// Kill stops a host forcefully
func (d *Driver) Kill() error {
return fmt.Errorf("Kill is not available for the triton driver")
}
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
func (d *Driver) PreCreateCheck() error {
return nil
}
// Remove a host
func (d *Driver) Remove() error {
return nil
}
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func (d *Driver) Restart() error {
return fmt.Errorf("Restart is not available for the triton driver")
}
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.CloudApiURL = flags.String("triton-url")
d.Account = flags.String("triton-account")
d.DataCenter = flags.String("triton-datacenter")
d.PrivateKey = flags.String("triton-key")
d.SkipTlsVerify = flags.Bool("triton-skip-tls-verify")
if d.CloudApiURL == "" {
if d.DataCenter == "" {
return fmt.Errorf("You must specify a cloudapi url or datacenter name")
}
// Shortend format for the cloudapi name, e.g. "us-east-1"
d.CloudApiURL = fmt.Sprintf("https://%s.%s", d.DataCenter, TritonDefaultCloudapiDomain)
}
if d.Account == "" {
return fmt.Errorf("You must specify the account name using --triton-account")
}
if d.PrivateKey == "" {
homedir := mcnutils.GetHomeDir()
if homedir == "" {
return fmt.Errorf("You must specify the SSH key using --triton-key")
}
d.PrivateKey = path.Join(homedir, ".ssh", "id_rsa")
}
_, err := os.Stat(d.PrivateKey)
if err != nil {
return fmt.Errorf("Unable to access SSH key file %s", d.PrivateKey)
}
log.Debugf("CloudApiURL: %s", d.CloudApiURL)
log.Debugf("Account: %s", d.Account)
log.Debugf("DataCenter: %s", d.DataCenter)
log.Debugf("PrivateKey: %s", d.PrivateKey)
log.Debugf("SkipTlsVerify: %d", d.SkipTlsVerify)
return nil
}
// Start a host
func (d *Driver) Start() error {
return fmt.Errorf("Start is not available for the triton driver")
}
// Stop a host gracefully
func (d *Driver) Stop() error {
return fmt.Errorf("Stop is not available for the triton driver")
}
/* --------------------------------------------------------- */
/* End of driver.Driver */
/* --------------------------------------------------------- */
// GetHttpsURL returns a https url to the docker endpoint
// e.g. https://1.2.3.4:2376
func (d *Driver) GetHttpsURL() string {
return strings.Replace(d.DockerApiURL, "tcp://", "https://", 1)
}
/*
* Download the certificate authority file from the sdc-docker server.
*/
func (d *Driver) DownloadCa() error {
dockerHttpsUrl := d.GetHttpsURL()
caUrl := fmt.Sprintf("%s/ca.pem", dockerHttpsUrl)
log.Debugf("Downloading ca.pem file from %s", caUrl)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: d.SkipTlsVerify},
},
}
resp, err := client.Get(caUrl)
if err != nil {
log.Debugf("Unable to open http request to url: %s", caUrl)
return err
}
defer resp.Body.Close()
caFile := d.ResolveStorePath("ca.pem")
log.Debugf("CA: %s", caFile)
out, err := os.Create(caFile)
if err != nil {
log.Debugf("Unable to create ca.pm: %s", caFile)
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func RunCommand(cmd []string, stdin string) (string, string, error) {
var stdinPipe io.WriteCloser
var err error
log.Debugf("runCommand %s stdin length %d", cmd, len(stdin))
args := cmd[1:]
subProcess := exec.Command(cmd[0], args...)
if stdin != "" {
stdinPipe, err = subProcess.StdinPipe()
if err != nil {
log.Debugf("runCommand subProcess.StdinPipe failed")
return "", "", err
}
}
var bufout bytes.Buffer
var buferr bytes.Buffer
subProcess.Stdout = &bufout
subProcess.Stderr = &buferr
err = subProcess.Start()
if err != nil {
log.Debugf("runCommand subProcess.Start failed")
return "", "", err
}
if stdin != "" {
io.WriteString(stdinPipe, stdin)
stdinPipe.Close()
}
err = subProcess.Wait()
stdout := bufout.String()
stderr := buferr.String()
if err != nil {
log.Debugf("runCommand subProcess.Wait failed")
return "", "", err
}
if stderr != "" {
log.Debugf("Stderr: %s", stderr)
}
return stdout, stderr, err
}
func (d *Driver) MakeCloudApiRequest(now string, encDateString string, sshKeyId string) error {
//response=$(curl $CURL_OPTS $curlOpts -isS \
// -H "Accept:application/json" -H "api-version:*" -H "Date: ${now}" \
// -H "Authorization: Signature keyId=\"/$account/keys/$sshKeyId\",algorithm=\"rsa-sha256\" ${signature}" \
// --url $cloudapiUrl/$account/services)
cloudapiUrl := fmt.Sprintf("%s/%s/services", d.CloudApiURL, d.Account)
log.Debugf("CloudAPI url: ", cloudapiUrl)
req, err := http.NewRequest("GET", cloudapiUrl, nil)
if err != nil {
log.Debugf("http.NewRequest failed for url %s", cloudapiUrl)
return err
}
sig := fmt.Sprintf("Signature keyId=\"/%s/keys/%s\",algorithm=\"rsa-sha256\" %s",
d.Account, sshKeyId, encDateString)
req.Header.Add("Authorization", sig)
req.Header.Add("Accept", "application/json")
req.Header.Add("api-version", "*")
req.Header.Add("Date", now)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: d.SkipTlsVerify},
},
}
resp, err := client.Do(req)
if err != nil {
log.Debugf("client.Do failed for url %s", cloudapiUrl)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Debugf("resp.Body.read failed for url %s", cloudapiUrl)
return err
}
log.Debugf("CloudAPI response: %s", body)
var respMap map[string]interface{}
json.Unmarshal([]byte(body), &respMap)
if resp.StatusCode == http.StatusForbidden { // 403
return fmt.Errorf("ERROR: CloudAPI registration was forbidden: %s", respMap["message"])
}
if resp.StatusCode != http.StatusOK { // 200
return fmt.Errorf("ERROR: CloudAPI registration failed: %s", respMap["message"])
}
dockerUrl, ok := respMap["docker"].(string)
if !ok {
return fmt.Errorf("Could not convert docker response url to string, respMap %s", respMap)
}
// Sanity check the url.
_, err = url.Parse(dockerUrl)
if err != nil {
return fmt.Errorf("Cloudapi returned an invalid url: %s - %s", dockerUrl, err)
}
d.DockerApiURL = dockerUrl
return nil
}
func (d *Driver) RegisterWithSdcCloudApi() error {
log.Debugf("registering with sdc cloud api")
// now=$(date -u "+%a, %d %h %Y %H:%M:%S GMT")
now := time.Now().UTC().Format(time.RFC1123)
// signature=$(echo -n ${now} | openssl dgst -sha256 -sign $sshPrivKeyPath | openssl enc -e -a | tr -d '\n')
cmd := []string{"openssl", "dgst", "-sha256", "-sign", d.PrivateKey}
stdout, _, err := RunCommand(cmd, now)
if err != nil {
log.Debugf("command %s failed %s", cmd, err)
return err
}
cmd = []string{"openssl", "enc", "-e", "-a"}
encDateString, _, err := RunCommand(cmd, stdout)
if err != nil {
log.Debugf("command %s failed %s", cmd, err)
return err
}
//ssh-keygen -l -f "$sshPubKeyPath" | awk '{print $2}' | tr -d '\n';
cmd = []string{"ssh-keygen", "-E", "md5", "-l", "-f", d.PrivateKey + ".pub"}
stdout, _, err = RunCommand(cmd, "")
if err != nil {
log.Debugf("command %s failed %s", cmd, err)
return err
}
sshKeyIdSplit := strings.SplitN(stdout, " ", 3)
if len(sshKeyIdSplit) < 2 {
log.Debugf("invalid ssh key id, length is %d", len(sshKeyIdSplit))
return err
}
sshKeyId := sshKeyIdSplit[1]
sshKeyIdSplit = strings.SplitN(sshKeyId, ":", 2)
if sshKeyIdSplit[0] != "MD5" {
return fmt.Errorf("invalid ssh key output: %s", sshKeyId)
}
sshKeyId = sshKeyIdSplit[1]
// Register this user/key with the SDC cloud API.
err = d.MakeCloudApiRequest(now, encDateString, sshKeyId)
if err != nil {
log.Debugf("MakeCloudApiRequest failed %s", err)
return err
}
return nil
}
/*
* Generate the client certificates (from the users private SSH key).
*
* This also generates the server*.pem certificate files, but these are
* not used by sdc-docker (it just keeps docker-machine happy).
*/
func (d *Driver) GenerateCertificates() error {
err := d.DownloadCa()
if err != nil {
return err
}
var keyFile = d.ResolveStorePath("key.pem")
var csrFile = d.ResolveStorePath("cert.csr")
var certFile = d.ResolveStorePath("cert.pem")
cmd := exec.Command("openssl", "rsa", "-in", d.PrivateKey, "-outform", "pem", "-out", keyFile)
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command("openssl", "req", "-new", "-key", keyFile, "-out", csrFile, "-subj", "/CN="+d.Account)
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command("openssl", "x509", "-req", "-days", "365", "-in", csrFile, "-signkey", keyFile, "-out", certFile)
err = cmd.Run()
if err != nil {
return err
}
log.Debugf("Generating server certificates")
pemBytes, _ := ioutil.ReadFile(keyFile)
block, _ := pem.Decode(pemBytes)
if block == nil {
return errors.New("ssh: no key found")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
ca := &x509.Certificate{
SerialNumber: big.NewInt(123),
Subject: pkix.Name{
CommonName: d.Account,
},
SignatureAlgorithm: x509.SHA512WithRSA,
PublicKeyAlgorithm: x509.RSA,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 5},
BasicConstraintsValid: true,
IsCA: false,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
ca_b, err := x509.CreateCertificate(rand.Reader, ca, ca, &key.PublicKey, key)
if err != nil {
return err
}
log.Debugf("writing server certificates")
var serverKeyFile = d.ResolveStorePath("server-key.pem")
var serverCertFile = d.ResolveStorePath("server.pem")
certOut, _ := os.OpenFile(serverCertFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer certOut.Close()
keyOut, _ := os.OpenFile(serverKeyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer keyOut.Close()
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: ca_b})
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
return nil
}