Skip to content

Commit

Permalink
Use ALLOW_LIST_FULL_ADMIN where practical (#15428)
Browse files Browse the repository at this point in the history
There are several places in the codebase where we return or
evaluate the dictionary `{"method": "*", "resource": "*"}`.

Place this in a privilege_constants file that has minimal
imports to avoid potential issues with circular imports and
to speed up usage in external applications and unit tests.
  • Loading branch information
anodos325 authored Jan 18, 2025
1 parent 7a96892 commit 041357d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/middlewared/middlewared/plugins/account_/privilege.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from middlewared.plugins.account import unixhash_is_valid
from middlewared.service import CallError, CRUDService, filter_list, private, ValidationErrors
from middlewared.service_exception import MatchNotFound
from middlewared.utils.privilege_constants import ALLOW_LIST_FULL_ADMIN, LocalAdminGroups
from middlewared.utils.privilege import (
LocalAdminGroups,
privilege_has_webui_access,
privileges_group_mapping
)
Expand Down Expand Up @@ -382,7 +382,7 @@ async def compose_privilege(self, privileges):
async def full_privilege(self):
return {
'roles': {'FULL_ADMIN'},
'allowlist': [{'method': '*', 'resource': '*'}],
'allowlist': [ALLOW_LIST_FULL_ADMIN.copy()],
'web_shell': True,
'webui_access': True,
}
Expand Down
3 changes: 2 additions & 1 deletion src/middlewared/middlewared/role.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict
from dataclasses import dataclass, field
from middlewared.utils.privilege_constants import ALLOW_LIST_FULL_ADMIN
from middlewared.utils.security import STIGType
import typing

Expand Down Expand Up @@ -351,7 +352,7 @@ def allowlist_for_role(self, role: str, enabled_stig: STIGType) -> typing.List[d
], [])

# Only non-stig FULL_ADMIN privilege can access REST endpoints
return [{"method": "*", "resource": "*"}]
return [ALLOW_LIST_FULL_ADMIN.copy()]

return sum([
self.methods.allowlists_for_roles[role] + self.events.allowlists_for_roles[role]
Expand Down
3 changes: 1 addition & 2 deletions src/middlewared/middlewared/utils/allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import re

from middlewared.api.current import HttpVerb

ALLOW_LIST_FULL_ADMIN = {'method': '*', 'resource': '*'}
from middlewared.utils.privilege_constants import ALLOW_LIST_FULL_ADMIN


class Allowlist:
Expand Down
5 changes: 0 additions & 5 deletions src/middlewared/middlewared/utils/privilege.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import enum
from middlewared.auth import TruenasNodeSessionManagerCredentials
from middlewared.role import ROLES


class LocalAdminGroups(enum.IntEnum):
BUILTIN_ADMINISTRATORS = 544


def privilege_has_webui_access(privilege: dict) -> bool:
"""
This method determines whether the specified privilege is sufficient
Expand Down
7 changes: 7 additions & 0 deletions src/middlewared/middlewared/utils/privilege_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import enum

ALLOW_LIST_FULL_ADMIN = {'method': '*', 'resource': '*'}


class LocalAdminGroups(enum.IntEnum):
BUILTIN_ADMINISTRATORS = 544

0 comments on commit 041357d

Please sign in to comment.