访问密钥与服务账号的后端(下一轮第 2 波 W2-d,细案 04 A1–A6、总方案 X8)

外部系统(门户、OA、BI)和个人脚本要用一个身份调接口,以前只能拿个人会话 cookie 去模拟。这次加访问密钥:

- 签发(A1):users.kind 区分人和服务账号(svc- 开头、没有口令、不能登录、角色固定使用者,管理员建);access_keys 只存
  sha256 和开头一截,明文 agl_ 加 32 字节随机串只显示一次;到期必填(最长 365 天、缺省 90 天)、可撤销、可轮换(新钥 + 旧钥
  24 小时后到期);个人访问密钥由管理员开关 auth.personal_keys 管,缺省关,关掉时已有的个人密钥一并不认
- 认人与门禁(A2):Principal 加 via / key_id / key_name / scopes / app_ids(全部带默认值);中间件认 Bearer agl_…,不看
  cookie、不查 CSRF、不看 X-Actor,认不出的各回 401 带各自的 code、绝不退回匿名;WebSocket 带密钥一律拒;关登录时 409
  keys_need_login。KEY_ROUTES(方法 + 路由模板 → scope)一张表,FastAPI(dependencies=[Depends(key_gate)]) 挂一次;不在表里的
  403 key_route_forbidden,缺 scope、限定应用、个人应用和试运行各有各的 403;每把密钥每分钟 120 次(429 + Retry-After)
- 封顶(A3):按密钥认出来的一律是使用者(拥有者现在的组),现有的使用者规则和遮罩全部自动生效,管理员的密钥也一样
- 写操作(A4):发起入口、刷新视图必须带 Idempotency-Key,api_receipts 按(拥有者, 键)去重,轮换后照样认得出;状态机从
  process/hooks.py 抽到 process/receipts.py,钩子和密钥共用(钩子现有测试原样通过),另按请求指纹拒掉同一个键办另一件事;
  有文件控件的入口 422;回复待办只给个人密钥、只限审批 / 补充输入 / 编辑稿件;run.origin、task.responded 记 via=key、哪把密钥
- 视图数据(A5):GET /api/apps/{id}/views/{view}/data(agentlab.viewdata/1),印章、清单哈希、口径卡指标原值和渲染文字,
  遮罩的 value 为 null(和页面同一个判断),按需视图只读缓存、不发起运行,只给正式运行
- 留痕(A6):建 / 撤销 / 轮换密钥、建 / 停用服务账号、开关个人密钥写审计;请求记录进程内缓冲、调度循环一拍里批量写、
  保留 90 天;last_used_at 至少隔 1 分钟写一次;钩子收件记录接口(manage),验签失败只在进程内按钩子计数
- 名录与派单:服务账号在名录里标出,岗位绑定、选人候选、提交审核、改派里排除,应用授权照收;服务账号发起的实例里派给发起人
  的待办交给任何人
- 接口说明(A7):GET /api/access-keys/reference 从 KEY_ROUTES 生成(要登录)

测试:签发与各种 401、门禁合约(遍历全站路由)、CSRF 与身份来源、封顶(管理员密钥取工件也是遮过的)、幂等(含轮换后、
半路杀进程)、留痕、待办种类、视图数据(真跑的遮罩会员库)、限流、钩子收件记录。
This commit is contained in:
ThearchyHelios
2026-10-16 13:48:09 +08:00
parent 301a599e9c
commit 19329c873e
25 changed files with 2794 additions and 87 deletions
+62 -2
View File
@@ -17,16 +17,21 @@ CSRF = {"X-AgentLab-Request": "1"}
async def reset() -> None:
from app.auth import SETTING_KEY, remember_settings, sessions
from app.auth import SETTING_KEY, keys, remember_settings, sessions
from app.db.models import AuditEvent, AuthSession, Group, GroupMember, Setting, User
from app.db.models_apps import ApiReceipt
from app.db.models_identity import AccessKey, AccessKeyRequest
async with SessionLocal() as session:
for model in (AuthSession, GroupMember, Group, User, AuditEvent):
for model in (AccessKeyRequest, ApiReceipt, AccessKey, AuthSession, GroupMember, Group, User, AuditEvent):
await session.execute(delete(model))
await session.execute(delete(Setting).where(Setting.key == SETTING_KEY))
await session.commit()
remember_settings({})
sessions._failures.clear()
# 访问密钥的进程内状态:限流的桶、还没写库的请求记录
keys._BUCKETS.clear()
keys._PENDING.clear()
def client(**headers: str) -> AsyncClient:
@@ -62,3 +67,58 @@ async def login(username: str, password: str = "password-123") -> AsyncClient:
r = await c.post("/api/auth/login", json={"username": username, "password": password})
assert r.status_code == 200, r.text
return c
def keyed(secret: str, **headers: str) -> AsyncClient:
"""带访问密钥的客户端:只有 Authorization 头,不带 CSRF 头、不带 cookie(外部系统就是这样调的)。"""
from app.main import app
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test",
headers={"Authorization": f"Bearer {secret}", **headers})
async def make_key(owner: Any, *, scopes: tuple[str, ...] = ("apps.read", "apps.run", "tasks.read", "tasks.respond",
"reports.read"),
app_ids: list[str] | None = None, days: float = 90, name: str = "测试密钥") -> tuple[Any, str]:
"""直接在库里签一把密钥(不经接口),交回 (行, 明文)。owner 是 User 行。"""
from datetime import timedelta
from app.auth import keys
from app.db.base import utcnow
from app.db.models_identity import AccessKey
secret = keys.new_secret()
async with SessionLocal() as session:
row = AccessKey(owner_user_id=owner.id, name=name, key_hash=keys.key_hash(secret), prefix=keys.prefix_of(secret),
scopes=list(scopes), app_ids=app_ids, expires_at=utcnow() + timedelta(days=days))
session.add(row)
await session.commit()
await session.refresh(row)
return row, secret
async def make_service(username: str = "svc-portal", **extra: Any) -> Any:
from app.db.models import User
async with SessionLocal() as session:
user = User(username=username, display_name=username, role="user", kind="service", **extra)
session.add(user)
await session.commit()
await session.refresh(user)
return user
async def personal_keys(on: bool = True) -> None:
"""个人访问密钥的开关(登录设置里的 personal_keys),直接改缓存和库。"""
from app.auth import PERSONAL_KEYS, SETTING_KEY, load_settings, remember_settings
from app.db.models import Setting
value = {**await load_settings(force=True), PERSONAL_KEYS: on}
async with SessionLocal() as session:
row = await session.get(Setting, SETTING_KEY)
if row is None:
session.add(Setting(key=SETTING_KEY, value=value))
else:
row.value = value
await session.commit()
remember_settings(value)
+173
View File
@@ -0,0 +1,173 @@
"""访问密钥的门禁(docs/design/next-round/04-对外集成与触达.md A2、A7):KEY_ROUTES 一张表,挂在全站一次。
合约测试遍历 app.routes:不在表里的 /api 接口,用密钥请求一律 403(key_route_forbidden);表里的每一行都对得上一个真实的
路由(改了路由、漏改表会在这里报出来)。另守:缺 scope 的 403(key_scope_missing);限定了应用时别的应用 403
(key_app_forbidden);个人应用、个人应用的刷新、蓝图的试运行一律 403(key_app_personal);带密钥连 WebSocket 被拒;
页面和 FastAPI 自带的接口说明不对密钥开放;接口说明(/api/access-keys/reference)和放行的接口出自同一张表。
"""
from __future__ import annotations
import re
import pytest
from fastapi.routing import APIRoute, APIWebSocketRoute
from sqlalchemy import delete
from app.auth import keys
from app.db.base import SessionLocal
from app.db.models import App, AppGrant, AppVersion, Run
from tests.fakes import auth_env
@pytest.fixture(autouse=True)
async def clean():
await auth_env.reset()
await auth_env.turn_on_local()
try:
yield
finally:
await auth_env.reset()
def _filled(path: str) -> str:
return re.sub(r"\{[^}]+\}", "x", path)
def _contexts():
"""全站的路由(FastAPI 0.14x 起 include_router 不再把路由摊平进 app.routes,按它给的路由上下文遍历)。"""
from fastapi.routing import iter_route_contexts
from app.main import app
return list(iter_route_contexts(app.routes))
async def test_every_route_not_in_the_table_is_forbidden_for_keys(monkeypatch):
monkeypatch.setattr(keys, "RATE_PER_MINUTE", 10_000) # 三百多个接口一口气调完,不让限流挡在门禁前面
svc = await auth_env.make_service()
_, secret = await auth_env.make_key(svc, scopes=keys.SCOPES)
client = auth_env.keyed(secret)
seen: set[tuple[str, str]] = set()
checked = 0
for ctx in _contexts():
route = ctx.original_route
if not isinstance(route, APIRoute) or not ctx.path.startswith("/api/"):
continue
# 门禁按 scope['route'].path(原路由的模板)比对:include_router 不能再加前缀,不然两边对不上
assert route.path == ctx.path, f"{ctx.path} 的原路由模板是 {route.path}"
for method in sorted(ctx.methods or ()):
seen.add((method, ctx.path))
if (method, ctx.path) in keys.KEY_ROUTES:
continue
r = await client.request(method, _filled(ctx.path), json={} if method in ("POST", "PUT", "PATCH") else None)
assert r.status_code == 403 and r.json().get("code") == "key_route_forbidden", \
f"{method} {ctx.path} 用密钥调回了 {r.status_code} {r.text[:120]}"
checked += 1
assert checked > 200, "真遍历到了全部接口"
stale = set(keys.KEY_ROUTES) - seen
assert not stale, f"KEY_ROUTES 里有对不上路由的行:{stale}"
async def test_routes_in_the_table_need_their_scope():
svc = await auth_env.make_service()
_, secret = await auth_env.make_key(svc, scopes=("apps.read",))
c = auth_env.keyed(secret)
assert (await c.get("/api/apps")).status_code == 200
r = await c.get("/api/tasks")
assert r.status_code == 403 and r.json()["code"] == "key_scope_missing"
r = await c.post("/api/apps/x/entries/y", json={"values": {}}, headers={"Idempotency-Key": "k"})
assert r.json()["code"] == "key_scope_missing", "发起要 apps.run"
assert (await c.get("/api/auth/me")).status_code == 200, "me 不用申请"
assert (await c.get("/api/health")).status_code == 200
async def test_pages_docs_and_websockets_are_closed_to_keys():
from app.auth.middleware import AuthMiddleware
from app.main import app
svc = await auth_env.make_service()
_, secret = await auth_env.make_key(svc, scopes=keys.SCOPES)
c = auth_env.keyed(secret)
for path in ("/docs", "/openapi.json", "/apps"):
r = await c.get(path)
assert r.status_code == 403 and r.json()["code"] == "key_route_forbidden", path
sent: list[dict] = []
async def receive():
return {"type": "websocket.connect"}
async def send(message):
sent.append(message)
scope = {"type": "websocket", "path": "/api/runs/x/stream", "headers": [(b"authorization", f"Bearer {secret}".encode())],
"query_string": b""}
await AuthMiddleware(app.router)(scope, receive, send)
assert sent == [{"type": "websocket.close", "code": 1008}], "带密钥的握手前就关"
async def _apps() -> dict[str, str]:
async with SessionLocal() as s:
shared = App(name="周报", published_version=1, created_by="root")
other = App(name="报销", published_version=1, created_by="root")
mine = App(name="我的看板", scope="personal", owner="pia")
s.add_all([shared, other, mine])
await s.flush()
for app in (shared, other):
s.add(AppVersion(app_id=app.id, version=1, spec={"format": "agentlab.app/1", "pages": []}, spec_hash="h"))
s.add(AppGrant(app_id=app.id, principal_kind="user", principal="pia", role="observe"))
await s.commit()
return {"shared": shared.id, "other": other.id, "mine": mine.id}
async def test_app_limits_personal_apps_and_trial_runs():
await auth_env.personal_keys(True)
pia = await auth_env.make_user("pia", "user")
ids = await _apps()
try:
_, limited = await auth_env.make_key(pia, app_ids=[ids["shared"]])
_, free = await auth_env.make_key(pia, name="不限应用")
c, f = auth_env.keyed(limited), auth_env.keyed(free)
assert [a["id"] for a in (await c.get("/api/apps")).json()["items"]] == [ids["shared"]], "只列限定的那几个"
assert {a["id"] for a in (await f.get("/api/apps")).json()["items"]} == {ids["shared"], ids["other"]}, \
"个人应用不列"
assert (await c.get(f"/api/apps/{ids['shared']}")).status_code == 200
r = await c.get(f"/api/apps/{ids['other']}")
assert r.status_code == 403 and r.json()["code"] == "key_app_forbidden"
r = await f.get(f"/api/apps/{ids['mine']}")
assert r.status_code == 403 and r.json()["code"] == "key_app_personal", "自己的个人应用也不行"
async with SessionLocal() as s:
trial = Run(workflow_name="试运行", run_class="exploratory", origin_kind="trial", started_by="pia")
refresh = Run(workflow_name="刷新", run_class="exploratory", origin_kind="personal_app",
app_id=ids["mine"], started_by="pia")
elsewhere = Run(workflow_name="别处", run_class="formal", origin_kind="app_entry", app_id=ids["other"],
started_by="pia")
s.add_all([trial, refresh, elsewhere])
await s.commit()
runs = (trial.id, refresh.id, elsewhere.id)
for run_id in runs[:2]:
r = await f.get(f"/api/runs/{run_id}/report.html")
assert r.status_code == 403 and r.json()["code"] == "key_app_personal"
r = await c.get(f"/api/runs/{runs[2]}/evidence")
assert r.status_code == 403 and r.json()["code"] == "key_app_forbidden", "限定了应用:别的应用的运行也不行"
finally:
async with SessionLocal() as s:
await s.execute(delete(AppGrant).where(AppGrant.app_id.in_(list(ids.values()))))
await s.execute(delete(AppVersion).where(AppVersion.app_id.in_(list(ids.values()))))
await s.execute(delete(App).where(App.id.in_(list(ids.values()))))
await s.commit()
async def test_the_reference_is_generated_from_the_same_table():
admin = await auth_env.login("root")
ref = (await admin.get("/api/access-keys/reference")).json()
assert {(r["method"], r["path"]) for r in ref["routes"]} == set(keys.KEY_ROUTES)
entry = next(r for r in ref["routes"] if r["path"] == "/api/apps/{app_id}/entries/{entry_id}")
assert entry["scope"] == "apps.run" and entry["idempotent"] is True
assert "key_idempotency_required" in entry["errors"] and "key_files_unsupported" in entry["errors"]
assert "key_route_forbidden" in ref["common_errors"] and ref["rate_per_minute"] == keys.RATE_PER_MINUTE
def test_websocket_routes_are_never_in_the_table():
sockets = {c.path for c in _contexts() if isinstance(c.original_route, APIWebSocketRoute)}
assert sockets and not {p for _, p in keys.KEY_ROUTES} & sockets
+470
View File
@@ -0,0 +1,470 @@
"""用访问密钥用应用(docs/design/next-round/04-对外集成与触达.md A3–A6):封顶在使用者、幂等、留痕、回复待办、视图数据、
钩子的收件记录。
- 封顶:管理员的密钥也按使用者算——取工件时查询快照是遮过的、节点产出 403;有「查看全部实例」授权的服务账号能列全部实例、
能下载报告,报告里遮罩的值写「已遮罩」;
- 幂等:同一个 Idempotency-Key 发起两次只有一个实例;轮换密钥后同一个键照样去重;处理到一半服务停了,同一个键认领回已
发起的实例;不带键 400;同一个键办另一件事 422;有文件控件的入口 422;刷新视图同样要带键;
- 留痕:run.origin 里有 via=key、key_id、密钥名(在清单哈希的封存范围内);task.responded 记了 via;
- 待办:个人密钥能回复审批、补充输入、编辑稿件;确认写入、双盲裁决、核实写入结果、工具审批 403(key_task_kind);
- 视图数据(agentlab.viewdata/1):印章、清单哈希、指标原值;遮罩的 value 为 null、state 为 masked,和页面同一个判断;
按需视图没算过时 empty、不发起运行;改了页面布局形状不变;
- 钩子收件记录:只有管理权限能读,列出状态和原因、近 1 小时验签失败次数。
"""
from __future__ import annotations
import asyncio
import json
import uuid
import pytest
from sqlalchemy import delete, select
from app.core.artifact_store import graph_hash, put_json
from app.db.base import SessionLocal, utcnow
from app.db.models import (
App, AppGrant, AppVersion, DataSource, Run, RunEvent, Task, TaskAssignment, Workflow, WorkflowVersion,
)
from app.db.models_apps import ApiReceipt
from app.engine.runner import run_manager
from tests.fakes import auth_env
@pytest.fixture(autouse=True)
async def world():
await auth_env.reset()
await run_manager.setup()
await auth_env.turn_on_local()
await auth_env.personal_keys(True)
made: dict[str, list[str]] = {"apps": [], "workflows": [], "sources": []}
try:
yield made
finally:
await run_manager.shutdown()
async with SessionLocal() as s:
await s.execute(delete(AppGrant).where(AppGrant.app_id.in_(made["apps"] or [""])))
await s.execute(delete(App).where(App.id.in_(made["apps"] or [""])))
await s.execute(delete(DataSource).where(DataSource.name.in_(made["sources"] or [""])))
await s.commit()
await auth_env.reset()
def node(nid, ntype, label=None, **config):
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": label or nid, "config": config}}
def chain(*nodes):
return {"nodes": list(nodes), "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
ECHO = chain(node("start", "input", "开始", fields=[{"name": "amount", "type": "number"}, {"name": "who"}]),
node("out", "output", "成果", fields=[{"name": "r", "value": "{{ input.who }}"}]))
KPI = chain(
node("start", "input", "开始", fields=[{"name": "week"}]),
node("fetch", "transform", "取数", mode="expression", expression="{'gmv': 4321.5, 'n': 7}", assign_to="kpi"),
node("card", "metrics", "口径卡", caliber="周报口径", caliber_version="v3",
metrics=[{"id": "gmv", "name": "销售额", "unit": "元", "expression": "vars.kpi.gmv"},
{"id": "orders", "name": "订单数", "unit": "单", "expression": "vars.kpi.n"}]),
node("out", "output", "成果", fields=[{"name": "r", "value": "{{ nodes.card.text }}"}]))
async def workflow(graph: dict) -> tuple[str, str]:
async with SessionLocal() as s:
wf = Workflow(name=f"密钥测试-{uuid.uuid4().hex[:6]}", graph=graph, status="published", published_version=1,
version=1)
s.add(wf)
await s.flush()
s.add(WorkflowVersion(workflow_id=wf.id, version=1, graph=graph, graph_hash=graph_hash(graph),
level="published", published_at=utcnow()))
await s.commit()
return wf.id, graph_hash(graph)
async def published(made: dict, spec: dict, grants: list[tuple[str, str, str]]) -> str:
admin = await auth_env.login("root")
r = await admin.post("/api/apps", json={"name": spec.get("name", "应用"), "spec": spec})
assert r.status_code == 201, r.text
app_id = r.json()["id"]
made["apps"].append(app_id)
items = [{"principal_kind": k, "principal": p, "role": role} for k, p, role in grants]
assert (await admin.put(f"/api/apps/{app_id}/grants", json={"items": items})).status_code == 200
out = (await admin.post(f"/api/apps/{app_id}/publish", json={})).json()
assert out["ok"], out
return app_id
def entry_spec(wf: str, hash_: str, *, files: bool = False) -> dict:
props = {"amount": {"type": "number", "title": "金额"}}
if files:
props["receipt"] = {"type": "object", "title": "发票", "x-widget": "file"}
return {"format": "agentlab.app/1", "name": "报销",
"pins": [{"alias": "main", "workflow_id": wf, "version": 1, "graph_hash": hash_, "use": "entry"}],
"entries": [{"id": "apply", "label": "发起", "pin": "main", "form": {"type": "object", "properties": props},
"map": {"amount": "form.amount", "who": "user.username"}}],
"pages": [{"id": "home", "title": "首页", "body": [{"id": "new", "type": "entry_button",
"props": {"entry": "apply"}}]}]}
def view_spec(wf: str, hash_: str, *, on_demand: bool = False, extra: list | None = None) -> dict:
source = ({"kind": "on_demand", "pin": "main", "params": {"type": "object", "properties": {"week": {"type": "string"}}}}
if on_demand else {"kind": "latest_sealed", "pin": "main", "from": "formal"})
return {"format": "agentlab.app/1", "name": "周报看板",
"pins": [{"alias": "main", "workflow_id": wf, "version": 1, "graph_hash": hash_, "use": "view"}],
"views": {"latest": {"source": source}},
"pages": [{"id": "home", "title": "首页", "body": [
{"id": "gmv", "type": "metric", "props": {"label": "销售额"}, "bind": {"view": "latest", "metric": "card.gmv"}},
*(extra or [])]}]}
async def wait(run_id: str, statuses=("succeeded", "failed", "cancelled")) -> Run:
for _ in range(600):
async with SessionLocal() as s:
row = await s.get(Run, run_id)
if row is not None and row.status in statuses and (row.status == "interrupted" or row.manifest_seq is not None):
return row
await asyncio.sleep(0.03)
raise AssertionError(f"运行 {run_id} 没有到 {statuses}")
async def formal(wf: str, week: str = "W1") -> Run:
from app.engine.launch import launch_run
async with SessionLocal() as s:
run = await launch_run(s, workflow_id=wf, run_class="formal", version=1, input_payload={"week": week},
actor="编辑", origin_kind="launcher")
row = await wait(run.id)
assert row.status == "succeeded", row.error
return row
# ---------------------------------------------------------------- 幂等与留痕
async def test_starting_with_a_key_is_idempotent_and_leaves_a_trace(world):
pia = await auth_env.make_user("pia", "user")
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h), [("user", "pia", "use")])
row, secret = await auth_env.make_key(pia, name="报销脚本")
c = auth_env.keyed(secret)
url = f"/api/apps/{app_id}/entries/apply"
missing = await c.post(url, json={"values": {"amount": 3}})
assert missing.status_code == 400 and missing.json()["code"] == "key_idempotency_required"
first = await c.post(url, json={"values": {"amount": 3}}, headers={"Idempotency-Key": "order-1"})
assert first.status_code == 201, first.text
again = await c.post(url, json={"values": {"amount": 3}}, headers={"Idempotency-Key": "order-1"})
assert again.json()["run_id"] == first.json()["run_id"] and again.json()["duplicate"] is True
other = await c.post(url, json={"values": {"amount": 4}}, headers={"Idempotency-Key": "order-1"})
assert other.status_code == 422 and other.json()["code"] == "key_idempotency_reused", "同一个键办另一件事"
run = await wait(first.json()["run_id"])
assert run.started_by == "pia"
assert {k: run.origin.get(k) for k in ("via", "key_id", "key_name", "idempotency_key")} == {
"via": "key", "key_id": row.id, "key_name": "报销脚本", "idempotency_key": "order-1"}
async with SessionLocal() as s:
note = (await s.execute(select(RunEvent).where(RunEvent.run_id == run.id, RunEvent.type == "run.origin")))\
.scalar_one()
assert (note.data["via"], note.data["key_id"]) == ("key", row.id) and note.seq <= run.manifest_seq, \
"来源在封存范围内"
async with SessionLocal() as s:
n = len(list((await s.execute(select(Run).where(Run.app_id == app_id))).scalars()))
assert n == 1, "同一个键只发起一个实例"
# 轮换之后:同一个键照样认得出(按拥有者去重,不按密钥)
admin = await auth_env.login("root")
rotated = (await admin.post(f"/api/access-keys/{row.id}/rotate")).json()["key"]
again = await auth_env.keyed(rotated).post(url, json={"values": {"amount": 3}}, headers={"Idempotency-Key": "order-1"})
assert again.json()["run_id"] == first.json()["run_id"]
async def test_a_half_done_start_is_reclaimed_after_a_crash(world):
from datetime import timedelta
from app.process import receipts
pia = await auth_env.make_user("pia", "user")
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h), [("user", "pia", "use")])
row, secret = await auth_env.make_key(pia)
# 上一次处理到一半服务停了:回执停在 processing,实例其实已经发起
async with SessionLocal() as s:
s.add(ApiReceipt(owner_user_id=pia.id, idem_key="crash-1", status="processing", route="x",
received_at=utcnow() - timedelta(seconds=receipts.STALE_S + 5)))
s.add(ApiReceipt(owner_user_id=pia.id, idem_key="busy-1", status="processing", route="x"))
s.add(Run(id=f"run-{uuid.uuid4().hex[:8]}", workflow_name="实例", run_class="formal", app_id=app_id,
origin_kind="app_entry", started_by="pia", origin={"via": "key", "idempotency_key": "crash-1"}))
await s.commit()
before = (await s.execute(select(Run.id).where(Run.app_id == app_id))).scalar_one()
c = auth_env.keyed(secret)
url = f"/api/apps/{app_id}/entries/apply"
busy = await c.post(url, json={"values": {}}, headers={"Idempotency-Key": "busy-1"})
assert busy.status_code == 409 and busy.json()["code"] == "key_idempotency_busy"
back = await c.post(url, json={"values": {}}, headers={"Idempotency-Key": "crash-1"})
assert back.status_code == 201 and back.json()["run_id"] == before, back.text
async with SessionLocal() as s:
assert len(list((await s.execute(select(Run.id).where(Run.app_id == app_id))).scalars())) == 1
receipt = (await s.execute(select(ApiReceipt).where(ApiReceipt.idem_key == "crash-1"))).scalar_one()
assert (receipt.status, receipt.run_id, receipt.key_id) == ("accepted", before, row.id)
async def test_entries_with_file_controls_cannot_be_started_with_a_key(world):
pia = await auth_env.make_user("pia", "user")
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h, files=True), [("user", "pia", "use")])
_, secret = await auth_env.make_key(pia)
r = await auth_env.keyed(secret).post(f"/api/apps/{app_id}/entries/apply", json={"values": {}},
headers={"Idempotency-Key": "f1"})
assert r.status_code == 422 and r.json()["code"] == "key_files_unsupported"
# ---------------------------------------------------------------- 待办
def human(mode: str) -> dict:
nodes = [node("start", "input", fields=[{"name": "q"}]),
node("ask", "human", mode=mode, title="处理一下", assignee={"kind": "initiator"}),
node("ok", "output", fields=[{"name": "结论", "value": "通过"}])]
edges = [{"source": "start", "target": "ask"}, {"source": "ask", "target": "ok"}]
return {"nodes": nodes, "edges": edges}
async def _task_for(app_id: str, graph: dict, started_by: str) -> Task:
run = await run_manager.start(graph=graph, input_payload={"q": "x"}, app_id=app_id, app_version=1,
started_by=started_by)
await wait(run.id, ("interrupted",))
await run_manager.wait_idle(run.id)
for _ in range(100):
async with SessionLocal() as s:
task = (await s.execute(select(Task).where(Task.run_id == run.id, Task.status == "pending"))).scalars().first()
if task is not None:
return task
await asyncio.sleep(0.05)
raise AssertionError("没有开出待办")
async def test_personal_keys_answer_only_three_kinds_of_tasks(world):
pia = await auth_env.make_user("pia", "user")
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h), [("user", "pia", "use")])
key, secret = await auth_env.make_key(pia, name="审批机器人")
c = auth_env.keyed(secret)
for mode, body in (("approve", {"decision": "approve"}), ("input", {"decision": "submit", "values": {"value": "补上"}}),
("edit", {"decision": "submit", "values": {"value": "改好"}})):
task = await _task_for(app_id, human(mode), "pia")
listed = [t["id"] for t in (await c.get("/api/tasks")).json()["items"]]
assert task.id in listed, "派给拥有者的待办列得出来"
r = await c.post(f"/api/tasks/{task.id}/respond", json=body)
assert r.status_code == 200 and r.json()["resolved"] is True, (mode, r.text)
async with SessionLocal() as s:
ev = (await s.execute(select(RunEvent).where(RunEvent.run_id == task.run_id,
RunEvent.type == "task.responded"))).scalar_one()
assert (ev.data["via"], ev.data["key_id"], ev.data["key_name"], ev.data["actor"]) == \
("key", key.id, "审批机器人", "pia")
async with SessionLocal() as s:
run = Run(workflow_name="写入", run_class="formal", app_id=app_id, origin_kind="app_entry", started_by="pia")
s.add(run)
await s.flush()
tasks = []
for kind in ("action_confirm", "dual_check", "reconcile", "tool"):
t = Task(run_id=run.id, kind=kind, app_id=app_id)
s.add(t)
await s.flush()
s.add(TaskAssignment(task_id=t.id, principal_kind="user", principal="pia", source="initiator"))
tasks.append(t.id)
await s.commit()
for task_id in tasks:
r = await c.post(f"/api/tasks/{task_id}/respond", json={"decision": "approve"})
assert r.status_code == 403 and r.json()["code"] == "key_task_kind", r.text
async def test_service_accounts_are_never_assigned_tasks(world):
svc = await auth_env.make_service()
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h), [("user", "svc-portal", "use")])
task = await _task_for(app_id, human("approve"), "svc-portal")
async with SessionLocal() as s:
rows = list((await s.execute(select(TaskAssignment).where(TaskAssignment.task_id == task.id))).scalars())
assert [(a.principal_kind, a.principal) for a in rows] == [("anyone", "")], \
"服务账号发起的实例里派给发起人的,交给任何人"
_, secret = await auth_env.make_key(svc, scopes=("tasks.read", "tasks.respond"))
r = await auth_env.keyed(secret).post(f"/api/tasks/{task.id}/respond", json={"decision": "approve"})
assert r.json()["code"] == "key_scope_missing", "服务账号的密钥没有回复待办这一项"
# ---------------------------------------------------------------- 封顶在使用者
async def test_an_admins_key_gets_masked_artifacts_and_no_raw_outputs(world):
root = (await auth_env.login("root"))
await root.get("/api/auth/me")
async with SessionLocal() as s:
from app.db.models import User
admin_row = (await s.execute(select(User).where(User.username == "root"))).scalar_one()
name = f"crm-{uuid.uuid4().hex[:6]}"
s.add(DataSource(name=name, kind="sqlite", options={"mask_columns": ["phone"]}))
app = App(name="自己的", published_version=1, created_by="root")
s.add(app)
await s.flush()
run = Run(workflow_name="自己发起的实例", run_class="formal", app_id=app.id, origin_kind="app_entry",
started_by="root")
s.add(run)
await s.commit()
world["sources"].append(name)
world["apps"].append(app.id)
_, secret = await auth_env.make_key(admin_row)
c = auth_env.keyed(secret)
snap = {"source": name, "columns": ["name", "phone"], "rows": [["客户甲", "19900000123"]],
"sql": "SELECT name, phone FROM customers", "truncated": False}
query_id = await put_json(snap, kind="query_snapshot", run_id=run.id, node_id="q")
output_id = await put_json({"q": snap}, kind="node_output", run_id=run.id, node_id="q")
got = await c.get(f"/api/artifacts/{query_id}")
assert got.status_code == 200 and got.json()["content"]["rows"][0][1] != "19900000123", "遮罩列换掉了"
assert (await c.get(f"/api/artifacts/{output_id}")).json()["code"] == "artifact_kind_forbidden"
assert (await root.get(f"/api/artifacts/{output_id}")).status_code == 200, "同一个管理员用会话照旧拿原文"
assert (await c.get("/api/apps")).json()["items"] == [], "没有授权给他的应用,管理员的密钥也看不到"
async def test_viewdata_and_report_for_an_observer_service_account(world, monkeypatch, tmp_path):
from tests import test_report_masked_metrics as vipmod
from app.data.engine import engines
path = tmp_path / "vip.db"
import sqlite3
db = sqlite3.connect(path)
db.executescript("CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT, phone TEXT);")
db.executemany("INSERT INTO customers VALUES (?, ?, ?)", vipmod.CUSTOMERS)
db.commit()
db.close()
source_id = await vipmod.set_source(path, {"mask_columns": ["phone"]})
await engines.invalidate(source_id)
world["sources"].append("vip")
vipmod.script(monkeypatch, vipmod.CLEAN)
graph = vipmod.graph(on_violation="flag")
wf, h = await workflow(graph)
svc = await auth_env.make_service()
spec = {"format": "agentlab.app/1", "name": "会员看板",
"pins": [{"alias": "main", "workflow_id": wf, "version": 1, "graph_hash": h, "use": "view"}],
"views": {"latest": {"source": {"kind": "latest_sealed", "pin": "main", "from": "formal"}}},
"pages": [{"id": "home", "title": "首页", "body": [
{"id": "phone", "type": "metric", "props": {"label": "手机号"},
"bind": {"view": "latest", "metric": "card.phone_raw"}}]}]}
app_id = await published(world, spec, [("user", "svc-portal", "observe")])
from app.engine.launch import launch_run
async with SessionLocal() as s:
launched = await launch_run(s, workflow_id=wf, run_class="formal", version=1, input_payload={},
actor="编辑", origin_kind="launcher")
run = await wait(launched.id)
assert run.status == "succeeded", run.error
_, secret = await auth_env.make_key(svc, scopes=("apps.read", "reports.read"))
c = auth_env.keyed(secret)
data = (await c.get(f"/api/apps/{app_id}/views/latest/data")).json()
assert data["format"] == "agentlab.viewdata/1" and data["state"] == "ok" and data["run_id"] == run.id
assert data["manifest_hash"] == run.manifest_hash and data["stamp"]["sealed"] is True
by_id = {m["id"]: m for m in data["metrics"]}
assert (by_id["phone_raw"]["value"], by_id["phone_raw"]["state"], by_id["phone_raw"]["text"]) == \
(None, "masked", "已遮罩"), "遮罩的指标不给原值"
assert (by_id["n_cust"]["value"], by_id["n_cust"]["state"]) == (3, "ok")
assert by_id["n_cust"]["caliber"] == "会员口径" and by_id["n_cust"]["evidence"]["run_id"] == run.id
assert vipmod.leaks(data) == []
page = (await c.get(f"/api/apps/{app_id}/pages/home")).json()
assert page["tree"][0]["data"]["text"] == by_id["phone_raw"]["text"], "和页面同一个判断"
evidence = await c.get(f"/api/runs/{run.id}/evidence/metrics/card/n_cust")
assert evidence.status_code == 200, "证据指针走得通"
report = await c.get(f"/api/runs/{run.id}/report.html")
assert report.status_code == 200 and vipmod.leaks(report.text) == [], "报告里没有遮罩列的原值"
async def test_viewdata_on_demand_does_not_launch_and_keeps_its_shape(world):
pia = await auth_env.make_user("pia", "user")
wf, h = await workflow(KPI)
app_id = await published(world, view_spec(wf, h, on_demand=True), [("user", "pia", "use")])
_, secret = await auth_env.make_key(pia, scopes=("apps.read", "apps.run"))
c = auth_env.keyed(secret)
params = {"params": json.dumps({"week": "W9"})}
empty = (await c.get(f"/api/apps/{app_id}/views/latest/data", params=params)).json()
assert empty["state"] == "empty" and empty["metrics"] == [] and "刷新" in empty["message"]
async with SessionLocal() as s:
assert (await s.execute(select(Run.id).where(Run.app_id == app_id))).first() is None, "读数据不发起运行"
missing = await c.post(f"/api/apps/{app_id}/views/latest/refresh", json={"params": {"week": "W9"}})
assert missing.json()["code"] == "key_idempotency_required", "刷新视图同样要带幂等键"
r = await c.post(f"/api/apps/{app_id}/views/latest/refresh", json={"params": {"week": "W9"}},
headers={"Idempotency-Key": "w9"})
assert r.status_code == 200, r.text
run = await wait(r.json()["run_id"])
assert run.origin.get("via") == "key"
again = await c.post(f"/api/apps/{app_id}/views/latest/refresh", json={"params": {"week": "W9"}},
headers={"Idempotency-Key": "w9"})
assert again.json()["run_id"] == run.id and again.json()["duplicate"] is True
full = (await c.get(f"/api/apps/{app_id}/views/latest/data", params=params)).json()
assert full["state"] == "ok" and full["params"] == {"week": "W9"}
assert {m["id"]: m["value"] for m in full["metrics"]} == {"gmv": 4321.5, "orders": 7}
assert full["stamp"]["calibers"] == [{"name": "周报口径", "version": "v3"}]
# 管理应用的人改了页面布局(加一块、换个 id):数据接口的形状不变
admin = await auth_env.login("root")
draft = (await admin.get(f"/api/apps/{app_id}")).json()["draft"]
draft["pages"][0]["body"] = [{"id": "renamed", "type": "metric", "props": {"label": "x"},
"bind": {"view": "latest", "metric": "card.orders"}}]
assert (await admin.patch(f"/api/apps/{app_id}", json={"draft": draft})).status_code == 200
assert (await admin.post(f"/api/apps/{app_id}/publish", json={})).json()["ok"]
after = (await c.get(f"/api/apps/{app_id}/views/latest/data", params=params)).json()
assert {k: v for k, v in after.items() if k != "app"} == {k: v for k, v in full.items() if k != "app"}
assert (await c.get(f"/api/apps/{app_id}/views/nope/data")).json()["code"] == "app_view_not_found"
async def test_service_account_with_observe_lists_all_instances(world):
pia = await auth_env.make_user("pia", "user")
svc = await auth_env.make_service()
wf, h = await workflow(ECHO)
app_id = await published(world, entry_spec(wf, h), [("user", "pia", "use"), ("user", "svc-portal", "observe")])
_, mine = await auth_env.make_key(pia)
started = await auth_env.keyed(mine).post(f"/api/apps/{app_id}/entries/apply", json={"values": {"amount": 1}},
headers={"Idempotency-Key": "a"})
run = await wait(started.json()["run_id"])
_, secret = await auth_env.make_key(svc, scopes=("apps.read",))
listed = (await auth_env.keyed(secret).get(f"/api/apps/{app_id}/instances", params={"who": "all"})).json()
assert [i["run_id"] for i in listed["items"]] == [run.id]
one = await auth_env.keyed(secret).get(f"/api/apps/{app_id}/instances/{run.id}")
assert one.status_code == 200
# ---------------------------------------------------------------- 钩子的收件记录
async def test_hook_receipts_are_listed_for_managers_with_signature_failures(world):
import time
from app.notify.core import sign
from app.process import hooks
pia = await auth_env.make_user("pia", "user") # noqa: F841 - 只有「查看全部实例」,没有管理权限
admin = await auth_env.login("root")
async with SessionLocal() as s:
app = App(name="收款", published_version=1, created_by="root")
s.add(app)
await s.flush()
s.add(AppVersion(app_id=app.id, version=1, spec={"hooks": [{"name": "paid", "kind": "signal"}]},
spec_hash="h" * 64, bindings={}))
s.add(AppGrant(app_id=app.id, principal_kind="user", principal="pia", role="observe"))
row, secret, _ = await hooks.rotate_hook(s, app.id, "paid", kind="signal")
await s.commit()
world["apps"].append(app.id)
hook_id = row.id
def signed(key: str, body: dict, *, bad: bool = False) -> tuple[dict, bytes]:
raw = json.dumps(body).encode()
ts = str(int(time.time()))
return {"X-AgentLab-Timestamp": ts, "X-AgentLab-Signature": f"sha256={'0' * 64 if bad else sign(secret, ts, raw)}",
"Idempotency-Key": key, "Content-Type": "application/json"}, raw
plain = auth_env.client()
headers, body = signed("k1", {"correlation": "none-waiting"})
assert (await plain.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 404
headers, body = signed("k2", {"correlation": "x"}, bad=True)
assert (await plain.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 401
out = (await admin.get(f"/api/apps/{app.id}/hooks/paid/receipts")).json()
assert [(i["idem_key"], i["status"]) for i in out["items"]] == [("k1", "rejected")], "验签失败的不落库"
assert "没有在等" in out["items"][0]["detail"] and out["signature_failures"] == 1
pia_c = await auth_env.login("pia")
assert (await pia_c.get(f"/api/apps/{app.id}/hooks/paid/receipts")).status_code == 403, "要管理权限"
+341
View File
@@ -0,0 +1,341 @@
"""访问密钥与服务账号:签发、认人、轮换、身份来源、限流、留痕(docs/design/next-round/04-对外集成与触达.md A1、A2、A6)。
要守住的:
- 密钥只在建、轮换时显示一次,库里只有 sha256 和开头一截;到期必填、最长 365 天;
- 认不出的密钥各回 401 带各自的 code(无效、到期、撤销、拥有者停用、拥有者被删、个人密钥开关关了),绝不退回匿名或会话;
- 轮换后旧密钥 24 小时内还能用,过了就失效;关登录时带密钥回 409(keys_need_login);
- 带密钥的请求只按密钥认:不查 CSRF、不看 cookie、不看 X-Actor;带 cookie、不带来源标记的 POST 照旧 403;
- 每把密钥每分钟 120 次,超了 429 带 Retry-After;
- 建、撤销、轮换密钥,建、停用服务账号,开关个人密钥都写审计;请求记录批量写之后查得到、超过保留期的清掉;
- 服务账号 svc- 开头、不能登录、角色固定;名录里标出,岗位绑定、选人候选里排除,授权照收。
"""
from __future__ import annotations
from datetime import timedelta
import pytest
from sqlalchemy import select, update
from app.auth import keys
from app.db.base import SessionLocal, utcnow
from app.db.models import AuditEvent, User
from app.db.models_identity import AccessKey, AccessKeyRequest
from tests.fakes import auth_env
@pytest.fixture(autouse=True)
async def clean():
await auth_env.reset()
try:
yield
finally:
await auth_env.reset()
async def _audits(action: str) -> list[AuditEvent]:
async with SessionLocal() as s:
return list((await s.execute(select(AuditEvent).where(AuditEvent.action == action))).scalars())
# ---------------------------------------------------------------- 签发
async def test_a_personal_key_is_shown_once_and_only_its_hash_is_stored():
await auth_env.turn_on_local()
await auth_env.make_user("ana", "builder")
ana = await auth_env.login("ana")
r = await ana.post("/api/access-keys", json={"name": "日报脚本"})
assert r.status_code == 409 and r.json()["code"] == "personal_keys_off", "开关缺省关:建不了个人密钥"
await auth_env.personal_keys(True)
r = await ana.post("/api/access-keys", json={"name": "日报脚本"})
assert r.status_code == 201, r.text
body = r.json()
secret = body["key"]
assert secret.startswith("agl_") and len(secret) >= 4 + 43
assert body["scopes"] == list(keys.READ_SCOPES), "新建时缺省只勾读的"
assert body["prefix"] == secret[:12] and body["state"] == "active"
async with SessionLocal() as s:
row = await s.get(AccessKey, body["id"])
assert row.key_hash == keys.key_hash(secret) and secret not in (row.key_hash, row.prefix), "库里没有明文"
listed = (await ana.get("/api/access-keys")).json()["items"]
assert [i["id"] for i in listed] == [body["id"]] and "key" not in listed[0], "列表里不再有明文"
days = (keys.aware(row.expires_at) - utcnow()).days
assert days in (89, 90), "缺省 90 天"
too_long = await ana.post("/api/access-keys", json={"name": "x", "expires_in_days": 366})
assert too_long.status_code == 422, "最长 365 天"
bad_scope = await ana.post("/api/access-keys", json={"name": "x", "scopes": ["runs.read"]})
assert bad_scope.status_code == 422 and bad_scope.json()["field"] == "scopes"
assert [a.data["name"] for a in await _audits("access_key.create")] == ["日报脚本"]
async def test_each_unusable_key_gets_its_own_401_and_never_falls_back():
admin = await auth_env.turn_on_local()
await auth_env.personal_keys(True)
uma = await auth_env.make_user("uma", "user")
ok_row, ok = await auth_env.make_key(uma)
_, expired = await auth_env.make_key(uma, days=-1)
revoked_row, revoked = await auth_env.make_key(uma)
assert (await admin.post(f"/api/access-keys/{revoked_row.id}/revoke")).status_code == 200
async def code(secret: str, client=None) -> tuple[int, str | None]:
r = await (client or auth_env.keyed(secret)).get("/api/auth/me")
return r.status_code, r.json().get("code")
assert (await code(ok)) == (200, None)
assert (await code("agl_not-a-real-key")) == (401, "key_invalid")
assert (await code(expired)) == (401, "key_expired")
assert (await code(revoked)) == (401, "key_revoked")
# 带着管理员的会话 cookie 也不退回会话:认不出的密钥就是 401
admin.headers["Authorization"] = f"Bearer {revoked}"
assert (await code(revoked, admin)) == (401, "key_revoked")
del admin.headers["Authorization"]
async with SessionLocal() as s:
await s.execute(update(User).where(User.id == uma.id).values(disabled=True))
await s.commit()
assert (await code(ok)) == (401, "key_owner_disabled")
async with SessionLocal() as s:
await s.execute(update(User).where(User.id == uma.id).values(disabled=False))
await s.commit()
await auth_env.personal_keys(False)
assert (await code(ok)) == (401, "key_personal_off"), "管理员关了开关:已有的个人密钥一并不认"
await auth_env.personal_keys(True)
assert (await code(ok)) == (200, None)
async with SessionLocal() as s:
await s.delete(await s.get(User, uma.id))
await s.commit()
assert await s.get(AccessKey, ok_row.id) is None, "拥有者删了,密钥随外键一起删"
assert (await code(ok)) == (401, "key_invalid")
async def test_me_tells_who_the_key_is_and_its_scope_and_expiry():
await auth_env.turn_on_local()
svc = await auth_env.make_service()
row, secret = await auth_env.make_key(svc, scopes=("apps.read", "tasks.respond"), app_ids=["app-a"])
me = (await auth_env.keyed(secret).get("/api/auth/me")).json()
assert (me["username"], me["role"], me["verified"]) == ("svc-portal", "user", True)
assert me["key"]["id"] == row.id and me["key"]["service"] is True
assert me["key"]["scopes"] == ["apps.read", "me"], "服务账号的密钥去掉只给个人密钥的回复待办"
assert me["key"]["app_ids"] == ["app-a"] and me["key"]["expires_at"]
async def test_rotation_keeps_the_old_key_for_24_hours():
admin = await auth_env.turn_on_local()
svc = await auth_env.make_service()
created = (await admin.post("/api/access-keys", json={"name": "门户", "service_account_id": svc.id,
"scopes": ["apps.read"], "expires_in_days": 30})).json()
old = created["key"]
rotated = await admin.post(f"/api/access-keys/{created['id']}/rotate")
assert rotated.status_code == 200, rotated.text
body = rotated.json()
new = body["key"]
assert new != old and body["rotated_from"] == created["id"] and body["scopes"] == ["apps.read"]
assert (await auth_env.keyed(old).get("/api/auth/me")).status_code == 200, "旧密钥 24 小时内还能用"
assert (await auth_env.keyed(new).get("/api/auth/me")).status_code == 200
async with SessionLocal() as s:
row = await s.get(AccessKey, created["id"])
grace = keys.aware(row.expires_at) - utcnow()
assert timedelta(hours=23) < grace <= timedelta(hours=24)
fresh = await s.get(AccessKey, body["id"])
assert 29 <= (keys.aware(fresh.expires_at) - utcnow()).days <= 30, "新密钥的有效期长短照旧"
row.expires_at = utcnow() - timedelta(seconds=1) # 24 小时过去了
await s.commit()
assert (await auth_env.keyed(old).get("/api/auth/me")).json()["code"] == "key_expired"
again = await admin.post(f"/api/access-keys/{created['id']}/rotate")
assert again.status_code == 409, "过期的不能再轮换"
assert [a.subject_id for a in await _audits("access_key.rotate")] == [created["id"]]
async def test_keys_do_nothing_with_login_off():
c = auth_env.keyed("agl_whatever")
r = await c.get("/api/apps")
assert r.status_code == 409 and r.json()["code"] == "keys_need_login", "关登录时不能让调用方以为自己被收窄了"
off = auth_env.client()
assert (await off.post("/api/access-keys", json={"name": "x"})).json()["code"] == "keys_need_login"
assert (await off.get("/api/access-keys")).json() == {"items": []}
assert (await off.get("/api/access-keys/settings")).json()["login_on"] is False
# ---------------------------------------------------------------- 身份来源与 CSRF
async def test_bearer_skips_csrf_but_cookie_requests_still_need_it():
admin = await auth_env.turn_on_local()
await auth_env.personal_keys(True)
uma = await auth_env.make_user("uma", "user")
_, secret = await auth_env.make_key(uma)
# 带密钥的 POST 不带来源标记也能过 CSRF(到了接口:应用不存在 404,不是 csrf_header)
r = await auth_env.keyed(secret).post("/api/apps/nope/entries/x", json={"values": {}},
headers={"Idempotency-Key": "k1", "Origin": "https://evil.example"})
assert r.status_code == 404 and r.json()["code"] == "app_not_found", r.text
# 带 cookie、不带来源标记的 POST 照旧拒
del admin.headers["X-AgentLab-Request"]
assert (await admin.post("/api/groups", json={"name": "g"})).json()["code"] == "csrf_header"
# 同时带 cookie 和密钥:按密钥认,cookie 不起作用;X-Actor 一律不看
admin.headers["Authorization"] = f"Bearer {secret}"
me = (await admin.get("/api/auth/me", headers={"X-Actor": "mallory"})).json()
assert (me["username"], me["role"]) == ("uma", "user")
assert (await admin.get("/api/users")).json()["code"] == "key_route_forbidden", "管理员的 cookie 不起作用"
async def test_the_principal_from_a_key_is_capped_at_user():
await auth_env.turn_on_local()
await auth_env.personal_keys(True)
root = await _user("root")
_, secret = await auth_env.make_key(root)
me = (await auth_env.keyed(secret).get("/api/auth/me")).json()
assert (me["username"], me["role"]) == ("root", "user"), "管理员的密钥也按使用者算"
async def _user(username: str) -> User:
async with SessionLocal() as s:
return (await s.execute(select(User).where(User.username == username))).scalar_one()
# ---------------------------------------------------------------- 限流
async def test_each_key_is_rate_limited_per_minute(monkeypatch):
await auth_env.turn_on_local()
svc = await auth_env.make_service()
row, secret = await auth_env.make_key(svc)
monkeypatch.setattr(keys, "RATE_PER_MINUTE", 3)
c = auth_env.keyed(secret)
assert [(await c.get("/api/auth/me")).status_code for _ in range(3)] == [200, 200, 200]
r = await c.get("/api/auth/me")
assert r.status_code == 429 and r.json()["code"] == "key_rate_limited"
assert int(r.headers["retry-after"]) >= 1
_, other = await auth_env.make_key(svc, name="另一把")
assert (await auth_env.keyed(other).get("/api/auth/me")).status_code == 200, "按密钥各算各的"
assert keys.take(row.id, now=10**9) == 0, "过一会儿补满了就又能用"
# ---------------------------------------------------------------- 留痕
async def test_requests_are_buffered_written_in_batches_and_purged():
admin = await auth_env.turn_on_local()
svc = await auth_env.make_service()
row, secret = await auth_env.make_key(svc, scopes=("apps.read",))
c = auth_env.keyed(secret)
await c.get("/api/apps")
await c.get("/api/tasks") # 缺 scope:被拒的也记
async with SessionLocal() as s:
assert (await s.execute(select(AccessKeyRequest))).first() is None, "先攒在进程里,不是每个请求同步写一行"
from app.scheduler.loop import process_due
await process_due() # 调度循环的一拍
async with SessionLocal() as s:
rows = list((await s.execute(select(AccessKeyRequest).order_by(AccessKeyRequest.at))).scalars())
assert [(r.method, r.route, r.status) for r in rows] == [("GET", "/api/apps", 200), ("GET", "/api/tasks", 403)]
assert all(r.key_id == row.id for r in rows)
listed = (await admin.get(f"/api/access-keys/{row.id}/requests")).json()
assert [i["route"] for i in listed["items"]] == ["/api/tasks", "/api/apps"] and listed["retain_days"] == 90
async with SessionLocal() as s:
await s.execute(update(AccessKeyRequest).where(AccessKeyRequest.route == "/api/apps")
.values(at=utcnow() - timedelta(days=91)))
await s.commit()
await keys.tick(utcnow())
async with SessionLocal() as s:
left = [r.route for r in (await s.execute(select(AccessKeyRequest))).scalars()]
assert left == ["/api/tasks"], "超过 90 天的清掉"
async def test_last_used_is_written_at_most_once_a_minute():
await auth_env.turn_on_local()
svc = await auth_env.make_service()
row, secret = await auth_env.make_key(svc)
c = auth_env.keyed(secret)
await c.get("/api/auth/me")
async with SessionLocal() as s:
first = (await s.get(AccessKey, row.id)).last_used_at
await c.get("/api/auth/me")
async with SessionLocal() as s:
assert (await s.get(AccessKey, row.id)).last_used_at == first, "一分钟内不再写"
await s.execute(update(AccessKey).where(AccessKey.id == row.id)
.values(last_used_at=utcnow() - timedelta(minutes=2)))
await s.commit()
await c.get("/api/auth/me")
async with SessionLocal() as s:
assert keys.aware((await s.get(AccessKey, row.id)).last_used_at) > utcnow() - timedelta(seconds=30)
async def test_only_owners_and_admins_manage_keys_and_everything_is_audited():
admin = await auth_env.turn_on_local()
r = await admin.put("/api/access-keys/settings", json={"personal_keys": True})
assert r.status_code == 200 and r.json()["personal_keys"] is True
assert len(await _audits("auth.personal_keys")) == 1
await auth_env.make_user("ana", "builder")
await auth_env.make_user("bo", "user")
ana, bo = await auth_env.login("ana"), await auth_env.login("bo")
assert (await ana.put("/api/access-keys/settings", json={"personal_keys": False})).status_code == 403
mine = (await ana.post("/api/access-keys", json={"name": "a"})).json()
assert (await bo.post(f"/api/access-keys/{mine['id']}/revoke")).status_code == 404, "别人的密钥当不存在"
assert (await bo.get(f"/api/access-keys/{mine['id']}/requests")).status_code == 404
assert (await bo.get("/api/access-keys")).json()["items"] == []
assert len((await admin.get("/api/access-keys")).json()["items"]) == 1, "管理员看得到全部"
assert (await ana.post(f"/api/access-keys/{mine['id']}/revoke")).json()["state"] == "revoked"
assert len(await _audits("access_key.revoke")) == 1
# 服务账号:只给管理员建
assert (await ana.post("/api/access-keys/service-accounts", json={"username": "svc-x"})).status_code == 403
bad = await admin.post("/api/access-keys/service-accounts", json={"username": "portal"})
assert bad.status_code == 422 and bad.json()["code"] == "service_account_invalid", "统一 svc- 开头"
svc = (await admin.post("/api/access-keys/service-accounts", json={"username": "svc-portal",
"display_name": "门户"})).json()
assert svc["username"] == "svc-portal" and len(await _audits("service_account.create")) == 1
no_respond = await admin.post("/api/access-keys", json={"name": "k", "service_account_id": svc["id"],
"scopes": ["apps.read", "tasks.respond"]})
assert no_respond.status_code == 422, "服务账号不能被派待办,它的密钥不能勾回复待办"
key = (await admin.post("/api/access-keys", json={"name": "k", "service_account_id": svc["id"]})).json()
assert (await auth_env.keyed(key["key"]).get("/api/auth/me")).status_code == 200
out = await admin.patch(f"/api/access-keys/service-accounts/{svc['id']}", json={"disabled": True})
assert out.json()["disabled"] is True and len(await _audits("service_account.disable")) == 1
assert (await auth_env.keyed(key["key"]).get("/api/auth/me")).json()["code"] == "key_owner_disabled", \
"停用之后密钥失效"
ref = (await bo.get("/api/access-keys/reference")).json()
assert {"method": "POST", "path": "/api/apps/{app_id}/entries/{entry_id}"}.items() <= next(
r for r in ref["routes"] if r["path"].endswith("/entries/{entry_id}")).items()
assert (await auth_env.client().get("/api/access-keys/reference")).status_code == 401, "接口说明要登录"
# ---------------------------------------------------------------- 服务账号
async def test_service_accounts_cannot_log_in_or_get_a_password_or_role():
admin = await auth_env.turn_on_local()
svc = (await admin.post("/api/access-keys/service-accounts", json={"username": "svc-bi"})).json()
r = await auth_env.client().post("/api/auth/login", json={"username": "svc-bi", "password": "password-123"})
assert r.status_code == 401
assert (await admin.patch(f"/api/users/{svc['id']}", json={"password": "password-123"})).status_code == 409
assert (await admin.patch(f"/api/users/{svc['id']}", json={"role": "admin"})).status_code == 409
users = {u["username"]: u["kind"] for u in (await admin.get("/api/users")).json()["items"]}
assert users["svc-bi"] == "service" and users["root"] == "person"
taken = await admin.post("/api/users", json={"username": "svc-me", "role": "user"})
assert taken.status_code == 422, "svc- 开头留给服务账号"
# 万一有会话指向服务账号也认不出来
from app.auth import sessions
async with SessionLocal() as s:
user = await s.get(User, svc["id"])
token = await sessions.create_session(s, user, ip=None, user_agent=None)
await s.commit()
assert await sessions.user_of(s, token) is None
async def test_service_accounts_are_marked_in_the_directory_and_kept_out_of_assignments():
from app.auth.directory import load_directory, mismatch
admin = await auth_env.turn_on_local()
await auth_env.make_service("svc-bi")
await auth_env.make_user("ana", "builder")
r = await admin.post("/api/groups", json={"name": "ops"})
await admin.put(f"/api/groups/{r.json()['id']}/members", json={"members": ["ana", "svc-bi"]})
users = {u["username"]: u for u in (await admin.get("/api/directory")).json()["users"]}
assert users["svc-bi"]["service"] is True and "service" not in users["ana"]
async with SessionLocal() as s:
known = await load_directory(s)
assert mismatch(known, "local", "user", "svc-bi").problem == "service", "岗位绑定:服务账号算错"
assert mismatch(known, "local", "user", "svc-bi", allow_service=True) is None, "授权:照收"
from app.auth.directory import _members, review_pool
assert set(await _members(s, "ops")) == {"ana"}, "候选池里不列服务账号"
assert "svc-bi" not in (await review_pool(s)).users