- plugin-domain 7 文件取上游 ddd4172+(config/store 独立、update_domains 三泛型) - 路由对齐上游:purge 移至 /api/admin/purge;开关指令 sso_domain → ipass_proxy - 未命中语义对齐上游:access 未命中由插件直接 404(不落转发层兜底), e2e zone 外 Host 断言同步修正 - nginx.conf / nginx.test.conf:sso_state/data_dir/api → ipass_*(sso_iap 保留, sso 插件特有开关)
206 lines
8.9 KiB
Plaintext
206 lines
8.9 KiB
Plaintext
# ============================================================================
|
||
# SSO 统一认证网关 —— 本机/容器验证配置
|
||
#
|
||
# 只启「认证层」,不含生产 TLS、不含真实业务后端。用途:
|
||
# * 手工观察 IdP 与 IAP 行为
|
||
# * `scripts/run-local-e2e.sh` 起停本配置跑三套端到端
|
||
#
|
||
# 明文 server:
|
||
# 18098 IdP + 管理台:/oauth/*、/oauth/v20/*(MaxKey 别名)、
|
||
# /.well-known/*、/login、/admin、/api/*
|
||
# 18099 IAP:受保护代理,注入 X-Auth-* 到 18097 回显后端(断言用)
|
||
# 18097 echo-backend:把收到的请求头原样回显为 JSON(用于断言 IAP 注入)
|
||
# 18095 IAP 接入 demo 业务系统:前置认证 + 注入 X-Auth-* → 18096
|
||
# 18094 企业域名转发:sub:<sub> → $proxy_backend_addr(域名插件)
|
||
# 18093 域名转发的回显后端:回显收到的 Host 与 URI,供断言
|
||
# 18096 demo 系统本体(**不由 nginx 监听**,是 tests/demo-system/app.py
|
||
# 自己起的进程;直连 18096 时它自己做 OAuth2 客户端,经 18095 访问时
|
||
# 网关已完成认证并注入身份,应用零改造)
|
||
#
|
||
# 跑法:scripts/run-local-e2e.sh(会自动把下面的绝对路径改成本机路径)
|
||
# ============================================================================
|
||
|
||
load_module /usr/local/nginx/modules/libsso_gateway.so;
|
||
|
||
worker_processes auto;
|
||
error_log /usr/local/nginx/logs/error.log info;
|
||
pid /usr/local/nginx/logs/nginx.pid;
|
||
|
||
# nginx 默认会剥掉所有环境变量,机制类配置必须显式白名单。
|
||
# 未设置时不报错,模块按各自缺省值兜底(见 source/rust/src/config.rs)。
|
||
env ADMIN_INIT_PASSWORD;
|
||
env OAUTH_SIGNING_SECRET;
|
||
env OAUTH_ISSUER;
|
||
env OAUTH_COOKIE_DOMAIN;
|
||
|
||
events {
|
||
worker_connections 1024;
|
||
}
|
||
|
||
http {
|
||
include mime.types;
|
||
default_type application/json;
|
||
|
||
# TLS 子配置目录(生产用;本验证配置不依赖,空目录不影响启动)
|
||
include /usr/local/nginx/conf/tls/*.conf;
|
||
|
||
# 共享内存 + 数据目录(OAuth 接入方注册表从这里加载)
|
||
ipass_state state 10m;
|
||
ipass_data_dir /usr/local/nginx/data;
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18097 回显后端:把注入进来的 X-Auth-* 请求头回显成 JSON,供断言
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18097;
|
||
location / {
|
||
default_type application/json;
|
||
return 200 '{"x_auth_user":"$http_x_auth_user","x_auth_email":"$http_x_auth_email","x_auth_groups":"$http_x_auth_groups","x_auth_sub":"$http_x_auth_sub","x_auth_token":"$http_x_auth_token","remote_addr":"$remote_addr"}';
|
||
}
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18098 IdP + 管理台
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18098 default_server;
|
||
server_name sso.yourdomain.com;
|
||
|
||
client_max_body_size 2m;
|
||
client_body_buffer_size 1m;
|
||
|
||
# 管理台单页(数据全部通过 /api/admin/* 拉取,故与 IdP 同端口不影响端点语义)
|
||
location = /admin {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
location = /admin/ {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
|
||
# 全部交给模块分发:MaxKey 别名在 /oauth/v20/*、/oauth2.0/*,还有
|
||
# /maxkey/oauth/v20/* 这类带 context-path 的写法(模块内做前缀归一化),
|
||
# 逐条列 location 必然漏,故整站交给 sso_api。
|
||
location / { ipass_api on; }
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18099 IAP:受保护代理示例(注入 X-Auth-* 到回显后端)
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18099 default_server;
|
||
server_name ~^(?<subdomain>.+)\.app\.yourdomain\.com$;
|
||
|
||
location / {
|
||
# 门禁:IAP 身份感知代理开关(access 阶段)
|
||
sso_iap on;
|
||
|
||
# 本验证配置用固定后端;生产用按环境生成的 proxy_pass
|
||
proxy_pass http://127.0.0.1:18097;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
||
# ---- IAP 身份注入:网关断言覆盖客户端伪造同名头 ----
|
||
# 后端只信这五个头,它们由网关在 access 阶段写入(见 lib.rs set_auth_vars)
|
||
proxy_set_header X-Auth-User $auth_user;
|
||
proxy_set_header X-Auth-Email $auth_email;
|
||
proxy_set_header X-Auth-Groups $auth_groups;
|
||
proxy_set_header X-Auth-Sub $auth_sub;
|
||
proxy_set_header X-Auth-Token $auth_token;
|
||
|
||
# 无身份(401)→ 内部跳登录页并回跳
|
||
error_page 401 = @oauth_login;
|
||
}
|
||
|
||
location @oauth_login {
|
||
return 302 "http://127.0.0.1:18098/login?redirect=$scheme://$host$request_uri";
|
||
}
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18095 IAP 接入:把统一认证前置于 demo 业务系统
|
||
#
|
||
# 与 18099 的区别:18099 指向回显后端(只为断言请求头),本 server 指向真正的
|
||
# 业务系统,演示**应用零改造接入**——应用不写 OAuth 代码,只读 X-Auth-* 头。
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18095 default_server;
|
||
server_name 127.0.0.1;
|
||
|
||
location / {
|
||
sso_iap on;
|
||
|
||
proxy_pass http://127.0.0.1:18096;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
||
proxy_set_header X-Auth-User $auth_user;
|
||
proxy_set_header X-Auth-Email $auth_email;
|
||
proxy_set_header X-Auth-Groups $auth_groups;
|
||
proxy_set_header X-Auth-Sub $auth_sub;
|
||
proxy_set_header X-Auth-Token $auth_token;
|
||
|
||
error_page 401 = @oauth_login_demo;
|
||
}
|
||
|
||
location @oauth_login_demo {
|
||
return 302 "http://127.0.0.1:18098/login?redirect=$scheme://$host$request_uri";
|
||
}
|
||
}
|
||
# ------------------------------------------------------------------
|
||
# 18093 域名转发回显后端:断言「子域名确实被转发到了目标后端」
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18093;
|
||
location / {
|
||
default_type application/json;
|
||
return 200 '{"backend":"18093","host":"$host","uri":"$request_uri"}';
|
||
}
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18094 企业域名转发(域名插件)
|
||
#
|
||
# 与 IAP 的区别:IAP 判定的是「你是谁」,这里判定的是「你要去哪台后端」。
|
||
# access 阶段从 Host 取子域名 → 查 sub: 共享内存 → 写入 $proxy_backend_addr,
|
||
# 由下面的 proxy_pass 转发;未命中(不存在 / 待审核 / 已过期)直接 404,
|
||
# 热路径**永不回源**。
|
||
#
|
||
# 本机无 PCRE,故不写正则 server_name(生产的正则见 conf/nginx.conf):
|
||
# 子域名判定完全由插件按 Host 后缀完成,不依赖 server_name 匹配。
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18094 default_server;
|
||
server_name _;
|
||
|
||
location / {
|
||
# 转发开关(access 阶段解析 Host 并注入 $proxy_backend_addr)
|
||
ipass_proxy on;
|
||
|
||
proxy_pass http://$proxy_backend_addr;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
||
# 本机无 PCRE,只能用 `server_name _`,于是 zone 外的 Host 也会进到这里;
|
||
# 此时插件已 decline、$proxy_backend_addr 为空,空 upstream 会报 500。
|
||
# 生产用正则 server_name(见 conf/nginx.conf),这类请求根本进不来。
|
||
#
|
||
# ⚠️ 不能用 `if ($proxy_backend_addr = "")`:`if` 在 **rewrite** 阶段求值,
|
||
# 而变量是 **access** 阶段才写入的 —— 合法子域名会被一起判成未解析。
|
||
error_page 500 502 503 = @no_backend;
|
||
}
|
||
|
||
# 兜底:转发层没有可用后端(body 是固定标记,便于与插件自身的 404 区分)
|
||
location @no_backend {
|
||
return 404 "NO_BACKEND";
|
||
}
|
||
}
|
||
}
|