terp-cli 0.41.1__tar.gz → 0.42.0__tar.gz
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.
- {terp_cli-0.41.1 → terp_cli-0.42.0}/PKG-INFO +9 -9
- {terp_cli-0.41.1 → terp_cli-0.42.0}/pyproject.toml +9 -9
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/__init__.py +4 -4
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/access.py +16 -66
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/authz_surface.py +14 -108
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/guide_capability.py +14 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_compare.py +38 -2
- terp_cli-0.41.1/src/terp/cli/authz_changes.py +0 -321
- {terp_cli-0.41.1 → terp_cli-0.42.0}/.gitignore +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/signer/Dockerfile +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/signer/terp-arch-releases.json +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/_appref.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/_cors_view.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/_engine.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/_output.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/_subjects.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/api_contract.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/api_contract_bounds.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/api_contract_check.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/api_contract_schemas.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/api_contract_shapes.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/apidocs.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/capabilities.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/dependency_audit.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deploy_safety.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deployment_adopt.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deployment_app.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deployment_check.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deployment_declare.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/deployment_render.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/dev.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/docker.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/docker_masks.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/envfile.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/envrequired.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/envschema.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/envseams.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/fmt.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/grants.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/guide_deployment.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/guide_passwords.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/heartbeat.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/jobs.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/leases.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/locks.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/module_roles.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/npm_tree.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/openapi.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/outbox.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/ports.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/profiles.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/py.typed +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/__init__.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_common.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_dockerfile.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_git.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_image.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_locks.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_npm_lock.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_plan.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_registry.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_render.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/_sandbox.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/build.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/cli.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/dsse.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/gate.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/manifest.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/promote.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/release/sign.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/reseal.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/reseal_report.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/routes.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/scaffold.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/schema.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/seed.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/service_accounts.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/smoke.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/users.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/verify.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/version.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/workbench.py +0 -0
- {terp_cli-0.41.1 → terp_cli-0.42.0}/src/terp/cli/workbench_adopt.py +0 -0
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: terp-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.42.0
|
|
4
4
|
Summary: Terp command-line tool — inspect, scaffolding, migrations, checks, api-docs.
|
|
5
5
|
Project-URL: Repository, https://github.com/AITT-NL/terp-framework
|
|
6
6
|
Project-URL: Changelog, https://github.com/AITT-NL/terp-framework/blob/main/CHANGELOG.md
|
|
7
7
|
License-Expression: Apache-2.0
|
|
8
8
|
Requires-Python: >=3.13
|
|
9
9
|
Requires-Dist: pyyaml>=6.0
|
|
10
|
-
Requires-Dist: terp-arch==0.
|
|
11
|
-
Requires-Dist: terp-core==0.
|
|
12
|
-
Requires-Dist: terp-migrations==0.
|
|
10
|
+
Requires-Dist: terp-arch==0.42.0
|
|
11
|
+
Requires-Dist: terp-core==0.42.0
|
|
12
|
+
Requires-Dist: terp-migrations==0.42.0
|
|
13
13
|
Provides-Extra: jobs
|
|
14
|
-
Requires-Dist: terp-cap-outbox==0.
|
|
15
|
-
Requires-Dist: terp-cap-scheduler-apscheduler==0.
|
|
14
|
+
Requires-Dist: terp-cap-outbox==0.42.0; extra == 'jobs'
|
|
15
|
+
Requires-Dist: terp-cap-scheduler-apscheduler==0.42.0; extra == 'jobs'
|
|
16
16
|
Provides-Extra: release
|
|
17
17
|
Requires-Dist: cryptography>=46; extra == 'release'
|
|
18
18
|
Requires-Dist: packaging>=24; extra == 'release'
|
|
19
|
-
Requires-Dist: terp-cap-egress==0.
|
|
19
|
+
Requires-Dist: terp-cap-egress==0.42.0; extra == 'release'
|
|
20
20
|
Provides-Extra: scheduler
|
|
21
|
-
Requires-Dist: terp-cap-scheduler-apscheduler==0.
|
|
21
|
+
Requires-Dist: terp-cap-scheduler-apscheduler==0.42.0; extra == 'scheduler'
|
|
22
22
|
Provides-Extra: worker
|
|
23
|
-
Requires-Dist: terp-cap-outbox==0.
|
|
23
|
+
Requires-Dist: terp-cap-outbox==0.42.0; extra == 'worker'
|
|
@@ -4,18 +4,18 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "terp-cli"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.42.0"
|
|
8
8
|
description = "Terp command-line tool — inspect, scaffolding, migrations, checks, api-docs."
|
|
9
9
|
requires-python = ">=3.13"
|
|
10
10
|
license = "Apache-2.0"
|
|
11
11
|
dependencies = [
|
|
12
|
-
"terp-core==0.
|
|
12
|
+
"terp-core==0.42.0",
|
|
13
13
|
# `terp migrate` delegates to terp-migrations (lazily imported), keeping Alembic
|
|
14
14
|
# off the path for `terp inspect` / `terp guide` while making migrations work.
|
|
15
|
-
"terp-migrations==0.
|
|
15
|
+
"terp-migrations==0.42.0",
|
|
16
16
|
# `terp guide rules` projects the live rule registry; terp-arch is imported lazily
|
|
17
17
|
# (only when that topic is rendered), so it stays off the common `terp guide` path.
|
|
18
|
-
"terp-arch==0.
|
|
18
|
+
"terp-arch==0.42.0",
|
|
19
19
|
# `terp verify --only env-seams` and `terp smoke` read the compose profiles as data
|
|
20
20
|
# (anchors and `<<:` merge keys resolved), so both answer without a Docker daemon or
|
|
21
21
|
# even the `docker` binary. Declared explicitly rather than leant on: every app
|
|
@@ -29,11 +29,11 @@ dependencies = [
|
|
|
29
29
|
# or a scheduler engine into apps that only use inspect/check/migrate. Deployments can select
|
|
30
30
|
# one process role, while `jobs` is the convenient complete jobs-process bundle.
|
|
31
31
|
[project.optional-dependencies]
|
|
32
|
-
worker = ["terp-cap-outbox==0.
|
|
33
|
-
scheduler = ["terp-cap-scheduler-apscheduler==0.
|
|
32
|
+
worker = ["terp-cap-outbox==0.42.0"]
|
|
33
|
+
scheduler = ["terp-cap-scheduler-apscheduler==0.42.0"]
|
|
34
34
|
jobs = [
|
|
35
|
-
"terp-cap-outbox==0.
|
|
36
|
-
"terp-cap-scheduler-apscheduler==0.
|
|
35
|
+
"terp-cap-outbox==0.42.0",
|
|
36
|
+
"terp-cap-scheduler-apscheduler==0.42.0",
|
|
37
37
|
]
|
|
38
38
|
# `terp release` and `terp promote` (ADR 0187): the DSSE signature (cryptography), the lock's
|
|
39
39
|
# markers (packaging), and the one declared way to a registry, an index and the vulnerability
|
|
@@ -42,7 +42,7 @@ jobs = [
|
|
|
42
42
|
release = [
|
|
43
43
|
"cryptography>=46",
|
|
44
44
|
"packaging>=24",
|
|
45
|
-
"terp-cap-egress==0.
|
|
45
|
+
"terp-cap-egress==0.42.0",
|
|
46
46
|
]
|
|
47
47
|
|
|
48
48
|
[project.scripts]
|
|
@@ -910,16 +910,16 @@ Boundaries for a second top-level package (an ungated worker)
|
|
|
910
910
|
- WHAT THE COMPANION IS HELD TO, and what it is not. A companion root gets the rules whose
|
|
911
911
|
invariant holds for any Python that ships — `no_hardcoded_credentials`, `no_dynamic_sql`,
|
|
912
912
|
`no_raw_outbound_http`, `no_adhoc_config_decrypt`, `no_internal_imports`, `no_print`,
|
|
913
|
-
`no_naive_datetime`, `no_eval_or_exec`, `
|
|
914
|
-
`no_mutable_default_args`, `no_todo_fixme`, `
|
|
915
|
-
`
|
|
913
|
+
`no_naive_datetime`, `no_eval_or_exec`, `no_framework_patching`, `no_star_imports`,
|
|
914
|
+
`no_blocking_sleep`, `no_mutable_default_args`, `no_todo_fixme`, `no_empty_tests`,
|
|
915
|
+
`no_oversized_python_files`, `no_manual_table_schema`, `frozen_values_hold_no_mutable_collection`
|
|
916
916
|
and `request_timestamps_carry_their_offset`, since a worker that reads another system's
|
|
917
917
|
timestamps through a schema is held to their offset just as the app is.
|
|
918
918
|
It does NOT get the rules that are properties of being a mounted app: a route's response
|
|
919
919
|
model, a module's policy, a table's migration, the guarded session (ADR 0141). The check
|
|
920
920
|
report says which, per root, under `roots` — so a green over the companion names the
|
|
921
921
|
rules it is green about, and is a claim you can publish.
|
|
922
|
-
Expect the first declared run to be red.
|
|
922
|
+
Expect the first declared run to be red. Eighteen rules arriving at once over a package
|
|
923
923
|
nothing has ever scanned will find things; they were violations all along.
|
|
924
924
|
- WHAT A HAND-WRITTEN TEST IS STILL FOR. Not `no_dynamic_sql` or `no_print` — those now
|
|
925
925
|
reach the second package, and a bespoke AST scan beside them is a weaker copy that has
|
|
@@ -30,8 +30,8 @@ import json
|
|
|
30
30
|
from collections.abc import Sequence
|
|
31
31
|
|
|
32
32
|
from fastapi import FastAPI
|
|
33
|
-
from fastapi.routing import APIRoute
|
|
34
|
-
from starlette.routing import Mount, Route
|
|
33
|
+
from fastapi.routing import APIRoute
|
|
34
|
+
from starlette.routing import Mount, Route
|
|
35
35
|
|
|
36
36
|
from terp.core import (
|
|
37
37
|
ActorStampedMixin,
|
|
@@ -41,14 +41,16 @@ from terp.core import (
|
|
|
41
41
|
SoftDeleteMixin,
|
|
42
42
|
)
|
|
43
43
|
from terp.core.authz import build_access_model
|
|
44
|
+
from terp.core.authz_surface import (
|
|
45
|
+
API_PREFIX,
|
|
46
|
+
render_authz_surface,
|
|
47
|
+
served_routes,
|
|
48
|
+
unplaced_routes,
|
|
49
|
+
)
|
|
44
50
|
from terp.core.object_authz import registered_object_authz_predicates
|
|
45
51
|
from terp.core.scoping import registered_scope_predicates
|
|
46
52
|
|
|
47
53
|
|
|
48
|
-
# Every module mounts under this prefix (``create_app``); a served path outside it is a
|
|
49
|
-
# kernel / open route (e.g. health), not part of any module's policy surface.
|
|
50
|
-
_API_PREFIX = "/api/v1/"
|
|
51
|
-
|
|
52
54
|
def _mro_names(model: type) -> set[str]:
|
|
53
55
|
return {klass.__name__ for klass in model.__mro__}
|
|
54
56
|
|
|
@@ -194,44 +196,6 @@ def _undeclared_subscriber_alarms(specs: Sequence[ModuleSpec]) -> list[dict[str,
|
|
|
194
196
|
]
|
|
195
197
|
|
|
196
198
|
|
|
197
|
-
#: What a served WebSocket route is listed under: it has no HTTP method after the upgrade.
|
|
198
|
-
_WEBSOCKET = "WS"
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
def _served_routes(app: FastAPI) -> dict[str, frozenset[str]]:
|
|
202
|
-
"""Every path the composed app serves -> its HTTP methods, from the route table itself.
|
|
203
|
-
|
|
204
|
-
The table the router answers from, walked through every included router at the path
|
|
205
|
-
each route is really served under (``iter_route_contexts``, which FastAPI's own schema
|
|
206
|
-
generation walks). It used to be ``app.openapi()``, and a route declared with
|
|
207
|
-
``include_in_schema=False`` is not in that document: one on a router included inside
|
|
208
|
-
a module's router was served, was in no module's endpoints, and was reconciled
|
|
209
|
-
against a document that did not list it, so nothing reported it. A WebSocket route,
|
|
210
|
-
which the document never lists, is in the table too, under :data:`_WEBSOCKET`.
|
|
211
|
-
|
|
212
|
-
Outside ``/api/v1`` only an ``APIRoute`` is listed here: the plain routes and the
|
|
213
|
-
mounts there are :func:`_schema_hidden_routes`'s, reported with their own note.
|
|
214
|
-
"""
|
|
215
|
-
served: dict[str, set[str]] = {}
|
|
216
|
-
for context in iter_route_contexts(app.routes):
|
|
217
|
-
route = context.original_route
|
|
218
|
-
path = context.path or ""
|
|
219
|
-
if isinstance(route, APIRoute):
|
|
220
|
-
methods = set(context.methods or ())
|
|
221
|
-
elif not path.startswith(_API_PREFIX):
|
|
222
|
-
continue
|
|
223
|
-
elif isinstance(route, APIWebSocketRoute | WebSocketRoute):
|
|
224
|
-
methods = {_WEBSOCKET}
|
|
225
|
-
elif isinstance(route, Mount):
|
|
226
|
-
path, methods = f"{path}/*", {"*"}
|
|
227
|
-
else:
|
|
228
|
-
# A plain route, or a kind this walk does not know: served under the API
|
|
229
|
-
# prefix, so listed, with every method when it names none.
|
|
230
|
-
methods = set(getattr(route, "methods", None) or {"*"})
|
|
231
|
-
served.setdefault(path, set()).update(method.upper() for method in methods)
|
|
232
|
-
return {path: frozenset(methods) for path, methods in served.items()}
|
|
233
|
-
|
|
234
|
-
|
|
235
199
|
def _schema_hidden_routes(app: FastAPI) -> list[dict[str, object]]:
|
|
236
200
|
"""Reachable top-level routes that are no API route — still reported.
|
|
237
201
|
|
|
@@ -243,7 +207,7 @@ def _schema_hidden_routes(app: FastAPI) -> list[dict[str, object]]:
|
|
|
243
207
|
hidden: list[dict[str, object]] = []
|
|
244
208
|
for route in app.routes:
|
|
245
209
|
if isinstance(route, APIRoute):
|
|
246
|
-
continue # an API route: listed by
|
|
210
|
+
continue # an API route: listed by served_routes, in the schema or not
|
|
247
211
|
if isinstance(route, Mount):
|
|
248
212
|
hidden.append(
|
|
249
213
|
{
|
|
@@ -285,7 +249,7 @@ def build_access_graph_for_app(app: FastAPI) -> dict[str, object]:
|
|
|
285
249
|
"app was not composed by terp.core.create_app (no access-graph state on "
|
|
286
250
|
"app.state) — pass a terp app factory, e.g. --app app.main:build"
|
|
287
251
|
)
|
|
288
|
-
served =
|
|
252
|
+
served = served_routes(app)
|
|
289
253
|
kernel_routes: list[dict[str, object]] = [
|
|
290
254
|
{
|
|
291
255
|
"path": path,
|
|
@@ -301,7 +265,7 @@ def build_access_graph_for_app(app: FastAPI) -> dict[str, object]:
|
|
|
301
265
|
"note": "unauthenticated kernel route (no module policy, no operation)",
|
|
302
266
|
}
|
|
303
267
|
for path, methods in sorted(served.items())
|
|
304
|
-
if not path.startswith(
|
|
268
|
+
if not path.startswith(API_PREFIX)
|
|
305
269
|
]
|
|
306
270
|
kernel_routes.extend(_schema_hidden_routes(app))
|
|
307
271
|
graph = build_access_graph(
|
|
@@ -310,23 +274,11 @@ def build_access_graph_for_app(app: FastAPI) -> dict[str, object]:
|
|
|
310
274
|
kernel_routes=kernel_routes,
|
|
311
275
|
undeclared_subscribers=_undeclared_subscriber_alarms(specs),
|
|
312
276
|
)
|
|
313
|
-
|
|
277
|
+
# Every served ``/api/v1`` method must map to a module endpoint; one that does not is
|
|
278
|
+
# a route the record cannot hold, which the surface check and the app's own startup
|
|
279
|
+
# check both refuse (:func:`terp.core.authz_surface.unplaced_routes`).
|
|
314
280
|
modules: list = graph["modules"] # type: ignore[assignment]
|
|
315
|
-
|
|
316
|
-
for endpoint in module["endpoints"]:
|
|
317
|
-
# A WebSocket endpoint carries no methods; it covers the served WebSocket.
|
|
318
|
-
covered.setdefault(endpoint["path"], set()).update(endpoint["methods"] or [_WEBSOCKET])
|
|
319
|
-
omitted: list[dict[str, object]] = []
|
|
320
|
-
for path, methods in sorted(served.items()):
|
|
321
|
-
if not path.startswith(_API_PREFIX):
|
|
322
|
-
continue
|
|
323
|
-
missing = methods - covered.get(path, set())
|
|
324
|
-
if missing - {_WEBSOCKET}:
|
|
325
|
-
omitted.append({"path": path, "methods": sorted(missing - {_WEBSOCKET})})
|
|
326
|
-
if _WEBSOCKET in missing:
|
|
327
|
-
# No methods, as a module's own WebSocket endpoint has none.
|
|
328
|
-
omitted.append({"path": path, "methods": []})
|
|
329
|
-
graph["omitted_routes"] = omitted
|
|
281
|
+
graph["omitted_routes"] = unplaced_routes(served, modules)
|
|
330
282
|
return graph
|
|
331
283
|
|
|
332
284
|
|
|
@@ -424,12 +376,10 @@ def render_access_graph(graph: dict[str, object], fmt: str = "text") -> str:
|
|
|
424
376
|
"""Render a prebuilt access *graph* as ``text``, ``json`` or ``surface``.
|
|
425
377
|
|
|
426
378
|
``surface`` is the authority baseline the ``authz-surface`` verify check pins
|
|
427
|
-
(:mod:`terp.
|
|
379
|
+
(:mod:`terp.core.authz_surface`): the same projection reduced to what is an
|
|
428
380
|
authority claim, sorted at every level so the committed file is diffable.
|
|
429
381
|
"""
|
|
430
382
|
if fmt == "surface":
|
|
431
|
-
from terp.cli.authz_surface import render_authz_surface
|
|
432
|
-
|
|
433
383
|
return render_authz_surface(graph)
|
|
434
384
|
if fmt == "json":
|
|
435
385
|
return json.dumps(graph, indent=2)
|
|
@@ -16,6 +16,10 @@ it against a committed baseline, and reports the difference in the vocabulary of
|
|
|
16
16
|
change rather than as a JSON diff: *this route's requirement moved from X to Y*, *this
|
|
17
17
|
module became public*, *this endpoint is new and nothing has reviewed what it requires*.
|
|
18
18
|
|
|
19
|
+
The reduction, the record's name and the comparison are :mod:`terp.core.authz_surface`'s
|
|
20
|
+
and :mod:`terp.core.authz_changes`'s, because the app runs them again when it starts (ADR
|
|
21
|
+
0212). What is here is the verdict ``terp verify`` gives.
|
|
22
|
+
|
|
19
23
|
**Adoption is opt-in and half-adoption is impossible**, the shape ``api-docs-drift``
|
|
20
24
|
settled on: with no committed baseline the check skips with a note naming the command
|
|
21
25
|
that writes one, because upgrading the framework must not turn an app's gate red for a
|
|
@@ -29,114 +33,29 @@ separate, explicit command and never a ``--fix`` on the checker.
|
|
|
29
33
|
|
|
30
34
|
from __future__ import annotations
|
|
31
35
|
|
|
32
|
-
import json
|
|
33
36
|
import pathlib
|
|
34
37
|
import tomllib
|
|
35
38
|
from typing import Any, NamedTuple
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def authz_surface(graph: dict[str, Any]) -> dict[str, Any]:
|
|
46
|
-
"""Reduce an access graph to the part that is an authority claim.
|
|
47
|
-
|
|
48
|
-
Deliberately **not** the whole graph. The graph carries things that move for honest
|
|
49
|
-
reasons — a model's traits, the reconciliation's ``omitted_routes``, whatever a
|
|
50
|
-
future field adds — and a baseline that churned on those would be regenerated
|
|
51
|
-
reflexively until nobody read the diff. What survives here is what answers "who may
|
|
52
|
-
reach what": the role ladder, each permission's floor, and per module its policy plus
|
|
53
|
-
each endpoint's requirement, extra permissions, and per-rung outcome.
|
|
54
|
-
|
|
55
|
-
Sorted at every level, because the baseline is a **diffable** artifact: a set
|
|
56
|
-
rendered in registration order would produce a reviewable diff only by luck.
|
|
57
|
-
"""
|
|
58
|
-
roles = [
|
|
59
|
-
{"name": role["name"], "rank": role["rank"]}
|
|
60
|
-
for role in sorted(graph.get("roles", ()), key=lambda item: item["rank"])
|
|
61
|
-
]
|
|
62
|
-
permissions = [
|
|
63
|
-
{"name": permission["name"], "min_role": permission["min_role"]}
|
|
64
|
-
for permission in sorted(
|
|
65
|
-
graph.get("permissions", ()), key=lambda item: item["name"]
|
|
66
|
-
)
|
|
67
|
-
]
|
|
68
|
-
modules = [
|
|
69
|
-
{
|
|
70
|
-
"name": module["name"],
|
|
71
|
-
"policy": module.get("policy"),
|
|
72
|
-
"access": module.get("access"),
|
|
73
|
-
"endpoints": [
|
|
74
|
-
{
|
|
75
|
-
"path": endpoint["path"],
|
|
76
|
-
"methods": sorted(endpoint.get("methods", ())),
|
|
77
|
-
"requirement": endpoint["requirement"],
|
|
78
|
-
"extra_permissions": sorted(endpoint.get("extra_permissions", ())),
|
|
79
|
-
"by_role": {
|
|
80
|
-
rung["role"]: rung["reason"]
|
|
81
|
-
for rung in endpoint.get("by_role", ())
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
for endpoint in sorted(
|
|
85
|
-
module.get("endpoints", ()),
|
|
86
|
-
key=lambda item: (item["path"], sorted(item.get("methods", ()))),
|
|
87
|
-
)
|
|
88
|
-
],
|
|
89
|
-
}
|
|
90
|
-
for module in sorted(graph.get("modules", ()), key=lambda item: item["name"])
|
|
91
|
-
]
|
|
92
|
-
return {"authz_surface": 1, "roles": roles, "permissions": permissions, "modules": modules}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def render_authz_surface(graph: dict[str, Any]) -> str:
|
|
96
|
-
"""The baseline as the bytes a project commits (trailing newline, stable key order)."""
|
|
97
|
-
return json.dumps(authz_surface(graph), indent=2, sort_keys=True) + "\n"
|
|
40
|
+
from terp.core.authz_changes import authority_changes
|
|
41
|
+
from terp.core.authz_surface import (
|
|
42
|
+
ADOPT_HINT,
|
|
43
|
+
SURFACE_ARTIFACT,
|
|
44
|
+
authz_surface,
|
|
45
|
+
read_baseline,
|
|
46
|
+
unseen_routes,
|
|
47
|
+
)
|
|
98
48
|
|
|
99
49
|
|
|
100
50
|
def diff_authz_surface(baseline: dict[str, Any], current: dict[str, Any]) -> list[str]:
|
|
101
51
|
"""Every authority difference, each phrased as the change a reviewer has to approve.
|
|
102
52
|
|
|
103
|
-
The sentences of :func:`terp.
|
|
53
|
+
The sentences of :func:`terp.core.authz_changes.authority_changes`, which also says of
|
|
104
54
|
each whether it widens who can reach what (ADR 0209).
|
|
105
55
|
"""
|
|
106
|
-
from terp.cli.authz_changes import authority_changes
|
|
107
|
-
|
|
108
56
|
return [entry["change"] for entry in authority_changes(baseline, current)]
|
|
109
57
|
|
|
110
58
|
|
|
111
|
-
def served_twice(surface: dict[str, Any]) -> list[str]:
|
|
112
|
-
"""Every method and path that more than one route serves.
|
|
113
|
-
|
|
114
|
-
The router answers with the first route registered, while a diff keyed by method and
|
|
115
|
-
path compares whichever it met last, so a second route under an existing path can
|
|
116
|
-
serve a different policy with no line moving. The surface cannot say which route
|
|
117
|
-
answers, so each such pair is reported rather than resolved.
|
|
118
|
-
"""
|
|
119
|
-
seen: dict[tuple[str, str], int] = {}
|
|
120
|
-
for module in surface.get("modules", ()):
|
|
121
|
-
for endpoint in module.get("endpoints", ()):
|
|
122
|
-
for method in endpoint.get("methods") or ["WS"]:
|
|
123
|
-
key = (method, endpoint["path"])
|
|
124
|
-
seen[key] = seen.get(key, 0) + 1
|
|
125
|
-
return [
|
|
126
|
-
f"{method} {path}: served by {count} routes; the first one registered answers, "
|
|
127
|
-
"and this check cannot say which that is"
|
|
128
|
-
for (method, path), count in sorted(seen.items())
|
|
129
|
-
if count > 1
|
|
130
|
-
]
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def read_baseline(root: pathlib.Path) -> dict[str, Any] | None:
|
|
134
|
-
"""The committed baseline, or ``None`` when the project has not adopted the check."""
|
|
135
|
-
path = root / SURFACE_ARTIFACT
|
|
136
|
-
if not path.is_file():
|
|
137
|
-
return None
|
|
138
|
-
return json.loads(path.read_text(encoding="utf-8"))
|
|
139
|
-
|
|
140
59
|
#: The checks an app may pin (``[tool.terp.verify] pinned``): with the line, a missing
|
|
141
60
|
#: artifact is red instead of a skip (ADR 0209).
|
|
142
61
|
PINNABLE = frozenset({"authz-surface"})
|
|
@@ -220,7 +139,6 @@ def verdict(project_root: pathlib.Path, app_ref: str, note_prefix: str) -> Verdi
|
|
|
220
139
|
"""
|
|
221
140
|
from terp.cli._appref import load_app, push_app_root
|
|
222
141
|
from terp.cli.access import build_access_graph_for_app
|
|
223
|
-
from terp.cli.authz_changes import authority_changes
|
|
224
142
|
|
|
225
143
|
try:
|
|
226
144
|
baseline = read_baseline(project_root)
|
|
@@ -277,19 +195,7 @@ def verdict(project_root: pathlib.Path, app_ref: str, note_prefix: str) -> Verdi
|
|
|
277
195
|
# could not place (a router included inside a module's router, served under a path
|
|
278
196
|
# the graph cannot recover) and a method and path two routes serve. Either one makes
|
|
279
197
|
# "no difference" an answer about a surface that is not the one being served.
|
|
280
|
-
unseen =
|
|
281
|
-
{
|
|
282
|
-
"kind": "unplaced",
|
|
283
|
-
"methods": list(route.get("methods") or ["WS"]),
|
|
284
|
-
"path": route["path"],
|
|
285
|
-
"detail": (
|
|
286
|
-
f"{','.join(route.get('methods') or ['WS'])} {route['path']}: served, but "
|
|
287
|
-
"the access graph cannot place it (a router included inside a module's "
|
|
288
|
-
"router); declare it on the module's own router"
|
|
289
|
-
),
|
|
290
|
-
}
|
|
291
|
-
for route in graph.get("omitted_routes") or ()
|
|
292
|
-
] + [{"kind": "served_twice", "detail": line} for line in served_twice(current)]
|
|
198
|
+
unseen = unseen_routes(graph.get("omitted_routes") or (), current)
|
|
293
199
|
if unseen:
|
|
294
200
|
listing = "\n".join(f" - {item['detail']}" for item in unseen)
|
|
295
201
|
return Verdict(
|
|
@@ -97,4 +97,18 @@ Using capabilities
|
|
|
97
97
|
allow_no_provider=True) mounts the SSO routes with an empty list, and each answers
|
|
98
98
|
503 sso_not_configured. The settings decide what a mounted route does, never whether
|
|
99
99
|
it exists.
|
|
100
|
+
- APP CODE CALLS THE FRAMEWORK AND NEVER REPLACES ITS FUNCTIONS (ADR 0213). A check that
|
|
101
|
+
runs the framework's code is worth what that code is, and one assignment turns it off
|
|
102
|
+
with every other check still green:
|
|
103
|
+
# refused (no_framework_patching)
|
|
104
|
+
import terp.core.authz_surface as surface
|
|
105
|
+
surface.startup_refusal = lambda *args, **kwargs: None
|
|
106
|
+
setattr(routing, "iter_route_contexts", mine) # fastapi and starlette too
|
|
107
|
+
sys.modules["terp.core.app"] = shim
|
|
108
|
+
mock.patch("terp.core.app.create_app", mine).start()
|
|
109
|
+
settings.ENVIRONMENT = "local"
|
|
110
|
+
Change behaviour through the seam the framework gives for it: a create_app parameter
|
|
111
|
+
(principal_provider=, middleware=, lease_store=), a ModuleSpec and its Policy, a
|
|
112
|
+
registered predicate. In a test, monkeypatch.setattr and mock.patch stay the way to
|
|
113
|
+
replace a function: the rule does not read `tests` directories.
|
|
100
114
|
"""
|
|
@@ -22,7 +22,11 @@ anything the image would say about itself when run:
|
|
|
22
22
|
site-packages), its marker files, ``lib64``, the interpreter links in ``bin`` (to an
|
|
23
23
|
interpreter outside ``/srv``) and the activation scripts. A ``.pth`` file and a
|
|
24
24
|
``sitecustomize`` or ``usercustomize`` module are held the same way, so one no ``RECORD``
|
|
25
|
-
lists is refused, and so is any file in ``/srv`` outside the two roots and the environment
|
|
25
|
+
lists is refused, and so is any file in ``/srv`` outside the two roots and the environment,
|
|
26
|
+
but the record below;
|
|
27
|
+
* ``/srv/authz-surface.json``, the record the app compares the routes it composed with before
|
|
28
|
+
it serves (ADR 0212), is the tree's, and is there when the tree holds one and the image's own
|
|
29
|
+
Dockerfile copies it;
|
|
26
30
|
* the configuration that decides what the image runs (entrypoint, command, environment, user,
|
|
27
31
|
working directory, healthcheck, volumes, stop signal, shell) is what its Dockerfile makes of
|
|
28
32
|
the configuration of the base it pins (:mod:`._dockerfile`).
|
|
@@ -51,6 +55,7 @@ from terp.cli.release._image import Node, metadata, record
|
|
|
51
55
|
from terp.cli.release._locks import marker_environment, normalise, runtime_set
|
|
52
56
|
from terp.cli.release._sandbox import SOURCE_ROOTS as ROOTS
|
|
53
57
|
from terp.cli.release._sandbox import pyc_source
|
|
58
|
+
from terp.core.authz_surface import SURFACE_ARTIFACT
|
|
54
59
|
|
|
55
60
|
_SITE = re.compile(r"(?P<site>srv/\.venv/lib/python[0-9.]+/site-packages)/[^/]+\.dist-info/METADATA")
|
|
56
61
|
_MACHINES = {"amd64": "x86_64", "arm64": "aarch64", "386": "i686", "arm": "armv7l"}
|
|
@@ -63,6 +68,11 @@ _MADE_WITH = frozenset({"pyvenv.cfg", ".gitignore", ".lock", "CACHEDIR.TAG"})
|
|
|
63
68
|
_ACTIVATION = re.compile(r"activate(?:\.[a-z0-9]+)?|activate_this\.py|deactivate\.bat|pydoc\.bat")
|
|
64
69
|
_INTERPRETER = re.compile(r"python(?:3(?:\.[0-9]+)?)?")
|
|
65
70
|
|
|
71
|
+
#: The record of who can reach what, the one file of ``/srv`` that is neither source nor
|
|
72
|
+
#: environment, and the instruction of a Dockerfile that copies it there.
|
|
73
|
+
_RECORD = f"srv/{SURFACE_ARTIFACT}"
|
|
74
|
+
_COPIES_RECORD = re.compile(r"^[ \t]*COPY[ \t][^\n]*authz-surface\.jso", re.IGNORECASE | re.MULTILINE)
|
|
75
|
+
|
|
66
76
|
|
|
67
77
|
def wheel_shape(tree: Tree) -> None:
|
|
68
78
|
"""Refuse an app wheel whose files land where the tree does not hold them."""
|
|
@@ -116,10 +126,35 @@ def _roots(tree: Tree, nodes: dict[str, Node], findings: list[str]) -> None:
|
|
|
116
126
|
findings.append(f"{path} is in the tree and the build context, and not in the image")
|
|
117
127
|
compared = tuple(f"srv/{root}/" for root in ROOTS) + (_VENV,)
|
|
118
128
|
for path, node in sorted(nodes.items()):
|
|
119
|
-
if node.kind != "dir" and not path.startswith(compared):
|
|
129
|
+
if node.kind != "dir" and path != _RECORD and not path.startswith(compared):
|
|
120
130
|
findings.append(f"/{path} is in /srv outside {', '.join(ROOTS)} and .venv, where sign holds it to nothing")
|
|
121
131
|
|
|
122
132
|
|
|
133
|
+
def _surface_record(tree: Tree, nodes: dict[str, Node], dockerfile: str, findings: list[str]) -> None:
|
|
134
|
+
"""Hold the image's record of who can reach what to the tree's (ADR 0212).
|
|
135
|
+
|
|
136
|
+
The app compares the routes it composed with ``/srv/authz-surface.json`` before it serves,
|
|
137
|
+
so a record that is not the tree's would have it hold itself to something no reviewer saw.
|
|
138
|
+
|
|
139
|
+
An image without the record is not compared when it starts. That is a finding where the
|
|
140
|
+
tree holds the record and the image's own Dockerfile copies it, because something then
|
|
141
|
+
kept it out of the build context, and one line in ``.dockerignore`` would otherwise turn
|
|
142
|
+
the startup check off with every other answer here unchanged. A Dockerfile that does not
|
|
143
|
+
copy it, one rendered before the record was shipped or an app's own, builds an image that
|
|
144
|
+
starts without the comparison, as it did before.
|
|
145
|
+
"""
|
|
146
|
+
image, ours = nodes.get(_RECORD), _tree_node(tree, SURFACE_ARTIFACT)
|
|
147
|
+
if image is not None:
|
|
148
|
+
if not _same(image, ours):
|
|
149
|
+
findings.append(f"/{_RECORD} is not the tree's {SURFACE_ARTIFACT}")
|
|
150
|
+
elif ours is not None and _COPIES_RECORD.search(tree.read_text(dockerfile)):
|
|
151
|
+
findings.append(
|
|
152
|
+
f"{SURFACE_ARTIFACT} is in the tree, {dockerfile} copies it, and it is not in the "
|
|
153
|
+
"image, so the app would start without comparing the routes it serves with it: "
|
|
154
|
+
"keep the record in the build context (.dockerignore)"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
123
158
|
def _bytecode(tree: Tree, nodes: dict[str, Node], compiled: dict[str, Any], findings: list[str]) -> None:
|
|
124
159
|
for path, node in sorted(nodes.items()):
|
|
125
160
|
if not path.startswith(tuple(f"srv/{root}/" for root in ROOTS)) or "/__pycache__/" not in path:
|
|
@@ -300,6 +335,7 @@ def compare_backend(
|
|
|
300
335
|
findings.append("the image is not built on the base its Dockerfile names, so its bytecode cannot be checked")
|
|
301
336
|
_configuration(tree, dockerfile, config, compiled, findings)
|
|
302
337
|
_roots(tree, nodes, findings)
|
|
338
|
+
_surface_record(tree, nodes, dockerfile, findings)
|
|
303
339
|
_bytecode(tree, nodes, compiled, findings)
|
|
304
340
|
site = _site(nodes)
|
|
305
341
|
compared = _wheel(tree, nodes, texts, site, project, findings)
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
"""Each change to who can reach what, labelled a widening or a narrowing (ADR 0209).
|
|
2
|
-
|
|
3
|
-
The ``authz-surface`` check compares an app's composed surface with its committed baseline
|
|
4
|
-
(ADR 0139). This module says what each difference is: one entry per changed endpoint,
|
|
5
|
-
module, role ladder or permission set, each carrying ``widens``.
|
|
6
|
-
|
|
7
|
-
A change is a narrowing only when the guard's own replayed answers prove that nobody gains
|
|
8
|
-
reach; everything else is a widening, including a difference this module cannot itemise.
|
|
9
|
-
It is set inclusion over the answers ``decide`` already gives, never a judgement of which
|
|
10
|
-
change is dangerous: that would be a second authorization model, which ADR 0121 exists to
|
|
11
|
-
prevent. A tool can then put the widenings, and only those, in front of a person.
|
|
12
|
-
"""
|
|
13
|
-
|
|
14
|
-
from __future__ import annotations
|
|
15
|
-
|
|
16
|
-
from typing import Any
|
|
17
|
-
|
|
18
|
-
#: How far one rung's answer reaches. A ``grant`` reaches whoever holds the permission. Every
|
|
19
|
-
#: answer not named here (``rank``, ``no_policy``, ``unknown_permission``,
|
|
20
|
-
#: ``unauthenticated``, ``unregistered_role``) refuses, and reaches nobody.
|
|
21
|
-
_REACH = {"public": 3, "allowed": 2, "allowed_in_module": 2, "grant": 1}
|
|
22
|
-
|
|
23
|
-
Key = tuple[str, str]
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def _reach(answer: object) -> int:
|
|
27
|
-
return _REACH.get(answer, 0) if isinstance(answer, str) else 0
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def endpoint_key(endpoint: dict[str, Any]) -> Key:
|
|
31
|
-
"""``(path, methods)``: the identity a baseline and a surface compare endpoints on."""
|
|
32
|
-
return endpoint["path"], ",".join(endpoint.get("methods", ())) or "WS"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def _endpoints(surface: dict[str, Any]) -> dict[Key, tuple[str, dict[str, Any]]]:
|
|
36
|
-
return {
|
|
37
|
-
endpoint_key(endpoint): (module["name"], endpoint)
|
|
38
|
-
for module in surface.get("modules", ())
|
|
39
|
-
for endpoint in module.get("endpoints", ())
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def _labels(graph: dict[str, Any] | None) -> tuple[dict[Key, Any], dict[str, Any]]:
|
|
44
|
-
"""Each endpoint's declared operation, and each permission's label, from the graph."""
|
|
45
|
-
operations: dict[Key, Any] = {}
|
|
46
|
-
permissions: dict[str, Any] = {}
|
|
47
|
-
for module in (graph or {}).get("modules", ()):
|
|
48
|
-
for endpoint in module.get("endpoints", ()):
|
|
49
|
-
operations[endpoint_key(endpoint)] = endpoint.get("operation")
|
|
50
|
-
for permission in (graph or {}).get("permissions", ()):
|
|
51
|
-
permissions[permission["name"]] = permission.get("label")
|
|
52
|
-
return operations, permissions
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def _entry(subject: str, change: str, widens: bool, **detail: Any) -> dict[str, Any]:
|
|
56
|
-
methods = detail.get("methods")
|
|
57
|
-
return {
|
|
58
|
-
"subject": subject,
|
|
59
|
-
"module": detail.get("module"),
|
|
60
|
-
"methods": methods.split(",") if methods else [],
|
|
61
|
-
"path": detail.get("path"),
|
|
62
|
-
"operation": detail.get("operation"),
|
|
63
|
-
"gains": sorted(detail.get("gains", ())),
|
|
64
|
-
"loses": sorted(detail.get("loses", ())),
|
|
65
|
-
"anonymous": bool(detail.get("anonymous")),
|
|
66
|
-
"permissions": detail.get("permissions", []),
|
|
67
|
-
"change": change,
|
|
68
|
-
"widens": widens,
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def _permissions_involved(
|
|
73
|
-
names: set[str], permission_labels: dict[str, Any]
|
|
74
|
-
) -> list[dict[str, Any]]:
|
|
75
|
-
return [{"name": name, "label": permission_labels.get(name)} for name in sorted(names)]
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def _named_permissions(*endpoints: dict[str, Any]) -> set[str]:
|
|
79
|
-
names: set[str] = set()
|
|
80
|
-
for endpoint in endpoints:
|
|
81
|
-
kind, _, name = str(endpoint.get("requirement", "")).partition(":")
|
|
82
|
-
if kind == "permission":
|
|
83
|
-
names.add(name)
|
|
84
|
-
names.update(endpoint.get("extra_permissions") or ())
|
|
85
|
-
return names
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def _endpoint_change(
|
|
89
|
-
key: Key,
|
|
90
|
-
module: str,
|
|
91
|
-
was: dict[str, Any],
|
|
92
|
-
now: dict[str, Any],
|
|
93
|
-
operation: Any,
|
|
94
|
-
permission_labels: dict[str, Any],
|
|
95
|
-
) -> dict[str, Any] | None:
|
|
96
|
-
path, methods = key
|
|
97
|
-
before, after = was.get("by_role", {}), now.get("by_role", {})
|
|
98
|
-
rungs = sorted(set(before) | set(after))
|
|
99
|
-
gains = [rung for rung in rungs if _reach(after.get(rung)) > _reach(before.get(rung))]
|
|
100
|
-
loses = [rung for rung in rungs if _reach(after.get(rung)) < _reach(before.get(rung))]
|
|
101
|
-
was_grants = list(was.get("extra_permissions") or ())
|
|
102
|
-
now_grants = list(now.get("extra_permissions") or ())
|
|
103
|
-
parts: list[str] = []
|
|
104
|
-
if was["requirement"] != now["requirement"]:
|
|
105
|
-
parts.append(f"requirement {was['requirement']!r} -> {now['requirement']!r}")
|
|
106
|
-
if was_grants != now_grants:
|
|
107
|
-
parts.append(f"required grants {was_grants} -> {now_grants}")
|
|
108
|
-
newly = [rung for rung in gains if _reach(before.get(rung)) == 0]
|
|
109
|
-
if newly:
|
|
110
|
-
parts.append(f"now reachable by {newly} (it was not before)")
|
|
111
|
-
if wider := [rung for rung in gains if rung not in newly]:
|
|
112
|
-
parts.append(f"reachable more widely by {wider}")
|
|
113
|
-
if loses:
|
|
114
|
-
parts.append(f"reachable less widely by {loses}")
|
|
115
|
-
if not parts:
|
|
116
|
-
if before == after:
|
|
117
|
-
return None
|
|
118
|
-
parts.append(f"the answers per rung changed: {before} -> {after}")
|
|
119
|
-
# A grant on both sides of a changed requirement names another permission, so the
|
|
120
|
-
# people who hold it may differ; fewer required grants admit more of them.
|
|
121
|
-
regranted = was["requirement"] != now["requirement"] and any(
|
|
122
|
-
before.get(rung) == "grant" and after.get(rung) == "grant" for rung in rungs
|
|
123
|
-
)
|
|
124
|
-
widens = bool(gains) or regranted or not set(was_grants) <= set(now_grants)
|
|
125
|
-
return _entry(
|
|
126
|
-
"endpoint",
|
|
127
|
-
f"{methods} {path}: " + "; ".join(parts),
|
|
128
|
-
widens,
|
|
129
|
-
module=module,
|
|
130
|
-
methods=methods,
|
|
131
|
-
path=path,
|
|
132
|
-
operation=operation,
|
|
133
|
-
gains=gains,
|
|
134
|
-
loses=loses,
|
|
135
|
-
anonymous=any(answer == "public" for answer in after.values()),
|
|
136
|
-
permissions=_permissions_involved(_named_permissions(was, now), permission_labels),
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
def _floor(requirement: object, surface: dict[str, Any]) -> tuple[int, bool] | None:
|
|
141
|
-
"""``(rank, needs a grant)`` for a ``role:`` or ``permission:`` requirement, or ``None``."""
|
|
142
|
-
kind, _, name = str(requirement).partition(":")
|
|
143
|
-
ranks = {role["name"]: role["rank"] for role in surface.get("roles", ())}
|
|
144
|
-
if kind == "role":
|
|
145
|
-
return (ranks[name], False) if name in ranks else None
|
|
146
|
-
if kind == "permission":
|
|
147
|
-
floors = {item["name"]: item["min_role"] for item in surface.get("permissions", ())}
|
|
148
|
-
floor = floors.get(name)
|
|
149
|
-
return (ranks[floor], True) if floor in ranks else None
|
|
150
|
-
return None
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def _requirement_narrows(
|
|
154
|
-
was: object, now: object, baseline: dict[str, Any], current: dict[str, Any]
|
|
155
|
-
) -> bool:
|
|
156
|
-
if was == now:
|
|
157
|
-
return True
|
|
158
|
-
old, new = _floor(was, baseline), _floor(now, current)
|
|
159
|
-
if old is None or new is None:
|
|
160
|
-
return False
|
|
161
|
-
(old_rank, old_grant), new_rank = old, new[0]
|
|
162
|
-
# A permission moved to another permission, or to a role, admits people who were not
|
|
163
|
-
# admitted before: a role admits its whole rung, a different permission other holders.
|
|
164
|
-
if old_grant:
|
|
165
|
-
return False
|
|
166
|
-
return new_rank >= old_rank
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
def _policy_narrows(
|
|
170
|
-
was: Any, now: Any, baseline: dict[str, Any], current: dict[str, Any]
|
|
171
|
-
) -> bool:
|
|
172
|
-
if was == now or now is None:
|
|
173
|
-
return True # no policy refuses every caller
|
|
174
|
-
if was is None:
|
|
175
|
-
return False
|
|
176
|
-
if now.get("public"):
|
|
177
|
-
return bool(was.get("public")) and not (
|
|
178
|
-
now.get("allows_public_writes") and not was.get("allows_public_writes")
|
|
179
|
-
)
|
|
180
|
-
if was.get("public"):
|
|
181
|
-
return True
|
|
182
|
-
if was.get("authenticated") and not now.get("authenticated"):
|
|
183
|
-
return False
|
|
184
|
-
return all(
|
|
185
|
-
_requirement_narrows(was.get(tier), now.get(tier), baseline, current)
|
|
186
|
-
for tier in ("read", "write")
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def _module_changes(baseline: dict[str, Any], current: dict[str, Any]) -> list[dict[str, Any]]:
|
|
191
|
-
"""A module's own policy and access, compared on their own.
|
|
192
|
-
|
|
193
|
-
The endpoint entries test one method per route, so a route serving GET and POST can have
|
|
194
|
-
its read tier lowered with no endpoint entry moving; and a module that starts taking
|
|
195
|
-
per-module roles lets an assignment lift a caller over a floor (ADR 0121) without any
|
|
196
|
-
rung's answer changing in this view.
|
|
197
|
-
"""
|
|
198
|
-
before = {module["name"]: module for module in baseline.get("modules", ())}
|
|
199
|
-
after = {module["name"]: module for module in current.get("modules", ())}
|
|
200
|
-
entries: list[dict[str, Any]] = []
|
|
201
|
-
for name in sorted(before.keys() & after.keys()):
|
|
202
|
-
was, now = before[name], after[name]
|
|
203
|
-
if was.get("policy") != now.get("policy"):
|
|
204
|
-
entries.append(
|
|
205
|
-
_entry(
|
|
206
|
-
"module",
|
|
207
|
-
f"module {name}: policy {was.get('policy')!r} -> {now.get('policy')!r}",
|
|
208
|
-
not _policy_narrows(was.get("policy"), now.get("policy"), baseline, current),
|
|
209
|
-
module=name,
|
|
210
|
-
)
|
|
211
|
-
)
|
|
212
|
-
if was.get("access") != now.get("access"):
|
|
213
|
-
assignable = bool((now.get("access") or {}).get("assignable"))
|
|
214
|
-
entries.append(
|
|
215
|
-
_entry(
|
|
216
|
-
"module",
|
|
217
|
-
f"module {name}: access {was.get('access')!r} -> {now.get('access')!r}",
|
|
218
|
-
assignable and not bool((was.get("access") or {}).get("assignable")),
|
|
219
|
-
module=name,
|
|
220
|
-
)
|
|
221
|
-
)
|
|
222
|
-
return entries
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
def _floors_fell(baseline: dict[str, Any], current: dict[str, Any]) -> bool:
|
|
226
|
-
"""Whether a permission declared on both sides now has a lower floor (or one unknown)."""
|
|
227
|
-
before = {item["name"]: item["min_role"] for item in baseline.get("permissions", ())}
|
|
228
|
-
after = {item["name"]: item["min_role"] for item in current.get("permissions", ())}
|
|
229
|
-
old_ranks = {role["name"]: role["rank"] for role in baseline.get("roles", ())}
|
|
230
|
-
new_ranks = {role["name"]: role["rank"] for role in current.get("roles", ())}
|
|
231
|
-
for name in before.keys() & after.keys():
|
|
232
|
-
old, new = old_ranks.get(before[name]), new_ranks.get(after[name])
|
|
233
|
-
if old is None or new is None or new < old:
|
|
234
|
-
return True
|
|
235
|
-
return False
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
def authority_changes(
|
|
239
|
-
baseline: dict[str, Any],
|
|
240
|
-
current: dict[str, Any],
|
|
241
|
-
graph: dict[str, Any] | None = None,
|
|
242
|
-
) -> list[dict[str, Any]]:
|
|
243
|
-
"""Every authority difference, each labelled with whether it widens who can reach what.
|
|
244
|
-
|
|
245
|
-
*graph* (the access graph *current* was reduced from) supplies the labels a person reads
|
|
246
|
-
a change by: each endpoint's declared operation and each permission's label. The
|
|
247
|
-
baseline has neither, so an endpoint that is gone carries no operation.
|
|
248
|
-
"""
|
|
249
|
-
operations, permission_labels = _labels(graph)
|
|
250
|
-
entries: list[dict[str, Any]] = []
|
|
251
|
-
before, after = _endpoints(baseline), _endpoints(current)
|
|
252
|
-
for key in sorted(before.keys() & after.keys()):
|
|
253
|
-
module, now = after[key]
|
|
254
|
-
changed = _endpoint_change(
|
|
255
|
-
key, module, before[key][1], now, operations.get(key), permission_labels
|
|
256
|
-
)
|
|
257
|
-
if changed is not None:
|
|
258
|
-
entries.append(changed)
|
|
259
|
-
for key in sorted(after.keys() - before.keys()):
|
|
260
|
-
module, now = after[key]
|
|
261
|
-
reaches = [rung for rung, answer in now.get("by_role", {}).items() if _reach(answer)]
|
|
262
|
-
entries.append(
|
|
263
|
-
_entry(
|
|
264
|
-
"endpoint",
|
|
265
|
-
f"{key[1]} {key[0]}: NEW endpoint, requirement {now['requirement']!r} - "
|
|
266
|
-
"nothing has reviewed what it requires",
|
|
267
|
-
bool(reaches),
|
|
268
|
-
module=module,
|
|
269
|
-
methods=key[1],
|
|
270
|
-
path=key[0],
|
|
271
|
-
operation=operations.get(key),
|
|
272
|
-
gains=reaches,
|
|
273
|
-
anonymous=any(answer == "public" for answer in now.get("by_role", {}).values()),
|
|
274
|
-
permissions=_permissions_involved(_named_permissions(now), permission_labels),
|
|
275
|
-
)
|
|
276
|
-
)
|
|
277
|
-
for key in sorted(before.keys() - after.keys()):
|
|
278
|
-
module, was = before[key]
|
|
279
|
-
entries.append(
|
|
280
|
-
_entry(
|
|
281
|
-
"endpoint",
|
|
282
|
-
f"{key[1]} {key[0]}: gone from the surface",
|
|
283
|
-
False,
|
|
284
|
-
module=module,
|
|
285
|
-
methods=key[1],
|
|
286
|
-
path=key[0],
|
|
287
|
-
loses=[rung for rung, answer in was.get("by_role", {}).items() if _reach(answer)],
|
|
288
|
-
)
|
|
289
|
-
)
|
|
290
|
-
entries.extend(_module_changes(baseline, current))
|
|
291
|
-
# The ladder itself, last: a changed rank silently re-scores every floor above it, so it
|
|
292
|
-
# is a widening however it moved.
|
|
293
|
-
if baseline.get("roles") != current.get("roles"):
|
|
294
|
-
entries.append(
|
|
295
|
-
_entry(
|
|
296
|
-
"roles",
|
|
297
|
-
f"the role ladder changed: {baseline.get('roles')} -> {current.get('roles')}",
|
|
298
|
-
True,
|
|
299
|
-
)
|
|
300
|
-
)
|
|
301
|
-
if baseline.get("permissions") != current.get("permissions"):
|
|
302
|
-
entries.append(
|
|
303
|
-
_entry(
|
|
304
|
-
"permissions",
|
|
305
|
-
"the declared permissions or their floors changed",
|
|
306
|
-
_floors_fell(baseline, current),
|
|
307
|
-
)
|
|
308
|
-
)
|
|
309
|
-
# Anything the entries above do not name is still a change to who can reach what, or to
|
|
310
|
-
# the record of it. Passing it as "no difference" is the one answer this check must never
|
|
311
|
-
# give, so it is reported whole, and as a widening.
|
|
312
|
-
if not entries and baseline != current:
|
|
313
|
-
entries.append(
|
|
314
|
-
_entry(
|
|
315
|
-
"surface",
|
|
316
|
-
"the authorization surface changed in a way this check does not itemise; "
|
|
317
|
-
"read the diff of authz-surface.json line by line",
|
|
318
|
-
True,
|
|
319
|
-
)
|
|
320
|
-
)
|
|
321
|
-
return entries
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|