-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
run_ee.go
287 lines (252 loc) · 8.13 KB
/
run_ee.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// +build !oss
/*
* Copyright 2018 Dgraph Labs, Inc. and Contributors
*
* 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 (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"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
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.")
subcommands := initSubcommands()
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() []*x.SubCommand {
// user creation command
var cmdUserAdd x.SubCommand
cmdUserAdd.Cmd = &cobra.Command{
Use: "useradd",
Short: "Run Dgraph acl tool to add a user",
Run: func(cmd *cobra.Command, args []string) {
if err := userAdd(cmdUserAdd.Conf); err != nil {
glog.Errorf("Unable to add user:%v", err)
os.Exit(1)
}
},
}
userAddFlags := cmdUserAdd.Cmd.Flags()
userAddFlags.StringP("user", "u", "", "The user id to be created")
userAddFlags.StringP("password", "p", "", "The password for the user")
// user deletion command
var cmdUserDel x.SubCommand
cmdUserDel.Cmd = &cobra.Command{
Use: "userdel",
Short: "Run Dgraph acl tool to delete a user",
Run: func(cmd *cobra.Command, args []string) {
if err := userDel(cmdUserDel.Conf); err != nil {
glog.Errorf("Unable to delete the user:%v", err)
os.Exit(1)
}
},
}
userDelFlags := cmdUserDel.Cmd.Flags()
userDelFlags.StringP("user", "u", "", "The user id to be deleted")
// login command
var cmdLogIn x.SubCommand
cmdLogIn.Cmd = &cobra.Command{
Use: "login",
Short: "Login to dgraph in order to get a jwt token",
Run: func(cmd *cobra.Command, args []string) {
if err := userLogin(cmdLogIn.Conf); err != nil {
glog.Errorf("Unable to login:%v", err)
os.Exit(1)
}
},
}
loginFlags := cmdLogIn.Cmd.Flags()
loginFlags.StringP("user", "u", "", "The user id to be created")
loginFlags.StringP("password", "p", "", "The password for the user")
// group creation command
var cmdGroupAdd x.SubCommand
cmdGroupAdd.Cmd = &cobra.Command{
Use: "groupadd",
Short: "Run Dgraph acl tool to add a group",
Run: func(cmd *cobra.Command, args []string) {
if err := groupAdd(cmdGroupAdd.Conf); err != nil {
glog.Errorf("Unable to add group:%v", err)
os.Exit(1)
}
},
}
groupAddFlags := cmdGroupAdd.Cmd.Flags()
groupAddFlags.StringP("group", "g", "", "The group id to be created")
// group deletion command
var cmdGroupDel x.SubCommand
cmdGroupDel.Cmd = &cobra.Command{
Use: "groupdel",
Short: "Run Dgraph acl tool to delete a group",
Run: func(cmd *cobra.Command, args []string) {
if err := groupDel(cmdGroupDel.Conf); err != nil {
glog.Errorf("Unable to delete group:%v", err)
os.Exit(1)
}
},
}
groupDelFlags := cmdGroupDel.Cmd.Flags()
groupDelFlags.StringP("group", "g", "", "The group id to be deleted")
// the usermod command used to set a user's groups
var cmdUserMod x.SubCommand
cmdUserMod.Cmd = &cobra.Command{
Use: "usermod",
Short: "Run Dgraph acl tool to change a user's groups",
Run: func(cmd *cobra.Command, args []string) {
if err := userMod(cmdUserMod.Conf); err != nil {
glog.Errorf("Unable to modify user:%v", err)
os.Exit(1)
}
},
}
userModFlags := cmdUserMod.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
var cmdChMod x.SubCommand
cmdChMod.Cmd = &cobra.Command{
Use: "chmod",
Short: "Run Dgraph acl tool to change a group's permissions",
Run: func(cmd *cobra.Command, args []string) {
if err := chMod(cmdChMod.Conf); err != nil {
glog.Errorf("Unable to change permisson for group:%v", err)
os.Exit(1)
}
},
}
chModFlags := cmdChMod.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")
var cmdInfo x.SubCommand
cmdInfo.Cmd = &cobra.Command{
Use: "info",
Short: "Show info about a user or group",
Run: func(cmd *cobra.Command, args []string) {
if err := info(cmdInfo.Conf); err != nil {
glog.Errorf("Unable to show info:%v", err)
os.Exit(1)
}
},
}
infoFlags := cmdInfo.Cmd.Flags()
infoFlags.StringP("user", "u", "", "The user to be shown")
infoFlags.StringP("group", "g", "", "The group to be shown")
return []*x.SubCommand{
&cmdUserAdd, &cmdUserDel, &cmdLogIn, &cmdGroupAdd, &cmdGroupDel, &cmdUserMod,
&cmdChMod, &cmdInfo,
}
}
func getDgraphClient(conf *viper.Viper) *dgo.Dgraph {
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)
x.Checkf(err, "While trying to setup connection to Dgraph alpha.")
dc := api.NewDgraphClient(conn)
clients = append(clients, dc)
}
return dgo.NewDgraphClient(clients...)
}
func info(conf *viper.Viper) error {
userId := conf.GetString("user")
groupId := conf.GetString("group")
if (len(userId) == 0 && len(groupId) == 0) ||
(len(userId) != 0 && len(groupId) != 0) {
return fmt.Errorf("either the user or group should be specified, not both")
}
dc := getDgraphClient(conf)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
txn := dc.NewTxn()
defer txn.Discard(ctx)
if len(userId) != 0 {
user, err := queryUser(ctx, txn, userId)
if err != nil {
return err
}
var userSB strings.Builder
userSB.WriteString(fmt.Sprintf("user %v:\n", userId))
userSB.WriteString(fmt.Sprintf("uid:%v\nid:%v\n", user.Uid, user.UserID))
var groupNames []string
for _, group := range user.Groups {
groupNames = append(groupNames, group.GroupID)
}
userSB.WriteString(fmt.Sprintf("groups:%v\n", strings.Join(groupNames, " ")))
glog.Infof(userSB.String())
}
if len(groupId) != 0 {
group, err := queryGroup(ctx, txn, groupId, "dgraph.xid", "~dgraph.user.group{dgraph.xid}",
"dgraph.group.acl")
if err != nil {
return err
}
// build the info string for group
var groupSB strings.Builder
groupSB.WriteString(fmt.Sprintf("group %v:\n", groupId))
groupSB.WriteString(fmt.Sprintf("uid:%v\nid:%v\n", group.Uid, group.GroupID))
var userNames []string
for _, user := range group.Users {
userNames = append(userNames, user.UserID)
}
groupSB.WriteString(fmt.Sprintf("users:%v\n", strings.Join(userNames, " ")))
var aclStrs []string
var acls []Acl
if err := json.Unmarshal([]byte(group.Acls), &acls); err != nil {
return fmt.Errorf("unable to unmarshal the acls associated with the group %v:%v",
groupId, err)
}
for _, acl := range acls {
aclStrs = append(aclStrs, fmt.Sprintf("(predicate:%v,perm:%v)", acl.Predicate, acl.Perm))
}
groupSB.WriteString(fmt.Sprintf("acls:%v\n", strings.Join(aclStrs, " ")))
glog.Infof(groupSB.String())
}
return nil
}