feat(saas): multi-tenant isolation (Phase 1) + rate-limit/audit/org-scoped export (Phase 2)
Phase 1 (tenant isolation):
- g.org_id set on require_auth; assert_tenant()/current_org_id() choke-point helpers.
- Multi-org provisioning: POST /api/admin/users {new_org:true} (super_admin) creates a
new org + its first admin; GET /api/admin/orgs (super_admin sees all, admin own).
- Fixed latent create_org double-id bug (dict id != store key).
- test_saas_tenant.py: org2 admin blocked from org1 group (403), can't list org1
groups/users, sees only own org; super_admin sees all.
Phase 2 (hardening):
- Rate limit login (per-IP + per-username) + chat send (per-user) to protect LLM cost
and slow brute force; services/rate_limit.py (in-memory + disk, no external deps).
- Audit log data/audit/audit.jsonl on org.create, user.promote_super_admin, analytics.export.
- CSV export now org-scoped (admin exports only own org).
All 8 backend suites pass.
This commit is contained in:
@@ -16,6 +16,43 @@ def _store():
|
||||
return current_app.extensions["user_store"]
|
||||
|
||||
|
||||
def _log_audit(action: str, subject: str, *, detail: dict | None = None) -> None:
|
||||
"""Append a line to the audit log (sensitive platform actions)."""
|
||||
import json
|
||||
import time
|
||||
from ..config import Config
|
||||
|
||||
entry = {
|
||||
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
"actor": (current_user() or {}).get("username") or (current_user() or {}).get("id"),
|
||||
"action": action,
|
||||
"subject": subject,
|
||||
"detail": detail or {},
|
||||
}
|
||||
try:
|
||||
log_dir = Config.DATA_DIR / "audit"
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
with open(log_dir / "audit.jsonl", "a", encoding="utf-8") as fh:
|
||||
fh.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
# Audit logging must never break the request.
|
||||
pass
|
||||
|
||||
|
||||
@admin_bp.get("/orgs")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
def list_orgs():
|
||||
"""Platform view: all organizations (super_admin). Admin sees only their own."""
|
||||
actor = current_user()
|
||||
if actor.get("role") == "super_admin":
|
||||
orgs = list(_store().orgs.all())
|
||||
return jsonify({"orgs": orgs})
|
||||
# plain admin: only their own org
|
||||
org = _store().orgs.get_or_none(actor.get("org_id"))
|
||||
return jsonify({"orgs": [org] if org else []})
|
||||
|
||||
|
||||
@admin_bp.post("/users")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
@@ -36,13 +73,26 @@ def create_user():
|
||||
actor_role = current_user().get("role")
|
||||
if role in ("admin", "super_admin") and actor_role != "super_admin":
|
||||
raise ApiError("only super_admin can grant admin roles", 403)
|
||||
|
||||
# Multi-tenant: provisioning a brand-new org (super_admin only). The created user
|
||||
# becomes that org's first admin (tenant owner). Everything is isolated per org.
|
||||
new_org = bool(data.get("new_org"))
|
||||
if new_org:
|
||||
if actor_role != "super_admin":
|
||||
raise ApiError("only super_admin can create a new organization", 403)
|
||||
if role != "admin":
|
||||
raise ApiError("new-org first user must be role=admin", 400)
|
||||
org = _store().create_org(name or username)
|
||||
org_id = org["id"]
|
||||
_log_audit("org.create", org_id, detail={"name": name or username})
|
||||
|
||||
try:
|
||||
user = _store().create_user(
|
||||
org_id=org_id, username=username, password=password, name=name, role=role
|
||||
)
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc))
|
||||
return jsonify({"user": _store().public_user(user)}), 201
|
||||
return jsonify({"user": _store().public_user(user), "org_id": org_id}), 201
|
||||
|
||||
|
||||
@admin_bp.get("/users")
|
||||
@@ -76,6 +126,8 @@ def update_user(username: str):
|
||||
if actor.get("role") != "super_admin":
|
||||
raise ApiError("only super_admin can change roles")
|
||||
_store().set_role(username, role)
|
||||
if role == "super_admin":
|
||||
_log_audit("user.promote_super_admin", username)
|
||||
|
||||
if "active" in data:
|
||||
if actor.get("role") != "super_admin":
|
||||
|
||||
@@ -19,6 +19,30 @@ def _stores():
|
||||
}
|
||||
|
||||
|
||||
def _log_audit(action: str, subject: str, *, detail: dict | None = None) -> None:
|
||||
import json
|
||||
import time
|
||||
|
||||
from ..config import Config
|
||||
from .helpers import current_user as _cu
|
||||
|
||||
actor = _cu()
|
||||
entry = {
|
||||
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
"actor": (actor or {}).get("username") or (actor or {}).get("id"),
|
||||
"action": action,
|
||||
"subject": subject,
|
||||
"detail": detail or {},
|
||||
}
|
||||
try:
|
||||
log_dir = Config.DATA_DIR / "audit"
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
with open(log_dir / "audit.jsonl", "a", encoding="utf-8") as fh:
|
||||
fh.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _parse_date_iso(value: str | None, *, end: bool = False) -> str | None:
|
||||
"""Parse a YYYY-MM-DD into an ISO datetime bound for created_at filtering."""
|
||||
if not value:
|
||||
@@ -124,11 +148,14 @@ def export_csv():
|
||||
from flask import Response, current_app
|
||||
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
# Tenant: an admin exports only their own org's sessions; super_admin sees all.
|
||||
export_org = actor.get("org_id") if actor.get("role") != "super_admin" else None
|
||||
users = {}
|
||||
try:
|
||||
user_store = current_app.extensions.get("user_store")
|
||||
if user_store and hasattr(user_store, "list_users"):
|
||||
for rec in user_store.list_users():
|
||||
for rec in user_store.list_users(org_id=export_org):
|
||||
users[rec.get("id") or rec.get("username")] = rec.get("username") or rec.get("id")
|
||||
except Exception:
|
||||
pass
|
||||
@@ -138,9 +165,9 @@ def export_csv():
|
||||
grp_store = s["groups"]
|
||||
sess_store = s["sessions"]
|
||||
if hasattr(grp_store, "groups"):
|
||||
group_ids = [g.get("id") for g in grp_store.groups.all()]
|
||||
group_ids = [g.get("id") for g in grp_store.groups.all() if export_org is None or g.get("org_id") == export_org]
|
||||
elif hasattr(grp_store, "all"):
|
||||
group_ids = [g.get("id") for g in grp_store.all()]
|
||||
group_ids = [g.get("id") for g in grp_store.all() if export_org is None or g.get("org_id") == export_org]
|
||||
else:
|
||||
group_ids = []
|
||||
for gid in group_ids:
|
||||
@@ -154,6 +181,8 @@ def export_csv():
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_log_audit("analytics.export", actor.get("org_id") or "?", detail={"rows": len(sessions)})
|
||||
|
||||
buf = io.StringIO()
|
||||
w = csv.writer(buf)
|
||||
w.writerow(["username", "persona", "scenario", "outcome", "score", "created_at"])
|
||||
|
||||
@@ -27,6 +27,14 @@ def login():
|
||||
password = data.get("password") or ""
|
||||
if not username or not password:
|
||||
raise ApiError("username and password are required")
|
||||
# Slow down brute-force / abuse: per-IP + per-username window.
|
||||
from ..services.rate_limit import check as ratelimit
|
||||
|
||||
client_ip = request.remote_addr or "?"
|
||||
if not ratelimit("login:ip", client_ip, limit=15, window=300):
|
||||
raise ApiError("too many attempts, try again later", 429)
|
||||
if not ratelimit("login:user", username, limit=8, window=300):
|
||||
raise ApiError("too many attempts, try again later", 429)
|
||||
try:
|
||||
user = _store().verify(username, password)
|
||||
token = _store().issue_token(user)
|
||||
|
||||
@@ -220,6 +220,13 @@ def send_message(gid: str, pid: str):
|
||||
if len(text) > 2000:
|
||||
raise ApiError("message too long")
|
||||
|
||||
# Protect LLM cost: per-user chat-send window.
|
||||
from ..services.rate_limit import check as ratelimit
|
||||
|
||||
actor_rl = current_user()
|
||||
if not ratelimit("chat:user", actor_rl.get("id") or actor_rl.get("username") or "?", limit=30, window=60):
|
||||
raise ApiError("slow down — too many messages", 429)
|
||||
|
||||
group = s["groups"].get_or_none(gid)
|
||||
persona = s["groups"].get_persona(gid, pid) if group else None
|
||||
if not group or not persona:
|
||||
|
||||
@@ -27,6 +27,20 @@ def current_user() -> dict[str, Any]:
|
||||
return g.user
|
||||
|
||||
|
||||
def current_org_id() -> str:
|
||||
return getattr(g, "org_id", None) or (g.user or {}).get("org_id") or "org-default"
|
||||
|
||||
|
||||
def assert_tenant(org_id: str | None) -> None:
|
||||
"""Raise 403 if the object's org does not belong to the actor's tenant.
|
||||
|
||||
super_admin is the platform operator and is exempt (can see all orgs)."""
|
||||
if (g.user or {}).get("role") == "super_admin":
|
||||
return
|
||||
if (org_id or "org-default") != current_org_id():
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
|
||||
def require_auth(fn: Callable) -> Callable:
|
||||
@functools.wraps(fn)
|
||||
def wrapper(*args, **kwargs):
|
||||
@@ -43,6 +57,9 @@ def require_auth(fn: Callable) -> Callable:
|
||||
raise ApiError("account is inactive", 401)
|
||||
g.user = user
|
||||
g.token_payload = payload
|
||||
# Tenant context: every request carries the actor's org id so downstream guards
|
||||
# can enforce per-org isolation without re-reading the user each time.
|
||||
g.org_id = (user or {}).get("org_id") or "org-default"
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user