Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GraphQL): Support auth with custom DQL #7775

Merged
merged 37 commits into from
May 26, 2021
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3323ed7
initial commit
minhaj-shakeel May 3, 2021
389349e
minor fix
minhaj-shakeel May 3, 2021
84c82c5
change Root Query
minhaj-shakeel May 4, 2021
8dec57e
add support for @groupby in AsString
minhaj-shakeel May 4, 2021
41a4eac
attach rewriter to custom resolver
minhaj-shakeel May 4, 2021
af4479b
add unit tests
minhaj-shakeel May 4, 2021
1e048ce
minor fix
minhaj-shakeel May 6, 2021
e74fadb
support for multiple query blocks
minhaj-shakeel May 6, 2021
04e5502
support var query with rbac = Negative
minhaj-shakeel May 6, 2021
181684d
fix AsString
minhaj-shakeel May 7, 2021
f050f8d
fix test case
minhaj-shakeel May 7, 2021
2afba2a
add auth rules on children
minhaj-shakeel May 10, 2021
d4f0031
fix var block auth query
minhaj-shakeel May 10, 2021
0b295b7
fix test name
minhaj-shakeel May 10, 2021
c6e8e2a
fix AsString
minhaj-shakeel May 10, 2021
fa81419
fix AsString
minhaj-shakeel May 10, 2021
9fbe48f
insert quotes in the argument
minhaj-shakeel May 11, 2021
4da6db7
remove redundant function
minhaj-shakeel May 11, 2021
188a4d2
fix for filter arguments
minhaj-shakeel May 11, 2021
b4977db
handle filter attribute
minhaj-shakeel May 11, 2021
f45a91f
fix rewriting of test cases
minhaj-shakeel May 11, 2021
55149db
add some comments
minhaj-shakeel May 11, 2021
f786c23
modify unit tests
minhaj-shakeel May 11, 2021
b19f215
handle has function
minhaj-shakeel May 12, 2021
c124b59
add e2e test cases
minhaj-shakeel May 12, 2021
f4bcf9c
fix var query for RBAC negative
minhaj-shakeel May 12, 2021
b54cc0c
fix test
minhaj-shakeel May 12, 2021
ba1aa4a
address comments
minhaj-shakeel May 13, 2021
e8a3de4
add e2e test for interface auth
minhaj-shakeel May 17, 2021
84f1af2
clean code
minhaj-shakeel May 17, 2021
8359e73
add example of order
minhaj-shakeel May 17, 2021
0abd898
add e2e case for interfaces
minhaj-shakeel May 17, 2021
7e21e44
minor fix
minhaj-shakeel May 17, 2021
434cdcd
fmt
minhaj-shakeel May 17, 2021
a1f7cad
address Rajas's comments
minhaj-shakeel May 24, 2021
209e2de
fix indent
minhaj-shakeel May 24, 2021
fee9e14
add Todo
minhaj-shakeel May 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add auth rules on children
minhaj-shakeel committed May 10, 2021
commit 2afba2a6bbcae84d8dfa6127a81fd581a0c0da19
12 changes: 12 additions & 0 deletions graphql/e2e/auth/schema.graphql
Original file line number Diff line number Diff line change
@@ -1026,6 +1026,18 @@ type Query {
}
}"""
)

queryUsers: [User] @custom(dql: """
query {
queryUsers(func: type(User)) {
username: User.username
tickets: User.tickets {
id: uid
title: Ticket.title
}
}
}"""
)
}

## custom DQL testing end
58 changes: 58 additions & 0 deletions graphql/resolve/custom_auth_query_test.yaml
Original file line number Diff line number Diff line change
@@ -79,3 +79,61 @@
random : Issue.random
}
}
- name : "Auth Rules with deep filter"
gqlquery: |
query {
queryUsers {
username
tickets {
id
title
}
}
}
jwtvar:
USER: "user1"
dgquery: |-
query {
queryUsers(func: uid(UserRoot)) {
username : User.username
tickets : User.tickets @filter(uid(Ticket_1)) {
id : uid
title : Ticket.title
}
}
UserRoot as var(func: uid(User_4))
User_4 as var(func: type(User))
var(func: uid(UserRoot)) {
Ticket_2 as User.tickets
}
Ticket_1 as var(func: uid(Ticket_2)) @filter(uid(Ticket_Auth3))
Ticket_Auth3 as var(func: uid(Ticket_2)) @cascade {
Ticket.onColumn : Ticket.onColumn {
Column.inProject : Column.inProject {
Project.roles : Project.roles @filter(eq(Role.permission, "VIEW")) {
Role.assignedTo : Role.assignedTo @filter(eq(User.username, "user1"))
}
}
}
}
}

- name : "Auth rules with deep filter failed"
gqlquery: |
query{
queryUsers{
username
tickets {
id
title
}
}
}
dgquery: |-
query {
queryUsers(func: uid(UserRoot)) {
username : User.username
}
UserRoot as var(func: uid(User_3))
User_3 as var(func: type(User))
}
140 changes: 140 additions & 0 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
@@ -697,6 +697,11 @@ func rewriteWithAuth(
dgQueries = append(dgQueries, qry)
continue
}

authRw.hasAuthRules = dqlHasAuthRules(qry, typ, authRw)

fldAuthQueries := addAuthQueriesOnSelectionSet(qry, typ, authRw)

rbac := authRw.evaluateStaticRules(typ)
if rbac == schema.Negative {
if qry.Attr == "var" {
@@ -708,7 +713,11 @@ func rewriteWithAuth(
}
continue
}

dgQueries = append(dgQueries, authRw.addAuthQueries(typ, []*gql.GraphQuery{qry}, rbac)...)
if len(fldAuthQueries) > 0 {
dgQueries = append(dgQueries, fldAuthQueries...)
}
}
return dgQueries, nil
}
@@ -1614,6 +1623,137 @@ func addSelectionSetFrom(
return authQueries
}

func dqlHasAuthRules(q *gql.GraphQuery, typ schema.Type, authRw *authRewriter) bool {
if q == nil {
return false
}
rn := authRw.selector(typ)
if rn != nil {
return true
}
for _, fld := range q.Children {
var fldName string
fldSplit := strings.Split(fld.Attr, ".")
if len(fldSplit) > 1 {
fldName = fldSplit[1]
} else {
continue
}
if authRules := dqlHasAuthRules(fld, typ.Field(fldName).Type(), authRw); authRules {
return true
}
}
return false
}

func addAuthQueriesOnSelectionSet(
q *gql.GraphQuery,
typ schema.Type,
auth *authRewriter) []*gql.GraphQuery {

var authQueries []*gql.GraphQuery
var children []*gql.GraphQuery
for _, f := range q.Children {
var fldName string

fldSplit := strings.Split(f.Attr, ".")

// field must of the type User.tickets or "uid".
if len(fldSplit) > 1 {
fldName = fldSplit[1]
} else {
children = append(children, f)
continue
}
fldType := typ.Field(fldName).Type()

rbac := auth.evaluateStaticRules(fldType)

// Since the recursion processes the query in bottom up way, we store the state of the so
// that we can restore it later.
var parentVarName, parentQryName string
if len(f.Children) > 0 && !auth.isWritingAuth && auth.hasAuthRules {
parentVarName = auth.parentVarName
parentQryName = auth.varName
auth.parentVarName = auth.varGen.Next(fldType, "", "", auth.isWritingAuth)
auth.varName = auth.varGen.Next(fldType, "", "", auth.isWritingAuth)
}

selectionAuth := addAuthQueriesOnSelectionSet(f, fldType, auth)

restoreAuthState := func() {
if len(f.Children) > 0 && !auth.isWritingAuth && auth.hasAuthRules {
// Restore the auth state after processing is done.
auth.parentVarName = parentVarName
auth.varName = parentQryName
}
}

//If RBAC rules are evaluated to `Uncertain` then we add the Auth rules.
var fieldAuth []*gql.GraphQuery
var authFilter *gql.FilterTree

if rbac == schema.Positive || rbac == schema.Uncertain {
children = append(children, f)
}

if rbac == schema.Negative {
restoreAuthState()
continue
}

if rbac == schema.Uncertain {
fieldAuth, authFilter = auth.rewriteAuthQueries(fldType)
}

if len(f.Children) > 0 && !auth.isWritingAuth && auth.hasAuthRules {

parentQry := &gql.GraphQuery{
Func: &gql.Function{
Name: "uid",
Args: []gql.Arg{{Value: parentVarName}},
},
Attr: "var",
Children: []*gql.GraphQuery{{Attr: f.Attr, Var: auth.varName}},
}

// This query aggregates all filters and auth rules and is used by root query to filter
// the final nodes for the current level.
// User3 as var(func: uid(User4)) @filter((eq(User.username, "User1") AND (...Auth Filter))))
selectionQry := &gql.GraphQuery{
Var: auth.parentVarName,
Attr: "var",
Func: &gql.Function{
Name: "uid",
Args: []gql.Arg{{Value: auth.varName}},
},
}

commonAuthQueryVars := commonAuthQueryVars{
parentQry: parentQry,
selectionQry: selectionQry,
}

// add child filter to parent query, auth filters to selection query and
// selection query as a filter to child
commonAuthQueryVars.parentQry.Children[0].Filter = f.Filter
commonAuthQueryVars.selectionQry.Filter = authFilter
f.Filter = &gql.FilterTree{
Func: &gql.Function{
Name: "uid",
Args: []gql.Arg{{Value: commonAuthQueryVars.selectionQry.Var}},
},
}
authQueries = append(authQueries, commonAuthQueryVars.parentQry, commonAuthQueryVars.selectionQry)
}
authQueries = append(authQueries, selectionAuth...)
authQueries = append(authQueries, fieldAuth...)
restoreAuthState()
}
q.Children = children
return authQueries
}

func addOrder(q *gql.GraphQuery, field schema.Field) {
orderArg := field.ArgValue("order")
order, ok := orderArg.(map[string]interface{})