Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(dev-mode): unify is_dev_env() helper + startup invariant (H-8 + P2-A3) (#127)
Beenu Arora06d76de4067b
diff --git a/services/api/app/api/v1/dev_auth.py b/services/api/app/api/v1/dev_auth.pyindex b6c993049..37e821f2e 100644--- a/services/api/app/api/v1/dev_auth.py+++ b/services/api/app/api/v1/dev_auth.py@@ -1,22 +1,30 @@"""Development authentication bypass.-When ``ENV`` is ``development`` and a request arrives without a bearer token,-we resolve a deterministic demo user. This makes the web console usable-without seeding users + logging in for every contributor running the stack-locally.--The demo IDs are also used by ``services/api/app/scripts/seed_demo.py`` so the-data the UI sees actually belongs to the user that the API hands back.--Production safety: this module reads ``ENV`` at call time and refuses to-return a demo user unless ``ENV in {development, dev, local, demo}``.+When ``ENV`` names a dev-class environment and a request arrives without+a bearer token, we resolve a deterministic demo user. This makes the web+console usable without seeding users + logging in for every contributor+running the stack locally.++The demo IDs are also used by ``services/api/app/scripts/seed_demo.py``+so the data the UI sees actually belongs to the user that the API hands+back.++Production safety: this module reads ``ENV`` from the live process+environment at call time (not from the cached :class:`Settings`+singleton) so a test that does ``monkeypatch.setenv("ENV", "production")``+mid-suite immediately stops handing back the demo user. The canonical+allow-list lives in ``app.core.config.AUTH_BYPASS_ENVIRONMENTS`` and+deliberately excludes ``"test"`` — test suites must seed an+``Authorization`` header rather than rely on the bypass, or genuine auth+regressions get masked."""from __future__ import annotations-import osimport uuid+from app.core.config import current_env_from_os, is_auth_bypass_env+# Deterministic demo IDs — kept in sync with seed_demo.pyDEMO_TENANT_ID: uuid.UUID = uuid.UUID("00000000-0000-0000-0000-000000000001")DEMO_USER_ID: uuid.UUID = uuid.UUID("00000000-0000-0000-0000-000000000002")@@ -26,10 +34,12 @@ DEMO_USER_EMAIL: str = "[email protected]"DEMO_USER_PASSWORD: str = "aisoc-demo"DEMO_USER_ROLE: str = "admin"-_DEV_ENVS = {"development", "dev", "local", "demo"}-def is_dev_mode() -> bool:- """True if the running environment permits the auth bypass."""- env = (os.getenv("ENV") or os.getenv("ENVIRONMENT") or "").strip().lower()- return env in _DEV_ENVS+ """True if the running environment permits the auth bypass.++ Reads the environment from ``os.environ`` at call time via+ :func:`app.core.config.current_env_from_os` so tests that+ ``monkeypatch.setenv(...)`` mid-suite see the change immediately.+ """+ return is_auth_bypass_env(current_env_from_os())diff --git a/services/api/app/core/config.py b/services/api/app/core/config.pyindex e3df0b541..8dc826ed4 100644--- a/services/api/app/core/config.py+++ b/services/api/app/core/config.py@@ -5,11 +5,12 @@ MIT License"""import logging+import osimport warningsfrom functools import lru_cache-from typing import Any+from typing import Any, Final-from pydantic import PostgresDsn, RedisDsn, field_validator+from pydantic import PostgresDsn, RedisDsn, field_validator, model_validatorfrom pydantic_settings import BaseSettings, SettingsConfigDict# Default placeholders shipped in source. Anything matching these in a@@ -25,6 +26,84 @@ INSECURE_SECRET_KEY_DEFAULTS: frozenset[str] = frozenset(})+# ------------------------------------------------------------------+# Canonical "dev mode" detection (H-8 + P2-A3).+#+# Before unification, the codebase had at least four different sets of+# strings hand-rolled across multiple modules — each with a slightly+# different list ("dev", "development", "local", "demo", sometimes+# "test", sometimes none of the above). That meant the same boot could+# count as "dev" for one check and "prod" for another, silently+# bypassing safety rails or, worse, gating them on the wrong axis.+#+# These two frozensets are now the **only** truth in the codebase.+#+# ``DEV_ENVIRONMENTS`` – Used by most subsystems (table autocreate,+# docs URLs, /metrics gate, structured-vs-JSON+# logging, GraphiQL UI, credential-vault+# ephemeral key, insecure-default warnings).+# Includes ``"test"`` because pytest sets+# ``ENV=test`` and we never want to gate+# test-suite behaviour behind production+# rails (e.g. requiring a real Fernet key).+#+# ``AUTH_BYPASS_ENVIRONMENTS`` – Used **only** by the request-time+# no-credentials-→-demo-user shim in+# ``app.api.v1.dev_auth``. Deliberately+# excludes ``"test"`` so a test suite+# that forgets to seed an Authorization+# header gets a 401 instead of being+# handed admin-on-the-demo-tenant — that+# used to mask real auth regressions.+# ------------------------------------------------------------------+DEV_ENVIRONMENTS: Final[frozenset[str]] = frozenset({"development", "dev", "local", "demo", "test"})+AUTH_BYPASS_ENVIRONMENTS: Final[frozenset[str]] = frozenset({"development", "dev", "local", "demo"})+++def _normalize_env(value: str | None) -> str:+ return (value or "").strip().lower()+++def is_dev_env(value: str | None) -> bool:+ """Return True if ``value`` names a development-class environment.++ Accepts any of ``development | dev | local | demo | test`` (case- and+ whitespace-insensitive). This is the **single authoritative**+ predicate; every other call site should route through this helper.+ """+ return _normalize_env(value) in DEV_ENVIRONMENTS+++def is_auth_bypass_env(value: str | None) -> bool:+ """Return True if ``value`` permits the no-credentials demo-user shim.++ Same canonical set as :func:`is_dev_env` minus ``"test"`` — see the+ module-level rationale.+ """+ return _normalize_env(value) in AUTH_BYPASS_ENVIRONMENTS+++def current_env_from_os() -> str:+ """Read the current environment from ``os.environ`` at call time.++ This deliberately bypasses the cached :class:`Settings` singleton so+ tests that ``monkeypatch.setenv("ENV", ...)`` mid-request still see+ the override. Used by request-time gates only (e.g. dev-auth shim);+ boot-time / once-per-process decisions should read ``settings.ENV``+ or ``settings.ENVIRONMENT`` instead.+ """+ return _normalize_env(os.getenv("ENV") or os.getenv("ENVIRONMENT"))+++def is_dev_mode_runtime() -> bool:+ """True if the live process is in a dev-class environment.++ Combines :func:`current_env_from_os` with :func:`is_dev_env` so+ monkeypatched env vars (tests) and the cached+ :class:`Settings` object cannot disagree.+ """+ return is_dev_env(current_env_from_os())+class Settings(BaseSettings):model_config = SettingsConfigDict(@@ -429,16 +508,72 @@ class Settings(BaseSettings):return [origin.strip() for origin in v.split(",")]return v+ @model_validator(mode="after")+ def _reconcile_env_aliases(self) -> "Settings":+ """Keep ``ENV`` and ``ENVIRONMENT`` consistent.++ Historically the codebase had two aliases for the same concept,+ each populated from a different env var, each read from a+ different module. When operators set only one (the common case),+ the unset one stayed as the class default ``"development"`` —+ silently bypassing the production gates that read the unset+ side. This validator fixes both cases:++ * If exactly one of ``ENV`` / ``ENVIRONMENT`` is set, mirror it+ to the other.+ * If both are set but disagree, emit a ``RuntimeWarning`` and+ prefer ``ENVIRONMENT`` (the newer name) so the more-explicit+ intent wins. Operators see the warning at boot and can+ collapse to a single var.+ """+ env_raw = (self.ENV or "").strip()+ environment_raw = (self.ENVIRONMENT or "").strip()++ # ``"development"`` is the class default, which we use as the+ # signal "this was never set by the operator". That's slightly+ # lossy (an operator who explicitly types ENV=development looks+ # the same as not-set), but the resulting behaviour is identical+ # so the distinction doesn't matter.+ env_is_default = _normalize_env(env_raw) == "development"+ environment_is_default = _normalize_env(environment_raw) == "development"++ if env_is_default and not environment_is_default:+ # Operator set ENVIRONMENT but not ENV — mirror.+ object.__setattr__(self, "ENV", environment_raw)+ elif environment_is_default and not env_is_default:+ # Operator set ENV but not ENVIRONMENT — mirror.+ object.__setattr__(self, "ENVIRONMENT", env_raw)+ elif not env_is_default and not environment_is_default and _normalize_env(env_raw) != _normalize_env(environment_raw):+ # Both set and disagree: prefer ENVIRONMENT, warn loudly so+ # ops collapses to one canonical name.+ warnings.warn(+ f"ENV={env_raw!r} and ENVIRONMENT={environment_raw!r} disagree; "+ f"using ENVIRONMENT and mirroring to ENV. Set only one of these.",+ RuntimeWarning,+ stacklevel=2,+ )+ object.__setattr__(self, "ENV", environment_raw)++ return self++ # Convenience predicates so callers don't need to import the module+ # level helpers when they already have a ``settings`` reference.+ @property+ def is_dev(self) -> bool:+ """True if this settings object names a dev-class environment."""+ return is_dev_env(self.ENVIRONMENT)++ @property+ def is_production(self) -> bool:+ """True if this is a non-dev (i.e. real) environment."""+ return not self.is_dev+@lru_cachedef get_settings() -> Settings:return Settings()-def _is_dev_env(env: str) -> bool:- return (env or "").strip().lower() in {"development", "dev", "local", "demo", "test"}--def warn_if_insecure_defaults(s: Settings | None = None) -> list[str]:"""Emit a structured warning for each insecure default still in place.@@ -459,17 +594,17 @@ def warn_if_insecure_defaults(s: Settings | None = None) -> list[str]:msgs.append("SECRET_KEY is set to a known insecure placeholder; rotate before exposing this instance to the network.")# /metrics auth: outside dev we expect a non-empty token.- if not _is_dev_env(s.ENVIRONMENT) and not s.METRICS_TOKEN:+ if not is_dev_env(s.ENVIRONMENT) and not s.METRICS_TOKEN:msgs.append("METRICS_TOKEN is empty in a non-development environment — /metrics is currently unauthenticated.")# Plugin trust mode: never silently default to disabled in prod.- if not _is_dev_env(s.ENVIRONMENT) and s.PLUGIN_TRUST_MODE.lower() == "disabled":+ if not is_dev_env(s.ENVIRONMENT) and s.PLUGIN_TRUST_MODE.lower() == "disabled":msgs.append("PLUGIN_TRUST_MODE=disabled outside development — plugins will load without signature verification.")# Connector credential vault: refuse to silently boot without an encryption# key outside development. Without this, ``Connector.auth_config`` would# round-trip in plaintext.- if not _is_dev_env(s.ENVIRONMENT) and not s.AISOC_CREDENTIAL_KEY:+ if not is_dev_env(s.ENVIRONMENT) and not s.AISOC_CREDENTIAL_KEY:msgs.append("AISOC_CREDENTIAL_KEY is empty in a non-development environment — connector credentials cannot be encrypted at rest.")# Air-gap sanity check: if an operator flipped on AISOC_AIRGAPPED butdiff --git a/services/api/app/core/logging.py b/services/api/app/core/logging.pyindex 79fe11d9c..5f604dbee 100644--- a/services/api/app/core/logging.py+++ b/services/api/app/core/logging.py@@ -5,7 +5,7 @@ import sysimport structlog-from app.core.config import settings+from app.core.config import is_dev_env, settingsdef configure_logging() -> None:@@ -20,7 +20,12 @@ def configure_logging() -> None:structlog.processors.TimeStamper(fmt="iso"),]- if settings.ENV == "development":+ # Previously this used ``settings.ENV == "development"`` — an exact-match+ # string compare that silently skipped the console renderer for the other+ # dev aliases (``dev``, ``local``, ``demo``, ``test``). Routing to+ # ``is_dev_env`` keeps a single source of truth for what "this is a+ # development-class environment" means across the service.+ if is_dev_env(settings.ENV):renderer: structlog.types.Processor = structlog.dev.ConsoleRenderer()else:renderer = structlog.processors.JSONRenderer()diff --git a/services/api/app/graphql/schema.py b/services/api/app/graphql/schema.pyindex ae28891b7..23e8d9e91 100644--- a/services/api/app/graphql/schema.py+++ b/services/api/app/graphql/schema.py@@ -14,9 +14,11 @@ filters as defense-in-depth in case a model is not yet covered by RLS.GraphiQL exposure------------------GraphiQL is enabled only when ``ENVIRONMENT == "development"``. In any-other environment the introspection UI is disabled to reduce attack-surface.+GraphiQL is enabled only in development-class environments (see+``app.core.config.DEV_ENVIRONMENTS``). In production the introspection UI+is disabled to reduce attack surface; this is checked via the canonical+``is_dev_env`` helper rather than an ad-hoc string compare so adding a+new dev alias automatically gets the right behaviour."""from __future__ import annotations@@ -29,7 +31,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom strawberry.fastapi import GraphQLRouterfrom app.api.v1.deps import CurrentUser, get_current_user-from app.core.config import settings+from app.core.config import is_dev_env, settingsfrom app.db.rls import get_tenant_dbfrom app.graphql.query import Query@@ -54,13 +56,13 @@ schema = strawberry.Schema(query=Query)# ─── FastAPI router ───────────────────────────────────────────────────────────-# GraphiQL is only enabled in development. Production-like environments get a-# pure JSON endpoint with no introspection UI.+# GraphiQL is only enabled in development-class environments. Production+# gets a pure JSON endpoint with no introspection UI.## strawberry-graphql >= 0.231 replaced the old ``graphiql: bool`` kwarg with# ``graphql_ide: GraphQL_IDE | None`` (where ``"graphiql"`` is the default and# ``None`` disables the IDE entirely). We pin to that newer API.-_graphql_ide: str | None = "graphiql" if settings.ENVIRONMENT.lower() == "development" else None+_graphql_ide: str | None = "graphiql" if is_dev_env(settings.ENVIRONMENT) else Nonegraphql_router = GraphQLRouter(schema,diff --git a/services/api/app/main.py b/services/api/app/main.pyindex 064c1feb1..803037eb7 100644--- a/services/api/app/main.py+++ b/services/api/app/main.py@@ -17,7 +17,7 @@ from app.api.v1.router import api_routerfrom app.auth.oidc import router as oidc_routerfrom app.auth.saml import router as saml_routerfrom app.core.airgap import airgap_status-from app.core.config import settings, warn_if_insecure_defaults+from app.core.config import is_dev_env, settings, warn_if_insecure_defaultsfrom app.core.logging import configure_loggingfrom app.core.telemetry import instrument_appfrom app.db.clickhouse import close_clickhouse@@ -31,7 +31,6 @@ from app.services.plugin_manager import get_plugin_managerfrom app.workers.oauth_refresh import run_forever as run_oauth_refreshfrom app.workers.weekly_digest_task import run_forever as run_weekly_digest-_DEV_ENVIRONMENTS = {"development", "dev", "local", "demo", "test"}_metrics_bearer = HTTPBearer(auto_error=False)logger = structlog.get_logger(__name__)@@ -60,8 +59,11 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:# log stream so operators see them before anything else.warn_if_insecure_defaults(settings)- # Create all database tables (dev only; use Alembic migrations in prod)- if settings.ENVIRONMENT == "development":+ # Create all database tables (dev only; use Alembic migrations in prod).+ # We keep this narrowly scoped to the canonical ``"development"`` label+ # so demo / test / local don't quietly run ``create_all`` on top of a+ # real schema. The looser dev gate is used below for SQL migrations.+ if (settings.ENVIRONMENT or "").strip().lower() == "development":try:async with engine.begin() as conn:await conn.run_sync(Base.metadata.create_all)@@ -77,7 +79,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:# production for now since prod is expected to have a managed migration# pipeline; demo / dev environments bootstrap themselves on boot so the# first deploy of a new feature is usable immediately.- if settings.ENVIRONMENT in _DEV_ENVIRONMENTS:+ if settings.is_dev:try:from app.scripts.run_migrations import main as run_sql_migrations # noqa: PLC0415@@ -158,9 +160,13 @@ def create_application() -> FastAPI:title="AiSOC Platform API",description=("AiSOC — open-source AI Security Operations Center. Autonomous threat detection, investigation, and response."),version=settings.VERSION,- docs_url="/api/docs" if settings.ENVIRONMENT != "production" else None,- redoc_url="/api/redoc" if settings.ENVIRONMENT != "production" else None,- openapi_url="/api/openapi.json" if settings.ENVIRONMENT != "production" else None,+ # Docs are exposed in every non-production environment. We use the+ # ``is_production`` predicate (the inverse of the dev set) so any+ # operator who names a new dev-class environment in DEV_ENVIRONMENTS+ # automatically gets docs without touching this file.+ docs_url="/api/docs" if not settings.is_production else None,+ redoc_url="/api/redoc" if not settings.is_production else None,+ openapi_url="/api/openapi.json" if not settings.is_production else None,lifespan=lifespan,)@@ -243,7 +249,12 @@ async def health_check() -> dict:def _metrics_environment_is_dev() -> bool:- return (settings.ENVIRONMENT or "").strip().lower() in _DEV_ENVIRONMENTS+ # Delegate to the canonical helper so the dev allow-list stays in one+ # place. We intentionally read ``settings.ENVIRONMENT`` (cached) rather+ # than ``os.environ`` because boot-time CORS/docs decisions were made+ # against the same cached value — flipping it at request time would be+ # inconsistent.+ return is_dev_env(settings.ENVIRONMENT)@app.get("/metrics", tags=["system"])diff --git a/services/api/app/security/credential_vault.py b/services/api/app/security/credential_vault.pyindex fa8548ebd..c603b4727 100644--- a/services/api/app/security/credential_vault.py+++ b/services/api/app/security/credential_vault.py@@ -43,7 +43,7 @@ from typing import Any, Finalfrom cryptography.fernet import Fernet, InvalidToken, MultiFernet-from app.core.config import settings+from app.core.config import is_dev_env, settingslogger = logging.getLogger("aisoc.credential_vault")@@ -187,10 +187,6 @@ _vault_singleton: CredentialVault | None = None_vault_lock = Lock()-def _is_dev_env() -> bool:- return (settings.ENVIRONMENT or "").strip().lower() in {"development", "dev", "local", "demo", "test"}--def get_vault() -> CredentialVault:"""Return the process-wide :class:`CredentialVault`.@@ -213,7 +209,11 @@ def get_vault() -> CredentialVault:return _vault_singletonprimary = (settings.AISOC_CREDENTIAL_KEY or "").strip().encode("ascii")if not primary:- if not _is_dev_env():+ # NB: vault startup is a boot-time decision so we read from the+ # cached ``settings`` (the same value our env warnings used),+ # not from the live process env. If an operator monkey-patches+ # the env post-boot, the vault key check has already happened.+ if not is_dev_env(settings.ENVIRONMENT):raise CredentialVaultError("AISOC_CREDENTIAL_KEY is required outside development; refusing to start credential vault.")primary = Fernet.generate_key()logger.warning(diff --git a/services/api/tests/test_dev_mode_unification.py b/services/api/tests/test_dev_mode_unification.pynew file mode 100644index 000000000..93e0748b5--- /dev/null+++ b/services/api/tests/test_dev_mode_unification.py@@ -0,0 +1,406 @@+"""Tests for the canonical "dev mode" detection helpers (Batch 11 — H-8 + P2-A3).++Background+----------+Before unification, several modules each rolled their own check for+"is this a development environment?":++* ``services/api/app/api/v1/dev_auth.py`` used ``ENV in {"dev", "development", "local"}``+* ``services/api/app/core/logging.py`` used ``ENV == "development"``+* ``services/api/app/main.py`` used ``ENVIRONMENT in {"development", "dev"}``+* ``services/api/app/security/credential_vault.py`` had its own helper++Each list was slightly different, so the same boot could be "dev" for+one check and "prod" for another. This file pins the unified behavior+so future refactors cannot regress it.++The canonical helpers live in ``app.core.config``:++* :data:`DEV_ENVIRONMENTS` — most subsystems (incl. ``"test"``)+* :data:`AUTH_BYPASS_ENVIRONMENTS` — dev-auth shim (excludes ``"test"``)+* :func:`is_dev_env(value)` — pure predicate over a string+* :func:`is_auth_bypass_env(value)` — pure predicate over a string+* :func:`current_env_from_os` — reads ``$ENV`` / ``$ENVIRONMENT`` at call time+* :func:`is_dev_mode_runtime` — convenience combiner of the two above+* :meth:`Settings.is_dev` / :meth:`Settings.is_production` — settings-level predicates+* :meth:`Settings._reconcile_env_aliases` — model-validator that mirrors+ ``ENV`` ↔ ``ENVIRONMENT`` so an operator who sets only one isn't+ silently treated as production by code that reads the other.+"""++from __future__ import annotations++import warnings++import pytest+from app.core.config import (+ AUTH_BYPASS_ENVIRONMENTS,+ DEV_ENVIRONMENTS,+ Settings,+ current_env_from_os,+ is_auth_bypass_env,+ is_dev_env,+ is_dev_mode_runtime,+)++# ─── Helpers ─────────────────────────────────────────────────────────────+++def _make_settings(**overrides) -> Settings:+ """Construct a Settings instance without reading the host .env file."""+ base: dict = {+ "ENVIRONMENT": "production",+ "ENV": "production",+ "SECRET_KEY": "a" * 64,+ "METRICS_TOKEN": "long-random-token",+ "PLUGIN_TRUST_MODE": "strict",+ "AISOC_CREDENTIAL_KEY": "Z7t9fM3pZ8U_bN5oVxQYr1w2kLsHd4aJiTvE6cNxYpA=",+ }+ base.update(overrides)+ return Settings(_env_file=None, **base)+++# ─── DEV_ENVIRONMENTS / AUTH_BYPASS_ENVIRONMENTS invariants ──────────────+++def test_dev_environments_contains_canonical_set():+ """The dev set MUST contain these five names (audit-pinned)."""+ assert {"development", "dev", "local", "demo", "test"} <= DEV_ENVIRONMENTS+++def test_auth_bypass_environments_excludes_test():+ """``"test"`` must NOT permit the auth bypass.++ Otherwise any test suite that forgets to seed an Authorization+ header is silently handed admin-on-the-demo-tenant, masking real+ auth regressions.+ """+ assert "test" in DEV_ENVIRONMENTS+ assert "test" not in AUTH_BYPASS_ENVIRONMENTS+++def test_auth_bypass_environments_is_strict_subset_of_dev():+ """Auth-bypass ⊊ dev. Anything that bypasses auth is by definition dev."""+ assert AUTH_BYPASS_ENVIRONMENTS < DEV_ENVIRONMENTS+++def test_dev_and_auth_bypass_sets_are_frozensets():+ """Both must be frozensets so a downstream caller cannot mutate them."""+ assert isinstance(DEV_ENVIRONMENTS, frozenset)+ assert isinstance(AUTH_BYPASS_ENVIRONMENTS, frozenset)+++# ─── is_dev_env() ────────────────────────────────────────────────────────+++ "value",+ ["development", "dev", "local", "demo", "test"],+)+def test_is_dev_env_true_for_canonical_names(value):+ assert is_dev_env(value) is True+++ "value",+ ["production", "prod", "staging", "stage", "preprod", "uat", "qa"],+)+def test_is_dev_env_false_for_non_dev(value):+ assert is_dev_env(value) is False+++ "value",+ [None, "", " "],+)+def test_is_dev_env_false_for_empty_or_none(value):+ # Empty/missing must NOT be silently treated as dev — otherwise+ # an operator forgetting to set the env var gets the dev rails+ # (auth bypass, ephemeral creds, etc.) in production.+ assert is_dev_env(value) is False+++ "value, expected",+ [+ ("DEVELOPMENT", True),+ ("Production", False),+ (" dev ", True),+ ("\tlocal\n", True),+ (" PROD ", False),+ ],+)+def test_is_dev_env_normalizes_case_and_whitespace(value, expected):+ assert is_dev_env(value) is expected+++def test_is_dev_env_unknown_string_is_false():+ """A typo / unknown environment name fails closed (treated as prod)."""+ assert is_dev_env("developement") is False # common typo+ assert is_dev_env("dev1") is False+ assert is_dev_env("development2") is False+++# ─── is_auth_bypass_env() ────────────────────────────────────────────────+++ "value",+ ["development", "dev", "local", "demo"],+)+def test_is_auth_bypass_env_true_for_bypass_names(value):+ assert is_auth_bypass_env(value) is True+++def test_is_auth_bypass_env_false_for_test():+ """Pinned in is own test: ``test`` must NOT enable the auth bypass."""+ assert is_auth_bypass_env("test") is False+++ "value",+ ["production", "prod", "staging", None, "", "qa"],+)+def test_is_auth_bypass_env_false_for_non_dev(value):+ assert is_auth_bypass_env(value) is False+++def test_is_auth_bypass_env_normalizes_case():+ assert is_auth_bypass_env("DEV") is True+ assert is_auth_bypass_env(" Demo ") is True+++# ─── current_env_from_os() ───────────────────────────────────────────────+++def test_current_env_from_os_prefers_env_over_environment(monkeypatch):+ """``ENV`` wins when both are set (matches Settings ordering)."""+ monkeypatch.setenv("ENV", "dev")+ monkeypatch.setenv("ENVIRONMENT", "production")+ assert current_env_from_os() == "dev"+++def test_current_env_from_os_falls_back_to_environment(monkeypatch):+ """When ``ENV`` is unset, ``ENVIRONMENT`` is used."""+ monkeypatch.delenv("ENV", raising=False)+ monkeypatch.setenv("ENVIRONMENT", "production")+ assert current_env_from_os() == "production"+++def test_current_env_from_os_returns_empty_when_neither_set(monkeypatch):+ monkeypatch.delenv("ENV", raising=False)+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert current_env_from_os() == ""+++def test_current_env_from_os_normalizes_case_and_whitespace(monkeypatch):+ monkeypatch.setenv("ENV", " DEVELOPMENT ")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert current_env_from_os() == "development"+++def test_current_env_from_os_reads_live_environment(monkeypatch):+ """No caching — a mid-test setenv is reflected immediately."""+ monkeypatch.setenv("ENV", "dev")+ assert current_env_from_os() == "dev"+ monkeypatch.setenv("ENV", "production")+ assert current_env_from_os() == "production"+++# ─── is_dev_mode_runtime() ───────────────────────────────────────────────+++def test_is_dev_mode_runtime_true_for_dev_envs(monkeypatch):+ monkeypatch.setenv("ENV", "development")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert is_dev_mode_runtime() is True+++def test_is_dev_mode_runtime_true_for_test_env(monkeypatch):+ monkeypatch.setenv("ENV", "test")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert is_dev_mode_runtime() is True+++def test_is_dev_mode_runtime_false_for_production(monkeypatch):+ monkeypatch.setenv("ENV", "production")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert is_dev_mode_runtime() is False+++def test_is_dev_mode_runtime_false_when_unset(monkeypatch):+ """Missing env fails closed → not dev (i.e. treat as production)."""+ monkeypatch.delenv("ENV", raising=False)+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert is_dev_mode_runtime() is False+++# ─── Settings.is_dev / Settings.is_production ────────────────────────────+++ "env_name",+ ["development", "dev", "local", "demo", "test"],+)+def test_settings_is_dev_true_for_dev_envs(env_name):+ s = _make_settings(ENVIRONMENT=env_name, ENV=env_name)+ assert s.is_dev is True+ assert s.is_production is False+++ "env_name",+ ["production", "prod", "staging", "qa"],+)+def test_settings_is_dev_false_for_non_dev_envs(env_name):+ s = _make_settings(ENVIRONMENT=env_name, ENV=env_name)+ assert s.is_dev is False+ assert s.is_production is True+++def test_settings_is_dev_is_property_not_method():+ """Catch the easy regression of calling it as ``settings.is_dev()``."""+ s = _make_settings(ENVIRONMENT="development", ENV="development")+ assert isinstance(s.is_dev, bool)+ assert isinstance(s.is_production, bool)+++# ─── _reconcile_env_aliases model validator ──────────────────────────────+++def test_reconcile_mirrors_environment_to_env_when_only_environment_set():+ """Operator sets ENVIRONMENT=production only; ENV is the class default.++ Validator must mirror ENVIRONMENT→ENV so a module that reads+ ``settings.ENV`` doesn't silently see ``"development"``.+ """+ s = Settings(+ _env_file=None,+ ENVIRONMENT="production",+ # leave ENV at its class default+ SECRET_KEY="a" * 64,+ METRICS_TOKEN="long-random-token",+ AISOC_CREDENTIAL_KEY="Z7t9fM3pZ8U_bN5oVxQYr1w2kLsHd4aJiTvE6cNxYpA=",+ )+ assert s.ENVIRONMENT == "production"+ assert s.ENV == "production"+++def test_reconcile_mirrors_env_to_environment_when_only_env_set():+ """Reverse direction — operator sets only ENV."""+ s = Settings(+ _env_file=None,+ ENV="production",+ # leave ENVIRONMENT at its class default+ SECRET_KEY="a" * 64,+ METRICS_TOKEN="long-random-token",+ AISOC_CREDENTIAL_KEY="Z7t9fM3pZ8U_bN5oVxQYr1w2kLsHd4aJiTvE6cNxYpA=",+ )+ assert s.ENV == "production"+ assert s.ENVIRONMENT == "production"+++def test_reconcile_both_set_and_agree_no_warning():+ """No warning when both are set to the same value."""+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ s = _make_settings(ENV="production", ENVIRONMENT="production")+ assert s.ENV == "production"+ assert s.ENVIRONMENT == "production"+ # No reconciliation warning should have fired.+ assert not any("disagree" in str(w.message) for w in caught), [str(w.message) for w in caught]+++def test_reconcile_disagreement_prefers_environment_and_warns():+ """When ENV and ENVIRONMENT both have non-default values and disagree,+ ENVIRONMENT wins (newer alias) and a RuntimeWarning surfaces so+ operators see it in boot logs.++ The validator only treats both as "explicitly set" when neither+ equals the class default ``"development"`` — so we use two distinct+ non-default values here.+ """+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ s = _make_settings(ENV="staging", ENVIRONMENT="production")+ # ENVIRONMENT preference → mirrored into ENV+ assert s.ENVIRONMENT == "production"+ assert s.ENV == "production"+ msgs = [str(w.message) for w in caught if issubclass(w.category, RuntimeWarning)]+ assert any("disagree" in m for m in msgs), msgs+++def test_reconcile_default_env_with_explicit_environment_no_warning():+ """``ENV`` defaulting to ``"development"`` while ``ENVIRONMENT`` is+ set explicitly is NOT a disagreement — it's the common mirror case.++ The validator must silently mirror ``ENVIRONMENT`` into ``ENV``+ without a noisy warning.+ """+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ s = Settings(+ _env_file=None,+ ENVIRONMENT="production",+ # ENV omitted → stays at class default "development"+ SECRET_KEY="a" * 64,+ METRICS_TOKEN="long-random-token",+ AISOC_CREDENTIAL_KEY="Z7t9fM3pZ8U_bN5oVxQYr1w2kLsHd4aJiTvE6cNxYpA=",+ )+ assert s.ENV == "production"+ assert s.ENVIRONMENT == "production"+ # No "disagree" warning — operator only set one of them.+ msgs = [str(w.message) for w in caught]+ assert not any("disagree" in m for m in msgs), msgs+++def test_reconcile_both_default_stays_development():+ """Neither set explicitly → both stay at the class default."""+ s = Settings(+ _env_file=None,+ SECRET_KEY="a" * 64,+ AISOC_CREDENTIAL_KEY="Z7t9fM3pZ8U_bN5oVxQYr1w2kLsHd4aJiTvE6cNxYpA=",+ )+ assert s.ENV == "development"+ assert s.ENVIRONMENT == "development"+ assert s.is_dev is True+++# ─── Integration: dev_auth.is_dev_mode honors env changes ────────────────+++def test_dev_auth_is_dev_mode_reads_live_env(monkeypatch):+ """``dev_auth.is_dev_mode()`` must reflect a mid-test env change.++ Catches the regression of caching the result with ``@lru_cache`` —+ we deliberately do NOT cache so monkeypatching works.+ """+ from app.api.v1 import dev_auth++ monkeypatch.setenv("ENV", "development")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert dev_auth.is_dev_mode() is True++ monkeypatch.setenv("ENV", "production")+ assert dev_auth.is_dev_mode() is False+++def test_dev_auth_is_dev_mode_excludes_test(monkeypatch):+ """``ENV=test`` must NOT trip the auth bypass even though it IS a dev env."""+ from app.api.v1 import dev_auth++ monkeypatch.setenv("ENV", "test")+ monkeypatch.delenv("ENVIRONMENT", raising=False)+ assert dev_auth.is_dev_mode() is False+++def test_dev_auth_is_dev_mode_honors_environment_fallback(monkeypatch):+ """Operator sets only ENVIRONMENT — dev_auth still picks it up."""+ from app.api.v1 import dev_auth++ monkeypatch.delenv("ENV", raising=False)+ monkeypatch.setenv("ENVIRONMENT", "development")+ assert dev_auth.is_dev_mode() is True++ monkeypatch.setenv("ENVIRONMENT", "production")+ assert dev_auth.is_dev_mode() is Falsediff --git a/services/ingest/internal/config/config.go b/services/ingest/internal/config/config.goindex 7a3be9b6f..aca31b569 100644--- a/services/ingest/internal/config/config.go+++ b/services/ingest/internal/config/config.go@@ -5,6 +5,8 @@ import ("fmt""os""strconv"++ "github.com/beenuar/aisoc/services/ingest/internal/envmode")// Config holds all ingest service configuration@@ -98,7 +100,13 @@ func Load() (*Config, error) {K8sAuditMaxBodyBytes: int64(mustGetEnvInt("K8S_AUDIT_MAX_BODY_BYTES", 16*1024*1024)),}- if cfg.JWTSecret == "" && os.Getenv("ENV") != "development" {+ // JWT_SECRET is required outside development-class environments. The+ // previous check exact-matched ``ENV == "development"`` only, so an+ // operator who set ``ENVIRONMENT=development`` (the alias the Python+ // API treats as equivalent) without also setting ``ENV`` would crash+ // here even though every other service treated their stack as dev.+ // envmode.IsDevRuntime closes that gap.+ if cfg.JWTSecret == "" && !envmode.IsDevRuntime() {return nil, fmt.Errorf("JWT_SECRET must be set in non-development environments")}diff --git a/services/ingest/internal/config/config_test.go b/services/ingest/internal/config/config_test.gonew file mode 100644index 000000000..ba75ed17b--- /dev/null+++ b/services/ingest/internal/config/config_test.go@@ -0,0 +1,130 @@+// Package config tests cover the JWT_SECRET dev-mode gate. The+// pre-H-8 implementation accepted only ``ENV=development``; these+// cases pin the new behaviour so ``ENVIRONMENT=development`` (the+// alias every other service honours) and the rest of the+// development-class allow-list keep working.+package config++import (+ "os"+ "testing"+)++// withEnv resets a process env var for the duration of a subtest,+// restoring whatever was there before. Tests run sequentially because+// they manipulate shared process state.+func withEnv(t *testing.T, key, value string) {+ t.Helper()+ prev, had := os.LookupEnv(key)+ if value == "" {+ if err := os.Unsetenv(key); err != nil {+ t.Fatalf("unset %s: %v", key, err)+ }+ } else {+ if err := os.Setenv(key, value); err != nil {+ t.Fatalf("set %s=%q: %v", key, value, err)+ }+ }+ t.Cleanup(func() {+ if had {+ _ = os.Setenv(key, prev)+ } else {+ _ = os.Unsetenv(key)+ }+ })+}++func TestLoad_DevEnvAllowsMissingJWTSecret(t *testing.T) {+ cases := []struct {+ name string+ envKey string+ envVal string+ }{+ {"ENV=development", "ENV", "development"},+ {"ENV=Dev (mixed case)", "ENV", "Dev"},+ {"ENV=local", "ENV", "local"},+ {"ENV=test", "ENV", "test"},+ {"ENVIRONMENT=development alias (no ENV)", "ENVIRONMENT", "development"},+ {"ENVIRONMENT=demo (no ENV)", "ENVIRONMENT", "demo"},+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ // Always clear both names before applying the case so a+ // stale ``ENV=production`` from the parent shell can't+ // swamp the ``ENVIRONMENT`` alias under test.+ withEnv(t, "ENV", "")+ withEnv(t, "ENVIRONMENT", "")+ withEnv(t, "JWT_SECRET", "")+ withEnv(t, tc.envKey, tc.envVal)++ cfg, err := Load()+ if err != nil {+ t.Fatalf("expected Load() to succeed in dev env %s=%s, got %v", tc.envKey, tc.envVal, err)+ }+ if cfg == nil {+ t.Fatalf("expected cfg, got nil")+ }+ if cfg.JWTSecret != "" {+ t.Fatalf("expected empty JWTSecret in dev, got %q", cfg.JWTSecret)+ }+ })+ }+}++func TestLoad_NonDevEnvRequiresJWTSecret(t *testing.T) {+ cases := []struct {+ name string+ envKey string+ envVal string+ }{+ {"ENV=production", "ENV", "production"},+ {"ENVIRONMENT=production alias", "ENVIRONMENT", "production"},+ {"ENV=staging", "ENV", "staging"},+ {"ENV and ENVIRONMENT both unset (defaults to non-dev)", "", ""},+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ withEnv(t, "ENV", "")+ withEnv(t, "ENVIRONMENT", "")+ withEnv(t, "JWT_SECRET", "")+ if tc.envKey != "" {+ withEnv(t, tc.envKey, tc.envVal)+ }++ _, err := Load()+ if err == nil {+ t.Fatalf("expected Load() to fail when JWT_SECRET is empty in env %s=%q", tc.envKey, tc.envVal)+ }+ })+ }+}++func TestLoad_NonDevEnvWithJWTSecretSucceeds(t *testing.T) {+ withEnv(t, "ENV", "production")+ withEnv(t, "ENVIRONMENT", "")+ withEnv(t, "JWT_SECRET", "test-secret-not-real")++ cfg, err := Load()+ if err != nil {+ t.Fatalf("Load() failed in production with JWT_SECRET set: %v", err)+ }+ if cfg.JWTSecret != "test-secret-not-real" {+ t.Fatalf("JWTSecret not threaded through: got %q", cfg.JWTSecret)+ }+}++func TestLoad_ENVTakesPrecedenceOverENVIRONMENT(t *testing.T) {+ // If both are set, ``ENV`` wins, matching envmode.Current(). The+ // pre-H-8 code already had this implicit behaviour because it+ // only looked at ENV; the test pins it now that ENVIRONMENT is+ // also consulted.+ withEnv(t, "ENV", "production")+ withEnv(t, "ENVIRONMENT", "development")+ withEnv(t, "JWT_SECRET", "")++ if _, err := Load(); err == nil {+ t.Fatalf("expected Load() to fail: ENV=production should override ENVIRONMENT=development")+ }+}diff --git a/services/ingest/internal/envmode/envmode.go b/services/ingest/internal/envmode/envmode.gonew file mode 100644index 000000000..bfc58eae4--- /dev/null+++ b/services/ingest/internal/envmode/envmode.go@@ -0,0 +1,62 @@+// Package envmode is the Go-side mirror of+// ``services/api/app/core/config.is_dev_env`` and friends.+//+// The same "dev mode" decision is made at multiple layers of the stack+// (Python API, Go ingest, Go enrichment), and the bugs we shipped pre-+// H-8 came from each layer hand-rolling its own slightly-different+// allow-list. This package is the single Go source of truth so a new+// dev alias (``staging-shadow``, ``preview``, ...) added in one place+// is visible to every other.+//+// Keep this in sync with+// ``services/api/app/core/config.DEV_ENVIRONMENTS``.+package envmode++import (+ "os"+ "strings"+)++// devEnvironments mirrors ``DEV_ENVIRONMENTS`` in the Python API+// settings. Update both at once if the canonical set changes.+var devEnvironments = map[string]struct{}{+ "development": {},+ "dev": {},+ "local": {},+ "demo": {},+ "test": {},+}++// Normalize lowercases and trims an env name so callers don't have to+// duplicate the same defensive cleanup. Exported so tests can hit it+// directly without depending on process state.+func Normalize(value string) string {+ return strings.ToLower(strings.TrimSpace(value))+}++// IsDev reports whether the supplied env name is a development-class+// environment. Matches Python ``is_dev_env``.+func IsDev(value string) bool {+ _, ok := devEnvironments[Normalize(value)]+ return ok+}++// Current reads the live process environment, preferring ``ENV`` and+// falling back to ``ENVIRONMENT``. The two names are treated as+// aliases of each other — the Python settings model reconciles them+// at boot, but Go services that only consult ``os.Getenv`` need to+// see both forms or they will disagree with the API.+func Current() string {+ if v := os.Getenv("ENV"); v != "" {+ return Normalize(v)+ }+ return Normalize(os.Getenv("ENVIRONMENT"))+}++// IsDevRuntime is the convenience wrapper most callers want:+// ``IsDev(Current())``. Use this instead of bespoke+// ``os.Getenv("ENV") == "development"`` checks scattered around the+// codebase.+func IsDevRuntime() bool {+ return IsDev(Current())+}diff --git a/services/ingest/internal/envmode/envmode_test.go b/services/ingest/internal/envmode/envmode_test.gonew file mode 100644index 000000000..66a46c970--- /dev/null+++ b/services/ingest/internal/envmode/envmode_test.go@@ -0,0 +1,80 @@+package envmode++import "testing"++func TestNormalize(t *testing.T) {+ cases := map[string]string{+ "": "",+ "Development": "development",+ " PRODUCTION ": "production",+ "\tDev\n": "dev",+ }+ for in, want := range cases {+ if got := Normalize(in); got != want {+ t.Errorf("Normalize(%q) = %q, want %q", in, got, want)+ }+ }+}++func TestIsDev(t *testing.T) {+ dev := []string{"development", "DEV", "local", "Demo", " test "}+ prod := []string{"production", "prod", "staging", "qa", "", "random"}++ for _, v := range dev {+ if !IsDev(v) {+ t.Errorf("IsDev(%q) = false, want true", v)+ }+ }+ for _, v := range prod {+ if IsDev(v) {+ t.Errorf("IsDev(%q) = true, want false", v)+ }+ }+}++func TestCurrentPrefersENVOverENVIRONMENT(t *testing.T) {+ t.Setenv("ENV", "production")+ t.Setenv("ENVIRONMENT", "development")+ if got := Current(); got != "production" {+ t.Fatalf("Current() = %q, want production (ENV wins)", got)+ }+}++func TestCurrentFallsBackToENVIRONMENT(t *testing.T) {+ t.Setenv("ENV", "")+ t.Setenv("ENVIRONMENT", "Development")+ if got := Current(); got != "development" {+ t.Fatalf("Current() = %q, want development (ENVIRONMENT fallback)", got)+ }+}++func TestCurrentEmptyWhenNeitherSet(t *testing.T) {+ t.Setenv("ENV", "")+ t.Setenv("ENVIRONMENT", "")+ if got := Current(); got != "" {+ t.Fatalf("Current() = %q, want empty string", got)+ }+}++func TestIsDevRuntime(t *testing.T) {+ t.Setenv("ENV", "")+ t.Setenv("ENVIRONMENT", "production")+ if IsDevRuntime() {+ t.Fatalf("IsDevRuntime() = true, want false for production via ENVIRONMENT alias")+ }++ t.Setenv("ENV", "demo")+ if !IsDevRuntime() {+ t.Fatalf("IsDevRuntime() = false, want true for ENV=demo")+ }++ // ENVIRONMENT=development with ENV unset is a previously-buggy case:+ // the old Go check only looked at ENV, so it would treat this as+ // production. The unified helper should treat it as dev to match+ // the Python settings model.+ t.Setenv("ENV", "")+ t.Setenv("ENVIRONMENT", "development")+ if !IsDevRuntime() {+ t.Fatalf("IsDevRuntime() = false, want true when only ENVIRONMENT=development is set")+ }+}diff --git a/services/ingest/main.go b/services/ingest/main.goindex 41b6333cb..b13b88f7e 100644--- a/services/ingest/main.go+++ b/services/ingest/main.go@@ -13,6 +13,7 @@ import ("time""github.com/beenuar/aisoc/services/ingest/internal/config"+ "github.com/beenuar/aisoc/services/ingest/internal/envmode""github.com/beenuar/aisoc/services/ingest/internal/handler""github.com/beenuar/aisoc/services/ingest/internal/inbox""github.com/beenuar/aisoc/services/ingest/internal/normalizer"@@ -24,9 +25,15 @@ import ()func main() {- // Configure structured JSON logging+ // Configure structured logging. Use the human-friendly console writer+ // in any dev-class environment (development, dev, local, demo, test) —+ // previously this exact-matched ``ENV == "development"`` only, so+ // ``ENVIRONMENT=development`` (the alias the Python API treats as+ // equivalent) silently flipped this service to JSON logs and made+ // local debugging confusing. envmode.IsDevRuntime keeps both layers+ // in lock-step.zerolog.TimeFieldFormat = zerolog.TimeFormatUnix- if os.Getenv("ENV") == "development" {+ if envmode.IsDevRuntime() {log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})}