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

Ensure the all_public_actions method works for Grape APIs #23

Merged
merged 2 commits into from
Aug 11, 2020

Conversation

tlconnor
Copy link
Contributor

@tlconnor tlconnor commented Aug 9, 2020

The DeclarativeAuthorization::Test::Helpers module includes a method called all_public_actions that is responsible for determining the set of publically available methods on a controller. This is used to verify that every public method has associated access tests.

This method assumes that the controller is a Rails controller, so does not work correctly with Grape APIs. This means that it is possible for APIs to be missing authorization rules.

This PR fixes the issue by adjusting the all_public_actions method to behave differently when run against Grape APIs.

@@ -155,8 +155,14 @@ def this_is_an_abstract_controller_so_it_needs_no_access_tests
alias :access_tests_not_required :this_is_an_abstract_controller_so_it_needs_no_access_tests

def all_public_actions
actions = controller_class.public_instance_methods(false)
actions += controller_class.superclass.public_instance_methods(false)
actions = []
Copy link
Member

Choose a reason for hiding this comment

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

Could you update the README as well?
Currently it says:
Support for Rails 4 and 5
Instead, I think it should follow the Appraisals:
https://github.com/appfolio/ae_declarative_authorization/blob/master/Appraisals#L1-L2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree it should be updated, but that seems unrelated to this change.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough.

actions += controller_class.superclass.public_instance_methods(false)
actions = []
if defined?(Grape) && [Grape::API, Grape::API::Instance].any? { |base| controller_class < base }
actions += controller_class.routes.map { |api| "#{api.request_method} #{api.origin}" }
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand how this works but I guess it adds a string that is somehow parsed somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In rails controllers, actions are uniquely identified by their method name (eg index, show, etc). In Grape APIs the endpoints are not generated in the same way, so we instead need to use a combination of the HTTP Verb and Path.

This means authorization rules for APIs look like this:

role :tenant do
  allowed to: ['GET /occupancies/:id'], when: [:read, :write_without_delete, :write]
end

The string GET /occupancies/:id is what is being checked here.

Copy link
Member

Choose a reason for hiding this comment

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

Oh right, that string. Yeah makes sense.

@barunio
Copy link

barunio commented Aug 11, 2020

Is there a way to add tests for this additional behavior?

@tlconnor tlconnor merged commit 964c9c6 into master Aug 11, 2020
@tlconnor tlconnor deleted the fixAllPublicActionsGrape branch August 11, 2020 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants