fastapi-m8 4.5.0__tar.gz → 4.6.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.
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.github/workflows/CI.yaml +1 -1
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/CHANGELOG.md +65 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/PKG-INFO +50 -2
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/README.md +48 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/constraints-all.txt +1 -1
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/constraints.txt +1 -1
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/__init__.py +26 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_compat.py +14 -3
- fastapi_m8-4.6.0/fastapi_m8/_service_client.py +109 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_version.py +1 -1
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/pyproject.toml +2 -2
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_compat.py +37 -8
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_public_typing.py +32 -0
- fastapi_m8-4.6.0/tests/test_service_client.py +204 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.codacy.yml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.dockerignore +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.env.example +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.gitattributes +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.github/FUNDING.yml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.github/dependabot.yml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.github/workflows/PiPy.yml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.gitignore +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.gitleaks.toml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.markdownlint.yaml +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/.pydocstyle +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/AGENTS.md +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/CLAUDE.md +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/LICENSE +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/REPOSITORY_CONTEXT.md +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/SECURITY.md +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_api_key.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_app.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_async_stub.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_deps.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_engine.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_events.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_health.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_internal_auth.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_revocation.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/_route_audit.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/config.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/scripts/__init__.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/scripts/docker_start.sh +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/fastapi_m8/scripts/pre_start.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/__init__.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/conftest.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_api_key.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_api_key_deps.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_app.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_app_extra.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_async_stub.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_changelog_version_parity.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_ci_policy.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_config.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_config_file_secrets.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_deps.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_engine.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_event_signing_gate.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_events.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_fixture_matrix_contract.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_health.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_host_header_routing.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_internal_auth.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_meta.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_packaging.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_pre_start.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_revocation.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_route_audit.py +0 -0
- {fastapi_m8-4.5.0 → fastapi_m8-4.6.0}/tests/test_version_source_parity.py +0 -0
|
@@ -103,7 +103,7 @@ jobs:
|
|
|
103
103
|
|
|
104
104
|
- name: Upload coverage to Codecov
|
|
105
105
|
if: matrix.python-version == '3.12' && github.actor != 'dependabot[bot]'
|
|
106
|
-
uses: codecov/codecov-action@
|
|
106
|
+
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
|
|
107
107
|
with:
|
|
108
108
|
token: ${{ secrets.CODECOV_TOKEN }}
|
|
109
109
|
files: coverage.xml
|
|
@@ -5,6 +5,71 @@ Format: [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) · Versioning:
|
|
|
5
5
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
+
## [4.6.0] - 2026-10-19
|
|
9
|
+
|
|
10
|
+
Adds the service-client guard a consumer needs when other services, not users,
|
|
11
|
+
call its routes (the `ai-gateway-m8` execution routes are the first), and
|
|
12
|
+
raises the `auth-sdk-m8` floor to `3.2.1`. Additive: no existing name changes
|
|
13
|
+
and no dependency is added. Absorbs the unpublished `4.5.2` floor raise, which
|
|
14
|
+
never shipped on its own.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- **`build_service_client_guard(registry, *, required_scopes)`**
|
|
19
|
+
(`fastapi_m8._service_client`). A FastAPI dependency that reads
|
|
20
|
+
`X-Internal-Client` and `X-Internal-Token` and returns the authenticated
|
|
21
|
+
client id. A missing header, an unknown client id or a wrong secret answers
|
|
22
|
+
`401` with one fixed body, and the registry's constant-time comparison runs in
|
|
23
|
+
each case (its throwaway credential answers an unknown id), so the guard adds
|
|
24
|
+
no client-enumeration oracle. An authenticated client lacking any required
|
|
25
|
+
scope answers `403`. The token is never logged or echoed. An empty scope set,
|
|
26
|
+
a bare `str` and an empty or non-string scope are refused at build time.
|
|
27
|
+
- **Re-exports from `auth-sdk-m8`**: `ConsumerCredential`,
|
|
28
|
+
`ConsumerCredentialRegistry`, `ConsumerAuthenticationError`,
|
|
29
|
+
`ConsumerScopeError`, `INTERNAL_CLIENT_HEADER` and `INTERNAL_TOKEN_HEADER`,
|
|
30
|
+
each the SDK object itself, so a consumer still imports nothing from
|
|
31
|
+
`auth_sdk_m8`.
|
|
32
|
+
- **`COMPAT_MATRIX["4.6"] = {"auth-sdk-m8": ">=3.2.1,<4.0.0"}`**, the
|
|
33
|
+
`pyproject.toml` floor. Every re-exported name already ships in
|
|
34
|
+
`auth-sdk-m8 3.2.0`; the floor is `3.2.1` for the dependency maintenance
|
|
35
|
+
below, not for the new names.
|
|
36
|
+
- `tests/test_service_client.py`, plus the new names in
|
|
37
|
+
`tests/test_public_typing.py` and the `4.6` rows in `tests/test_compat.py`.
|
|
38
|
+
|
|
39
|
+
### Changed
|
|
40
|
+
|
|
41
|
+
- **`auth-sdk-m8` floor raised to `>=3.2.1,<4.0.0`** (was `>=3.2.0`) in
|
|
42
|
+
`pyproject.toml`, and `COMPAT_MATRIX["4.5"]` raised in place to match.
|
|
43
|
+
`auth-sdk-m8 3.2.1` carries pinned dependency and CI updates with no API or
|
|
44
|
+
behaviour change.
|
|
45
|
+
- **`constraints.txt` and `constraints-all.txt` re-pinned to
|
|
46
|
+
`auth-sdk-m8==3.2.1`** by a narrow regeneration under Python 3.12
|
|
47
|
+
(`pip-compile --upgrade-package auth-sdk-m8==3.2.1`, each file's recorded
|
|
48
|
+
command). No other pin moved.
|
|
49
|
+
|
|
50
|
+
### Fixed
|
|
51
|
+
|
|
52
|
+
- **README *Compatibility* table**: adds the `4.5.0` row it was missing,
|
|
53
|
+
beside the new `4.6.0` row.
|
|
54
|
+
|
|
55
|
+
## [4.5.1] — 2026-09-12 · Move the compiled `auth-sdk-m8` pin to the `4.5.0` floor
|
|
56
|
+
|
|
57
|
+
`4.5.0` raised the `pyproject.toml` floor to `auth-sdk-m8>=3.2.0,<4.0.0` but
|
|
58
|
+
left both compiled constraint files pinning `==3.1.3`, so the lockfiles
|
|
59
|
+
contradicted the declaration they were compiled from.
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
- **`constraints.txt` and `constraints-all.txt` re-pinned to
|
|
64
|
+
`auth-sdk-m8==3.2.0`.** Edited surgically rather than regenerated:
|
|
65
|
+
`auth-sdk-m8 3.2.0` has byte-identical dependency metadata to `3.1.3`, so
|
|
66
|
+
this is provably the complete diff a correct regeneration would produce for
|
|
67
|
+
the SDK change alone. A trial regeneration on this host's Python 3.14 (the
|
|
68
|
+
files were compiled under 3.12, unavailable here) moved 20+ unrelated pins
|
|
69
|
+
and rewrote the self-referential devcontainer path, so it was rejected in
|
|
70
|
+
favor of the surgical edit. A full regeneration on Python 3.12 remains
|
|
71
|
+
follow-on work.
|
|
72
|
+
|
|
8
73
|
## [4.5.0] — 2026-09-10 · Raise the `auth-sdk-m8` floor for the JWKS `kid`/key-binding fix (`J3`)
|
|
9
74
|
|
|
10
75
|
`W2A` of the workspace's `fa-auth-m8` JWKS `kid`/key-binding remediation plan
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fastapi-m8
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.6.0
|
|
4
4
|
Summary: FastAPI application framework for m8 consumer microservices.
|
|
5
5
|
Author-email: Eli Serra <e.serra173@gmail.com>
|
|
6
6
|
License: Apache License
|
|
@@ -215,7 +215,7 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
215
215
|
Classifier: Topic :: Software Development :: Libraries
|
|
216
216
|
Requires-Python: >=3.12
|
|
217
217
|
Requires-Dist: anyio>=4.0
|
|
218
|
-
Requires-Dist: auth-sdk-m8[config,events,fastapi,observability,security]<4.0.0,>=3.2.
|
|
218
|
+
Requires-Dist: auth-sdk-m8[config,events,fastapi,observability,security]<4.0.0,>=3.2.1
|
|
219
219
|
Requires-Dist: fastapi>=0.136.3
|
|
220
220
|
Requires-Dist: httpx>=0.27.0
|
|
221
221
|
Requires-Dist: packaging>=24.0
|
|
@@ -290,6 +290,7 @@ boilerplate from every consumer service.
|
|
|
290
290
|
- [Role System](#role-system)
|
|
291
291
|
- [Protecting Routes](#protecting-routes)
|
|
292
292
|
- [API-key routes (remote principal)](#api-key-routes-remote-principal)
|
|
293
|
+
- [Service-client routes](#service-client-routes)
|
|
293
294
|
8. [Health Endpoint](#health-endpoint)
|
|
294
295
|
9. [Database Integration](#database-integration)
|
|
295
296
|
10. [Pre-Start Script](#pre-start-script)
|
|
@@ -1200,6 +1201,12 @@ control, and can be broken by an SDK change `fastapi-m8` has already absorbed.
|
|
|
1200
1201
|
| `render_metrics` | `auth_sdk_m8.observability.metrics` | Render the Prometheus exposition payload |
|
|
1201
1202
|
| `REGISTRY` | `auth_sdk_m8.observability.metrics` | The shared Prometheus collector registry `render_metrics` renders |
|
|
1202
1203
|
| `make_scrape_credential_guard` | `auth_sdk_m8.security.guards` | Build the `/metrics` scrape-credential guard |
|
|
1204
|
+
| `ConsumerCredential` | `auth_sdk_m8.security.consumer_auth` | One service client's salted secret digest and granted scopes |
|
|
1205
|
+
| `ConsumerCredentialRegistry` | `auth_sdk_m8.security.consumer_auth` | Client id → credential lookup with constant-time verification |
|
|
1206
|
+
| `ConsumerAuthenticationError` | `auth_sdk_m8.security.consumer_auth` | Unknown client id or wrong secret (indistinguishable) |
|
|
1207
|
+
| `ConsumerScopeError` | `auth_sdk_m8.security.consumer_auth` | Authenticated client lacking a required scope |
|
|
1208
|
+
| `INTERNAL_CLIENT_HEADER` | `auth_sdk_m8.security.consumer_auth` | `X-Internal-Client`, the calling service's id |
|
|
1209
|
+
| `INTERNAL_TOKEN_HEADER` | `auth_sdk_m8.security.consumer_auth` | `X-Internal-Token`, the calling service's secret |
|
|
1203
1210
|
|
|
1204
1211
|
```python
|
|
1205
1212
|
from fastapi_m8 import (
|
|
@@ -1396,6 +1403,45 @@ Responses:
|
|
|
1396
1403
|
circuit, shed load, or a response this release cannot interpret). **Always a
|
|
1397
1404
|
denial** — there is no fallback to a cached principal or to bare key validity
|
|
1398
1405
|
|
|
1406
|
+
### Service-client routes
|
|
1407
|
+
|
|
1408
|
+
A route that other services call — not users — authenticates the caller with
|
|
1409
|
+
the fleet's per-consumer credential pair: `X-Internal-Client` names the calling
|
|
1410
|
+
service and `X-Internal-Token` carries its secret. Build the registry from the
|
|
1411
|
+
stored `sha256$<salt>$<digest>` strings (never plaintext secrets) and guard the
|
|
1412
|
+
route with `build_service_client_guard()`:
|
|
1413
|
+
|
|
1414
|
+
```python
|
|
1415
|
+
from fastapi import Depends
|
|
1416
|
+
from fastapi_m8 import ConsumerCredentialRegistry, build_service_client_guard
|
|
1417
|
+
|
|
1418
|
+
registry = ConsumerCredentialRegistry.from_encoded(
|
|
1419
|
+
{"media-worker": (encoded_secret, ("execute:ocr",))}
|
|
1420
|
+
)
|
|
1421
|
+
require_ocr_client = build_service_client_guard(
|
|
1422
|
+
registry, required_scopes=("execute:ocr",)
|
|
1423
|
+
)
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
@router.post("/execute/ocr")
|
|
1427
|
+
async def execute_ocr(client_id: str = Depends(require_ocr_client)): ...
|
|
1428
|
+
```
|
|
1429
|
+
|
|
1430
|
+
The dependency returns the authenticated client id. The client must hold
|
|
1431
|
+
**every** scope in `required_scopes`; the set may not be empty, and a bare
|
|
1432
|
+
string is refused because it would be read as one scope per character.
|
|
1433
|
+
|
|
1434
|
+
Responses:
|
|
1435
|
+
|
|
1436
|
+
- `401 Unauthorized` — a missing header, an unknown client id or a wrong
|
|
1437
|
+
secret. All three return the same body, and the registry runs its
|
|
1438
|
+
constant-time digest comparison in each (against a throwaway credential for
|
|
1439
|
+
an unknown id), so a caller cannot tell which client ids exist
|
|
1440
|
+
- `403 Forbidden` — the client authenticated but lacks a required scope
|
|
1441
|
+
|
|
1442
|
+
The guard never logs or echoes the token. It does not accept a JWT: a route
|
|
1443
|
+
that also needs a user must add a JWT dependency of its own.
|
|
1444
|
+
|
|
1399
1445
|
---
|
|
1400
1446
|
|
|
1401
1447
|
## Health Endpoint
|
|
@@ -1700,6 +1746,8 @@ async def test_health(client):
|
|
|
1700
1746
|
|
|
1701
1747
|
| `fastapi-m8` | `auth-sdk-m8` | Python |
|
|
1702
1748
|
|---|---|---|
|
|
1749
|
+
| `4.6.0` | `>=3.2.1, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1750
|
+
| `4.5.0` | `>=3.2.0, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1703
1751
|
| `4.4.0` | `>=3.1.3, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1704
1752
|
| `4.3.0` | `>=3.1.2, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1705
1753
|
| `4.2.0` | `>=3.1.0, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
@@ -33,6 +33,7 @@ boilerplate from every consumer service.
|
|
|
33
33
|
- [Role System](#role-system)
|
|
34
34
|
- [Protecting Routes](#protecting-routes)
|
|
35
35
|
- [API-key routes (remote principal)](#api-key-routes-remote-principal)
|
|
36
|
+
- [Service-client routes](#service-client-routes)
|
|
36
37
|
8. [Health Endpoint](#health-endpoint)
|
|
37
38
|
9. [Database Integration](#database-integration)
|
|
38
39
|
10. [Pre-Start Script](#pre-start-script)
|
|
@@ -943,6 +944,12 @@ control, and can be broken by an SDK change `fastapi-m8` has already absorbed.
|
|
|
943
944
|
| `render_metrics` | `auth_sdk_m8.observability.metrics` | Render the Prometheus exposition payload |
|
|
944
945
|
| `REGISTRY` | `auth_sdk_m8.observability.metrics` | The shared Prometheus collector registry `render_metrics` renders |
|
|
945
946
|
| `make_scrape_credential_guard` | `auth_sdk_m8.security.guards` | Build the `/metrics` scrape-credential guard |
|
|
947
|
+
| `ConsumerCredential` | `auth_sdk_m8.security.consumer_auth` | One service client's salted secret digest and granted scopes |
|
|
948
|
+
| `ConsumerCredentialRegistry` | `auth_sdk_m8.security.consumer_auth` | Client id → credential lookup with constant-time verification |
|
|
949
|
+
| `ConsumerAuthenticationError` | `auth_sdk_m8.security.consumer_auth` | Unknown client id or wrong secret (indistinguishable) |
|
|
950
|
+
| `ConsumerScopeError` | `auth_sdk_m8.security.consumer_auth` | Authenticated client lacking a required scope |
|
|
951
|
+
| `INTERNAL_CLIENT_HEADER` | `auth_sdk_m8.security.consumer_auth` | `X-Internal-Client`, the calling service's id |
|
|
952
|
+
| `INTERNAL_TOKEN_HEADER` | `auth_sdk_m8.security.consumer_auth` | `X-Internal-Token`, the calling service's secret |
|
|
946
953
|
|
|
947
954
|
```python
|
|
948
955
|
from fastapi_m8 import (
|
|
@@ -1139,6 +1146,45 @@ Responses:
|
|
|
1139
1146
|
circuit, shed load, or a response this release cannot interpret). **Always a
|
|
1140
1147
|
denial** — there is no fallback to a cached principal or to bare key validity
|
|
1141
1148
|
|
|
1149
|
+
### Service-client routes
|
|
1150
|
+
|
|
1151
|
+
A route that other services call — not users — authenticates the caller with
|
|
1152
|
+
the fleet's per-consumer credential pair: `X-Internal-Client` names the calling
|
|
1153
|
+
service and `X-Internal-Token` carries its secret. Build the registry from the
|
|
1154
|
+
stored `sha256$<salt>$<digest>` strings (never plaintext secrets) and guard the
|
|
1155
|
+
route with `build_service_client_guard()`:
|
|
1156
|
+
|
|
1157
|
+
```python
|
|
1158
|
+
from fastapi import Depends
|
|
1159
|
+
from fastapi_m8 import ConsumerCredentialRegistry, build_service_client_guard
|
|
1160
|
+
|
|
1161
|
+
registry = ConsumerCredentialRegistry.from_encoded(
|
|
1162
|
+
{"media-worker": (encoded_secret, ("execute:ocr",))}
|
|
1163
|
+
)
|
|
1164
|
+
require_ocr_client = build_service_client_guard(
|
|
1165
|
+
registry, required_scopes=("execute:ocr",)
|
|
1166
|
+
)
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
@router.post("/execute/ocr")
|
|
1170
|
+
async def execute_ocr(client_id: str = Depends(require_ocr_client)): ...
|
|
1171
|
+
```
|
|
1172
|
+
|
|
1173
|
+
The dependency returns the authenticated client id. The client must hold
|
|
1174
|
+
**every** scope in `required_scopes`; the set may not be empty, and a bare
|
|
1175
|
+
string is refused because it would be read as one scope per character.
|
|
1176
|
+
|
|
1177
|
+
Responses:
|
|
1178
|
+
|
|
1179
|
+
- `401 Unauthorized` — a missing header, an unknown client id or a wrong
|
|
1180
|
+
secret. All three return the same body, and the registry runs its
|
|
1181
|
+
constant-time digest comparison in each (against a throwaway credential for
|
|
1182
|
+
an unknown id), so a caller cannot tell which client ids exist
|
|
1183
|
+
- `403 Forbidden` — the client authenticated but lacks a required scope
|
|
1184
|
+
|
|
1185
|
+
The guard never logs or echoes the token. It does not accept a JWT: a route
|
|
1186
|
+
that also needs a user must add a JWT dependency of its own.
|
|
1187
|
+
|
|
1142
1188
|
---
|
|
1143
1189
|
|
|
1144
1190
|
## Health Endpoint
|
|
@@ -1443,6 +1489,8 @@ async def test_health(client):
|
|
|
1443
1489
|
|
|
1444
1490
|
| `fastapi-m8` | `auth-sdk-m8` | Python |
|
|
1445
1491
|
|---|---|---|
|
|
1492
|
+
| `4.6.0` | `>=3.2.1, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1493
|
+
| `4.5.0` | `>=3.2.0, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1446
1494
|
| `4.4.0` | `>=3.1.3, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1447
1495
|
| `4.3.0` | `>=3.1.2, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
1448
1496
|
| `4.2.0` | `>=3.1.0, <4.0.0` | 3.12, 3.13, 3.14 |
|
|
@@ -16,7 +16,7 @@ anyio==4.14.1
|
|
|
16
16
|
# fastapi-m8 (pyproject.toml)
|
|
17
17
|
# httpx
|
|
18
18
|
# starlette
|
|
19
|
-
auth-sdk-m8[config,events,fastapi,observability,security]==3.1
|
|
19
|
+
auth-sdk-m8[config,events,fastapi,observability,security]==3.2.1
|
|
20
20
|
# via
|
|
21
21
|
# fastapi-m8
|
|
22
22
|
# fastapi-m8 (pyproject.toml)
|
|
@@ -15,6 +15,13 @@ Tier 1 — remote API-key principal (issuer introspection)::
|
|
|
15
15
|
from fastapi_m8 import ApiKeyIntrospectionError, ApiKeyQuotaExceededError
|
|
16
16
|
from fastapi_m8 import audit_api_key_routes, BareApiKeyDependency
|
|
17
17
|
|
|
18
|
+
Tier 1 — service-client routes (other services calling this consumer)::
|
|
19
|
+
|
|
20
|
+
from fastapi_m8 import build_service_client_guard
|
|
21
|
+
from fastapi_m8 import ConsumerCredential, ConsumerCredentialRegistry
|
|
22
|
+
from fastapi_m8 import ConsumerAuthenticationError, ConsumerScopeError
|
|
23
|
+
from fastapi_m8 import INTERNAL_CLIENT_HEADER, INTERNAL_TOKEN_HEADER
|
|
24
|
+
|
|
18
25
|
Tier 1 — auth event stream (fa-auth SSE bridge)::
|
|
19
26
|
|
|
20
27
|
from fastapi_m8 import build_event_stream_client
|
|
@@ -57,6 +64,14 @@ from auth_sdk_m8.observability.metrics import render as render_metrics
|
|
|
57
64
|
from auth_sdk_m8.schemas.base import ResponseMessage, ResponseModelBase, RoleType
|
|
58
65
|
from auth_sdk_m8.schemas.shared import ValidationConstants
|
|
59
66
|
from auth_sdk_m8.schemas.user import UserModel
|
|
67
|
+
from auth_sdk_m8.security.consumer_auth import (
|
|
68
|
+
INTERNAL_CLIENT_HEADER,
|
|
69
|
+
INTERNAL_TOKEN_HEADER,
|
|
70
|
+
ConsumerAuthenticationError,
|
|
71
|
+
ConsumerCredential,
|
|
72
|
+
ConsumerCredentialRegistry,
|
|
73
|
+
ConsumerScopeError,
|
|
74
|
+
)
|
|
60
75
|
from auth_sdk_m8.security.guards import make_scrape_credential_guard
|
|
61
76
|
from auth_sdk_m8.utils.paths import find_dotenv
|
|
62
77
|
|
|
@@ -99,6 +114,9 @@ from fastapi_m8._internal_auth import (
|
|
|
99
114
|
|
|
100
115
|
# Tier 1 — API-key route-wiring audit (§3.3.1)
|
|
101
116
|
from fastapi_m8._route_audit import BareApiKeyDependency, audit_api_key_routes
|
|
117
|
+
|
|
118
|
+
# Tier 1 — service-client routes
|
|
119
|
+
from fastapi_m8._service_client import build_service_client_guard
|
|
102
120
|
from fastapi_m8._version import __version__
|
|
103
121
|
from fastapi_m8.config import ConsumerServiceSettings
|
|
104
122
|
|
|
@@ -187,6 +205,14 @@ __all__ = [
|
|
|
187
205
|
"InternalAuthProvider",
|
|
188
206
|
"ServiceTokenInternalAuth",
|
|
189
207
|
"derive_service_token_url",
|
|
208
|
+
# Tier 1 — service-client routes
|
|
209
|
+
"build_service_client_guard",
|
|
210
|
+
"ConsumerCredential",
|
|
211
|
+
"ConsumerCredentialRegistry",
|
|
212
|
+
"ConsumerAuthenticationError",
|
|
213
|
+
"ConsumerScopeError",
|
|
214
|
+
"INTERNAL_CLIENT_HEADER",
|
|
215
|
+
"INTERNAL_TOKEN_HEADER",
|
|
190
216
|
# Tier 1 — auth event stream
|
|
191
217
|
"build_event_stream_client",
|
|
192
218
|
"AuthEventStreamClient",
|
|
@@ -120,8 +120,8 @@ COMPAT_MATRIX: dict[str, dict[str, str]] = {
|
|
|
120
120
|
# every consumer. Floor raised in place to >=3.1.3 (was >=3.1.2 when this
|
|
121
121
|
# row was first added) before publish, once auth-sdk-m8 3.1.3 shipped —
|
|
122
122
|
# dependency maintenance only, no new SDK API, no source change on either
|
|
123
|
-
# side,
|
|
124
|
-
#
|
|
123
|
+
# side, rode the same 4.4.0 rather than a separate bump; 4.4.0 is now
|
|
124
|
+
# tagged, merged and published. See CHANGELOG.
|
|
125
125
|
"4.4": {"auth-sdk-m8": ">=3.1.3,<4.0.0"},
|
|
126
126
|
# 4.5 (MINOR) raises the floor to auth-sdk-m8 3.2.0, which carries W2.1's
|
|
127
127
|
# same-`kid` key-change recovery (a cached key rotates without a `kid`
|
|
@@ -132,7 +132,18 @@ COMPAT_MATRIX: dict[str, dict[str, str]] = {
|
|
|
132
132
|
# constraint). This row is what makes the fix mandatory rather than
|
|
133
133
|
# optional: a consumer that resolves an older SDK is rejected at boot by
|
|
134
134
|
# _assert_compat instead of silently running the unfixed resolver.
|
|
135
|
-
|
|
135
|
+
# Floor raised in place to >=3.2.1 with the pyproject floor (auth-sdk-m8
|
|
136
|
+
# 3.2.1: dependency maintenance only, no new SDK API, no source change on
|
|
137
|
+
# either side). See CHANGELOG 4.6.0.
|
|
138
|
+
"4.5": {"auth-sdk-m8": ">=3.2.1,<4.0.0"},
|
|
139
|
+
# 4.6 (MINOR) adds build_service_client_guard and re-exports the SDK's
|
|
140
|
+
# per-consumer credential primitives (ConsumerCredential,
|
|
141
|
+
# ConsumerCredentialRegistry, ConsumerAuthenticationError,
|
|
142
|
+
# ConsumerScopeError, INTERNAL_CLIENT_HEADER, INTERNAL_TOKEN_HEADER), so a
|
|
143
|
+
# consumer that serves other services authenticates them without importing
|
|
144
|
+
# auth_sdk_m8. Every re-exported name already ships in auth-sdk-m8 3.2.0;
|
|
145
|
+
# the floor is the pyproject floor, 3.2.1. See CHANGELOG.
|
|
146
|
+
"4.6": {"auth-sdk-m8": ">=3.2.1,<4.0.0"},
|
|
136
147
|
}
|
|
137
148
|
|
|
138
149
|
_EXTRAS = "[config,security,fastapi,observability]"
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Service-client guard for consumer routes that other services call.
|
|
3
|
+
|
|
4
|
+
A consumer that exposes routes to other services (not to users) authenticates
|
|
5
|
+
each caller by the per-consumer credential pair the fleet already uses for
|
|
6
|
+
private calls to the issuer: ``X-Internal-Client`` names the calling service
|
|
7
|
+
and ``X-Internal-Token`` carries its secret. The verification primitives live
|
|
8
|
+
in auth-sdk-m8 (``ConsumerCredentialRegistry``) and are re-exported from
|
|
9
|
+
``fastapi_m8`` so a consumer never imports ``auth_sdk_m8`` directly.
|
|
10
|
+
|
|
11
|
+
:func:`build_service_client_guard` turns a registry and a required scope set
|
|
12
|
+
into a FastAPI dependency:
|
|
13
|
+
|
|
14
|
+
* a missing header, an unknown client id or a wrong secret → ``401`` with one
|
|
15
|
+
fixed body. The registry runs its constant-time comparison in every one of
|
|
16
|
+
these cases (against a throwaway credential when the id is unknown), so the
|
|
17
|
+
guard adds no client-enumeration oracle;
|
|
18
|
+
* an authenticated client that lacks any required scope → ``403``;
|
|
19
|
+
* success → the authenticated client id (a ``str``).
|
|
20
|
+
|
|
21
|
+
The token is never logged, echoed or attached to an exception.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from collections.abc import Callable, Iterable
|
|
27
|
+
|
|
28
|
+
from auth_sdk_m8.security.consumer_auth import (
|
|
29
|
+
INTERNAL_CLIENT_HEADER,
|
|
30
|
+
INTERNAL_TOKEN_HEADER,
|
|
31
|
+
ConsumerCredentialRegistry,
|
|
32
|
+
)
|
|
33
|
+
from fastapi import HTTPException, Request, status
|
|
34
|
+
|
|
35
|
+
_UNAUTHORIZED_DETAIL = "Unauthorized"
|
|
36
|
+
_FORBIDDEN_DETAIL = "Forbidden"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _normalise_required_scopes(required_scopes: Iterable[str]) -> frozenset[str]:
|
|
40
|
+
"""Return the required scopes as a non-empty ``frozenset`` of strings."""
|
|
41
|
+
if isinstance(required_scopes, str):
|
|
42
|
+
# frozenset("execute:ocr") would require each character as a scope.
|
|
43
|
+
raise TypeError(
|
|
44
|
+
"required_scopes must be an iterable of scope strings, not a str"
|
|
45
|
+
)
|
|
46
|
+
scopes = frozenset(required_scopes)
|
|
47
|
+
if not scopes:
|
|
48
|
+
raise ValueError(
|
|
49
|
+
"required_scopes must name at least one scope; a service-client "
|
|
50
|
+
"guard is deny-by-default"
|
|
51
|
+
)
|
|
52
|
+
for scope in scopes:
|
|
53
|
+
if not isinstance(scope, str) or not scope:
|
|
54
|
+
raise ValueError("every required scope must be a non-empty string")
|
|
55
|
+
return scopes
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def build_service_client_guard(
|
|
59
|
+
registry: ConsumerCredentialRegistry,
|
|
60
|
+
*,
|
|
61
|
+
required_scopes: Iterable[str],
|
|
62
|
+
) -> Callable[[Request], str]:
|
|
63
|
+
"""
|
|
64
|
+
Build a FastAPI dependency that authenticates a service client.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
registry : ConsumerCredentialRegistry
|
|
69
|
+
The hashed, scoped client registry to verify against.
|
|
70
|
+
required_scopes : Iterable[str]
|
|
71
|
+
Every scope the route needs. The client must hold all of them. At
|
|
72
|
+
least one is required, and a bare ``str`` is refused.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
Callable[[Request], str]
|
|
77
|
+
A dependency returning the authenticated client id. It raises
|
|
78
|
+
``HTTPException`` 401 for a missing or unknown client or a wrong
|
|
79
|
+
secret, and 403 for an authenticated client lacking a scope.
|
|
80
|
+
|
|
81
|
+
Raises
|
|
82
|
+
------
|
|
83
|
+
TypeError
|
|
84
|
+
If ``required_scopes`` is a ``str``.
|
|
85
|
+
ValueError
|
|
86
|
+
If ``required_scopes`` is empty or holds an empty or non-string scope.
|
|
87
|
+
|
|
88
|
+
"""
|
|
89
|
+
scopes = _normalise_required_scopes(required_scopes)
|
|
90
|
+
|
|
91
|
+
def _service_client(request: Request) -> str:
|
|
92
|
+
client_id = request.headers.get(INTERNAL_CLIENT_HEADER)
|
|
93
|
+
secret = request.headers.get(INTERNAL_TOKEN_HEADER)
|
|
94
|
+
# verify() compares in constant time in every branch and reports an
|
|
95
|
+
# unknown id and a wrong secret identically (None).
|
|
96
|
+
credential = registry.verify(client_id, secret)
|
|
97
|
+
if credential is None:
|
|
98
|
+
raise HTTPException(
|
|
99
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
100
|
+
detail=_UNAUTHORIZED_DETAIL,
|
|
101
|
+
)
|
|
102
|
+
if not all(credential.has_scope(scope) for scope in scopes):
|
|
103
|
+
raise HTTPException(
|
|
104
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
105
|
+
detail=_FORBIDDEN_DETAIL,
|
|
106
|
+
)
|
|
107
|
+
return credential.client_id
|
|
108
|
+
|
|
109
|
+
return _service_client
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "fastapi-m8"
|
|
7
|
-
version = "4.
|
|
7
|
+
version = "4.6.0"
|
|
8
8
|
description = "FastAPI application framework for m8 consumer microservices."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
@@ -26,7 +26,7 @@ dependencies = [
|
|
|
26
26
|
"httpx>=0.27.0",
|
|
27
27
|
"packaging>=24.0",
|
|
28
28
|
"anyio>=4.0",
|
|
29
|
-
"auth-sdk-m8[config,security,fastapi,observability,events]>=3.2.
|
|
29
|
+
"auth-sdk-m8[config,security,fastapi,observability,events]>=3.2.1,<4.0.0",
|
|
30
30
|
]
|
|
31
31
|
|
|
32
32
|
[project.optional-dependencies]
|
|
@@ -123,29 +123,58 @@ def test_assert_compat_40_row_rejects_old_major() -> None:
|
|
|
123
123
|
|
|
124
124
|
|
|
125
125
|
def test_compat_matrix_45_row_matches_pyproject_floor() -> None:
|
|
126
|
-
"""The 4.5 gate is exactly the auth-sdk-m8 3.2.
|
|
127
|
-
assert COMPAT_MATRIX["4.5"] == {"auth-sdk-m8": ">=3.2.
|
|
126
|
+
"""The 4.5 gate is exactly the pyproject auth-sdk-m8 floor (3.2.1)."""
|
|
127
|
+
assert COMPAT_MATRIX["4.5"] == {"auth-sdk-m8": ">=3.2.1,<4.0.0"}
|
|
128
128
|
|
|
129
129
|
|
|
130
130
|
def test_assert_compat_45_row_accepts_intended_sdk_major() -> None:
|
|
131
|
-
"""The 4.5 gate accepts the released SDK version it targets (3.2.
|
|
131
|
+
"""The 4.5 gate accepts the released SDK version it targets (3.2.1)."""
|
|
132
132
|
_reset_compat()
|
|
133
133
|
with (
|
|
134
134
|
patch("fastapi_m8._compat.__version__", "4.5.0"),
|
|
135
|
-
patch("fastapi_m8._compat.md.version", return_value="3.2.
|
|
135
|
+
patch("fastapi_m8._compat.md.version", return_value="3.2.1"),
|
|
136
136
|
):
|
|
137
|
-
_assert_compat() # should not raise: 3.2.
|
|
137
|
+
_assert_compat() # should not raise: 3.2.1 is in >=3.2.1,<4.0.0
|
|
138
138
|
assert _COMPAT_STATE["checked"] is True
|
|
139
|
-
assert _COMPAT_STATE["auth_version"] == "3.2.
|
|
139
|
+
assert _COMPAT_STATE["auth_version"] == "3.2.1"
|
|
140
140
|
_reset_compat()
|
|
141
141
|
|
|
142
142
|
|
|
143
143
|
def test_assert_compat_45_row_rejects_old_major() -> None:
|
|
144
|
-
"""The 4.5 gate rejects
|
|
144
|
+
"""The 4.5 gate rejects an SDK below its 3.2.1 floor."""
|
|
145
145
|
_reset_compat()
|
|
146
146
|
with (
|
|
147
147
|
patch("fastapi_m8._compat.__version__", "4.5.0"),
|
|
148
|
-
patch("fastapi_m8._compat.md.version", return_value="3.
|
|
148
|
+
patch("fastapi_m8._compat.md.version", return_value="3.2.0"),
|
|
149
|
+
):
|
|
150
|
+
with pytest.raises(RuntimeError, match="requires auth-sdk-m8"):
|
|
151
|
+
_assert_compat()
|
|
152
|
+
_reset_compat()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_compat_matrix_46_row_keeps_the_45_floor() -> None:
|
|
156
|
+
"""The 4.6 gate is exactly the pyproject auth-sdk-m8 floor (3.2.1)."""
|
|
157
|
+
assert COMPAT_MATRIX["4.6"] == {"auth-sdk-m8": ">=3.2.1,<4.0.0"}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_assert_compat_46_row_accepts_the_published_sdk() -> None:
|
|
161
|
+
"""The 4.6 gate accepts auth-sdk-m8 3.2.1, its floor."""
|
|
162
|
+
_reset_compat()
|
|
163
|
+
with (
|
|
164
|
+
patch("fastapi_m8._compat.__version__", "4.6.0"),
|
|
165
|
+
patch("fastapi_m8._compat.md.version", return_value="3.2.1"),
|
|
166
|
+
):
|
|
167
|
+
_assert_compat()
|
|
168
|
+
assert _COMPAT_STATE["auth_version"] == "3.2.1"
|
|
169
|
+
_reset_compat()
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_assert_compat_46_row_rejects_old_sdk() -> None:
|
|
173
|
+
"""The 4.6 gate rejects an SDK below the 3.2.1 floor."""
|
|
174
|
+
_reset_compat()
|
|
175
|
+
with (
|
|
176
|
+
patch("fastapi_m8._compat.__version__", "4.6.0"),
|
|
177
|
+
patch("fastapi_m8._compat.md.version", return_value="3.2.0"),
|
|
149
178
|
):
|
|
150
179
|
with pytest.raises(RuntimeError, match="requires auth-sdk-m8"):
|
|
151
180
|
_assert_compat()
|
|
@@ -44,6 +44,10 @@ from fastapi_m8 import (
|
|
|
44
44
|
BareApiKeyDependency,
|
|
45
45
|
BaseController,
|
|
46
46
|
COMPAT_MATRIX,
|
|
47
|
+
ConsumerAuthenticationError,
|
|
48
|
+
ConsumerCredential,
|
|
49
|
+
ConsumerCredentialRegistry,
|
|
50
|
+
ConsumerScopeError,
|
|
47
51
|
ConsumerServiceSettings,
|
|
48
52
|
DbEngine,
|
|
49
53
|
HealthAggregatePolicy,
|
|
@@ -51,6 +55,8 @@ from fastapi_m8 import (
|
|
|
51
55
|
HealthCheckResult,
|
|
52
56
|
HealthConfig,
|
|
53
57
|
HealthStatus,
|
|
58
|
+
INTERNAL_CLIENT_HEADER,
|
|
59
|
+
INTERNAL_TOKEN_HEADER,
|
|
54
60
|
InternalAuthProvider,
|
|
55
61
|
ResponseMessage,
|
|
56
62
|
ResponseModelBase,
|
|
@@ -64,6 +70,7 @@ from fastapi_m8 import (
|
|
|
64
70
|
build_auth_deps,
|
|
65
71
|
build_event_stream_client,
|
|
66
72
|
build_internal_auth,
|
|
73
|
+
build_service_client_guard,
|
|
67
74
|
capabilities,
|
|
68
75
|
create_app,
|
|
69
76
|
create_async_app,
|
|
@@ -149,6 +156,25 @@ def use_metrics_surface(app: FastAPI, credential: str | None) -> None:
|
|
|
149
156
|
list(REGISTRY.collect())
|
|
150
157
|
|
|
151
158
|
|
|
159
|
+
def use_service_client_guard(app: FastAPI, encoded: str) -> None:
|
|
160
|
+
"""The service-client guard and its re-exported registry stay typed."""
|
|
161
|
+
credential: ConsumerCredential = ConsumerCredential.from_encoded(
|
|
162
|
+
"media-worker", encoded, ("execute:ocr",)
|
|
163
|
+
)
|
|
164
|
+
registry = ConsumerCredentialRegistry([credential])
|
|
165
|
+
guard = build_service_client_guard(registry, required_scopes=("execute:ocr",))
|
|
166
|
+
|
|
167
|
+
@app.post("/execute")
|
|
168
|
+
def route(client_id: str = Depends(guard)) -> dict[str, str]:
|
|
169
|
+
return {"client": client_id, "header": INTERNAL_CLIENT_HEADER}
|
|
170
|
+
|
|
171
|
+
errors: tuple[type[Exception], ...] = (
|
|
172
|
+
ConsumerAuthenticationError,
|
|
173
|
+
ConsumerScopeError,
|
|
174
|
+
)
|
|
175
|
+
assert INTERNAL_TOKEN_HEADER and errors
|
|
176
|
+
|
|
177
|
+
|
|
152
178
|
def use_schema_and_model_primitives() -> tuple[str, bool]:
|
|
153
179
|
"""The schema/controller/model/constant primitives are real types, not Any."""
|
|
154
180
|
dotenv: Path = find_dotenv()
|
|
@@ -201,6 +227,12 @@ _SDK_REEXPORTS: dict[str, str] = {
|
|
|
201
227
|
"find_dotenv": "auth_sdk_m8.utils.paths",
|
|
202
228
|
"REGISTRY": "auth_sdk_m8.observability.metrics",
|
|
203
229
|
"make_scrape_credential_guard": "auth_sdk_m8.security.guards",
|
|
230
|
+
"ConsumerCredential": "auth_sdk_m8.security.consumer_auth",
|
|
231
|
+
"ConsumerCredentialRegistry": "auth_sdk_m8.security.consumer_auth",
|
|
232
|
+
"ConsumerAuthenticationError": "auth_sdk_m8.security.consumer_auth",
|
|
233
|
+
"ConsumerScopeError": "auth_sdk_m8.security.consumer_auth",
|
|
234
|
+
"INTERNAL_CLIENT_HEADER": "auth_sdk_m8.security.consumer_auth",
|
|
235
|
+
"INTERNAL_TOKEN_HEADER": "auth_sdk_m8.security.consumer_auth",
|
|
204
236
|
}
|
|
205
237
|
|
|
206
238
|
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""Tests for fastapi_m8._service_client — the service-client route guard."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from fastapi import Depends, FastAPI
|
|
9
|
+
from fastapi.testclient import TestClient
|
|
10
|
+
|
|
11
|
+
import fastapi_m8
|
|
12
|
+
from fastapi_m8 import (
|
|
13
|
+
INTERNAL_CLIENT_HEADER,
|
|
14
|
+
INTERNAL_TOKEN_HEADER,
|
|
15
|
+
ConsumerCredential,
|
|
16
|
+
ConsumerCredentialRegistry,
|
|
17
|
+
build_service_client_guard,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
_SECRET = "s3cret-canary-" + "a" * 50 # noqa: S105 - test fixture
|
|
21
|
+
_WRONG = "wrong-canary-" + "b" * 50
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _registry() -> ConsumerCredentialRegistry:
|
|
25
|
+
return ConsumerCredentialRegistry(
|
|
26
|
+
[
|
|
27
|
+
ConsumerCredential.create(
|
|
28
|
+
"media-worker", _SECRET, ("execute:ocr", "job-context:issue")
|
|
29
|
+
),
|
|
30
|
+
ConsumerCredential.create("media-service", _SECRET, ("execute:embedding",)),
|
|
31
|
+
]
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _client(*scopes: str) -> TestClient:
|
|
36
|
+
app = FastAPI()
|
|
37
|
+
guard = build_service_client_guard(_registry(), required_scopes=scopes)
|
|
38
|
+
|
|
39
|
+
@app.post("/execute")
|
|
40
|
+
def route(client_id: str = Depends(guard)) -> dict[str, str]:
|
|
41
|
+
return {"client_id": client_id}
|
|
42
|
+
|
|
43
|
+
return TestClient(app)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _headers(client: str | None, token: str | None) -> dict[str, str]:
|
|
47
|
+
headers: dict[str, str] = {}
|
|
48
|
+
if client is not None:
|
|
49
|
+
headers[INTERNAL_CLIENT_HEADER] = client
|
|
50
|
+
if token is not None:
|
|
51
|
+
headers[INTERNAL_TOKEN_HEADER] = token
|
|
52
|
+
return headers
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ── outcomes ──────────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_success_returns_the_authenticated_client_id() -> None:
|
|
59
|
+
response = _client("execute:ocr").post(
|
|
60
|
+
"/execute", headers=_headers("media-worker", _SECRET)
|
|
61
|
+
)
|
|
62
|
+
assert response.status_code == 200
|
|
63
|
+
assert response.json() == {"client_id": "media-worker"}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.mark.parametrize(
|
|
67
|
+
("client", "token"),
|
|
68
|
+
[
|
|
69
|
+
(None, None),
|
|
70
|
+
("media-worker", None),
|
|
71
|
+
(None, _SECRET),
|
|
72
|
+
("media-worker", ""),
|
|
73
|
+
("", _SECRET),
|
|
74
|
+
],
|
|
75
|
+
ids=["no-headers", "no-token", "no-client", "empty-token", "empty-client"],
|
|
76
|
+
)
|
|
77
|
+
def test_missing_credentials_are_401(client: str | None, token: str | None) -> None:
|
|
78
|
+
response = _client("execute:ocr").post("/execute", headers=_headers(client, token))
|
|
79
|
+
assert response.status_code == 401
|
|
80
|
+
assert response.json() == {"detail": "Unauthorized"}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_unknown_client_is_401() -> None:
|
|
84
|
+
response = _client("execute:ocr").post(
|
|
85
|
+
"/execute", headers=_headers("intruder", _SECRET)
|
|
86
|
+
)
|
|
87
|
+
assert response.status_code == 401
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_wrong_secret_is_401() -> None:
|
|
91
|
+
response = _client("execute:ocr").post(
|
|
92
|
+
"/execute", headers=_headers("media-worker", _WRONG)
|
|
93
|
+
)
|
|
94
|
+
assert response.status_code == 401
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_unknown_client_and_wrong_secret_are_indistinguishable() -> None:
|
|
98
|
+
client = _client("execute:ocr")
|
|
99
|
+
unknown = client.post("/execute", headers=_headers("intruder", _WRONG))
|
|
100
|
+
wrong = client.post("/execute", headers=_headers("media-worker", _WRONG))
|
|
101
|
+
assert unknown.status_code == wrong.status_code == 401
|
|
102
|
+
assert unknown.content == wrong.content
|
|
103
|
+
assert dict(unknown.headers) == dict(wrong.headers)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_authenticated_client_without_the_scope_is_403() -> None:
|
|
107
|
+
response = _client("execute:ocr").post(
|
|
108
|
+
"/execute", headers=_headers("media-service", _SECRET)
|
|
109
|
+
)
|
|
110
|
+
assert response.status_code == 403
|
|
111
|
+
assert response.json() == {"detail": "Forbidden"}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_every_required_scope_must_be_held() -> None:
|
|
115
|
+
both = _client("execute:ocr", "job-context:issue")
|
|
116
|
+
assert (
|
|
117
|
+
both.post("/execute", headers=_headers("media-worker", _SECRET)).status_code
|
|
118
|
+
== 200
|
|
119
|
+
)
|
|
120
|
+
one_missing = _client("execute:ocr", "execute:rerank")
|
|
121
|
+
assert (
|
|
122
|
+
one_missing.post(
|
|
123
|
+
"/execute", headers=_headers("media-worker", _SECRET)
|
|
124
|
+
).status_code
|
|
125
|
+
== 403
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_wrong_secret_with_a_missing_scope_is_still_401() -> None:
|
|
130
|
+
"""Authentication is decided before authorization: no scope oracle."""
|
|
131
|
+
response = _client("execute:rerank").post(
|
|
132
|
+
"/execute", headers=_headers("media-service", _WRONG)
|
|
133
|
+
)
|
|
134
|
+
assert response.status_code == 401
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# ── constant-time comparison in both failure branches ────────────────────────
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_unknown_client_and_wrong_secret_both_run_a_digest_comparison(
|
|
141
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
142
|
+
) -> None:
|
|
143
|
+
"""The registry's dummy credential answers an unknown id, so both run one."""
|
|
144
|
+
calls: list[str] = []
|
|
145
|
+
original = ConsumerCredential.verify_secret
|
|
146
|
+
|
|
147
|
+
def spy(self: ConsumerCredential, provided: str | None) -> bool:
|
|
148
|
+
calls.append(self.client_id)
|
|
149
|
+
return original(self, provided)
|
|
150
|
+
|
|
151
|
+
monkeypatch.setattr(ConsumerCredential, "verify_secret", spy)
|
|
152
|
+
client = _client("execute:ocr")
|
|
153
|
+
|
|
154
|
+
client.post("/execute", headers=_headers("intruder", _WRONG))
|
|
155
|
+
assert calls == ["\x00dummy"]
|
|
156
|
+
|
|
157
|
+
calls.clear()
|
|
158
|
+
client.post("/execute", headers=_headers("media-worker", _WRONG))
|
|
159
|
+
assert calls == ["media-worker"]
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
# ── the token never leaks ─────────────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_the_token_is_never_logged_or_echoed(caplog: pytest.LogCaptureFixture) -> None:
|
|
166
|
+
caplog.set_level(logging.DEBUG)
|
|
167
|
+
client = _client("execute:rerank")
|
|
168
|
+
responses = [
|
|
169
|
+
client.post("/execute", headers=_headers("media-worker", _WRONG)),
|
|
170
|
+
client.post("/execute", headers=_headers("intruder", _SECRET)),
|
|
171
|
+
client.post("/execute", headers=_headers("media-worker", _SECRET)),
|
|
172
|
+
]
|
|
173
|
+
for response in responses:
|
|
174
|
+
assert _SECRET not in response.text
|
|
175
|
+
assert _WRONG not in response.text
|
|
176
|
+
assert _SECRET not in caplog.text
|
|
177
|
+
assert _WRONG not in caplog.text
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ── construction refusals ─────────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_a_bare_string_scope_is_refused() -> None:
|
|
184
|
+
with pytest.raises(TypeError, match="not a str"):
|
|
185
|
+
build_service_client_guard(_registry(), required_scopes="execute:ocr")
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def test_an_empty_scope_set_is_refused() -> None:
|
|
189
|
+
with pytest.raises(ValueError, match="at least one scope"):
|
|
190
|
+
build_service_client_guard(_registry(), required_scopes=())
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@pytest.mark.parametrize("bad", ["", 7])
|
|
194
|
+
def test_an_empty_or_non_string_scope_is_refused(bad: object) -> None:
|
|
195
|
+
with pytest.raises(ValueError, match="non-empty string"):
|
|
196
|
+
build_service_client_guard(
|
|
197
|
+
_registry(),
|
|
198
|
+
required_scopes=("execute:ocr", bad), # type: ignore[arg-type]
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_the_guard_is_reexported_from_the_package() -> None:
|
|
203
|
+
assert fastapi_m8.build_service_client_guard is build_service_client_guard
|
|
204
|
+
assert "build_service_client_guard" in fastapi_m8.__all__
|
|
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
|