-
Notifications
You must be signed in to change notification settings - Fork 60
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
[RHCLOUD-36627] Query only system roles for cross account request access and creation #1372
Open
lpichler
wants to merge
4
commits into
master
Choose a base branch
from
dont_query_cross_accoun_role_by_name
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.
+198
−31
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
927b7f5
Improve query to fetch related roles in access
lpichler 1ac6e54
Improve query to fetch only system roles in cross access request crea…
lpichler c29fb82
Leverage query in validation to store role assigments for cross acces…
lpichler bd6e32e
WIP
lpichler 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 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 |
---|---|---|
|
@@ -42,7 +42,7 @@ def setUp(self): | |
user.account = self.customer_data["account_id"] | ||
user.org_id = self.customer_data["org_id"] | ||
request.user = user | ||
public_tenant = Tenant.objects.get(tenant_name="public") | ||
self.public_tenant = Tenant.objects.get(tenant_name="public") | ||
|
||
self.access_data = { | ||
"permission": "app:*:*", | ||
|
@@ -151,8 +151,8 @@ def create_platform_default_resource(self): | |
) | ||
default_group.policies.add(default_policy) | ||
|
||
def create_role_and_permission(self, role_name, permission): | ||
role = Role.objects.create(name=role_name, tenant=self.tenant) | ||
def create_role_and_permission(self, role_name, permission, system=False): | ||
role = Role.objects.create(name=role_name, tenant=self.tenant, system=system) | ||
assigned_permission = Permission.objects.create(permission=permission, tenant=self.tenant) | ||
access = Access.objects.create(role=role, permission=assigned_permission, tenant=self.tenant) | ||
return role | ||
|
@@ -227,6 +227,53 @@ def test_get_access_to_return_unique_records(self): | |
response = client.get(url, **self.headers) | ||
self.assertEqual({"permission": "default:*:*", "resourceDefinitions": []}, response.data.get("data")[0]) | ||
|
||
def test_access_for_cross_account_principal_return_permissions_based_on_assigned_system_role(self): | ||
self.create_platform_default_resource() | ||
client = APIClient() | ||
url = "{}?application=".format(reverse("v1_management:access")) | ||
account_id = self.customer_data["account_id"] | ||
org_id = self.customer_data["org_id"] | ||
user_id = "123456" | ||
user_name = f"{account_id}-{user_id}" | ||
|
||
# create system role with access | ||
system_role = Role.objects.create(name="Test Role one", tenant=self.public_tenant, system=True) | ||
assigned_permission = Permission.objects.create( | ||
permission="test:assigned:permission1", tenant=self.public_tenant | ||
) | ||
Access.objects.create(role=system_role, permission=assigned_permission, tenant=self.tenant) | ||
|
||
cross_account_request = CrossAccountRequest.objects.create( | ||
target_account=account_id, | ||
user_id=user_id, | ||
target_org=org_id, | ||
end_date=timezone.now() + timedelta(10), | ||
status="approved", | ||
) | ||
cross_account_request.roles.add(system_role) | ||
|
||
# create non-system role with access and same name as system role | ||
role = Role.objects.create(name="Test Role one", tenant=self.tenant, system=False) | ||
assigned_permission = Permission.objects.create( | ||
permission="test:assigned:permission2", tenant=self.public_tenant | ||
) | ||
Access.objects.create(role=role, permission=assigned_permission, tenant=self.tenant) | ||
|
||
# Create cross_account principal and role, permission in the account | ||
user_data = {"username": user_name, "email": "[email protected]"} | ||
request_context = self._create_request_context(self.customer_data, user_data, is_org_admin=False) | ||
request = request_context["request"] | ||
self.test_headers = request.META | ||
Principal.objects.create(username=user_name, cross_account=True, tenant=self.tenant) | ||
|
||
response = client.get(url, **self.test_headers) | ||
|
||
# only assigned role permissions without platform default permission | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data.get("data")), 1) | ||
|
||
self.assertEqual(response.data.get("data")[0]["permission"], "test:assigned:permission1") | ||
|
||
def test_access_for_cross_account_principal_return_permissions_based_on_assigned_role(self): | ||
"""Test that the expected access for cross account principal return permissions based on assigned role.""" | ||
# setup default group/role | ||
|
@@ -240,7 +287,7 @@ def test_access_for_cross_account_principal_return_permissions_based_on_assigned | |
|
||
# setup cross account request, role and permission in public schema | ||
## This CAR will provide permission: "test:assigned:permission" | ||
role = self.create_role_and_permission("Test Role one", "test:assigned:permission1") | ||
role = self.create_role_and_permission("Test Role one", "test:assigned:permission1", True) | ||
cross_account_request = CrossAccountRequest.objects.create( | ||
target_account=account_id, | ||
user_id=user_id, | ||
|
@@ -250,7 +297,7 @@ def test_access_for_cross_account_principal_return_permissions_based_on_assigned | |
) | ||
cross_account_request.roles.add(role) | ||
## CAR below will provide permission: "app:*:*" | ||
role = self.create_role_and_permission("Test Role two", "test:assigned:permission2") | ||
role = self.create_role_and_permission("Test Role two", "test:assigned:permission2", True) | ||
cross_account_request = CrossAccountRequest.objects.create( | ||
target_account=account_id, | ||
user_id=user_id, | ||
|
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.
I was talking with @astrozzc about this during collab. I wonder if we could move the validation and transformation to the serializer, if that would break things?
Something like this?
And then you would take out the format / find role by display name stuff in the view and just leave it to the serializer.
This feels like what Django Rest Framework wants you to do, but dunno if it would change the existing behavior. Not tested!!
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.
Reference: https://www.django-rest-framework.org/api-guide/serializers/#field-level-validation
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.
Tried this would work: the to_internal_value will call the role serializer validation, if we don't transform the display_name (strings) to list of {"uuid": "xxx"} before that, it would raise validation error.
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 wonder, why does returning the value from the validate role method not work? It looked like it used that value for the validated data from the docs & code
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.
Yea, I spent sometime tracking it down, because it runs the validation of the RoleSerializer first before running the validate_roles of the CrossAccountDetailSerializer. The RoleSerializer requires it to be a dictionary like {"display_names": "xxx"} instead of list of display_names ["xxx"]