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
test(entitlements): prove enforcement live, in both failure directions
Beenu Arorad7b6c36f9a1b
diff --git a/.github/workflows/entitlements-live.yml b/.github/workflows/entitlements-live.ymlnew file mode 100644index 000000000..036fdcfb6--- /dev/null+++ b/.github/workflows/entitlements-live.yml@@ -0,0 +1,213 @@+# Opt-in limit enforcement — against real Postgres.+#+# Why live and not offline+# ------------------------+# Every decision reads rows: the opt-in table, the per-tenant+# `tenants.limits` override, and a usage count over real rows. A double+# answers whatever the test wrote into it.+#+# The counts are the specific risk. `measure_usage` binds the tenant as a+# typed `UUID`; it used to bind `str(uuid)`, which only Postgres coerces,+# so every quota count returned zero on any other driver — and a quota+# reading zero is the direction nobody investigates.+#+# `ENVIRONMENT=test`, never `development`, which is in+# `AUTH_BYPASS_ENVIRONMENTS`. The connection is the DML-only `aisoc_app`+# role, so the RLS policy on the opt-in table is actually in force.+#+# Both negative controls matter, and the second is the one that is usually+# missing. Removing the guard must fail the suite; **so must leaving it+# stuck closed**, because a control that refuses everything passes every+# "the limit is enforced" assertion perfectly, and that is precisely how a+# fail-closed control shipped here withholding every grant on every+# deployment while its negative tests stayed green.+#+# No `paths:` filter. A quota that silently stops refusing is indis-+# tinguishable from one nobody configured, and a filtered gate is absent+# exactly when something unexpected moved.+name: Entitlement enforcement (live)++on:+ push:+ branches: [main]+ pull_request:+ workflow_dispatch:++concurrency:+ group: entitlements-live-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}++permissions:+ contents: read++jobs:+ entitlements-live:+ name: An enforced limit refuses, and an unenforced one does not+ runs-on: ubuntu-latest+ timeout-minutes: 20++ services:+ postgres:+ image: public.ecr.aws/docker/library/postgres:16-alpine+ env:+ POSTGRES_USER: aisoc+ POSTGRES_PASSWORD: aisoc_ci_only+ POSTGRES_DB: aisoc+ ports: ["5432:5432"]+ options: >-+ --health-cmd "pg_isready -U aisoc"+ --health-interval 5s+ --health-timeout 5s+ --health-retries 20++ env:+ ENT_DSN: postgresql+asyncpg://aisoc_app:aisoc_app_ci_only@localhost:5432/aisoc+ PGPASSWORD: aisoc_ci_only+ ENVIRONMENT: test+ OTEL_SDK_DISABLED: "true"++ steps:+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7++ - uses: actions/setup-python@e9d6f990972a57673cdb72ec29e19d42ba28880f # v6+ with:+ python-version: "3.11"++ - name: Install api from its lockfile+ working-directory: services/api+ run: |+ set -euo pipefail+ sudo apt-get update -qq && sudo apt-get install -y -qq libxmlsec1-dev pkg-config+ python -m pip install --quiet --upgrade pip "poetry==2.4.1"+ poetry config virtualenvs.create false+ poetry install --no-root --no-interaction+ python -m pip install --quiet pytest pytest-asyncio++ - name: Apply every migration, in order+ run: |+ set -euo pipefail+ for f in services/api/migrations/*.sql; do+ psql -h localhost -U aisoc -d aisoc -v ON_ERROR_STOP=1 -q -f "$f"+ done++ - name: Give the runtime role its password+ run: |+ set -euo pipefail+ psql -h localhost -U aisoc -d aisoc -v ON_ERROR_STOP=1 -q -c \+ "ALTER ROLE aisoc_app WITH PASSWORD 'aisoc_app_ci_only';"+ psql -h localhost -U aisoc -d aisoc -v ON_ERROR_STOP=1 -q -c \+ "GRANT USAGE ON SCHEMA public TO aisoc_app;"++ # No migration may write an opt-in row. Enforcement arriving switched+ # on would turn every ceiling an operator had written for *reporting*+ # into a refusal on upgrade, and that is a one-line regression a+ # future migration could introduce without anyone noticing.+ - name: A fresh database enforces nothing+ run: |+ set -euo pipefail+ rows=$(psql -h localhost -U aisoc -d aisoc -tAc \+ "SELECT count(*) FROM aisoc_tenant_entitlement_enforcement;")+ if [ "$rows" != "0" ]; then+ echo "::error::a migration wrote $rows enforcement row(s); enforcement must be opt-in"+ exit 1+ fi+ echo "OK: a fresh database enforces no limit for any tenant."++ - name: Opt-in enforcement, both directions+ working-directory: services/api+ env:+ ISOLATION_ENTITLEMENTS_DSN: ${{ env.ENT_DSN }}+ DATABASE_URL: ${{ env.ENT_DSN }}+ run: |+ set -euo pipefail+ python -m pytest ../../tests/isolation/test_entitlement_enforcement_live.py \+ -v --tb=short --junitxml=/tmp/entitlements.xml++ - name: Nothing skipped, and something ran+ run: |+ set -euo pipefail+ python - <<'PY'+ import sys, xml.etree.ElementTree as ET++ root = ET.parse("/tmp/entitlements.xml").getroot()+ tests = skipped = errors = 0+ for s in root.iter("testsuite"):+ tests += int(s.get("tests", 0))+ skipped += int(s.get("skipped", 0))+ errors += int(s.get("errors", 0))+ print(f"tests={tests} skipped={skipped} errors={errors}")+ if tests == 0:+ print("::error::the live suite collected nothing; a job that ran no case is not evidence")+ sys.exit(1)+ if skipped:+ print("::error::the live suite skipped a case; in a job that provides the DSN a skip "+ "means the environment is not what this workflow claims it is")+ sys.exit(1)+ if errors:+ print("::error::the live suite errored outside an assertion")+ sys.exit(1)+ PY++ - name: The suite fails when the guard never refuses+ working-directory: services/api+ env:+ ISOLATION_ENTITLEMENTS_DSN: ${{ env.ENT_DSN }}+ DATABASE_URL: ${{ env.ENT_DSN }}+ run: |+ set -euo pipefail+ cp app/services/entitlements.py /tmp/entitlements.bak+ python - <<'PY'+ import pathlib, sys++ p = pathlib.Path("app/services/entitlements.py")+ s = p.read_text()+ needle = " if key not in await enforced_keys(db, tenant_id):\n return\n"+ if needle not in s:+ print("negative control could not find the opt-in check to remove; "+ "assert_within_limit has been reshaped and this control proves nothing")+ sys.exit(1)+ p.write_text(s.replace(needle, " return\n", 1))+ PY+ if python -m pytest ../../tests/isolation/test_entitlement_enforcement_live.py -q >/dev/null 2>&1; then+ cp /tmp/entitlements.bak app/services/entitlements.py+ echo "::error::the suite passed with a guard that never refuses"+ exit 1+ fi+ cp /tmp/entitlements.bak app/services/entitlements.py+ echo "OK: a guard that never refuses makes the suite fail, as it must."++ # The control that is usually missing. A guard stuck closed refuses+ # everything and satisfies every "the limit is enforced" assertion,+ # which is how a fail-closed control shipped here withholding every+ # grant on every deployment with its negative tests green.+ - name: The suite fails when the guard refuses everything+ working-directory: services/api+ env:+ ISOLATION_ENTITLEMENTS_DSN: ${{ env.ENT_DSN }}+ DATABASE_URL: ${{ env.ENT_DSN }}+ run: |+ set -euo pipefail+ cp app/services/entitlements.py /tmp/entitlements.bak+ python - <<'PY'+ import pathlib, sys++ p = pathlib.Path("app/services/entitlements.py")+ s = p.read_text()+ opt_in = " if key not in await enforced_keys(db, tenant_id):\n return\n"+ no_ceiling = " limit = limit_for(tenant_limits, key)\n if limit is None:\n return\n"+ if opt_in not in s or no_ceiling not in s:+ print("negative control could not find both early returns to remove; "+ "assert_within_limit has been reshaped and this control proves nothing")+ sys.exit(1)+ s = s.replace(opt_in, "", 1)+ s = s.replace(no_ceiling, " limit = limit_for(tenant_limits, key) or 0\n", 1)+ p.write_text(s)+ PY+ if python -m pytest ../../tests/isolation/test_entitlement_enforcement_live.py -q >/dev/null 2>&1; then+ cp /tmp/entitlements.bak app/services/entitlements.py+ echo "::error::the suite passed with a guard stuck closed"+ exit 1+ fi+ cp /tmp/entitlements.bak app/services/entitlements.py+ echo "OK: a guard stuck closed makes the suite fail, as it must."+ python -m pytest ../../tests/isolation/test_entitlement_enforcement_live.py -qdiff --git a/tests/isolation/test_entitlement_enforcement_live.py b/tests/isolation/test_entitlement_enforcement_live.pynew file mode 100644index 000000000..58aaa57e6--- /dev/null+++ b/tests/isolation/test_entitlement_enforcement_live.py@@ -0,0 +1,287 @@+"""A configured limit refuses work only when a tenant opted in — live.++Depth 8.1, parity 4.5's second bullet.++Why this suite is live rather than offline+------------------------------------------+Every decision here reads rows: the opt-in table, the per-tenant+`tenants.limits` override, and a usage count over real rows. A double+answers whatever the test wrote into it, which is the shape that has+shipped defects in this repository repeatedly — a fake row answering for+columns a table does not have, a fake store matching whatever key it was+handed.++The counts are the specific risk. `measure_usage` binds the tenant as a+typed `UUID`; it used to bind `str(uuid)`, which only Postgres coerces,+so every quota count returned zero in any harness that was not Postgres+— and a quota reading zero is the direction nobody investigates.++`ENVIRONMENT=test`, never `development`, which is in+`AUTH_BYPASS_ENVIRONMENTS`.++The positive control+--------------------+`test_a_tenant_with_room_is_not_refused` and+`test_a_tenant_that_did_not_opt_in_is_not_refused` are not padding. A+guard stuck closed refuses everything and passes every "the limit is+enforced" assertion perfectly, which is exactly how a fail-closed control+shipped here withholding every grant on every deployment while its+negative tests were green. Half of this suite exists to catch that.+"""++from __future__ import annotations++import os+import uuid++import pytest+import pytest_asyncio++pytestmark = [+ pytest.mark.asyncio(loop_scope="module"),+ pytest.mark.skipif(+ not os.environ.get("ISOLATION_ENTITLEMENTS_DSN", "").strip(),+ reason="ISOLATION_ENTITLEMENTS_DSN is not set; this suite needs a live Postgres",+ ),+]+++def _dsn() -> str:+ value = os.environ.get("ISOLATION_ENTITLEMENTS_DSN", "").strip()+ if not value:+ pytest.skip("ISOLATION_ENTITLEMENTS_DSN is not set")+ if value.startswith("postgresql://"):+ return value.replace("postgresql://", "postgresql+asyncpg://", 1)+ return value+++@pytest_asyncio.fixture(scope="module", loop_scope="module")+async def sessionmaker_():+ from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine++ engine = create_async_engine(_dsn())+ try:+ yield async_sessionmaker(engine, expire_on_commit=False)+ finally:+ await engine.dispose()+++async def _make_tenant(session, *, connectors: int, limit: object | None) -> uuid.UUID:+ """A tenant with *connectors* enabled connectors and an optional ceiling."""+ import json++ from sqlalchemy import text++ tenant_id = uuid.uuid4()+ tag = str(tenant_id)[:8]+ limits = {} if limit is None else {"connectors": limit}+ await session.execute(+ text("INSERT INTO tenants (id, name, slug, limits) VALUES (:id, :n, :s, CAST(:l AS jsonb))"),+ {"id": str(tenant_id), "n": f"ent-{tag}", "s": f"ent-{tag}", "l": json.dumps(limits)},+ )+ for n in range(connectors):+ await session.execute(+ text(+ "INSERT INTO connectors (id, tenant_id, name, connector_type, category, is_enabled) "+ "VALUES (:id, :t, :n, 'aws_cloudtrail', 'cloud', TRUE)"+ ),+ {"id": str(uuid.uuid4()), "t": str(tenant_id), "n": f"c{n}"},+ )+ await session.commit()+ return tenant_id+++async def _opt_in(session, tenant_id: uuid.UUID, key: str) -> None:+ from sqlalchemy import text++ await session.execute(+ text("INSERT INTO aisoc_tenant_entitlement_enforcement (tenant_id, limit_key, enforce) VALUES (:t, :k, TRUE)"),+ {"t": str(tenant_id), "k": key},+ )+ await session.commit()+++async def _drop(session, tenant_id: uuid.UUID) -> None:+ from sqlalchemy import text++ await session.execute(text("DELETE FROM connectors WHERE tenant_id = :t"), {"t": str(tenant_id)})+ await session.execute(text("DELETE FROM aisoc_tenant_entitlement_enforcement WHERE tenant_id = :t"), {"t": str(tenant_id)})+ await session.execute(text("DELETE FROM tenants WHERE id = :t"), {"t": str(tenant_id)})+ await session.commit()+++class TestTheCountIsReal:+ async def test_usage_is_measured_from_rows(self, sessionmaker_):+ """`measure_usage` used to bind the tenant as a string.++ Only Postgres coerces that against a `UUID` column, so every count+ returned zero on any other driver. Asserting a non-zero count+ against rows this test inserted — rather than against a second+ call to the thing being measured — is what catches it.+ """+ from app.services.entitlements import measure_usage++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=3, limit=None)+ try:+ usage = await measure_usage(session, tenant_id)+ assert usage["connectors"] == 3+ finally:+ await _drop(session, tenant_id)+++class TestEnforcementIsOptIn:+ async def test_a_tenant_that_did_not_opt_in_is_not_refused(self, sessionmaker_):+ """The property that makes this safe to ship.++ `tenants.limits` has been a reporting input for as long as it has+ existed, so a ceiling already written there was never chosen as a+ hard cap. A tenant well over one, with no row in the opt-in table,+ must still be allowed through.+ """+ from app.services.entitlements import assert_within_limit++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=5, limit=1)+ try:+ await assert_within_limit(session, tenant_id, {"connectors": 1}, "connectors")+ finally:+ await _drop(session, tenant_id)++ async def test_an_opted_in_tenant_over_its_limit_is_refused(self, sessionmaker_):+ from app.services.entitlements import LimitExceeded, assert_within_limit++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=5, limit=2)+ await _opt_in(session, tenant_id, "connectors")+ try:+ with pytest.raises(LimitExceeded) as caught:+ await assert_within_limit(session, tenant_id, {"connectors": 2}, "connectors")+ # The numbers, not a bare message: "quota exceeded" sends an+ # operator to a support channel, "5 of 2" sends them to the+ # setting.+ assert caught.value.used == 5+ assert caught.value.limit == 2+ assert caught.value.key == "connectors"+ finally:+ await _drop(session, tenant_id)+++class TestThePositiveControls:+ """A guard stuck closed passes every negative assertion above."""++ async def test_a_tenant_with_room_is_not_refused(self, sessionmaker_):+ from app.services.entitlements import assert_within_limit++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=1, limit=10)+ await _opt_in(session, tenant_id, "connectors")+ try:+ await assert_within_limit(session, tenant_id, {"connectors": 10}, "connectors")+ finally:+ await _drop(session, tenant_id)++ async def test_an_opt_in_on_one_key_does_not_enforce_another(self, sessionmaker_):+ """Per key, not per tenant.++ Opting in to `connectors` must not start refusing seats. The two+ are stored in one table and a predicate that forgot `limit_key`+ would enforce every key the moment a tenant enabled one.+ """+ from app.services.entitlements import assert_within_limit++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=5, limit=1)+ await _opt_in(session, tenant_id, "connectors")+ try:+ # `seats` is over nothing here, but the point is that the+ # key is not in the enforced set at all.+ await assert_within_limit(session, tenant_id, {"seats": 0}, "seats")+ finally:+ await _drop(session, tenant_id)++ async def test_no_ceiling_configured_is_never_a_refusal(self, sessionmaker_):+ """AiSOC ships uncapped, and opting in must not invent a ceiling."""+ from app.services.entitlements import assert_within_limit++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=5, limit=None)+ await _opt_in(session, tenant_id, "connectors")+ try:+ await assert_within_limit(session, tenant_id, {}, "connectors")+ await assert_within_limit(session, tenant_id, {"connectors": "unlimited"}, "connectors")+ finally:+ await _drop(session, tenant_id)+++class TestOneTenantsPolicyDoesNotReachAnother:+ async def test_enforcement_is_scoped_to_its_tenant(self, sessionmaker_):+ from app.services.entitlements import assert_within_limit, enforced_keys++ async with sessionmaker_() as session:+ strict = await _make_tenant(session, connectors=5, limit=1)+ relaxed = await _make_tenant(session, connectors=5, limit=1)+ await _opt_in(session, strict, "connectors")+ try:+ assert await enforced_keys(session, strict) == frozenset({"connectors"})+ assert await enforced_keys(session, relaxed) == frozenset()+ # The neighbour is over the same ceiling and still allowed.+ await assert_within_limit(session, relaxed, {"connectors": 1}, "connectors")+ finally:+ await _drop(session, strict)+ await _drop(session, relaxed)+++class TestTheVocabularyIsChecked:+ async def test_an_unknown_limit_key_is_refused_by_the_database(self, sessionmaker_):+ """A typo stored here would match no key and enforce nothing.++ Checked in SQL as well as in the route, because the route is not+ the only thing that can write this table.+ """+ from sqlalchemy import text+ from sqlalchemy.exc import DBAPIError, IntegrityError++ async with sessionmaker_() as session:+ tenant_id = await _make_tenant(session, connectors=0, limit=None)+ try:+ with pytest.raises((IntegrityError, DBAPIError)):+ await session.execute(+ text(+ "INSERT INTO aisoc_tenant_entitlement_enforcement "+ "(tenant_id, limit_key, enforce) VALUES (:t, 'connetcors', TRUE)"+ ),+ {"t": str(tenant_id)},+ )+ await session.commit()+ await session.rollback()+ finally:+ await _drop(session, tenant_id)++ async def test_the_check_constraint_and_the_python_vocabulary_agree(self, sessionmaker_):+ """Two places list the keys, so they are compared rather than trusted.++ The constraint cannot reference a table of keys -- there is not+ one -- so adding a key means editing both. This is the gate that+ makes forgetting one a failure rather than a silent half-enforced+ vocabulary.+ """+ import re++ from app.services.entitlements import LIMIT_KEYS_BY_NAME+ from sqlalchemy import text++ async with sessionmaker_() as session:+ definition = (+ await session.execute(+ text(+ "SELECT pg_get_constraintdef(oid) FROM pg_constraint "+ " WHERE conname = 'aisoc_tenant_entitlement_enforcement_key_known'"+ )+ )+ ).scalar_one()+ in_sql = set(re.findall(r"'([a-z_]+)'", definition))+ assert in_sql == set(LIMIT_KEYS_BY_NAME), (+ f"the CHECK constraint allows {sorted(in_sql)} while LIMIT_KEYS defines "+ f"{sorted(LIMIT_KEYS_BY_NAME)}; a key in one and not the other is half enforced"+ )