From d4d7ad39080f0e84a69142149c442a4bf8ed48a0 Mon Sep 17 00:00:00 2001 From: schlitzered Date: Wed, 4 Dec 2019 23:06:49 +0100 Subject: [PATCH] mirror: make etcdctl make-mirror subcommand accept --dest-user and --dest-password (#11384) --- etcdctl/ctlv3/command/make_mirror_command.go | 37 +++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/etcdctl/ctlv3/command/make_mirror_command.go b/etcdctl/ctlv3/command/make_mirror_command.go index 243aacea4..e5d091463 100644 --- a/etcdctl/ctlv3/command/make_mirror_command.go +++ b/etcdctl/ctlv3/command/make_mirror_command.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "github.com/bgentry/speakeasy" "strings" "sync/atomic" "time" @@ -37,6 +38,8 @@ var ( mmcacert string mmprefix string mmdestprefix string + mmuser string + mmpassword string mmnodestprefix bool ) @@ -56,10 +59,40 @@ func NewMakeMirrorCommand() *cobra.Command { c.Flags().StringVar(&mmcacert, "dest-cacert", "", "Verify certificates of TLS enabled secure servers using this CA bundle") // TODO: secure by default when etcd enables secure gRPC by default. c.Flags().BoolVar(&mminsecureTr, "dest-insecure-transport", true, "Disable transport security for client connections") + c.Flags().StringVar(&mmuser, "dest-user", "", "Destination username[:password] for authentication (prompt if password is not supplied)") + c.Flags().StringVar(&mmpassword, "dest-password", "", "Destination password for authentication (if this option is used, --user option shouldn't include password)") return c } +func authDestCfg() *authCfg { + if mmuser == "" { + return nil + } + + var cfg authCfg + + if mmpassword == "" { + splitted := strings.SplitN(mmuser, ":", 2) + if len(splitted) < 2 { + var err error + cfg.username = mmuser + cfg.password, err = speakeasy.Ask("Destination Password: ") + if err != nil { + ExitWithError(ExitError, err) + } + } else { + cfg.username = splitted[0] + cfg.password = splitted[1] + } + } else { + cfg.username = mmuser + cfg.password = mmpassword + } + + return &cfg +} + func makeMirrorCommandFunc(cmd *cobra.Command, args []string) { if len(args) != 1 { ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination argument")) @@ -75,13 +108,15 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) { insecureTransport: mminsecureTr, } + auth := authDestCfg() + cc := &clientConfig{ endpoints: []string{args[0]}, dialTimeout: dialTimeout, keepAliveTime: keepAliveTime, keepAliveTimeout: keepAliveTimeout, scfg: sec, - acfg: nil, + acfg: auth, } dc := cc.mustClient() c := mustClientFromCmd(cmd)