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,67 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
|
|
3
|
+
import httpx2
|
|
4
|
+
from django.test import TestCase, override_settings
|
|
5
|
+
|
|
6
|
+
from graphcore.auth.authn.testing import TestIssuer
|
|
7
|
+
from graphcore.transport.agent_asgi import with_agent_frontend
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def _inner(scope, receive, send):
|
|
11
|
+
await send({'type': 'http.response.start', 'status': 418, 'headers': []})
|
|
12
|
+
await send({'type': 'http.response.body', 'body': b'inner'})
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@override_settings(ALLOWED_HOSTS=['localhost'])
|
|
16
|
+
class ProtectedResourceMetadataTests(TestCase):
|
|
17
|
+
def setUp(self):
|
|
18
|
+
self.first, self.second = TestIssuer('https://one.example'), TestIssuer('https://two.example')
|
|
19
|
+
self.enterContext(self.first.trusted(self.second))
|
|
20
|
+
apps_dir = self.enterContext(tempfile.TemporaryDirectory())
|
|
21
|
+
self.app = with_agent_frontend(_inner, apps_dir)
|
|
22
|
+
|
|
23
|
+
async def get(self, path, method='GET', base_url='http://localhost:8000'):
|
|
24
|
+
transport = httpx2.ASGITransport(app=self.app)
|
|
25
|
+
async with httpx2.AsyncClient(transport=transport, base_url=base_url) as http:
|
|
26
|
+
return await http.request(method, path)
|
|
27
|
+
|
|
28
|
+
async def test_mcp_document_names_the_mcp_resource_and_issuers_in_order(self):
|
|
29
|
+
response = await self.get('/.well-known/oauth-protected-resource/mcp')
|
|
30
|
+
self.assertEqual(response.status_code, 200)
|
|
31
|
+
self.assertEqual(response.json(), {
|
|
32
|
+
'resource': 'http://localhost:8000/mcp',
|
|
33
|
+
'authorization_servers': ['https://one.example', 'https://two.example'],
|
|
34
|
+
'bearer_methods_supported': ['header'],
|
|
35
|
+
'scopes_supported': ['openid'],
|
|
36
|
+
})
|
|
37
|
+
self.assertEqual(response.headers['access-control-allow-origin'], '*')
|
|
38
|
+
|
|
39
|
+
async def test_root_document_names_the_deployment(self):
|
|
40
|
+
response = await self.get('/.well-known/oauth-protected-resource')
|
|
41
|
+
self.assertEqual(response.json()['resource'], 'http://localhost:8000')
|
|
42
|
+
|
|
43
|
+
@override_settings(GRAPHCORE_PUBLIC_URL='https://app.example')
|
|
44
|
+
async def test_public_url_overrides_the_host(self):
|
|
45
|
+
response = await self.get('/.well-known/oauth-protected-resource/mcp')
|
|
46
|
+
self.assertEqual(response.json()['resource'], 'https://app.example/mcp')
|
|
47
|
+
|
|
48
|
+
async def test_unlisted_host_is_refused_rather_than_echoed(self):
|
|
49
|
+
for path in ('/.well-known/oauth-protected-resource', '/mcp'):
|
|
50
|
+
response = await self.get(path, base_url='http://evil.example')
|
|
51
|
+
self.assertEqual(response.status_code, 400)
|
|
52
|
+
|
|
53
|
+
async def test_preflight(self):
|
|
54
|
+
response = await self.get('/.well-known/oauth-protected-resource', method='OPTIONS')
|
|
55
|
+
self.assertEqual(response.status_code, 204)
|
|
56
|
+
self.assertEqual(response.headers['access-control-allow-origin'], '*')
|
|
57
|
+
|
|
58
|
+
async def test_mcp_401_points_at_the_mcp_document(self):
|
|
59
|
+
response = await self.get('/mcp', method='POST')
|
|
60
|
+
self.assertEqual(response.status_code, 401)
|
|
61
|
+
self.assertEqual(
|
|
62
|
+
response.headers['www-authenticate'],
|
|
63
|
+
'Bearer resource_metadata="http://localhost:8000/.well-known/oauth-protected-resource/mcp"',
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
async def test_other_paths_reach_the_inner_app(self):
|
|
67
|
+
self.assertEqual((await self.get('/graphcore/api/routes/')).status_code, 418)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Framework HTTP API. Deployments mount this with ``path('graphcore/', include('graphcore.urls'))``."""
|
|
2
|
+
|
|
3
|
+
from django.urls import path
|
|
4
|
+
from graphcore.transport import views
|
|
5
|
+
|
|
6
|
+
urlpatterns = [
|
|
7
|
+
# Identity document
|
|
8
|
+
path('.well-known/graphcore/', views.api_identity_document, name='api_identity_document'),
|
|
9
|
+
|
|
10
|
+
# Directory endpoints
|
|
11
|
+
path('api/get_entity_directory/', views.api_get_entity_directory, name='api_get_entity_directory'),
|
|
12
|
+
path('api/get_strategy_directory/', views.api_get_strategy_directory, name='api_get_strategy_directory'),
|
|
13
|
+
|
|
14
|
+
path('api/routes/', views.api_get_routes, name='api_get_routes'),
|
|
15
|
+
path('api/resolve/', views.api_resolve, name='api_resolve'),
|
|
16
|
+
|
|
17
|
+
# Entity endpoints
|
|
18
|
+
path('api/get_entity/<path:entity_id>/', views.api_get_entity, name='api_get_entity'),
|
|
19
|
+
path('api/get_entities/', views.api_get_entities, name='api_get_entities'),
|
|
20
|
+
path('api/access/<path:resource_id>/', views.api_get_access, name='api_get_access'),
|
|
21
|
+
path('api/referrers/<path:entity_id>/', views.api_get_referrers, name='api_get_referrers'),
|
|
22
|
+
path('api/list_entities/', views.api_list_entities, name='api_list_entities'),
|
|
23
|
+
path('api/execute_strategy_req/', views.api_execute_strategy_req, name='api_execute_strategy_req'),
|
|
24
|
+
|
|
25
|
+
path('api/auth/me/', views.api_auth_me, name='api_auth_me'),
|
|
26
|
+
|
|
27
|
+
path('api/invites/', views.api_create_invite, name='api_create_invite'),
|
|
28
|
+
path('api/invites/redeem/', views.api_redeem_invite, name='api_redeem_invite'),
|
|
29
|
+
]
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
"""HTTP adapter for the GraphAPI. Format and status mapping only."""
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from django.http import JsonResponse
|
|
6
|
+
from django.views.decorators.csrf import csrf_exempt
|
|
7
|
+
|
|
8
|
+
from graphcore.runtime import get_runtime
|
|
9
|
+
from graphcore.types.identity import Principal
|
|
10
|
+
from graphcore.types.api import StrategyRequestPayload
|
|
11
|
+
from graphcore.types.entities import EntityFilter, Tombstone, client_view
|
|
12
|
+
from graphcore.auth.authn import authenticate, resource_metadata
|
|
13
|
+
from graphcore.api.predicate_codec import decode_predicate
|
|
14
|
+
from graphcore.errors import (
|
|
15
|
+
Forbidden, InvalidQuery, InvalidRequest, NotFound, UnsupportedQuery, log_and_correlate,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _error_response(logger_message: str, status: int = 500) -> JsonResponse:
|
|
22
|
+
correlation_id = log_and_correlate(logger, logger_message)
|
|
23
|
+
return JsonResponse(
|
|
24
|
+
{'error': 'Internal server error', 'correlation_id': correlation_id},
|
|
25
|
+
status=status,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _unauthorized(request) -> JsonResponse:
|
|
30
|
+
base = resource_metadata.public_base(request.scheme, request.get_host())
|
|
31
|
+
response = JsonResponse({'error': 'Authentication required'}, status=401)
|
|
32
|
+
response['WWW-Authenticate'] = resource_metadata.challenge(base)
|
|
33
|
+
return response
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
async def _principal(request) -> Principal | None:
|
|
37
|
+
auth_header = request.headers.get('Authorization', '')
|
|
38
|
+
if not auth_header.startswith('Bearer '):
|
|
39
|
+
return None
|
|
40
|
+
return await authenticate(auth_header[7:])
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
async def api_identity_document(request):
|
|
44
|
+
"""This deployment's identity document: the only place it states where
|
|
45
|
+
it is reachable (docs/ENTITY_REFERENCE_TDD.md, Peer table and identity
|
|
46
|
+
document). Unauthenticated - a backend identity is not a credential and
|
|
47
|
+
authenticates nothing."""
|
|
48
|
+
if request.method != 'GET':
|
|
49
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
50
|
+
runtime = get_runtime()
|
|
51
|
+
return JsonResponse({
|
|
52
|
+
'backend_id': runtime.backend_id,
|
|
53
|
+
'deployment_entity_id': runtime.deployment_entity_id,
|
|
54
|
+
'handle': runtime.handle,
|
|
55
|
+
'endpoint': request.build_absolute_uri('/graphcore/'),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async def api_auth_me(request):
|
|
60
|
+
"""Who the bearer is here: issuer tokens carry no GraphCore ids."""
|
|
61
|
+
if request.method != 'GET':
|
|
62
|
+
return JsonResponse({'error': 'GET method required'}, status=405)
|
|
63
|
+
principal = await _principal(request)
|
|
64
|
+
if not principal:
|
|
65
|
+
return _unauthorized(request)
|
|
66
|
+
runtime = get_runtime()
|
|
67
|
+
entity = await runtime.entity_service.repository.load(principal.principal_entity_id)
|
|
68
|
+
return JsonResponse({
|
|
69
|
+
'principal_entity_id': principal.principal_entity_id,
|
|
70
|
+
'deployment_entity_id': runtime.deployment_entity_id,
|
|
71
|
+
'username': entity.display_name if entity else None,
|
|
72
|
+
'issuer': principal.iss,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
async def api_get_entity_directory(request):
|
|
77
|
+
if request.method != 'GET':
|
|
78
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
79
|
+
if not await _principal(request):
|
|
80
|
+
return _unauthorized(request)
|
|
81
|
+
try:
|
|
82
|
+
return JsonResponse({'entities': get_runtime().graph_api.describe_entities()}, safe=False)
|
|
83
|
+
except Exception:
|
|
84
|
+
return _error_response("Error getting entity directory")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
async def api_get_strategy_directory(request):
|
|
88
|
+
if request.method != 'GET':
|
|
89
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
90
|
+
if not await _principal(request):
|
|
91
|
+
return _unauthorized(request)
|
|
92
|
+
try:
|
|
93
|
+
return JsonResponse(get_runtime().graph_api.describe_strategies(), safe=False)
|
|
94
|
+
except Exception:
|
|
95
|
+
return _error_response("Error getting strategy directory")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def api_get_routes(request):
|
|
99
|
+
if request.method != 'GET':
|
|
100
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
101
|
+
return JsonResponse({'routes': get_runtime().graph_api.routes()})
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
async def api_resolve(request):
|
|
105
|
+
if request.method != 'GET':
|
|
106
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
107
|
+
try:
|
|
108
|
+
return JsonResponse(get_runtime().graph_api.resolve(request.GET.get('address', '')))
|
|
109
|
+
except NotFound:
|
|
110
|
+
return JsonResponse({'error': 'No route matches the address'}, status=404)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@csrf_exempt
|
|
114
|
+
async def api_get_entity(request, entity_id):
|
|
115
|
+
if request.method != 'GET':
|
|
116
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
117
|
+
|
|
118
|
+
principal = await _principal(request)
|
|
119
|
+
if not principal:
|
|
120
|
+
return _unauthorized(request)
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
result = await get_runtime().graph_api.get_entity(principal, entity_id)
|
|
124
|
+
if isinstance(result, Tombstone):
|
|
125
|
+
if result.cause == 'forbidden':
|
|
126
|
+
return JsonResponse({'error': 'Permission denied'}, status=403)
|
|
127
|
+
if result.cause in ('unreachable', 'undelegated'):
|
|
128
|
+
return JsonResponse(
|
|
129
|
+
{'error': 'Owning deployment unavailable', 'cause': result.cause},
|
|
130
|
+
status=502,
|
|
131
|
+
)
|
|
132
|
+
return JsonResponse({'error': 'Entity not found'}, status=404)
|
|
133
|
+
return JsonResponse(client_view(result))
|
|
134
|
+
except Exception:
|
|
135
|
+
return _error_response(f"Error getting entity {entity_id}")
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
_TOMBSTONE_STATUS = {
|
|
139
|
+
'forbidden': 403, 'missing': 404, 'deleted': 404,
|
|
140
|
+
'unreachable': 502, 'undelegated': 502,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _batch_error(entity_id, result) -> dict:
|
|
145
|
+
"""One `errors` entry, carrying the tombstone's cause so a caller can
|
|
146
|
+
tell a target it may not read from one its owner could not deliver."""
|
|
147
|
+
cause = result.cause if isinstance(result, Tombstone) else 'missing'
|
|
148
|
+
return {
|
|
149
|
+
'entity_id': entity_id,
|
|
150
|
+
'error': 'Entity not found or access denied',
|
|
151
|
+
'cause': cause,
|
|
152
|
+
'status': _TOMBSTONE_STATUS.get(cause, 404),
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@csrf_exempt
|
|
157
|
+
async def api_get_entities(request):
|
|
158
|
+
if request.method != 'POST':
|
|
159
|
+
return JsonResponse({'error': 'POST method required'}, status=400)
|
|
160
|
+
|
|
161
|
+
principal = await _principal(request)
|
|
162
|
+
if not principal:
|
|
163
|
+
return _unauthorized(request)
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
data = json.loads(request.body or b'{}')
|
|
167
|
+
entity_ids = data.get('entity_ids', [])
|
|
168
|
+
if not isinstance(entity_ids, list):
|
|
169
|
+
return JsonResponse({'error': 'entity_ids must be a list'}, status=400)
|
|
170
|
+
|
|
171
|
+
found = await get_runtime().graph_api.get_entities(principal, entity_ids)
|
|
172
|
+
entities = {
|
|
173
|
+
eid: client_view(result)
|
|
174
|
+
for eid, result in found.items()
|
|
175
|
+
if not isinstance(result, Tombstone)
|
|
176
|
+
}
|
|
177
|
+
errors = [
|
|
178
|
+
_batch_error(eid, found.get(eid))
|
|
179
|
+
for eid in entity_ids if eid not in entities
|
|
180
|
+
]
|
|
181
|
+
return JsonResponse({'entities': entities, 'errors': errors})
|
|
182
|
+
except InvalidRequest as exc:
|
|
183
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
184
|
+
except json.JSONDecodeError:
|
|
185
|
+
return JsonResponse({'error': 'Invalid JSON'}, status=400)
|
|
186
|
+
except Exception:
|
|
187
|
+
return _error_response("Error in get_entities endpoint")
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@csrf_exempt
|
|
191
|
+
async def api_get_access(request, resource_id):
|
|
192
|
+
if request.method != 'GET':
|
|
193
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
194
|
+
principal = await _principal(request)
|
|
195
|
+
if not principal:
|
|
196
|
+
return _unauthorized(request)
|
|
197
|
+
try:
|
|
198
|
+
summary = await get_runtime().graph_api.get_access(principal, resource_id)
|
|
199
|
+
return JsonResponse(summary.to_dict())
|
|
200
|
+
except Forbidden:
|
|
201
|
+
return JsonResponse({'error': 'Permission denied'}, status=403)
|
|
202
|
+
except NotFound:
|
|
203
|
+
return JsonResponse({'error': 'Entity not found'}, status=404)
|
|
204
|
+
except InvalidRequest as exc:
|
|
205
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
206
|
+
except Exception:
|
|
207
|
+
return _error_response(f"Error getting access for {resource_id}")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# A predicate the storage adapter cannot resolve is the caller's mistake.
|
|
211
|
+
_QUERY_ERRORS = (InvalidRequest, InvalidQuery, UnsupportedQuery)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@csrf_exempt
|
|
215
|
+
async def api_get_referrers(request, entity_id):
|
|
216
|
+
if request.method != 'GET':
|
|
217
|
+
return JsonResponse({'error': 'GET method required'}, status=400)
|
|
218
|
+
principal = await _principal(request)
|
|
219
|
+
if not principal:
|
|
220
|
+
return _unauthorized(request)
|
|
221
|
+
try:
|
|
222
|
+
where = request.GET.get('where')
|
|
223
|
+
page = await get_runtime().graph_api.get_referrers(
|
|
224
|
+
principal, entity_id,
|
|
225
|
+
where=decode_predicate(json.loads(where)) if where else None,
|
|
226
|
+
cursor=request.GET.get('cursor'), limit=int(request.GET.get('limit', 25)),
|
|
227
|
+
)
|
|
228
|
+
return JsonResponse({
|
|
229
|
+
'entities': [client_view(entity) for entity in page.entities],
|
|
230
|
+
'next_cursor': page.next_cursor,
|
|
231
|
+
})
|
|
232
|
+
except _QUERY_ERRORS as exc:
|
|
233
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
234
|
+
except json.JSONDecodeError:
|
|
235
|
+
return JsonResponse({'error': 'where is not valid JSON'}, status=400)
|
|
236
|
+
except (TypeError, ValueError) as exc:
|
|
237
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
238
|
+
except Exception:
|
|
239
|
+
return _error_response(f"Error getting referrers for {entity_id}")
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
@csrf_exempt
|
|
243
|
+
async def api_list_entities(request):
|
|
244
|
+
if request.method != 'POST':
|
|
245
|
+
return JsonResponse({'error': 'POST method required'}, status=400)
|
|
246
|
+
principal = await _principal(request)
|
|
247
|
+
if not principal:
|
|
248
|
+
return _unauthorized(request)
|
|
249
|
+
try:
|
|
250
|
+
data = json.loads(request.body or b'{}')
|
|
251
|
+
type_ids = data.get('type_ids')
|
|
252
|
+
if not isinstance(type_ids, list) or not type_ids:
|
|
253
|
+
return JsonResponse({'error': 'type_ids must be a non-empty list'}, status=400)
|
|
254
|
+
where = data.get('where')
|
|
255
|
+
granted_to_me = data.get('granted_to_me', False)
|
|
256
|
+
if not isinstance(granted_to_me, bool):
|
|
257
|
+
return JsonResponse({'error': 'granted_to_me must be a boolean'}, status=400)
|
|
258
|
+
entity_filter = EntityFilter(
|
|
259
|
+
type_ids=frozenset(type_ids), parent_id=data.get('parent_id'),
|
|
260
|
+
where=decode_predicate(where) if where is not None else None,
|
|
261
|
+
granted_to_me=granted_to_me,
|
|
262
|
+
)
|
|
263
|
+
page = await get_runtime().graph_api.list_entities(
|
|
264
|
+
principal, entity_filter,
|
|
265
|
+
cursor=data.get('cursor'), limit=int(data.get('limit', 25)),
|
|
266
|
+
)
|
|
267
|
+
return JsonResponse({
|
|
268
|
+
'entities': [client_view(entity) for entity in page.entities],
|
|
269
|
+
'next_cursor': page.next_cursor,
|
|
270
|
+
})
|
|
271
|
+
except _QUERY_ERRORS as exc:
|
|
272
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
273
|
+
except json.JSONDecodeError:
|
|
274
|
+
return JsonResponse({'error': 'Invalid JSON'}, status=400)
|
|
275
|
+
except (TypeError, ValueError) as exc:
|
|
276
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
277
|
+
except Exception:
|
|
278
|
+
return _error_response("Error in list_entities endpoint")
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@csrf_exempt
|
|
282
|
+
async def api_execute_strategy_req(request):
|
|
283
|
+
if request.method != 'POST':
|
|
284
|
+
return JsonResponse({'error': 'POST method required'}, status=400)
|
|
285
|
+
|
|
286
|
+
principal = await _principal(request)
|
|
287
|
+
if not principal:
|
|
288
|
+
return _unauthorized(request)
|
|
289
|
+
|
|
290
|
+
try:
|
|
291
|
+
data = json.loads(request.body or b'{}')
|
|
292
|
+
strategy_payload = data.get('strategy', [])
|
|
293
|
+
if isinstance(strategy_payload, dict):
|
|
294
|
+
strategy_payload = [strategy_payload]
|
|
295
|
+
if not isinstance(strategy_payload, list):
|
|
296
|
+
return JsonResponse({'error': 'strategy must be an object or list'}, status=400)
|
|
297
|
+
payloads = [StrategyRequestPayload.from_json(item) for item in strategy_payload]
|
|
298
|
+
await_response = data.get('await_response', False)
|
|
299
|
+
|
|
300
|
+
outcomes = await get_runtime().graph_api.execute_strategies(
|
|
301
|
+
principal, payloads, wait=bool(await_response)
|
|
302
|
+
)
|
|
303
|
+
return JsonResponse({
|
|
304
|
+
'await_response': bool(await_response),
|
|
305
|
+
'results': [o.to_dict() for o in outcomes],
|
|
306
|
+
})
|
|
307
|
+
except InvalidRequest as exc:
|
|
308
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
309
|
+
except json.JSONDecodeError:
|
|
310
|
+
return JsonResponse({'error': 'Invalid JSON'}, status=400)
|
|
311
|
+
except Exception:
|
|
312
|
+
return _error_response("Error in execute_strategy_req endpoint")
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
@csrf_exempt
|
|
316
|
+
async def api_create_invite(request):
|
|
317
|
+
"""Create an invite link's entity and return its token, the only copy of
|
|
318
|
+
the secret. Takes `resource_id`, `role_name`, and optionally
|
|
319
|
+
`expires_in_seconds` and `max_uses`."""
|
|
320
|
+
if request.method != 'POST':
|
|
321
|
+
return JsonResponse({'error': 'POST method required'}, status=405)
|
|
322
|
+
principal = await _principal(request)
|
|
323
|
+
if not principal:
|
|
324
|
+
return _unauthorized(request)
|
|
325
|
+
try:
|
|
326
|
+
data = json.loads(request.body or b'{}')
|
|
327
|
+
options = {k: data[k] for k in ('expires_in_seconds', 'max_uses') if k in data}
|
|
328
|
+
invite_id, token = await get_runtime().graph_api.create_invite(
|
|
329
|
+
principal, str(data['resource_id']), str(data['role_name']), **options,
|
|
330
|
+
)
|
|
331
|
+
return JsonResponse({'invite_entity_id': invite_id, 'token': token})
|
|
332
|
+
except (KeyError, json.JSONDecodeError):
|
|
333
|
+
return JsonResponse({'error': 'resource_id and role_name are required'}, status=400)
|
|
334
|
+
except InvalidRequest as exc:
|
|
335
|
+
return JsonResponse({'error': str(exc)}, status=400)
|
|
336
|
+
except Forbidden:
|
|
337
|
+
return JsonResponse({'error': 'Cannot manage access to this resource'}, status=403)
|
|
338
|
+
except NotFound:
|
|
339
|
+
return JsonResponse({'error': 'Entity not found'}, status=404)
|
|
340
|
+
except Exception:
|
|
341
|
+
return _error_response("Error in create_invite endpoint")
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
@csrf_exempt
|
|
345
|
+
async def api_redeem_invite(request):
|
|
346
|
+
"""Redeem an invite token as the signed-in principal."""
|
|
347
|
+
if request.method != 'POST':
|
|
348
|
+
return JsonResponse({'error': 'POST method required'}, status=405)
|
|
349
|
+
principal = await _principal(request)
|
|
350
|
+
if not principal:
|
|
351
|
+
return _unauthorized(request)
|
|
352
|
+
try:
|
|
353
|
+
data = json.loads(request.body or b'{}')
|
|
354
|
+
return JsonResponse(await get_runtime().graph_api.redeem_invite(principal, str(data.get('token', ''))))
|
|
355
|
+
except (NotFound, json.JSONDecodeError):
|
|
356
|
+
return JsonResponse({'error': 'This invite is invalid or has expired'}, status=404)
|
|
357
|
+
except Exception:
|
|
358
|
+
return _error_response("Error in redeem_invite endpoint")
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Authorization vocabulary: Perm, Role, and the signals derived from grant
|
|
2
|
+
writes. The kernel's own roles are in ``graphcore.auth.authz.defaults``.
|
|
3
|
+
|
|
4
|
+
See ``graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md`` for the design these implement.
|
|
5
|
+
This module is pure data - no storage, no adapter, no registry lookups - so
|
|
6
|
+
every other authorization module (policy, port, adapter) can depend on it
|
|
7
|
+
without a cycle.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import ClassVar, Literal, Optional
|
|
13
|
+
|
|
14
|
+
class Perm:
|
|
15
|
+
"""Base for typed permission tokens. An adapter denies any perm it does
|
|
16
|
+
not recognize (i.e. any instance that is not itself a ``Perm``, or a
|
|
17
|
+
kind the adapter's policy has no perm/role wiring for).
|
|
18
|
+
|
|
19
|
+
``admin_implied``: whether a role carrying ``ManageAccess`` satisfies this
|
|
20
|
+
perm without holding it. Defaults True - an access administrator can
|
|
21
|
+
grant themselves any role, so denying them is friction, not security. An
|
|
22
|
+
application declares ``admin_implied = False`` on a perm it wants held
|
|
23
|
+
explicitly (a distinct duty an admin must take on by grant)."""
|
|
24
|
+
|
|
25
|
+
admin_implied: ClassVar[bool] = True
|
|
26
|
+
description: ClassVar[str] = ''
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class Read(Perm):
|
|
31
|
+
description: ClassVar[str] = 'View the entity'
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class Write(Perm):
|
|
36
|
+
description: ClassVar[str] = 'Change the entity and run its mutating strategies'
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class Delete(Perm):
|
|
41
|
+
description: ClassVar[str] = 'Delete the entity and everything it contains'
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class ManageAccess(Perm):
|
|
46
|
+
description: ClassVar[str] = 'Grant and revoke access'
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class Create(Perm):
|
|
51
|
+
"""Create entities inside the one it is held on; on the deployment,
|
|
52
|
+
create top-level entities."""
|
|
53
|
+
description: ClassVar[str] = 'Create entities inside it'
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class CreatePermission(Perm):
|
|
57
|
+
"""Base for a type's own create permission (`define(create=...)`) that
|
|
58
|
+
must be held explicitly: owning or administering the parent does not
|
|
59
|
+
imply it."""
|
|
60
|
+
admin_implied: ClassVar[bool] = False
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class Role:
|
|
65
|
+
"""A named bundle of perms, the vocabulary of grants.
|
|
66
|
+
|
|
67
|
+
Equality and hash are by ``name`` only - ``perms`` is metadata carried
|
|
68
|
+
alongside the identity, not part of it. Two ``Role`` instances with the
|
|
69
|
+
same name are the same role even if constructed with different perm
|
|
70
|
+
sets; the registered definition (see ``auth.authz.policy``) is what
|
|
71
|
+
ultimately governs which perms a role's name confers.
|
|
72
|
+
"""
|
|
73
|
+
name: str
|
|
74
|
+
perms: frozenset[Perm] = frozenset()
|
|
75
|
+
description: str = ''
|
|
76
|
+
|
|
77
|
+
def __eq__(self, other) -> bool:
|
|
78
|
+
return isinstance(other, Role) and self.name == other.name
|
|
79
|
+
|
|
80
|
+
def __hash__(self) -> int:
|
|
81
|
+
return hash(self.name)
|
|
82
|
+
|
|
83
|
+
def allows(self, perm: Perm) -> bool:
|
|
84
|
+
"""Whether this role satisfies ``perm``: held outright, or implied
|
|
85
|
+
by ``ManageAccess`` for perms that permit it (see ``Perm``)."""
|
|
86
|
+
return perm in self.perms or (perm.admin_implied and ManageAccess() in self.perms)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
Provenance = Literal['direct', 'owner', 'type-policy'] | str
|
|
90
|
+
"""'direct' | 'owner' | 'type-policy' | 'inherited-from-<scope-id>'."""
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(frozen=True)
|
|
94
|
+
class GrantInfo:
|
|
95
|
+
"""One entry in ``AccessPort.list_grants``: who holds what role on a
|
|
96
|
+
resource, and why. ``provenance`` is computed, never stored."""
|
|
97
|
+
subject_id: str
|
|
98
|
+
role: Role
|
|
99
|
+
provenance: str
|
|
100
|
+
grant_entity_id: Optional[str] = None
|
|
101
|
+
resource_id: Optional[str] = None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True)
|
|
105
|
+
class AccessChange:
|
|
106
|
+
"""Derived by the adapter's ``entity_saved``/``entities_deleted`` hooks
|
|
107
|
+
from a grant write. Carries what just changed, for cache
|
|
108
|
+
invalidation and live-update delivery. Never persisted itself - a
|
|
109
|
+
signal, not a record."""
|
|
110
|
+
resource_id: str
|
|
111
|
+
subject_id: str
|
|
112
|
+
old_role: Optional[Role]
|
|
113
|
+
new_role: Optional[Role]
|
|
114
|
+
resource_type_id: Optional[str] = None
|
graphcore/types/api.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Data payloads owned by the GraphAPI facade.
|
|
2
|
+
|
|
3
|
+
`StrategyRequestPayload` is re-exported here for the facade's callers; it
|
|
4
|
+
lives in `types/strategy.py` because the layers below the facade speak it
|
|
5
|
+
too.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Any, Literal, Optional
|
|
11
|
+
|
|
12
|
+
from graphcore.types.access import GrantInfo
|
|
13
|
+
from graphcore.types.strategy import RequestStatus, StrategyRequestPayload
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class StrategyOutcome:
|
|
18
|
+
"""Per-strategy result from GraphAPI.execute_strategies: the request's
|
|
19
|
+
status when the front door returned, or `failed` for a request it
|
|
20
|
+
refused (which has no `strategy_request_id`). A failed outcome carries
|
|
21
|
+
the `KernelError`'s `code` and `details` as `error_code` and
|
|
22
|
+
`error_details`."""
|
|
23
|
+
|
|
24
|
+
status: RequestStatus
|
|
25
|
+
strategy_request_id: Optional[str] = None
|
|
26
|
+
target_entity_id: Optional[str] = None
|
|
27
|
+
ret_val: Any = field(default_factory=dict)
|
|
28
|
+
error: Optional[str] = None
|
|
29
|
+
error_code: Optional[str] = None
|
|
30
|
+
error_details: dict = field(default_factory=dict)
|
|
31
|
+
client_request_id: Optional[str] = None
|
|
32
|
+
|
|
33
|
+
def to_dict(self) -> dict:
|
|
34
|
+
out: dict = {'status': self.status}
|
|
35
|
+
if self.strategy_request_id is not None:
|
|
36
|
+
out['strategy_request_id'] = self.strategy_request_id
|
|
37
|
+
if self.target_entity_id is not None:
|
|
38
|
+
out['target_entity_id'] = self.target_entity_id
|
|
39
|
+
if self.status == 'succeeded':
|
|
40
|
+
out['ret_val'] = self.ret_val if self.ret_val is not None else {}
|
|
41
|
+
if self.status == 'failed':
|
|
42
|
+
if self.error is not None:
|
|
43
|
+
out['error'] = self.error
|
|
44
|
+
out['error_code'] = self.error_code
|
|
45
|
+
out['error_details'] = self.error_details or {}
|
|
46
|
+
if self.client_request_id is not None:
|
|
47
|
+
out['client_request_id'] = self.client_request_id
|
|
48
|
+
return out
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class AccessSummary:
|
|
53
|
+
"""Instance-level access facts for one resource, from GraphAPI.get_access.
|
|
54
|
+
Type-level facts (roles, public_permissions) live on the entity directory
|
|
55
|
+
instead - they're identical for every instance of a type."""
|
|
56
|
+
|
|
57
|
+
resource_id: str
|
|
58
|
+
can_manage: bool
|
|
59
|
+
grants: list[GrantInfo]
|
|
60
|
+
|
|
61
|
+
def to_dict(self) -> dict:
|
|
62
|
+
return {
|
|
63
|
+
'resource_id': self.resource_id,
|
|
64
|
+
'can_manage': self.can_manage,
|
|
65
|
+
'grants': [
|
|
66
|
+
{
|
|
67
|
+
'subject_id': grant.subject_id,
|
|
68
|
+
'role': grant.role.name,
|
|
69
|
+
'provenance': grant.provenance,
|
|
70
|
+
'grant_entity_id': grant.grant_entity_id,
|
|
71
|
+
'resource_id': grant.resource_id,
|
|
72
|
+
}
|
|
73
|
+
for grant in self.grants
|
|
74
|
+
],
|
|
75
|
+
}
|
|
76
|
+
|