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,637 @@
|
|
|
1
|
+
"""Step 12: kernel end-to-end integration suite - see
|
|
2
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md and the plan's scenario matrix (A-H).
|
|
3
|
+
|
|
4
|
+
Exercises the fully assembled stack through the real front door - `GraphAPI`
|
|
5
|
+
+ the strategy executor + `EntityService` + the default adapter + the
|
|
6
|
+
broadcaster over the in-memory channel layer. The only writes that bypass a
|
|
7
|
+
strategy are fixtures (the initial owner-owned scope/article, and user/group
|
|
8
|
+
creation), exactly as `kernel_internal_context()` documents.
|
|
9
|
+
"""
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from unittest.mock import patch
|
|
12
|
+
|
|
13
|
+
from asgiref.sync import async_to_sync
|
|
14
|
+
from channels.testing import WebsocketCommunicator
|
|
15
|
+
from django.test import TransactionTestCase
|
|
16
|
+
|
|
17
|
+
from graphcore.types.access import Perm, Read, Role
|
|
18
|
+
from graphcore.auth.authz.defaults import DEFAULT_ROLES
|
|
19
|
+
from graphcore.types.identity import Principal
|
|
20
|
+
from graphcore.auth.authn.testing import TestIssuer
|
|
21
|
+
from graphcore.domain.entity.Entity import Entity
|
|
22
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
23
|
+
from graphcore.persistence.django.models import GrantModel
|
|
24
|
+
from graphcore.domain.entity.group.Group import Group
|
|
25
|
+
from graphcore.domain.entity.user.User import User
|
|
26
|
+
from graphcore.domain.strategy.BaseStrategy import (
|
|
27
|
+
CreateEntity,
|
|
28
|
+
DeleteEntity,
|
|
29
|
+
SetAttributes,
|
|
30
|
+
Strategy,
|
|
31
|
+
)
|
|
32
|
+
from graphcore.domain.strategy.GrantStrategies import ChangeGrantRole, RevokeGrant
|
|
33
|
+
from graphcore.domain.strategy.GroupStrategies import AddMember, ModifyGroup
|
|
34
|
+
from graphcore.errors import Forbidden
|
|
35
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
36
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
37
|
+
from graphcore.runtime import django_runtime
|
|
38
|
+
from graphcore.transport.consumers import GlobalEntityConsumer
|
|
39
|
+
import graphcore.transport.consumers as consumers
|
|
40
|
+
from graphcore.tests.runs import run
|
|
41
|
+
|
|
42
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class _Scope(Entity):
|
|
46
|
+
"""A shareable scope type, standing in for the TDD's DocumentHub."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _Doc(Entity):
|
|
50
|
+
"""A plain contained type with no grants of its own."""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
TEST_REGISTRY.entities.register_entity(_Scope, app_label='graphcore')
|
|
54
|
+
TEST_REGISTRY.policy.define(_Scope, shareable=True)
|
|
55
|
+
TEST_REGISTRY.entities.register_entity(_Doc, app_label='graphcore')
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class Publish(Perm):
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
PUBLISHER = Role('publisher', perms=frozenset({Read(), Publish()}))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class _Article(Entity):
|
|
67
|
+
"""An app-registered shareable type with its own role/perm (scenario F)."""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
TEST_REGISTRY.entities.register_entity(_Article, app_label='graphcore')
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class PublishArticle(Strategy):
|
|
74
|
+
"""Publish-gated mutation on `_Article` (scenario F)."""
|
|
75
|
+
strategy_description = "Publishes an article"
|
|
76
|
+
|
|
77
|
+
async def apply(self) -> tuple:
|
|
78
|
+
self.entity.set_attribute('published', True)
|
|
79
|
+
self.entity = await self.save_entity(self.entity)
|
|
80
|
+
return self.strategy_request, self.entity
|
|
81
|
+
|
|
82
|
+
@classmethod
|
|
83
|
+
def request_constructor(cls, target_entity_id):
|
|
84
|
+
return cls._build_request(
|
|
85
|
+
target_entity_id=target_entity_id, param_config={},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _RogueLaundering(Strategy):
|
|
90
|
+
"""Targets an entity it's legitimately allowed to touch, but tries to
|
|
91
|
+
persist an unrelated `Grant` loaded around the read check - the
|
|
92
|
+
laundering move the persistence check must block (scenario G). The
|
|
93
|
+
test hands it the grant, as kernel code it imported could."""
|
|
94
|
+
strategy_description = "Rogue: targets one entity, tries to persist another"
|
|
95
|
+
smuggled = None
|
|
96
|
+
|
|
97
|
+
async def apply(self) -> tuple:
|
|
98
|
+
grant = type(self).smuggled
|
|
99
|
+
grant.role_name = 'admin'
|
|
100
|
+
await self.save_entity(grant)
|
|
101
|
+
return self.strategy_request, self.entity
|
|
102
|
+
|
|
103
|
+
@classmethod
|
|
104
|
+
def request_constructor(cls, target_entity_id, grant_id):
|
|
105
|
+
return cls._build_request(
|
|
106
|
+
target_entity_id=target_entity_id, param_config={'grant_id': grant_id},
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class _RogueDetach(Strategy):
|
|
111
|
+
"""Targets a `Grant` directly and tries to detach it from its
|
|
112
|
+
resource by clearing `parent_id` - the structural protection this
|
|
113
|
+
refactor buys: `Grant`'s allowlist is the only thing that governs
|
|
114
|
+
a write to the grant row itself, so no other strategy may move or
|
|
115
|
+
orphan it (see docs/AUTHORIZATION_TECHNICAL_TDD.md, Grant storage)."""
|
|
116
|
+
strategy_description = "Rogue: tries to detach a grant from its resource"
|
|
117
|
+
|
|
118
|
+
async def apply(self) -> tuple:
|
|
119
|
+
grant: Grant = self.entity
|
|
120
|
+
grant.parent_id = None
|
|
121
|
+
self.entity = await self.save_entity(grant)
|
|
122
|
+
return self.strategy_request, self.entity
|
|
123
|
+
|
|
124
|
+
@classmethod
|
|
125
|
+
def request_constructor(cls, target_entity_id):
|
|
126
|
+
return cls._build_request(
|
|
127
|
+
target_entity_id=target_entity_id, param_config={},
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class _MisdeclaredReadOnly(Strategy):
|
|
132
|
+
"""Declares itself read-only, then persists anyway, so only the
|
|
133
|
+
chokepoint can stop it (scenario G)."""
|
|
134
|
+
strategy_description = "Misdeclared: read-only by declaration, mutating in fact"
|
|
135
|
+
read_only_strategy = True
|
|
136
|
+
|
|
137
|
+
async def apply(self) -> tuple:
|
|
138
|
+
self.entity.set_attribute('smuggled', True)
|
|
139
|
+
await self.session.save(self.entity)
|
|
140
|
+
return self.strategy_request, self.entity
|
|
141
|
+
|
|
142
|
+
@classmethod
|
|
143
|
+
def request_constructor(cls, target_entity_id):
|
|
144
|
+
return cls._build_request(
|
|
145
|
+
target_entity_id=target_entity_id, param_config={},
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class _ReadOnlyProbe(Strategy):
|
|
150
|
+
strategy_description = "Reads nothing, writes nothing"
|
|
151
|
+
read_only_strategy = True
|
|
152
|
+
|
|
153
|
+
async def apply(self) -> tuple:
|
|
154
|
+
return self.strategy_request, self.entity
|
|
155
|
+
|
|
156
|
+
@classmethod
|
|
157
|
+
def request_constructor(cls, target_entity_id):
|
|
158
|
+
return cls._build_request(
|
|
159
|
+
target_entity_id=target_entity_id, param_config={},
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
TEST_REGISTRY.strategies.register_strategy('Entity', PublishArticle)
|
|
164
|
+
TEST_REGISTRY.policy.define(
|
|
165
|
+
_Article, shareable=True, roles=[*DEFAULT_ROLES, PUBLISHER],
|
|
166
|
+
strategies={PublishArticle: Publish()},
|
|
167
|
+
)
|
|
168
|
+
TEST_REGISTRY.strategies.register_strategy('Entity', _RogueLaundering)
|
|
169
|
+
TEST_REGISTRY.strategies.register_strategy('Entity', _RogueDetach)
|
|
170
|
+
TEST_REGISTRY.strategies.register_strategy('Entity', _MisdeclaredReadOnly)
|
|
171
|
+
TEST_REGISTRY.strategies.register_strategy('Entity', _ReadOnlyProbe)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class AuthorizationIntegrationTestCase(TransactionTestCase):
|
|
175
|
+
"""Step 12 scenario matrix A-H."""
|
|
176
|
+
|
|
177
|
+
OWNER = 'integration-owner'
|
|
178
|
+
|
|
179
|
+
def setUp(self):
|
|
180
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
181
|
+
self.api = self.runtime.graph_api
|
|
182
|
+
self._get_runtime_patch = patch.object(consumers, 'get_runtime', lambda: self.runtime)
|
|
183
|
+
self._get_runtime_patch.start()
|
|
184
|
+
self.owner_principal = Principal(principal_entity_id=self.OWNER)
|
|
185
|
+
self.issuer = TestIssuer()
|
|
186
|
+
self.enterContext(self.issuer.trusted())
|
|
187
|
+
self._subjects = {}
|
|
188
|
+
|
|
189
|
+
def tearDown(self):
|
|
190
|
+
self._get_runtime_patch.stop()
|
|
191
|
+
|
|
192
|
+
async def _create_user(self, username: str) -> str:
|
|
193
|
+
user = self.runtime.entity_service.create(User)
|
|
194
|
+
user.username = username
|
|
195
|
+
self.issuer.bind(user)
|
|
196
|
+
with kernel_internal_context():
|
|
197
|
+
user = await self.api.entity_service.save_entity(user, self.owner_principal)
|
|
198
|
+
self._subjects[user.entity_id] = user.subject
|
|
199
|
+
return user.entity_id
|
|
200
|
+
|
|
201
|
+
async def _create_scope(self, entity_cls=_Scope):
|
|
202
|
+
scope = self.api.entity_service.create(entity_cls)
|
|
203
|
+
scope.owner = self.OWNER
|
|
204
|
+
with kernel_internal_context():
|
|
205
|
+
return await self.api.entity_service.save_entity(scope, self.owner_principal)
|
|
206
|
+
|
|
207
|
+
async def _create_child(self, parent_id: str, principal: Principal, entity_cls=_Doc) -> str:
|
|
208
|
+
request = CreateEntity.request_constructor(
|
|
209
|
+
target_entity_id=parent_id,
|
|
210
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(entity_cls),
|
|
211
|
+
)
|
|
212
|
+
request, _ = await run(self.api.strategy_executor, request, principal=principal)
|
|
213
|
+
return request.ret_val['child_entity']
|
|
214
|
+
|
|
215
|
+
async def _connect(self, principal_entity_id: str) -> WebsocketCommunicator:
|
|
216
|
+
token = self.issuer.token(self._subjects[principal_entity_id])
|
|
217
|
+
communicator = WebsocketCommunicator(GlobalEntityConsumer.as_asgi(), "/ws/entities/", subprotocols=["graphcore.bearer", token])
|
|
218
|
+
connected, _ = await communicator.connect()
|
|
219
|
+
assert connected
|
|
220
|
+
await communicator.receive_json_from() # connected message
|
|
221
|
+
return communicator
|
|
222
|
+
|
|
223
|
+
async def _receive_until(self, communicator: WebsocketCommunicator, matches, max_messages: int = 5):
|
|
224
|
+
for _ in range(max_messages):
|
|
225
|
+
message = await communicator.receive_json_from()
|
|
226
|
+
if matches(message):
|
|
227
|
+
return message
|
|
228
|
+
raise AssertionError(f"no matching message received within {max_messages} messages")
|
|
229
|
+
|
|
230
|
+
# --- A: share lifecycle ------------------------------------------------
|
|
231
|
+
|
|
232
|
+
@async_to_sync
|
|
233
|
+
async def test_a_share_lifecycle(self):
|
|
234
|
+
scope = await self._create_scope()
|
|
235
|
+
user2 = await self._create_user('a-user2')
|
|
236
|
+
user2_principal = Principal(principal_entity_id=user2)
|
|
237
|
+
child_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
238
|
+
|
|
239
|
+
await self.api.grant(self.owner_principal, scope.entity_id, user2, 'viewer')
|
|
240
|
+
|
|
241
|
+
self.assertIsInstance(await self.api.get_entity(user2_principal, scope.entity_id), _Scope)
|
|
242
|
+
self.assertIsInstance(await self.api.get_entity(user2_principal, child_id), _Doc)
|
|
243
|
+
|
|
244
|
+
write_req = SetAttributes.request_constructor(target_entity_id=child_id, attribute_map={'x': 1})
|
|
245
|
+
with self.assertRaises(Forbidden):
|
|
246
|
+
await run(self.api.strategy_executor, write_req, principal=user2_principal)
|
|
247
|
+
|
|
248
|
+
# A viewer cannot change the grants on their own scope (needs ManageAccess).
|
|
249
|
+
with self.assertRaises(Forbidden):
|
|
250
|
+
await self.api.grant(user2_principal, scope.entity_id, user2, 'admin')
|
|
251
|
+
|
|
252
|
+
# Replace semantics: re-granting upgrades the same row rather than adding a second.
|
|
253
|
+
await self.api.grant(self.owner_principal, scope.entity_id, user2, 'editor')
|
|
254
|
+
grants = await self.api.list_grants(self.owner_principal, scope.entity_id)
|
|
255
|
+
user2_grants = [g for g in grants if g.subject_id == user2]
|
|
256
|
+
self.assertEqual(len(user2_grants), 1)
|
|
257
|
+
self.assertEqual(user2_grants[0].role.name, 'editor')
|
|
258
|
+
|
|
259
|
+
write_req2 = SetAttributes.request_constructor(target_entity_id=child_id, attribute_map={'x': 1})
|
|
260
|
+
result, _ = await run(self.api.strategy_executor, write_req2, principal=user2_principal)
|
|
261
|
+
self.assertIsNotNone(result)
|
|
262
|
+
|
|
263
|
+
await self.api.revoke(self.owner_principal, scope.entity_id, user2)
|
|
264
|
+
|
|
265
|
+
self.assertEqual((await self.api.get_entity(user2_principal, scope.entity_id)).cause, 'forbidden')
|
|
266
|
+
self.assertEqual((await self.api.get_entity(user2_principal, child_id)).cause, 'forbidden')
|
|
267
|
+
|
|
268
|
+
# --- B: inheritance over time -------------------------------------------
|
|
269
|
+
|
|
270
|
+
@async_to_sync
|
|
271
|
+
async def test_b_inheritance_over_time(self):
|
|
272
|
+
scope = await self._create_scope()
|
|
273
|
+
user2 = await self._create_user('b-user2')
|
|
274
|
+
user2_principal = Principal(principal_entity_id=user2)
|
|
275
|
+
|
|
276
|
+
await self.api.grant(self.owner_principal, scope.entity_id, user2, 'viewer')
|
|
277
|
+
|
|
278
|
+
child_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
279
|
+
self.assertIsInstance(await self.api.get_entity(user2_principal, child_id), _Doc)
|
|
280
|
+
|
|
281
|
+
grandchild_id = await self._create_child(child_id, self.owner_principal)
|
|
282
|
+
self.assertIsInstance(await self.api.get_entity(user2_principal, grandchild_id), _Doc)
|
|
283
|
+
|
|
284
|
+
# --- B2: reparenting moves effective access immediately ------------------
|
|
285
|
+
|
|
286
|
+
@async_to_sync
|
|
287
|
+
async def test_b2_reparent_moves_effective_access(self):
|
|
288
|
+
scope = await self._create_scope()
|
|
289
|
+
scope2 = await self._create_scope()
|
|
290
|
+
original_grantee = await self._create_user('b2-original')
|
|
291
|
+
new_grantee = await self._create_user('b2-new')
|
|
292
|
+
original_principal = Principal(principal_entity_id=original_grantee)
|
|
293
|
+
new_principal = Principal(principal_entity_id=new_grantee)
|
|
294
|
+
|
|
295
|
+
await self.api.grant(self.owner_principal, scope.entity_id, original_grantee, 'viewer')
|
|
296
|
+
await self.api.grant(self.owner_principal, scope2.entity_id, new_grantee, 'viewer')
|
|
297
|
+
|
|
298
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
299
|
+
self.assertIsInstance(await self.api.get_entity(original_principal, doc_id), _Doc)
|
|
300
|
+
self.assertEqual((await self.api.get_entity(new_principal, doc_id)).cause, 'forbidden')
|
|
301
|
+
|
|
302
|
+
# Reparent doc from scope to scope2 - a direct save under
|
|
303
|
+
# kernel-internal context, the same pattern this suite's other
|
|
304
|
+
# fixtures use to mutate entity state outside a strategy. No
|
|
305
|
+
# explicit reindex call: the entity_saved hook does it.
|
|
306
|
+
doc = await self.api.entity_service.load_from_db(doc_id)
|
|
307
|
+
doc.parent_id = scope2.entity_id
|
|
308
|
+
with kernel_internal_context():
|
|
309
|
+
await self.api.entity_service.save_entity(doc, self.owner_principal)
|
|
310
|
+
|
|
311
|
+
self.assertEqual((await self.api.get_entity(original_principal, doc_id)).cause, 'forbidden')
|
|
312
|
+
self.assertIsInstance(await self.api.get_entity(new_principal, doc_id), _Doc)
|
|
313
|
+
|
|
314
|
+
# --- C: groups -----------------------------------------------------------
|
|
315
|
+
|
|
316
|
+
@async_to_sync
|
|
317
|
+
async def test_c_groups(self):
|
|
318
|
+
scope = await self._create_scope()
|
|
319
|
+
member = await self._create_user('c-member')
|
|
320
|
+
roster_only = await self._create_user('c-roster-only')
|
|
321
|
+
group_editor = await self._create_user('c-group-editor')
|
|
322
|
+
member_principal = Principal(principal_entity_id=member)
|
|
323
|
+
roster_only_principal = Principal(principal_entity_id=roster_only)
|
|
324
|
+
group_editor_principal = Principal(principal_entity_id=group_editor)
|
|
325
|
+
|
|
326
|
+
group = self.runtime.entity_service.create(Group)
|
|
327
|
+
group.name = 'Engineering'
|
|
328
|
+
group.owner = self.OWNER
|
|
329
|
+
group.add_member(roster_only)
|
|
330
|
+
with kernel_internal_context():
|
|
331
|
+
group = await self.api.entity_service.save_entity(group, self.owner_principal)
|
|
332
|
+
|
|
333
|
+
await self.api.grant(self.owner_principal, scope.entity_id, group.entity_id, 'editor')
|
|
334
|
+
# editor-of-group: a direct grant on the group *resource*, distinct from roster membership.
|
|
335
|
+
await self.api.grant(self.owner_principal, group.entity_id, group_editor, 'editor')
|
|
336
|
+
|
|
337
|
+
add_req = AddMember.request_constructor(target_entity_id=group.entity_id, principal_entity_id=member)
|
|
338
|
+
await run(self.api.strategy_executor, add_req, principal=self.owner_principal)
|
|
339
|
+
# The reverse member_ids lookup uses a Postgres-only JSONB operator
|
|
340
|
+
# (`?`), unavailable on the SQLite test backend - warm the adapter's
|
|
341
|
+
# cache directly, matching test_consumers.py's group scenario.
|
|
342
|
+
adapter = self.api.entity_service.access_adapter
|
|
343
|
+
adapter.cache.set(adapter._subject_groups_cache_key(member), [group.entity_id], timeout=300)
|
|
344
|
+
|
|
345
|
+
child_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
346
|
+
write_req = SetAttributes.request_constructor(target_entity_id=child_id, attribute_map={'x': 1})
|
|
347
|
+
result, _ = await run(self.api.strategy_executor, write_req, principal=member_principal)
|
|
348
|
+
self.assertIsNotNone(result)
|
|
349
|
+
|
|
350
|
+
# A plain roster member (no grant on the group itself) cannot manage the roster.
|
|
351
|
+
add_req2 = AddMember.request_constructor(target_entity_id=group.entity_id, principal_entity_id=roster_only)
|
|
352
|
+
with self.assertRaises(Forbidden):
|
|
353
|
+
await run(self.api.strategy_executor, add_req2, principal=roster_only_principal)
|
|
354
|
+
|
|
355
|
+
# Editor-of-group can rename (Write suffices)...
|
|
356
|
+
rename_req = ModifyGroup.request_constructor(target_entity_id=group.entity_id, name='Eng Team')
|
|
357
|
+
renamed, _ = await run(self.api.strategy_executor, rename_req, principal=group_editor_principal)
|
|
358
|
+
self.assertIsNotNone(renamed)
|
|
359
|
+
|
|
360
|
+
# ...but not the roster (ManageAccess required).
|
|
361
|
+
add_req3 = AddMember.request_constructor(target_entity_id=group.entity_id, principal_entity_id=roster_only)
|
|
362
|
+
with self.assertRaises(Forbidden):
|
|
363
|
+
await run(self.api.strategy_executor, add_req3, principal=group_editor_principal)
|
|
364
|
+
|
|
365
|
+
# --- D: grant security ----------------------------------------------------
|
|
366
|
+
|
|
367
|
+
@async_to_sync
|
|
368
|
+
async def test_d_grant_security(self):
|
|
369
|
+
scope = await self._create_scope()
|
|
370
|
+
editor = await self._create_user('d-editor')
|
|
371
|
+
admin_user = await self._create_user('d-admin')
|
|
372
|
+
editor_principal = Principal(principal_entity_id=editor)
|
|
373
|
+
admin_principal = Principal(principal_entity_id=admin_user)
|
|
374
|
+
|
|
375
|
+
grant_id = await self.api.grant(self.owner_principal, scope.entity_id, editor, 'editor')
|
|
376
|
+
await self.api.grant(self.owner_principal, scope.entity_id, admin_user, 'admin')
|
|
377
|
+
|
|
378
|
+
# Editor cannot invoke CreateGrant (needs ManageAccess).
|
|
379
|
+
with self.assertRaises(Forbidden):
|
|
380
|
+
await self.api.grant(editor_principal, scope.entity_id, editor, 'admin')
|
|
381
|
+
|
|
382
|
+
# Editor cannot generic-create a Grant (exhaustive allowlist).
|
|
383
|
+
create_req = CreateEntity.request_constructor(
|
|
384
|
+
target_entity_id=scope.entity_id, entity_type_id=Grant.get_entity_type_id(),
|
|
385
|
+
)
|
|
386
|
+
with self.assertRaises(Forbidden):
|
|
387
|
+
await run(self.api.strategy_executor, create_req, principal=editor_principal)
|
|
388
|
+
|
|
389
|
+
# Editor cannot generic-delete one either.
|
|
390
|
+
delete_req = DeleteEntity.request_constructor(target_entity_id=grant_id)
|
|
391
|
+
with self.assertRaises(Forbidden):
|
|
392
|
+
await run(self.api.strategy_executor, delete_req, principal=editor_principal)
|
|
393
|
+
|
|
394
|
+
# An admin (not necessarily the owner) can revoke.
|
|
395
|
+
await self.api.revoke(admin_principal, scope.entity_id, editor)
|
|
396
|
+
self.assertEqual((await self.api.get_entity(editor_principal, scope.entity_id)).cause, 'forbidden')
|
|
397
|
+
|
|
398
|
+
@async_to_sync
|
|
399
|
+
async def test_grant_creator_loses_control_after_demotion(self):
|
|
400
|
+
"""Grants have no owner: creating one confers nothing once the
|
|
401
|
+
creator stops holding ManageAccess on the resource."""
|
|
402
|
+
scope = await self._create_scope()
|
|
403
|
+
manager = await self._create_user('d-manager')
|
|
404
|
+
grantee = await self._create_user('d-grantee')
|
|
405
|
+
manager_principal = Principal(principal_entity_id=manager)
|
|
406
|
+
|
|
407
|
+
await self.api.grant(self.owner_principal, scope.entity_id, manager, 'admin')
|
|
408
|
+
grant_id = await self.api.grant(manager_principal, scope.entity_id, grantee, 'viewer')
|
|
409
|
+
self.assertIsNone((await self.runtime.entity_service.load_from_db(grant_id)).owner)
|
|
410
|
+
self.assertIsInstance(await self.api.get_entity(manager_principal, grant_id), Grant)
|
|
411
|
+
|
|
412
|
+
await self.api.grant(self.owner_principal, scope.entity_id, manager, 'viewer')
|
|
413
|
+
|
|
414
|
+
self.assertEqual((await self.api.get_entity(manager_principal, grant_id)).cause, 'forbidden')
|
|
415
|
+
change_req = ChangeGrantRole.request_constructor(target_entity_id=grant_id, role_name='editor')
|
|
416
|
+
with self.assertRaises(Forbidden):
|
|
417
|
+
await run(self.api.strategy_executor, change_req, principal=manager_principal)
|
|
418
|
+
revoke_req = RevokeGrant.request_constructor(target_entity_id=grant_id)
|
|
419
|
+
with self.assertRaises(Forbidden):
|
|
420
|
+
await run(self.api.strategy_executor, revoke_req, principal=manager_principal)
|
|
421
|
+
|
|
422
|
+
# --- E: cascade ------------------------------------------------------------
|
|
423
|
+
|
|
424
|
+
@async_to_sync
|
|
425
|
+
async def test_e_cascade(self):
|
|
426
|
+
scope = await self._create_scope()
|
|
427
|
+
editor = await self._create_user('e-editor')
|
|
428
|
+
editor_principal = Principal(principal_entity_id=editor)
|
|
429
|
+
await self.api.grant(self.owner_principal, scope.entity_id, editor, 'editor')
|
|
430
|
+
child_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
431
|
+
|
|
432
|
+
socket = await self._connect(editor)
|
|
433
|
+
|
|
434
|
+
delete_req = DeleteEntity.request_constructor(target_entity_id=scope.entity_id)
|
|
435
|
+
await run(self.api.strategy_executor, delete_req, principal=editor_principal)
|
|
436
|
+
|
|
437
|
+
tombstone = await self._receive_until(
|
|
438
|
+
socket,
|
|
439
|
+
lambda m: m['type'] == 'entity_update' and m['entities'].get(scope.entity_id, {}).get('deleted'),
|
|
440
|
+
max_messages=12,
|
|
441
|
+
)
|
|
442
|
+
self.assertTrue(tombstone['entities'][scope.entity_id]['deleted'])
|
|
443
|
+
await socket.disconnect()
|
|
444
|
+
|
|
445
|
+
for entity_id in (scope.entity_id, child_id):
|
|
446
|
+
self.assertEqual((await self.api.get_entity(self.owner_principal, entity_id)).cause, 'missing')
|
|
447
|
+
|
|
448
|
+
grants_remain = await GrantModel.objects.filter(parent_id=scope.entity_id).aexists()
|
|
449
|
+
self.assertFalse(grants_remain)
|
|
450
|
+
|
|
451
|
+
# --- F: type policy ----------------------------------------------------
|
|
452
|
+
|
|
453
|
+
@async_to_sync
|
|
454
|
+
async def test_f_type_policy(self):
|
|
455
|
+
article = await self._create_scope(entity_cls=_Article)
|
|
456
|
+
editor = await self._create_user('f-editor')
|
|
457
|
+
publisher = await self._create_user('f-publisher')
|
|
458
|
+
stranger = await self._create_user('f-stranger')
|
|
459
|
+
editor_principal = Principal(principal_entity_id=editor)
|
|
460
|
+
publisher_principal = Principal(principal_entity_id=publisher)
|
|
461
|
+
stranger_principal = Principal(principal_entity_id=stranger)
|
|
462
|
+
|
|
463
|
+
await self.api.grant(self.owner_principal, article.entity_id, editor, 'editor')
|
|
464
|
+
await self.api.grant(self.owner_principal, article.entity_id, publisher, 'publisher')
|
|
465
|
+
|
|
466
|
+
publish_req = PublishArticle.request_constructor(target_entity_id=article.entity_id)
|
|
467
|
+
result, _ = await run(self.api.strategy_executor, publish_req, principal=publisher_principal)
|
|
468
|
+
self.assertIsNotNone(result)
|
|
469
|
+
|
|
470
|
+
publish_req2 = PublishArticle.request_constructor(target_entity_id=article.entity_id)
|
|
471
|
+
with self.assertRaises(Forbidden):
|
|
472
|
+
await run(self.api.strategy_executor, publish_req2, principal=editor_principal)
|
|
473
|
+
|
|
474
|
+
# public_perms(User, Read()): any authenticated principal reads user entities.
|
|
475
|
+
read_user = await self.api.get_entity(stranger_principal, publisher)
|
|
476
|
+
self.assertIsInstance(read_user, User)
|
|
477
|
+
# --- G: laundering + read-only -------------------------------------------
|
|
478
|
+
|
|
479
|
+
@async_to_sync
|
|
480
|
+
async def test_g_laundering_blocked_and_read_only_succeeds(self):
|
|
481
|
+
scope = await self._create_scope()
|
|
482
|
+
editor = await self._create_user('g-editor')
|
|
483
|
+
viewer = await self._create_user('g-viewer')
|
|
484
|
+
editor_principal = Principal(principal_entity_id=editor)
|
|
485
|
+
viewer_principal = Principal(principal_entity_id=viewer)
|
|
486
|
+
|
|
487
|
+
grant_id = await self.api.grant(self.owner_principal, scope.entity_id, editor, 'editor')
|
|
488
|
+
await self.api.grant(self.owner_principal, scope.entity_id, viewer, 'viewer')
|
|
489
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
490
|
+
|
|
491
|
+
# The rogue strategy's invocation check (against the Doc it declares
|
|
492
|
+
# as its target) passes; the persistence check (against the
|
|
493
|
+
# Grant it actually tries to write) must still block it.
|
|
494
|
+
_RogueLaundering.smuggled = await self.runtime.entity_service.load_from_db(grant_id)
|
|
495
|
+
rogue_req = _RogueLaundering.request_constructor(target_entity_id=doc_id, grant_id=grant_id)
|
|
496
|
+
with self.assertRaisesMessage(Forbidden, 'may not write a Grant'):
|
|
497
|
+
await run(self.api.strategy_executor, rogue_req, principal=editor_principal)
|
|
498
|
+
|
|
499
|
+
read_req = _ReadOnlyProbe.request_constructor(target_entity_id=doc_id)
|
|
500
|
+
result, _ = await run(self.api.strategy_executor, read_req, principal=viewer_principal)
|
|
501
|
+
self.assertIsNotNone(result)
|
|
502
|
+
|
|
503
|
+
@async_to_sync
|
|
504
|
+
async def test_g_rogue_detach_blocked_for_editor_and_owner(self):
|
|
505
|
+
"""Containment is derived from the child's own `parent_id`, so the
|
|
506
|
+
only way to detach a grant from its resource is to write the grant
|
|
507
|
+
row - which `Grant`'s allowlist rejects for every strategy
|
|
508
|
+
but the three grant strategies, regardless of who invokes it."""
|
|
509
|
+
scope = await self._create_scope()
|
|
510
|
+
editor = await self._create_user('g-detach-editor')
|
|
511
|
+
editor_principal = Principal(principal_entity_id=editor)
|
|
512
|
+
|
|
513
|
+
grant_id = await self.api.grant(self.owner_principal, scope.entity_id, editor, 'editor')
|
|
514
|
+
|
|
515
|
+
detach_req = _RogueDetach.request_constructor(target_entity_id=grant_id)
|
|
516
|
+
with self.assertRaises(Forbidden):
|
|
517
|
+
await run(self.api.strategy_executor, detach_req, principal=editor_principal)
|
|
518
|
+
|
|
519
|
+
detach_req_owner = _RogueDetach.request_constructor(target_entity_id=grant_id)
|
|
520
|
+
with self.assertRaises(Forbidden):
|
|
521
|
+
await run(self.api.strategy_executor, detach_req_owner, principal=self.owner_principal)
|
|
522
|
+
|
|
523
|
+
scope_after = await self.api.get_entity(self.owner_principal, scope.entity_id)
|
|
524
|
+
self.assertIn(grant_id, scope_after.children_ids)
|
|
525
|
+
|
|
526
|
+
@async_to_sync
|
|
527
|
+
async def test_g_read_only_strategy_cannot_persist(self):
|
|
528
|
+
scope = await self._create_scope()
|
|
529
|
+
viewer = await self._create_user('g-ro-viewer')
|
|
530
|
+
viewer_principal = Principal(principal_entity_id=viewer)
|
|
531
|
+
await self.api.grant(self.owner_principal, scope.entity_id, viewer, 'viewer')
|
|
532
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
533
|
+
|
|
534
|
+
# Invocation passes - a read-only strategy requires only Read.
|
|
535
|
+
req = _MisdeclaredReadOnly.request_constructor(target_entity_id=doc_id)
|
|
536
|
+
with self.assertRaises(Forbidden):
|
|
537
|
+
await run(self.api.strategy_executor, req, principal=viewer_principal)
|
|
538
|
+
|
|
539
|
+
# A contract violation, not a perm question: the owner's implicit
|
|
540
|
+
# Admin does not clear it either.
|
|
541
|
+
owner_req = _MisdeclaredReadOnly.request_constructor(target_entity_id=doc_id)
|
|
542
|
+
with self.assertRaises(Forbidden):
|
|
543
|
+
await run(self.api.strategy_executor, owner_req, principal=self.owner_principal)
|
|
544
|
+
|
|
545
|
+
doc = await self.api.get_entity(self.owner_principal, doc_id)
|
|
546
|
+
self.assertFalse(doc.has_attribute('smuggled'))
|
|
547
|
+
|
|
548
|
+
# --- H: live delivery (WebsocketCommunicator, full stack) ----------------
|
|
549
|
+
|
|
550
|
+
@async_to_sync
|
|
551
|
+
async def test_h_fan_out_on_child_save(self):
|
|
552
|
+
scope = await self._create_scope()
|
|
553
|
+
bob = await self._create_user('h-bob')
|
|
554
|
+
stranger = await self._create_user('h-stranger')
|
|
555
|
+
await self.api.grant(self.owner_principal, scope.entity_id, bob, 'editor')
|
|
556
|
+
|
|
557
|
+
bob_socket = await self._connect(bob)
|
|
558
|
+
stranger_socket = await self._connect(stranger)
|
|
559
|
+
|
|
560
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
561
|
+
|
|
562
|
+
update = await self._receive_until(
|
|
563
|
+
bob_socket, lambda m: m['type'] == 'entity_update' and doc_id in m['entities'],
|
|
564
|
+
)
|
|
565
|
+
self.assertIn(doc_id, update['entities'])
|
|
566
|
+
await stranger_socket.receive_nothing()
|
|
567
|
+
|
|
568
|
+
await bob_socket.disconnect()
|
|
569
|
+
await stranger_socket.disconnect()
|
|
570
|
+
|
|
571
|
+
@async_to_sync
|
|
572
|
+
async def test_h_grant_while_connected_joins_and_delivers(self):
|
|
573
|
+
scope = await self._create_scope()
|
|
574
|
+
alice = await self._create_user('h-alice')
|
|
575
|
+
alice_socket = await self._connect(alice)
|
|
576
|
+
|
|
577
|
+
await self.api.grant(self.owner_principal, scope.entity_id, alice, 'viewer')
|
|
578
|
+
|
|
579
|
+
granted = await self._receive_until(alice_socket, lambda m: m['type'] == 'access_granted')
|
|
580
|
+
self.assertEqual(granted['resource_id'], scope.entity_id)
|
|
581
|
+
|
|
582
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
583
|
+
update = await self._receive_until(
|
|
584
|
+
alice_socket, lambda m: m['type'] == 'entity_update' and doc_id in m['entities'],
|
|
585
|
+
)
|
|
586
|
+
self.assertIn(doc_id, update['entities'])
|
|
587
|
+
|
|
588
|
+
await alice_socket.disconnect()
|
|
589
|
+
|
|
590
|
+
@async_to_sync
|
|
591
|
+
async def test_h_revoke_while_connected_leaves_and_stops_delivery(self):
|
|
592
|
+
scope = await self._create_scope()
|
|
593
|
+
alice = await self._create_user('h-alice2')
|
|
594
|
+
await self.api.grant(self.owner_principal, scope.entity_id, alice, 'viewer')
|
|
595
|
+
alice_socket = await self._connect(alice)
|
|
596
|
+
|
|
597
|
+
await self.api.revoke(self.owner_principal, scope.entity_id, alice)
|
|
598
|
+
|
|
599
|
+
revoked = await self._receive_until(alice_socket, lambda m: m['type'] == 'access_revoked')
|
|
600
|
+
self.assertEqual(revoked['resource_id'], scope.entity_id)
|
|
601
|
+
|
|
602
|
+
await self._create_child(scope.entity_id, self.owner_principal)
|
|
603
|
+
await alice_socket.receive_nothing()
|
|
604
|
+
|
|
605
|
+
await alice_socket.disconnect()
|
|
606
|
+
|
|
607
|
+
@async_to_sync
|
|
608
|
+
async def test_h_group_member_receives_and_stops_after_removal(self):
|
|
609
|
+
scope = await self._create_scope()
|
|
610
|
+
member = await self._create_user('h-group-member')
|
|
611
|
+
group = self.runtime.entity_service.create(Group)
|
|
612
|
+
group.name = 'Live Fan-out'
|
|
613
|
+
group.owner = self.OWNER
|
|
614
|
+
group.add_member(member)
|
|
615
|
+
with kernel_internal_context():
|
|
616
|
+
group = await self.api.entity_service.save_entity(group, self.owner_principal)
|
|
617
|
+
|
|
618
|
+
await self.api.grant(self.owner_principal, scope.entity_id, group.entity_id, 'viewer')
|
|
619
|
+
adapter = self.api.entity_service.access_adapter
|
|
620
|
+
adapter.cache.set(adapter._subject_groups_cache_key(member), [group.entity_id], timeout=300)
|
|
621
|
+
|
|
622
|
+
member_socket = await self._connect(member)
|
|
623
|
+
|
|
624
|
+
doc_id = await self._create_child(scope.entity_id, self.owner_principal)
|
|
625
|
+
update = await self._receive_until(
|
|
626
|
+
member_socket, lambda m: m['type'] == 'entity_update' and doc_id in m['entities'],
|
|
627
|
+
)
|
|
628
|
+
self.assertIn(doc_id, update['entities'])
|
|
629
|
+
|
|
630
|
+
group.remove_member(member)
|
|
631
|
+
with kernel_internal_context():
|
|
632
|
+
await self.api.entity_service.save_entity(group, self.owner_principal)
|
|
633
|
+
|
|
634
|
+
await self._create_child(scope.entity_id, self.owner_principal)
|
|
635
|
+
await member_socket.receive_nothing()
|
|
636
|
+
|
|
637
|
+
await member_socket.disconnect()
|