From dcf081d758a5a090d6fac3d2d2cf3022d86738fd Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Sat, 6 Dec 2014 20:54:15 +0100 Subject: [PATCH] Check that public key exists when creating machine Fixes #4 Signed-off-by: Ben Firshman --- commands.go | 8 ++++++++ drivers/utils.go | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index ed2069ade9..7c6c06f094 100644 --- a/commands.go +++ b/commands.go @@ -180,6 +180,14 @@ func (cli *DockerCli) CmdCreate(args ...string) error { return nil } + keyExists, err := drivers.PublicKeyExists() + if err != nil { + return err + } + if !keyExists { + log.Fatalf("Identity auth public key does not exist at %s. Please run the docker client without any options to create it.", drivers.PublicKeyPath()) + } + name := cmd.Arg(0) store := NewStore() diff --git a/drivers/utils.go b/drivers/utils.go index 0fa57f10c8..b8bbeca44f 100644 --- a/drivers/utils.go +++ b/drivers/utils.go @@ -6,8 +6,12 @@ import ( "path/filepath" ) +func PublicKeyPath() string { + return filepath.Join(os.Getenv("HOME"), ".docker/public-key.json") +} + func AddPublicKeyToAuthorizedHosts(d Driver, authorizedKeysPath string) error { - f, err := os.Open(filepath.Join(os.Getenv("HOME"), ".docker/public-key.json")) + f, err := os.Open(PublicKeyPath()) if err != nil { return err } @@ -21,3 +25,13 @@ func AddPublicKeyToAuthorizedHosts(d Driver, authorizedKeysPath string) error { cmd.Stdin = f return cmd.Run() } + +func PublicKeyExists() (bool, error) { + _, err := os.Stat(PublicKeyPath()) + if err == nil { + return true, nil + } else if os.IsNotExist(err) { + return false, nil + } + return false, err +}