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,154 @@
|
|
|
1
|
+
"""Deployment seeding - see docs/AUTHORIZATION_TECHNICAL_TDD.md
|
|
2
|
+
(Identity-based policy, Seeding)."""
|
|
3
|
+
import asyncio
|
|
4
|
+
import inspect
|
|
5
|
+
|
|
6
|
+
from asgiref.sync import async_to_sync
|
|
7
|
+
from django.test import TransactionTestCase
|
|
8
|
+
|
|
9
|
+
import graphcore.bootstrap.deployment as deployment_module
|
|
10
|
+
from graphcore.auth.authz.lookups import find_grant, find_group_by_key
|
|
11
|
+
from graphcore.bootstrap.deployment import ensure_deployment
|
|
12
|
+
from graphcore.auth.authz.defaults import ADMINS_GROUP_KEY
|
|
13
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
14
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
15
|
+
from graphcore.domain.entity.group.Group import Group
|
|
16
|
+
from graphcore.errors import InvalidRequest
|
|
17
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
18
|
+
from graphcore.persistence.django.models import DeploymentModel, GrantModel, GroupModel
|
|
19
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
20
|
+
from graphcore.runtime import django_runtime
|
|
21
|
+
from graphcore.types.access import ManageAccess
|
|
22
|
+
from graphcore.types.identity import PUBLIC
|
|
23
|
+
from graphcore.types.identity import Principal
|
|
24
|
+
from graphcore.types.query import Equal
|
|
25
|
+
|
|
26
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
27
|
+
with TEST_REGISTRY.policy.declaring_app('seedtest'):
|
|
28
|
+
TEST_REGISTRY.policy.declare_group(key='seedtest.staff', name='Staff')
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class DeploymentSeedTests(TransactionTestCase):
|
|
32
|
+
def setUp(self):
|
|
33
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
34
|
+
self.service = self.runtime.entity_service
|
|
35
|
+
|
|
36
|
+
def _seed(self):
|
|
37
|
+
return ensure_deployment(self.service, TEST_REGISTRY)
|
|
38
|
+
|
|
39
|
+
async def _grants_on(self, resource_id):
|
|
40
|
+
return list((await self.service.find_entities(Equal('parent_id', resource_id))).values())
|
|
41
|
+
|
|
42
|
+
@async_to_sync
|
|
43
|
+
async def test_seeds_deployment_admins_and_default_grants(self):
|
|
44
|
+
await self._seed()
|
|
45
|
+
|
|
46
|
+
deployment = await self.service.repository.load(self.runtime.deployment_entity_id)
|
|
47
|
+
self.assertIsInstance(deployment, Deployment)
|
|
48
|
+
self.assertIsNone(deployment.owner)
|
|
49
|
+
|
|
50
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
51
|
+
self.assertIsNone(admins.owner)
|
|
52
|
+
self.assertEqual((await find_grant(self.service, deployment.entity_id, PUBLIC)).role_name, 'member')
|
|
53
|
+
self.assertEqual((await find_grant(self.service, deployment.entity_id, admins.entity_id)).role_name, 'admin')
|
|
54
|
+
self.assertEqual((await find_grant(self.service, admins.entity_id, admins.entity_id)).role_name, 'admin')
|
|
55
|
+
|
|
56
|
+
staff = await find_group_by_key(self.service, 'seedtest.staff')
|
|
57
|
+
self.assertEqual(staff.name, 'Staff')
|
|
58
|
+
self.assertEqual((await find_grant(self.service, staff.entity_id, admins.entity_id)).role_name, 'admin')
|
|
59
|
+
|
|
60
|
+
@async_to_sync
|
|
61
|
+
async def test_seeded_grants_have_no_owner(self):
|
|
62
|
+
await self._seed()
|
|
63
|
+
grants = [g for g in await self._grants_on(self.runtime.deployment_entity_id) if isinstance(g, Grant)]
|
|
64
|
+
self.assertTrue(grants)
|
|
65
|
+
self.assertTrue(all(g.owner is None for g in grants))
|
|
66
|
+
|
|
67
|
+
@async_to_sync
|
|
68
|
+
async def test_rerun_creates_nothing(self):
|
|
69
|
+
await self._seed()
|
|
70
|
+
groups, grants = await GroupModel.objects.acount(), await GrantModel.objects.acount()
|
|
71
|
+
|
|
72
|
+
await self._seed()
|
|
73
|
+
|
|
74
|
+
self.assertEqual(await GroupModel.objects.acount(), groups)
|
|
75
|
+
self.assertEqual(await GrantModel.objects.acount(), grants)
|
|
76
|
+
self.assertEqual(await DeploymentModel.objects.acount(), 1)
|
|
77
|
+
|
|
78
|
+
@async_to_sync
|
|
79
|
+
async def test_rerun_keeps_a_revoked_default_grant_revoked(self):
|
|
80
|
+
await self._seed()
|
|
81
|
+
public_grant = await find_grant(self.service, self.runtime.deployment_entity_id, PUBLIC)
|
|
82
|
+
with kernel_internal_context():
|
|
83
|
+
await self.service.delete_entities([public_grant.entity_id], Principal(principal_entity_id='op'))
|
|
84
|
+
|
|
85
|
+
await self._seed()
|
|
86
|
+
|
|
87
|
+
self.assertIsNone(await find_grant(self.service, self.runtime.deployment_entity_id, PUBLIC))
|
|
88
|
+
|
|
89
|
+
@async_to_sync
|
|
90
|
+
async def test_deleted_declared_group_is_recreated_empty(self):
|
|
91
|
+
await self._seed()
|
|
92
|
+
staff = await find_group_by_key(self.service, 'seedtest.staff')
|
|
93
|
+
with kernel_internal_context():
|
|
94
|
+
await self.service.delete_entities([staff.entity_id], Principal(principal_entity_id='op'))
|
|
95
|
+
|
|
96
|
+
await self._seed()
|
|
97
|
+
|
|
98
|
+
recreated = await find_group_by_key(self.service, 'seedtest.staff')
|
|
99
|
+
self.assertNotEqual(recreated.entity_id, staff.entity_id)
|
|
100
|
+
self.assertEqual(recreated.member_ids, [])
|
|
101
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
102
|
+
self.assertIsNotNone(await find_grant(self.service, recreated.entity_id, admins.entity_id))
|
|
103
|
+
|
|
104
|
+
@async_to_sync
|
|
105
|
+
async def test_concurrent_seeding_creates_one_of_everything(self):
|
|
106
|
+
await asyncio.gather(self._seed(), self._seed())
|
|
107
|
+
|
|
108
|
+
self.assertEqual(await DeploymentModel.objects.acount(), 1)
|
|
109
|
+
self.assertEqual(await GroupModel.objects.filter(key=ADMINS_GROUP_KEY).acount(), 1)
|
|
110
|
+
grants = await self._grants_on(self.runtime.deployment_entity_id)
|
|
111
|
+
self.assertEqual(sorted(g.subject_id for g in grants if isinstance(g, Grant)).count(PUBLIC), 1)
|
|
112
|
+
|
|
113
|
+
@async_to_sync
|
|
114
|
+
async def test_nobody_manages_the_deployment_until_added_to_admins(self):
|
|
115
|
+
await self._seed()
|
|
116
|
+
user = Principal(principal_entity_id='seed-user')
|
|
117
|
+
self.assertFalse(await self.runtime.access.allows(user, ManageAccess(), self.runtime.deployment_entity_id))
|
|
118
|
+
|
|
119
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
120
|
+
admins.add_member('seed-user')
|
|
121
|
+
with kernel_internal_context():
|
|
122
|
+
await self.service.save_entity(admins, None)
|
|
123
|
+
|
|
124
|
+
self.assertTrue(await self.runtime.access.allows(user, ManageAccess(), self.runtime.deployment_entity_id))
|
|
125
|
+
|
|
126
|
+
@async_to_sync
|
|
127
|
+
async def test_nothing_but_grants_can_be_placed_under_the_deployment(self):
|
|
128
|
+
await self._seed()
|
|
129
|
+
child = self.service.create(Group)
|
|
130
|
+
child.parent_id = self.runtime.deployment_entity_id
|
|
131
|
+
with kernel_internal_context(), self.assertRaisesMessage(InvalidRequest, 'cannot contain Group'):
|
|
132
|
+
await self.service.save_entity(child, None)
|
|
133
|
+
|
|
134
|
+
@async_to_sync
|
|
135
|
+
async def test_group_key_cannot_change_once_set(self):
|
|
136
|
+
await self._seed()
|
|
137
|
+
staff = await find_group_by_key(self.service, 'seedtest.staff')
|
|
138
|
+
staff.key = 'seedtest.other'
|
|
139
|
+
with kernel_internal_context(), self.assertRaises(InvalidRequest):
|
|
140
|
+
await self.service.save_entity(staff, None)
|
|
141
|
+
|
|
142
|
+
def test_seeding_does_not_import_the_django_adapter(self):
|
|
143
|
+
self.assertNotIn('persistence.django', inspect.getsource(deployment_module))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class DeploymentSurvivesFlushTests(TransactionTestCase):
|
|
147
|
+
"""A flush empties every table and re-sends `post_migrate`; seeding must
|
|
148
|
+
re-establish the deployment, as it does the backend identity."""
|
|
149
|
+
|
|
150
|
+
def test_deployment_is_reseeded_after_a_flush(self):
|
|
151
|
+
# Each TransactionTestCase test starts after the previous one's flush.
|
|
152
|
+
runtime = django_runtime(DEFAULT_REGISTRY)
|
|
153
|
+
deployment = async_to_sync(runtime.repository.load)(runtime.deployment_entity_id)
|
|
154
|
+
self.assertIsInstance(deployment, Deployment)
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""The live-update delivery contract.
|
|
2
|
+
|
|
3
|
+
Engine-neutral: publishes entity saves/deletes to scope channels and access
|
|
4
|
+
changes to connected principals covered by the change's subject. Never makes
|
|
5
|
+
an authorization decision - it only asks the `AccessPort`'s sync-support
|
|
6
|
+
slice (`scopes`/`covers`) who to tell. A concrete subclass implements four
|
|
7
|
+
primitives (`group_send`, `group_add`, `group_discard`, `send`); everything
|
|
8
|
+
else - routing, ordering, the presence registry - is inherited.
|
|
9
|
+
See docs/GRAPHAPI_FRONT_DOOR.md.
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
from abc import ABC, abstractmethod
|
|
15
|
+
|
|
16
|
+
from graphcore.types.access import AccessChange, Read
|
|
17
|
+
from graphcore.types.entities import Tombstone, client_view
|
|
18
|
+
from graphcore.types.identity import Principal
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _group_safe(entity_id: str) -> str:
|
|
22
|
+
r"""Channels group names allow only `[a-zA-Z0-9\-_.]`, and an entity
|
|
23
|
+
identifier carries `:` and `/`. Injective over the id alphabet: `_` never
|
|
24
|
+
appears in a raw identifier, so two ids never collide here."""
|
|
25
|
+
return re.sub(r'[^a-zA-Z0-9\-_.]', '_', str(entity_id))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def scope_channel(scope_id: str) -> str:
|
|
29
|
+
return f"scope_{_group_safe(scope_id)}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def owner_channel(subject_id: str) -> str:
|
|
33
|
+
return f"owner_{_group_safe(subject_id)}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Broadcaster(ABC):
|
|
37
|
+
"""`register_connection`/`deregister_connection` back a lightweight,
|
|
38
|
+
per-process presence registry - the minimal mechanism `publish_access_change`
|
|
39
|
+
needs to know which principals are connected without a roster scan
|
|
40
|
+
(transport/consumers.py calls these on connect/disconnect)."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, access_adapter, registry):
|
|
43
|
+
self.access = access_adapter
|
|
44
|
+
self.registry = registry
|
|
45
|
+
self._connected: dict[str, set[str]] = {}
|
|
46
|
+
|
|
47
|
+
# --- primitives, backend-specific ----------------------------------
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
async def group_send(self, group_name: str, message: dict) -> None:
|
|
51
|
+
"""Best-effort fan-out to every channel in `group_name`."""
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
async def group_add(self, group_name: str, channel_name: str) -> None:
|
|
55
|
+
"""Best-effort join of `channel_name` to `group_name`."""
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
async def group_discard(self, group_name: str, channel_name: str) -> None:
|
|
59
|
+
"""Best-effort removal of `channel_name` from `group_name`."""
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
async def send(self, channel_name: str, message: dict) -> None:
|
|
63
|
+
"""Best-effort direct send to one channel."""
|
|
64
|
+
|
|
65
|
+
# --- presence registry -------------------------------------------------
|
|
66
|
+
|
|
67
|
+
def register_connection(self, principal_id: str, channel_name: str) -> None:
|
|
68
|
+
self._connected.setdefault(principal_id, set()).add(channel_name)
|
|
69
|
+
|
|
70
|
+
def deregister_connection(self, principal_id: str, channel_name: str) -> None:
|
|
71
|
+
channels = self._connected.get(principal_id)
|
|
72
|
+
if not channels:
|
|
73
|
+
return
|
|
74
|
+
channels.discard(channel_name)
|
|
75
|
+
if not channels:
|
|
76
|
+
del self._connected[principal_id]
|
|
77
|
+
|
|
78
|
+
def connected_principals(self) -> list[str]:
|
|
79
|
+
return list(self._connected)
|
|
80
|
+
|
|
81
|
+
def _channels_for(self, principal_id: str) -> set[str]:
|
|
82
|
+
return set(self._connected.get(principal_id, ()))
|
|
83
|
+
|
|
84
|
+
# --- publish ------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
async def publish_save(self, entity) -> None:
|
|
87
|
+
"""Fan out a save to every scope channel covering it, plus `public`
|
|
88
|
+
when the type grants Read to everyone."""
|
|
89
|
+
await self._publish(entity, {entity.entity_id: client_view(entity)})
|
|
90
|
+
|
|
91
|
+
async def publish_delete(self, entity) -> None:
|
|
92
|
+
"""Fan out a tombstone to every scope channel that covered `entity`,
|
|
93
|
+
from its last-known in-memory state (see `AccessPort.scopes`). A
|
|
94
|
+
tombstone carries only the id, so it also takes the Read route: a
|
|
95
|
+
subject whose last managing grant was this one still hears it."""
|
|
96
|
+
tombstone = client_view(Tombstone(entity.entity_id, 'deleted'))
|
|
97
|
+
await self._publish(entity, {entity.entity_id: tombstone}, tombstone=True)
|
|
98
|
+
|
|
99
|
+
async def publish_reference_changes(self, source_entity, added, removed) -> None:
|
|
100
|
+
"""Announce the references one save of `source_entity` added and
|
|
101
|
+
removed, one `reference_update` per target. Routed on the source, so
|
|
102
|
+
only principals who can read the source hear about it - the target's
|
|
103
|
+
readers learn nothing about a source they cannot see."""
|
|
104
|
+
changes = [(target, 'added') for target in added] + [(target, 'removed') for target in removed]
|
|
105
|
+
if not changes:
|
|
106
|
+
return
|
|
107
|
+
groups = await self._recipients(source_entity)
|
|
108
|
+
for target_entity_id, change in changes:
|
|
109
|
+
await self._send(groups, {
|
|
110
|
+
'type': 'reference_update',
|
|
111
|
+
'target_entity_id': target_entity_id,
|
|
112
|
+
'source_entity_id': source_entity.entity_id,
|
|
113
|
+
'change': change,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
async def _publish(self, entity, entities_payload: dict, *, tombstone: bool = False) -> None:
|
|
117
|
+
message = {'type': 'entity_update', 'entities': entities_payload}
|
|
118
|
+
await self._send(await self._recipients(entity, tombstone=tombstone), message)
|
|
119
|
+
|
|
120
|
+
async def _recipients(self, entity, *, tombstone: bool = False) -> list[str]:
|
|
121
|
+
"""Scope channels carry Read. A type read with another permission
|
|
122
|
+
goes to the owner channel of each principal holding it instead."""
|
|
123
|
+
scope_ids = await self.access.scopes(entity.entity_id, entity.ancestor_ids)
|
|
124
|
+
policy = self.registry.policy
|
|
125
|
+
entity_cls = self._entity_class(entity)
|
|
126
|
+
read_perm = policy.read_permission(entity_cls)
|
|
127
|
+
|
|
128
|
+
groups: list[str] = []
|
|
129
|
+
if read_perm != Read():
|
|
130
|
+
holder_ids, everyone = await self.access.holders(read_perm, scope_ids)
|
|
131
|
+
if everyone or read_perm in policy.type_public_perms(entity_cls):
|
|
132
|
+
groups.append('public')
|
|
133
|
+
else:
|
|
134
|
+
groups += [owner_channel(pid) for pid in sorted(holder_ids)]
|
|
135
|
+
if not tombstone:
|
|
136
|
+
return groups
|
|
137
|
+
|
|
138
|
+
# The entity's own owner is on it already: owner never changes once set.
|
|
139
|
+
owner_principals = await self.access.owner_principals(scope_ids - {str(entity.entity_id)})
|
|
140
|
+
if owner := getattr(entity, 'owner', None):
|
|
141
|
+
owner_principals.add(owner)
|
|
142
|
+
groups += [owner_channel(principal_id) for principal_id in owner_principals]
|
|
143
|
+
groups += [scope_channel(scope_id) for scope_id in scope_ids]
|
|
144
|
+
if Read() in policy.type_public_perms(entity_cls):
|
|
145
|
+
groups.append('public')
|
|
146
|
+
return list(dict.fromkeys(groups))
|
|
147
|
+
|
|
148
|
+
async def _send(self, groups, message: dict) -> None:
|
|
149
|
+
for group in groups:
|
|
150
|
+
await self.group_send(group, message)
|
|
151
|
+
|
|
152
|
+
def _entity_class(self, entity):
|
|
153
|
+
entity_type_id = getattr(entity, 'entity_type_id', None)
|
|
154
|
+
return self.registry.entities.get_entity_class(entity_type_id) if entity_type_id else None
|
|
155
|
+
|
|
156
|
+
def _read_permission(self, entity_type_id):
|
|
157
|
+
entity_cls = self.registry.entities.get_entity_class(entity_type_id) if entity_type_id else None
|
|
158
|
+
return self.registry.policy.read_permission(entity_cls) if entity_cls else Read()
|
|
159
|
+
|
|
160
|
+
async def publish_access_change(self, change: AccessChange) -> None:
|
|
161
|
+
"""Deliver a channel join/leave plus a lightweight notification to
|
|
162
|
+
every *connected* principal the change's subject resolves to - never
|
|
163
|
+
a data backfill (the scope channel join already covers the whole
|
|
164
|
+
subtree, including entities created later)."""
|
|
165
|
+
connected = self.connected_principals()
|
|
166
|
+
if not connected:
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
covered = await self.access.covers(change.subject_id, connected)
|
|
170
|
+
read_perm = self._read_permission(change.resource_type_id)
|
|
171
|
+
for principal_id in covered:
|
|
172
|
+
if change.new_role is None and await self.access.allows(
|
|
173
|
+
Principal(principal_entity_id=principal_id), read_perm, change.resource_id
|
|
174
|
+
):
|
|
175
|
+
# This subject's grant was revoked, but `principal_id` still
|
|
176
|
+
# reads the resource via some other grant (e.g. their own
|
|
177
|
+
# direct role) - never tombstone access they still hold.
|
|
178
|
+
continue
|
|
179
|
+
for channel_name in self._channels_for(principal_id):
|
|
180
|
+
await self._deliver_access_change(channel_name, change)
|
|
181
|
+
|
|
182
|
+
async def _deliver_access_change(self, channel_name: str, change: AccessChange) -> None:
|
|
183
|
+
group_name = scope_channel(change.resource_id)
|
|
184
|
+
if change.new_role is None:
|
|
185
|
+
await self.group_discard(group_name, channel_name)
|
|
186
|
+
await self.send(channel_name, {
|
|
187
|
+
'type': 'access_revoked', 'resource_id': change.resource_id,
|
|
188
|
+
})
|
|
189
|
+
elif change.old_role is None:
|
|
190
|
+
# Join before notify, so no save lands in the gap between the
|
|
191
|
+
# notification and the subscription taking effect.
|
|
192
|
+
await self.group_add(group_name, channel_name)
|
|
193
|
+
await self.send(channel_name, {
|
|
194
|
+
'type': 'access_granted', 'resource_id': change.resource_id,
|
|
195
|
+
'role': change.new_role.name,
|
|
196
|
+
})
|
|
197
|
+
else:
|
|
198
|
+
await self.send(channel_name, {
|
|
199
|
+
'type': 'access_changed', 'resource_id': change.resource_id,
|
|
200
|
+
'role': change.new_role.name,
|
|
201
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""`Broadcaster` over Django Channels. The only kernel module besides
|
|
2
|
+
`transport/` and `runtime.py` that imports `channels`."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from graphcore.broadcast.broadcaster import Broadcaster, owner_channel, scope_channel
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
__all__ = ['ChannelsBroadcaster', 'owner_channel', 'scope_channel']
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ChannelsBroadcaster(Broadcaster):
|
|
15
|
+
"""The four primitives over a Channels layer, best-effort: a
|
|
16
|
+
disconnected/unavailable layer must never surface as a failure to the
|
|
17
|
+
caller of a save/delete. `channel_layer` is `None` when unavailable -
|
|
18
|
+
resolved by the composition root (`graphcore.runtime`), never here."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, access_adapter, registry, channel_layer):
|
|
21
|
+
super().__init__(access_adapter, registry)
|
|
22
|
+
self.channel_layer = channel_layer
|
|
23
|
+
|
|
24
|
+
async def group_send(self, group_name: str, message: dict) -> None:
|
|
25
|
+
if self.channel_layer is None:
|
|
26
|
+
return
|
|
27
|
+
try:
|
|
28
|
+
await self.channel_layer.group_send(group_name, message)
|
|
29
|
+
except (RuntimeError, AttributeError, ConnectionError):
|
|
30
|
+
logger.debug("Broadcast to %s skipped (no active connection)", group_name)
|
|
31
|
+
except Exception:
|
|
32
|
+
logger.exception("Unexpected error broadcasting to %s", group_name)
|
|
33
|
+
|
|
34
|
+
async def group_add(self, group_name: str, channel_name: str) -> None:
|
|
35
|
+
if self.channel_layer is None:
|
|
36
|
+
return
|
|
37
|
+
try:
|
|
38
|
+
await self.channel_layer.group_add(group_name, channel_name)
|
|
39
|
+
except Exception:
|
|
40
|
+
logger.exception("Unexpected error joining %s to %s", channel_name, group_name)
|
|
41
|
+
|
|
42
|
+
async def group_discard(self, group_name: str, channel_name: str) -> None:
|
|
43
|
+
if self.channel_layer is None:
|
|
44
|
+
return
|
|
45
|
+
try:
|
|
46
|
+
await self.channel_layer.group_discard(group_name, channel_name)
|
|
47
|
+
except Exception:
|
|
48
|
+
logger.exception("Unexpected error removing %s from %s", channel_name, group_name)
|
|
49
|
+
|
|
50
|
+
async def send(self, channel_name: str, message: dict) -> None:
|
|
51
|
+
if self.channel_layer is None:
|
|
52
|
+
return
|
|
53
|
+
try:
|
|
54
|
+
await self.channel_layer.send(channel_name, message)
|
|
55
|
+
except Exception:
|
|
56
|
+
logger.exception("Unexpected error sending to channel %s", channel_name)
|
|
File without changes
|