Skip to content

Commit

Permalink
Refactor: avoid double parsing of mutation string in ACL
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 committed May 31, 2019
1 parent 93cdbdb commit d153e45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
11 changes: 3 additions & 8 deletions edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func ResetAcl() {
Set: createUserNQuads,
}

if _, err := (&Server{}).doMutate(context.Background(), mu); err != nil {
if _, err := (&Server{}).doMutate(context.Background(), mu, false); err != nil {
return err
}
glog.Infof("Successfully upserted the groot account")
Expand Down Expand Up @@ -541,17 +541,12 @@ func isAclPredMutation(nquads []*api.NQuad) bool {
}

// authorizeMutation authorizes the mutation using the aclCache
func authorizeMutation(ctx context.Context, mu *api.Mutation) error {
func authorizeMutation(ctx context.Context, gmu *gql.Mutation) error {
if len(Config.HmacSecret) == 0 {
// the user has not turned on the acl feature
return nil
}

// parse predicates from the mutation object
gmu, err := parseMutationObject(mu)
if err != nil {
return err
}
preds := parsePredsFromMutation(gmu.Set)

var userId string
Expand Down Expand Up @@ -596,7 +591,7 @@ func authorizeMutation(ctx context.Context, mu *api.Mutation) error {
return nil
}

err = doAuthorizeMutation()
err := doAuthorizeMutation()
span := otrace.FromContext(ctx)
if span != nil {
span.Annotatef(nil, (&AccessEntry{
Expand Down
35 changes: 19 additions & 16 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,28 @@ func annotateStartTs(span *otrace.Span, ts uint64) {
span.Annotate([]otrace.Attribute{otrace.Int64Attribute("startTs", int64(ts))}, "")
}

func (s *Server) Mutate(ctx context.Context, mu *api.Mutation) (resp *api.Assigned, err error) {
if err := authorizeMutation(ctx, mu); err != nil {
return nil, err
func (s *Server) Mutate(ctx context.Context, mu *api.Mutation) (*api.Assigned, error) {
return s.doMutate(ctx, mu, true)
}

func (s *Server) doMutate(ctx context.Context, mu *api.Mutation, authorize bool) (
resp *api.Assigned, rerr error) {

var l query.Latency
l.Start = time.Now()
gmu, err := parseMutationObject(mu)
if err != nil {
return resp, err
}
parseEnd := time.Now()
l.Parsing = parseEnd.Sub(l.Start)

return s.doMutate(ctx, mu)
}
if authorize {
if err := authorizeMutation(ctx, gmu); err != nil {
return nil, err
}
}

func (s *Server) doMutate(ctx context.Context, mu *api.Mutation) (resp *api.Assigned, rerr error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand Down Expand Up @@ -473,16 +486,6 @@ func (s *Server) doMutate(ctx context.Context, mu *api.Mutation) (resp *api.Assi
return resp, fmt.Errorf("Empty mutation")
}

var l query.Latency
l.Start = time.Now()
gmu, err := parseMutationObject(mu)
if err != nil {
return resp, err
}

parseEnd := time.Now()
l.Parsing = parseEnd.Sub(l.Start)

defer func() {
l.Processing = time.Since(parseEnd)
resp.Latency = &api.Latency{
Expand Down

0 comments on commit d153e45

Please sign in to comment.