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,391 @@
|
|
|
1
|
+
"""The creation rule, the move rule, and `creatable` - see
|
|
2
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement, Creation)."""
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from asgiref.sync import async_to_sync
|
|
6
|
+
from django.test import TransactionTestCase
|
|
7
|
+
|
|
8
|
+
from graphcore.auth.authz.lookups import find_grant, find_group_by_key
|
|
9
|
+
from graphcore.bootstrap.deployment import ensure_deployment
|
|
10
|
+
from graphcore.auth.authz.defaults import ADMINS_GROUP_KEY
|
|
11
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
12
|
+
from graphcore.domain.entity.Entity import Entity
|
|
13
|
+
from graphcore.domain.entity.group.Group import Group
|
|
14
|
+
from graphcore.domain.entity.user.User import User
|
|
15
|
+
from graphcore.domain.strategy.BaseStrategy import (
|
|
16
|
+
CreateEntity, DeleteEntity, ReparentChild, SetAttributes, Strategy,
|
|
17
|
+
)
|
|
18
|
+
from graphcore.domain.strategy.DeploymentStrategies import SetNewUserDefaults
|
|
19
|
+
from graphcore.domain.strategy.GroupStrategies import AddMember, CreateGroup
|
|
20
|
+
from graphcore.errors import Forbidden, InvalidRequest
|
|
21
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
22
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
23
|
+
from graphcore.runtime import django_runtime
|
|
24
|
+
from graphcore.types.access import Create, CreatePermission, Read, Role
|
|
25
|
+
from graphcore.types.identity import PUBLIC
|
|
26
|
+
from graphcore.types.identity import Principal
|
|
27
|
+
from graphcore.types.query import Equal
|
|
28
|
+
from graphcore.tests.runs import run
|
|
29
|
+
|
|
30
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _Note(Entity):
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class _Workspace(Entity):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class CreateWorkspace(CreatePermission):
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
WORKSPACE_CREATOR = Role('test.workspace_creator', perms=frozenset({Read(), Create(), CreateWorkspace()}))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _UndeclaredDeployment(Strategy):
|
|
50
|
+
"""Registered against the deployment with no declared permission."""
|
|
51
|
+
strategy_description = "Does nothing"
|
|
52
|
+
|
|
53
|
+
async def apply(self) -> tuple:
|
|
54
|
+
return self.strategy_request, self.entity
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def request_constructor(cls, target_entity_id):
|
|
58
|
+
return cls._build_request(target_entity_id=target_entity_id, param_config={})
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
TEST_REGISTRY.entities.register_entity(_Note, app_label='graphcore')
|
|
62
|
+
TEST_REGISTRY.entities.register_entity(_Workspace, app_label='graphcore')
|
|
63
|
+
TEST_REGISTRY.policy.define(_Workspace, shareable=True, create=CreateWorkspace())
|
|
64
|
+
TEST_REGISTRY.strategies.register_strategy('Deployment', _UndeclaredDeployment)
|
|
65
|
+
TEST_REGISTRY.policy.extend(Deployment, roles=[WORKSPACE_CREATOR])
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _DeploymentFixture(TransactionTestCase):
|
|
69
|
+
def setUp(self):
|
|
70
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
71
|
+
self.service = self.runtime.entity_service
|
|
72
|
+
self.executor = self.runtime.executor
|
|
73
|
+
self.deployment_id = self.runtime.deployment_entity_id
|
|
74
|
+
|
|
75
|
+
async def _user(self, username):
|
|
76
|
+
user = self.service.create(User, username=username)
|
|
77
|
+
with kernel_internal_context():
|
|
78
|
+
await self.service.save_entity(user, Principal(principal_entity_id=user.entity_id))
|
|
79
|
+
return Principal(principal_entity_id=user.entity_id)
|
|
80
|
+
|
|
81
|
+
async def _seed(self):
|
|
82
|
+
await ensure_deployment(self.service, TEST_REGISTRY)
|
|
83
|
+
self.member = await self._user('top-level-member')
|
|
84
|
+
self.admin = await self._user('top-level-admin')
|
|
85
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
86
|
+
admins.add_member(self.admin.principal_entity_id)
|
|
87
|
+
with kernel_internal_context():
|
|
88
|
+
await self.service.save_entity(admins, None)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class CreationTests(_DeploymentFixture):
|
|
92
|
+
async def _create_note(self, principal):
|
|
93
|
+
request = CreateEntity.request_constructor(
|
|
94
|
+
target_entity_id=None,
|
|
95
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(_Note),
|
|
96
|
+
)
|
|
97
|
+
request, _ = await run(self.executor, request, principal=principal)
|
|
98
|
+
return await self.service.repository.load(request.ret_val['child_entity'])
|
|
99
|
+
|
|
100
|
+
async def _revoke_public_member(self):
|
|
101
|
+
public_grant = await find_grant(self.service, self.deployment_id, PUBLIC)
|
|
102
|
+
with kernel_internal_context():
|
|
103
|
+
await self.service.delete_entities([public_grant.entity_id], self.admin)
|
|
104
|
+
|
|
105
|
+
@async_to_sync
|
|
106
|
+
async def test_member_creates_a_top_level_entity(self):
|
|
107
|
+
await self._seed()
|
|
108
|
+
note = await self._create_note(self.member)
|
|
109
|
+
self.assertIsNone(note.parent_id)
|
|
110
|
+
self.assertEqual(note.owner, self.member.principal_entity_id)
|
|
111
|
+
deployment = await self.service.repository.load(self.deployment_id)
|
|
112
|
+
self.assertNotIn(note.entity_id, deployment.children_ids)
|
|
113
|
+
|
|
114
|
+
@async_to_sync
|
|
115
|
+
async def test_revoking_the_public_member_grant_locks_creation(self):
|
|
116
|
+
await self._seed()
|
|
117
|
+
await self._revoke_public_member()
|
|
118
|
+
with self.assertRaises(Forbidden):
|
|
119
|
+
await self._create_note(self.member)
|
|
120
|
+
|
|
121
|
+
await self.runtime.graph_api.grant(self.admin, self.deployment_id, self.member.principal_entity_id, 'member')
|
|
122
|
+
self.assertIsNone((await self._create_note(self.member)).parent_id)
|
|
123
|
+
|
|
124
|
+
@async_to_sync
|
|
125
|
+
async def test_strategies_off_the_allowlist_cannot_target_the_deployment_even_for_admins(self):
|
|
126
|
+
await self._seed()
|
|
127
|
+
for request in (
|
|
128
|
+
_UndeclaredDeployment.request_constructor(self.deployment_id),
|
|
129
|
+
DeleteEntity.request_constructor(target_entity_id=self.deployment_id),
|
|
130
|
+
SetAttributes.request_constructor(target_entity_id=self.deployment_id, attribute_map={'x': 1}),
|
|
131
|
+
):
|
|
132
|
+
with self.subTest(strategy=request.strategy_name), self.assertRaises(Forbidden):
|
|
133
|
+
await run(self.executor, request, principal=self.admin)
|
|
134
|
+
self.assertIsNotNone(await self.service.repository.load(self.deployment_id))
|
|
135
|
+
|
|
136
|
+
@async_to_sync
|
|
137
|
+
async def test_only_strategies_that_run_without_a_target_accept_none(self):
|
|
138
|
+
await self._seed()
|
|
139
|
+
request = SetAttributes.request_constructor(target_entity_id=None, attribute_map={'x': 1})
|
|
140
|
+
with self.assertRaisesMessage(InvalidRequest, 'target_entity_id is required'):
|
|
141
|
+
await run(self.executor, request, principal=self.admin)
|
|
142
|
+
|
|
143
|
+
async def _create(self, principal, entity_cls, parent_id=None):
|
|
144
|
+
request = CreateEntity.request_constructor(
|
|
145
|
+
target_entity_id=parent_id,
|
|
146
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(entity_cls),
|
|
147
|
+
)
|
|
148
|
+
request, _ = await run(self.executor, request, principal=principal)
|
|
149
|
+
return await self.service.repository.load(request.ret_val['child_entity'])
|
|
150
|
+
|
|
151
|
+
@async_to_sync
|
|
152
|
+
async def test_type_create_permission_is_needed_at_top_level_and_nested(self):
|
|
153
|
+
await self._seed()
|
|
154
|
+
with self.assertRaises(Forbidden):
|
|
155
|
+
await self._create(self.member, _Workspace)
|
|
156
|
+
note = await self._create(self.member, _Note)
|
|
157
|
+
with self.assertRaises(Forbidden):
|
|
158
|
+
await self._create(self.member, _Workspace, parent_id=note.entity_id)
|
|
159
|
+
|
|
160
|
+
await self.runtime.graph_api.grant(
|
|
161
|
+
self.admin, self.deployment_id, self.member.principal_entity_id, 'test.workspace_creator',
|
|
162
|
+
)
|
|
163
|
+
workspace = await self._create(self.member, _Workspace)
|
|
164
|
+
self.assertIsNone(workspace.parent_id)
|
|
165
|
+
child = await self._create(self.member, _Note, parent_id=workspace.entity_id)
|
|
166
|
+
self.assertEqual(child.parent_id, workspace.entity_id)
|
|
167
|
+
|
|
168
|
+
@async_to_sync
|
|
169
|
+
async def test_a_viewer_cannot_create_inside_and_an_editor_can(self):
|
|
170
|
+
await self._seed()
|
|
171
|
+
workspace_owner = await self._user('workspace-owner')
|
|
172
|
+
await self.runtime.graph_api.grant(
|
|
173
|
+
self.admin, self.deployment_id, workspace_owner.principal_entity_id, 'test.workspace_creator',
|
|
174
|
+
)
|
|
175
|
+
workspace = await self._create(workspace_owner, _Workspace)
|
|
176
|
+
viewer, editor = await self._user('viewer'), await self._user('editor')
|
|
177
|
+
await self.runtime.graph_api.grant(workspace_owner, workspace.entity_id, viewer.principal_entity_id, 'viewer')
|
|
178
|
+
await self.runtime.graph_api.grant(workspace_owner, workspace.entity_id, editor.principal_entity_id, 'editor')
|
|
179
|
+
|
|
180
|
+
with self.assertRaises(Forbidden):
|
|
181
|
+
await self._create(viewer, _Note, parent_id=workspace.entity_id)
|
|
182
|
+
self.assertEqual((await self._create(editor, _Note, parent_id=workspace.entity_id)).parent_id, workspace.entity_id)
|
|
183
|
+
|
|
184
|
+
async def _move(self, principal, entity_id, parent_id):
|
|
185
|
+
request = ReparentChild.request_constructor(entity_id, parent_id)
|
|
186
|
+
await run(self.executor, request, principal=principal)
|
|
187
|
+
|
|
188
|
+
@async_to_sync
|
|
189
|
+
async def test_an_entity_moves_only_where_it_could_have_been_created(self):
|
|
190
|
+
await self._seed()
|
|
191
|
+
await self.runtime.graph_api.grant(
|
|
192
|
+
self.admin, self.deployment_id, self.member.principal_entity_id, 'test.workspace_creator',
|
|
193
|
+
)
|
|
194
|
+
workspace = await self._create(self.member, _Workspace)
|
|
195
|
+
note = await self._create(self.member, _Note)
|
|
196
|
+
with self.assertRaisesMessage(Forbidden, 'CreateWorkspace'):
|
|
197
|
+
await self._move(self.member, workspace.entity_id, note.entity_id)
|
|
198
|
+
|
|
199
|
+
await self._move(self.member, note.entity_id, workspace.entity_id)
|
|
200
|
+
self.assertEqual((await self.service.repository.load(note.entity_id)).parent_id, workspace.entity_id)
|
|
201
|
+
|
|
202
|
+
@async_to_sync
|
|
203
|
+
async def test_moving_to_the_top_level_needs_create_on_the_deployment(self):
|
|
204
|
+
await self._seed()
|
|
205
|
+
parent = await self._create(self.member, _Note)
|
|
206
|
+
child = await self._create(self.member, _Note, parent_id=parent.entity_id)
|
|
207
|
+
await self._revoke_public_member()
|
|
208
|
+
with self.assertRaises(Forbidden):
|
|
209
|
+
await self._move(self.member, child.entity_id, None)
|
|
210
|
+
|
|
211
|
+
@async_to_sync
|
|
212
|
+
async def test_member_creates_a_group_but_cannot_key_or_admin_manage_it(self):
|
|
213
|
+
await self._seed()
|
|
214
|
+
request, _ = await run(self.executor,
|
|
215
|
+
CreateGroup.request_constructor(None, name='Team'), principal=self.member,
|
|
216
|
+
)
|
|
217
|
+
group = await self.service.repository.load(request.ret_val['group_entity_id'])
|
|
218
|
+
self.assertIsInstance(group, Group)
|
|
219
|
+
self.assertIsNone(group.parent_id)
|
|
220
|
+
self.assertEqual(group.member_ids, [self.member.principal_entity_id])
|
|
221
|
+
|
|
222
|
+
for kwargs in ({'key': 'test.team'}, {'admin_managed': True}):
|
|
223
|
+
with self.assertRaises(Forbidden):
|
|
224
|
+
await run(self.executor,
|
|
225
|
+
CreateGroup.request_constructor(None, name='X', **kwargs),
|
|
226
|
+
principal=self.member,
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
group_ids = await self.service.repository.list_ids(Equal('entity_type_id', Group.get_entity_type_id()))
|
|
230
|
+
names = [g.name for g in (await self.service.repository.load_many(group_ids)).values()]
|
|
231
|
+
self.assertNotIn('X', names)
|
|
232
|
+
|
|
233
|
+
@async_to_sync
|
|
234
|
+
async def test_admin_managed_group_is_administered_by_admins(self):
|
|
235
|
+
await self._seed()
|
|
236
|
+
request, _ = await run(self.executor,
|
|
237
|
+
CreateGroup.request_constructor(
|
|
238
|
+
None, name='Staff', key='test.staff', admin_managed=True,
|
|
239
|
+
),
|
|
240
|
+
principal=self.admin,
|
|
241
|
+
)
|
|
242
|
+
group_id = request.ret_val['group_entity_id']
|
|
243
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
244
|
+
self.assertEqual((await find_grant(self.service, group_id, admins.entity_id)).role_name, 'admin')
|
|
245
|
+
self.assertEqual((await self.service.repository.load(group_id)).key, 'test.staff')
|
|
246
|
+
|
|
247
|
+
@async_to_sync
|
|
248
|
+
async def test_generic_create_cannot_target_a_group(self):
|
|
249
|
+
await self._seed()
|
|
250
|
+
request, _ = await run(self.executor,
|
|
251
|
+
CreateGroup.request_constructor(None, name='Team'), principal=self.member,
|
|
252
|
+
)
|
|
253
|
+
create_under_group = CreateEntity.request_constructor(
|
|
254
|
+
target_entity_id=request.ret_val['group_entity_id'],
|
|
255
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(_Note),
|
|
256
|
+
)
|
|
257
|
+
with self.assertRaises(Forbidden):
|
|
258
|
+
await run(self.executor, create_under_group, principal=self.member)
|
|
259
|
+
|
|
260
|
+
async def _workspace_with(self, **roles):
|
|
261
|
+
owner = await self._user('group-workspace-owner')
|
|
262
|
+
await self.runtime.graph_api.grant(
|
|
263
|
+
self.admin, self.deployment_id, owner.principal_entity_id, 'test.workspace_creator',
|
|
264
|
+
)
|
|
265
|
+
workspace = await self._create(owner, _Workspace)
|
|
266
|
+
principals = {}
|
|
267
|
+
for role in roles:
|
|
268
|
+
principals[role] = await self._user(f'group-workspace-{role}')
|
|
269
|
+
await self.runtime.graph_api.grant(owner, workspace.entity_id, principals[role].principal_entity_id, role)
|
|
270
|
+
return owner, workspace, principals
|
|
271
|
+
|
|
272
|
+
async def _create_group(self, principal, target_id, **kwargs):
|
|
273
|
+
request, _ = await run(self.executor,
|
|
274
|
+
CreateGroup.request_constructor(target_id, **kwargs), principal=principal,
|
|
275
|
+
)
|
|
276
|
+
return request.ret_val['group_entity_id']
|
|
277
|
+
|
|
278
|
+
@async_to_sync
|
|
279
|
+
async def test_an_editor_creates_a_group_inside_a_workspace(self):
|
|
280
|
+
await self._seed()
|
|
281
|
+
_, workspace, users = await self._workspace_with(editor=True)
|
|
282
|
+
group_id = await self._create_group(users['editor'], workspace.entity_id, name='Workspace team')
|
|
283
|
+
group = await self.service.repository.load(group_id)
|
|
284
|
+
self.assertEqual(group.parent_id, workspace.entity_id)
|
|
285
|
+
self.assertEqual(group.member_ids, [users['editor'].principal_entity_id])
|
|
286
|
+
|
|
287
|
+
@async_to_sync
|
|
288
|
+
async def test_the_workspace_admin_manages_its_group_and_a_viewer_only_reads_it(self):
|
|
289
|
+
await self._seed()
|
|
290
|
+
owner, workspace, users = await self._workspace_with(editor=True, viewer=True)
|
|
291
|
+
group_id = await self._create_group(users['editor'], workspace.entity_id, name='Workspace team')
|
|
292
|
+
|
|
293
|
+
await run(self.executor,
|
|
294
|
+
AddMember.request_constructor(group_id, users['viewer'].principal_entity_id), principal=owner,
|
|
295
|
+
)
|
|
296
|
+
self.assertIn(users['viewer'].principal_entity_id, (await self.service.repository.load(group_id)).member_ids)
|
|
297
|
+
|
|
298
|
+
self.assertIsInstance(await self.service.get_entity(group_id, users['viewer']), Group)
|
|
299
|
+
with self.assertRaises(Forbidden):
|
|
300
|
+
await run(self.executor,
|
|
301
|
+
AddMember.request_constructor(group_id, self.member.principal_entity_id),
|
|
302
|
+
principal=users['viewer'],
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
@async_to_sync
|
|
306
|
+
async def test_a_group_key_needs_a_deployment_administrator_whatever_the_parent(self):
|
|
307
|
+
await self._seed()
|
|
308
|
+
owner, workspace, _ = await self._workspace_with()
|
|
309
|
+
with self.assertRaisesMessage(Forbidden, 'ManageAccess() on the deployment to set key'):
|
|
310
|
+
await self._create_group(owner, workspace.entity_id, name='Keyed', key='test.keyed')
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
class NewUserDefaultsTests(_DeploymentFixture):
|
|
314
|
+
async def _set(self, principal, group_ids, roles):
|
|
315
|
+
return await run(self.executor,
|
|
316
|
+
SetNewUserDefaults.request_constructor(self.deployment_id, group_ids, roles),
|
|
317
|
+
principal=principal,
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
@async_to_sync
|
|
321
|
+
async def test_admin_sets_defaults(self):
|
|
322
|
+
await self._seed()
|
|
323
|
+
admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
324
|
+
await self._set(self.admin, [admins.entity_id], ['member'])
|
|
325
|
+
deployment = await self.service.repository.load(self.deployment_id)
|
|
326
|
+
self.assertEqual(deployment.new_user_group_ids, [admins.entity_id])
|
|
327
|
+
self.assertEqual(deployment.new_user_roles, ['member'])
|
|
328
|
+
|
|
329
|
+
@async_to_sync
|
|
330
|
+
async def test_member_cannot_set_defaults(self):
|
|
331
|
+
await self._seed()
|
|
332
|
+
with self.assertRaises(Forbidden):
|
|
333
|
+
await self._set(self.member, [], ['member'])
|
|
334
|
+
|
|
335
|
+
@async_to_sync
|
|
336
|
+
async def test_invalid_group_or_role_is_rejected(self):
|
|
337
|
+
await self._seed()
|
|
338
|
+
with self.assertRaises(InvalidRequest):
|
|
339
|
+
await self._set(self.admin, [self.deployment_id], [])
|
|
340
|
+
with self.assertRaises(InvalidRequest):
|
|
341
|
+
await self._set(self.admin, [], ['no-such-role'])
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
class DeploymentPolicyTests(_DeploymentFixture):
|
|
345
|
+
@async_to_sync
|
|
346
|
+
async def test_the_deployment_offers_no_content_roles(self):
|
|
347
|
+
await self._seed()
|
|
348
|
+
names = [role.name for role in TEST_REGISTRY.policy.available_roles(Deployment)]
|
|
349
|
+
self.assertEqual(names[:2], ['member', 'admin'])
|
|
350
|
+
self.assertNotIn('viewer', names)
|
|
351
|
+
self.assertNotIn('editor', names)
|
|
352
|
+
with self.assertRaises(InvalidRequest):
|
|
353
|
+
await self.runtime.graph_api.grant(
|
|
354
|
+
self.admin, self.deployment_id, self.member.principal_entity_id, 'editor',
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
@async_to_sync
|
|
358
|
+
async def test_generic_create_cannot_make_an_uncreatable_type(self):
|
|
359
|
+
await self._seed()
|
|
360
|
+
for entity_cls in (Deployment, User):
|
|
361
|
+
request = CreateEntity.request_constructor(
|
|
362
|
+
target_entity_id=None,
|
|
363
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(entity_cls),
|
|
364
|
+
)
|
|
365
|
+
with self.subTest(type=entity_cls.__name__), self.assertRaisesMessage(Forbidden, 'kernel code'):
|
|
366
|
+
await run(self.executor, request, principal=self.admin)
|
|
367
|
+
|
|
368
|
+
@async_to_sync
|
|
369
|
+
async def test_a_creator_deletes_their_own_group(self):
|
|
370
|
+
await self._seed()
|
|
371
|
+
request, _ = await run(self.executor,
|
|
372
|
+
CreateGroup.request_constructor(None, name='Team'), principal=self.member,
|
|
373
|
+
)
|
|
374
|
+
group_id = request.ret_val['group_entity_id']
|
|
375
|
+
await run(self.executor,
|
|
376
|
+
DeleteEntity.request_constructor(target_entity_id=group_id), principal=self.member,
|
|
377
|
+
)
|
|
378
|
+
self.assertIsNone(await self.service.repository.load(group_id))
|
|
379
|
+
|
|
380
|
+
@async_to_sync
|
|
381
|
+
async def test_recreated_admins_group_is_granted_admin_on_the_deployment(self):
|
|
382
|
+
await self._seed()
|
|
383
|
+
old_admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
384
|
+
with kernel_internal_context():
|
|
385
|
+
await self.service.delete_entity_tree(old_admins.entity_id, self.admin)
|
|
386
|
+
|
|
387
|
+
await ensure_deployment(self.service, TEST_REGISTRY)
|
|
388
|
+
|
|
389
|
+
new_admins = await find_group_by_key(self.service, ADMINS_GROUP_KEY)
|
|
390
|
+
self.assertNotEqual(new_admins.entity_id, old_admins.entity_id)
|
|
391
|
+
self.assertEqual((await find_grant(self.service, self.deployment_id, new_admins.entity_id)).role_name, 'admin')
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""`define(delete=...)`: the permission the deletion chokepoint asks for on
|
|
2
|
+
the root - see docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement)."""
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.test import TransactionTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.bootstrap.deployment import ensure_deployment
|
|
7
|
+
from graphcore.domain.entity.Entity import Entity
|
|
8
|
+
from graphcore.domain.entity.user.User import User
|
|
9
|
+
from graphcore.domain.strategy.BaseStrategy import CreateEntity, DeleteEntity
|
|
10
|
+
from graphcore.errors import Forbidden
|
|
11
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
12
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
13
|
+
from graphcore.runtime import django_runtime
|
|
14
|
+
from graphcore.types.access import ManageAccess
|
|
15
|
+
from graphcore.types.identity import Principal
|
|
16
|
+
from graphcore.tests.runs import run
|
|
17
|
+
|
|
18
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _Guarded(Entity):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _Plain(Entity):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
TEST_REGISTRY.entities.register_entity(_Guarded, app_label='graphcore')
|
|
30
|
+
TEST_REGISTRY.entities.register_entity(_Plain, app_label='graphcore')
|
|
31
|
+
TEST_REGISTRY.policy.define(_Guarded, shareable=True, delete=ManageAccess())
|
|
32
|
+
TEST_REGISTRY.policy.define(_Plain, shareable=True)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class DeletePermissionTests(TransactionTestCase):
|
|
36
|
+
def setUp(self):
|
|
37
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
38
|
+
self.service = self.runtime.entity_service
|
|
39
|
+
self.executor = self.runtime.executor
|
|
40
|
+
self.deployment_id = self.runtime.deployment_entity_id
|
|
41
|
+
|
|
42
|
+
async def _user(self, username):
|
|
43
|
+
user = self.service.create(User, username=username)
|
|
44
|
+
with kernel_internal_context():
|
|
45
|
+
await self.service.save_entity(user, None)
|
|
46
|
+
return Principal(principal_entity_id=user.entity_id)
|
|
47
|
+
|
|
48
|
+
async def _seed(self, entity_cls):
|
|
49
|
+
await ensure_deployment(self.service, TEST_REGISTRY)
|
|
50
|
+
self.owner = await self._user('delete-owner')
|
|
51
|
+
self.editor = await self._user('delete-editor')
|
|
52
|
+
request = CreateEntity.request_constructor(
|
|
53
|
+
target_entity_id=None,
|
|
54
|
+
entity_type_id=TEST_REGISTRY.entities.get_entity_type_id(entity_cls),
|
|
55
|
+
)
|
|
56
|
+
request, _ = await run(self.executor, request, principal=self.owner)
|
|
57
|
+
entity_id = request.ret_val['child_entity']
|
|
58
|
+
await self.runtime.graph_api.grant(self.owner, entity_id, self.editor.principal_entity_id, 'editor')
|
|
59
|
+
return entity_id
|
|
60
|
+
|
|
61
|
+
async def _delete(self, entity_id, principal):
|
|
62
|
+
request = DeleteEntity.request_constructor(target_entity_id=entity_id)
|
|
63
|
+
await run(self.executor, request, principal=principal)
|
|
64
|
+
|
|
65
|
+
@async_to_sync
|
|
66
|
+
async def test_an_editor_cannot_delete_a_type_that_needs_manage_access(self):
|
|
67
|
+
entity_id = await self._seed(_Guarded)
|
|
68
|
+
with self.assertRaisesMessage(Forbidden, 'ManageAccess'):
|
|
69
|
+
await self._delete(entity_id, self.editor)
|
|
70
|
+
self.assertIsNotNone(await self.service.repository.load(entity_id))
|
|
71
|
+
|
|
72
|
+
@async_to_sync
|
|
73
|
+
async def test_the_owner_deletes_a_type_that_needs_manage_access(self):
|
|
74
|
+
entity_id = await self._seed(_Guarded)
|
|
75
|
+
await self._delete(entity_id, self.owner)
|
|
76
|
+
self.assertIsNone(await self.service.repository.load(entity_id))
|
|
77
|
+
|
|
78
|
+
@async_to_sync
|
|
79
|
+
async def test_a_default_type_needs_only_delete(self):
|
|
80
|
+
entity_id = await self._seed(_Plain)
|
|
81
|
+
await self._delete(entity_id, self.editor)
|
|
82
|
+
self.assertIsNone(await self.service.repository.load(entity_id))
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""`EntityFilter.granted_to_me`: list what is shared with the caller - grants
|
|
2
|
+
to them or a group they belong to, never grants to PUBLIC."""
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.test import SimpleTestCase, TransactionTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.domain.entity.Entity import Entity
|
|
7
|
+
from graphcore.domain.entity.group.Group import Group
|
|
8
|
+
from graphcore.domain.entity.user.User import User
|
|
9
|
+
from graphcore.errors import InvalidQuery
|
|
10
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
11
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
12
|
+
from graphcore.runtime import django_runtime
|
|
13
|
+
from graphcore.types.entities import EntityFilter
|
|
14
|
+
from graphcore.types.identity import PUBLIC, Principal
|
|
15
|
+
from graphcore.types.query import Equal, Predicate
|
|
16
|
+
|
|
17
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _Shelf(Entity):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _Board(Entity):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
for _cls in (_Shelf, _Board):
|
|
29
|
+
TEST_REGISTRY.entities.register_entity(_cls, app_label='graphcore')
|
|
30
|
+
TEST_REGISTRY.policy.define(_cls, shareable=True)
|
|
31
|
+
|
|
32
|
+
SHELF = TEST_REGISTRY.entities.get_entity_type_id(_Shelf)
|
|
33
|
+
BOARD = TEST_REGISTRY.entities.get_entity_type_id(_Board)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class FromFilterTests(SimpleTestCase):
|
|
37
|
+
def test_granted_to_me_is_never_compiled_without_the_caller(self):
|
|
38
|
+
with self.assertRaises(InvalidQuery):
|
|
39
|
+
Predicate.from_filter(EntityFilter(type_ids=frozenset({SHELF}), granted_to_me=True))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class GrantedToMeTests(TransactionTestCase):
|
|
43
|
+
OWNER = 'granted-owner'
|
|
44
|
+
|
|
45
|
+
def setUp(self):
|
|
46
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
47
|
+
self.api = self.runtime.graph_api
|
|
48
|
+
self.service = self.runtime.entity_service
|
|
49
|
+
self.owner = Principal(principal_entity_id=self.OWNER)
|
|
50
|
+
|
|
51
|
+
async def _user(self, username) -> Principal:
|
|
52
|
+
user = self.service.create(User, username=username)
|
|
53
|
+
with kernel_internal_context():
|
|
54
|
+
user = await self.service.save_entity(user, None)
|
|
55
|
+
return Principal(principal_entity_id=user.entity_id)
|
|
56
|
+
|
|
57
|
+
async def _scope(self, entity_cls=_Shelf, owner=OWNER, parent_id=None):
|
|
58
|
+
scope = self.service.create(entity_cls)
|
|
59
|
+
scope.parent_id = parent_id
|
|
60
|
+
with kernel_internal_context():
|
|
61
|
+
return (await self.service.save_entity(scope, Principal(principal_entity_id=owner))).entity_id
|
|
62
|
+
|
|
63
|
+
async def _group_with(self, member: Principal) -> str:
|
|
64
|
+
group = self.service.create(Group)
|
|
65
|
+
group.name = 'Readers'
|
|
66
|
+
group.add_member(member.principal_entity_id)
|
|
67
|
+
with kernel_internal_context():
|
|
68
|
+
group = await self.service.save_entity(group, self.owner)
|
|
69
|
+
# The reverse member_ids lookup is Postgres-only (JSONB `?`); warm the
|
|
70
|
+
# adapter's cache so the scenario runs on SQLite too, as test_integration does.
|
|
71
|
+
adapter = self.service.access_adapter
|
|
72
|
+
adapter.cache.set(adapter._subject_groups_cache_key(member.principal_entity_id), [group.entity_id], timeout=300)
|
|
73
|
+
return group.entity_id
|
|
74
|
+
|
|
75
|
+
async def _ids(self, principal, type_ids=(SHELF,), **kwargs) -> set[str]:
|
|
76
|
+
page = await self.api.list_entities(
|
|
77
|
+
principal, EntityFilter(type_ids=frozenset(type_ids), granted_to_me=True, **kwargs),
|
|
78
|
+
)
|
|
79
|
+
return {e.entity_id for e in page.entities}
|
|
80
|
+
|
|
81
|
+
@async_to_sync
|
|
82
|
+
async def test_direct_and_group_grants_are_listed(self):
|
|
83
|
+
alice = await self._user('granted-alice')
|
|
84
|
+
direct = await self._scope()
|
|
85
|
+
via_group = await self._scope()
|
|
86
|
+
await self._scope()
|
|
87
|
+
await self.api.grant(self.owner, direct, alice.principal_entity_id, 'viewer')
|
|
88
|
+
await self.api.grant(self.owner, via_group, await self._group_with(alice), 'editor')
|
|
89
|
+
|
|
90
|
+
self.assertEqual(await self._ids(alice), {direct, via_group})
|
|
91
|
+
|
|
92
|
+
@async_to_sync
|
|
93
|
+
async def test_grants_to_public_are_readable_but_not_listed(self):
|
|
94
|
+
alice = await self._user('granted-public')
|
|
95
|
+
public = await self._scope()
|
|
96
|
+
await self.api.grant(self.owner, public, PUBLIC, 'viewer')
|
|
97
|
+
|
|
98
|
+
readable = await self.api.list_entities(alice, EntityFilter(type_ids=frozenset({SHELF})))
|
|
99
|
+
self.assertIn(public, {e.entity_id for e in readable.entities})
|
|
100
|
+
self.assertEqual(await self._ids(alice), set())
|
|
101
|
+
|
|
102
|
+
@async_to_sync
|
|
103
|
+
async def test_entities_reached_through_containment_are_not_listed(self):
|
|
104
|
+
alice = await self._user('granted-nested')
|
|
105
|
+
outer = await self._scope()
|
|
106
|
+
inner = await self._scope(parent_id=outer)
|
|
107
|
+
await self.api.grant(self.owner, outer, alice.principal_entity_id, 'viewer')
|
|
108
|
+
|
|
109
|
+
readable = await self.api.list_entities(alice, EntityFilter(type_ids=frozenset({SHELF})))
|
|
110
|
+
self.assertIn(inner, {e.entity_id for e in readable.entities})
|
|
111
|
+
self.assertEqual(await self._ids(alice), {outer})
|
|
112
|
+
|
|
113
|
+
@async_to_sync
|
|
114
|
+
async def test_owned_entities_are_not_listed(self):
|
|
115
|
+
alice = await self._user('granted-owns')
|
|
116
|
+
await self._scope(owner=alice.principal_entity_id)
|
|
117
|
+
self.assertEqual(await self._ids(alice), set())
|
|
118
|
+
|
|
119
|
+
@async_to_sync
|
|
120
|
+
async def test_a_revoked_grant_drops_out(self):
|
|
121
|
+
alice = await self._user('granted-revoked')
|
|
122
|
+
scope = await self._scope()
|
|
123
|
+
await self.api.grant(self.owner, scope, alice.principal_entity_id, 'viewer')
|
|
124
|
+
self.assertEqual(await self._ids(alice), {scope})
|
|
125
|
+
|
|
126
|
+
await self.api.revoke(self.owner, scope, alice.principal_entity_id)
|
|
127
|
+
self.assertEqual(await self._ids(alice), set())
|
|
128
|
+
|
|
129
|
+
@async_to_sync
|
|
130
|
+
async def test_combined_with_type_ids_and_where(self):
|
|
131
|
+
alice = await self._user('granted-combined')
|
|
132
|
+
first = await self._scope()
|
|
133
|
+
second = await self._scope()
|
|
134
|
+
board = await self._scope(_Board)
|
|
135
|
+
for resource in (first, second, board):
|
|
136
|
+
await self.api.grant(self.owner, resource, alice.principal_entity_id, 'viewer')
|
|
137
|
+
|
|
138
|
+
self.assertEqual(await self._ids(alice, type_ids=(SHELF,)), {first, second})
|
|
139
|
+
self.assertEqual(await self._ids(alice, type_ids=(SHELF, BOARD)), {first, second, board})
|
|
140
|
+
self.assertEqual(await self._ids(alice, where=Equal('entity_id', second)), {second})
|
|
141
|
+
|
|
142
|
+
@async_to_sync
|
|
143
|
+
async def test_pages_like_any_listing(self):
|
|
144
|
+
alice = await self._user('granted-pages')
|
|
145
|
+
scopes = {await self._scope() for _ in range(3)}
|
|
146
|
+
for scope in scopes:
|
|
147
|
+
await self.api.grant(self.owner, scope, alice.principal_entity_id, 'viewer')
|
|
148
|
+
|
|
149
|
+
entity_filter = EntityFilter(type_ids=frozenset({SHELF}), granted_to_me=True)
|
|
150
|
+
first = await self.api.list_entities(alice, entity_filter, limit=2)
|
|
151
|
+
self.assertEqual(len(first.entities), 2)
|
|
152
|
+
rest = await self.api.list_entities(alice, entity_filter, cursor=first.next_cursor, limit=2)
|
|
153
|
+
self.assertIsNone(rest.next_cursor)
|
|
154
|
+
self.assertEqual({e.entity_id for e in first.entities + rest.entities}, scopes)
|
|
155
|
+
|
|
156
|
+
@async_to_sync
|
|
157
|
+
async def test_nothing_granted_lists_nothing(self):
|
|
158
|
+
bob = await self._user('granted-nobody')
|
|
159
|
+
await self._scope()
|
|
160
|
+
self.assertEqual(await self._ids(bob), set())
|