diff --git a/etcdctl/ctlv3/command/auth_command.go b/etcdctl/ctlv3/command/auth_command.go index cf0e9e1ba..f9a909049 100644 --- a/etcdctl/ctlv3/command/auth_command.go +++ b/etcdctl/ctlv3/command/auth_command.go @@ -17,6 +17,7 @@ package command import ( "fmt" + "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" "github.com/spf13/cobra" ) @@ -48,7 +49,21 @@ func authEnableCommandFunc(cmd *cobra.Command, args []string) { } ctx, cancel := commandCtx(cmd) - _, err := mustClientFromCmd(cmd).Auth.AuthEnable(ctx) + cli := mustClientFromCmd(cmd) + var err error + for err == nil { + if _, err = cli.AuthEnable(ctx); err == nil { + break + } + if err == rpctypes.ErrRootRoleNotExist { + if _, err = cli.RoleAdd(ctx, "root"); err != nil { + break + } + if _, err = cli.UserGrantRole(ctx, "root", "root"); err != nil { + break + } + } + } cancel() if err != nil { ExitWithError(ExitError, err)