From 7e0fb1585da92dc99109417937d8b9917b425ad6 Mon Sep 17 00:00:00 2001 From: Ying Li Date: Thu, 15 Dec 2016 18:36:37 -0800 Subject: [PATCH] Before asking a user for the unlock key when they run `docker swarm unlock`, actually check to see if the node is part of a swarm, and if so, if it is unlocked first. If neither of these are true, abort the command. Signed-off-by: Ying Li Upstream-commit: e4102ce61e8727296c0acc75531088064173de8c Component: cli --- components/cli/command/swarm/unlock.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/components/cli/command/swarm/unlock.go b/components/cli/command/swarm/unlock.go index 048fb56e3d..abb9e89fe7 100644 --- a/components/cli/command/swarm/unlock.go +++ b/components/cli/command/swarm/unlock.go @@ -2,6 +2,7 @@ package swarm import ( "bufio" + "errors" "fmt" "io" "strings" @@ -24,6 +25,22 @@ func newUnlockCommand(dockerCli *command.DockerCli) *cobra.Command { client := dockerCli.Client() ctx := context.Background() + // First see if the node is actually part of a swarm, and if it's is actually locked first. + // If it's in any other state than locked, don't ask for the key. + info, err := client.Info(ctx) + if err != nil { + return err + } + + switch info.Swarm.LocalNodeState { + case swarm.LocalNodeStateInactive: + return errors.New("Error: This node is not part of a swarm") + case swarm.LocalNodeStateLocked: + break + default: + return errors.New("Error: swarm is not locked") + } + key, err := readKey(dockerCli.In(), "Please enter unlock key: ") if err != nil { return err