-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathaccess_ee.go
568 lines (493 loc) · 14.9 KB
/
access_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// +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 edgraph
import (
"context"
"encoding/json"
"fmt"
"strconv"
"sync"
"time"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/ee/acl"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgrijalva/jwt-go"
"github.com/golang/glog"
otrace "go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
func (s *Server) Login(ctx context.Context,
request *api.LoginRequest) (*api.Response, error) {
ctx, span := otrace.StartSpan(ctx, "server.Login")
defer span.End()
// record the client ip for this login request
var addr string
if ip, ok := peer.FromContext(ctx); ok {
addr = ip.Addr.String()
glog.Infof("Login request from: %s", addr)
span.Annotate([]otrace.Attribute{
otrace.StringAttribute("client_ip", addr),
}, "client ip for login")
}
user, err := s.authenticateLogin(ctx, request)
if err != nil {
errMsg := fmt.Sprintf("authentication from address %s failed: %v", addr, err)
glog.Errorf(errMsg)
return nil, fmt.Errorf(errMsg)
}
resp := &api.Response{}
accessJwt, err := getAccessJwt(request.Userid, user.Groups)
if err != nil {
errMsg := fmt.Sprintf("unable to get access jwt (userid=%s,addr=%s):%v",
request.Userid, addr, err)
glog.Errorf(errMsg)
return nil, fmt.Errorf(errMsg)
}
refreshJwt, err := getRefreshJwt(request.Userid)
if err != nil {
errMsg := fmt.Sprintf("unable to get refresh jwt (userid=%s,addr=%s):%v",
request.Userid, addr, err)
glog.Errorf(errMsg)
return nil, fmt.Errorf(errMsg)
}
loginJwt := api.Jwt{
AccessJwt: accessJwt,
RefreshJwt: refreshJwt,
}
jwtBytes, err := loginJwt.Marshal()
if err != nil {
errMsg := fmt.Sprintf("unable to marshal jwt (userid=%s,addr=%s):%v",
request.Userid, addr, err)
glog.Errorf(errMsg)
return nil, fmt.Errorf(errMsg)
}
resp.Json = jwtBytes
return resp, nil
}
// Authenticate the login request using either the refresh token if present, or the
// <userId, password> pair. If authentication passes, query the user's uid and associated groups
// from DB and returns the user object
func (s *Server) authenticateLogin(ctx context.Context, request *api.LoginRequest) (*acl.User,
error) {
if err := validateLoginRequest(request); err != nil {
return nil, fmt.Errorf("invalid login request: %v", err)
}
var user *acl.User
if len(request.RefreshToken) > 0 {
userData, err := validateToken(request.RefreshToken)
if err != nil {
return nil, fmt.Errorf("unable to authenticate the refresh token %v: %v",
request.RefreshToken, err)
}
userId := userData[0]
user, err = authorizeUser(ctx, userId, "")
if err != nil {
return nil, fmt.Errorf("error while querying user with id %v: %v", userId, err)
}
if user == nil {
return nil, fmt.Errorf("unable to authenticate through refresh token: "+
"user not found for id %v", userId)
}
return user, nil
} else {
var err error
user, err = authorizeUser(ctx, request.Userid, request.Password)
if err != nil {
return nil, fmt.Errorf("error while querying user with id: %v",
request.Userid)
}
if user == nil {
return nil, fmt.Errorf("unable to authenticate through password: "+
"user not found for id %v", request.Userid)
}
if !user.PasswordMatch {
return nil, fmt.Errorf("password mismatch for user: %v", request.Userid)
}
return user, nil
}
}
// verify signature and expiration of the jwt and if validation passes,
// return the extracted userId, and groupIds encoded in the jwt
func validateToken(jwtStr string) (userData []string, err error) {
token, err := jwt.Parse(jwtStr, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return Config.HmacSecret, nil
})
if err != nil {
return nil, fmt.Errorf("unable to parse jwt token:%v", err)
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
return nil, fmt.Errorf("claims in jwt token is not map claims")
}
// by default, the MapClaims.Valid will return true if the exp field is not set
// here we enforce the checking to make sure that the refresh token has not expired
now := time.Now().Unix()
if !claims.VerifyExpiresAt(now, true) {
return nil, fmt.Errorf("jwt token has expired at %v", now)
}
userId, ok := claims["userid"].(string)
if !ok {
return nil, fmt.Errorf("userid in claims is not a string:%v", userId)
}
groups, ok := claims["groups"].([]interface{})
var groupIds []string
if ok {
groupIds = make([]string, 0, len(groups))
for _, group := range groups {
groupId, ok := group.(string)
if !ok {
glog.Errorf("unable to convert group to string:%v", group)
}
groupIds = append(groupIds, groupId)
}
}
return append([]string{userId}, groupIds...), nil
}
// validate that the login request has either the refresh token or the <userid, password> pair
func validateLoginRequest(request *api.LoginRequest) error {
if request == nil {
return fmt.Errorf("the request should not be nil")
}
// we will use the refresh token for authentication if it's set
if len(request.RefreshToken) > 0 {
return nil
}
// otherwise make sure both userid and password are set
if len(request.Userid) == 0 {
return fmt.Errorf("the userid should not be empty")
}
if len(request.Password) == 0 {
return fmt.Errorf("the password should not be empty")
}
return nil
}
// construct an access jwt with the given userid, groupIds, and expiration ttl specified by
// Config.AccessJwtTtl
func getAccessJwt(userId string, groups []acl.Group) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userid": userId,
"groups": acl.GetGroupIDs(groups),
// set the jwt exp according to the ttl
"exp": json.Number(
strconv.FormatInt(time.Now().Add(Config.AccessJwtTtl).Unix(), 10)),
})
jwtString, err := token.SignedString(Config.HmacSecret)
if err != nil {
return "", fmt.Errorf("unable to encode jwt to string: %v", err)
}
return jwtString, nil
}
// construct a refresh jwt with the given userid, and expiration ttl specified by
// Config.RefreshJwtTtl
func getRefreshJwt(userId string) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userid": userId,
// set the jwt exp according to the ttl
"exp": json.Number(
strconv.FormatInt(time.Now().Add(Config.RefreshJwtTtl).Unix(), 10)),
})
jwtString, err := token.SignedString(Config.HmacSecret)
if err != nil {
return "", fmt.Errorf("unable to encode jwt to string: %v", err)
}
return jwtString, nil
}
const queryUser = `
query search($userid: string, $password: string){
user(func: eq(dgraph.xid, $userid)) {
uid
password_match: checkpwd(dgraph.password, $password)
dgraph.user.group {
uid
dgraph.xid
}
}
}`
// query the user with the given userid, and returns associated uid, acl groups,
// and whether the password stored in DB matches the supplied password
func authorizeUser(ctx context.Context, userid string, password string) (user *acl.User,
err error) {
queryVars := map[string]string{
"$userid": userid,
"$password": password,
}
queryRequest := api.Request{
Query: queryUser,
Vars: queryVars,
}
queryResp, err := (&Server{}).doQuery(ctx, &queryRequest)
if err != nil {
glog.Errorf("Error while query user with id %s: %v", userid, err)
return nil, err
}
user, err = acl.UnmarshalUser(queryResp, "user")
if err != nil {
return nil, err
}
return user, nil
}
func RefreshAcls(closeCh <-chan struct{}) {
if len(Config.HmacSecret) == 0 {
// the acl feature is not turned on
return
}
ticker := time.NewTicker(Config.AclRefreshInterval)
defer ticker.Stop()
// retrieve the full data set of ACLs from the corresponding alpha server, and update the aclCache
RetrieveAcls := func() error {
glog.Infof("Retrieving ACLs")
queryRequest := api.Request{
Query: queryAcls,
}
ctx := context.Background()
var err error
queryResp, err := (&Server{}).doQuery(ctx, &queryRequest)
if err != nil {
return fmt.Errorf("unable to retrieve acls: %v", err)
}
groups, err := acl.UnmarshalGroups(queryResp.GetJson(), "allAcls")
if err != nil {
return err
}
storedEntries := 0
for _, group := range groups {
// convert the serialized acl into a map for easy lookups
group.MappedAcls, err = acl.UnmarshalAcl([]byte(group.Acls))
if err != nil {
glog.Errorf("Error while unmarshalling ACLs for group %v:%v", group, err)
continue
}
storedEntries++
aclCache.Store(group.GroupID, &group)
}
glog.Infof("updated the ACL cache with %d entries", storedEntries)
return nil
}
for {
select {
case <-closeCh:
return
case <-ticker.C:
if err := RetrieveAcls(); err != nil {
glog.Errorf("Error while retrieving acls:%v", err)
}
}
}
}
const queryAcls = `
{
allAcls(func: has(dgraph.group.acl)) {
dgraph.xid
dgraph.group.acl
}
}
`
// the acl cache mapping group names to the corresponding group acls
var aclCache sync.Map
// clear the aclCache and upsert the admin account
func ResetAcl() {
if len(Config.HmacSecret) == 0 {
// the acl feature is not turned on
return
}
aclCache = sync.Map{}
// upsert the admin account
adminUser, err := authorizeUser(context.Background(), "admin", "")
if err != nil {
glog.Errorf("error while querying the admin account")
return
}
if adminUser != nil {
// the admin user already exists, no need to create
return
}
// insert the admin user
createUserNQuads := []*api.NQuad{
{
Subject: "_:newuser",
Predicate: "dgraph.xid",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "admin"}},
},
{
Subject: "_:newuser",
Predicate: "dgraph.password",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "password"}},
}}
mu := &api.Mutation{
CommitNow: true,
Set: createUserNQuads,
}
if _, err := (&Server{}).doMutate(context.Background(), mu); err != nil {
glog.Errorf("unable to create admin: %v", err)
return
}
glog.Info("Created the admin account with the default password")
}
// extract the userId, groupIds from the accessJwt in the context
func extractUserAndGroups(ctx context.Context) ([]string, error) {
// extract the jwt and unmarshal the jwt to get the list of groups
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("no metadata available")
}
accessJwt := md.Get("accessJwt")
if len(accessJwt) == 0 {
//glog.Infof("no accessJwt available, type is %v", reflect.TypeOf(ctx.Value("accessJwt")))
return nil, fmt.Errorf("no accessJwt available")
}
return validateToken(accessJwt[0])
}
// parse the Schema in the operation and authorize the operation using the aclCache
func authorizeAlter(ctx context.Context, op *api.Operation) error {
if len(Config.HmacSecret) == 0 {
// the user has not turned on the acl feature
return nil
}
updates, err := schema.Parse(op.Schema)
if err != nil {
return err
}
userData, err := extractUserAndGroups(ctx)
if err != nil {
return status.Error(codes.Unauthenticated, err.Error())
}
userId := userData[0]
if userId == "admin" {
// admin is allowed to do anything
return nil
}
// if we get here, we know the user is not admin
if op.DropAll {
return fmt.Errorf("only the admin is allowed to drop all predicates")
}
groupIds := userData[1:]
if len(op.DropAttr) > 0 {
// check that we have the modify permission on the predicate
if !authorizePredicate(groupIds, op.DropAttr, acl.Modify) {
return fmt.Errorf("unauthorized to modify the predicate %v", op.DropAttr)
}
return nil
}
for _, update := range updates {
if !authorizePredicate(groupIds, update.Predicate, acl.Modify) {
return fmt.Errorf("unauthorized to modify the predicate %v", update.Predicate)
}
}
return nil
}
func populatePredsFromMutation(nquads []*api.NQuad, preds map[string]struct{}) {
for _, nquad := range nquads {
preds[nquad.Predicate] = struct{}{}
}
}
// authorize the mutation using the aclCache
func authorizeMutation(ctx context.Context, mu *api.Mutation) error {
if len(Config.HmacSecret) == 0 {
// the user has not turned on the acl feature
return nil
}
userData, err := extractUserAndGroups(ctx)
if err != nil {
return status.Error(codes.Unauthenticated, err.Error())
}
userId := userData[0]
if userId == "admin" {
// the admin account has access to everything
return nil
}
gmu, err := parseMutationObject(mu)
if err != nil {
return err
}
preds := make(map[string]struct{})
populatePredsFromMutation(gmu.Set, preds)
groupIds := userData[1:]
for pred := range preds {
if !authorizePredicate(groupIds, pred, acl.Write) {
return fmt.Errorf("unauthorized to access the predicate %v", pred)
}
}
return nil
}
func populatePredsFromQuery(gqls []*gql.GraphQuery, preds map[string]struct{}) {
for _, gq := range gqls {
if gq.Func != nil {
preds[gq.Func.Attr] = struct{}{}
}
if len(gq.Attr) > 0 {
preds[gq.Attr] = struct{}{}
}
populatePredsFromQuery(gq.Children, preds)
}
}
// authorize the query using the aclCache
func authorizeQuery(ctx context.Context, req *api.Request) error {
if len(Config.HmacSecret) == 0 {
// the user has not turned on the acl feature
return nil
}
userData, err := extractUserAndGroups(ctx)
if err != nil {
return status.Error(codes.Unauthenticated, err.Error())
}
userId := userData[0]
if userId == "admin" {
// the admin account has access to everything
return nil
}
parsedReq, err := gql.Parse(gql.Request{
Str: req.Query,
Variables: req.Vars,
})
if err != nil {
return err
}
preds := make(map[string]struct{})
populatePredsFromQuery(parsedReq.Query, preds)
groupIds := userData[1:]
for pred := range preds {
if !authorizePredicate(groupIds, pred, acl.Read) {
return status.Error(codes.PermissionDenied,
fmt.Sprintf("unauthorized to access the predicate %v", pred))
}
}
return nil
}
func authorizePredicate(groups []string, predicate string, operation int32) bool {
for _, group := range groups {
if hasAccess(group, predicate, operation) {
return true
}
}
return false
}
// hasAccess checks the aclCache and returns whether the specified group is authorized to perform
// the operation on the given predicate
func hasAccess(groupId string, predicate string, operation int32) bool {
entry, found := aclCache.Load(groupId)
if !found {
glog.Infof("acl not found")
return false
}
aclGroup := entry.(*acl.Group)
perm, found := aclGroup.MappedAcls[predicate]
allowed := found && (perm&operation) != 0
glog.Infof("authorizing group %v on predicate %v for op %d, allowed %v", groupId,
predicate, operation, allowed)
return allowed
}