From 5cfdcfe7b64fcc93a29976d650da1c4e346d6dd9 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 30 Dec 2015 13:15:19 +0100 Subject: [PATCH] Remove duplication on certificates code Signed-off-by: David Gageot --- commands/commands.go | 35 ------------------------------- commands/create.go | 19 +++++++++++------ libmachine/cert/cert_path_info.go | 10 --------- 3 files changed, 13 insertions(+), 51 deletions(-) delete mode 100644 libmachine/cert/cert_path_info.go diff --git a/commands/commands.go b/commands/commands.go index e35d164ce9..6e8159be0e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -4,13 +4,11 @@ import ( "errors" "fmt" "os" - "path/filepath" "strings" "github.com/codegangsta/cli" "github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/libmachine" - "github.com/docker/machine/libmachine/cert" "github.com/docker/machine/libmachine/crashreport" "github.com/docker/machine/libmachine/drivers/rpc" "github.com/docker/machine/libmachine/host" @@ -391,36 +389,3 @@ func consolidateErrs(errs []error) error { return errors.New(strings.TrimSpace(finalErr)) } - -// Returns the cert paths. codegangsta/cli will not set the cert paths if the -// storage-path is set to something different so we cannot use the paths in the -// global options. le sigh. -func getCertPathInfoFromCommandLine(c CommandLine) cert.PathInfo { - caCertPath := c.GlobalString("tls-ca-cert") - caKeyPath := c.GlobalString("tls-ca-key") - clientCertPath := c.GlobalString("tls-client-cert") - clientKeyPath := c.GlobalString("tls-client-key") - - if caCertPath == "" { - caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem") - } - - if caKeyPath == "" { - caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem") - } - - if clientCertPath == "" { - clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem") - } - - if clientKeyPath == "" { - clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem") - } - - return cert.PathInfo{ - CaCertPath: caCertPath, - CaPrivateKeyPath: caKeyPath, - ClientCertPath: clientCertPath, - ClientKeyPath: clientKeyPath, - } -} diff --git a/commands/create.go b/commands/create.go index ab53bb5e62..870129708a 100644 --- a/commands/create.go +++ b/commands/create.go @@ -162,15 +162,13 @@ func cmdCreateInner(c CommandLine, api libmachine.API) error { return fmt.Errorf("Error getting new host: %s", err) } - certInfo := getCertPathInfoFromCommandLine(c) - h.HostOptions = &host.Options{ AuthOptions: &auth.Options{ CertDir: mcndirs.GetMachineCertDir(), - CaCertPath: certInfo.CaCertPath, - CaPrivateKeyPath: certInfo.CaPrivateKeyPath, - ClientCertPath: certInfo.ClientCertPath, - ClientKeyPath: certInfo.ClientKeyPath, + CaCertPath: tlsPath(c, "tls-ca-cert", "ca.pem"), + CaPrivateKeyPath: tlsPath(c, "tls-ca-key", "ca-key.pem"), + ClientCertPath: tlsPath(c, "tls-client-cert", "cert.pem"), + ClientKeyPath: tlsPath(c, "tls-client-key", "key.pem"), ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"), ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"), StorePath: filepath.Join(mcndirs.GetMachineDir(), name), @@ -433,3 +431,12 @@ func validateSwarmDiscovery(discovery string) error { return fmt.Errorf("Swarm Discovery URL was in the wrong format: %s", discovery) } + +func tlsPath(c CommandLine, flag string, defaultName string) string { + path := c.GlobalString(flag) + if path != "" { + return path + } + + return filepath.Join(mcndirs.GetMachineCertDir(), defaultName) +} diff --git a/libmachine/cert/cert_path_info.go b/libmachine/cert/cert_path_info.go deleted file mode 100644 index 30fd64cb69..0000000000 --- a/libmachine/cert/cert_path_info.go +++ /dev/null @@ -1,10 +0,0 @@ -package cert - -type PathInfo struct { - CaCertPath string - CaPrivateKeyPath string - ClientCertPath string - ClientKeyPath string - ServerCertPath string - ServerKeyPath string -}