resource-engine 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.
- resource_engine/__init__.py +96 -0
- resource_engine/application/__init__.py +1 -0
- resource_engine/application/actions.py +48 -0
- resource_engine/application/context.py +111 -0
- resource_engine/application/events.py +54 -0
- resource_engine/application/hooks.py +272 -0
- resource_engine/application/idempotency.py +277 -0
- resource_engine/application/manager.py +1987 -0
- resource_engine/application/query.py +47 -0
- resource_engine/application/registry.py +137 -0
- resource_engine/application/relations.py +470 -0
- resource_engine/application/resource.py +984 -0
- resource_engine/application/searching.py +162 -0
- resource_engine/application/settings.py +169 -0
- resource_engine/application/slots.py +67 -0
- resource_engine/application/state_machines.py +177 -0
- resource_engine/application/timers.py +347 -0
- resource_engine/application/transaction.py +100 -0
- resource_engine/application/views.py +239 -0
- resource_engine/audit/__init__.py +40 -0
- resource_engine/audit/export.py +280 -0
- resource_engine/audit/history.py +165 -0
- resource_engine/audit/janitor.py +257 -0
- resource_engine/audit/model.py +98 -0
- resource_engine/audit/recorder.py +145 -0
- resource_engine/audit/resource.py +68 -0
- resource_engine/audit/seal.py +327 -0
- resource_engine/audit/sink.py +69 -0
- resource_engine/authz/__init__.py +72 -0
- resource_engine/authz/apikeys.py +300 -0
- resource_engine/authz/base.py +143 -0
- resource_engine/authz/catalog.py +291 -0
- resource_engine/authz/conditions.py +416 -0
- resource_engine/authz/config.py +77 -0
- resource_engine/authz/evaluator.py +382 -0
- resource_engine/authz/external.py +249 -0
- resource_engine/authz/fields.py +44 -0
- resource_engine/authz/model.py +145 -0
- resource_engine/authz/resources.py +289 -0
- resource_engine/authz/roles.py +101 -0
- resource_engine/cache.py +144 -0
- resource_engine/calls.py +100 -0
- resource_engine/capabilities.py +145 -0
- resource_engine/composite.py +295 -0
- resource_engine/documents/__init__.py +69 -0
- resource_engine/documents/content.py +57 -0
- resource_engine/documents/events.py +49 -0
- resource_engine/documents/janitor.py +93 -0
- resource_engine/documents/machine.py +38 -0
- resource_engine/documents/manager.py +704 -0
- resource_engine/documents/model.py +91 -0
- resource_engine/documents/pipeline.py +98 -0
- resource_engine/documents/processors.py +320 -0
- resource_engine/documents/resource.py +39 -0
- resource_engine/domain/__init__.py +10 -0
- resource_engine/domain/events.py +50 -0
- resource_engine/domain/exceptions.py +23 -0
- resource_engine/email.py +417 -0
- resource_engine/errors.py +562 -0
- resource_engine/explorer.py +133 -0
- resource_engine/exports.py +687 -0
- resource_engine/generation.py +233 -0
- resource_engine/i18n.py +251 -0
- resource_engine/identity.py +46 -0
- resource_engine/integration.py +80 -0
- resource_engine/jobs.py +390 -0
- resource_engine/locale/ca/LC_MESSAGES/resource_engine.mo +0 -0
- resource_engine/locale/ca/LC_MESSAGES/resource_engine.po +206 -0
- resource_engine/locale/es/LC_MESSAGES/resource_engine.mo +0 -0
- resource_engine/locale/es/LC_MESSAGES/resource_engine.po +206 -0
- resource_engine/logs.py +201 -0
- resource_engine/messaging/__init__.py +73 -0
- resource_engine/messaging/asyncapi.py +529 -0
- resource_engine/messaging/cloudevents.py +98 -0
- resource_engine/messaging/consumer.py +165 -0
- resource_engine/messaging/delivery.py +115 -0
- resource_engine/messaging/envelope.py +193 -0
- resource_engine/messaging/hub.py +148 -0
- resource_engine/messaging/inbound.py +76 -0
- resource_engine/messaging/inspection.py +113 -0
- resource_engine/messaging/janitor.py +136 -0
- resource_engine/messaging/live.py +179 -0
- resource_engine/messaging/processing.py +357 -0
- resource_engine/messaging/publishing.py +318 -0
- resource_engine/messaging/relay.py +84 -0
- resource_engine/messaging/worker.py +166 -0
- resource_engine/nosql.py +394 -0
- resource_engine/ports/__init__.py +13 -0
- resource_engine/ports/activity.py +171 -0
- resource_engine/ports/audit.py +42 -0
- resource_engine/ports/authz.py +132 -0
- resource_engine/ports/cache.py +41 -0
- resource_engine/ports/email.py +118 -0
- resource_engine/ports/event_publisher.py +34 -0
- resource_engine/ports/idempotency.py +69 -0
- resource_engine/ports/identity.py +112 -0
- resource_engine/ports/locks.py +28 -0
- resource_engine/ports/messaging.py +197 -0
- resource_engine/ports/observability.py +60 -0
- resource_engine/ports/ratelimit.py +59 -0
- resource_engine/ports/repository.py +56 -0
- resource_engine/ports/storage.py +93 -0
- resource_engine/ports/templates.py +54 -0
- resource_engine/ports/tracing.py +61 -0
- resource_engine/ports/unit_of_work.py +46 -0
- resource_engine/processes.py +408 -0
- resource_engine/py.typed +0 -0
- resource_engine/querying.py +319 -0
- resource_engine/ratelimit.py +217 -0
- resource_engine/reporting.py +230 -0
- resource_engine/schema_examples.py +51 -0
- resource_engine/templates.py +143 -0
- resource_engine/tenancy.py +68 -0
- resource_engine/testing/__init__.py +34 -0
- resource_engine/testing/activity.py +123 -0
- resource_engine/testing/contracts.py +1416 -0
- resource_engine/testing/email.py +36 -0
- resource_engine/testing/engine.py +40 -0
- resource_engine/testing/memory.py +603 -0
- resource_engine/testing/storage.py +110 -0
- resource_engine/testing/tracing.py +100 -0
- resource_engine/tracing.py +156 -0
- resource_engine/translatable.py +80 -0
- resource_engine/versions.py +189 -0
- resource_engine-0.1.0.dist-info/METADATA +36 -0
- resource_engine-0.1.0.dist-info/RECORD +128 -0
- resource_engine-0.1.0.dist-info/WHEEL +4 -0
- resource_engine-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"""``EngineAuthorizer``: the ``Authorizer`` port on top of an external policy
|
|
2
|
+
engine (OPA, Cedar, OpenFGA... see the adapter packages).
|
|
3
|
+
|
|
4
|
+
The engine answers one question (``PolicyEngine.evaluate``): allowed, denied, or
|
|
5
|
+
a residual condition over what is not known yet (the record, the input). This
|
|
6
|
+
module turns those answers into everything the manager needs:
|
|
7
|
+
|
|
8
|
+
- ``check``: residuals mean "depends on the record" until the record is loaded;
|
|
9
|
+
- ``scope``: the residual of ``<resource>:list`` is the row filter, applied by
|
|
10
|
+
the repository like the built-in policies' filters;
|
|
11
|
+
- ``field_rules``: fields restricted in code are asked once per operation and
|
|
12
|
+
their residuals evaluated per record, locally.
|
|
13
|
+
|
|
14
|
+
System roles in code (``AuthzConfig``) still apply first: ``admin`` is allowed
|
|
15
|
+
without asking the engine, and drives tenancy (``cross_tenant``).
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import dataclasses
|
|
21
|
+
import enum
|
|
22
|
+
from collections.abc import Mapping
|
|
23
|
+
from datetime import UTC, datetime
|
|
24
|
+
from typing import TYPE_CHECKING, Any, override
|
|
25
|
+
|
|
26
|
+
from pydantic import BaseModel
|
|
27
|
+
|
|
28
|
+
from resource_engine.authz.base import RoleBasedAuthorizer, SystemPrincipal, subject_view
|
|
29
|
+
from resource_engine.authz.catalog import FIELD_KINDS, ActionInfo
|
|
30
|
+
from resource_engine.authz.conditions import UNKNOWN, Env, evaluate, partial
|
|
31
|
+
from resource_engine.authz.config import AuthzConfig
|
|
32
|
+
from resource_engine.ports.authz import (
|
|
33
|
+
AuthzRequest,
|
|
34
|
+
Decision,
|
|
35
|
+
FieldAccess,
|
|
36
|
+
FieldRules,
|
|
37
|
+
PolicyEngine,
|
|
38
|
+
)
|
|
39
|
+
from resource_engine.ports.identity import Principal
|
|
40
|
+
|
|
41
|
+
if TYPE_CHECKING:
|
|
42
|
+
from resource_engine.application.registry import ResourceRegistry
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def plain(value: Any) -> Any:
|
|
46
|
+
"""A record or input as a dict of plain Python values (``Decimal``, ``date``
|
|
47
|
+
and ``datetime`` kept; enums, sets and nested objects flattened)."""
|
|
48
|
+
if value is ... or value is None:
|
|
49
|
+
return value
|
|
50
|
+
if isinstance(value, BaseModel):
|
|
51
|
+
return {k: _plain_value(v) for k, v in value.model_dump().items()}
|
|
52
|
+
if dataclasses.is_dataclass(value) and not isinstance(value, type):
|
|
53
|
+
return {f.name: _plain_value(getattr(value, f.name)) for f in dataclasses.fields(value)}
|
|
54
|
+
if isinstance(value, Mapping):
|
|
55
|
+
return {str(k): _plain_value(v) for k, v in value.items()}
|
|
56
|
+
return {k: _plain_value(v) for k, v in vars(value).items() if not k.startswith("_")}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _plain_value(value: Any) -> Any:
|
|
60
|
+
if isinstance(value, enum.Enum):
|
|
61
|
+
return value.value
|
|
62
|
+
if isinstance(value, set | frozenset | tuple | list):
|
|
63
|
+
return [_plain_value(v) for v in value]
|
|
64
|
+
if isinstance(value, Mapping | BaseModel):
|
|
65
|
+
return plain(value)
|
|
66
|
+
if dataclasses.is_dataclass(value) and not isinstance(value, type):
|
|
67
|
+
return plain(value)
|
|
68
|
+
return value
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ResidualFieldRules(FieldRules):
|
|
72
|
+
"""Field decisions prepared from engine answers (see
|
|
73
|
+
``EngineAuthorizer.field_rules``): ``True``, ``False`` or a residual per
|
|
74
|
+
field, evaluated per record without calling the engine again."""
|
|
75
|
+
|
|
76
|
+
def __init__(self, rules: dict[str, Any], subject: dict[str, Any] | None = None) -> None:
|
|
77
|
+
self.rules = {name: rule for name, rule in rules.items() if rule is not True}
|
|
78
|
+
self.subject = subject or {}
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def empty(self) -> bool:
|
|
82
|
+
"""Whether no field is restricted."""
|
|
83
|
+
return not self.rules
|
|
84
|
+
|
|
85
|
+
@override
|
|
86
|
+
def decide(self, *, resource: Any = ..., input: Any = ...) -> FieldAccess:
|
|
87
|
+
denied: set[str] = set()
|
|
88
|
+
conditional: set[str] = set()
|
|
89
|
+
env = _env(self.subject, resource, input)
|
|
90
|
+
for name, rule in self.rules.items():
|
|
91
|
+
result = rule if isinstance(rule, bool) else evaluate(rule, env)
|
|
92
|
+
if result is False:
|
|
93
|
+
denied.add(name)
|
|
94
|
+
elif result is UNKNOWN:
|
|
95
|
+
conditional.add(name)
|
|
96
|
+
return FieldAccess(frozenset(denied), frozenset(conditional))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _env(subject: dict[str, Any] | None, resource: Any, input: Any) -> Env:
|
|
100
|
+
namespaces: dict[str, Any] = {"subject": subject or {}}
|
|
101
|
+
if resource is not ...:
|
|
102
|
+
namespaces["resource"] = resource
|
|
103
|
+
if input is not ...:
|
|
104
|
+
namespaces["input"] = plain(input)
|
|
105
|
+
return Env(**namespaces)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class EngineAuthorizer(RoleBasedAuthorizer):
|
|
109
|
+
"""``Authorizer`` delegating decisions to a ``PolicyEngine``.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
engine: The adapter (``OpaEngine``, ``CedarEngine``, ``OpenFgaEngine``...).
|
|
113
|
+
config: System roles, role source and bootstrap roles, as for
|
|
114
|
+
``PolicyAuthorizer``. System-role grants are decided here, before
|
|
115
|
+
the engine is asked.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
def __init__(self, engine: PolicyEngine, config: AuthzConfig | None = None) -> None:
|
|
119
|
+
super().__init__(config or AuthzConfig())
|
|
120
|
+
self.engine = engine
|
|
121
|
+
self.registry: ResourceRegistry | None = None
|
|
122
|
+
|
|
123
|
+
@override
|
|
124
|
+
def bind(self, registry: ResourceRegistry) -> None:
|
|
125
|
+
super().bind(registry)
|
|
126
|
+
self.registry = registry
|
|
127
|
+
bind = getattr(self.engine, "bind", None)
|
|
128
|
+
if bind is not None:
|
|
129
|
+
bind(registry)
|
|
130
|
+
|
|
131
|
+
# ---------------------------------------------------------------- requests
|
|
132
|
+
|
|
133
|
+
def _info(self, action: str) -> ActionInfo | None:
|
|
134
|
+
return self.catalog.actions.get(action) if self.catalog else None
|
|
135
|
+
|
|
136
|
+
def _request(
|
|
137
|
+
self,
|
|
138
|
+
subject: dict[str, Any] | None,
|
|
139
|
+
action: str,
|
|
140
|
+
*,
|
|
141
|
+
resource: Any = ...,
|
|
142
|
+
input: Any = ...,
|
|
143
|
+
context: dict[str, Any] | None = None,
|
|
144
|
+
filter: bool = False,
|
|
145
|
+
) -> AuthzRequest:
|
|
146
|
+
info = self._info(action)
|
|
147
|
+
resource_type = info.resource if info else action.partition(":")[0] or None
|
|
148
|
+
id_field = "id"
|
|
149
|
+
if self.registry is not None and resource_type and resource_type in self.registry:
|
|
150
|
+
id_field = self.registry.get(resource_type).id_field
|
|
151
|
+
data = plain(resource)
|
|
152
|
+
return AuthzRequest(
|
|
153
|
+
subject=subject,
|
|
154
|
+
action=action,
|
|
155
|
+
resource_type=resource_type,
|
|
156
|
+
resource=data,
|
|
157
|
+
input=plain(input),
|
|
158
|
+
context={"action": action, "now": datetime.now(UTC), **(context or {})},
|
|
159
|
+
entity_id=data.get(id_field) if isinstance(data, dict) else None,
|
|
160
|
+
id_field=id_field,
|
|
161
|
+
filter=filter,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
async def _subject(self, principal: Any) -> tuple[dict[str, Any] | None, frozenset[str]]:
|
|
165
|
+
if not isinstance(principal, Principal):
|
|
166
|
+
return None, frozenset()
|
|
167
|
+
roles = await self.roles_of(principal)
|
|
168
|
+
return subject_view(principal, roles), roles
|
|
169
|
+
|
|
170
|
+
def _field_grant(self, roles: frozenset[str], info: ActionInfo) -> bool:
|
|
171
|
+
"""A system role grants a field action; restricted fields need a grant
|
|
172
|
+
naming them or a role with ``include_sensitive`` (as for policies)."""
|
|
173
|
+
from resource_engine.authz.catalog import action_matches
|
|
174
|
+
from resource_engine.authz.evaluator import names_field
|
|
175
|
+
|
|
176
|
+
for name in sorted(roles):
|
|
177
|
+
role = self.config.system_role(name)
|
|
178
|
+
if role and any(
|
|
179
|
+
action_matches(
|
|
180
|
+
g,
|
|
181
|
+
info.name,
|
|
182
|
+
wildcards=info.wildcards or role.include_sensitive,
|
|
183
|
+
exact=info.exact,
|
|
184
|
+
)
|
|
185
|
+
and (not info.restricted or role.include_sensitive or names_field(g))
|
|
186
|
+
for g in role.grants
|
|
187
|
+
):
|
|
188
|
+
return True
|
|
189
|
+
return False
|
|
190
|
+
|
|
191
|
+
# ------------------------------------------------------------------ checks
|
|
192
|
+
|
|
193
|
+
@override
|
|
194
|
+
async def check(
|
|
195
|
+
self,
|
|
196
|
+
principal: Principal | None,
|
|
197
|
+
action: str,
|
|
198
|
+
*,
|
|
199
|
+
resource: Any = ...,
|
|
200
|
+
input: Any = ...,
|
|
201
|
+
context: dict[str, Any] | None = None,
|
|
202
|
+
) -> Decision:
|
|
203
|
+
if isinstance(principal, SystemPrincipal):
|
|
204
|
+
return Decision(True, "system principal")
|
|
205
|
+
subject, roles = await self._subject(principal)
|
|
206
|
+
info = self._info(action)
|
|
207
|
+
if info is not None and info.kind in FIELD_KINDS:
|
|
208
|
+
if not info.restricted or self._field_grant(roles, info):
|
|
209
|
+
return Decision(True, "field allowed")
|
|
210
|
+
else:
|
|
211
|
+
system = self._system_grant(roles, action)
|
|
212
|
+
if system:
|
|
213
|
+
return Decision(True, f"system role {system}", system_role=system)
|
|
214
|
+
request = self._request(subject, action, resource=resource, input=input, context=context)
|
|
215
|
+
result = await self.engine.evaluate(request)
|
|
216
|
+
if isinstance(result, bool):
|
|
217
|
+
return Decision(result, "allowed by the engine" if result else "denied by the engine")
|
|
218
|
+
local = evaluate(result, _env(subject, resource, input))
|
|
219
|
+
if local is UNKNOWN:
|
|
220
|
+
return Decision(None, "depends on the record")
|
|
221
|
+
return Decision(local, "allowed by the engine" if local else "denied by the engine")
|
|
222
|
+
|
|
223
|
+
@override
|
|
224
|
+
async def scope(self, principal: Principal | None, action: str) -> Any:
|
|
225
|
+
if isinstance(principal, SystemPrincipal):
|
|
226
|
+
return True
|
|
227
|
+
subject, roles = await self._subject(principal)
|
|
228
|
+
if self._system_grant(roles, action):
|
|
229
|
+
return True
|
|
230
|
+
result = await self.engine.evaluate(self._request(subject, action, input={}, filter=True))
|
|
231
|
+
if isinstance(result, bool):
|
|
232
|
+
return result
|
|
233
|
+
return partial(result, Env(subject=subject or {}, input={}))
|
|
234
|
+
|
|
235
|
+
async def field_rules(
|
|
236
|
+
self, principal: Principal | None, resource: str, mode: str
|
|
237
|
+
) -> ResidualFieldRules:
|
|
238
|
+
"""Engine answers for the fields of ``resource`` restricted in code;
|
|
239
|
+
other fields are open (not asked)."""
|
|
240
|
+
assert self.catalog is not None, "bind() the authorizer to a registry first"
|
|
241
|
+
if isinstance(principal, SystemPrincipal):
|
|
242
|
+
return ResidualFieldRules({})
|
|
243
|
+
subject, roles = await self._subject(principal)
|
|
244
|
+
rules: dict[str, Any] = {}
|
|
245
|
+
for name, info in self.catalog.field_actions(resource, mode).items():
|
|
246
|
+
if not info.restricted or self._field_grant(roles, info):
|
|
247
|
+
continue
|
|
248
|
+
rules[name] = await self.engine.evaluate(self._request(subject, info.name, filter=True))
|
|
249
|
+
return ResidualFieldRules(rules, subject)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Field-level masking of read results.
|
|
2
|
+
|
|
3
|
+
Hidden fields are *omitted*: the returned model has no value for them (reading
|
|
4
|
+
the attribute raises ``AttributeError``) and ``model_dump`` leaves them out.
|
|
5
|
+
Entry adapters use ``hidden_fields`` to serialize without them.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Iterable
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from pydantic import BaseModel
|
|
14
|
+
|
|
15
|
+
_MARK = "_re_hidden_fields"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def mask(model: Any, hidden: Iterable[str]) -> Any:
|
|
19
|
+
"""A copy of ``model`` without ``hidden`` fields (``model`` itself when
|
|
20
|
+
nothing is hidden or it is not a Pydantic model)."""
|
|
21
|
+
hidden = frozenset(hidden)
|
|
22
|
+
if not hidden or not isinstance(model, BaseModel):
|
|
23
|
+
return model
|
|
24
|
+
fields = type(model).model_fields
|
|
25
|
+
hidden &= frozenset(fields)
|
|
26
|
+
if not hidden:
|
|
27
|
+
return model
|
|
28
|
+
values = {k: v for k, v in model.__dict__.items() if k not in hidden}
|
|
29
|
+
masked = type(model).model_construct(_fields_set=model.model_fields_set - hidden, **values)
|
|
30
|
+
for name in hidden:
|
|
31
|
+
masked.__dict__.pop(name, None)
|
|
32
|
+
private = dict(masked.__pydantic_private__ or {})
|
|
33
|
+
private[_MARK] = hidden
|
|
34
|
+
object.__setattr__(masked, "__pydantic_private__", private)
|
|
35
|
+
return masked
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def hidden_fields(model: Any) -> frozenset[str]:
|
|
39
|
+
"""Fields ``mask`` removed from ``model`` (empty for unmasked values)."""
|
|
40
|
+
private = getattr(model, "__pydantic_private__", None)
|
|
41
|
+
if not isinstance(private, dict):
|
|
42
|
+
return frozenset()
|
|
43
|
+
marked: frozenset[str] = private.get(_MARK, frozenset())
|
|
44
|
+
return marked
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Entities and schemas of the built-in authorization resources."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from typing import Any, Literal
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Policy:
|
|
14
|
+
"""An access rule, created and edited from the front end."""
|
|
15
|
+
|
|
16
|
+
id: str
|
|
17
|
+
name: str
|
|
18
|
+
effect: str # allow | deny
|
|
19
|
+
actions: list[str]
|
|
20
|
+
subjects: dict[str, Any] = field(default_factory=dict)
|
|
21
|
+
condition: Any = None
|
|
22
|
+
description: str | None = None
|
|
23
|
+
enabled: bool = True
|
|
24
|
+
created_at: datetime | None = None
|
|
25
|
+
created_by: str | None = None
|
|
26
|
+
updated_at: datetime | None = None
|
|
27
|
+
updated_by: str | None = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class Role:
|
|
32
|
+
"""A custom role (system roles live in code and are not stored)."""
|
|
33
|
+
|
|
34
|
+
name: str
|
|
35
|
+
description: str | None = None
|
|
36
|
+
created_at: datetime | None = None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class RoleAssignment:
|
|
41
|
+
"""A role given to a principal (``Principal.id``) or to a group."""
|
|
42
|
+
|
|
43
|
+
id: str
|
|
44
|
+
role: str
|
|
45
|
+
principal: str | None = None
|
|
46
|
+
group: str | None = None
|
|
47
|
+
created_at: datetime | None = None
|
|
48
|
+
created_by: str | None = None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Subjects(BaseModel):
|
|
52
|
+
"""Who a policy applies to. Empty means nobody, ``everyone`` any authenticated
|
|
53
|
+
principal."""
|
|
54
|
+
|
|
55
|
+
roles: list[str] = Field(default_factory=list, description="Application roles")
|
|
56
|
+
groups: list[str] = Field(default_factory=list, description="Identity provider groups")
|
|
57
|
+
users: list[str] = Field(default_factory=list, description="Principal ids (provider:subject)")
|
|
58
|
+
everyone: bool = Field(default=False, description="Any authenticated principal")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class PolicyCreate(BaseModel):
|
|
62
|
+
"""A new access policy."""
|
|
63
|
+
|
|
64
|
+
name: str = Field(min_length=1, max_length=200, description="Short name")
|
|
65
|
+
description: str | None = Field(default=None, description="What it allows or denies")
|
|
66
|
+
effect: Literal["allow", "deny"] = Field(description="allow or deny")
|
|
67
|
+
subjects: Subjects = Field(description="Who it applies to")
|
|
68
|
+
actions: list[str] = Field(min_length=1, description="Action patterns, e.g. invoices:*")
|
|
69
|
+
condition: Any = Field(default=None, description="Condition (JSON), see GET /authz/catalog")
|
|
70
|
+
enabled: bool = Field(default=True, description="Disabled policies are ignored")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class PolicyUpdate(BaseModel):
|
|
74
|
+
"""Changes to a policy."""
|
|
75
|
+
|
|
76
|
+
name: str | None = Field(default=None, min_length=1, max_length=200)
|
|
77
|
+
description: str | None = None
|
|
78
|
+
effect: Literal["allow", "deny"] | None = None
|
|
79
|
+
subjects: Subjects | None = None
|
|
80
|
+
actions: list[str] | None = Field(default=None, min_length=1)
|
|
81
|
+
condition: Any = None
|
|
82
|
+
enabled: bool | None = None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class PolicyRead(BaseModel):
|
|
86
|
+
"""An access policy."""
|
|
87
|
+
|
|
88
|
+
model_config = ConfigDict(from_attributes=True)
|
|
89
|
+
|
|
90
|
+
id: str = Field(description="Policy id")
|
|
91
|
+
name: str = Field(description="Short name")
|
|
92
|
+
description: str | None = Field(default=None, description="What it allows or denies")
|
|
93
|
+
effect: str = Field(description="allow or deny")
|
|
94
|
+
subjects: dict[str, Any] = Field(description="Who it applies to")
|
|
95
|
+
actions: list[str] = Field(description="Action patterns")
|
|
96
|
+
condition: Any = Field(default=None, description="Condition (JSON)")
|
|
97
|
+
enabled: bool = Field(description="Whether it is evaluated")
|
|
98
|
+
created_at: datetime | None = Field(default=None, description="Creation time")
|
|
99
|
+
created_by: str | None = Field(default=None, description="Who created it")
|
|
100
|
+
updated_at: datetime | None = Field(default=None, description="Last change")
|
|
101
|
+
updated_by: str | None = Field(default=None, description="Who changed it last")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class RoleCreate(BaseModel):
|
|
105
|
+
"""A new custom role."""
|
|
106
|
+
|
|
107
|
+
name: str = Field(pattern=r"^[a-z][a-z0-9_.-]*$", max_length=100, description="Role name")
|
|
108
|
+
description: str | None = Field(default=None, description="What the role is for")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class RoleUpdate(BaseModel):
|
|
112
|
+
"""Changes to a custom role."""
|
|
113
|
+
|
|
114
|
+
description: str | None = None
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class RoleRead(BaseModel):
|
|
118
|
+
"""A custom role."""
|
|
119
|
+
|
|
120
|
+
model_config = ConfigDict(from_attributes=True)
|
|
121
|
+
|
|
122
|
+
name: str = Field(description="Role name")
|
|
123
|
+
description: str | None = Field(default=None, description="What the role is for")
|
|
124
|
+
created_at: datetime | None = Field(default=None, description="Creation time")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class AssignmentCreate(BaseModel):
|
|
128
|
+
"""Give a role to a principal or a group (exactly one of them)."""
|
|
129
|
+
|
|
130
|
+
role: str = Field(min_length=1, description="Role name (system or custom)")
|
|
131
|
+
principal: str | None = Field(default=None, description="Principal id (provider:subject)")
|
|
132
|
+
group: str | None = Field(default=None, description="Identity provider group")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class AssignmentRead(BaseModel):
|
|
136
|
+
"""A role assignment."""
|
|
137
|
+
|
|
138
|
+
model_config = ConfigDict(from_attributes=True)
|
|
139
|
+
|
|
140
|
+
id: str = Field(description="Assignment id")
|
|
141
|
+
role: str = Field(description="Role name")
|
|
142
|
+
principal: str | None = Field(default=None, description="Principal id")
|
|
143
|
+
group: str | None = Field(default=None, description="Group")
|
|
144
|
+
created_at: datetime | None = Field(default=None, description="Creation time")
|
|
145
|
+
created_by: str | None = Field(default=None, description="Who assigned it")
|