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,96 @@
|
|
|
1
|
+
"""Which tokens are a service account's, and the account each maps to.
|
|
2
|
+
|
|
3
|
+
A token is a service account's when its `azp` (the client it was issued to)
|
|
4
|
+
is a client an installed app declares in `graphcore_service_clients`, named
|
|
5
|
+
`<app_id>-<name>` at the issuer. Those clients allow only the
|
|
6
|
+
client-credentials grant, so no person can obtain a token for one.
|
|
7
|
+
|
|
8
|
+
An account is identified by its declaration, its `client_id`, and exists
|
|
9
|
+
from `migrate` on, before its first token; that token binds its issuer.
|
|
10
|
+
The client secret is the credential, so the token's `sub` is not kept: a
|
|
11
|
+
client recreated at the issuer keeps its account.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import functools
|
|
16
|
+
import logging
|
|
17
|
+
from typing import NamedTuple, Optional
|
|
18
|
+
|
|
19
|
+
from django.apps import apps
|
|
20
|
+
|
|
21
|
+
from graphcore.domain.entity.service_account.ServiceAccount import ServiceAccount
|
|
22
|
+
from graphcore.errors import ConcurrentModificationError, DuplicateEntity
|
|
23
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
24
|
+
from graphcore.graph.EntityService import EntityService
|
|
25
|
+
from graphcore.types.query import AllOf, Equal
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ServiceClient(NamedTuple):
|
|
31
|
+
app_id: str
|
|
32
|
+
name: str
|
|
33
|
+
client_id: str
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@functools.cache
|
|
37
|
+
def declared_service_clients() -> dict[str, ServiceClient]:
|
|
38
|
+
"""Every installed app's declared service clients, by issuer client id."""
|
|
39
|
+
declared = {}
|
|
40
|
+
for config in apps.get_app_configs():
|
|
41
|
+
app_id = getattr(config, 'graphcore_app_id', None)
|
|
42
|
+
for name in getattr(config, 'graphcore_service_clients', ()) if app_id else ():
|
|
43
|
+
client_id = f'{app_id}-{name}'
|
|
44
|
+
declared[client_id] = ServiceClient(app_id, name, client_id)
|
|
45
|
+
return declared
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def service_client_for(payload: dict) -> Optional[ServiceClient]:
|
|
49
|
+
"""The declared service client a verified token was issued to, if any."""
|
|
50
|
+
azp = payload.get('azp')
|
|
51
|
+
return declared_service_clients().get(azp) if azp else None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def find_service_account(entity_service: EntityService, client_id: str) -> Optional[ServiceAccount]:
|
|
55
|
+
found = await entity_service.find_entities(AllOf((
|
|
56
|
+
Equal('entity_type_id', ServiceAccount.get_entity_type_id()),
|
|
57
|
+
Equal('client_id', client_id),
|
|
58
|
+
)))
|
|
59
|
+
return next(iter(found.values()), None)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def ensure_service_account(entity_service: EntityService, client: ServiceClient) -> ServiceAccount:
|
|
63
|
+
"""The `ServiceAccount` of a declared client, minted with no issuer yet
|
|
64
|
+
when missing. It is not onboarded: beyond grants to `PUBLIC`, it holds
|
|
65
|
+
only what is granted to it."""
|
|
66
|
+
existing = await find_service_account(entity_service, client.client_id)
|
|
67
|
+
if existing is not None:
|
|
68
|
+
return existing
|
|
69
|
+
account = entity_service.create(
|
|
70
|
+
ServiceAccount, app_id=client.app_id, client_name=client.name, client_id=client.client_id)
|
|
71
|
+
try:
|
|
72
|
+
with kernel_internal_context():
|
|
73
|
+
return await entity_service.save_entity(account, None)
|
|
74
|
+
except DuplicateEntity:
|
|
75
|
+
return await find_service_account(entity_service, client.client_id)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
async def resolve_service_account(entity_service: EntityService, claims: dict) -> Optional[str]:
|
|
79
|
+
"""The account of the declared client a verified token was issued to,
|
|
80
|
+
bound to the first issuer that signs one; a token from another issuer
|
|
81
|
+
maps to nothing."""
|
|
82
|
+
client = service_client_for(claims)
|
|
83
|
+
account = await ensure_service_account(entity_service, client)
|
|
84
|
+
if account.issuer is None:
|
|
85
|
+
account.issuer = claims['iss']
|
|
86
|
+
try:
|
|
87
|
+
with kernel_internal_context():
|
|
88
|
+
account = await entity_service.save_entity(account, None)
|
|
89
|
+
except ConcurrentModificationError:
|
|
90
|
+
# A concurrent first token bound it.
|
|
91
|
+
account = await find_service_account(entity_service, client.client_id)
|
|
92
|
+
if account.issuer != claims['iss']:
|
|
93
|
+
logger.warning('Token for service client %s from %s refused: the account is bound to %s',
|
|
94
|
+
client.client_id, claims['iss'], account.issuer)
|
|
95
|
+
return None
|
|
96
|
+
return account.entity_id
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""A trusted issuer for tests: signs RS256 tokens and answers discovery and
|
|
2
|
+
JWKS in-process, so tests authenticate the way production does."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import time
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from functools import cache
|
|
9
|
+
from unittest.mock import patch
|
|
10
|
+
|
|
11
|
+
import jwt
|
|
12
|
+
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
13
|
+
from django.test import override_settings
|
|
14
|
+
from jwt.algorithms import RSAAlgorithm
|
|
15
|
+
|
|
16
|
+
from graphcore.auth.authn import authenticator
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@cache
|
|
20
|
+
def _signing_key(name: str):
|
|
21
|
+
return rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TestIssuer:
|
|
25
|
+
__test__ = False
|
|
26
|
+
|
|
27
|
+
def __init__(self, issuer: str = 'https://issuer.test/realms/test', audience: str = 'graphcore'):
|
|
28
|
+
self.issuer = issuer
|
|
29
|
+
self.audience = audience
|
|
30
|
+
self.kid = f'{issuer}#key'
|
|
31
|
+
self._key = _signing_key(issuer)
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def settings_entry(self) -> dict:
|
|
35
|
+
return {'issuer': self.issuer, 'audience': self.audience}
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def jwks_uri(self) -> str:
|
|
39
|
+
return f'{self.issuer}/certs'
|
|
40
|
+
|
|
41
|
+
def token(self, sub: str, *, expires_in: int = 3600, **claims) -> str:
|
|
42
|
+
now = int(time.time())
|
|
43
|
+
payload = {
|
|
44
|
+
'iss': self.issuer, 'sub': sub, 'aud': self.audience,
|
|
45
|
+
'iat': now, 'exp': now + expires_in, 'preferred_username': sub,
|
|
46
|
+
**claims,
|
|
47
|
+
}
|
|
48
|
+
return jwt.encode(payload, self._key, algorithm='RS256', headers={'kid': self.kid})
|
|
49
|
+
|
|
50
|
+
def jwks(self) -> dict:
|
|
51
|
+
key = json.loads(RSAAlgorithm.to_jwk(self._key.public_key()))
|
|
52
|
+
key.update(kid=self.kid, use='sig', alg='RS256')
|
|
53
|
+
return {'keys': [key]}
|
|
54
|
+
|
|
55
|
+
@contextmanager
|
|
56
|
+
def trusted(self, *others: TestIssuer, entries: list[dict] | None = None):
|
|
57
|
+
"""Make these issuers the deployment's trusted issuers. ``entries``
|
|
58
|
+
overrides the settings value (e.g. to add ``internal_url``)."""
|
|
59
|
+
issuers = (self, *others)
|
|
60
|
+
documents = {}
|
|
61
|
+
for issuer in issuers:
|
|
62
|
+
documents[issuer.issuer + '/.well-known/openid-configuration'] = {
|
|
63
|
+
'issuer': issuer.issuer, 'jwks_uri': issuer.jwks_uri,
|
|
64
|
+
}
|
|
65
|
+
documents[issuer.jwks_uri] = issuer.jwks()
|
|
66
|
+
setting = entries if entries is not None else [i.settings_entry for i in issuers]
|
|
67
|
+
for entry in setting:
|
|
68
|
+
if internal := entry.get('internal_url'):
|
|
69
|
+
public = entry['issuer']
|
|
70
|
+
for url in list(documents):
|
|
71
|
+
if url.startswith(public):
|
|
72
|
+
documents[internal + url[len(public):]] = documents[url]
|
|
73
|
+
fetched: list[str] = []
|
|
74
|
+
|
|
75
|
+
def fetch_json(url):
|
|
76
|
+
fetched.append(url)
|
|
77
|
+
return documents[url]
|
|
78
|
+
|
|
79
|
+
def fetch_data(client):
|
|
80
|
+
fetched.append(client.uri)
|
|
81
|
+
return documents[client.uri]
|
|
82
|
+
|
|
83
|
+
authenticator.clear_caches()
|
|
84
|
+
try:
|
|
85
|
+
with override_settings(GRAPHCORE_TRUSTED_ISSUERS=setting), \
|
|
86
|
+
patch.object(authenticator, '_fetch_json', fetch_json), \
|
|
87
|
+
patch.object(jwt.PyJWKClient, 'fetch_data', fetch_data):
|
|
88
|
+
yield fetched
|
|
89
|
+
finally:
|
|
90
|
+
authenticator.clear_caches()
|
|
91
|
+
|
|
92
|
+
def bind(self, user, sub: str | None = None):
|
|
93
|
+
"""Give an unsaved ``User`` this issuer's identity."""
|
|
94
|
+
user.issuer = self.issuer
|
|
95
|
+
user.subject = sub or user.username
|
|
96
|
+
return user
|
|
File without changes
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
from unittest.mock import patch
|
|
2
|
+
|
|
3
|
+
import jwt
|
|
4
|
+
from django.conf import settings
|
|
5
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
6
|
+
from django.test import TestCase, override_settings
|
|
7
|
+
|
|
8
|
+
from graphcore.auth.authn import authenticate
|
|
9
|
+
from graphcore.auth.authn.testing import TestIssuer
|
|
10
|
+
from graphcore.domain.entity.user.User import User
|
|
11
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
12
|
+
from graphcore.runtime import get_runtime
|
|
13
|
+
from graphcore.types.identity import Principal
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TestAuthenticate(TestCase):
|
|
17
|
+
def setUp(self):
|
|
18
|
+
self.issuer = TestIssuer()
|
|
19
|
+
self.fetched = self.enterContext(self.issuer.trusted())
|
|
20
|
+
|
|
21
|
+
async def _save_user(self, username='alice', subject='alice'):
|
|
22
|
+
entity_service = get_runtime().entity_service
|
|
23
|
+
entity = entity_service.create(
|
|
24
|
+
User, username=username, issuer=self.issuer.issuer, subject=subject,
|
|
25
|
+
)
|
|
26
|
+
with kernel_internal_context():
|
|
27
|
+
return await entity_service.save_entity(entity, Principal(principal_entity_id=entity.entity_id))
|
|
28
|
+
|
|
29
|
+
async def test_maps_existing_user(self):
|
|
30
|
+
user = await self._save_user()
|
|
31
|
+
token = self.issuer.token('alice')
|
|
32
|
+
principal = await authenticate(token)
|
|
33
|
+
self.assertEqual(principal.principal_entity_id, user.entity_id)
|
|
34
|
+
self.assertEqual(principal.iss, self.issuer.issuer)
|
|
35
|
+
self.assertEqual(principal.sub, 'alice')
|
|
36
|
+
self.assertEqual(principal.token, token)
|
|
37
|
+
|
|
38
|
+
async def test_first_contact_creates_user(self):
|
|
39
|
+
principal = await authenticate(self.issuer.token('bob-sub', preferred_username='bob'))
|
|
40
|
+
from graphcore.persistence.django.models import UserModel
|
|
41
|
+
model = await UserModel.objects.aget(issuer=self.issuer.issuer, subject='bob-sub')
|
|
42
|
+
self.assertEqual(principal.principal_entity_id, str(model.entity_id))
|
|
43
|
+
self.assertEqual(model.username, 'bob')
|
|
44
|
+
|
|
45
|
+
async def test_preferred_username_change_resyncs_username(self):
|
|
46
|
+
user = await self._save_user(username='old-name')
|
|
47
|
+
await authenticate(self.issuer.token('alice', preferred_username='new-name'))
|
|
48
|
+
from graphcore.persistence.django.models import UserModel
|
|
49
|
+
model = await UserModel.objects.aget(entity_id=user.entity_id)
|
|
50
|
+
self.assertEqual(model.username, 'new-name')
|
|
51
|
+
|
|
52
|
+
async def test_first_sign_in_race_resolves_to_the_winner(self):
|
|
53
|
+
"""Another sign-in creates the user between our lookup and our save."""
|
|
54
|
+
from graphcore.auth.authn import users
|
|
55
|
+
|
|
56
|
+
winner = await self._save_user()
|
|
57
|
+
real_find_user = users.find_user_by_identity
|
|
58
|
+
lookups = []
|
|
59
|
+
|
|
60
|
+
async def find_user_missing_first_time(*args):
|
|
61
|
+
lookups.append(args)
|
|
62
|
+
return None if len(lookups) == 1 else await real_find_user(*args)
|
|
63
|
+
|
|
64
|
+
with patch.object(users, 'find_user_by_identity', find_user_missing_first_time):
|
|
65
|
+
principal = await authenticate(self.issuer.token('alice'))
|
|
66
|
+
|
|
67
|
+
self.assertEqual(principal.principal_entity_id, winner.entity_id)
|
|
68
|
+
self.assertEqual(len(lookups), 2)
|
|
69
|
+
|
|
70
|
+
async def test_discovery_and_jwks_come_from_the_issuer(self):
|
|
71
|
+
await authenticate(self.issuer.token('alice'))
|
|
72
|
+
self.assertEqual(self.fetched, [
|
|
73
|
+
self.issuer.issuer + '/.well-known/openid-configuration',
|
|
74
|
+
self.issuer.jwks_uri,
|
|
75
|
+
])
|
|
76
|
+
|
|
77
|
+
async def test_none_token_returns_none(self):
|
|
78
|
+
self.assertIsNone(await authenticate(None))
|
|
79
|
+
|
|
80
|
+
async def test_garbage_token_returns_none(self):
|
|
81
|
+
self.assertIsNone(await authenticate('not-a-real-token'))
|
|
82
|
+
|
|
83
|
+
async def test_expired_token_returns_none(self):
|
|
84
|
+
self.assertIsNone(await authenticate(self.issuer.token('alice', expires_in=-60)))
|
|
85
|
+
|
|
86
|
+
async def test_wrong_audience_returns_none(self):
|
|
87
|
+
self.assertIsNone(await authenticate(self.issuer.token('alice', aud='other-app')))
|
|
88
|
+
|
|
89
|
+
async def test_missing_audience_returns_none(self):
|
|
90
|
+
self.assertIsNone(await authenticate(self.issuer.token('alice', aud=None)))
|
|
91
|
+
|
|
92
|
+
async def test_unknown_issuer_returns_none(self):
|
|
93
|
+
stranger = TestIssuer('https://evil.example')
|
|
94
|
+
self.assertIsNone(await authenticate(stranger.token('alice')))
|
|
95
|
+
|
|
96
|
+
async def test_key_from_another_issuer_returns_none(self):
|
|
97
|
+
forged = TestIssuer('https://evil.example').token('alice', iss=self.issuer.issuer)
|
|
98
|
+
self.assertIsNone(await authenticate(forged))
|
|
99
|
+
|
|
100
|
+
async def test_hs256_token_with_trusted_issuer_returns_none(self):
|
|
101
|
+
token = jwt.encode(
|
|
102
|
+
{'iss': self.issuer.issuer, 'sub': 'alice', 'aud': 'graphcore', 'exp': 9999999999},
|
|
103
|
+
settings.SECRET_KEY, algorithm='HS256',
|
|
104
|
+
)
|
|
105
|
+
self.assertIsNone(await authenticate(token))
|
|
106
|
+
|
|
107
|
+
async def test_unreachable_issuer_returns_none(self):
|
|
108
|
+
from graphcore.auth.authn import authenticator
|
|
109
|
+
|
|
110
|
+
with patch.object(authenticator, '_fetch_json', side_effect=OSError('down')):
|
|
111
|
+
self.assertIsNone(await authenticate(self.issuer.token('alice')))
|
|
112
|
+
|
|
113
|
+
async def test_malformed_discovery_returns_none(self):
|
|
114
|
+
from graphcore.auth.authn import authenticator
|
|
115
|
+
|
|
116
|
+
for document in ({'issuer': self.issuer.issuer}, ['not', 'a', 'dict']):
|
|
117
|
+
with patch.object(authenticator, '_fetch_json', return_value=document):
|
|
118
|
+
self.assertIsNone(await authenticate(self.issuer.token('alice')))
|
|
119
|
+
authenticator.clear_caches()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class TestIssuerSettings(TestCase):
|
|
123
|
+
async def test_internal_url_used_for_fetches_but_not_for_iss(self):
|
|
124
|
+
issuer = TestIssuer('http://localhost:8180/realms/graphcore')
|
|
125
|
+
internal = 'http://host.docker.internal:8180/realms/graphcore'
|
|
126
|
+
entries = [{**issuer.settings_entry, 'internal_url': internal}]
|
|
127
|
+
with issuer.trusted(entries=entries) as fetched:
|
|
128
|
+
principal = await authenticate(issuer.token('alice'))
|
|
129
|
+
self.assertEqual(principal.iss, issuer.issuer)
|
|
130
|
+
self.assertEqual(fetched, [
|
|
131
|
+
internal + '/.well-known/openid-configuration',
|
|
132
|
+
internal + '/certs',
|
|
133
|
+
])
|
|
134
|
+
|
|
135
|
+
async def test_two_issuers_each_verify_their_own_tokens(self):
|
|
136
|
+
first, second = TestIssuer('https://one.example'), TestIssuer('https://two.example')
|
|
137
|
+
with first.trusted(second):
|
|
138
|
+
a = await authenticate(first.token('alice'))
|
|
139
|
+
b = await authenticate(second.token('alice'))
|
|
140
|
+
self.assertNotEqual(a.principal_entity_id, b.principal_entity_id)
|
|
141
|
+
|
|
142
|
+
@override_settings(GRAPHCORE_TRUSTED_ISSUERS=[{'issuer': 'https://idp.example'}])
|
|
143
|
+
async def test_entry_without_audience_is_a_configuration_error(self):
|
|
144
|
+
with self.assertRaises(ImproperlyConfigured):
|
|
145
|
+
await authenticate(TestIssuer('https://idp.example').token('alice'))
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Principal creation and onboarding - see docs/AUTHORIZATION_TECHNICAL_TDD.md
|
|
2
|
+
(Identity-based policy, Onboarding)."""
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.test import TransactionTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.auth.authn.principals import create_principal
|
|
7
|
+
from graphcore.auth.authz.lookups import find_grant, find_group_by_key
|
|
8
|
+
from graphcore.bootstrap.deployment import ensure_deployment
|
|
9
|
+
from graphcore.domain.entity.Entity import Entity
|
|
10
|
+
from graphcore.domain.strategy.BaseStrategy import CreateEntity
|
|
11
|
+
from graphcore.domain.strategy.GroupStrategies import CreateGroup
|
|
12
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
13
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
14
|
+
from graphcore.runtime import django_runtime
|
|
15
|
+
from graphcore.types.identity import Principal
|
|
16
|
+
from graphcore.types.onboarding import Onboarding
|
|
17
|
+
from graphcore.types.query import AllOf, Equal
|
|
18
|
+
|
|
19
|
+
BASE_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _Note(Entity):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
BASE_REGISTRY.entities.register_entity(_Note, app_label='graphcore')
|
|
27
|
+
with BASE_REGISTRY.policy.declaring_app('onb'):
|
|
28
|
+
BASE_REGISTRY.policy.declare_group(key='onb.staff', name='Staff')
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class OnboardingTests(TransactionTestCase):
|
|
32
|
+
def _runtime(self, *hooks):
|
|
33
|
+
registry = BASE_REGISTRY.fork_open()
|
|
34
|
+
registry.principal_hooks.extend(hooks)
|
|
35
|
+
self.runtime = django_runtime(registry)
|
|
36
|
+
self.service = self.runtime.entity_service
|
|
37
|
+
return registry
|
|
38
|
+
|
|
39
|
+
async def _seed(self, registry):
|
|
40
|
+
await ensure_deployment(self.service, registry)
|
|
41
|
+
|
|
42
|
+
async def _create(self, username='new-user'):
|
|
43
|
+
return await create_principal(self.service, self.runtime.executor, username=username)
|
|
44
|
+
|
|
45
|
+
@async_to_sync
|
|
46
|
+
async def test_default_hook_applies_the_deployments_new_user_settings(self):
|
|
47
|
+
registry = self._runtime()
|
|
48
|
+
await self._seed(registry)
|
|
49
|
+
staff = await find_group_by_key(self.service, 'onb.staff')
|
|
50
|
+
deployment = await self.service.repository.load(self.service.deployment_entity_id)
|
|
51
|
+
deployment.new_user_group_ids = [staff.entity_id]
|
|
52
|
+
deployment.new_user_roles = ['member']
|
|
53
|
+
with kernel_internal_context():
|
|
54
|
+
await self.service.save_entity(deployment, None)
|
|
55
|
+
|
|
56
|
+
user = await self._create()
|
|
57
|
+
|
|
58
|
+
self.assertIn(user.entity_id, (await self.service.repository.load(staff.entity_id)).member_ids)
|
|
59
|
+
grant = await find_grant(self.service, self.service.deployment_entity_id, user.entity_id)
|
|
60
|
+
self.assertEqual(grant.role_name, 'member')
|
|
61
|
+
|
|
62
|
+
@async_to_sync
|
|
63
|
+
async def test_app_hook_names_groups_by_key_and_unknowns_are_skipped(self):
|
|
64
|
+
registry = self._runtime(lambda principal, deployment: Onboarding(
|
|
65
|
+
groups=['onb.staff', 'onb.missing'], roles=['no-such-role'],
|
|
66
|
+
))
|
|
67
|
+
await self._seed(registry)
|
|
68
|
+
|
|
69
|
+
user = await self._create()
|
|
70
|
+
|
|
71
|
+
staff = await find_group_by_key(self.service, 'onb.staff')
|
|
72
|
+
self.assertIn(user.entity_id, staff.member_ids)
|
|
73
|
+
self.assertIsNone(await find_grant(self.service, self.service.deployment_entity_id, user.entity_id))
|
|
74
|
+
|
|
75
|
+
@async_to_sync
|
|
76
|
+
async def test_requests_run_as_the_new_principal(self):
|
|
77
|
+
requests = []
|
|
78
|
+
|
|
79
|
+
def hook(principal, deployment):
|
|
80
|
+
requests[:] = [
|
|
81
|
+
CreateEntity.request_constructor(
|
|
82
|
+
target_entity_id=None,
|
|
83
|
+
entity_type_id=BASE_REGISTRY.entities.get_entity_type_id(_Note),
|
|
84
|
+
),
|
|
85
|
+
# Needs ManageAccess on the deployment, which a new user lacks.
|
|
86
|
+
CreateGroup.request_constructor(None, name='X', key='onb.x'),
|
|
87
|
+
]
|
|
88
|
+
return Onboarding(requests=requests)
|
|
89
|
+
|
|
90
|
+
registry = self._runtime(hook)
|
|
91
|
+
await self._seed(registry)
|
|
92
|
+
|
|
93
|
+
user = await self._create()
|
|
94
|
+
|
|
95
|
+
owned = await self.service.find_entities(AllOf((
|
|
96
|
+
Equal('owner', user.entity_id),
|
|
97
|
+
Equal('entity_type_id', BASE_REGISTRY.entities.get_entity_type_id(_Note)),
|
|
98
|
+
)))
|
|
99
|
+
self.assertEqual(len(owned), 1)
|
|
100
|
+
self.assertIsNone(await find_group_by_key(self.service, 'onb.x'))
|
|
101
|
+
|
|
102
|
+
@async_to_sync
|
|
103
|
+
async def test_a_failing_hook_does_not_block_creation(self):
|
|
104
|
+
def broken(principal, deployment):
|
|
105
|
+
raise RuntimeError('boom')
|
|
106
|
+
|
|
107
|
+
registry = self._runtime(broken, lambda p, d: Onboarding(groups=['onb.staff']))
|
|
108
|
+
await self._seed(registry)
|
|
109
|
+
|
|
110
|
+
user = await self._create()
|
|
111
|
+
|
|
112
|
+
self.assertIsNotNone(await self.service.repository.load(user.entity_id))
|
|
113
|
+
self.assertIn(user.entity_id, (await find_group_by_key(self.service, 'onb.staff')).member_ids)
|
|
114
|
+
|
|
115
|
+
@async_to_sync
|
|
116
|
+
async def test_hook_code_runs_without_kernel_internal_authority(self):
|
|
117
|
+
attempted = []
|
|
118
|
+
|
|
119
|
+
async def rogue(principal, deployment):
|
|
120
|
+
staff = await find_group_by_key(self.service, 'onb.staff')
|
|
121
|
+
staff.add_member('smuggled')
|
|
122
|
+
try:
|
|
123
|
+
await self.service.save_entity(staff, Principal(principal_entity_id=principal.entity_id))
|
|
124
|
+
finally:
|
|
125
|
+
attempted.append(True)
|
|
126
|
+
|
|
127
|
+
registry = self._runtime(rogue)
|
|
128
|
+
await self._seed(registry)
|
|
129
|
+
|
|
130
|
+
await self._create()
|
|
131
|
+
|
|
132
|
+
self.assertEqual(attempted, [True])
|
|
133
|
+
self.assertNotIn('smuggled', (await find_group_by_key(self.service, 'onb.staff')).member_ids)
|
|
134
|
+
|
|
135
|
+
@async_to_sync
|
|
136
|
+
async def test_async_hooks_are_awaited(self):
|
|
137
|
+
async def hook(principal, deployment):
|
|
138
|
+
return Onboarding(groups=['onb.staff'])
|
|
139
|
+
|
|
140
|
+
registry = self._runtime(hook)
|
|
141
|
+
await self._seed(registry)
|
|
142
|
+
|
|
143
|
+
user = await self._create()
|
|
144
|
+
|
|
145
|
+
self.assertIn(user.entity_id, (await find_group_by_key(self.service, 'onb.staff')).member_ids)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
from unittest.mock import AsyncMock, patch
|
|
3
|
+
|
|
4
|
+
from django.test import SimpleTestCase, TestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.auth.authn import authenticate, service_accounts
|
|
7
|
+
from graphcore.auth.authn.service_accounts import (
|
|
8
|
+
ServiceClient, declared_service_clients, ensure_service_account, find_service_account,
|
|
9
|
+
)
|
|
10
|
+
from graphcore.auth.authn.testing import TestIssuer
|
|
11
|
+
from graphcore.auth.authz.lookups import find_grant
|
|
12
|
+
from graphcore.bootstrap.deployment import ensure_deployment
|
|
13
|
+
from graphcore.domain.entity.service_account.ServiceAccount import ServiceAccount
|
|
14
|
+
from graphcore.domain.strategy.BaseStrategy import SetAttributes
|
|
15
|
+
from graphcore.errors import Forbidden
|
|
16
|
+
from graphcore.public.testing import GraphCoreTestCase
|
|
17
|
+
from graphcore.registry.EntityDirectory import EntityDirectory
|
|
18
|
+
from graphcore.runtime import get_runtime
|
|
19
|
+
from graphcore.types.entities import EntityFilter
|
|
20
|
+
from graphcore.types.identity import Principal
|
|
21
|
+
from graphcore.types.query import Equal
|
|
22
|
+
|
|
23
|
+
GATEWAY = ServiceClient('wedding', 'gateway', 'wedding-gateway')
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _declare_gateway():
|
|
27
|
+
return patch.object(service_accounts, 'declared_service_clients', return_value={GATEWAY.client_id: GATEWAY})
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ServiceAccountAuthenticationTests(TestCase):
|
|
31
|
+
def setUp(self):
|
|
32
|
+
self.issuer = TestIssuer()
|
|
33
|
+
self.enterContext(self.issuer.trusted())
|
|
34
|
+
self.enterContext(_declare_gateway())
|
|
35
|
+
|
|
36
|
+
def gateway_token(self, sub='sa-1', issuer=None):
|
|
37
|
+
return (issuer or self.issuer).token(
|
|
38
|
+
sub, azp='wedding-gateway', preferred_username='service-account-wedding-gateway')
|
|
39
|
+
|
|
40
|
+
async def test_a_declared_clients_token_maps_to_its_account(self):
|
|
41
|
+
from graphcore.persistence.django.models import ServiceAccountModel, UserModel
|
|
42
|
+
|
|
43
|
+
principal = await authenticate(self.gateway_token())
|
|
44
|
+
|
|
45
|
+
model = await ServiceAccountModel.objects.aget(client_id='wedding-gateway')
|
|
46
|
+
self.assertEqual(principal.principal_entity_id, str(model.entity_id))
|
|
47
|
+
self.assertEqual((model.app_id, model.client_name), ('wedding', 'gateway'))
|
|
48
|
+
self.assertEqual(model.issuer, self.issuer.issuer)
|
|
49
|
+
self.assertFalse(await UserModel.objects.filter(subject='sa-1').aexists())
|
|
50
|
+
|
|
51
|
+
async def test_the_first_token_binds_the_account_seeded_before_it(self):
|
|
52
|
+
seeded = await ensure_service_account(get_runtime().entity_service, GATEWAY)
|
|
53
|
+
self.assertIsNone(seeded.issuer)
|
|
54
|
+
|
|
55
|
+
principal = await authenticate(self.gateway_token())
|
|
56
|
+
|
|
57
|
+
self.assertEqual(principal.principal_entity_id, seeded.entity_id)
|
|
58
|
+
|
|
59
|
+
async def test_a_service_account_is_not_onboarded(self):
|
|
60
|
+
with patch('graphcore.auth.authn.principals.run_onboarding', new_callable=AsyncMock) as onboarding:
|
|
61
|
+
await authenticate(self.gateway_token())
|
|
62
|
+
|
|
63
|
+
onboarding.assert_not_awaited()
|
|
64
|
+
|
|
65
|
+
async def test_later_tokens_map_to_the_same_account(self):
|
|
66
|
+
first = await authenticate(self.gateway_token())
|
|
67
|
+
second = await authenticate(self.gateway_token())
|
|
68
|
+
|
|
69
|
+
self.assertEqual(first.principal_entity_id, second.principal_entity_id)
|
|
70
|
+
|
|
71
|
+
async def test_a_recreated_client_keeps_its_account(self):
|
|
72
|
+
first = await authenticate(self.gateway_token(sub='sa-1'))
|
|
73
|
+
|
|
74
|
+
again = await authenticate(self.gateway_token(sub='sa-2'))
|
|
75
|
+
|
|
76
|
+
self.assertEqual(again.principal_entity_id, first.principal_entity_id)
|
|
77
|
+
|
|
78
|
+
async def test_a_token_from_another_issuer_is_refused(self):
|
|
79
|
+
await authenticate(self.gateway_token())
|
|
80
|
+
other = TestIssuer('https://other.example')
|
|
81
|
+
|
|
82
|
+
with other.trusted(self.issuer):
|
|
83
|
+
self.assertIsNone(await authenticate(self.gateway_token(issuer=other)))
|
|
84
|
+
|
|
85
|
+
async def test_a_concurrent_first_token_keeps_the_binding_it_lost_to(self):
|
|
86
|
+
entity_service = get_runtime().entity_service
|
|
87
|
+
account = await ensure_service_account(entity_service, GATEWAY)
|
|
88
|
+
real_save = entity_service.save_entity
|
|
89
|
+
other = TestIssuer('https://other.example')
|
|
90
|
+
|
|
91
|
+
async def bound_elsewhere_first(entity, principal):
|
|
92
|
+
account.issuer = other.issuer
|
|
93
|
+
await real_save(account, None)
|
|
94
|
+
return await real_save(entity, principal)
|
|
95
|
+
|
|
96
|
+
with other.trusted(self.issuer), patch.object(entity_service, 'save_entity', bound_elsewhere_first):
|
|
97
|
+
self.assertIsNone(await authenticate(self.gateway_token()))
|
|
98
|
+
self.assertEqual((await find_service_account(entity_service, 'wedding-gateway')).issuer, other.issuer)
|
|
99
|
+
|
|
100
|
+
async def test_a_token_for_an_undeclared_client_is_a_user(self):
|
|
101
|
+
from graphcore.persistence.django.models import ServiceAccountModel, UserModel
|
|
102
|
+
|
|
103
|
+
await authenticate(self.issuer.token('alice', azp='graphcore-web'))
|
|
104
|
+
|
|
105
|
+
self.assertTrue(await UserModel.objects.filter(subject='alice').aexists())
|
|
106
|
+
self.assertFalse(await ServiceAccountModel.objects.aexists())
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class SeedingTests(TestCase):
|
|
110
|
+
async def test_migrate_creates_every_declared_account(self):
|
|
111
|
+
runtime = get_runtime()
|
|
112
|
+
with _declare_gateway():
|
|
113
|
+
await ensure_deployment(runtime.entity_service, runtime.registry)
|
|
114
|
+
await ensure_deployment(runtime.entity_service, runtime.registry)
|
|
115
|
+
|
|
116
|
+
found = await runtime.entity_service.find_entities(
|
|
117
|
+
Equal('entity_type_id', ServiceAccount.get_entity_type_id()))
|
|
118
|
+
account, = found.values()
|
|
119
|
+
self.assertEqual(account.client_id, 'wedding-gateway')
|
|
120
|
+
self.assertIsNone(account.issuer)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class ServiceAccountAccessTests(GraphCoreTestCase):
|
|
124
|
+
async def _account(self):
|
|
125
|
+
return await ensure_service_account(self.runtime.entity_service, GATEWAY)
|
|
126
|
+
|
|
127
|
+
async def test_any_user_finds_it_with_an_ordinary_query(self):
|
|
128
|
+
account = await self._account()
|
|
129
|
+
alice = await self.create_user('alice')
|
|
130
|
+
|
|
131
|
+
page = await self.runtime.entity_service.list_entities(alice, EntityFilter(
|
|
132
|
+
type_ids=frozenset({ServiceAccount.get_entity_type_id()}),
|
|
133
|
+
where=Equal('client_id', 'wedding-gateway'),
|
|
134
|
+
))
|
|
135
|
+
|
|
136
|
+
self.assertEqual([e.entity_id for e in page.entities], [account.entity_id])
|
|
137
|
+
|
|
138
|
+
async def test_it_can_be_granted_before_its_first_token(self):
|
|
139
|
+
account = await self._account()
|
|
140
|
+
admin = await self.create_user('admin')
|
|
141
|
+
await self.make_admin(admin)
|
|
142
|
+
|
|
143
|
+
await self.grant(admin, self.deployment_entity_id,
|
|
144
|
+
Principal(principal_entity_id=account.entity_id), 'member')
|
|
145
|
+
|
|
146
|
+
self.assertIsNotNone(await find_grant(
|
|
147
|
+
self.runtime.entity_service, self.deployment_entity_id, account.entity_id))
|
|
148
|
+
|
|
149
|
+
async def test_nobody_can_change_it(self):
|
|
150
|
+
account = await self._account()
|
|
151
|
+
admin = await self.create_user('admin')
|
|
152
|
+
await self.make_admin(admin)
|
|
153
|
+
|
|
154
|
+
with self.assertRaises(Forbidden):
|
|
155
|
+
await self.run_strategy(admin, SetAttributes.request_constructor(
|
|
156
|
+
target_entity_id=account.entity_id, attribute_map={'client_id': 'other'}))
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class DeclaredServiceClientsTests(SimpleTestCase):
|
|
160
|
+
def setUp(self):
|
|
161
|
+
declared_service_clients.cache_clear()
|
|
162
|
+
self.addCleanup(declared_service_clients.cache_clear)
|
|
163
|
+
|
|
164
|
+
def test_each_declaration_is_named_by_its_app(self):
|
|
165
|
+
configs = [
|
|
166
|
+
SimpleNamespace(graphcore_app_id='graphcore', graphcore_service_clients=()),
|
|
167
|
+
SimpleNamespace(graphcore_app_id='wedding', graphcore_service_clients=('gateway', 'mailer')),
|
|
168
|
+
SimpleNamespace(), # a plain Django app
|
|
169
|
+
]
|
|
170
|
+
with patch.object(service_accounts.apps, 'get_app_configs', return_value=configs):
|
|
171
|
+
declared = declared_service_clients()
|
|
172
|
+
|
|
173
|
+
self.assertEqual(declared, {
|
|
174
|
+
'wedding-gateway': ServiceClient('wedding', 'gateway', 'wedding-gateway'),
|
|
175
|
+
'wedding-mailer': ServiceClient('wedding', 'mailer', 'wedding-mailer'),
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
def test_service_account_is_a_principal_type(self):
|
|
179
|
+
self.assertTrue(EntityDirectory.is_principal(ServiceAccount.get_entity_type_id()))
|
|
180
|
+
self.assertEqual(ServiceAccount.get_entity_type_id(), 'graphcore.ServiceAccount')
|