-
Notifications
You must be signed in to change notification settings - Fork 15
/
etcd_useradd.go
55 lines (45 loc) · 1.16 KB
/
etcd_useradd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"context"
"errors"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
// etcdUserAddCmd represents the "etcd user-add" command
var etcdUserAddCmd = &cobra.Command{
Use: "user-add NAME PREFIX",
Short: "add a user to CKE managed etcd",
Long: `Add a user to etcd managed by CKE (not the one used by CKE).
NAME must not be "root" or "backup".
PREFIX limits the user's privilege to keys having the prefix.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("wrong number of arguments")
}
switch args[0] {
case "", "root", "backup":
return errors.New("bad etcd username: " + args[0])
}
if args[1] == "" {
return errors.New("bad etcd prefix: " + args[1])
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
username := args[0]
prefix := args[1]
well.Go(func(ctx context.Context) error {
etcd, err := inf.NewEtcdClient(ctx, nil)
if err != nil {
return err
}
return cke.AddUserRole(ctx, etcd, username, prefix)
})
well.Stop()
return well.Wait()
},
}
func init() {
etcdCmd.AddCommand(etcdUserAddCmd)
}