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

graphql: Added RBAC and related tests #5392

Merged
merged 4 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
99 changes: 90 additions & 9 deletions graphql/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ func getJWT(t *testing.T, user, role string) http.Header {
},
}

claims.Foo["USER"] = user
claims.Foo["ROLE"] = role
if user != "" {
claims.Foo["USER"] = user
}

if role != "" {
claims.Foo["ROLE"] = role
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString([]byte("secretkey"))
Expand All @@ -129,8 +134,40 @@ func getJWT(t *testing.T, user, role string) http.Header {
}

func TestOrRBACFilter(t *testing.T) {
t.Skip()
testCases := []TestCase{}
testCases := []TestCase{{
user: "user1",
role: "ADMIN",
result: `{
"queryProject": [
{
"name": "Project1"
},
{
"name": "Project2"
}
]
}`,
}, {
user: "user1",
role: "USER",
result: `{
"queryProject": [
{
"name": "Project1"
}
]
}`,
}, {
user: "user4",
role: "USER",
result: `{
"queryProject": [
{
"name": "Project2"
}
]
}`,
}}

query := `
query {
Expand Down Expand Up @@ -300,7 +337,38 @@ func TestRootFilter(t *testing.T) {
queryColumn(order: {asc: name}) {
name
}
}`

for _, tcase := range testCases {
t.Run(tcase.role+tcase.user, func(t *testing.T) {
getUserParams := &common.GraphQLParams{
Headers: getJWT(t, tcase.user, tcase.role),
Query: query,
}

gqlResponse := getUserParams.ExecuteAsPost(t, graphqlURL)
require.Nil(t, gqlResponse.Errors)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}
}

func TestDeepRBACValue(t *testing.T) {
testCases := []TestCase{
{user: "user1", role: "USER", result: `{"queryUser": [{"username": "user1", "issues":[]}]}`},
{user: "user1", role: "ADMIN", result: `{"queryUser":[{"username":"user1","issues":[{"msg":"Issue1"}]}]}`},
}

query := `
{
queryUser (filter:{username:{eq:"user1"}}) {
username
issues {
msg
}
}
}
`

for _, tcase := range testCases {
Expand All @@ -319,8 +387,11 @@ func TestRootFilter(t *testing.T) {
}

func TestRBACFilter(t *testing.T) {
t.Skip()
testCases := []TestCase{}
testCases := []TestCase{
{role: "USER", result: `{"queryLog": []}`},
{result: `{"queryLog": []}`},
{role: "ADMIN", result: `{"queryLog": [{"logs": "Log1"},{"logs": "Log2"}]}`}}

query := `
query {
queryLog (order: {asc: logs}) {
Expand All @@ -345,8 +416,19 @@ func TestRBACFilter(t *testing.T) {
}

func TestAndRBACFilter(t *testing.T) {
t.Skip()
testCases := []TestCase{}
testCases := []TestCase{{
user: "user1",
role: "USER",
result: `{"queryIssue": []}`,
}, {
user: "user2",
role: "USER",
result: `{"queryIssue": []}`,
}, {
user: "user2",
role: "ADMIN",
result: `{"queryIssue": [{"msg": "Issue2"}]}`,
}}
query := `
query {
queryIssue (order: {asc: msg}) {
Expand All @@ -368,7 +450,6 @@ func TestAndRBACFilter(t *testing.T) {
require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}

}

func TestNestedFilter(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion graphql/e2e/auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type User {
disabled: Boolean
tickets: [Ticket] @hasInverse(field: assignedTo)
secrets: [UserSecret]
issues: [Issue]
}

type UserSecret @auth(
Expand Down Expand Up @@ -108,8 +109,19 @@ type Log @auth(
logs: String
}

type ComplexLog @auth(
query: { and : [
{ rule: "{$ROLE: { eq: \"ADMIN\" }}" },
{ not : { rule: "{$ROLE: { eq: \"USER\" }}" }}
]}
) {
id: ID!
logs: String
}

type Project @auth(
query: { rule: """query($USER: String!) {
query: { or: [
{ rule: """query($USER: String!) {
queryProject {
roles(filter: { permission: { eq: VIEW } }) {
assignedTo(filter: { username: { eq: $USER } }) {
Expand All @@ -118,6 +130,8 @@ type Project @auth(
}
}
}""" },
{ rule: "{$ROLE: { eq: \"ADMIN\" }}" }
]}
) {
projID: ID!
name: String!
Expand Down Expand Up @@ -245,3 +259,4 @@ type Ticket @auth(
title: String! @search(by: [term])
assignedTo: [User!]
}

6 changes: 4 additions & 2 deletions graphql/e2e/auth/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"User.age": 10,
"User.username": "user1",
"User.isPublic": true,
"User.disabled": false
"User.disabled": false,
"User.issues": [{"uid": "_:issue1"}]
},
{
"uid": "_:user2",
"dgraph.type": "User",
"User.age": 11,
"User.username": "user2",
"User.isPublic": true,
"User.disabled": true
"User.disabled": true,
"User.issues": [{"uid": "_:issue2"}]
},
{
"uid": "_:user3",
Expand Down
176 changes: 176 additions & 0 deletions graphql/resolve/auth_query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,182 @@
UserSecret2 as var(func: uid(UserSecret1)) @filter(eq(UserSecret.ownedBy, "user1")) @cascade
}

- name: "Deep RBAC rules true"
gqlquery: |
query {
queryUser {
issues {
id
}
}
}
role: "ADMIN"
dgquery: |-
query {
queryUser(func: type(User)) {
issues : User.issues @filter(uid(Issue1)) {
id : uid
}
dgraph.uid : uid
}
Issue1 as var(func: type(Issue)) @cascade {
owner : Issue.owner @filter(eq(User.username, "user1"))
dgraph.uid : uid
}
}

- name: "Deep RBAC rules false"
gqlquery: |
query {
queryUser {
username
issues {
id
}
}
}
role: "USER"
dgquery: |-
query {
queryUser(func: type(User)) {
username : User.username
dgraph.uid : uid
}
}


- name: "Auth with top level AND rbac true"
gqlquery: |
query {
queryIssue {
msg
}
}
role: "ADMIN"
dgquery: |-
query {
queryIssue(func: uid(Issue1)) @filter(uid(Issue2)) {
msg : Issue.msg
dgraph.uid : uid
}
Issue1 as var(func: type(Issue))
Issue2 as var(func: uid(Issue1)) @cascade {
owner : Issue.owner @filter(eq(User.username, "user1"))
dgraph.uid : uid
}
}

- name: "Auth with complex rbac rules, true"
gqlquery: |
query {
queryComplexLog {
logs
}
}
role: "ADMIN"
dgquery: |-
query {
queryComplexLog(func: type(ComplexLog)) {
logs : ComplexLog.logs
dgraph.uid : uid
}
}

- name: "Auth with complex rbac rules, false"
gqlquery: |
query {
queryComplexLog {
logs
}
}
role: "USER"
dgquery: |-
query {
queryComplexLog()
}

- name: "Auth with top level rbac true"
gqlquery: |
query {
queryLog {
logs
}
}
role: "ADMIN"
dgquery: |-
query {
queryLog(func: type(Log)) {
logs : Log.logs
dgraph.uid : uid
}
}

- name: "Auth with top level rbac false"
gqlquery: |
query {
queryLog {
logs
}
}
role: "USER"
dgquery: |-
query {
queryLog()
}

- name: "Auth with top level AND rbac false"
gqlquery: |
query {
queryIssue {
msg
}
}
role: "USER"
dgquery: |-
query {
queryIssue()
}


- name: "Auth with top level OR rbac true"
gqlquery: |
query {
queryProject {
name
}
}
role: "ADMIN"
dgquery: |-
query {
queryProject(func: type(Project)) {
name : Project.name
dgraph.uid : uid
}
}

- name: "Auth with top level OR rbac false"
gqlquery: |
query {
queryProject {
name
}
}
role: "USER"
dgquery: |-
query {
queryProject(func: uid(Project1)) @filter(uid(Project2)) {
name : Project.name
dgraph.uid : uid
}
Project1 as var(func: type(Project))
Project2 as var(func: uid(Project1)) @cascade {
roles : Project.roles @filter(eq(Role.permission, "VIEW")) {
assignedTo : Role.assignedTo @filter(eq(User.username, "user1"))
dgraph.uid : uid
}
dgraph.uid : uid
}
}

- name: "Auth with top level filter : query, filter and order"
gqlquery: |
Expand Down
Loading