mirror: make etcdctl make-mirror subcommand accept --dest-user and --dest-password (#11384)

This commit is contained in:
schlitzered
2019-12-04 23:06:49 +01:00
committed by Xiang Li
parent e5356cca80
commit d4d7ad3908

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/bgentry/speakeasy"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "time"
@ -37,6 +38,8 @@ var (
mmcacert string mmcacert string
mmprefix string mmprefix string
mmdestprefix string mmdestprefix string
mmuser string
mmpassword string
mmnodestprefix bool 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") 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. // 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().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 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) { func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 { if len(args) != 1 {
ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination argument")) ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination argument"))
@ -75,13 +108,15 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
insecureTransport: mminsecureTr, insecureTransport: mminsecureTr,
} }
auth := authDestCfg()
cc := &clientConfig{ cc := &clientConfig{
endpoints: []string{args[0]}, endpoints: []string{args[0]},
dialTimeout: dialTimeout, dialTimeout: dialTimeout,
keepAliveTime: keepAliveTime, keepAliveTime: keepAliveTime,
keepAliveTimeout: keepAliveTimeout, keepAliveTimeout: keepAliveTimeout,
scfg: sec, scfg: sec,
acfg: nil, acfg: auth,
} }
dc := cc.mustClient() dc := cc.mustClient()
c := mustClientFromCmd(cmd) c := mustClientFromCmd(cmd)