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,55 @@
|
|
|
1
|
+
"""Administrator settings on the `Deployment`. See
|
|
2
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Identity-based policy, Onboarding)."""
|
|
3
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
4
|
+
from graphcore.domain.entity.group.Group import Group
|
|
5
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
6
|
+
from graphcore.errors import InvalidRequest
|
|
7
|
+
from graphcore.registry.decorators import register_strategy
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@register_strategy(Deployment)
|
|
11
|
+
class SetNewUserDefaults(Strategy):
|
|
12
|
+
"""Set the groups and deployment roles every newly created principal
|
|
13
|
+
receives. Requires ManageAccess on the deployment (kernel_policy)."""
|
|
14
|
+
strategy_description = "Sets the groups and roles new users receive"
|
|
15
|
+
|
|
16
|
+
def verify_executable(self):
|
|
17
|
+
if not isinstance(self.entity, Deployment):
|
|
18
|
+
raise ValueError("SetNewUserDefaults targets the deployment entity")
|
|
19
|
+
|
|
20
|
+
async def apply(self) -> tuple:
|
|
21
|
+
config = self.strategy_request.param_config
|
|
22
|
+
group_ids = list(dict.fromkeys(config.get('group_ids') or []))
|
|
23
|
+
roles = list(dict.fromkeys(config.get('roles') or []))
|
|
24
|
+
|
|
25
|
+
groups = await self.session.get_many(group_ids)
|
|
26
|
+
not_groups = [gid for gid in group_ids if not isinstance(groups.get(gid), Group)]
|
|
27
|
+
if not_groups:
|
|
28
|
+
raise InvalidRequest(f"Not groups: {', '.join(not_groups)}")
|
|
29
|
+
valid_roles = {role.name for role in self.registry.policy.available_roles(Deployment)}
|
|
30
|
+
unknown_roles = [role for role in roles if role not in valid_roles]
|
|
31
|
+
if unknown_roles:
|
|
32
|
+
raise InvalidRequest(f"Not deployment roles: {', '.join(unknown_roles)}")
|
|
33
|
+
|
|
34
|
+
self.entity.new_user_group_ids = group_ids
|
|
35
|
+
self.entity.new_user_roles = roles
|
|
36
|
+
self.entity = await self.save_entity(self.entity)
|
|
37
|
+
return self.strategy_request, self.entity
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def get_request_schema():
|
|
41
|
+
return {
|
|
42
|
+
"type": "object",
|
|
43
|
+
"properties": {
|
|
44
|
+
"group_ids": {"type": "array", "items": {"type": "string"}},
|
|
45
|
+
"roles": {"type": "array", "items": {"type": "string"}},
|
|
46
|
+
},
|
|
47
|
+
"required": ["group_ids", "roles"],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def request_constructor(cls, target_entity_id: str, group_ids: list, roles: list):
|
|
52
|
+
return cls._build_request(
|
|
53
|
+
target_entity_id=target_entity_id,
|
|
54
|
+
param_config={"group_ids": list(group_ids), "roles": list(roles)},
|
|
55
|
+
)
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Grant mutation: the only place `Grant` rows are created, re-roled,
|
|
2
|
+
or revoked. See docs/AUTHORIZATION_TECHNICAL_TDD.md (Grant storage).
|
|
3
|
+
|
|
4
|
+
Enforcement (Step 8) requires `ManageAccess` for all three, via
|
|
5
|
+
`auth/authz/kernel_policy.py`'s exhaustive `Grant` allowlist.
|
|
6
|
+
"""
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
from graphcore.types.identity import PUBLIC
|
|
10
|
+
from graphcore.domain.entity.Entity import Entity
|
|
11
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
12
|
+
from graphcore.domain.entity.group.Group import Group
|
|
13
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
14
|
+
from graphcore.errors import InvalidRequest, NotFound
|
|
15
|
+
from graphcore.registry.decorators import register_strategy
|
|
16
|
+
from graphcore.types.entities import EntityFilter
|
|
17
|
+
from graphcore.types.query import Equal
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _resolve_role_or_raise(registry, resource_type_id: Optional[str], role_name: str):
|
|
21
|
+
entity_cls = registry.entities.get_entity_class(resource_type_id) if resource_type_id else None
|
|
22
|
+
for role in registry.policy.available_roles(entity_cls):
|
|
23
|
+
if role.name == role_name:
|
|
24
|
+
return role
|
|
25
|
+
raise InvalidRequest(f"'{role_name}' is not a registered role for {resource_type_id}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def _validate_subject(strategy: Strategy, subject_id: str) -> None:
|
|
29
|
+
"""A grant's subject is PUBLIC, a `principal=True` type (e.g. `User`),
|
|
30
|
+
or a group (`Group` or a subclass)."""
|
|
31
|
+
if subject_id == PUBLIC:
|
|
32
|
+
return
|
|
33
|
+
try:
|
|
34
|
+
subject = await strategy.get_entity(subject_id)
|
|
35
|
+
except NotFound:
|
|
36
|
+
raise InvalidRequest(f"subject {subject_id} does not exist")
|
|
37
|
+
except (ValueError, TypeError):
|
|
38
|
+
raise InvalidRequest(f"subject {subject_id} does not exist")
|
|
39
|
+
if strategy.registry.entities.is_principal(subject.entity_type_id):
|
|
40
|
+
return
|
|
41
|
+
if isinstance(subject, Group):
|
|
42
|
+
return
|
|
43
|
+
raise InvalidRequest(f"subject {subject_id} is not a principal or a group")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def _find_existing_grant(strategy: Strategy, resource, subject_id: str) -> Optional[Grant]:
|
|
47
|
+
page = await strategy.session.list(EntityFilter(
|
|
48
|
+
type_ids=frozenset({Grant.get_entity_type_id()}),
|
|
49
|
+
parent_id=resource.entity_id,
|
|
50
|
+
where=Equal('subject_id', subject_id),
|
|
51
|
+
), limit=1)
|
|
52
|
+
return next(iter(page.entities), None)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@register_strategy(Entity)
|
|
56
|
+
class CreateGrant(Strategy):
|
|
57
|
+
"""Grant `role_name` to `subject_id` on the target resource.
|
|
58
|
+
|
|
59
|
+
Replace semantics: an existing grant for the (resource, subject) pair
|
|
60
|
+
gets its role updated in place instead of a second row.
|
|
61
|
+
"""
|
|
62
|
+
strategy_description = 'Grants a role on a resource to a subject, creating or replacing the grant'
|
|
63
|
+
|
|
64
|
+
async def apply(self) -> tuple:
|
|
65
|
+
resource = self.entity
|
|
66
|
+
config = self.strategy_request.param_config
|
|
67
|
+
subject_id = config['subject_id']
|
|
68
|
+
role_name = config['role_name']
|
|
69
|
+
|
|
70
|
+
if not self.registry.policy.is_shareable(type(resource)):
|
|
71
|
+
raise InvalidRequest(f"{resource.entity_type_id} is not shareable - cannot hold grants")
|
|
72
|
+
|
|
73
|
+
role = _resolve_role_or_raise(self.registry, resource.entity_type_id, role_name)
|
|
74
|
+
await _validate_subject(self, subject_id)
|
|
75
|
+
|
|
76
|
+
existing = await _find_existing_grant(self, resource, subject_id)
|
|
77
|
+
if existing is not None:
|
|
78
|
+
existing.role_name = role.name
|
|
79
|
+
existing = await self.save_entity(existing)
|
|
80
|
+
self.strategy_request.ret_val['grant_entity_id'] = existing.entity_id
|
|
81
|
+
return self.strategy_request, resource
|
|
82
|
+
|
|
83
|
+
grant = self.session.create(Grant)
|
|
84
|
+
grant.subject_id = subject_id
|
|
85
|
+
grant.role_name = role.name
|
|
86
|
+
grant.set_parent(resource.entity_id)
|
|
87
|
+
grant = await self.save_entity(grant)
|
|
88
|
+
|
|
89
|
+
self.strategy_request.ret_val['grant_entity_id'] = grant.entity_id
|
|
90
|
+
return self.strategy_request, resource
|
|
91
|
+
|
|
92
|
+
@staticmethod
|
|
93
|
+
def get_request_schema():
|
|
94
|
+
return {
|
|
95
|
+
"type": "object",
|
|
96
|
+
"properties": {
|
|
97
|
+
"subject_id": {"type": "string", "description": "A principal, group, or PUBLIC ('*') id"},
|
|
98
|
+
"role_name": {"type": "string", "description": "A role registered for the resource's type"},
|
|
99
|
+
},
|
|
100
|
+
"required": ["subject_id", "role_name"],
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@classmethod
|
|
104
|
+
def request_constructor(
|
|
105
|
+
cls,
|
|
106
|
+
target_entity_id: str,
|
|
107
|
+
subject_id: str,
|
|
108
|
+
role_name: str,
|
|
109
|
+
):
|
|
110
|
+
return cls._build_request(
|
|
111
|
+
target_entity_id=target_entity_id,
|
|
112
|
+
param_config={"subject_id": subject_id, "role_name": role_name},
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@register_strategy(Grant)
|
|
117
|
+
class ChangeGrantRole(Strategy):
|
|
118
|
+
"""Change the role on an existing grant. Targets the `Grant` itself."""
|
|
119
|
+
strategy_description = "Changes the role on an existing grant"
|
|
120
|
+
|
|
121
|
+
def verify_executable(self):
|
|
122
|
+
if not isinstance(self.entity, Grant):
|
|
123
|
+
raise ValueError(f"Entity {self.entity.entity_id} is not a Grant")
|
|
124
|
+
|
|
125
|
+
async def apply(self) -> tuple:
|
|
126
|
+
grant: Grant = self.entity
|
|
127
|
+
role_name = self.strategy_request.param_config['role_name']
|
|
128
|
+
|
|
129
|
+
resource = await self.get_entity(grant.parent_id)
|
|
130
|
+
role = _resolve_role_or_raise(self.registry, resource.entity_type_id, role_name)
|
|
131
|
+
|
|
132
|
+
grant.role_name = role.name
|
|
133
|
+
grant = await self.save_entity(grant)
|
|
134
|
+
|
|
135
|
+
self.strategy_request.ret_val['role_name'] = grant.role_name
|
|
136
|
+
return self.strategy_request, grant
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def get_request_schema():
|
|
140
|
+
return {
|
|
141
|
+
"type": "object",
|
|
142
|
+
"properties": {
|
|
143
|
+
"role_name": {"type": "string", "description": "A role registered for the resource's type"},
|
|
144
|
+
},
|
|
145
|
+
"required": ["role_name"],
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
@classmethod
|
|
149
|
+
def request_constructor(
|
|
150
|
+
cls,
|
|
151
|
+
target_entity_id: str,
|
|
152
|
+
role_name: str,
|
|
153
|
+
):
|
|
154
|
+
return cls._build_request(
|
|
155
|
+
target_entity_id=target_entity_id,
|
|
156
|
+
param_config={"role_name": role_name},
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@register_strategy(Grant)
|
|
161
|
+
class RevokeGrant(Strategy):
|
|
162
|
+
"""Delete a grant. Targets the `Grant` itself."""
|
|
163
|
+
strategy_description = "Revokes an existing grant"
|
|
164
|
+
|
|
165
|
+
def verify_executable(self):
|
|
166
|
+
if not isinstance(self.entity, Grant):
|
|
167
|
+
raise ValueError(f"Entity {self.entity.entity_id} is not a Grant")
|
|
168
|
+
|
|
169
|
+
async def apply(self) -> tuple:
|
|
170
|
+
grant: Grant = self.entity
|
|
171
|
+
|
|
172
|
+
await self.session.delete_tree(grant.entity_id)
|
|
173
|
+
|
|
174
|
+
self.strategy_request.ret_val['revoked_grant_entity_id'] = grant.entity_id
|
|
175
|
+
return self.strategy_request, grant
|
|
176
|
+
|
|
177
|
+
@staticmethod
|
|
178
|
+
def get_request_schema():
|
|
179
|
+
return {"type": "object", "properties": {}, "required": []}
|
|
180
|
+
|
|
181
|
+
@classmethod
|
|
182
|
+
def request_constructor(
|
|
183
|
+
cls,
|
|
184
|
+
target_entity_id: str,
|
|
185
|
+
):
|
|
186
|
+
return cls._build_request(
|
|
187
|
+
target_entity_id=target_entity_id,
|
|
188
|
+
param_config={},
|
|
189
|
+
)
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""`Group` roster and identity mutation.
|
|
2
|
+
|
|
3
|
+
See docs/AUTHORIZATION_TECHNICAL_TDD.md (Groups).
|
|
4
|
+
"""
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
from graphcore.domain.entity.Entity import Entity
|
|
8
|
+
from graphcore.domain.entity.group.Group import Group
|
|
9
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
10
|
+
from graphcore.errors import Forbidden
|
|
11
|
+
from graphcore.types.entities import EntityFilter
|
|
12
|
+
from graphcore.types.query import Equal
|
|
13
|
+
from graphcore.auth.authz.defaults import ADMINS_GROUP_KEY
|
|
14
|
+
from graphcore.registry.decorators import register_strategy
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@register_strategy(Group)
|
|
18
|
+
class AddMember(Strategy):
|
|
19
|
+
"""Add a user to a `Group`'s roster."""
|
|
20
|
+
strategy_description = "Adds a user to a Group's roster"
|
|
21
|
+
|
|
22
|
+
def verify_executable(self):
|
|
23
|
+
if not isinstance(self.entity, Group):
|
|
24
|
+
raise ValueError(f"Entity {self.entity.entity_id} is not a Group")
|
|
25
|
+
|
|
26
|
+
async def apply(self) -> tuple:
|
|
27
|
+
principal_entity_id: str = self.strategy_request.param_config['principal_entity_id']
|
|
28
|
+
self.entity.add_member(principal_entity_id)
|
|
29
|
+
self.entity = await self.save_entity(self.entity)
|
|
30
|
+
self.strategy_request.ret_val['principal_entity_id'] = principal_entity_id
|
|
31
|
+
return self.strategy_request, self.entity
|
|
32
|
+
|
|
33
|
+
@staticmethod
|
|
34
|
+
def get_request_schema():
|
|
35
|
+
return {
|
|
36
|
+
"type": "object",
|
|
37
|
+
"properties": {"principal_entity_id": {"type": "string"}},
|
|
38
|
+
"required": ["principal_entity_id"],
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def request_constructor(
|
|
43
|
+
cls,
|
|
44
|
+
target_entity_id: str,
|
|
45
|
+
principal_entity_id: str,
|
|
46
|
+
):
|
|
47
|
+
return cls._build_request(
|
|
48
|
+
target_entity_id=target_entity_id,
|
|
49
|
+
param_config={"principal_entity_id": principal_entity_id},
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@register_strategy(Group)
|
|
54
|
+
class RemoveMember(Strategy):
|
|
55
|
+
"""Remove a user from a `Group`'s roster."""
|
|
56
|
+
strategy_description = "Removes a user from a Group's roster"
|
|
57
|
+
|
|
58
|
+
def verify_executable(self):
|
|
59
|
+
if not isinstance(self.entity, Group):
|
|
60
|
+
raise ValueError(f"Entity {self.entity.entity_id} is not a Group")
|
|
61
|
+
|
|
62
|
+
async def apply(self) -> tuple:
|
|
63
|
+
principal_entity_id: str = self.strategy_request.param_config['principal_entity_id']
|
|
64
|
+
self.entity.remove_member(principal_entity_id)
|
|
65
|
+
self.entity = await self.save_entity(self.entity)
|
|
66
|
+
self.strategy_request.ret_val['principal_entity_id'] = principal_entity_id
|
|
67
|
+
return self.strategy_request, self.entity
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def get_request_schema():
|
|
71
|
+
return {
|
|
72
|
+
"type": "object",
|
|
73
|
+
"properties": {"principal_entity_id": {"type": "string"}},
|
|
74
|
+
"required": ["principal_entity_id"],
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@classmethod
|
|
78
|
+
def request_constructor(
|
|
79
|
+
cls,
|
|
80
|
+
target_entity_id: str,
|
|
81
|
+
principal_entity_id: str,
|
|
82
|
+
):
|
|
83
|
+
return cls._build_request(
|
|
84
|
+
target_entity_id=target_entity_id,
|
|
85
|
+
param_config={"principal_entity_id": principal_entity_id},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@register_strategy(Group)
|
|
90
|
+
class ModifyGroup(Strategy):
|
|
91
|
+
"""Rename a `Group`. Editor-of-group access (Write) suffices;
|
|
92
|
+
roster mutation requires ManageAccess (see auth/authz/kernel_policy.py)."""
|
|
93
|
+
strategy_description = "Renames a Group"
|
|
94
|
+
|
|
95
|
+
def verify_executable(self):
|
|
96
|
+
if not isinstance(self.entity, Group):
|
|
97
|
+
raise ValueError(f"Entity {self.entity.entity_id} is not a Group")
|
|
98
|
+
|
|
99
|
+
async def apply(self) -> tuple:
|
|
100
|
+
self.entity.name = self.strategy_request.param_config['name']
|
|
101
|
+
self.entity = await self.save_entity(self.entity)
|
|
102
|
+
return self.strategy_request, self.entity
|
|
103
|
+
|
|
104
|
+
@staticmethod
|
|
105
|
+
def get_request_schema():
|
|
106
|
+
return {
|
|
107
|
+
"type": "object",
|
|
108
|
+
"properties": {"name": {"type": "string"}},
|
|
109
|
+
"required": ["name"],
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def request_constructor(
|
|
114
|
+
cls,
|
|
115
|
+
target_entity_id: str,
|
|
116
|
+
name: str,
|
|
117
|
+
):
|
|
118
|
+
return cls._build_request(
|
|
119
|
+
target_entity_id=target_entity_id,
|
|
120
|
+
param_config={"name": name},
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@register_strategy(Entity)
|
|
125
|
+
class CreateGroup(Strategy):
|
|
126
|
+
"""Create a `Group` under the target, or at the top level when
|
|
127
|
+
there is no target, with the creator as its first member. Only a
|
|
128
|
+
deployment administrator may give it a key (the `Group` policy's
|
|
129
|
+
`deployment_fields`) or have `graphcore.admins` administer it (see
|
|
130
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md, Groups)."""
|
|
131
|
+
strategy_description = "Creates a group under the target, or at the top level"
|
|
132
|
+
|
|
133
|
+
async def apply(self) -> tuple:
|
|
134
|
+
from graphcore.domain.strategy.GrantStrategies import CreateGrant
|
|
135
|
+
|
|
136
|
+
config = self.strategy_request.param_config
|
|
137
|
+
admins = await self._admins() if config.get('admin_managed') else None
|
|
138
|
+
group = self.session.create(Group)
|
|
139
|
+
group.name = config['name']
|
|
140
|
+
group.key = config.get('key')
|
|
141
|
+
group.add_member(self.principal_entity_id)
|
|
142
|
+
if self.entity is None:
|
|
143
|
+
group = await self.save_entity(group)
|
|
144
|
+
else:
|
|
145
|
+
group.set_parent(self.entity.entity_id)
|
|
146
|
+
group = await self.save_entity(group)
|
|
147
|
+
|
|
148
|
+
if admins is not None:
|
|
149
|
+
await self.execute_request(CreateGrant.request_constructor(
|
|
150
|
+
target_entity_id=group.entity_id, subject_id=admins.entity_id, role_name='admin',
|
|
151
|
+
))
|
|
152
|
+
|
|
153
|
+
self.strategy_request.ret_val['group_entity_id'] = group.entity_id
|
|
154
|
+
return self.strategy_request, self.entity
|
|
155
|
+
|
|
156
|
+
async def _admins(self) -> Group:
|
|
157
|
+
"""The admins group as the caller sees it: one who cannot read it
|
|
158
|
+
cannot hand a group to it."""
|
|
159
|
+
page = await self.session.list(EntityFilter(
|
|
160
|
+
type_ids=frozenset({Group.get_entity_type_id()}), where=Equal('key', ADMINS_GROUP_KEY),
|
|
161
|
+
), limit=1)
|
|
162
|
+
if not page.entities:
|
|
163
|
+
raise Forbidden("Only a deployment administrator may set admin_managed")
|
|
164
|
+
return page.entities[0]
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def get_request_schema():
|
|
168
|
+
return {
|
|
169
|
+
"type": "object",
|
|
170
|
+
"properties": {
|
|
171
|
+
"name": {"type": "string"},
|
|
172
|
+
"key": {"type": ["string", "null"], "description": "Stable key; administrators only"},
|
|
173
|
+
"admin_managed": {
|
|
174
|
+
"type": "boolean",
|
|
175
|
+
"description": "Also let graphcore.admins administer it; administrators only",
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
"required": ["name"],
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
@classmethod
|
|
182
|
+
def request_constructor(
|
|
183
|
+
cls,
|
|
184
|
+
target_entity_id: Optional[str],
|
|
185
|
+
name: str,
|
|
186
|
+
key: str = None,
|
|
187
|
+
admin_managed: bool = False,
|
|
188
|
+
):
|
|
189
|
+
param_config = {"name": name, "key": key, "admin_managed": admin_managed}
|
|
190
|
+
return cls._build_request(
|
|
191
|
+
target_entity_id=target_entity_id,
|
|
192
|
+
param_config=param_config,
|
|
193
|
+
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Invite creation. Redemption is not a strategy: the redeemer holds no
|
|
2
|
+
access yet, so it is a fixed kernel write (`GraphAPI.redeem_invite`). See
|
|
3
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Invites)."""
|
|
4
|
+
import re
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from graphcore.domain.entity.Entity import Entity
|
|
8
|
+
from graphcore.domain.entity.invite.Invite import Invite
|
|
9
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
10
|
+
from graphcore.domain.strategy.GrantStrategies import _resolve_role_or_raise
|
|
11
|
+
from graphcore.errors import InvalidRequest
|
|
12
|
+
from graphcore.registry.decorators import register_strategy
|
|
13
|
+
|
|
14
|
+
DEFAULT_LIFETIME_SECONDS = 7 * 24 * 3600
|
|
15
|
+
MAX_LIFETIME_SECONDS = 30 * 24 * 3600
|
|
16
|
+
_SHA256_HEX = re.compile(r'[0-9a-f]{64}')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@register_strategy(Entity)
|
|
20
|
+
class CreateInvite(Strategy):
|
|
21
|
+
"""Create an invite to the target resource. Takes the hash of the link
|
|
22
|
+
secret, never the secret: requests are persisted."""
|
|
23
|
+
strategy_description = 'Creates an invite link that grants a role on a resource to whoever redeems it'
|
|
24
|
+
|
|
25
|
+
async def apply(self) -> tuple:
|
|
26
|
+
resource = self.entity
|
|
27
|
+
config = self.strategy_request.param_config
|
|
28
|
+
|
|
29
|
+
if not self.registry.policy.is_shareable(type(resource)):
|
|
30
|
+
raise InvalidRequest(f"{resource.entity_type_id} is not shareable - cannot hold invites")
|
|
31
|
+
role = _resolve_role_or_raise(self.registry, resource.entity_type_id, config['role_name'])
|
|
32
|
+
|
|
33
|
+
token_hash = str(config['token_hash'])
|
|
34
|
+
if not _SHA256_HEX.fullmatch(token_hash):
|
|
35
|
+
raise InvalidRequest('token_hash must be a lowercase hex SHA-256 digest')
|
|
36
|
+
lifetime = config.get('expires_in_seconds', DEFAULT_LIFETIME_SECONDS)
|
|
37
|
+
if not isinstance(lifetime, int) or not 0 < lifetime <= MAX_LIFETIME_SECONDS:
|
|
38
|
+
raise InvalidRequest(f'expires_in_seconds must be between 1 and {MAX_LIFETIME_SECONDS}')
|
|
39
|
+
max_uses = config.get('max_uses', 1)
|
|
40
|
+
if not isinstance(max_uses, int) or max_uses < 1:
|
|
41
|
+
raise InvalidRequest('max_uses must be a positive integer')
|
|
42
|
+
|
|
43
|
+
invite = self.session.create(Invite)
|
|
44
|
+
invite.role_name = role.name
|
|
45
|
+
invite.token_hash = token_hash
|
|
46
|
+
invite.expires_at = time.time() + lifetime
|
|
47
|
+
invite.max_uses = max_uses
|
|
48
|
+
invite.set_parent(resource.entity_id)
|
|
49
|
+
invite = await self.save_entity(invite)
|
|
50
|
+
|
|
51
|
+
self.strategy_request.ret_val['invite_entity_id'] = invite.entity_id
|
|
52
|
+
return self.strategy_request, resource
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def get_request_schema():
|
|
56
|
+
return {
|
|
57
|
+
"type": "object",
|
|
58
|
+
"properties": {
|
|
59
|
+
"role_name": {"type": "string", "description": "A role registered for the resource's type"},
|
|
60
|
+
"token_hash": {"type": "string", "description": "Hex SHA-256 of the link secret"},
|
|
61
|
+
"expires_in_seconds": {"type": "integer", "description": "Default 7 days, at most 30"},
|
|
62
|
+
"max_uses": {"type": "integer", "description": "Distinct principals that may redeem it; default 1"},
|
|
63
|
+
},
|
|
64
|
+
"required": ["role_name", "token_hash"],
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def request_constructor(
|
|
69
|
+
cls,
|
|
70
|
+
target_entity_id: str,
|
|
71
|
+
role_name: str,
|
|
72
|
+
token_hash: str,
|
|
73
|
+
expires_in_seconds: int = DEFAULT_LIFETIME_SECONDS,
|
|
74
|
+
max_uses: int = 1,
|
|
75
|
+
):
|
|
76
|
+
return cls._build_request(
|
|
77
|
+
target_entity_id=target_entity_id,
|
|
78
|
+
param_config={
|
|
79
|
+
"role_name": role_name,
|
|
80
|
+
"token_hash": token_hash,
|
|
81
|
+
"expires_in_seconds": expires_in_seconds,
|
|
82
|
+
"max_uses": max_uses,
|
|
83
|
+
},
|
|
84
|
+
)
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Creating and managing `RequestTemplate`s. Each change is one write; the
|
|
2
|
+
scheduler follows it (docs/SCHEDULING_TDD.md)."""
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from graphcore.domain.entity.request_template.RequestTemplate import RequestTemplate
|
|
6
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
7
|
+
from graphcore.errors import InvalidRequest
|
|
8
|
+
from graphcore.registry.decorators import register_strategy
|
|
9
|
+
from graphcore.types.schedules import validate_cron
|
|
10
|
+
from graphcore.types.strategy import StrategyRequestPayload
|
|
11
|
+
|
|
12
|
+
ACTIONS = ('update', 'pause', 'resume', 'unschedule')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _callbacks(value) -> list:
|
|
16
|
+
if value is None:
|
|
17
|
+
return []
|
|
18
|
+
if not isinstance(value, list):
|
|
19
|
+
raise InvalidRequest('callbacks must be a list of strategy payloads')
|
|
20
|
+
return [StrategyRequestPayload.from_json(c).to_dict() for c in value]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _cron(value) -> Optional[str]:
|
|
24
|
+
return None if value is None else validate_cron(value)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@register_strategy(RequestTemplate)
|
|
28
|
+
class CreateRequestTemplate(Strategy):
|
|
29
|
+
"""Create a template, with or without a schedule. Runs untargeted; the
|
|
30
|
+
template's own target is `target_entity_id`."""
|
|
31
|
+
strategy_description = "Creates a request template, optionally on a cron schedule"
|
|
32
|
+
|
|
33
|
+
def verify_executable(self):
|
|
34
|
+
if self.entity is not None:
|
|
35
|
+
raise InvalidRequest("CreateRequestTemplate runs without a target")
|
|
36
|
+
|
|
37
|
+
async def apply(self) -> tuple:
|
|
38
|
+
config = self.strategy_request.param_config
|
|
39
|
+
self.registry.strategies.require(config['strategy_name'])
|
|
40
|
+
if config.get('target_entity_id'):
|
|
41
|
+
await self.get_entity(config['target_entity_id'])
|
|
42
|
+
|
|
43
|
+
template = self.session.create(RequestTemplate)
|
|
44
|
+
template.strategy_name = config['strategy_name']
|
|
45
|
+
template.param_config = dict(config.get('param_config') or {})
|
|
46
|
+
template.target_entity_id = config.get('target_entity_id')
|
|
47
|
+
template.callbacks = _callbacks(config.get('callbacks'))
|
|
48
|
+
template.cron = _cron(config.get('cron'))
|
|
49
|
+
template.enabled = config.get('enabled', True)
|
|
50
|
+
template = await self.save_entity(template)
|
|
51
|
+
|
|
52
|
+
self.strategy_request.ret_val['template_id'] = template.entity_id
|
|
53
|
+
return self.strategy_request, self.entity
|
|
54
|
+
|
|
55
|
+
param_schema = {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"properties": {
|
|
58
|
+
"strategy_name": {"type": "string"},
|
|
59
|
+
"param_config": {"type": "object"},
|
|
60
|
+
"target_entity_id": {"type": ["string", "null"]},
|
|
61
|
+
"callbacks": {"type": ["array", "null"], "items": {"type": "object"}},
|
|
62
|
+
"cron": {"type": ["string", "null"], "description": "Five-field cron; null for no schedule"},
|
|
63
|
+
"enabled": {"type": "boolean"},
|
|
64
|
+
},
|
|
65
|
+
"required": ["strategy_name"],
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@classmethod
|
|
69
|
+
def request_constructor(cls, target_entity_id: Optional[str] = None, *, strategy_name: str,
|
|
70
|
+
param_config: Optional[dict] = None, template_target_id: Optional[str] = None,
|
|
71
|
+
callbacks: Optional[list] = None, cron: Optional[str] = None,
|
|
72
|
+
enabled: bool = True):
|
|
73
|
+
return cls._build_request(target_entity_id=None, param_config={
|
|
74
|
+
'strategy_name': strategy_name, 'param_config': param_config or {},
|
|
75
|
+
'target_entity_id': template_target_id, 'callbacks': callbacks,
|
|
76
|
+
'cron': cron, 'enabled': enabled,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@register_strategy(RequestTemplate)
|
|
81
|
+
class ManageRequestTemplate(Strategy):
|
|
82
|
+
"""`update` sets any of `param_config`, `callbacks`, `cron`, `enabled`;
|
|
83
|
+
`pause` and `resume` set `enabled`; `unschedule` clears `cron`."""
|
|
84
|
+
strategy_description = "Updates, pauses, resumes, or unschedules a request template"
|
|
85
|
+
|
|
86
|
+
def verify_executable(self):
|
|
87
|
+
if not isinstance(self.entity, RequestTemplate):
|
|
88
|
+
raise InvalidRequest(f"{self.entity.entity_id} is not a RequestTemplate")
|
|
89
|
+
|
|
90
|
+
async def apply(self) -> tuple:
|
|
91
|
+
config = self.strategy_request.param_config
|
|
92
|
+
template: RequestTemplate = self.entity
|
|
93
|
+
action = config['action']
|
|
94
|
+
if action == 'pause':
|
|
95
|
+
template.enabled = False
|
|
96
|
+
elif action == 'resume':
|
|
97
|
+
template.enabled = True
|
|
98
|
+
elif action == 'unschedule':
|
|
99
|
+
template.cron = None
|
|
100
|
+
else:
|
|
101
|
+
if 'param_config' in config:
|
|
102
|
+
template.param_config = dict(config['param_config'] or {})
|
|
103
|
+
if 'callbacks' in config:
|
|
104
|
+
template.callbacks = _callbacks(config['callbacks'])
|
|
105
|
+
if 'cron' in config:
|
|
106
|
+
template.cron = _cron(config['cron'])
|
|
107
|
+
if 'enabled' in config:
|
|
108
|
+
template.enabled = config['enabled']
|
|
109
|
+
return self.strategy_request, template
|
|
110
|
+
|
|
111
|
+
param_schema = {
|
|
112
|
+
"type": "object",
|
|
113
|
+
"properties": {
|
|
114
|
+
"action": {"type": "string", "enum": list(ACTIONS)},
|
|
115
|
+
"param_config": {"type": "object"},
|
|
116
|
+
"callbacks": {"type": ["array", "null"], "items": {"type": "object"}},
|
|
117
|
+
"cron": {"type": ["string", "null"]},
|
|
118
|
+
"enabled": {"type": "boolean"},
|
|
119
|
+
},
|
|
120
|
+
"required": ["action"],
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@classmethod
|
|
124
|
+
def request_constructor(cls, target_entity_id: str, action: str, **changes):
|
|
125
|
+
return cls._build_request(target_entity_id=target_entity_id,
|
|
126
|
+
param_config={'action': action, **changes})
|