graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
"""The kernel's default, in-process AccessPort implementation.
|
|
2
|
+
|
|
3
|
+
Decisions are answered from persisted state through the entity repository -
|
|
4
|
+
the unchecked internal path the TDD requires (see
|
|
5
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md, Grant storage) - never through
|
|
6
|
+
EntityService's authorized read path, which would recurse into this adapter.
|
|
7
|
+
|
|
8
|
+
Containment is not an index this module maintains: `Entity.ancestor_ids` is
|
|
9
|
+
a repository-owned column, so "does a grant on some containing scope reach
|
|
10
|
+
this entity" is a plain predicate over entity fields.
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import logging
|
|
16
|
+
from typing import Iterable, Optional
|
|
17
|
+
|
|
18
|
+
from graphcore.auth.authz.access_port import AccessPort
|
|
19
|
+
from graphcore.domain.entity.Entity import Entity
|
|
20
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
21
|
+
from graphcore.domain.entity.group.Group import Group
|
|
22
|
+
from graphcore.errors import Forbidden
|
|
23
|
+
from graphcore.registry.registry import Registry
|
|
24
|
+
from graphcore.types.access import AccessChange, GrantInfo, ManageAccess, Perm, Role
|
|
25
|
+
from graphcore.auth.authz.defaults import ADMIN
|
|
26
|
+
from graphcore.types.identity import PUBLIC
|
|
27
|
+
from graphcore.types.entity_id import is_entity_id
|
|
28
|
+
from graphcore.types.identity import Principal
|
|
29
|
+
from graphcore.types.query import (
|
|
30
|
+
AllOf, AnyOf, ContainsAny, ContainsValue, Equal, InValues, Predicate,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
logger = logging.getLogger(__name__)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class DefaultAccessAdapter(AccessPort):
|
|
37
|
+
"""In-process adapter: grants and containment read through the entity
|
|
38
|
+
repository, with a cache for the group-membership reverse lookup (the
|
|
39
|
+
adapter's only hot-path cache)."""
|
|
40
|
+
|
|
41
|
+
def __init__(self, registry: Registry, repository, cache) -> None:
|
|
42
|
+
self.registry = registry
|
|
43
|
+
self.entity_directory = registry.entities
|
|
44
|
+
self.policy = registry.policy
|
|
45
|
+
self.repository = repository
|
|
46
|
+
self.cache = cache
|
|
47
|
+
self._frozen_group_type_ids: Optional[frozenset[str]] = None
|
|
48
|
+
|
|
49
|
+
def _group_type_ids(self) -> frozenset[str]:
|
|
50
|
+
"""Registered types that are `Group` or a subclass of it.
|
|
51
|
+
Cached once the directory is closed."""
|
|
52
|
+
if self._frozen_group_type_ids is not None:
|
|
53
|
+
return self._frozen_group_type_ids
|
|
54
|
+
type_ids = frozenset(
|
|
55
|
+
type_id for type_id, cls in self.entity_directory.get_all_entities().items()
|
|
56
|
+
if issubclass(cls, Group)
|
|
57
|
+
)
|
|
58
|
+
if self.entity_directory.closed:
|
|
59
|
+
self._frozen_group_type_ids = type_ids
|
|
60
|
+
return type_ids
|
|
61
|
+
|
|
62
|
+
# --- subject-set expansion -----------------------------------------
|
|
63
|
+
|
|
64
|
+
async def get_subject_group_ids(self, subject_id: str) -> frozenset[str]:
|
|
65
|
+
"""Group entity ids whose member_ids contain this subject.
|
|
66
|
+
|
|
67
|
+
Cached under ``user_groups:{subject_id}``. One level of membership
|
|
68
|
+
only - nested groups are out of v1 (see
|
|
69
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md, Groups).
|
|
70
|
+
"""
|
|
71
|
+
cache_key = self._subject_groups_cache_key(subject_id)
|
|
72
|
+
cached = self.cache.get(cache_key)
|
|
73
|
+
if cached is not None:
|
|
74
|
+
return frozenset(cached)
|
|
75
|
+
|
|
76
|
+
group_type_ids = self._group_type_ids()
|
|
77
|
+
if not group_type_ids:
|
|
78
|
+
return frozenset()
|
|
79
|
+
|
|
80
|
+
group_ids = sorted(await self.repository.list_ids(AllOf((
|
|
81
|
+
InValues('entity_type_id', group_type_ids),
|
|
82
|
+
ContainsValue('member_ids', subject_id),
|
|
83
|
+
))))
|
|
84
|
+
self.cache.set(cache_key, group_ids, timeout=300)
|
|
85
|
+
return frozenset(group_ids)
|
|
86
|
+
|
|
87
|
+
def _subject_groups_cache_key(self, subject_id: str) -> str:
|
|
88
|
+
"""Namespace membership caches by the frozen group-type snapshot."""
|
|
89
|
+
type_namespace = ','.join(sorted(self._group_type_ids()))
|
|
90
|
+
fingerprint = hashlib.sha256(type_namespace.encode()).hexdigest()[:16]
|
|
91
|
+
return f"user_groups:{fingerprint}:{subject_id}"
|
|
92
|
+
|
|
93
|
+
async def _get_subject_group_ids_safe(self, subject_id: str) -> frozenset[str]:
|
|
94
|
+
"""Swallow-on-error wrapper for the hot decision path - see
|
|
95
|
+
`get_subject_group_ids` for why errors must not be cached."""
|
|
96
|
+
try:
|
|
97
|
+
return await self.get_subject_group_ids(subject_id)
|
|
98
|
+
except Exception:
|
|
99
|
+
return frozenset()
|
|
100
|
+
|
|
101
|
+
def bust_subject_groups(self, subject_id: str) -> None:
|
|
102
|
+
"""Invalidate the cached group-membership set for a subject."""
|
|
103
|
+
self.cache.delete(self._subject_groups_cache_key(subject_id))
|
|
104
|
+
|
|
105
|
+
async def _subject_set(self, subject_id: str) -> frozenset[str]:
|
|
106
|
+
"""The subject id itself, PUBLIC, and the groups it belongs to."""
|
|
107
|
+
group_ids = await self._get_subject_group_ids_safe(subject_id)
|
|
108
|
+
return frozenset({subject_id, PUBLIC}) | group_ids
|
|
109
|
+
|
|
110
|
+
# --- scope resolution -------------------------------------------------
|
|
111
|
+
|
|
112
|
+
def _shareable_type_ids(self) -> set[str]:
|
|
113
|
+
return {
|
|
114
|
+
type_id for type_id, entity_cls in self.entity_directory.get_all_entities().items()
|
|
115
|
+
if self.policy.is_shareable(entity_cls)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
def _public_type_ids(self, perm: Perm) -> set[str]:
|
|
119
|
+
return {
|
|
120
|
+
type_id for type_id, entity_cls in self.entity_directory.get_all_entities().items()
|
|
121
|
+
if perm in self.policy.type_public_perms(entity_cls)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
def _resolve_role(self, entity_type_id: Optional[str], role_name: str) -> Optional[Role]:
|
|
125
|
+
entity_cls = self.entity_directory.get_entity_class(entity_type_id) if entity_type_id else None
|
|
126
|
+
for role in self.policy.available_roles(entity_cls):
|
|
127
|
+
if role.name == role_name:
|
|
128
|
+
return role
|
|
129
|
+
return None
|
|
130
|
+
|
|
131
|
+
async def _find(self, predicate: Predicate) -> dict:
|
|
132
|
+
"""Entities matching `predicate`, unauthorized - the adapter cannot
|
|
133
|
+
route reads through the authorized path it is itself answering."""
|
|
134
|
+
return await self.repository.load_many(await self.repository.list_ids(predicate))
|
|
135
|
+
|
|
136
|
+
async def _grants_for_subjects(self, subject_set) -> list[Grant]:
|
|
137
|
+
grant_type_id = self.entity_directory.get_entity_type_id(Grant)
|
|
138
|
+
grants = await self._find(AllOf((
|
|
139
|
+
Equal('entity_type_id', grant_type_id),
|
|
140
|
+
InValues('subject_id', subject_set),
|
|
141
|
+
)))
|
|
142
|
+
return list(grants.values())
|
|
143
|
+
|
|
144
|
+
async def _grants_on_resources(self, resource_ids) -> list[Grant]:
|
|
145
|
+
if not resource_ids:
|
|
146
|
+
return []
|
|
147
|
+
grant_type_id = self.entity_directory.get_entity_type_id(Grant)
|
|
148
|
+
grants = await self._find(AllOf((
|
|
149
|
+
Equal('entity_type_id', grant_type_id),
|
|
150
|
+
InValues('parent_id', resource_ids),
|
|
151
|
+
)))
|
|
152
|
+
return list(grants.values())
|
|
153
|
+
|
|
154
|
+
async def _held_by_perm(
|
|
155
|
+
self, principal_id: str, subject_set, perms,
|
|
156
|
+
) -> dict[Perm, set[str]]:
|
|
157
|
+
"""Per perm, the shareable scopes on which this subject set holds it:
|
|
158
|
+
owned ones (owner-implicit Admin) plus the resources of grants whose
|
|
159
|
+
role allows it. Grants and owned scopes are loaded once for every
|
|
160
|
+
perm."""
|
|
161
|
+
owned: set[str] = set()
|
|
162
|
+
if any(ADMIN.allows(perm) for perm in perms):
|
|
163
|
+
owned = await self.repository.list_ids(AllOf((
|
|
164
|
+
Equal('owner', principal_id),
|
|
165
|
+
InValues('entity_type_id', self._shareable_type_ids()),
|
|
166
|
+
)))
|
|
167
|
+
grants = await self._grants_for_subjects(subject_set)
|
|
168
|
+
resource_types = await self._scope_types({g.parent_id for g in grants if g.parent_id})
|
|
169
|
+
|
|
170
|
+
result = {}
|
|
171
|
+
for perm in perms:
|
|
172
|
+
held = set(owned) if ADMIN.allows(perm) else set()
|
|
173
|
+
for grant in grants:
|
|
174
|
+
role = self._resolve_role(resource_types.get(grant.parent_id), grant.role_name)
|
|
175
|
+
if role and role.allows(perm):
|
|
176
|
+
held.add(grant.parent_id)
|
|
177
|
+
result[perm] = held
|
|
178
|
+
return result
|
|
179
|
+
|
|
180
|
+
# --- decisions --------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
async def allows(self, principal: Principal, perm: Perm, entity_id: str) -> bool:
|
|
183
|
+
entity = await self.repository.load(str(entity_id))
|
|
184
|
+
if entity is None:
|
|
185
|
+
return False
|
|
186
|
+
|
|
187
|
+
entity_cls = self.entity_directory.get_entity_class(entity.entity_type_id)
|
|
188
|
+
|
|
189
|
+
# (a) type-wide public perms - checked first: it answers without the
|
|
190
|
+
# group-membership lookup `_subject_set` would otherwise do.
|
|
191
|
+
if perm in self.policy.type_public_perms(entity_cls):
|
|
192
|
+
return True
|
|
193
|
+
|
|
194
|
+
# (b) owner-implicit Admin - exactly the ADMIN role, no more: an
|
|
195
|
+
# `admin_implied = False` perm falls through to (c), where an owner
|
|
196
|
+
# holding that perm by grant still passes.
|
|
197
|
+
principal_id = principal.principal_entity_id
|
|
198
|
+
if entity.owner == principal_id and ADMIN.allows(perm):
|
|
199
|
+
return True
|
|
200
|
+
|
|
201
|
+
# (c) direct + inherited grants, union-only. An owned ancestor scope
|
|
202
|
+
# reaches this entity the same way a granted one does.
|
|
203
|
+
subject_set = await self._subject_set(principal_id)
|
|
204
|
+
held = (await self._held_by_perm(principal_id, subject_set, [perm]))[perm]
|
|
205
|
+
return entity.entity_id in held or bool(set(entity.ancestor_ids) & held)
|
|
206
|
+
|
|
207
|
+
async def read_filters(self, principal: Principal, perms: Iterable[Perm]) -> dict[Perm, Predicate]:
|
|
208
|
+
perms = list(dict.fromkeys(perms))
|
|
209
|
+
principal_id = principal.principal_entity_id
|
|
210
|
+
subject_set = await self._subject_set(principal_id)
|
|
211
|
+
held_by_perm = await self._held_by_perm(principal_id, subject_set, perms)
|
|
212
|
+
return {perm: self._read_clause(principal_id, perm, held_by_perm[perm]) for perm in perms}
|
|
213
|
+
|
|
214
|
+
def _read_clause(self, principal_id: str, perm: Perm, held: set[str]) -> Predicate:
|
|
215
|
+
clauses: list[Predicate] = [InValues('entity_type_id', self._public_type_ids(perm))]
|
|
216
|
+
if ADMIN.allows(perm):
|
|
217
|
+
clauses.append(Equal('owner', principal_id))
|
|
218
|
+
clauses.append(InValues('entity_id', held))
|
|
219
|
+
clauses.append(ContainsAny('ancestor_ids', held))
|
|
220
|
+
return AnyOf(tuple(clauses))
|
|
221
|
+
|
|
222
|
+
# --- grant interpretation ---------------------------------------------
|
|
223
|
+
|
|
224
|
+
async def available_roles(self, resource_id: str) -> list[Role]:
|
|
225
|
+
entity_type_id = await self._resource_type_id(resource_id)
|
|
226
|
+
entity_cls = self.entity_directory.get_entity_class(entity_type_id) if entity_type_id else None
|
|
227
|
+
return self.policy.available_roles(entity_cls)
|
|
228
|
+
|
|
229
|
+
async def list_grants(self, principal: Principal, resource_id: str) -> list[GrantInfo]:
|
|
230
|
+
"""Direct grants on `resource_id`, grants inherited from ancestor
|
|
231
|
+
scopes, and the resource's owner (implicit Admin). Type-policy
|
|
232
|
+
(public_perms) entries aren't discrete grants and are not listed
|
|
233
|
+
here; callers read `type_public_perms` separately if needed."""
|
|
234
|
+
resource = await self.repository.load(str(resource_id))
|
|
235
|
+
if resource is None:
|
|
236
|
+
return []
|
|
237
|
+
|
|
238
|
+
scope_types = await self._scope_types(set(resource.ancestor_ids))
|
|
239
|
+
shareable_types = self._shareable_type_ids()
|
|
240
|
+
cover = {resource.entity_id} | {
|
|
241
|
+
scope_id for scope_id, type_id in scope_types.items() if type_id in shareable_types
|
|
242
|
+
}
|
|
243
|
+
scope_types[resource.entity_id] = resource.entity_type_id
|
|
244
|
+
|
|
245
|
+
infos: list[GrantInfo] = []
|
|
246
|
+
if resource.owner:
|
|
247
|
+
infos.append(GrantInfo(
|
|
248
|
+
subject_id=resource.owner, role=ADMIN, provenance='owner',
|
|
249
|
+
resource_id=resource.entity_id,
|
|
250
|
+
))
|
|
251
|
+
|
|
252
|
+
for grant in await self._grants_on_resources(cover):
|
|
253
|
+
role = self._resolve_role(scope_types.get(grant.parent_id), grant.role_name)
|
|
254
|
+
if not role:
|
|
255
|
+
continue
|
|
256
|
+
provenance = 'direct' if grant.parent_id == resource.entity_id else f'inherited-from-{grant.parent_id}'
|
|
257
|
+
infos.append(GrantInfo(
|
|
258
|
+
subject_id=grant.subject_id, role=role, provenance=provenance,
|
|
259
|
+
grant_entity_id=grant.entity_id, resource_id=grant.parent_id,
|
|
260
|
+
))
|
|
261
|
+
return infos
|
|
262
|
+
|
|
263
|
+
async def _scope_types(self, scope_ids) -> dict:
|
|
264
|
+
entities = await self.repository.load_many(scope_ids)
|
|
265
|
+
return {eid: entity.entity_type_id for eid, entity in entities.items()}
|
|
266
|
+
|
|
267
|
+
# --- sync support -----------------------------------------------------
|
|
268
|
+
|
|
269
|
+
async def granted_resources(self, principal: Principal, *, include_public: bool = True) -> set[str]:
|
|
270
|
+
"""Resource ids granted to this principal directly or via one level
|
|
271
|
+
of group membership - the broadcaster's per-connection subscription
|
|
272
|
+
set, so a connecting principal joins every scope channel they may
|
|
273
|
+
read updates from."""
|
|
274
|
+
subject_set = await self._subject_set(principal.principal_entity_id)
|
|
275
|
+
if not include_public:
|
|
276
|
+
subject_set = subject_set - {PUBLIC}
|
|
277
|
+
return {
|
|
278
|
+
grant.parent_id for grant in await self._grants_for_subjects(subject_set)
|
|
279
|
+
if grant.parent_id
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async def owner_principals(self, resource_ids: set[str]) -> set[str]:
|
|
283
|
+
if not resource_ids:
|
|
284
|
+
return set()
|
|
285
|
+
entities = await self.repository.load_many(resource_ids)
|
|
286
|
+
return {entity.owner for entity in entities.values() if entity.owner}
|
|
287
|
+
|
|
288
|
+
async def scopes(self, entity_id: str, ancestor_ids) -> set[str]:
|
|
289
|
+
"""entity_id plus every shareable ancestor - the scopes whose grants
|
|
290
|
+
cover it. Takes the ancestry from the caller rather than re-reading
|
|
291
|
+
the row: the broadcaster publishing a just-committed delete has only
|
|
292
|
+
a `DeletedEntity`."""
|
|
293
|
+
ancestor_ids = {str(scope_id) for scope_id in (ancestor_ids or ())}
|
|
294
|
+
shareable = await self.repository.list_ids(AllOf((
|
|
295
|
+
InValues('entity_id', ancestor_ids),
|
|
296
|
+
InValues('entity_type_id', self._shareable_type_ids()),
|
|
297
|
+
))) if ancestor_ids else set()
|
|
298
|
+
return {str(entity_id)} | shareable
|
|
299
|
+
|
|
300
|
+
async def holders(self, perm: Perm, scope_ids) -> tuple[set[str], bool]:
|
|
301
|
+
"""`(principal_ids, everyone)` holding `perm` on any of `scope_ids`:
|
|
302
|
+
their owners when owner-implicit Admin allows it, and the subjects of
|
|
303
|
+
grants on them whose role allows it - a group expanded one level to
|
|
304
|
+
its members, PUBLIC as `everyone`."""
|
|
305
|
+
scope_ids = {str(scope_id) for scope_id in scope_ids}
|
|
306
|
+
if not scope_ids:
|
|
307
|
+
return set(), False
|
|
308
|
+
scopes = await self.repository.load_many(scope_ids)
|
|
309
|
+
principal_ids: set[str] = set()
|
|
310
|
+
if ADMIN.allows(perm):
|
|
311
|
+
principal_ids |= {scope.owner for scope in scopes.values() if scope.owner}
|
|
312
|
+
|
|
313
|
+
subject_ids = set()
|
|
314
|
+
for grant in await self._grants_on_resources(scope_ids):
|
|
315
|
+
scope = scopes.get(grant.parent_id)
|
|
316
|
+
role = self._resolve_role(scope.entity_type_id if scope else None, grant.role_name)
|
|
317
|
+
if role and role.allows(perm):
|
|
318
|
+
subject_ids.add(grant.subject_id)
|
|
319
|
+
if PUBLIC in subject_ids:
|
|
320
|
+
return principal_ids, True
|
|
321
|
+
|
|
322
|
+
subjects = await self.repository.load_many(subject_ids)
|
|
323
|
+
for subject_id in subject_ids:
|
|
324
|
+
subject = subjects.get(subject_id)
|
|
325
|
+
if isinstance(subject, Group):
|
|
326
|
+
principal_ids |= set(subject.member_ids or ())
|
|
327
|
+
elif subject is not None or not is_entity_id(subject_id):
|
|
328
|
+
principal_ids.add(subject_id)
|
|
329
|
+
return principal_ids, False
|
|
330
|
+
|
|
331
|
+
async def _has_access_manager(self, resource_id, *, grant_overrides=None, group_overrides=None) -> bool:
|
|
332
|
+
"""Whether any principal could manage access to the resource if the
|
|
333
|
+
given grants took these roles (None: removed) and the given groups
|
|
334
|
+
had these rosters (None: removed)."""
|
|
335
|
+
grant_overrides = grant_overrides or {}
|
|
336
|
+
group_overrides = group_overrides or {}
|
|
337
|
+
resource = await self.repository.load(str(resource_id))
|
|
338
|
+
if resource is None or resource.owner:
|
|
339
|
+
return True
|
|
340
|
+
|
|
341
|
+
ancestors = await self.repository.load_many(set(resource.ancestor_ids))
|
|
342
|
+
shareable_types = self._shareable_type_ids()
|
|
343
|
+
shareable = {aid for aid, ancestor in ancestors.items() if ancestor.entity_type_id in shareable_types}
|
|
344
|
+
if any(ancestors[aid].owner for aid in shareable):
|
|
345
|
+
return True
|
|
346
|
+
|
|
347
|
+
scope_types = {aid: ancestor.entity_type_id for aid, ancestor in ancestors.items()}
|
|
348
|
+
scope_types[resource.entity_id] = resource.entity_type_id
|
|
349
|
+
for grant in await self._grants_on_resources({resource.entity_id} | shareable):
|
|
350
|
+
role_name = grant_overrides.get(grant.entity_id, grant.role_name)
|
|
351
|
+
role = self._resolve_role(scope_types.get(grant.parent_id), role_name) if role_name else None
|
|
352
|
+
if role and role.allows(ManageAccess()) and await self._subject_can_act(grant.subject_id, group_overrides):
|
|
353
|
+
return True
|
|
354
|
+
return False
|
|
355
|
+
|
|
356
|
+
async def _subject_can_act(self, subject_id: str, group_overrides: dict) -> bool:
|
|
357
|
+
"""Whether a grant's subject resolves to at least one principal."""
|
|
358
|
+
if subject_id == PUBLIC:
|
|
359
|
+
return True
|
|
360
|
+
if subject_id in group_overrides:
|
|
361
|
+
return bool(group_overrides[subject_id])
|
|
362
|
+
subject = await self.repository.load(str(subject_id))
|
|
363
|
+
if subject is None:
|
|
364
|
+
# A bare id that is not an entity identifier names a principal
|
|
365
|
+
# directly; a missing entity (a deleted group) resolves to nobody.
|
|
366
|
+
return not is_entity_id(subject_id)
|
|
367
|
+
if isinstance(subject, Group):
|
|
368
|
+
return bool(subject.member_ids)
|
|
369
|
+
return True
|
|
370
|
+
|
|
371
|
+
async def covers(self, subject_id: str, principal_ids: list[str]) -> set[str]:
|
|
372
|
+
"""Which of `principal_ids` the subject resolves to: itself, or (for
|
|
373
|
+
a group subject) its current members - one level, matching the
|
|
374
|
+
adapter's subject-set expansion elsewhere. PUBLIC resolves to every
|
|
375
|
+
principal, mirroring `_subject_set`'s implicit PUBLIC membership -
|
|
376
|
+
without this, a live grant/revoke to PUBLIC would reach no connected
|
|
377
|
+
client (only a reconnect, via `granted_resources`, would see it)."""
|
|
378
|
+
principal_id_set = set(principal_ids)
|
|
379
|
+
if subject_id == PUBLIC:
|
|
380
|
+
return principal_id_set
|
|
381
|
+
matched = {pid for pid in principal_id_set if pid == subject_id}
|
|
382
|
+
|
|
383
|
+
subject = await self.repository.load(str(subject_id))
|
|
384
|
+
if isinstance(subject, Group):
|
|
385
|
+
matched |= principal_id_set & set(subject.member_ids or [])
|
|
386
|
+
return matched
|
|
387
|
+
|
|
388
|
+
# --- the lockout rule ---------------------------------------------------
|
|
389
|
+
|
|
390
|
+
async def check_save(self, entity: Entity, existing: Entity) -> None:
|
|
391
|
+
"""A changed grant or a shrinking group roster."""
|
|
392
|
+
if isinstance(entity, Grant):
|
|
393
|
+
unchanged = entity.subject_id == existing.subject_id and entity.parent_id == existing.parent_id
|
|
394
|
+
if unchanged and entity.role_name == existing.role_name:
|
|
395
|
+
return
|
|
396
|
+
override = {entity.entity_id: entity.role_name if unchanged else None}
|
|
397
|
+
await self._require_access_manager({existing.parent_id}, grant_overrides=override)
|
|
398
|
+
elif isinstance(entity, Group):
|
|
399
|
+
if set(existing.member_ids) - set(entity.member_ids):
|
|
400
|
+
resources = await self._resources_granted_to({entity.entity_id})
|
|
401
|
+
await self._require_access_manager(
|
|
402
|
+
resources, group_overrides={entity.entity_id: list(entity.member_ids)},
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
async def check_delete(self, entity_ids: set[str]) -> None:
|
|
406
|
+
"""Deleting grants and groups; resources deleted with them are exempt."""
|
|
407
|
+
doomed = await self._load_by_type(entity_ids, Grant, Group)
|
|
408
|
+
grant_ids = {e.entity_id for e in doomed if isinstance(e, Grant)}
|
|
409
|
+
group_ids = {e.entity_id for e in doomed if isinstance(e, Group)}
|
|
410
|
+
if not grant_ids and not group_ids:
|
|
411
|
+
return
|
|
412
|
+
resources = {e.parent_id for e in doomed if isinstance(e, Grant)}
|
|
413
|
+
resources |= await self._resources_granted_to(group_ids)
|
|
414
|
+
await self._require_access_manager(
|
|
415
|
+
resources - set(entity_ids),
|
|
416
|
+
grant_overrides={grant_id: None for grant_id in grant_ids},
|
|
417
|
+
group_overrides={group_id: None for group_id in group_ids},
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
async def _resources_granted_to(self, subject_ids: set) -> set:
|
|
421
|
+
if not subject_ids:
|
|
422
|
+
return set()
|
|
423
|
+
return {grant.parent_id for grant in await self._grants_for_subjects(subject_ids) if grant.parent_id}
|
|
424
|
+
|
|
425
|
+
async def _require_access_manager(self, resource_ids, **overrides) -> None:
|
|
426
|
+
for resource_id in resource_ids:
|
|
427
|
+
if not await self._has_access_manager(resource_id, **overrides):
|
|
428
|
+
raise Forbidden(
|
|
429
|
+
f"Refused: afterwards nobody could manage access to {resource_id}. "
|
|
430
|
+
"Give someone else a role that includes managing access first."
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
async def _load_by_type(self, entity_ids, *entity_classes) -> list:
|
|
434
|
+
"""Hydrate just the entities of these types (subclasses included)
|
|
435
|
+
among `entity_ids`."""
|
|
436
|
+
if not entity_ids:
|
|
437
|
+
return []
|
|
438
|
+
type_ids = {
|
|
439
|
+
type_id for type_id, cls in self.entity_directory.get_all_entities().items()
|
|
440
|
+
if issubclass(cls, entity_classes)
|
|
441
|
+
}
|
|
442
|
+
entities = await self._find(AllOf((
|
|
443
|
+
InValues('entity_id', {str(eid) for eid in entity_ids}),
|
|
444
|
+
InValues('entity_type_id', type_ids),
|
|
445
|
+
)))
|
|
446
|
+
return list(entities.values())
|
|
447
|
+
|
|
448
|
+
async def _resource_type_id(self, resource_id: str) -> Optional[str]:
|
|
449
|
+
"""A single entity's `entity_type_id`. Used to resolve a grant's role
|
|
450
|
+
name to a `Role`, and to check whether a subject id names a group.
|
|
451
|
+
Not every subject id names a persisted entity (PUBLIC, a bare user
|
|
452
|
+
id in tests) - those simply resolve to None."""
|
|
453
|
+
entity = await self.repository.load(str(resource_id))
|
|
454
|
+
return entity.entity_type_id if entity else None
|
|
455
|
+
|
|
456
|
+
# --- entity-write notification ----------------------------------------
|
|
457
|
+
|
|
458
|
+
def _bust_symmetric_difference(
|
|
459
|
+
self, previous_ids: list[str] | None, new_ids: list[str] | None
|
|
460
|
+
) -> None:
|
|
461
|
+
for subject_id in set(previous_ids or []) ^ set(new_ids or []):
|
|
462
|
+
self.bust_subject_groups(subject_id)
|
|
463
|
+
|
|
464
|
+
async def _grants_by_subject(self, subject_id: str) -> list[Grant]:
|
|
465
|
+
return await self._grants_for_subjects({subject_id})
|
|
466
|
+
|
|
467
|
+
async def _member_access_changes(
|
|
468
|
+
self, group_id: str, member_ids: set[str], *, removed: bool,
|
|
469
|
+
) -> list[AccessChange]:
|
|
470
|
+
"""One synthetic `AccessChange` per (resource the group holds a
|
|
471
|
+
grant on, member): revoke-shaped for a removed member, fresh-grant-
|
|
472
|
+
shaped for an added one, so the broadcaster leaves or joins their
|
|
473
|
+
connections live. A removed member who still reads the resource
|
|
474
|
+
another way is the broadcaster's to skip."""
|
|
475
|
+
if not member_ids:
|
|
476
|
+
return []
|
|
477
|
+
|
|
478
|
+
grants = await self._grants_by_subject(group_id)
|
|
479
|
+
scope_types = await self._scope_types({g.parent_id for g in grants if g.parent_id})
|
|
480
|
+
changes: list[AccessChange] = []
|
|
481
|
+
for grant in grants:
|
|
482
|
+
resource_type_id = scope_types.get(grant.parent_id)
|
|
483
|
+
role = self._resolve_role(resource_type_id, grant.role_name)
|
|
484
|
+
if role is None:
|
|
485
|
+
continue
|
|
486
|
+
for member_id in member_ids:
|
|
487
|
+
changes.append(AccessChange(
|
|
488
|
+
resource_id=grant.parent_id, subject_id=member_id,
|
|
489
|
+
old_role=role if removed else None, new_role=None if removed else role,
|
|
490
|
+
resource_type_id=resource_type_id,
|
|
491
|
+
))
|
|
492
|
+
return changes
|
|
493
|
+
|
|
494
|
+
async def entity_saved(
|
|
495
|
+
self, entity: Entity, previous: Optional[Entity]
|
|
496
|
+
) -> Optional[AccessChange] | list[AccessChange]:
|
|
497
|
+
"""React to a committed write. Most entities no-op; `Group`
|
|
498
|
+
busts the changed members' cached subject sets so the very next
|
|
499
|
+
decision recomputes their groups, and returns one revoke-shaped
|
|
500
|
+
`AccessChange` per removed member plus one fresh-grant-shaped
|
|
501
|
+
`AccessChange` per added member, so the broadcaster leaves/joins
|
|
502
|
+
their connections on any scope the group's grants covered, in both
|
|
503
|
+
directions; `Grant` returns the single `AccessChange` the
|
|
504
|
+
broadcaster routes on."""
|
|
505
|
+
if isinstance(entity, Group):
|
|
506
|
+
previous_member_ids = getattr(previous, 'member_ids', None) if previous else None
|
|
507
|
+
new_member_ids = entity.member_ids
|
|
508
|
+
self._bust_symmetric_difference(previous_member_ids, new_member_ids)
|
|
509
|
+
removed_ids = set(previous_member_ids or []) - set(new_member_ids or [])
|
|
510
|
+
added_ids = set(new_member_ids or []) - set(previous_member_ids or [])
|
|
511
|
+
return (
|
|
512
|
+
await self._member_access_changes(entity.entity_id, removed_ids, removed=True)
|
|
513
|
+
+ await self._member_access_changes(entity.entity_id, added_ids, removed=False)
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
if isinstance(entity, Grant):
|
|
517
|
+
resource_type_id = await self._resource_type_id(entity.parent_id)
|
|
518
|
+
old_role = (
|
|
519
|
+
self._resolve_role(resource_type_id, previous.role_name)
|
|
520
|
+
if previous is not None else None
|
|
521
|
+
)
|
|
522
|
+
new_role = self._resolve_role(resource_type_id, entity.role_name)
|
|
523
|
+
return AccessChange(
|
|
524
|
+
resource_id=entity.parent_id,
|
|
525
|
+
subject_id=entity.subject_id,
|
|
526
|
+
old_role=old_role,
|
|
527
|
+
new_role=new_role,
|
|
528
|
+
resource_type_id=resource_type_id,
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
return None
|
|
532
|
+
|
|
533
|
+
async def prepare_delete(self, entity_ids: list[str]) -> list[Entity]:
|
|
534
|
+
"""The grants and groups among `entity_ids`, hydrated while their
|
|
535
|
+
rows still exist."""
|
|
536
|
+
return await self._load_by_type(entity_ids, Grant, Group)
|
|
537
|
+
|
|
538
|
+
async def entities_deleted(self, plan: list[Entity]) -> list[AccessChange]:
|
|
539
|
+
"""React to a committed delete, from each entity's last known state.
|
|
540
|
+
A group's members lose it from their cached subject sets; a grant
|
|
541
|
+
yields its revoke."""
|
|
542
|
+
changes: list[AccessChange] = []
|
|
543
|
+
for entity in plan:
|
|
544
|
+
if isinstance(entity, Group):
|
|
545
|
+
self._bust_symmetric_difference(entity.member_ids, [])
|
|
546
|
+
elif isinstance(entity, Grant):
|
|
547
|
+
resource_type_id = await self._resource_type_id(entity.parent_id)
|
|
548
|
+
changes.append(AccessChange(
|
|
549
|
+
resource_id=entity.parent_id,
|
|
550
|
+
subject_id=entity.subject_id,
|
|
551
|
+
old_role=self._resolve_role(resource_type_id, entity.role_name),
|
|
552
|
+
new_role=None,
|
|
553
|
+
resource_type_id=resource_type_id,
|
|
554
|
+
))
|
|
555
|
+
return changes
|
|
556
|
+
|
|
File without changes
|