Skip to content

Commit

Permalink
Add option to disable creating, update and deleting users and groups.
Browse files Browse the repository at this point in the history
Issue: AAP-24300
  • Loading branch information
newswangerd committed Jun 4, 2024
1 parent 7e6b335 commit 8fd6f25
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGES/24300.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to disable creating, update and deleting users and groups.
29 changes: 16 additions & 13 deletions galaxy_ng/app/access_control/access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,22 @@ def require_requirements_yaml(self, request, view, action):
})
return True

def is_direct_shared_resource_management_disabled(self, request, view, action):
return not settings.DIRECT_SHARED_RESOURCE_MANAGEMENT_ENABLED

def user_is_superuser(self, request, view, action):
if getattr(self, "swagger_fake_view", False):
# If OpenAPI schema is requested, don't check for superuser
return False
user = view.get_object()
return user.is_superuser

def is_current_user(self, request, view, action):
if getattr(self, "swagger_fake_view", False):
# If OpenAPI schema is requested, don't check for current user
return False
return request.user == view.get_object()


class AIDenyIndexAccessPolicy(AccessPolicyBase):
NAME = "AIDenyIndexView"
Expand Down Expand Up @@ -613,19 +629,6 @@ class CollectionRemoteAccessPolicy(AccessPolicyBase):
class UserAccessPolicy(AccessPolicyBase):
NAME = "UserViewSet"

def user_is_superuser(self, request, view, action):
if getattr(self, "swagger_fake_view", False):
# If OpenAPI schema is requested, don't check for superuser
return False
user = view.get_object()
return user.is_superuser

def is_current_user(self, request, view, action):
if getattr(self, "swagger_fake_view", False):
# If OpenAPI schema is requested, don't check for current user
return False
return request.user == view.get_object()


class MyUserAccessPolicy(AccessPolicyBase):
NAME = "MyUserViewSet"
Expand Down
28 changes: 27 additions & 1 deletion galaxy_ng/app/access_control/statements/pulp.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from galaxy_ng.app.access_control.statements.standalone import (
_collection_statements as _galaxy_collection_statements,
_group_statements as _galaxy_group_statements,
_group_role_statements as _galaxy_group_role_statements
)

from galaxy_ng.app.access_control.statements.standalone import _user_statements

_collection_statements = {"statements": _galaxy_collection_statements}


_group_statements = {"statements": _galaxy_group_statements}


_group_role_statements = {"statements": _galaxy_group_role_statements}


_deny_all = {
"statements": [
{"principal": "*", "action": "*", "effect": "deny"},
Expand Down Expand Up @@ -565,8 +570,29 @@


PULP_CORE_VIEWSETS = {
"groups/roles": _group_statements,
"groups/roles": _group_role_statements,
"groups": _group_statements,
"groups/users": {"statements": [
# We didn't have an access policy here before 4.10. The default pulp access policy
# checks core.group permissions, rather than galaxy.group permissions, which isn't
# used in our system. The end result should be that only admins can make modifications
# on this endpoint. This should be changed to match the validation we use for the
# ui apis (https://github.com/ansible/galaxy_ng/blob/7e6b335326fd1d1f366e3c5dd81b3f6e
# 75da9e1e/galaxy_ng/app/api/ui/serializers/user.py#L62), but given that we're looking
# at adopting DAB RBAC, I'm going to leave this as is for now.
{
"action": "*",
"principal": "admin",
"effect": "allow"
},
{
"action": ["create", "destroy"],
"principal": "*",
"effect": "deny",
"condition": "is_direct_shared_resource_management_disabled"
},
]},
"users": {"statements": _user_statements},
"roles": {
"statements": [
{
Expand Down
124 changes: 76 additions & 48 deletions galaxy_ng/app/access_control/statements/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}
]

_group_statements = [
_group_role_statements = [
{
"action": ["list", "retrieve"],
"principal": "authenticated",
Expand All @@ -86,22 +86,87 @@
"action": "destroy",
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.delete_group"
"condition": [
"has_model_perms:galaxy.delete_group",
]
},
{
"action": "create",
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.add_group"
"condition": [
"has_model_perms:galaxy.add_group",
]
},
{
"action": ["update", "partial_update"],
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.update_group"
"condition": [
"has_model_perms:galaxy.update_group",
]
},
]

_group_statements = _group_role_statements + [
{
"action": ["create", "destroy", "update", "partial_update"],
"principal": "*",
"effect": "deny",
"condition": "is_direct_shared_resource_management_disabled"
},
]

_user_statements = [
{
"action": ["list"],
"principal": "authenticated",
"effect": "allow",
"condition": ["v3_can_view_users"],
},
{
"action": ["retrieve"],
"principal": "authenticated",
"effect": "allow",
"condition": ["v3_can_view_users"],
},
{
"action": "destroy",
"principal": "*",
"effect": "deny",
"condition": ["user_is_superuser"]
},
{
"action": "destroy",
"principal": "*",
"effect": "deny",
"condition": ["is_current_user"]
},
{
"action": "destroy",
"principal": "*",
"effect": "allow",
"condition": "has_model_perms:galaxy.delete_user"
},
{
"action": "create",
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.add_user"
},
{
"action": ["update", "partial_update"],
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.change_user"
},
{
"action": ["create", "destroy", "update", "partial_update"],
"principal": "*",
"effect": "deny",
"condition": "is_direct_shared_resource_management_disabled"
},
]
_deny_all = [
{
"principal": "*",
Expand Down Expand Up @@ -187,50 +252,7 @@
"condition": "has_model_perms:ansible.change_collectionremote"
}
],
'UserViewSet': [
{
"action": ["list"],
"principal": "authenticated",
"effect": "allow",
"condition": ["v3_can_view_users"],
},
{
"action": ["retrieve"],
"principal": "authenticated",
"effect": "allow",
"condition": ["v3_can_view_users"],
},
{
"action": "destroy",
"principal": "*",
"effect": "deny",
"condition": ["user_is_superuser"]
},
{
"action": "destroy",
"principal": "*",
"effect": "deny",
"condition": ["is_current_user"]
},
{
"action": "destroy",
"principal": "*",
"effect": "allow",
"condition": "has_model_perms:galaxy.delete_user"
},
{
"action": "create",
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.add_user"
},
{
"action": ["update", "partial_update"],
"principal": "authenticated",
"effect": "allow",
"condition": "has_model_perms:galaxy.change_user"
},
],
'UserViewSet': _user_statements,
'MyUserViewSet': [
{
"action": ["retrieve"],
Expand All @@ -244,6 +266,12 @@
"effect": "allow",
"condition": "is_current_user"
},
{
"action": ["create", "destroy", "update", "partial_update"],
"principal": "*",
"effect": "deny",
"condition": "is_direct_shared_resource_management_disabled"
},
],
# disable synclists for on prem installations
'SyncListViewSet': _deny_all,
Expand Down
5 changes: 4 additions & 1 deletion galaxy_ng/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

# Galaxy authentication classes are used to set REST_FRAMEWORK__DEFAULT_AUTHENTICATION_CLASSES
GALAXY_AUTHENTICATION_CLASSES = [
"galaxy_ng.app.auth.session.SessionAuthentication",
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
"rest_framework.authentication.BasicAuthentication",
"ansible_base.jwt_consumer.hub.auth.HubJWTAuth",
Expand Down Expand Up @@ -308,3 +308,6 @@

# WARNING: This setting is used in database migrations to create a default organization.
DEFAULT_ORGANIZATION_NAME = "Default"

# Disables editing and managing users and groups.
DIRECT_SHARED_RESOURCE_MANAGEMENT_ENABLED = True
Loading

0 comments on commit 8fd6f25

Please sign in to comment.