Files
sso/scripts/local-serve.sh
T
ssw 9ac47f7942 feat: 凭据改 .env 注入 + 本地联调脚本 + nginx 域名转发配置
凭据管理
  新增 .env.example(**进版本库**,只放本机/CI 演示默认值)与 scripts/load-env.sh
  (统一读取项目根 .env,已存在的环境变量优先)。三套既有 e2e 改为从 .env 取
  口令与签名密钥,命令行不再出现任何密钥 —— 既避免误入库,也避开终端历史泄露。

本地联调
  scripts/local-serve.sh start|stop|restart:按 .env 起停 nginx 与 demo 系统,
  替代手工拼路径。

nginx 配置
  nginx.test.conf 新增 18093(转发回显后端)+ 18094(sso_domain on +
  proxy_pass http://$proxy_backend_addr),并补 error_page 500 502 503 =
  @no_backend 兜底。
  nginx.conf(生产模板)新增企业域名转发 server:
  server_name ~^(?<subdomain>.+dm)\.mayishangshu\.cn$,443 与 80 跳转各一个。

  为什么不给空上游用 `if ($proxy_backend_addr = "")` 兜底:`if` 在 **rewrite**
  阶段求值,而变量是 **access** 阶段才写入的 —— 合法子域名会被一起误判成
  未解析。改用 error_page + 固定 body 标记(NO_BACKEND),顺带把「转发层兜底」
  与「插件自己发的 404」区分开,e2e 断言才有意义。
2026-09-13 10:25:19 +08:00

82 lines
3.2 KiB
Bash
Executable File
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
# ============================================================================
# 本地起停 SSO 网关(手工联调用,与 scripts/run-local-e2e.sh 同一套配置)
#
# 与 run-local-e2e.sh 的区别:那个跑完就停进程,这个是常驻,
# 用来开着浏览器点管理台、或者手工 curl 端点。
#
# 用法:
# bash scripts/local-serve.sh start # 起(默认)
# bash scripts/local-serve.sh stop # 停
# bash scripts/local-serve.sh restart
#
# 所有凭据来自项目根 .env(不进 git),命令行里不出现任何密钥。
# ============================================================================
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/.." && pwd)"
# shellcheck source=scripts/load-env.sh
. "$HERE/load-env.sh"
NGINX_SRC="${NGINX_SRC:-/tmp/sso-build/src/nginx-1.30.4}"
NGINX_BIN="${NGINX_BIN:-$NGINX_SRC/objs/nginx}"
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="/tmp/sso-nginx-test.conf"
PID="/tmp/sso-nginx.pid"
SHELL_PID="/tmp/sso-nginx-shell.pid"
ERR_LOG="/tmp/sso-nginx-error.log"
# 签名密钥 / issuer:缺省值兜底,正常情况都由 .env 提供
export OAUTH_SIGNING_SECRET="${OAUTH_SIGNING_SECRET:-$(openssl rand -hex 32)}"
export OAUTH_ISSUER="${OAUTH_ISSUER:-http://127.0.0.1:18098}"
if [ -n "${OAUTH_COOKIE_DOMAIN:-}" ]; then export OAUTH_COOKIE_DOMAIN; fi
stop() {
[ -f "$PID" ] && kill "$(cat "$PID")" 2>/dev/null || true
[ -f "$SHELL_PID" ] && kill "$(cat "$SHELL_PID")" 2>/dev/null || true
rm -f "$PID" "$SHELL_PID"
echo "stopped"
}
case "${1:-start}" in
stop) stop; exit 0 ;;
restart) stop; sleep 0.5 ;;
start) ;;
*) echo "用法: bash $0 [start|stop|restart]" >&2; exit 2 ;;
esac
[ -x "$NGINX_BIN" ] || { echo "!! nginx 二进制缺失:$NGINX_BIN" >&2; exit 1; }
[ -f "$MODULE" ] || { echo "!! Rust 模块缺失:$MODULE(先 cargo build --release)" >&2; exit 1; }
[ -f "$CONF" ] || {
# 没现成配置就复用 run-local-e2e.sh 的生成逻辑(它会连带物化种子数据)
echo "## 没有 $CONF,先跑一次 run-local-e2e.sh 生成配置 ..."
bash "$HERE/run-local-e2e.sh" --keep > /dev/null 2>&1 || true
if [ -f "$PID" ]; then kill "$(cat "$PID")" 2>/dev/null || true; fi
pkill -f "demo-system/app.py" 2>/dev/null || true
[ -f "$CONF" ] || { echo "!! 配置生成失败" >&2; exit 1; }
}
"$NGINX_BIN" -c "$CONF" -g 'daemon off;' -e "$ERR_LOG" > /tmp/sso-nginx-stdout.log 2>&1 &
echo $! > "$SHELL_PID"
for i in $(seq 1 20); do
code="$(curl -s -o /dev/null -w '%{http_code}' "${OAUTH_ISSUER}/.well-known/openid-configuration" || true)"
[ "$code" = "200" ] && break
sleep 0.5
done
code="$(curl -s -o /dev/null -w '%{http_code}' "${OAUTH_ISSUER}/.well-known/openid-configuration" || true)"
if [ "$code" != "200" ]; then
echo "!! 网关未就绪(HTTP $code),日志:" >&2; tail -30 "$ERR_LOG" >&2; exit 1
fi
echo "started: $OAUTH_ISSUER (discovery=$code)"
echo " admin: ${ADMIN_USER:-admin} / (口令见 .env 的 ADMIN_PASSWORD)"
echo " 停止: bash $0 stop"