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.
Files changed (343) hide show
  1. graphcore/__init__.py +5 -0
  2. graphcore/agent_frontend/__init__.py +8 -0
  3. graphcore/agent_frontend/api.py +69 -0
  4. graphcore/agent_frontend/base/__init__.py +11 -0
  5. graphcore/agent_frontend/base/views.py +167 -0
  6. graphcore/agent_frontend/client.py +31 -0
  7. graphcore/agent_frontend/context.py +157 -0
  8. graphcore/agent_frontend/errors.py +12 -0
  9. graphcore/agent_frontend/loading.py +54 -0
  10. graphcore/agent_frontend/registry.py +42 -0
  11. graphcore/agent_frontend/render.py +207 -0
  12. graphcore/agent_frontend/server.py +103 -0
  13. graphcore/agent_frontend/signatures.py +35 -0
  14. graphcore/agent_frontend/testing.py +45 -0
  15. graphcore/agent_frontend/tests/__init__.py +0 -0
  16. graphcore/agent_frontend/tests/fakes.py +114 -0
  17. graphcore/agent_frontend/tests/test_frontend.py +447 -0
  18. graphcore/agent_frontend/tests/test_loading.py +83 -0
  19. graphcore/agent_frontend/tests/test_server.py +109 -0
  20. graphcore/agent_frontend/tools.py +137 -0
  21. graphcore/api/__init__.py +4 -0
  22. graphcore/api/graph_api.py +378 -0
  23. graphcore/api/predicate_codec.py +81 -0
  24. graphcore/api/tests/__init__.py +0 -0
  25. graphcore/api/tests/test_changed_entities.py +109 -0
  26. graphcore/api/tests/test_graph_api.py +290 -0
  27. graphcore/api/tests/test_graph_api_access.py +94 -0
  28. graphcore/api/tests/test_graph_api_grants.py +90 -0
  29. graphcore/api/tests/test_predicate_codec.py +83 -0
  30. graphcore/api/tests/test_subtree_query.py +79 -0
  31. graphcore/apps.py +71 -0
  32. graphcore/auth/__init__.py +1 -0
  33. graphcore/auth/authn/README.md +178 -0
  34. graphcore/auth/authn/__init__.py +3 -0
  35. graphcore/auth/authn/authenticator.py +120 -0
  36. graphcore/auth/authn/onboarding.py +105 -0
  37. graphcore/auth/authn/principals.py +20 -0
  38. graphcore/auth/authn/resource_metadata.py +43 -0
  39. graphcore/auth/authn/service_accounts.py +96 -0
  40. graphcore/auth/authn/testing.py +96 -0
  41. graphcore/auth/authn/tests/__init__.py +0 -0
  42. graphcore/auth/authn/tests/test_authenticator.py +145 -0
  43. graphcore/auth/authn/tests/test_onboarding.py +145 -0
  44. graphcore/auth/authn/tests/test_service_accounts.py +180 -0
  45. graphcore/auth/authn/users.py +59 -0
  46. graphcore/auth/authz/__init__.py +0 -0
  47. graphcore/auth/authz/access_port.py +120 -0
  48. graphcore/auth/authz/adapter/__init__.py +0 -0
  49. graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
  50. graphcore/auth/authz/adapter/tests/__init__.py +0 -0
  51. graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
  52. graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
  53. graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
  54. graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
  55. graphcore/auth/authz/defaults.py +26 -0
  56. graphcore/auth/authz/kernel_policy.py +191 -0
  57. graphcore/auth/authz/kernel_writes.py +31 -0
  58. graphcore/auth/authz/lookups.py +25 -0
  59. graphcore/auth/authz/policy.py +403 -0
  60. graphcore/auth/authz/tests/__init__.py +0 -0
  61. graphcore/auth/authz/tests/test_can_contain.py +150 -0
  62. graphcore/auth/authz/tests/test_contained_by.py +167 -0
  63. graphcore/auth/authz/tests/test_creation.py +391 -0
  64. graphcore/auth/authz/tests/test_delete_permission.py +82 -0
  65. graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
  66. graphcore/auth/authz/tests/test_integration.py +637 -0
  67. graphcore/auth/authz/tests/test_invites.py +184 -0
  68. graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
  69. graphcore/auth/authz/tests/test_lockout.py +132 -0
  70. graphcore/auth/authz/tests/test_policy.py +329 -0
  71. graphcore/auth/authz/tests/test_primitives.py +73 -0
  72. graphcore/auth/authz/tests/test_principal_creation.py +62 -0
  73. graphcore/bootstrap/__init__.py +0 -0
  74. graphcore/bootstrap/deployment.py +67 -0
  75. graphcore/bootstrap/signals.py +16 -0
  76. graphcore/bootstrap/tests/__init__.py +0 -0
  77. graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
  78. graphcore/broadcast/__init__.py +4 -0
  79. graphcore/broadcast/broadcaster.py +201 -0
  80. graphcore/broadcast/channels.py +56 -0
  81. graphcore/broadcast/tests/__init__.py +0 -0
  82. graphcore/broadcast/tests/test_broadcaster.py +444 -0
  83. graphcore/celery.py +14 -0
  84. graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
  85. graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
  86. graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
  87. graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
  88. graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
  89. graphcore/docs/RUNTIME.md +90 -0
  90. graphcore/docs/SCHEDULING_TDD.md +244 -0
  91. graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
  92. graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
  93. graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
  94. graphcore/docs/__init__.py +0 -0
  95. graphcore/docs/doc_topics.py +97 -0
  96. graphcore/docs/docs_discovery.py +64 -0
  97. graphcore/docs/public_api.py +56 -0
  98. graphcore/domain/__init__.py +0 -0
  99. graphcore/domain/entity/Entity.py +204 -0
  100. graphcore/domain/entity/__init__.py +0 -0
  101. graphcore/domain/entity/deployment/Deployment.py +25 -0
  102. graphcore/domain/entity/deployment/__init__.py +0 -0
  103. graphcore/domain/entity/grant/Grant.py +31 -0
  104. graphcore/domain/entity/grant/__init__.py +0 -0
  105. graphcore/domain/entity/group/Group.py +48 -0
  106. graphcore/domain/entity/group/README.md +88 -0
  107. graphcore/domain/entity/group/__init__.py +0 -0
  108. graphcore/domain/entity/invite/Invite.py +31 -0
  109. graphcore/domain/entity/invite/__init__.py +0 -0
  110. graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
  111. graphcore/domain/entity/request_template/__init__.py +0 -0
  112. graphcore/domain/entity/request_template/tests/__init__.py +0 -0
  113. graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
  114. graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
  115. graphcore/domain/entity/service_account/__init__.py +0 -0
  116. graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
  117. graphcore/domain/entity/strategy_request/__init__.py +0 -0
  118. graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
  119. graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
  120. graphcore/domain/entity/tests/__init__.py +0 -0
  121. graphcore/domain/entity/tests/test_entity.py +227 -0
  122. graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
  123. graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
  124. graphcore/domain/entity/user/User.py +39 -0
  125. graphcore/domain/entity/user/__init__.py +0 -0
  126. graphcore/domain/entity/user/tests/__init__.py +0 -0
  127. graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
  128. graphcore/domain/entity/view/View.py +37 -0
  129. graphcore/domain/entity/view/__init__.py +0 -0
  130. graphcore/domain/entity/view/position.py +78 -0
  131. graphcore/domain/entity/view/tests/__init__.py +0 -0
  132. graphcore/domain/entity/view/tests/test_position.py +71 -0
  133. graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
  134. graphcore/domain/strategy/BaseStrategy.py +759 -0
  135. graphcore/domain/strategy/DeploymentStrategies.py +55 -0
  136. graphcore/domain/strategy/GrantStrategies.py +189 -0
  137. graphcore/domain/strategy/GroupStrategies.py +193 -0
  138. graphcore/domain/strategy/InviteStrategies.py +84 -0
  139. graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
  140. graphcore/domain/strategy/ViewStrategies.py +219 -0
  141. graphcore/domain/strategy/__init__.py +0 -0
  142. graphcore/domain/strategy/runner.py +84 -0
  143. graphcore/domain/strategy/session.py +56 -0
  144. graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
  145. graphcore/domain/strategy/tests/__init__.py +0 -0
  146. graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
  147. graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
  148. graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
  149. graphcore/domain/strategy/tests/test_runner.py +220 -0
  150. graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
  151. graphcore/domain/strategy/tests/test_utils.py +95 -0
  152. graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
  153. graphcore/errors.py +145 -0
  154. graphcore/execution/README.md +97 -0
  155. graphcore/execution/StrategyExecutorService.py +333 -0
  156. graphcore/execution/__init__.py +0 -0
  157. graphcore/execution/dispatcher.py +19 -0
  158. graphcore/execution/environments/__init__.py +0 -0
  159. graphcore/execution/environments/celery.py +59 -0
  160. graphcore/execution/environments/in_process.py +55 -0
  161. graphcore/execution/local_session.py +117 -0
  162. graphcore/execution/redis_run_events.py +74 -0
  163. graphcore/execution/requests.py +22 -0
  164. graphcore/execution/run_events.py +85 -0
  165. graphcore/execution/strategy_context.py +54 -0
  166. graphcore/execution/tasks.py +34 -0
  167. graphcore/execution/tests/__init__.py +5 -0
  168. graphcore/execution/tests/contract.py +171 -0
  169. graphcore/execution/tests/support.py +310 -0
  170. graphcore/execution/tests/test_dispatchers.py +76 -0
  171. graphcore/execution/tests/test_executor.py +368 -0
  172. graphcore/execution/tests/test_local_session.py +95 -0
  173. graphcore/execution/tests/test_reconcile_runs.py +134 -0
  174. graphcore/execution/tests/test_run_events.py +106 -0
  175. graphcore/execution/tests/test_strategy_context.py +62 -0
  176. graphcore/gateway/__init__.py +1 -0
  177. graphcore/gateway/remote.py +35 -0
  178. graphcore/gateway/tests/__init__.py +0 -0
  179. graphcore/gateway/tests/test_remote.py +30 -0
  180. graphcore/graph/EntityService.py +710 -0
  181. graphcore/graph/__init__.py +0 -0
  182. graphcore/graph/entity_cache.py +152 -0
  183. graphcore/graph/tests/__init__.py +4 -0
  184. graphcore/graph/tests/test_EntityService.py +908 -0
  185. graphcore/graph/tests/test_EntityService_list.py +170 -0
  186. graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
  187. graphcore/graph/tests/test_EntityService_referrers.py +236 -0
  188. graphcore/graph/tests/test_EntityService_routing.py +119 -0
  189. graphcore/graph/tests/test_dependent_on.py +141 -0
  190. graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
  191. graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
  192. graphcore/graph/tests/test_entity_cache.py +159 -0
  193. graphcore/graph/tests/test_null_entity_cache.py +12 -0
  194. graphcore/management/__init__.py +0 -0
  195. graphcore/management/commands/__init__.py +0 -0
  196. graphcore/management/commands/dev_reset.py +18 -0
  197. graphcore/management/commands/graphcore_createadmin.py +65 -0
  198. graphcore/management/commands/graphcore_policy.py +59 -0
  199. graphcore/management/commands/graphcore_status.py +98 -0
  200. graphcore/management/tests/__init__.py +0 -0
  201. graphcore/management/tests/test_createadmin.py +78 -0
  202. graphcore/management/tests/test_policy_command.py +46 -0
  203. graphcore/management/tests/test_status_command.py +85 -0
  204. graphcore/migrations/0001_initial.py +186 -0
  205. graphcore/migrations/0002_invite.py +26 -0
  206. graphcore/migrations/__init__.py +0 -0
  207. graphcore/models.py +11 -0
  208. graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
  209. graphcore/persistence/__init__.py +6 -0
  210. graphcore/persistence/django/__init__.py +0 -0
  211. graphcore/persistence/django/backends.py +43 -0
  212. graphcore/persistence/django/bindings.py +28 -0
  213. graphcore/persistence/django/cache.py +38 -0
  214. graphcore/persistence/django/compiler.py +214 -0
  215. graphcore/persistence/django/identity.py +18 -0
  216. graphcore/persistence/django/models.py +252 -0
  217. graphcore/persistence/django/repository.py +525 -0
  218. graphcore/persistence/django/tests/__init__.py +0 -0
  219. graphcore/persistence/django/tests/test_backends.py +68 -0
  220. graphcore/persistence/django/tests/test_compiler.py +269 -0
  221. graphcore/persistence/django/tests/test_repository.py +472 -0
  222. graphcore/persistence/django/tests/test_shared_column.py +120 -0
  223. graphcore/persistence/django/tests/test_threads.py +50 -0
  224. graphcore/persistence/django/tests/test_update_shape.py +175 -0
  225. graphcore/persistence/django/threads.py +38 -0
  226. graphcore/persistence/repository.py +172 -0
  227. graphcore/persistence/tests/__init__.py +0 -0
  228. graphcore/persistence/tests/contract.py +688 -0
  229. graphcore/persistence/tests/evaluate.py +51 -0
  230. graphcore/persistence/tests/fixtures.py +32 -0
  231. graphcore/persistence/tests/memory.py +263 -0
  232. graphcore/persistence/tests/test_memory_repository.py +10 -0
  233. graphcore/persistence/tests/test_query_types.py +127 -0
  234. graphcore/public/__init__.py +9 -0
  235. graphcore/public/api.py +9 -0
  236. graphcore/public/apps.py +10 -0
  237. graphcore/public/auth/__init__.py +1 -0
  238. graphcore/public/auth/grants.py +7 -0
  239. graphcore/public/auth/onboarding.py +8 -0
  240. graphcore/public/auth/perms.py +7 -0
  241. graphcore/public/auth/policy.py +10 -0
  242. graphcore/public/auth/roles.py +8 -0
  243. graphcore/public/auth/subjects.py +7 -0
  244. graphcore/public/entities/__init__.py +32 -0
  245. graphcore/public/errors.py +19 -0
  246. graphcore/public/persistence/__init__.py +44 -0
  247. graphcore/public/persistence/django.py +24 -0
  248. graphcore/public/scheduling.py +23 -0
  249. graphcore/public/strategies/__init__.py +85 -0
  250. graphcore/public/test_settings.py +16 -0
  251. graphcore/public/testing.py +47 -0
  252. graphcore/registry/EntityDirectory.py +246 -0
  253. graphcore/registry/README.md +147 -0
  254. graphcore/registry/StrategyDirectory.py +308 -0
  255. graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
  256. graphcore/registry/__init__.py +0 -0
  257. graphcore/registry/app_discovery.py +108 -0
  258. graphcore/registry/decorators.py +77 -0
  259. graphcore/registry/discovery.py +111 -0
  260. graphcore/registry/registry.py +79 -0
  261. graphcore/registry/tests/__init__.py +0 -0
  262. graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
  263. graphcore/registry/tests/test_app_config.py +36 -0
  264. graphcore/registry/tests/test_discovery.py +147 -0
  265. graphcore/registry/tests/test_entity_directory_storage.py +73 -0
  266. graphcore/registry/tests/test_import_package.py +87 -0
  267. graphcore/registry/tests/test_principal_flag.py +67 -0
  268. graphcore/registry/tests/test_strategy_directory.py +29 -0
  269. graphcore/routes/__init__.py +116 -0
  270. graphcore/routes/tests/__init__.py +0 -0
  271. graphcore/routes/tests/test_routes.py +90 -0
  272. graphcore/runtime.py +243 -0
  273. graphcore/scheduling/__init__.py +0 -0
  274. graphcore/scheduling/beat_scheduler.py +164 -0
  275. graphcore/scheduling/beat_tasks.py +63 -0
  276. graphcore/scheduling/scheduler.py +37 -0
  277. graphcore/scheduling/tests/__init__.py +0 -0
  278. graphcore/scheduling/tests/contract.py +136 -0
  279. graphcore/scheduling/tests/memory.py +42 -0
  280. graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
  281. graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
  282. graphcore/scheduling/tests/test_ticks.py +143 -0
  283. graphcore/tasks.py +9 -0
  284. graphcore/test_settings.py +78 -0
  285. graphcore/testing/__init__.py +0 -0
  286. graphcore/testing/case.py +112 -0
  287. graphcore/testing/runner.py +32 -0
  288. graphcore/testing/settings.py +26 -0
  289. graphcore/testing/tests/__init__.py +0 -0
  290. graphcore/testing/tests/agent_view_fixture.py +19 -0
  291. graphcore/testing/tests/test_case_and_runner.py +219 -0
  292. graphcore/testing/tests/test_settings_for_apps.py +50 -0
  293. graphcore/tests/__init__.py +0 -0
  294. graphcore/tests/ids.py +14 -0
  295. graphcore/tests/public_api_manifest.py +102 -0
  296. graphcore/tests/runs.py +7 -0
  297. graphcore/tests/test_app_discovery.py +90 -0
  298. graphcore/tests/test_composition_boundaries.py +231 -0
  299. graphcore/tests/test_docs_discovery.py +108 -0
  300. graphcore/tests/test_no_print.py +70 -0
  301. graphcore/tests/test_packaging.py +64 -0
  302. graphcore/tests/test_public_api_boundaries.py +229 -0
  303. graphcore/tests/test_public_api_contract.py +62 -0
  304. graphcore/tests/test_public_api_docs.py +28 -0
  305. graphcore/tests/test_runtime.py +42 -0
  306. graphcore/tests/test_shared_type_boundaries.py +128 -0
  307. graphcore/tests/test_shared_types.py +144 -0
  308. graphcore/tests/test_test_settings.py +45 -0
  309. graphcore/transport/__init__.py +0 -0
  310. graphcore/transport/agent_asgi.py +68 -0
  311. graphcore/transport/agent_client.py +85 -0
  312. graphcore/transport/channel_layer.py +68 -0
  313. graphcore/transport/consumers.py +507 -0
  314. graphcore/transport/routing.py +6 -0
  315. graphcore/transport/tests/__init__.py +0 -0
  316. graphcore/transport/tests/test_agent_client.py +31 -0
  317. graphcore/transport/tests/test_api_views.py +733 -0
  318. graphcore/transport/tests/test_channel_layer.py +96 -0
  319. graphcore/transport/tests/test_consumers.py +963 -0
  320. graphcore/transport/tests/test_resource_metadata.py +67 -0
  321. graphcore/transport/urls.py +29 -0
  322. graphcore/transport/views.py +358 -0
  323. graphcore/types/__init__.py +4 -0
  324. graphcore/types/access.py +114 -0
  325. graphcore/types/api.py +76 -0
  326. graphcore/types/entities.py +100 -0
  327. graphcore/types/entity_id.py +85 -0
  328. graphcore/types/execution.py +41 -0
  329. graphcore/types/identity.py +20 -0
  330. graphcore/types/onboarding.py +20 -0
  331. graphcore/types/query.py +116 -0
  332. graphcore/types/schedules.py +91 -0
  333. graphcore/types/strategy.py +73 -0
  334. graphcore/types/tests/__init__.py +0 -0
  335. graphcore/types/tests/test_entity_id.py +50 -0
  336. graphcore/urls.py +5 -0
  337. graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
  338. graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
  339. graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
  340. graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
  341. graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
  342. graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
  343. graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,79 @@
1
+ """A client `ancestor_ids` predicate returns exactly the readable descendants."""
2
+ from asgiref.sync import async_to_sync
3
+ from django.test import TransactionTestCase
4
+
5
+ from graphcore.api.predicate_codec import decode_predicate
6
+ from graphcore.domain.entity.Entity import Entity
7
+ from graphcore.domain.entity.grant.Grant import Grant
8
+ from graphcore.domain.entity.user.User import User
9
+ from graphcore.execution.strategy_context import kernel_internal_context
10
+ from graphcore.registry.registry import DEFAULT_REGISTRY
11
+ from graphcore.runtime import django_runtime
12
+ from graphcore.types.entities import EntityFilter
13
+ from graphcore.types.identity import Principal
14
+
15
+
16
+ class _SubtreeNode(Entity):
17
+ @classmethod
18
+ def get_entity_type_id(cls) -> str:
19
+ return "graphcore._SubtreeNode"
20
+
21
+
22
+ REGISTRY = DEFAULT_REGISTRY.fork_open()
23
+ REGISTRY.entities.register_entity(_SubtreeNode, app_label='graphcore')
24
+ REGISTRY.policy.define(_SubtreeNode, shareable=True, can_contain=[_SubtreeNode, Grant])
25
+
26
+
27
+ class SubtreeQueryTests(TransactionTestCase):
28
+ OWNER = 'subtree-owner'
29
+
30
+ def setUp(self):
31
+ self.api = django_runtime(REGISTRY).graph_api
32
+ self.owner = Principal(principal_entity_id=self.OWNER)
33
+
34
+ async def _viewer(self):
35
+ user = self.api.entity_service.create(User)
36
+ user.username = 'subtree-viewer'
37
+ with kernel_internal_context():
38
+ user = await self.api.entity_service.save_entity(user, self.owner)
39
+ return user.entity_id, Principal(principal_entity_id=user.entity_id)
40
+
41
+ async def _node(self, parent=None):
42
+ node = self.api.entity_service.create(_SubtreeNode)
43
+ node.owner = self.OWNER
44
+ if parent is not None:
45
+ node.set_parent(parent.entity_id)
46
+ with kernel_internal_context():
47
+ return await self.api.entity_service.save_entity(node, self.owner)
48
+
49
+ async def _list(self, principal, root_id):
50
+ where = decode_predicate({'contains_any': {'field': 'ancestor_ids', 'values': [root_id]}})
51
+ type_id = REGISTRY.entities.get_entity_type_id(_SubtreeNode)
52
+ page = await self.api.list_entities(
53
+ principal, EntityFilter(type_ids=frozenset({type_id}), where=where),
54
+ )
55
+ return {entity.entity_id for entity in page.entities}
56
+
57
+ @async_to_sync
58
+ async def test_returns_descendants_and_not_the_root(self):
59
+ root = await self._node()
60
+ child = await self._node(root)
61
+ grandchild = await self._node(child)
62
+
63
+ found = await self._list(self.owner, root.entity_id)
64
+
65
+ self.assertEqual(found, {child.entity_id, grandchild.entity_id})
66
+
67
+ @async_to_sync
68
+ async def test_omits_descendants_the_principal_cannot_read(self):
69
+ root = await self._node()
70
+ readable = await self._node(root)
71
+ readable_child = await self._node(readable)
72
+ hidden = await self._node(root)
73
+ viewer_id, viewer = await self._viewer()
74
+ await self.api.grant(self.owner, readable.entity_id, viewer_id, 'viewer')
75
+
76
+ found = await self._list(viewer, root.entity_id)
77
+
78
+ self.assertEqual(found, {readable.entity_id, readable_child.entity_id})
79
+ self.assertNotIn(hidden.entity_id, found)
graphcore/apps.py ADDED
@@ -0,0 +1,71 @@
1
+ from django.apps import AppConfig
2
+
3
+ __all__ = ["GraphCoreAppConfig"]
4
+
5
+
6
+ class GraphCoreAppConfig(AppConfig):
7
+ """
8
+ Base config for a graphcore app.
9
+
10
+ Subclass it and declare ``graphcore_app_id`` — a stable identifier, independent
11
+ of the package's location on disk, that namespaces the app's entity type ids
12
+ and keys its per-user mount folder. Renaming the directory is safe; changing
13
+ this id is not.
14
+ """
15
+
16
+ # Never the auto-selected config for an app package; subclasses opt in.
17
+ default = False
18
+ default_auto_field = 'django.db.models.BigAutoField'
19
+ graphcore_app_id: str = None
20
+ # Packages within the app that hold decorated classes.
21
+ graphcore_packages = ('entities', 'strategies')
22
+ # Optional dotted path to configure this app's authorization policy.
23
+ graphcore_authorization: str = None
24
+ # Optional dotted path to a `configure(entities: EntityDirectory)`
25
+ # function that binds this app's entity types to storage.
26
+ graphcore_persistence: str = None
27
+ # Optional dotted path to `(principal, deployment) -> Onboarding | None`,
28
+ # run when a principal is first created.
29
+ graphcore_principal_created: str = None
30
+ # Optional dotted path to a `configure(routes: RouteTable)` function that
31
+ # adds this app's routes (`routes.add('/my-tasks', 'tasks.my_tasks')`).
32
+ graphcore_routes: str = None
33
+ # Confidential clients (client-credentials grant) for processes the app
34
+ # ships, registered at the issuer as `<app_id>-<name>`; their tokens
35
+ # authenticate as a `ServiceAccount`.
36
+ graphcore_service_clients: tuple[str, ...] = ()
37
+
38
+ def ready(self):
39
+ if self.graphcore_app_id is None:
40
+ raise ValueError(f"{type(self).__name__} must declare a graphcore_app_id")
41
+
42
+
43
+ class GraphCoreConfig(GraphCoreAppConfig):
44
+ """The framework's own app: kernel entity types live here."""
45
+
46
+ default = True
47
+ name = 'graphcore'
48
+ graphcore_app_id = 'graphcore'
49
+ graphcore_packages = ('domain.entity', 'domain.strategy')
50
+ graphcore_persistence = 'graphcore.persistence.django.bindings.configure'
51
+ graphcore_authorization = 'graphcore.auth.authz.kernel_policy.configure'
52
+ graphcore_principal_created = 'graphcore.auth.authn.onboarding.default_onboarding'
53
+
54
+ def ready(self):
55
+ super().ready()
56
+ # Deferred: `graphcore.public.apps` re-exports this module and must
57
+ # import before `django.setup()`.
58
+ from django.core import checks
59
+ from django.db.models.signals import post_migrate
60
+
61
+ import graphcore.celery # noqa: F401 — configures the Celery app from settings
62
+ from graphcore.bootstrap.signals import seed_on_migrate
63
+ from graphcore.persistence.django import identity
64
+ from graphcore.persistence.django.backends import check_database_vendor
65
+ from graphcore.registry.discovery import discover_all
66
+
67
+ checks.register(check_database_vendor)
68
+ identity.connect(self)
69
+ # After the backend identity: seeding mints ids from it.
70
+ post_migrate.connect(seed_on_migrate, sender=self, dispatch_uid='graphcore_deployment_seed')
71
+ discover_all()
@@ -0,0 +1 @@
1
+ """Authentication (authn) and authorization (authz)."""
@@ -0,0 +1,178 @@
1
+ # Authentication
2
+
3
+ How a request becomes a principal. The kernel is an OAuth 2.0 resource
4
+ server and nothing more: it verifies bearer tokens issued by an OIDC
5
+ provider it is configured to trust, and maps each token's subject to a
6
+ `User` of this deployment. It issues no tokens, keeps no passwords,
7
+ shows no login page, and never writes to the issuer. Which issuer a
8
+ deployment trusts, and how that issuer is run, is deployment configuration;
9
+ the defaults the distro provides are described in
10
+ `graphcore docs distro/identity-infrastructure`.
11
+
12
+ Authorization, what a principal may do, is a separate subsystem:
13
+ `graphcore docs kernel/authorization-technical-tdd`.
14
+
15
+ ## Definitions
16
+
17
+ | Term | Meaning |
18
+ |---|---|
19
+ | Issuer (`iss`) | The system that signs tokens, identified by its URL. Its discovery document, `<issuer>/.well-known/openid-configuration`, names its endpoints and JWKS. |
20
+ | Subject (`sub`) | The issuer's stable identifier for one principal. |
21
+ | Audience (`aud`) | Which resource server a token is for. A deployment accepts only tokens that name its audience. |
22
+ | Principal | Who a request acts as: `graphcore.types.identity.Principal`, naming a `User` or a `ServiceAccount`. |
23
+ | Trusted issuer | One entry of `GRAPHCORE_TRUSTED_ISSUERS`: an issuer this deployment accepts tokens from, with the audience it requires. |
24
+ | Protected resource metadata | RFC 9728: the document at `/.well-known/oauth-protected-resource` naming this deployment's trusted issuers. |
25
+ | Service account | A process an app ships, signing in with a confidential client the app declares (`graphcore_service_clients`). Its principal is a `ServiceAccount`. |
26
+ | First contact | The first authenticated request from an `(iss, sub)` this deployment has not seen. It creates the `User`, or binds the `ServiceAccount` of a declared client. |
27
+
28
+ ## Configuration
29
+
30
+ ```python
31
+ GRAPHCORE_TRUSTED_ISSUERS = [{
32
+ 'issuer': 'https://id.example.com/realms/notes', # public URL; matched against `iss`
33
+ 'audience': 'notes', # required in `aud`
34
+ 'internal_url': 'http://keycloak:8080/realms/notes', # optional: where this process fetches discovery and keys
35
+ }]
36
+ GRAPHCORE_PUBLIC_URL = 'https://notes.example.com' # optional: this deployment's origin behind a proxy
37
+ ```
38
+
39
+ - `issuer` and `audience` are required. An entry without both raises
40
+ `ImproperlyConfigured` when the list is first read.
41
+ - Several entries are allowed. A token is checked against the entry whose
42
+ `issuer` equals its `iss`.
43
+ - `internal_url` changes only where discovery and the JWKS are fetched.
44
+ `iss` is always matched against the public `issuer`, and a `jwks_uri`
45
+ under the public URL is rewritten onto the internal one.
46
+ - Nothing names a provider. Any issuer that publishes OIDC discovery and
47
+ signs with RS256 works.
48
+
49
+ ## Verifying a token
50
+
51
+ `graphcore.auth.authn.authenticator.verify_token`:
52
+
53
+ 1. Decode the token without verifying it, to read `iss`. An `iss` that
54
+ matches no trusted issuer is rejected.
55
+ 2. Fetch that issuer's discovery document and the signing key from its
56
+ JWKS. Both are cached in the process; a key id the cache does not hold is
57
+ fetched on demand.
58
+ 3. Verify the signature (RS256 only), `iss`, `aud` and `exp`. `exp`, `iss`,
59
+ `sub` and `aud` must all be present.
60
+
61
+ A failure at any step rejects the token. The caller gets `None`; the reason
62
+ is logged, never returned to the client. An unreachable issuer rejects the
63
+ token too: the kernel does not skip the check or fall back to a stale key.
64
+
65
+ Both transports take the same token: `Authorization: Bearer <token>` on
66
+ HTTP, and the `token` query parameter on the WebSocket.
67
+
68
+ ## Mapping a token to a principal
69
+
70
+ `authenticate` turns a valid token's claims into a `Principal`:
71
+
72
+ - The `User` is found by `(issuer, subject)`, a pair the store keeps
73
+ unique (`graphcore.auth.authn.users.find_user_by_identity`). Nothing else
74
+ identifies a person: not username, not email.
75
+ - **First contact.** With no match, the kernel creates the `User`
76
+ (`graphcore.auth.authn.principals.create_principal`) with `issuer`,
77
+ `subject` and a username taken from `preferred_username`, else `email`,
78
+ else `sub`, then runs onboarding: the groups and deployment roles a new
79
+ principal receives (`graphcore docs distro/deployment-access`). Two
80
+ concurrent first sign-ins race on the unique key; the loser re-reads and
81
+ uses the winner's entity.
82
+ - **Service accounts.** A token whose `azp` is a client an installed app
83
+ declares in `graphcore_service_clients`, named `<app_id>-<name>` at the
84
+ issuer, belongs to a service account
85
+ (`graphcore.auth.authn.service_accounts`). It maps to the
86
+ `ServiceAccount` of that client instead of a `User`.
87
+ - **Identified by its declaration.** Every `migrate` ensures one
88
+ `ServiceAccount` per declared client, keyed by `client_id` and holding
89
+ `app_id` and `client_name`, before any token arrives. Apps and people
90
+ find it with an ordinary query on `client_id` and grant it access
91
+ before the process first runs.
92
+ - **Bound on first contact.** The first token records the account's
93
+ `issuer`; a later token from another issuer is refused. The token's
94
+ `sub` is not kept: the client secret is the credential, so a client
95
+ recreated at the issuer, whose tokens carry a new `sub`, keeps its
96
+ account.
97
+ - **Not onboarded.** A service account joins no groups and receives no
98
+ deployment roles, so beyond what is granted to `PUBLIC` it holds only
99
+ what is granted to it. Those clients allow only the client-credentials
100
+ grant, so no person can obtain a token that maps to one.
101
+ - **Read like users, written by no one.** Every signed-in principal may
102
+ read service accounts, unless `GRAPHCORE_PUBLIC_USER_READ` is `False`,
103
+ which turns that off for users and service accounts alike. No strategy
104
+ may change or delete one.
105
+ - A changed `preferred_username` is written back on sign-in.
106
+ - The `Principal` carries `principal_entity_id`, `iss`, `sub`, the raw
107
+ `token` and `expires_at`. The token is kept so a strategy that crosses to
108
+ another deployment can present it for exchange. Background executions
109
+ (Celery, Beat) reconstruct a `Principal` with the identity only; they hold
110
+ no token and cannot make delegated calls.
111
+
112
+ The frontend learns its own `principal_entity_id` from
113
+ `GET /graphcore/api/auth/me/`, because issuer tokens carry no GraphCore
114
+ identifiers.
115
+
116
+ ## Telling clients where to sign in
117
+
118
+ The kernel publishes RFC 9728 protected resource metadata, and every 401
119
+ points at it:
120
+
121
+ ```
122
+ GET /.well-known/oauth-protected-resource[/mcp]
123
+ {"resource": "<origin>[/mcp]", "authorization_servers": ["<issuer>", …],
124
+ "bearer_methods_supported": ["header"], "scopes_supported": ["openid"]}
125
+
126
+ 401 WWW-Authenticate: Bearer resource_metadata="<origin>/.well-known/oauth-protected-resource"
127
+ ```
128
+
129
+ `<origin>` is `GRAPHCORE_PUBLIC_URL` when set, else the request's scheme and
130
+ host, which must pass `ALLOWED_HOSTS` (localhost under `DEBUG`) so a forged
131
+ `Host` is never echoed.
132
+
133
+ Clients start from this document, never from a configured issuer URL:
134
+
135
+ | Client | Flow at the issuer |
136
+ |---|---|
137
+ | Web frontend | Authorization code + PKCE as a public client; silent refresh |
138
+ | `graphcore` CLI | Device authorization grant (RFC 8628); refresh token kept in the deployment's session file |
139
+ | MCP host | 401 → resource metadata → issuer metadata → dynamic client registration (RFC 7591) → code + PKCE |
140
+
141
+ ## What the kernel requires of an issuer
142
+
143
+ - OIDC discovery and an RS256 JWKS.
144
+ - Tokens carrying `iss`, `sub`, `aud` and `exp`, with `aud` including the
145
+ deployment's audience.
146
+ - The grants its clients use: code + PKCE for browsers, the device grant
147
+ for the CLI, and dynamic client registration or a pre-registered client
148
+ for MCP hosts.
149
+ - For delegation between deployments, a token exchange the interop client
150
+ can request: RFC 8693 within one issuer, RFC 8693 followed by RFC 7523
151
+ across issuers. The model is `docs/IDENTITY.md` in the monorepo. A read
152
+ of an entity another deployment owns, for which no delegated token is
153
+ obtained, resolves to a `Tombstone` (`graphcore.gateway.remote`).
154
+ - A service client an app declares (`graphcore_service_clients`) is a
155
+ confidential client of the deployment's issuer, `<app_id>-<name>`, allowed
156
+ only the client-credentials grant. Its tokens carry the client in `azp`.
157
+
158
+ ## Invariants
159
+
160
+ - A principal is identified across any boundary by `(iss, sub)` and by
161
+ nothing else.
162
+ - The wire never carries a principal field. The principal is derived from
163
+ the connection's token.
164
+ - Only an identity path creates a `User` (first contact, or
165
+ `graphcore createadmin`) and only seeding or first contact creates a
166
+ `ServiceAccount`. No strategy creates or changes either.
167
+ - A `ServiceAccount` is never onboarded.
168
+ - The kernel verifies and maps. It never issues a token, exchanges one on
169
+ its own initiative, or writes to an issuer.
170
+ - Authentication decides who; authorization decides what. A valid token
171
+ grants nothing by itself.
172
+
173
+ ## Testing
174
+
175
+ `graphcore.auth.authn.testing.TestIssuer` is an in-memory issuer: it signs
176
+ tokens with its own key, serves a JWKS, and `trusted()` installs it as a
177
+ trusted issuer with discovery and key fetches stubbed, so authentication
178
+ tests run without a network or a provider.
@@ -0,0 +1,3 @@
1
+ from graphcore.auth.authn.authenticator import authenticate
2
+
3
+ __all__ = ['authenticate']
@@ -0,0 +1,120 @@
1
+ """Token → Principal. Verifies RS256 tokens from ``GRAPHCORE_TRUSTED_ISSUERS``
2
+ against the issuer's JWKS (found through OIDC discovery), then maps
3
+ ``(iss, sub)`` to a local User, or to a ServiceAccount when the token was
4
+ issued to a client an app declares."""
5
+ import json
6
+ import logging
7
+ import urllib.request
8
+
9
+ from django.conf import settings
10
+ from django.core.exceptions import ImproperlyConfigured
11
+ from jwt import PyJWKClient
12
+ import jwt
13
+
14
+ from graphcore.types.identity import Principal
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ _DISCOVERY_PATH = '/.well-known/openid-configuration'
19
+ _discovery: dict[str, dict] = {}
20
+ _jwks_clients: dict[str, PyJWKClient] = {}
21
+
22
+
23
+ def trusted_issuers() -> dict[str, dict]:
24
+ """Settings entries keyed by public issuer URL: ``issuer``, ``audience``
25
+ and an optional ``internal_url`` (the issuer URL as the backend reaches it)."""
26
+ issuers = {}
27
+ for entry in getattr(settings, 'GRAPHCORE_TRUSTED_ISSUERS', []):
28
+ if not entry.get('issuer') or not entry.get('audience'):
29
+ raise ImproperlyConfigured('GRAPHCORE_TRUSTED_ISSUERS entries need an issuer and an audience')
30
+ issuers[entry['issuer']] = entry
31
+ return issuers
32
+
33
+
34
+ def clear_caches() -> None:
35
+ _discovery.clear()
36
+ _jwks_clients.clear()
37
+
38
+
39
+ def _fetch_json(url: str) -> dict:
40
+ with urllib.request.urlopen(url, timeout=10) as response:
41
+ return json.load(response)
42
+
43
+
44
+ def _jwks_uri(entry: dict) -> str:
45
+ issuer = entry['issuer'].rstrip('/')
46
+ base = (entry.get('internal_url') or issuer).rstrip('/')
47
+ document = _discovery.get(base)
48
+ if document is None:
49
+ document = _fetch_json(base + _DISCOVERY_PATH)
50
+ uri = document.get('jwks_uri') if isinstance(document, dict) else None
51
+ if not isinstance(uri, str) or not uri:
52
+ raise jwt.PyJWKClientError(f'Discovery document at {base} has no jwks_uri')
53
+ _discovery[base] = document
54
+ uri = document['jwks_uri']
55
+ if base != issuer and uri.startswith(issuer):
56
+ uri = base + uri[len(issuer):]
57
+ return uri
58
+
59
+
60
+ def _jwks_client(jwks_uri: str) -> PyJWKClient:
61
+ client = _jwks_clients.get(jwks_uri)
62
+ if client is None:
63
+ client = PyJWKClient(jwks_uri)
64
+ _jwks_clients[jwks_uri] = client
65
+ return client
66
+
67
+
68
+ def verify_token(token: str) -> dict | None:
69
+ """Claims of a valid token from a trusted issuer addressed to this
70
+ deployment's audience; ``None`` otherwise."""
71
+ try:
72
+ unverified = jwt.decode(token, options={'verify_signature': False, 'verify_exp': False})
73
+ except jwt.InvalidTokenError:
74
+ return None
75
+ entry = trusted_issuers().get(unverified.get('iss'))
76
+ if entry is None:
77
+ logger.info('Rejected token: issuer %r is not trusted', unverified.get('iss'))
78
+ return None
79
+ try:
80
+ key = _jwks_client(_jwks_uri(entry)).get_signing_key_from_jwt(token)
81
+ return jwt.decode(
82
+ token, key.key, algorithms=['RS256'],
83
+ issuer=entry['issuer'], audience=entry['audience'],
84
+ options={'require': ['exp', 'iss', 'sub', 'aud']},
85
+ )
86
+ except (jwt.InvalidTokenError, jwt.PyJWKClientError) as exc:
87
+ logger.info('Rejected token from %s: %s: %s', entry['issuer'], type(exc).__name__, exc)
88
+ return None
89
+ except (OSError, ValueError):
90
+ logger.warning('Could not reach issuer %s for discovery or JWKS', entry['issuer'], exc_info=True)
91
+ return None
92
+
93
+
94
+ async def authenticate(token: str | None) -> Principal | None:
95
+ if not token:
96
+ return None
97
+ payload = verify_token(token)
98
+ if not payload:
99
+ return None
100
+ uid = await _resolve_principal_entity_id(payload)
101
+ if uid is None:
102
+ return None
103
+ return Principal(
104
+ principal_entity_id=uid,
105
+ iss=payload['iss'],
106
+ sub=payload['sub'],
107
+ token=token,
108
+ expires_at=payload['exp'],
109
+ )
110
+
111
+
112
+ async def _resolve_principal_entity_id(payload: dict) -> str | None:
113
+ from graphcore.auth.authn.service_accounts import resolve_service_account, service_client_for
114
+ from graphcore.auth.authn.users import resolve_user
115
+ from graphcore.runtime import get_runtime
116
+
117
+ runtime = get_runtime()
118
+ if service_client_for(payload) is not None:
119
+ return await resolve_service_account(runtime.entity_service, payload)
120
+ return await resolve_user(runtime.entity_service, runtime.executor, payload)
@@ -0,0 +1,105 @@
1
+ """Running principal-created hooks and applying what they return. Hook code
2
+ runs outside any kernel-internal context; only the fixed membership and
3
+ grant writes below run inside one. See docs/AUTHORIZATION_TECHNICAL_TDD.md
4
+ (Identity-based policy, Onboarding)."""
5
+ from __future__ import annotations
6
+
7
+ import inspect
8
+ import logging
9
+
10
+ from graphcore.auth.authz.lookups import find_group_by_key
11
+ from graphcore.auth.authz.kernel_writes import add_member, grant
12
+ from graphcore.domain.entity.deployment.Deployment import Deployment
13
+ from graphcore.domain.entity.group.Group import Group
14
+ from graphcore.errors import KernelError
15
+ from graphcore.execution.strategy_context import kernel_internal_context
16
+ from graphcore.types.identity import Principal
17
+ from graphcore.types.onboarding import Onboarding
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+
22
+ def default_onboarding(principal, deployment: Deployment) -> Onboarding:
23
+ """The kernel's own hook: what administrators set on the deployment."""
24
+ return Onboarding(group_ids=list(deployment.new_user_group_ids), roles=list(deployment.new_user_roles))
25
+
26
+
27
+ async def _collect(hooks, principal, deployment) -> Onboarding:
28
+ merged = Onboarding()
29
+ for hook in hooks:
30
+ try:
31
+ result = hook(principal, deployment)
32
+ if inspect.isawaitable(result):
33
+ result = await result
34
+ except Exception:
35
+ logger.exception("Principal-created hook %r failed for %s", hook, principal.entity_id)
36
+ continue
37
+ if result is None:
38
+ continue
39
+ merged.groups += result.groups
40
+ merged.group_ids += result.group_ids
41
+ merged.roles += result.roles
42
+ merged.requests += result.requests
43
+ return merged
44
+
45
+
46
+ async def _apply_grants_and_memberships(entity_service, principal_id: str, onboarding: Onboarding) -> None:
47
+ registry = entity_service.registry
48
+ valid_roles = {role.name for role in registry.policy.available_roles(Deployment)}
49
+ with kernel_internal_context():
50
+ groups = []
51
+ for key in dict.fromkeys(onboarding.groups):
52
+ group = await find_group_by_key(entity_service, key)
53
+ if group is None:
54
+ logger.warning("Onboarding names unknown group key %r; skipped", key)
55
+ groups.append(group)
56
+ for group_id in dict.fromkeys(onboarding.group_ids):
57
+ group = await entity_service.repository.load(group_id)
58
+ if not isinstance(group, Group):
59
+ logger.warning("Onboarding names %r, which is not a group; skipped", group_id)
60
+ group = None
61
+ groups.append(group)
62
+ for group in groups:
63
+ if group is None:
64
+ continue
65
+ try:
66
+ await add_member(entity_service, group, principal_id, None)
67
+ except Exception:
68
+ logger.exception("Onboarding could not add %s to group %s", principal_id, group.entity_id)
69
+
70
+ for role_name in dict.fromkeys(onboarding.roles):
71
+ if role_name not in valid_roles:
72
+ logger.warning("Onboarding names unknown deployment role %r; skipped", role_name)
73
+ continue
74
+ try:
75
+ await grant(entity_service, entity_service.deployment_entity_id, principal_id, role_name, None)
76
+ except Exception:
77
+ logger.exception("Onboarding could not grant %r to %s", role_name, principal_id)
78
+
79
+
80
+ async def run_onboarding(entity_service, executor, principal) -> None:
81
+ """Never raises: a failure here leaves the principal with less access,
82
+ which administrators can see, rather than blocking sign-in."""
83
+ try:
84
+ deployment = await entity_service.repository.load(entity_service.deployment_entity_id)
85
+ if deployment is None:
86
+ logger.warning("No deployment entity; onboarding skipped for %s", principal.entity_id)
87
+ return
88
+ onboarding = await _collect(entity_service.registry.principal_hooks, principal, deployment)
89
+ await _apply_grants_and_memberships(entity_service, principal.entity_id, onboarding)
90
+ except Exception:
91
+ logger.exception("Onboarding failed for %s", principal.entity_id)
92
+ return
93
+
94
+ acting = Principal(principal_entity_id=principal.entity_id)
95
+ for request in onboarding.requests:
96
+ if executor is None:
97
+ logger.warning("Onboarding request %s skipped: no executor", request.strategy_name)
98
+ continue
99
+ try:
100
+ await executor.submit(request, acting)
101
+ except KernelError as exc:
102
+ logger.warning("Onboarding request %s failed for %s: %s",
103
+ request.strategy_name, principal.entity_id, exc)
104
+ except Exception:
105
+ logger.exception("Onboarding request %s failed for %s", request.strategy_name, principal.entity_id)
@@ -0,0 +1,20 @@
1
+ """The one way a `User` comes into being; a `ServiceAccount` is ensured from
2
+ its declaration in `service_accounts`. See
3
+ docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement, Principals are created
4
+ only by identity paths)."""
5
+ from __future__ import annotations
6
+
7
+ from graphcore.auth.authn.onboarding import run_onboarding
8
+ from graphcore.domain.entity.user.User import User
9
+ from graphcore.execution.strategy_context import kernel_internal_context
10
+ from graphcore.types.identity import Principal
11
+
12
+
13
+ async def create_principal(entity_service, executor=None, **fields) -> User:
14
+ """Mint and save a `User`, then onboard it. A `DuplicateEntity`
15
+ from a concurrent first sign-in propagates before any onboarding."""
16
+ user = entity_service.create(User, **fields)
17
+ with kernel_internal_context():
18
+ user = await entity_service.save_entity(user, Principal(principal_entity_id=user.entity_id))
19
+ await run_onboarding(entity_service, executor, user)
20
+ return user
@@ -0,0 +1,43 @@
1
+ """RFC 9728 protected resource metadata: how a client learns which issuers
2
+ this deployment trusts, starting from a 401 or a well-known URL."""
3
+ from django.conf import settings
4
+ from django.http.request import split_domain_port, validate_host
5
+
6
+ from graphcore.auth.authn.authenticator import trusted_issuers
7
+
8
+ WELL_KNOWN = '/.well-known/oauth-protected-resource'
9
+
10
+
11
+ def public_base(scheme: str, host: str) -> str:
12
+ """``GRAPHCORE_PUBLIC_URL`` when set (behind a proxy), else the request's origin."""
13
+ return (getattr(settings, 'GRAPHCORE_PUBLIC_URL', '') or f'{scheme}://{host}').rstrip('/')
14
+
15
+
16
+ def host_allowed(host: str) -> bool:
17
+ """Whether the request's Host may be echoed into metadata. Always true when
18
+ ``GRAPHCORE_PUBLIC_URL`` fixes the origin; otherwise it must pass ``ALLOWED_HOSTS``
19
+ as Django requires for its own views (localhost only under DEBUG)."""
20
+ if getattr(settings, 'GRAPHCORE_PUBLIC_URL', ''):
21
+ return True
22
+ allowed = list(settings.ALLOWED_HOSTS)
23
+ if settings.DEBUG and not allowed:
24
+ allowed = ['.localhost', '127.0.0.1', '[::1]']
25
+ domain, _ = split_domain_port(host)
26
+ return bool(domain) and validate_host(domain, allowed)
27
+
28
+
29
+ def metadata_url(base: str, resource_path: str = '') -> str:
30
+ return base + WELL_KNOWN + resource_path
31
+
32
+
33
+ def document(base: str, resource_path: str = '') -> dict:
34
+ return {
35
+ 'resource': base + resource_path,
36
+ 'authorization_servers': list(trusted_issuers()),
37
+ 'bearer_methods_supported': ['header'],
38
+ 'scopes_supported': ['openid'],
39
+ }
40
+
41
+
42
+ def challenge(base: str, resource_path: str = '') -> str:
43
+ return f'Bearer resource_metadata="{metadata_url(base, resource_path)}"'