Skip to content

Commit 5cfdcfe

Browse files
committed
Remove duplication on certificates code
Signed-off-by: David Gageot <david@gageot.net>
1 parent 885c37e commit 5cfdcfe

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

commands/commands.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"path/filepath"
87
"strings"
98

109
"github.com/codegangsta/cli"
1110
"github.com/docker/machine/commands/mcndirs"
1211
"github.com/docker/machine/libmachine"
13-
"github.com/docker/machine/libmachine/cert"
1412
"github.com/docker/machine/libmachine/crashreport"
1513
"github.com/docker/machine/libmachine/drivers/rpc"
1614
"github.com/docker/machine/libmachine/host"
@@ -391,36 +389,3 @@ func consolidateErrs(errs []error) error {
391389

392390
return errors.New(strings.TrimSpace(finalErr))
393391
}
394-
395-
// Returns the cert paths. codegangsta/cli will not set the cert paths if the
396-
// storage-path is set to something different so we cannot use the paths in the
397-
// global options. le sigh.
398-
func getCertPathInfoFromCommandLine(c CommandLine) cert.PathInfo {
399-
caCertPath := c.GlobalString("tls-ca-cert")
400-
caKeyPath := c.GlobalString("tls-ca-key")
401-
clientCertPath := c.GlobalString("tls-client-cert")
402-
clientKeyPath := c.GlobalString("tls-client-key")
403-
404-
if caCertPath == "" {
405-
caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem")
406-
}
407-
408-
if caKeyPath == "" {
409-
caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem")
410-
}
411-
412-
if clientCertPath == "" {
413-
clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem")
414-
}
415-
416-
if clientKeyPath == "" {
417-
clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem")
418-
}
419-
420-
return cert.PathInfo{
421-
CaCertPath: caCertPath,
422-
CaPrivateKeyPath: caKeyPath,
423-
ClientCertPath: clientCertPath,
424-
ClientKeyPath: clientKeyPath,
425-
}
426-
}

commands/create.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,13 @@ func cmdCreateInner(c CommandLine, api libmachine.API) error {
162162
return fmt.Errorf("Error getting new host: %s", err)
163163
}
164164

165-
certInfo := getCertPathInfoFromCommandLine(c)
166-
167165
h.HostOptions = &host.Options{
168166
AuthOptions: &auth.Options{
169167
CertDir: mcndirs.GetMachineCertDir(),
170-
CaCertPath: certInfo.CaCertPath,
171-
CaPrivateKeyPath: certInfo.CaPrivateKeyPath,
172-
ClientCertPath: certInfo.ClientCertPath,
173-
ClientKeyPath: certInfo.ClientKeyPath,
168+
CaCertPath: tlsPath(c, "tls-ca-cert", "ca.pem"),
169+
CaPrivateKeyPath: tlsPath(c, "tls-ca-key", "ca-key.pem"),
170+
ClientCertPath: tlsPath(c, "tls-client-cert", "cert.pem"),
171+
ClientKeyPath: tlsPath(c, "tls-client-key", "key.pem"),
174172
ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"),
175173
ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"),
176174
StorePath: filepath.Join(mcndirs.GetMachineDir(), name),
@@ -433,3 +431,12 @@ func validateSwarmDiscovery(discovery string) error {
433431

434432
return fmt.Errorf("Swarm Discovery URL was in the wrong format: %s", discovery)
435433
}
434+
435+
func tlsPath(c CommandLine, flag string, defaultName string) string {
436+
path := c.GlobalString(flag)
437+
if path != "" {
438+
return path
439+
}
440+
441+
return filepath.Join(mcndirs.GetMachineCertDir(), defaultName)
442+
}

libmachine/cert/cert_path_info.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)