etcdctl: create root role on auth enable if it does not yet exist
Kind of tedious to add the root role when enabling auth; can just add
it automatically.
@@ -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)
