-
Notifications
You must be signed in to change notification settings - Fork 4
[PW-2409] Policies implementation #123
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
Conversation
84a48b8
to
5c53e6f
Compare
7a28bd9
to
326db00
Compare
050aefd
to
5a0e178
Compare
f7f15f3
to
e2ca5f5
Compare
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.
On going review. but first set of comments to be addressed. I'm currently looking at the URN logic that is quite complex and I believe it can be simplified.
|
||
def urn_match?(urn_to_compare) | ||
params = %i[txt partition_name service_name region account_id resource] | ||
record_urn = Ros::Urn.from_urn(self.class.to_urn) | ||
record_urn = Ros::Urn.from_urn(self.to_urn) |
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 do we need the class urn_match?
method for?
check_action(:create?) | ||
end | ||
actions = if user.attached_actions.is_a?(String) | ||
JSON.parse(user.attached_actions) |
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.
I think that this should be handled by the PolicyUser and not the ApplicationPolicy itself, meaning that once we ask the PolicyUser for its actions, we should always get the same data type back.
extend ActiveSupport::Concern | ||
|
||
included do | ||
scope :everything, ->(_user_context) { all } |
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.
I'm not sure we should be sending the whole context to the scope. This would mean that all the models now all of the sudden know how to handle PolicyUser and know what to expect inside of it. I don't think that is the right way of doing it.
The context should most likely receive only the cognito_user_id and use it when needed, ignore it when they don't care about it
@@ -16,22 +16,24 @@ def find_by_urn(value); find_by(urn_id => value) end | |||
|
|||
# NOTE: Override in model to provide a custom id | |||
def urn_id; :id end | |||
end | |||
|
|||
included do |
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.
Do we need to have urn_match? method both in the class level and instance level?
def edit? | ||
update? | ||
end | ||
actions.select! { |i| i['effect'] == 'allow' && (i['name'] == action.to_s || i['name'] == '*') } |
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.
This should also probably be the responsibility of the PolicyUser instead of the application policy.
E.g.
user.can?(action, record) which returns true/false
|
||
scopes = [] | ||
|
||
actions.select! { |i| i['effect'] == 'allow' && (i['name'] == user.params['action'] || i['name'] == '*') } |
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.
This could probably become
user.scopes_for?(action, scope) which would return the scopes to be sent to the original scope (line 61)
@@ -6,6 +6,8 @@ class User < Cognito::ApplicationRecord | |||
has_many :user_pools | |||
has_many :pools, through: :user_pools | |||
|
|||
scope :owned, ->(user_context) { where(id: user_context.cognito_user_id) } |
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.
So, as mentioned above, I don't think active records should know anything about request contexts
@@ -17,7 +17,7 @@ | |||
let(:scope) { Pundit.policy_scope!(policy_user, User) } | |||
|
|||
context 'admin iam user' do | |||
it 'returns all users' do | |||
xit 'returns all users' do |
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.
If we don't need these, then we can remove them
end | ||
end | ||
end | ||
# RSpec.describe PolicyPolicy do |
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.
Once again if this is not needed, then we can delete it.
closing in favor of #170 |
Refer to the issue id if any. Include the link to the issue. E.g:
ApplicationPolicy should make use of the actions assigned to a user
Describe the changes made. What does this PR changes that might be critical. If any critical decisions have been made, make sure you explain the rationale for these decisions.
Introducing new (almost) concept of Policy Actions represented by
Action
model on IAM service.Action
model has these attributes:name
represented action name and could benew
,create
,index
,show
,destroy
,edit
,update
or*
for wildcard. Action name points to actual controller action so if you have a custom one likepublish
you can create policy action with this name.effect
—allow
ordeny
. Everything is denied.deny
effect more valuable than any amount ofallow
effects for the resource.target_resource
— resource represented by urn (including wildcarded ones):urn:ros:iam::222222222:credential
urn:ros:iam::222222222:*
urn:ros:*
segment
— scope of resources described by action. Points to actual scope on model. scope MUST accept user context as a parameter (user context is instance ofPolicyUser
object).There's predefined scopes
everything
that just alias forall
but with user context andowned
, that initially also pointed toall
and should be redefined if necessary, for exampleiam/user
model:Actions belongs to Policies and Policy has many Actions. IAM User has field
attached_actions
with all cached actions for this User, User Groups or User Roles. Combined and deduplicated*.How does the implementation addresses the problem
User can manage policies and actions now to set permissions for User, Group and Role.