-
Notifications
You must be signed in to change notification settings - Fork 172
THREESCALE-11887 Add configurable whitelist_deny_unmatched policy option #1605
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
Open
borisurbanik
wants to merge
2
commits into
3scale:master
Choose a base branch
from
borisurbanik:bu-THREESCALE-11887
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -99,6 +99,10 @@ end | |
| function _M.new(config) | ||
| local self = new() | ||
| self.type = config.type or "whitelist" | ||
| -- When true (default), a whitelist denies requests to paths not matching any | ||
| -- configured scope. When false, unmatched paths are allowed through. | ||
| -- Has no effect when type is "blacklist". | ||
| self.whitelist_deny_unmatched = config.whitelist_deny_unmatched ~= false and true or false | ||
| self.scopes = config.scopes or {} | ||
|
|
||
| build_scopes(self.scopes) | ||
|
|
@@ -159,7 +163,7 @@ local function match_client_roles(scope, context) | |
| end | ||
|
|
||
| local function validate_scope_access(scope, context, uri, request_method) | ||
| for _, method in ipairs(scope.methods) do | ||
| for _, method in ipairs(scope.methods) do | ||
|
|
||
| local resource = scope.resource_template_string:render(context) | ||
|
|
||
|
|
@@ -173,40 +177,53 @@ local function validate_scope_access(scope, context, uri, request_method) | |
|
|
||
| if mapping_rule:matches(request_method, uri) then | ||
| if match_realm_roles(scope, context) and match_client_roles(scope, context) then | ||
| return true | ||
| return true, true | ||
| end | ||
| return true, false | ||
| end | ||
| end | ||
| return false | ||
| return false, false | ||
| end | ||
|
Comment on lines
178
to
186
Contributor
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. This thing is confusing and error prone, if I accidentally swap the variables order then I would get the wrong behavior. I suggest to do the following:
Then just check for true/false at outer scope. |
||
|
|
||
| local function scopes_check(scopes, context) | ||
| local uri = ngx.var.uri | ||
| local request_method = ngx.req.get_method() | ||
| local request_method = ngx.req.get_method() | ||
|
|
||
| if not context.jwt then | ||
| return false | ||
| return false, false | ||
| end | ||
|
|
||
| local any_resource_matched = false | ||
|
|
||
| for _, scope in ipairs(scopes) do | ||
| if validate_scope_access(scope, context, uri, request_method) then | ||
| return true | ||
| local resource_matched, roles_passed = validate_scope_access(scope, context, uri, request_method) | ||
| if resource_matched then | ||
| if roles_passed then | ||
| return true, true | ||
| end | ||
| any_resource_matched = true | ||
| end | ||
| end | ||
|
|
||
| return false | ||
| return any_resource_matched, false | ||
| end | ||
|
|
||
| function _M:access(context) | ||
| if scopes_check(self.scopes, context) then | ||
| if self.type == "blacklist" then | ||
| local resource_matched, roles_passed = scopes_check(self.scopes, context) | ||
|
|
||
| if self.type == "whitelist" then | ||
| if resource_matched and not roles_passed then | ||
| return errors.authorization_failed(context.service) | ||
| end | ||
| if not resource_matched and self.whitelist_deny_unmatched then | ||
| return errors.authorization_failed(context.service) | ||
| end | ||
| else | ||
| if self.type == "whitelist" then | ||
| elseif self.type == "blacklist" then | ||
| if resource_matched and roles_passed then | ||
| return errors.authorization_failed(context.service) | ||
| end | ||
| end | ||
|
|
||
| return true | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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.
What is this? why can't we do the following?