-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrun_ee.go
249 lines (214 loc) · 6.81 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
// +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"
"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
tlsConf x.TLSHelperConfig
CmdAcl x.SubCommand
)
const gPassword = "gpassword"
const defaultGroupList = "dgraph-unused-group"
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 Alpha gRPC server address")
flag.StringP(gPassword, "x", "", "Groot password to authorize this operation")
// 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()
if err := sc.Conf.BindPFlags(sc.Cmd.Flags()); err != nil {
glog.Fatalf("Unable to bind flags for command %v: %v", sc, err)
}
if err := sc.Conf.BindPFlags(CmdAcl.Cmd.PersistentFlags()); err != nil {
glog.Fatalf("Unable to bind persistent flags from acl for command %v: %v", sc, err)
}
sc.Conf.SetEnvPrefix(sc.EnvPrefix)
}
}
func initSubcommands() []*x.SubCommand {
var cmdAdd x.SubCommand
cmdAdd.Cmd = &cobra.Command{
Use: "add",
Short: "Run Dgraph acl tool to add a user or group",
Run: func(cmd *cobra.Command, args []string) {
if err := add(cmdAdd.Conf); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
},
}
addFlags := cmdAdd.Cmd.Flags()
addFlags.StringP("user", "u", "", "The user id to be created")
addFlags.StringP("password", "p", "", "The password for the user")
addFlags.StringP("group", "g", "", "The group id to be created")
var cmdDel x.SubCommand
cmdDel.Cmd = &cobra.Command{
Use: "del",
Short: "Run Dgraph acl tool to delete a user or group",
Run: func(cmd *cobra.Command, args []string) {
if err := del(cmdDel.Conf); err != nil {
fmt.Printf("Unable to delete the user: %v\n", err)
os.Exit(1)
}
},
}
delFlags := cmdDel.Cmd.Flags()
delFlags.StringP("user", "u", "", "The user id to be deleted")
delFlags.StringP("group", "g", "", "The group id to be deleted")
var cmdMod x.SubCommand
cmdMod.Cmd = &cobra.Command{
Use: "mod",
Short: "Run Dgraph acl tool to modify a user's password, a user's group list, or a" +
"group's predicate permissions",
Run: func(cmd *cobra.Command, args []string) {
if err := mod(cmdMod.Conf); err != nil {
fmt.Printf("Unable to modify: %v\n", err)
os.Exit(1)
}
},
}
modFlags := cmdMod.Cmd.Flags()
modFlags.StringP("user", "u", "", "The user id to be changed")
modFlags.BoolP("new_password", "", false, "Whether to reset password for the user")
modFlags.StringP("group_list", "l", defaultGroupList,
"The list of groups to be set for the user")
modFlags.StringP("group", "g", "", "The group whose permission is to be changed")
modFlags.StringP("pred", "p", "", "The predicates whose acls are to be changed")
modFlags.StringP("pred_regex", "P", "", "The regular expression specifying predicates"+
" whose acls are to be changed")
modFlags.IntP("perm", "m", 0, "The acl represented using "+
"an integer: 4 for read, 2 for write, and 1 for modify. Use a negative value to remove a "+
"predicate from the group")
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 {
fmt.Printf("Unable to show info: %v\n", 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{&cmdAdd, &cmdDel, &cmdMod, &cmdInfo}
}
type CloseFunc func()
func getDgraphClient(conf *viper.Viper) (*dgo.Dgraph, CloseFunc) {
opt = options{
dgraph: conf.GetString("dgraph"),
}
fmt.Printf("\nRunning transaction with dgraph endpoint: %v\n", 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")
conn, err := x.SetupConnection(opt.dgraph, &tlsConf, false)
x.Checkf(err, "While trying to setup connection to Dgraph alpha.")
dc := api.NewDgraphClient(conn)
return dgo.NewDgraphClient(dc), func() {
if err := conn.Close(); err != nil {
fmt.Printf("Error while closing connection: %v\n", err)
}
}
}
func queryAndPrintUser(ctx context.Context, txn *dgo.Txn, userId string) error {
user, err := queryUser(ctx, txn, userId)
if err != nil {
return err
}
if user == nil {
return fmt.Errorf("The user %q does not exist.\n", userId)
}
fmt.Printf("User : %s\n", userId)
fmt.Printf("UID : %s\n", user.Uid)
for _, group := range user.Groups {
fmt.Printf("Group : %-5s\n", group.GroupID)
}
return nil
}
func queryAndPrintGroup(ctx context.Context, txn *dgo.Txn, groupId string) error {
group, err := queryGroup(ctx, txn, groupId, "dgraph.xid", "~dgraph.user.group{dgraph.xid}",
"dgraph.group.acl")
if err != nil {
return err
}
if group == nil {
return fmt.Errorf("The group %q does not exist.\n", groupId)
}
fmt.Printf("Group: %s\n", groupId)
fmt.Printf("UID : %s\n", group.Uid)
fmt.Printf("ID : %s\n", group.GroupID)
var userNames []string
for _, user := range group.Users {
userNames = append(userNames, user.UserID)
}
fmt.Printf("Users: %s\n", strings.Join(userNames, " "))
var acls []Acl
if len(group.Acls) != 0 {
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 {
fmt.Printf("ACL : %v\n", acl)
}
}
return nil
}
func info(conf *viper.Viper) error {
userId, groupId, err := getUserAndGroup(conf)
if err != nil {
return err
}
dc, cancel, err := getClientWithAdminCtx(conf)
defer cancel()
if err != nil {
return fmt.Errorf("unable to get admin context: %v\n", err)
}
ctx := context.Background()
txn := dc.NewTxn()
defer func() {
if err := txn.Discard(ctx); err != nil {
fmt.Printf("Unable to discard transaction: %v\n", err)
}
}()
if len(userId) != 0 {
return queryAndPrintUser(ctx, txn, userId)
}
return queryAndPrintGroup(ctx, txn, groupId)
}