fusion-identity 0.2.35__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fusion_identity/__init__.py +9 -0
- fusion_identity/app.py +405 -0
- fusion_identity/auth.py +1231 -0
- fusion_identity/billing.py +111 -0
- fusion_identity/cache.py +292 -0
- fusion_identity/client/__init__.py +3 -0
- fusion_identity/client/identity_client.py +170 -0
- fusion_identity/client/lease_guard.py +49 -0
- fusion_identity/concurrency.py +123 -0
- fusion_identity/config.py +383 -0
- fusion_identity/crypto.py +92 -0
- fusion_identity/db.py +2030 -0
- fusion_identity/deps.py +245 -0
- fusion_identity/email_sender.py +113 -0
- fusion_identity/events.py +187 -0
- fusion_identity/grpc/__init__.py +0 -0
- fusion_identity/grpc/identity_pb2.py +54 -0
- fusion_identity/grpc/identity_pb2_grpc.py +198 -0
- fusion_identity/grpc_interceptor.py +34 -0
- fusion_identity/grpc_server.py +41 -0
- fusion_identity/grpc_servicer.py +443 -0
- fusion_identity/jwks.py +319 -0
- fusion_identity/jwt_utils.py +129 -0
- fusion_identity/kdf.py +49 -0
- fusion_identity/metrics_collector.py +95 -0
- fusion_identity/migrations.py +78 -0
- fusion_identity/models.py +372 -0
- fusion_identity/payments.py +76 -0
- fusion_identity/ratelimit.py +179 -0
- fusion_identity/routes/__init__.py +1 -0
- fusion_identity/routes/admin.py +576 -0
- fusion_identity/routes/api_keys.py +116 -0
- fusion_identity/routes/app_credentials.py +148 -0
- fusion_identity/routes/app_secrets.py +214 -0
- fusion_identity/routes/audit.py +338 -0
- fusion_identity/routes/auth.py +373 -0
- fusion_identity/routes/departments.py +95 -0
- fusion_identity/routes/health.py +55 -0
- fusion_identity/routes/idps.py +134 -0
- fusion_identity/routes/join.py +310 -0
- fusion_identity/routes/jwks.py +57 -0
- fusion_identity/routes/members.py +262 -0
- fusion_identity/routes/metrics.py +77 -0
- fusion_identity/routes/mfa.py +88 -0
- fusion_identity/routes/oidc.py +374 -0
- fusion_identity/routes/provisioning.py +297 -0
- fusion_identity/routes/quotas.py +68 -0
- fusion_identity/routes/rbac.py +244 -0
- fusion_identity/routes/recharge.py +136 -0
- fusion_identity/routes/registration.py +407 -0
- fusion_identity/routes/scim.py +331 -0
- fusion_identity/routes/service_accounts.py +117 -0
- fusion_identity/routes/tenants.py +86 -0
- fusion_identity/routes/usage.py +253 -0
- fusion_identity/store.py +1981 -0
- fusion_identity/watchdog.py +70 -0
- fusion_identity-0.2.35.dist-info/METADATA +716 -0
- fusion_identity-0.2.35.dist-info/RECORD +62 -0
- fusion_identity-0.2.35.dist-info/WHEEL +5 -0
- fusion_identity-0.2.35.dist-info/entry_points.txt +2 -0
- fusion_identity-0.2.35.dist-info/licenses/LICENSE +201 -0
- fusion_identity-0.2.35.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError
|
|
4
|
+
from importlib.metadata import version as _pkg_version
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
__version__ = _pkg_version("fusion-identity")
|
|
8
|
+
except PackageNotFoundError:
|
|
9
|
+
__version__ = "0.0.0+unknown"
|
fusion_identity/app.py
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import sys
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from fusion_core.http import create_app
|
|
8
|
+
from fusion_core.logging import setup_logging
|
|
9
|
+
from fusion_core.tenant import install_tenant_middleware
|
|
10
|
+
|
|
11
|
+
from fusion_identity.auth import AuthService, bootstrap
|
|
12
|
+
from fusion_identity.config import Settings, load_settings
|
|
13
|
+
from fusion_identity.store import InMemoryStore
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def _build_redis(settings: Settings) -> Any:
|
|
19
|
+
# F13: when redis_url is set the operator intends the quota/concurrency
|
|
20
|
+
# plane to be active. A connection failure must fail-closed (refuse to
|
|
21
|
+
# start) rather than silently degrading to "no concurrency limits, no
|
|
22
|
+
# quota cache". Only an explicitly unset redis_url is a valid opt-out.
|
|
23
|
+
if not settings.redis_url:
|
|
24
|
+
logger.info("build_app: redis disabled (FUSION_IDENTITY_REDIS_URL unset)")
|
|
25
|
+
return None
|
|
26
|
+
try:
|
|
27
|
+
import redis.asyncio as aioredis
|
|
28
|
+
|
|
29
|
+
client = aioredis.from_url(settings.redis_url, decode_responses=True)
|
|
30
|
+
await client.ping()
|
|
31
|
+
logger.info("build_app: redis connected url=%s", settings.redis_url)
|
|
32
|
+
return client
|
|
33
|
+
except Exception as exc:
|
|
34
|
+
logger.error("build_app: redis connect failed (%s) — refusing to start", exc)
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
f"redis connect failed but FUSION_IDENTITY_REDIS_URL is set: {exc}"
|
|
37
|
+
) from exc
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
PREFIX_EXEMPT = ("/api/v1/auth/oidc/",)
|
|
41
|
+
SCIM_PREFIX = "/scim/v2/"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _query_param(scope, name: str) -> str | None:
|
|
45
|
+
qs = scope.get("query_string", b"")
|
|
46
|
+
if not qs:
|
|
47
|
+
return None
|
|
48
|
+
from urllib.parse import parse_qs
|
|
49
|
+
|
|
50
|
+
params = parse_qs(qs.decode("latin-1"))
|
|
51
|
+
vals = params.get(name)
|
|
52
|
+
return vals[0] if vals else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class _OidcContextMiddleware:
|
|
56
|
+
def __init__(self, app) -> None:
|
|
57
|
+
self._app = app
|
|
58
|
+
|
|
59
|
+
async def __call__(self, scope, receive, send):
|
|
60
|
+
if scope.get("type") == "http":
|
|
61
|
+
path = scope.get("path") or ""
|
|
62
|
+
headers = list(scope.get("headers") or [])
|
|
63
|
+
changed = False
|
|
64
|
+
if path.startswith(PREFIX_EXEMPT):
|
|
65
|
+
if not any(k == b"x-tenant-id" for k, _ in headers):
|
|
66
|
+
headers.append((b"x-tenant-id", b"oidc"))
|
|
67
|
+
changed = True
|
|
68
|
+
elif path.startswith(SCIM_PREFIX) and not any(k == b"x-tenant-id" for k, _ in headers):
|
|
69
|
+
tid = _query_param(scope, "tenantId")
|
|
70
|
+
if tid:
|
|
71
|
+
headers.append((b"x-tenant-id", tid.encode("latin-1")))
|
|
72
|
+
changed = True
|
|
73
|
+
if changed:
|
|
74
|
+
scope["headers"] = headers
|
|
75
|
+
await self._app(scope, receive, send)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class _SecurityHeadersMiddleware:
|
|
79
|
+
# P1-6: attach baseline security response headers to every HTTP response.
|
|
80
|
+
# No X-Content-Type-Options/ nosniff on the HTML /docs routes left a
|
|
81
|
+
# content-sniffing surface; HSTS + X-Frame-Options harden the service
|
|
82
|
+
# regardless of upstream proxy. This is ASGI, not a FastAPI BaseHTTPMiddleware,
|
|
83
|
+
# to avoid the known request-body buffering overhead.
|
|
84
|
+
def __init__(self, app) -> None:
|
|
85
|
+
self._app = app
|
|
86
|
+
|
|
87
|
+
async def __call__(self, scope, receive, send):
|
|
88
|
+
if scope.get("type") != "http":
|
|
89
|
+
await self._app(scope, receive, send)
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
async def _send(message):
|
|
93
|
+
if message.get("type") == "http.response.start":
|
|
94
|
+
headers = list(message.get("headers") or [])
|
|
95
|
+
for name, val in (
|
|
96
|
+
(b"x-content-type-options", b"nosniff"),
|
|
97
|
+
(b"x-frame-options", b"DENY"),
|
|
98
|
+
(b"strict-transport-security", b"max-age=31536000; includeSubDomains"),
|
|
99
|
+
(b"x-xss-protection", b"0"),
|
|
100
|
+
(b"referrer-policy", b"no-referrer"),
|
|
101
|
+
):
|
|
102
|
+
if not any(k.lower() == name for k, _ in headers):
|
|
103
|
+
headers.append((name, val))
|
|
104
|
+
message = {**message, "headers": headers}
|
|
105
|
+
await send(message)
|
|
106
|
+
|
|
107
|
+
await self._app(scope, receive, _send)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
TENANT_EXEMPT = frozenset(
|
|
111
|
+
{
|
|
112
|
+
"/health",
|
|
113
|
+
"/ready",
|
|
114
|
+
"/docs",
|
|
115
|
+
"/openapi.json",
|
|
116
|
+
"/redoc",
|
|
117
|
+
"/.well-known/jwks.json",
|
|
118
|
+
"/.well-known/jwks/rotate",
|
|
119
|
+
"/metrics",
|
|
120
|
+
"/api/v1/auth/login",
|
|
121
|
+
"/api/v1/auth/verify",
|
|
122
|
+
"/api/v1/auth/refresh",
|
|
123
|
+
"/api/v1/auth/introspect",
|
|
124
|
+
"/api/v1/auth/ticket/exchange",
|
|
125
|
+
"/api/v1/auth/service-accounts/token",
|
|
126
|
+
"/api/v1/auth/session",
|
|
127
|
+
"/api/v1/auth/session/verify",
|
|
128
|
+
"/api/v1/auth/sessions",
|
|
129
|
+
"/api/v1/auth/register",
|
|
130
|
+
"/api/v1/auth/register-tenant",
|
|
131
|
+
"/api/v1/auth/verify-email",
|
|
132
|
+
"/api/v1/auth/verify-email/resend",
|
|
133
|
+
"/api/v1/auth/password/forgot",
|
|
134
|
+
"/api/v1/auth/password/reset",
|
|
135
|
+
"/scim/v2/Users",
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _build_store(settings: Settings) -> Any:
|
|
141
|
+
url = settings.database_url
|
|
142
|
+
# F14: when use_pgstore=True the operator intends production persistence.
|
|
143
|
+
# A PgStore init failure must refuse to start, not silently fall back to
|
|
144
|
+
# InMemoryStore (which would lose all tenant data on restart and has no
|
|
145
|
+
# consistency across workers). Only use_pgstore=False opts into InMemory.
|
|
146
|
+
if settings.use_pgstore and url:
|
|
147
|
+
from fusion_identity.db import PgStore
|
|
148
|
+
|
|
149
|
+
store = PgStore(url, pool_max=settings.db_pool_max)
|
|
150
|
+
logger.info(
|
|
151
|
+
"build_app: store=PgStore url=%s pool_max=%s (production)",
|
|
152
|
+
_safe_url(url),
|
|
153
|
+
settings.db_pool_max,
|
|
154
|
+
)
|
|
155
|
+
return store
|
|
156
|
+
logger.warning("build_app: store=InMemoryStore (NOT for production)")
|
|
157
|
+
return InMemoryStore()
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _build_auth_service(settings: Settings, store: Any) -> AuthService:
|
|
161
|
+
key_ring: Any = None
|
|
162
|
+
if settings.jwt_algorithm == "RS256":
|
|
163
|
+
from fusion_identity.jwks import KeyRing
|
|
164
|
+
|
|
165
|
+
key_ring = KeyRing.rs256(
|
|
166
|
+
settings.jwt_private_key_pem,
|
|
167
|
+
public_keys_pem=settings.jwt_public_keys,
|
|
168
|
+
persist_path=settings.jwt_keyring_path,
|
|
169
|
+
)
|
|
170
|
+
logger.info("build_app: KeyRing RS256 kid=%s", key_ring.kid)
|
|
171
|
+
return AuthService(
|
|
172
|
+
store,
|
|
173
|
+
signing_key=settings.jwt_signing_key,
|
|
174
|
+
issuer=settings.jwt_issuer,
|
|
175
|
+
audience=settings.jwt_audience,
|
|
176
|
+
ttl=settings.jwt_ttl_seconds,
|
|
177
|
+
refresh_ttl=settings.refresh_ttl_seconds,
|
|
178
|
+
key_ring=key_ring,
|
|
179
|
+
kek=settings.kek,
|
|
180
|
+
kek_prev=settings.kek_prev,
|
|
181
|
+
mfa_enforce_admin=settings.mfa_enforce_admin,
|
|
182
|
+
email_verification_required=settings.email_verification_required,
|
|
183
|
+
sso_ticket_ttl=settings.sso_ticket_ttl_seconds,
|
|
184
|
+
service_account_ttl=settings.service_account_ttl_seconds,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _package_version() -> str:
|
|
189
|
+
# Live-read the installed distribution version instead of a hardcoded
|
|
190
|
+
# literal that drifts from pyproject.toml on every release.
|
|
191
|
+
try:
|
|
192
|
+
from importlib.metadata import version
|
|
193
|
+
|
|
194
|
+
return version("fusion-identity")
|
|
195
|
+
except Exception:
|
|
196
|
+
logger.warning("build_app: fusion-identity metadata unavailable, fallback 0.0.0")
|
|
197
|
+
return "0.0.0"
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def build_app(settings: Settings, *, store: Any | None = None, run_bootstrap: bool = True) -> Any:
|
|
201
|
+
setup_logging("fusion_identity", level=settings.log_level, json_format=settings.log_json)
|
|
202
|
+
app = create_app("fusion-identity", version=_package_version())
|
|
203
|
+
app.state.settings = settings
|
|
204
|
+
app.state.store = store if store is not None else _build_store(settings)
|
|
205
|
+
app.state.auth_service = _build_auth_service(settings, app.state.store)
|
|
206
|
+
app.state.redis = None
|
|
207
|
+
app.state.cache = None
|
|
208
|
+
app.state.concurrency = None
|
|
209
|
+
app.state.grpc_server = None
|
|
210
|
+
|
|
211
|
+
from fusion_identity.routes.admin import router as admin_router
|
|
212
|
+
from fusion_identity.routes.api_keys import router as api_keys_router
|
|
213
|
+
from fusion_identity.routes.app_credentials import router as app_credentials_router
|
|
214
|
+
from fusion_identity.routes.app_secrets import router as app_secrets_router
|
|
215
|
+
from fusion_identity.routes.audit import router as audit_router
|
|
216
|
+
from fusion_identity.routes.auth import router as auth_router
|
|
217
|
+
from fusion_identity.routes.departments import router as departments_router
|
|
218
|
+
from fusion_identity.routes.health import router as health_router
|
|
219
|
+
from fusion_identity.routes.idps import router as idps_router
|
|
220
|
+
from fusion_identity.routes.join import router as join_router
|
|
221
|
+
from fusion_identity.routes.jwks import router as jwks_router
|
|
222
|
+
from fusion_identity.routes.members import router as members_router
|
|
223
|
+
from fusion_identity.routes.metrics import router as metrics_router
|
|
224
|
+
from fusion_identity.routes.mfa import router as mfa_router
|
|
225
|
+
from fusion_identity.routes.oidc import router as oidc_router
|
|
226
|
+
from fusion_identity.routes.provisioning import router as provisioning_router
|
|
227
|
+
from fusion_identity.routes.quotas import router as quotas_router
|
|
228
|
+
from fusion_identity.routes.rbac import router as rbac_router
|
|
229
|
+
from fusion_identity.routes.recharge import router as recharge_router
|
|
230
|
+
from fusion_identity.routes.registration import router as registration_router
|
|
231
|
+
from fusion_identity.routes.scim import router as scim_router
|
|
232
|
+
from fusion_identity.routes.service_accounts import admin_router as sa_admin_router
|
|
233
|
+
from fusion_identity.routes.service_accounts import auth_router as sa_auth_router
|
|
234
|
+
from fusion_identity.routes.tenants import router as tenants_router
|
|
235
|
+
from fusion_identity.routes.usage import router as usage_router
|
|
236
|
+
|
|
237
|
+
app.include_router(auth_router)
|
|
238
|
+
app.include_router(health_router)
|
|
239
|
+
app.include_router(tenants_router)
|
|
240
|
+
app.include_router(members_router)
|
|
241
|
+
app.include_router(api_keys_router)
|
|
242
|
+
app.include_router(quotas_router)
|
|
243
|
+
app.include_router(audit_router)
|
|
244
|
+
app.include_router(departments_router)
|
|
245
|
+
app.include_router(rbac_router)
|
|
246
|
+
app.include_router(usage_router)
|
|
247
|
+
app.include_router(metrics_router)
|
|
248
|
+
app.include_router(jwks_router)
|
|
249
|
+
app.include_router(idps_router)
|
|
250
|
+
app.include_router(oidc_router)
|
|
251
|
+
app.include_router(scim_router)
|
|
252
|
+
app.include_router(mfa_router)
|
|
253
|
+
app.include_router(admin_router)
|
|
254
|
+
app.include_router(recharge_router)
|
|
255
|
+
app.include_router(sa_admin_router)
|
|
256
|
+
app.include_router(sa_auth_router)
|
|
257
|
+
app.include_router(app_secrets_router)
|
|
258
|
+
app.include_router(app_credentials_router)
|
|
259
|
+
app.include_router(provisioning_router)
|
|
260
|
+
app.include_router(registration_router)
|
|
261
|
+
app.include_router(join_router)
|
|
262
|
+
app.state.webhook_worker = None
|
|
263
|
+
app.state.watchdog = None
|
|
264
|
+
|
|
265
|
+
def _verify_jwt(token: str) -> dict[str, Any] | None:
|
|
266
|
+
try:
|
|
267
|
+
parts = token.split(".")
|
|
268
|
+
except (AttributeError, ValueError):
|
|
269
|
+
return None
|
|
270
|
+
if len(parts) != 3:
|
|
271
|
+
return None
|
|
272
|
+
try:
|
|
273
|
+
from fusion_core.tenant.jwt_utils import decode_jwt_claims
|
|
274
|
+
|
|
275
|
+
return decode_jwt_claims(token)
|
|
276
|
+
except Exception as exc:
|
|
277
|
+
logger.debug("tenant middleware: non-jwt bearer skipped: %s", exc)
|
|
278
|
+
return None
|
|
279
|
+
|
|
280
|
+
install_tenant_middleware(
|
|
281
|
+
app, exempt_paths=TENANT_EXEMPT, verify_jwt=_verify_jwt, require_jwt=False
|
|
282
|
+
)
|
|
283
|
+
app.add_middleware(_OidcContextMiddleware)
|
|
284
|
+
app.add_middleware(_SecurityHeadersMiddleware)
|
|
285
|
+
logger.info("build_app: tenant-identity service wired, port=%s", settings.port)
|
|
286
|
+
|
|
287
|
+
@app.on_event("startup")
|
|
288
|
+
async def _startup() -> None:
|
|
289
|
+
await app.state.store.connect()
|
|
290
|
+
if hasattr(app.state.store, "ensure_schema"):
|
|
291
|
+
await app.state.store.ensure_schema()
|
|
292
|
+
redis = await _build_redis(settings)
|
|
293
|
+
app.state.redis = redis
|
|
294
|
+
if redis is not None:
|
|
295
|
+
from fusion_identity.cache import IdentityCache
|
|
296
|
+
from fusion_identity.concurrency import ConcurrencyManager
|
|
297
|
+
|
|
298
|
+
app.state.cache = IdentityCache(redis)
|
|
299
|
+
app.state.concurrency = ConcurrencyManager(redis, settings.lease_ttl_seconds)
|
|
300
|
+
await app.state.cache.init_scripts()
|
|
301
|
+
await app.state.concurrency.init_scripts()
|
|
302
|
+
if run_bootstrap:
|
|
303
|
+
# F15: bootstrap establishes the first admin — a failure must
|
|
304
|
+
# fail-closed (refuse to start), not be swallowed. A silently
|
|
305
|
+
# started service with no admin locks operators out.
|
|
306
|
+
await bootstrap(
|
|
307
|
+
app.state.store,
|
|
308
|
+
settings.bootstrap_admin_user,
|
|
309
|
+
settings.bootstrap_admin_pass,
|
|
310
|
+
settings.bootstrap_tenants,
|
|
311
|
+
)
|
|
312
|
+
if settings.grpc_port > 0 and app.state.concurrency is not None:
|
|
313
|
+
try:
|
|
314
|
+
from fusion_identity.grpc_server import serve as serve_grpc
|
|
315
|
+
|
|
316
|
+
app.state.grpc_server = await serve_grpc(
|
|
317
|
+
app, host=settings.host, port=settings.grpc_port
|
|
318
|
+
)
|
|
319
|
+
logger.info("build_app: grpc listening on %s:%s", settings.host, settings.grpc_port)
|
|
320
|
+
except Exception as exc:
|
|
321
|
+
# P0-2: fail-closed — gRPC is the authorization control plane.
|
|
322
|
+
# A swallowed start failure leaves HTTP healthy (200) while every
|
|
323
|
+
# AuthorizeAndAcquire dead-ends, causing a silent gateway
|
|
324
|
+
# meltdown. Re-raise so the service refuses to come up, consistent
|
|
325
|
+
# with F13 (redis) / F14 (store) fail-closed semantics.
|
|
326
|
+
logger.error("build_app: grpc start failed, aborting startup (%s)", exc)
|
|
327
|
+
raise
|
|
328
|
+
# Issue #14 (M-B): push plane — drain the event outbox to the signed
|
|
329
|
+
# webhook endpoint when configured; without it consumers pull via
|
|
330
|
+
# GET /api/v1/admin/events.
|
|
331
|
+
if settings.webhook_url and settings.webhook_secret:
|
|
332
|
+
from fusion_identity.events import WebhookWorker
|
|
333
|
+
|
|
334
|
+
app.state.webhook_worker = WebhookWorker(
|
|
335
|
+
app.state.store,
|
|
336
|
+
settings.webhook_url,
|
|
337
|
+
settings.webhook_secret,
|
|
338
|
+
tick_seconds=settings.webhook_tick_seconds,
|
|
339
|
+
max_attempts=settings.webhook_max_attempts,
|
|
340
|
+
)
|
|
341
|
+
app.state.webhook_worker.start()
|
|
342
|
+
logger.info("build_app: webhook worker started url=%s", settings.webhook_url)
|
|
343
|
+
# Issue #64: stall watchdog — a threading.Timer OUTSIDE the event loop
|
|
344
|
+
# probes /health so a stalled loop is at least observable (CRITICAL
|
|
345
|
+
# log). Detection only; recovery is the operator's fusion-sv restart.
|
|
346
|
+
if settings.stall_watchdog_seconds > 0:
|
|
347
|
+
from fusion_identity.watchdog import StallWatchdog
|
|
348
|
+
|
|
349
|
+
app.state.watchdog = StallWatchdog(
|
|
350
|
+
settings.port,
|
|
351
|
+
host=settings.host,
|
|
352
|
+
interval=settings.stall_watchdog_seconds,
|
|
353
|
+
)
|
|
354
|
+
app.state.watchdog.start()
|
|
355
|
+
|
|
356
|
+
@app.on_event("shutdown")
|
|
357
|
+
async def _shutdown() -> None:
|
|
358
|
+
if app.state.watchdog is not None:
|
|
359
|
+
app.state.watchdog.stop()
|
|
360
|
+
app.state.watchdog = None
|
|
361
|
+
if app.state.webhook_worker is not None:
|
|
362
|
+
try:
|
|
363
|
+
await app.state.webhook_worker.stop()
|
|
364
|
+
except Exception as exc:
|
|
365
|
+
logger.warning("webhook worker stop: %s", exc)
|
|
366
|
+
app.state.webhook_worker = None
|
|
367
|
+
if app.state.grpc_server is not None:
|
|
368
|
+
try:
|
|
369
|
+
await app.state.grpc_server.stop(grace=2.0)
|
|
370
|
+
except Exception as exc:
|
|
371
|
+
logger.warning("grpc stop: %s", exc)
|
|
372
|
+
if app.state.redis is not None:
|
|
373
|
+
try:
|
|
374
|
+
await app.state.redis.aclose()
|
|
375
|
+
except Exception as exc:
|
|
376
|
+
logger.warning("redis close: %s", exc)
|
|
377
|
+
await app.state.store.close()
|
|
378
|
+
|
|
379
|
+
return app
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def main() -> None:
|
|
383
|
+
# Homebrew formula test block (and operators) need a version probe that
|
|
384
|
+
# does not boot the whole fail-closed settings/app stack.
|
|
385
|
+
if "--version" in sys.argv or "-V" in sys.argv:
|
|
386
|
+
print(f"fusion-identity {_package_version()}")
|
|
387
|
+
return
|
|
388
|
+
|
|
389
|
+
import uvicorn
|
|
390
|
+
|
|
391
|
+
settings = load_settings()
|
|
392
|
+
app = build_app(settings)
|
|
393
|
+
uvicorn.run(app, host=settings.host, port=settings.port, log_level=settings.log_level.lower())
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _safe_url(url: str) -> str:
|
|
397
|
+
if "@" in url:
|
|
398
|
+
scheme, rest = url.split("://", 1)
|
|
399
|
+
host = rest.split("@", 1)[1]
|
|
400
|
+
return f"{scheme}://***@{host}"
|
|
401
|
+
return url
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
if __name__ == "__main__":
|
|
405
|
+
main()
|