Browse Source

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.
Anthony Romano 9 years ago
parent
commit
d20f23c795
1 changed files with 16 additions and 1 deletions
  1. 16 1
      etcdctl/ctlv3/command/auth_command.go

+ 16 - 1
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)