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

Authorize rest requests #2753

Conversation

DarshitChanpura
Copy link
Member

@DarshitChanpura DarshitChanpura commented May 9, 2023

Description

Continuation of #2601. Adds support for legacy naming conventions

  • Category : Enhancement
  • Why these changes are required? To support legacy action names to authorize REST requests

Issues Resolved

#2752

Testing

Manual testing.

  1. Setup opensearch-cluster and helloWorld extension following this developer guide.
  2. Check-out this branch.
  3. Install security in opensearch.
  4. Ensure certificates are setup correctly in config folder and in opensearch.yml
  5. Following should be the output of each curl command:
~ curl -XGET https://new-user:admin@localhost:9200/_extensions/_hw/hello --insecure
Hello, World!%~ curl -XPUT https://new-user:admin@localhost:9200/_extensions/_hw/hello/HW --insecure -H 'Content-Type: application/json' -d '{}'
Updated the world's name to HW%~ curl -XGET https://new-user:admin@localhost:9200/_extensions/_hw/hello --insecure
Hello, HW!%~ curl -XPOST https://new-user:admin@localhost:9200/_extensions/_hw/hello --insecure -H 'Content-Type: application/json' -d '{ }'
no permissions for [hw:greet_with_adjective] and User [name=new-user, backend_roles=[], requestedTenant=null]%~ curl -XDELETE https://new-user:admin@localhost:9200/_extensions/_hw/goodbye --insecure
no permissions for [hw:goodbye] and User [name=new-user, backend_roles=[], requestedTenant=null]%

Check List

  • New functionality includes testing
  • New functionality has been documented
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@DarshitChanpura DarshitChanpura changed the title Authorize rest requests Authorize rest requests bound to Extensions May 9, 2023
@DarshitChanpura DarshitChanpura changed the title Authorize rest requests bound to Extensions [Feature/Extensions] Authorize rest requests bound to Extensions May 9, 2023
@DarshitChanpura DarshitChanpura marked this pull request as draft May 9, 2023 21:51
@DarshitChanpura DarshitChanpura marked this pull request as ready for review May 19, 2023 17:06
Copy link
Contributor

@stephen-crawford stephen-crawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DarshitChanpura, looks good outside two blank lines and Craig;s comment.

config/roles.yml Outdated Show resolved Hide resolved
Copy link
Contributor

@stephen-crawford stephen-crawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your plans for testing these changes? Are you just trying to let the existing tests cover the changes? Is there a way to add explicit checks for the changes you have introduced?

@stephen-crawford stephen-crawford self-requested a review May 23, 2023 16:23
@stephen-crawford stephen-crawford dismissed their stale review May 23, 2023 16:23

clicked wrong button

@DarshitChanpura
Copy link
Member Author

DarshitChanpura commented May 23, 2023

What are your plans for testing these changes? Are you just trying to let the existing tests cover the changes? Is there a way to add explicit checks for the changes you have introduced?

This will be covered once end to end testing is implemented.

Copy link
Member

@cwperks cwperks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took another pass. @DarshitChanpura Is it possible to remove the impliesLegacyPermission? This PR looks close, but I'd like to get resolution on this first.

DEVELOPER_GUIDE.md Outdated Show resolved Hide resolved
REST_AUTHZ_FOR_PLUGINS.md Show resolved Hide resolved
REST_AUTHZ_FOR_PLUGINS.md Show resolved Hide resolved
REST_AUTHZ_FOR_PLUGINS.md Show resolved Hide resolved
* @param handlerPath The path from the {@link RestHandler.Route}
* @return true if the request path matches the route
*/
private boolean restPathMatches(String requestPath, String handlerPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Units like this should have tests with them. I've seen code similar to this in core, can this be extracted to a library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test file already exists. Check it out https://github.com/opensearch-project/security/blob/9b05d44a6ba15bca521e1ccbd6e33124facddcc0/src/test/java/org/opensearch/security/filter/RestPathMatchesTest.java

can this be extracted to a library?

It's only used at 2 places and I don't think it will be used at other places in future. I think keeping it here is fine.

Copy link
Collaborator

@RyanL1997 RyanL1997 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DarshitChanpura, in generally LGTM beyond some of the suggestions that Craig and Stephen left and thanks for all the work on this one!

Signed-off-by: Darshit Chanpura <[email protected]>
@DarshitChanpura DarshitChanpura dismissed peternied’s stale review July 7, 2023 20:31

All requested changes have been addressed.

RyanL1997
RyanL1997 previously approved these changes Jul 7, 2023
@DarshitChanpura
Copy link
Member Author

Can we change an existing action in the security plugin to use NamedRoute so we can write an integration test that confirms AuthZ for rest works?

@peternied Added a new endpoint GET /whoamiprotected and integrations tests for it WhoAmITests.java.

@@ -179,7 +179,7 @@ public void testInjectedUser() throws Exception {
Assert.assertTrue(resc.getBody().contains("\"remote_address\":\"8.8.8.8:8\""));
Assert.assertTrue(resc.getBody().contains("\"backend_roles\":[\"role1\",\"role2\"]"));
// mapped by username
Assert.assertTrue(resc.getBody().contains("\"roles\":[\"opendistro_security_all_access\""));
Assert.assertTrue(resc.getBody().contains("\"opendistro_security_all_access\""));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. because nagilum is now associated with another role called who_am_i, the request body won't always start with \"roles\":[

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the assertion above with backend roles, I think this should assert all expected roles

Copy link
Contributor

@stephen-crawford stephen-crawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job DC!

log.debug("Mapped roles: {}", mappedRoles.toString());
}

for (String action : action0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I had to reference up above that action0 is a set. This looks like its looping through a singular action.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be singular for name, but it will be in loop for actionNames

cwperks
cwperks previously approved these changes Jul 7, 2023
Signed-off-by: Darshit Chanpura <[email protected]>
@DarshitChanpura DarshitChanpura merged commit a53a8a6 into opensearch-project:main Jul 7, 2023
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.x 2.x
# Navigate to the new working tree
cd .worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-2753-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 a53a8a6c9d990911cb5f6a42e937b395c598b0d5
# Push it to GitHub
git push --set-upstream origin backport/backport-2753-to-2.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-2753-to-2.x.

DarshitChanpura added a commit to DarshitChanpura/security that referenced this pull request Jul 7, 2023
* WIP on rest layer authz

Signed-off-by: Craig Perkins <[email protected]>

* WIP on rest-layer authz

Signed-off-by: Craig Perkins <[email protected]>

* Extension handshake

Signed-off-by: Craig Perkins <[email protected]>

* Extension TLS

Signed-off-by: Craig Perkins <[email protected]>

* Remove SecurityRestFilterChanges to isolate extension TLS change

Signed-off-by: Craig Perkins <[email protected]>

* Remove SecurityRestFilterChanges to isolate extension TLS change

Signed-off-by: Craig Perkins <[email protected]>

* Remove SecurityRestFilterChanges to isolate extension TLS change

Signed-off-by: Craig Perkins <[email protected]>

* Remove SecurityRestFilterChanges to isolate extension TLS change

Signed-off-by: Craig Perkins <[email protected]>

* Remove SecurityRestFilterChanges to isolate extension TLS change

Signed-off-by: Craig Perkins <[email protected]>

* WIP for HelloWorld sample extension role

Signed-off-by: Craig Perkins <[email protected]>

* Initial implementation of authz check in REST layer

Signed-off-by: Craig Perkins <[email protected]>

* Remove header

Signed-off-by: Craig Perkins <[email protected]>

* Create authorizeRequest method

Signed-off-by: Craig Perkins <[email protected]>

* small fix

Signed-off-by: Craig Perkins <[email protected]>

* Change to ProtectedRoute

Signed-off-by: Craig Perkins <[email protected]>

* Remove extension permissions

Signed-off-by: Craig Perkins <[email protected]>

* Initial implementation of authz check in REST layer

Signed-off-by: Craig Perkins <[email protected]>

* Extension TLS

Signed-off-by: Craig Perkins <[email protected]>

* Adds dummy roles for testing rest authorization against legacy permissions

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds support for legacy permissions to perform rest authorization

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes white-space changes

Signed-off-by: Darshit Chanpura <[email protected]>

* Rebases ConfigConstants with main

Signed-off-by: Darshit Chanpura <[email protected]>

* Implements a new logic for rest permissions check to be more flexible

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes spotless errors

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds regex to match against current role permissions when comparing new permission with legacy ones

Signed-off-by: Darshit Chanpura <[email protected]>

* Moves legacy permission check logic to ConfigModelV7

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes extra new-lines

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes unused imports

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes out-of-scope white space changes

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes code-ql errors

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes spotless and code-ql errors

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes variable name and remove references to whitelist in javadoc

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds tests for rest layer privilege evaluator

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds license header to the test file

Signed-off-by: Darshit Chanpura <[email protected]>

* Updates zstd dependency to fetch from core version.properties

Signed-off-by: Darshit Chanpura <[email protected]>

* Updates action name in the regex to be dynamic

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds support for allowing evaluation against multiple actions names for a registered named route

Signed-off-by: Darshit Chanpura <[email protected]>

* Updates tests

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds null check

Signed-off-by: Darshit Chanpura <[email protected]>

* Makes authorize logic clearer to follow

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds extra check to ensure new actions are also evaluated against transport actions

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes spotless errors

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes security rest filter setup

Signed-off-by: Darshit Chanpura <[email protected]>

* Removes extension reference

Signed-off-by: Darshit Chanpura <[email protected]>

* turn on audit logging

Signed-off-by: Maciej Mierzwa <[email protected]>

* Adds unit tests for restPathMatches method

Signed-off-by: Darshit Chanpura <[email protected]>

* Cleans up TODOs

Signed-off-by: Darshit Chanpura <[email protected]>

* Organizes demo users and roles for extension

Signed-off-by: Darshit Chanpura <[email protected]>

* Address PR feedback

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds more comments

Signed-off-by: Darshit Chanpura <[email protected]>

* add privileges info

Signed-off-by: Maciej Mierzwa <[email protected]>

* Makes whoami action a named route and fixes license header check

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds integ tests for whoami route

Signed-off-by: Darshit Chanpura <[email protected]>

* Change permissions order in roles.yml

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds developer documentation for authorization in REST layer

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes broken tests

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes checkstyle errors

Signed-off-by: Darshit Chanpura <[email protected]>

* Addresses feedback and cleans up logic for super admin check

Signed-off-by: Darshit Chanpura <[email protected]>

* Addresses Plugin Install CI failure

Signed-off-by: Darshit Chanpura <[email protected]>

* Fixes failing citest task

Signed-off-by: Darshit Chanpura <[email protected]>

* Modifies WhoAmI integ tests

Signed-off-by: Darshit Chanpura <[email protected]>

* Adds a new endpoint called whoamiprotected and removes changes made to whoami route

Signed-off-by: Darshit Chanpura <[email protected]>

* Updates documentation to reflect the new API

Signed-off-by: Darshit Chanpura <[email protected]>

* Addresses PR feedback

Signed-off-by: Darshit Chanpura <[email protected]>

* Renames action0 to actions

Signed-off-by: Darshit Chanpura <[email protected]>

---------

Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Darshit Chanpura <[email protected]>
Signed-off-by: Maciej Mierzwa <[email protected]>
Co-authored-by: Craig Perkins <[email protected]>
Co-authored-by: MaciejMierzwa <[email protected]>
(cherry picked from commit a53a8a6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x backport to 2.x branch v2.9.0 v2.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants