Files
sso/scripts/run-local-e2e.sh
T
ssw c8a02b8c49 feat: 从网关项目剥离为独立 SSO 统一认证服务
定位:身份层(IdP + IAP),不含动态域名转发 / LLM 上游代理 / 用量计费。

代码(source/rust,nginx 动态模块 + Rust,单进程零外部依赖)
- IdP:authorization_code(含 PKCE)/ refresh_token / client_credentials / password
  四种 grant,含 discovery / introspect / userinfo / jwks 占位
- IAP:access 阶段门禁,注入 X-Auth-* 到后端,客户端伪造同名头被覆盖
- MaxKey 互通:/oauth/v20/* 与 /oauth2.0/* 别名 + context-path 归一化 + 扁平字段
- 管理接口:账号(含角色/部门/用户组)与 OAuth 接入方注册表,热写穿共享内存
- 模块改名 ngx_http_sso_module,指令 sso_state / sso_data_dir / sso_api / sso_iap,
  Cookie sso_sid(终端用户)与 sso_admin_session(管理员)两套身份两套会话

配置(source/conf)
- nginx.conf 生产模板(TLS、IAP 子域、含 auth_sub 的日志格式)
- nginx.test.conf 本机验证配置(明文 18095-18099)
- tls/README.md 证书约定与公钥比对校验

前端(source/www/admin.html)
- 三 tab:账号管理 / 接入方 / 接入指南;去掉审核队列、域名、LLM 上游、用量统计

测试与脚本
- tests/ 三套端到端:协议基线 14 项 · MaxKey 互通与接入 24 项 · 管理接口 31 项,全绿
- tests/demo-system 演示业务系统(Python 标准库),覆盖应用自建客户端与 IAP 零改造两种姿势
- scripts/run-local-e2e.sh 本机一键起停;scripts/seed-data.sh 物化运行时数据目录

文档
- README.md 概览与快速开始;CLAUDE.md 硬约定与排障速查
- docs/design.md 完整设计;docs/roadmap.md 待执行任务(v2 RS256+JWKS、用户管理界面)
2026-09-12 21:42:06 +08:00

230 lines
9.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ============================================================================
# 本地(macOS / Linux)端到端验证:本机编的 nginx 1.30.4
# + 本机 cargo 编的 SSO 动态模块
# + tests/demo-system(简单业务系统)
#
# 不依赖 Docker。动态模块必须对着「同一份 nginx 源码 + 相同 configure」的
# nginx 二进制编译加载,所以本脚本假定:
# - nginx 源码树:${NGINX_SRC}(默认 /tmp/sso-build/src/nginx-1.30.4)
# - nginx 二进制:${NGINX_BIN}(默认 $NGINX_SRC/objs/nginx)
# - Rust 模块 :$ROOT/source/rust/target/release/libsso_gateway.{dylib,so}
#
# 会一并拉起 demo-system(Python 标准库,:18096),用于验证:
# - MaxKey 端点别名 / context-path 归一化 (tests/oauth2-maxkey-e2e.sh)
# - 业务系统自己当 OAuth2 客户端(授权码流)
# - 业务系统经 IAP(:18095)零改造接入
#
# 用法:
# bash scripts/run-local-e2e.sh # 起服务 + 跑三套 e2e + 关服务
# bash scripts/run-local-e2e.sh --keep # 测试后保留进程(手动排查)
# NGINX_BIN=/path/to/nginx bash scripts/run-local-e2e.sh
# OAUTH_COOKIE_DOMAIN=.example.com bash scripts/run-local-e2e.sh # 跨子域 SSO
# ============================================================================
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/.." && pwd)"
NGINX_SRC="${NGINX_SRC:-/tmp/sso-build/src/nginx-1.30.4}"
NGINX_BIN="${NGINX_BIN:-$NGINX_SRC/objs/nginx}"
MIME_TYPES="${MIME_TYPES:-$NGINX_SRC/conf/mime.types}"
PYTHON="${PYTHON:-python3}"
# macOS 上 cdylib 产物是 .dylib;Linux 是 .so。优先 .dylib,回退 .so。
MODULE_DIR="$ROOT/source/rust/target/release"
if [ -f "$MODULE_DIR/libsso_gateway.dylib" ]; then
MODULE="$MODULE_DIR/libsso_gateway.dylib"
else
MODULE="$MODULE_DIR/libsso_gateway.so"
fi
CONF_SRC="$ROOT/source/conf/nginx.test.conf"
WWW_DIR="$ROOT/source/www"
DEMO_APP="$ROOT/tests/demo-system/app.py"
DATA_DIR="$ROOT/source/data"
CONF_LOCAL="/tmp/sso-nginx-test.conf"
PID="/tmp/sso-nginx.pid"
ERR_LOG="/tmp/sso-nginx-error.log"
DEMO_LOG="/tmp/sso-demo-app.log"
HOST_IDP="http://127.0.0.1:18098"
HOST_IAP="http://127.0.0.1:18099"
HOST_DEMO="http://127.0.0.1:18096"
HOST_IAP_DEMO="http://127.0.0.1:18095"
KEEP=0
for a in "$@"; do [ "$a" = "--keep" ] && KEEP=1; done
echo "## [precheck]"
# nginx prefix 临时目录(源码 make 出来的二进制没跑 make install,需自建)
NGINX_PREFIX="$(dirname "$(dirname "$NGINX_BIN")")"
mkdir -p "$NGINX_PREFIX"/{client_body_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp,logs,conf}
[ -x "$NGINX_BIN" ] || {
echo "!! nginx 二进制缺失:$NGINX_BIN" >&2
echo " 先编译(源码树内直接 make,不要 --prefix):" >&2
echo " cd $NGINX_SRC && ./configure --with-pcre=$(dirname "$NGINX_SRC")/pcre2-10.44 \\" >&2
echo " --with-http_realip_module --without-http_gzip_module --without-http_ssi_module \\" >&2
echo " --without-http_userid_module --without-http_autoindex_module --without-http_geo_module \\" >&2
echo " --without-http_split_clients_module --without-http_referer_module \\" >&2
echo " --without-http_uwsgi_module --without-http_scgi_module --without-http_grpc_module && make" >&2
exit 1
}
[ -f "$MODULE" ] || {
echo "!! Rust 模块缺失:$MODULE" >&2
echo " cd $ROOT/source/rust && NGINX_SOURCE_DIR=$NGINX_SRC NGINX_BUILD_DIR=$NGINX_SRC/objs cargo build --release" >&2
exit 1
}
[ -f "$MIME_TYPES" ] || { echo "!! mime.types 缺失:$MIME_TYPES" >&2; exit 1; }
[ -f "$DEMO_APP" ] || { echo "!! demo 系统缺失:$DEMO_APP" >&2; exit 1; }
# ---------------------------------------------------------------------------
# 种子数据:source/data-seed/ -> source/data/(幂等,不覆盖已有 users.json)
# ---------------------------------------------------------------------------
echo "## [seed] 物化数据目录 $DATA_DIR"
bash "$HERE/seed-data.sh"
# 生成本机配置(把模板里的绝对路径换成本机路径)
#
# 用 python 而不是 sed:BSD sed 对 `/re/{s###;}` 这类块命令 + `#` 分隔符的解析
# 很脆(会报 `bad flag in substitute command: '}'`),改一行踩一次坑。这里做确定性的
# 逐行替换,顺带把正则 server_name 降级为普通 server_name(本机无 PCRE 也能起)。
echo "## [conf] 生成本机配置 $CONF_LOCAL"
MODULE="$MODULE" MIME_TYPES="$MIME_TYPES" DATA_DIR="$DATA_DIR" ERR_LOG="$ERR_LOG" PID="$PID" \
CONF_SRC="$CONF_SRC" CONF_LOCAL="$CONF_LOCAL" WWW_DIR="$WWW_DIR" \
"$PYTHON" - <<'PYEOF'
import os
src = open(os.environ["CONF_SRC"], encoding="utf-8").read().splitlines()
out = []
for ln in src:
s = ln.strip()
if s.startswith("load_module "):
out.append("load_module %s;" % os.environ["MODULE"]); continue
if s.startswith("include ") and "mime.types" in s:
out.append(" include %s;" % os.environ["MIME_TYPES"]); continue
# 生产模板里的 TLS 子目录本机不存在,跳过(否则 nginx 起不来)
if s.startswith("include ") and "/conf/tls/" in s:
out.append(" # include conf/tls/*.conf; # 本机验证配置不需要 TLS")
continue
if s.startswith("sso_data_dir"):
out.append(" sso_data_dir %s;" % os.environ["DATA_DIR"]); continue
if s.startswith("error_log"):
out.append("error_log %s info;" % os.environ["ERR_LOG"]); continue
if s.startswith("pid "):
out.append("pid %s;" % os.environ["PID"]); continue
# 管理台静态页:模板里的 /usr/local/nginx/www → 仓库 source/www/
if "/usr/local/nginx/www/" in ln:
out.append(ln.replace("/usr/local/nginx/www/", os.environ["WWW_DIR"] + "/")); continue
# 正则 server_name(生产用子域捕获)→ 普通 server_name:本机 nginx 无 PCRE
if s.startswith("server_name ~"):
out.append(" server_name _;"); continue
if s == "env ADMIN_INIT_PASSWORD;":
out.append(ln)
out.append("env OAUTH_SIGNING_SECRET;")
out.append("env OAUTH_ISSUER;")
continue
out.append(ln)
open(os.environ["CONF_LOCAL"], "w", encoding="utf-8").write("\n".join(out) + "\n")
print(" wrote %s (%d lines)" % (os.environ["CONF_LOCAL"], len(out)))
PYEOF
# 设置 OAuth 环境变量(确定性密钥 + 与访问 host 一致的 issuer)
export OAUTH_SIGNING_SECRET="${OAUTH_SIGNING_SECRET:-local-test-secret-CHANGE-ME}"
export OAUTH_ISSUER="${OAUTH_ISSUER:-http://127.0.0.1:18098}"
# 跨子域 SSO 时才需要;不设 = 不写 Domain(主机级 cookie)
if [ -n "${OAUTH_COOKIE_DOMAIN:-}" ]; then export OAUTH_COOKIE_DOMAIN; fi
# 关掉可能残留的旧实例
if [ -f "$PID" ]; then kill "$(cat "$PID")" 2>/dev/null || true; sleep 0.5; fi
pkill -f "demo-system/app.py" 2>/dev/null || true
# ---------------------------------------------------------------------------
# 启动 demo-system(简单业务系统)
# ---------------------------------------------------------------------------
echo "## [demo] 启动 demo-system(style=maxkey, :18096)"
DEMO_SECRET="$("$PYTHON" - "$DATA_DIR/oauth_clients.json" <<'PY'
import json, sys
for c in json.load(open(sys.argv[1])):
if c["client_id"] == "demo-system":
print(c.get("client_secret", "")); break
PY
)"
: > "$DEMO_LOG"
DEMO_CLIENT_SECRET="$DEMO_SECRET" \
DEMO_STYLE=maxkey \
DEMO_PORT=18096 \
DEMO_IDP_BASE="$HOST_IDP" \
DEMO_REDIRECT_URI="$HOST_DEMO/oauth2/callback" \
"$PYTHON" "$DEMO_APP" >> "$DEMO_LOG" 2>&1 &
DEMO_PID=$!
echo " demo pid=$DEMO_PID (日志 $DEMO_LOG)"
# 后面几处失败分支都要清理 demo 进程,否则脚本退出后残留端口占用
cleanup_demo() { kill "${DEMO_PID:-0}" 2>/dev/null || true; }
for i in $(seq 1 25); do
if curl -sf -o /dev/null "$HOST_DEMO/healthz" 2>/dev/null; then echo " demo 就绪"; break; fi
if [ "$i" = "25" ]; then
echo "!! demo-system 未就绪,日志:" >&2; tail -20 "$DEMO_LOG" >&2
cleanup_demo; exit 1
fi
sleep 0.4
done
echo "## [run] 启动 nginx(daemon off)"
"$NGINX_BIN" -c "$CONF_LOCAL" -g 'daemon off;' -e "$ERR_LOG" &
NGX_PID=$!
sleep 1.5
if ! kill -0 "$NGX_PID" 2>/dev/null; then
echo "!! nginx 启动失败,错误日志:" >&2; tail -30 "$ERR_LOG" >&2
cleanup_demo; exit 1
fi
echo " nginx pid=$NGX_PID"
echo "## [wait] 等待 IdP discovery 就绪 ..."
for i in $(seq 1 30); do
code="$(curl -s -o /dev/null -w '%{http_code}' "$HOST_IDP/.well-known/openid-configuration" || true)"
if [ "$code" = "200" ]; then echo " IdP 就绪"; break; fi
if [ "$i" = "30" ]; then
echo "!! IdP 未就绪,日志:" >&2; tail -30 "$ERR_LOG" >&2
kill "$NGX_PID" 2>/dev/null || true; cleanup_demo; exit 1
fi
sleep 1
done
# ---------------------------------------------------------------------------
# 跑测试(三套,各自独立计分)
# ---------------------------------------------------------------------------
RC=0
set +e
echo
echo "############### [1/3] OAuth2 / OIDC 基线 ###############"
bash "$ROOT/tests/oauth2-e2e.sh" "$HOST_IDP" "$HOST_IAP" "$DATA_DIR"
RC1=$?
echo
echo "############### [2/3] MaxKey 互通 + demo 系统接入 ###############"
bash "$ROOT/tests/oauth2-maxkey-e2e.sh" "$HOST_IDP" "$HOST_DEMO" "$HOST_IAP_DEMO" "$DATA_DIR"
RC2=$?
echo
echo "############### [3/3] 管理接口:OAuth 接入方 ###############"
bash "$ROOT/tests/admin-oauth-clients-e2e.sh" "$HOST_IDP" "$DATA_DIR"
RC3=$?
set -e
[ "$RC1" != "0" ] && RC=1
[ "$RC2" != "0" ] && RC=1
[ "$RC3" != "0" ] && RC=1
echo
echo "## [summary] 基线 rc=$RC1 互通/接入 rc=$RC2 管理接口 rc=$RC3"
if [ "$KEEP" = "1" ]; then
echo "## [done] 测试完成 (rc=$RC);进程保留,手动排查:"
echo " kill $NGX_PID $DEMO_PID"
else
kill "$NGX_PID" 2>/dev/null || true
kill "$DEMO_PID" 2>/dev/null || true
echo "## [done] nginx + demo-system 已停止 (rc=$RC)"
fi
exit $RC