-
Notifications
You must be signed in to change notification settings - Fork 8
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
Support for policy query #143
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5919e03
params injection supports deep paths
AleF83 086c408
Query model changed
AleF83 cdecf15
First working version
AleF83 3aad83b
Second iteration
AleF83 3a8678c
rebase fixed
AleF83 dc9ccf5
fix 1
AleF83 39cd24d
tests fixed
AleF83 aeb25db
tests fixed
AleF83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import {GraphQLResolveInfo} from 'graphql'; | ||
import {GraphQLResolveInfo, graphql} from 'graphql'; | ||
import {RequestContext} from '../../context'; | ||
import {Policy, GraphQLArguments} from './types'; | ||
import {Policy as PolicyDefinition, PolicyArgsObject, PolicyAttachments} from '../../resource-repository'; | ||
import {Policy, GraphQLArguments, QueryResults} from './types'; | ||
import {Policy as PolicyDefinition, PolicyArgsObject, PolicyAttachments, PolicyQuery} from '../../resource-repository'; | ||
import {evaluate as evaluateOpa} from './opa'; | ||
import {injectParameters} from '../../paramInjection'; | ||
|
||
|
@@ -28,22 +28,28 @@ export class PolicyExecutor { | |
await Promise.all(this.policies.map(r => this.validatePolicy(r))); | ||
} | ||
|
||
async validatePolicy(policy: Policy) { | ||
async evaluatePolicy(policy: Policy): Promise<boolean> { | ||
const policyDefinition = this.getPolicyDefinition(policy.namespace, policy.name); | ||
|
||
const args = policyDefinition.args && this.preparePolicyArgs(policyDefinition.args, policy); | ||
// TODO: evaluate queries | ||
|
||
const query = policyDefinition.query && (await this.evaluatePolicyQuery(policyDefinition.query, args)); | ||
|
||
const evaluate = typeEvaluators[policyDefinition.type]; | ||
if (!evaluate) throw new Error(`Unsupported policy type ${policyDefinition.type}`); | ||
|
||
const {done, allow} = await evaluate({ | ||
...policy, | ||
args, | ||
query, | ||
policyAttachments: this.policyAttachments, | ||
}); | ||
if (!done) throw new Error('in-line query evaluation not yet supported'); | ||
return allow || false; | ||
} | ||
|
||
async validatePolicy(policy: Policy): Promise<void> { | ||
const allow = await this.evaluatePolicy(policy); | ||
if (!allow) throw new Error(`Unauthorized by policy ${policy.name} in namespace ${policy.namespace}`); | ||
} | ||
|
||
|
@@ -73,4 +79,33 @@ export class PolicyExecutor { | |
if (!policyDefinition) throw new Error(`The policy ${name} in namespace ${namespace} was not found`); | ||
return policyDefinition; | ||
} | ||
|
||
private async evaluatePolicyQuery( | ||
query: PolicyQuery, | ||
args: PolicyArgsObject = {} | ||
): Promise<QueryResults | undefined> { | ||
let variableValues = | ||
query.variables && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
Object.entries(query.variables).reduce<{[key: string]: any}>((policyArgs, [varName, varValue]) => { | ||
if (typeof varValue === 'string') { | ||
varValue = injectParameters(varValue, this.parent, args, this.context, this.info).value; | ||
} | ||
policyArgs[varName] = varValue; | ||
return policyArgs; | ||
}, {}); | ||
|
||
// TODO: Run with admin permissions | ||
const context: RequestContext = {...this.context, ignorePolicies: true}; | ||
const gqlResult = await graphql(this.info.schema, query.gql, undefined, context, variableValues); | ||
return gqlResult.data || undefined; | ||
} | ||
} | ||
|
||
declare module '../../context' { | ||
interface RequestContext { | ||
/** | ||
* This flag indicates that request should be resolved without invoking authorization policies evaluation | ||
*/ | ||
ignorePolicies: boolean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {SchemaDirectiveVisitor} from 'graphql-tools'; | ||
import {GraphQLField, GraphQLResolveInfo} from 'graphql'; | ||
import {RequestContext} from '../../context'; | ||
import {gql} from 'apollo-server-core'; | ||
import {PolicyResult, Policy} from './types'; | ||
import {PolicyExecutor} from './policy-executor'; | ||
|
||
export class PolicyQueryDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field: GraphQLField<any, any>) { | ||
field.resolve = async ( | ||
parent: any, | ||
args: any, | ||
context: RequestContext, | ||
info: GraphQLResolveInfo | ||
): Promise<PolicyResult> => { | ||
const policy: Policy = { | ||
namespace: this.args.namespace, | ||
name: this.args.name, | ||
args: args, | ||
}; | ||
|
||
const executor = new PolicyExecutor([], parent, args, context, info); | ||
const allow = await executor.evaluatePolicy(policy); | ||
return {allow}; | ||
}; | ||
} | ||
} | ||
|
||
export const sdl = gql` | ||
directive @policyQuery(namespace: String!, name: String!) on FIELD_DEFINITION | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should also support param injection from
jwt
in addition to args, though this is something I'm adding in my next PR. We should probably wait until after both PRs are merged and then addjwt
injection support to queries.Probably worth adding a
TODO
for it though to be safe