-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
run.go
188 lines (161 loc) · 5.17 KB
/
run.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// +build !oss
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
* Licensed under the Dgraph Community License (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
*/
package acl
import (
"os"
"strings"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type options struct {
dgraph string
}
var opt options
var tlsConf x.TLSHelperConfig
var CmdAcl x.SubCommand
var UserAdd x.SubCommand
var UserDel x.SubCommand
var LogIn x.SubCommand
var GroupAdd x.SubCommand
var GroupDel x.SubCommand
var UserMod x.SubCommand
var ChMod x.SubCommand
func init() {
CmdAcl.Cmd = &cobra.Command{
Use: "acl",
Short: "Run the Dgraph acl tool",
}
flag := CmdAcl.Cmd.PersistentFlags()
flag.StringP("dgraph", "d", "127.0.0.1:9080", "Dgraph gRPC server address")
// TLS configuration
x.RegisterTLSFlags(flag)
flag.String("tls_server_name", "", "Used to verify the server hostname.")
initSubcommands()
var subcommands = []*x.SubCommand{
&UserAdd, &UserDel, &LogIn, &GroupAdd, &GroupDel, &UserMod, &ChMod,
}
for _, sc := range subcommands {
CmdAcl.Cmd.AddCommand(sc.Cmd)
sc.Conf = viper.New()
sc.Conf.BindPFlags(sc.Cmd.Flags())
sc.Conf.BindPFlags(CmdAcl.Cmd.PersistentFlags())
sc.Conf.SetEnvPrefix(sc.EnvPrefix)
}
}
func initSubcommands() {
// user creation command
UserAdd.Cmd = &cobra.Command{
Use: "useradd",
Short: "Run Dgraph acl tool to add a user",
Run: func(cmd *cobra.Command, args []string) {
runTxn(UserAdd.Conf, userAdd)
},
}
userAddFlags := UserAdd.Cmd.Flags()
userAddFlags.StringP("user", "u", "", "The user id to be created")
userAddFlags.StringP("password", "p", "", "The password for the user")
// user deletion command
UserDel.Cmd = &cobra.Command{
Use: "userdel",
Short: "Run Dgraph acl tool to delete a user",
Run: func(cmd *cobra.Command, args []string) {
runTxn(UserDel.Conf, userDel)
},
}
userDelFlags := UserDel.Cmd.Flags()
userDelFlags.StringP("user", "u", "", "The user id to be deleted")
// login command
LogIn.Cmd = &cobra.Command{
Use: "login",
Short: "Login to dgraph in order to get a jwt token",
Run: func(cmd *cobra.Command, args []string) {
runTxn(LogIn.Conf, userLogin)
},
}
loginFlags := LogIn.Cmd.Flags()
loginFlags.StringP("user", "u", "", "The user id to be created")
loginFlags.StringP("password", "p", "", "The password for the user")
// group creation command
GroupAdd.Cmd = &cobra.Command{
Use: "groupadd",
Short: "Run Dgraph acl tool to add a group",
Run: func(cmd *cobra.Command, args []string) {
runTxn(GroupAdd.Conf, groupAdd)
},
}
groupAddFlags := GroupAdd.Cmd.Flags()
groupAddFlags.StringP("group", "g", "", "The group id to be created")
// group deletion command
GroupDel.Cmd = &cobra.Command{
Use: "groupdel",
Short: "Run Dgraph acl tool to delete a group",
Run: func(cmd *cobra.Command, args []string) {
runTxn(GroupDel.Conf, groupDel)
},
}
groupDelFlags := GroupDel.Cmd.Flags()
groupDelFlags.StringP("group", "g", "", "The group id to be deleted")
// the usermod command used to set a user's groups
UserMod.Cmd = &cobra.Command{
Use: "usermod",
Short: "Run Dgraph acl tool to change a user's groups",
Run: func(cmd *cobra.Command, args []string) {
runTxn(UserMod.Conf, userMod)
},
}
userModFlags := UserMod.Cmd.Flags()
userModFlags.StringP("user", "u", "", "The user id to be changed")
userModFlags.StringP("groups", "g", "", "The groups to be set for the user")
// the chmod command is used to change a group's permissions
ChMod.Cmd = &cobra.Command{
Use: "chmod",
Short: "Run Dgraph acl tool to change a group's permissions",
Run: func(cmd *cobra.Command, args []string) {
runTxn(ChMod.Conf, chMod)
},
}
chModFlags := ChMod.Cmd.Flags()
chModFlags.StringP("group", "g", "", "The group whose permission "+
"is to be changed")
chModFlags.StringP("pred", "p", "", "The predicates whose acls"+
" are to be changed")
chModFlags.IntP("perm", "P", 0, "The acl represented using "+
"an integer, 4 for read-only, 2 for write-only, and 1 for modify-only")
}
func runTxn(conf *viper.Viper, f func(dgraph *dgo.Dgraph) error) {
opt = options{
dgraph: conf.GetString("dgraph"),
}
glog.Infof("Running transaction with dgraph endpoint: %v", opt.dgraph)
if len(opt.dgraph) == 0 {
glog.Fatalf("The --dgraph option must be set in order to connect to dgraph")
}
x.LoadTLSConfig(&tlsConf, CmdAcl.Conf, x.TlsClientCert, x.TlsClientKey)
tlsConf.ServerName = CmdAcl.Conf.GetString("tls_server_name")
ds := strings.Split(opt.dgraph, ",")
var clients []api.DgraphClient
for _, d := range ds {
conn, err := x.SetupConnection(d, !tlsConf.CertRequired, &tlsConf)
x.Checkf(err, "While trying to setup connection to Dgraph alpha.")
defer conn.Close()
dc := api.NewDgraphClient(conn)
clients = append(clients, dc)
}
dgraphClient := dgo.NewDgraphClient(clients...)
if err := f(dgraphClient); err != nil {
glog.Errorf("Error while running transaction: %v", err)
os.Exit(1)
}
}