chore: SSO-only 精简版初始提交(从 ipass-all 派生)
**影响**:新建独立仓 ipass-sso 作为 ipass-all 的 SSO-only 精简发行版—— 剔除了 domain / llm / apix / cache / waf 5 个插件及对应的 nginx location 与 LLM/域名数据面 server,PLUGINS 数组只剩 sso + core,单 .so 体积与编 译时间同步下降。镜像名沿用 ssw188/nginx(同 repo 加 tag),新 tag `ipass-sso-v0.6.0`,K8s 部署在 wuguirong ns NodePort 30082,与生产 ipass-all-v0.5.2 (30083/30084/30085) 并存互不干扰。 **改动**: - `rust/Cargo.toml` + `rust/gateway/Cargo.toml`:删 5 个 plugin path 依赖, package/lib 重命名为 `ipass-sso-gateway` / `libipass_sso_gateway` - `rust/gateway/src/lib.rs`:PLUGINS 只剩 `&SsoPlugin` + `&CorePlugin`; GatewayExt 4 个方法改为 no-op / 返回默认值(无 domain/llm 装饰); ModuleConfig 只剩 api + iap 两个开关;NGX_HTTP_COMMANDS 数组 11→4 条; init_process / exit_process 删 llm audit / stats persist 调用; state_zone_init 删 domain/upstreams/apix 的预装载 - `rust/gateway/src/api.rs`:删除(h_admin_trend 依赖 llm + apix) - `deploy/nginx.conf`:删 server 498 (LLM) + server 565 (域名); 主 server 删 `/apix/` `/apixp/` `/_apix_probe/` 等 7 个 location; 删 6 条 `proxy_cache_path` 与 8 个 cache location;删 `ipass_audit_dir` 指令 - `deploy/Dockerfile`:删 5 个 plugin named context 的 COPY 块; .so 文件名改 `libipass_sso_gateway.so` - `deploy/build.sh`:TAG 默认 `ipass-sso-v0.6.0`; 5 个 plugin build-context 删除;删 collect-legacy.sh 调用 - `data-seed/plugin-config.json`:只保留 `sso` 一个 plugin - `scripts/collect-plugin-dist.sh`:PLUGINS 默认值改 `sso` - `scripts/container-entrypoint.sh`:删 cache/audit 目录预创建 - `deploy/legacy/` `tests/` `docs/` `deploy/custom/` `deploy/compose.yml`: 精简版不需要,删除 - `deploy/k8s/ipass-sso.yaml`:新建精简版 K8s manifest (NodePort 30082 单端口;replicas=1;同 10106 准入 + nginx-data PVC sso/ subPath; 探针用 `/admin/` try_files 兜底,与 ipass-all 同款止血方案) **设计**:保留 ipass-core("core 是网关身份底座,永不跳过")+ ipass-plugin-sso, 其余插件整链剥离;不再维护 ssow/ssw 时代的多插件装配文档。剔除 ipass_audit_dir 是因为精简版没有 LLM 对话审计,剔除 proxy_cache_path 同理。K8s 部署沿用 nginx-data PVC + subPath 隔离模式(sso/ 与 ipass-all/ 互不干扰)。 **已知约束**:精简版仍是单进程共享内存,pod 重启使所有管理员/用户会话失效 (与 ipass-all 同款代价)。精简版不带单元/E2E 测试,老管理台 iframe 集成 也未引入。 Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
commit
c3689d501e
21 files changed
+3964
No files matched your search
+19
@@ -0,0 +1,19 @@
|
||||
# 运行时数据(含口令散列与 Key),绝不入库
|
||||
data/
|
||||
|
||||
# 前端收集产物由 collect-plugin-dist.sh 再生(保留 .gitkeep 目录占位)
|
||||
deploy/shell/*
|
||||
!deploy/shell/.gitkeep
|
||||
deploy/static-plugins/*
|
||||
!deploy/static-plugins/.gitkeep
|
||||
|
||||
# Rust
|
||||
rust/target/
|
||||
|
||||
# 本机调试
|
||||
*.log
|
||||
.DS_Store
|
||||
.env
|
||||
|
||||
# e2e 运行时产物(cookie/临时 conf/审计/陈旧快照)
|
||||
tests/.run/
|
||||
@@ -0,0 +1,201 @@
|
||||
# CLAUDE.md
|
||||
|
||||
给 AI 协作者的项目说明。本仓是 **ipass-all**:把 iPass 内核与全部插件装配成
|
||||
**一个** nginx 动态模块 / **一个**容器 / **一个** K8s Deployment。
|
||||
|
||||
## 一句话定位
|
||||
|
||||
iPass 的「全家桶」发行版:`ipass-core`(内核)+ 6 个 crate 级插件
|
||||
(sso / domain / llm / apix / cache / waf)+ trend 组合端点,编译期装配进
|
||||
单个 `libipass_all_gateway.so`。单进程、零外部依赖(无 DB / Redis / MQ)。
|
||||
|
||||
**为什么单 .so**:nginx 动态模块无稳定 ABI,多 .so 会撞共享内存 zone tag 冲突
|
||||
(启动即 EMERG)与同名变量互相覆盖。论证见 `ipass-sso/docs/plugin-architecture-analysis.md`。
|
||||
|
||||
## 依赖布局(兄弟仓库,编译期 path 依赖)
|
||||
|
||||
```
|
||||
../ipass-core 内核 + vendored ngx-rust(crates.io/git 版 ngx 不可用,见下)
|
||||
../ipass-plugin-sso 身份:OAuth2/OIDC + IAP(dispatch 排第一,认领身份端点)
|
||||
../ipass-plugin-domain 动态域名($proxy_backend_addr)
|
||||
../ipass-plugin-llm LLM 代理($llm_* + 用量统计 + 对话审计)
|
||||
../ipass-plugin-apix API 代理($apix_* / $apixp_*;依赖 plugin-llm)
|
||||
../ipass-plugin-cache 制品缓存($cache_*;上游表管理台配,改上游不 reload)
|
||||
../ipass-plugin-waf WAF(默认 enabled + 内置规则随 .so 分发;vendor/zentinel-modsec 在其仓内)
|
||||
```
|
||||
|
||||
⚠️ `ngx` 必须用 `../ipass-core/rust/vendor/ngx-rust`(path 依赖):crates.io 的
|
||||
0.5.1 没发请求阶段 API;git 依赖会克隆 nginx 仓库在国内容 networks 永久挂起。
|
||||
|
||||
## 本机构建(快速检查用)
|
||||
|
||||
```bash
|
||||
cd rust
|
||||
export LIBCLANG_PATH=/Library/Developer/CommandLineTools/usr/lib
|
||||
export NGINX_SOURCE_DIR=$HOME/Desktop/workspace/ipass/ngxbuild/nginx-1.30.4
|
||||
export NGINX_BUILD_DIR=$NGINX_SOURCE_DIR/objs
|
||||
cargo check --release # 或 cargo build --release 出 .dylib
|
||||
```
|
||||
|
||||
nginx 源码树不可 `mv`(`objs/Makefile` 里的 `-I` 是绝对路径,挪了要重新 configure)。
|
||||
|
||||
## 容器构建 / 部署
|
||||
|
||||
```bash
|
||||
bash deploy/build.sh # 收集前端 dist → docker build(7 个 named context)→ push
|
||||
kubectl apply -f deploy/k8s/ipass-all.yaml
|
||||
```
|
||||
|
||||
镜像 tag 约定:`crpi-...aliyuncs.com/ssw188/nginx:ipass-all-vX.Y.Z`(只推 ssw188/nginx
|
||||
这一个 repo,不建新 repo)。**节点 imagePullPolicy=IfNotPresent,新版必须换新 tag。**
|
||||
|
||||
K8s 约束(wuguirong 租户,细节见 Claude 技能 k8s-tenant-wuguirong):
|
||||
- 容器级 `runAsUser: 10106`(准入强制,pod 级无效);非 root → conf 里 pid/temp 全在 /tmp
|
||||
- 无新 PVC 可用 → 复用共享 PVC `nginx-data`,subPath `ipass-all/`(initContainer 以
|
||||
10106 预建目录,否则 kubelet 按 root 建目录,业务写盘才炸)
|
||||
- **replicas 必须 = 1**:运行期真源是进程内共享内存 + JSON 写穿,多副本 = 脑裂
|
||||
- 暴露:NodePort 30083(管理台/IdP/apix/cache)+ 30084(LLM)
|
||||
|
||||
## 装配层(rust/gateway/src/lib.rs)要点
|
||||
|
||||
- `PLUGINS` 数组顺序 = preconfig/postconfig/dispatch 顺序:**SsoPlugin 第一**
|
||||
(认领 /api/admin|v1 身份端点,带 CAPTCHA/锁定/审计),core 补 sso 没有的端点,
|
||||
之后 domain → llm → apix → cache → waf → GatewayRoutes(trend)。
|
||||
- 指令集中在 gateway(10 条):main `ipass_state/ipass_data_dir/ipass_audit_dir/
|
||||
ipass_stats_persist/ipass_probe_min`;loc `ipass_api/sso_iap/ipass_proxy/ipass_llm/ipass_apix`。
|
||||
⚠️ cache 插件**没有** `ipass_cache` 指令(legacy 独立仓形态才有)——它靠路由前缀
|
||||
+ R19 总闸生效,conf 里写 `ipass_cache on` 会 unknown directive 启动失败。
|
||||
- `state_zone_init` 两层都要:显式装载(domains/users/llm upstreams/apix)+
|
||||
各插件 `zone_load()` 回调(sso 的 OAuth 接入方表靠它)。
|
||||
- `GatewayExt` 承 ssw 完整版(decorate_admin_users / decorate_me / reclaim_on_delete
|
||||
回收域名 / validate_slots)。llm 的 usage_filter 用 `r->ctx[ctx_index]` 请求槽,
|
||||
sso 不用请求槽 → 合流无冲突。
|
||||
- WAF 默认 `enabled:true` 且规则 `include_str!` 进 .so;误拦时改
|
||||
`data/plugin-config.json` 里 `waf.enabled=false` + 重启(改文件需重启,不热生效)。
|
||||
|
||||
## nginx.conf(deploy/nginx.conf)要点
|
||||
|
||||
- 端口分工:18098 主(管理台/IdP/API/apix/cache 路由/域名 Host 转发)、18099 LLM
|
||||
(`/<slot>/` 首段即 slot,不能挂在带前缀的 location 下)。
|
||||
- 管理台静态 location **不开** `ipass_api`;`/_apix_probe//_apixp_probe//_upstream_test/`
|
||||
三个 internal 探针必须与 `/api/` 同 server(内部重定向不出 server)。
|
||||
- cache location **不写** `ipass_cache`(见上);`proxy_cache_path` 必须
|
||||
`use_temp_path=off`(/var/tmp root 属主,10106 写不进)。
|
||||
- `ipass_audit_dir` 必须显式指进数据卷(缺省 = data 兄弟目录,不可写)。
|
||||
- env 白名单在 main 上下文(sso 的 9 个 env;llm/apix/cache/waf 不读 env)。
|
||||
|
||||
## 边界 / 常见坑
|
||||
|
||||
- 改 conf 或 .so 后必须**重启**(reload 不重载动态模块;JSON 是写穿持久化)。
|
||||
- **会话在共享内存里**:每次 pod 重启全部管理员/用户会话失效,要重新登录。
|
||||
- `KNOWN_PLUGINS`(ipass-core)目前没有 "cache" → 壳菜单不显示制品缓存页,
|
||||
插件本身照常工作;要上菜单需给 ipass-core 补登记(上游改动,暂缓)。
|
||||
- `plugin-cache` 仓没有 www/(管理页待重写),壳里自然没有它的菜单。
|
||||
- 前端壳更新:改完 ipass-core/www 或各插件 www 后重跑 `npm run build` + 本仓
|
||||
`scripts/collect-plugin-dist.sh` + 重新构建镜像。
|
||||
|
||||
## 生产迁移与 TLS(2026-09-26 v0.2.9)
|
||||
|
||||
- **生产数据**(`/Users/xs/Desktop/ipass-88-data`:22 账号 / 5 域名 / 4 LLM 上游 /
|
||||
3 API 代理 / 用量快照)已灌入 PVC `ipass-all/`;此前 dev 数据备份在同目录
|
||||
`backup-dev-20260926.tar.gz`。⚠️ 直接改 data/*.json 不热生效——**改完必须重启**。
|
||||
- **TLS**:证书放**数据目录** `data/tls/`(fullchain.cer / tls.key / ca.cer),
|
||||
换证书 = 换文件 + 重启,不重打镜像。证书为多域名证书(SAN 含
|
||||
`*.mayishangshu.cn` 等,**2026-11-04 到期**,来源 ipass-ssw/conf/tls)。
|
||||
- **端口分工**:30083 = TLS only(SNI:mdm/lm/`*`dm.mayishangshu.cn,路由器
|
||||
WAN:188 → 节点 30083,外部入口 `https://mdm.mayishangshu.cn:188`);30085 =
|
||||
管理台/域名代理**明文**(内网调试);30084 = LLM 明文。⚠️ nginx 同一端口不能
|
||||
既明文又 TLS(duplicate listen 启动即炸)。⚠️ 容器端口改名后 Service 的
|
||||
`targetPort` 必须同步(v0.2.9 实测漏改 → 30083 悬空)。
|
||||
- **issuer** = `https://mdm.mayishangshu.cn:188`;`OAUTH_COOKIE_DOMAIN=mayishangshu.cn`
|
||||
只影响 sso 终端用户 cookie;admin 会话 cookie 恒为 host-only,IP 直连登录不受影响。
|
||||
- 生产域名后端(192.168.1.222:23 / .2:8080 / .22:88 / .15:8787)在本环境**不在线**
|
||||
→ 域名转发 404 属预期,后端上线即通;ssw 测试域名 `i29214898dm → 192.168.1.7:89`
|
||||
已并入(.7 同样待开机)。
|
||||
- 向数据目录写文件(certs/JSON)可用一次性 busybox Pod 挂 subPath + base64 内嵌
|
||||
manifest(`kubectl run -i` 的 stdin 在 overrides 场景不可靠;准入要求
|
||||
pod 级 fsGroup=10106 + 容器级 runAsUser=10106)。
|
||||
|
||||
## 管理台架构定案(2026-09-26,v0.4.0)
|
||||
|
||||
1. **ipass-core 壳只带核心域原生页**:账号管理(`AccountAdmin.vue`,唯一可
|
||||
添加/删除/解锁/Key/密码 的地方,**只管身份与凭据**)+ 插件管理
|
||||
(`PluginAdmin.vue`,`POST /api/core/plugins/set` 即时生效 + 落盘)。
|
||||
插件域的用户绑定功能归插件自己:**临时授权**在域名管理(grants.html)、
|
||||
**上游白名单/限额**在 LLM 网关(llm-users.html)——core 账号页不再出现这些列。
|
||||
2. **装配默认用 core 壳,可整体覆写**:装配仓放 `shell/dist/` 即覆盖默认
|
||||
(collect-plugin-dist.sh 已实现);装配自定义页用 `deploy/custom/custom-pages.json`
|
||||
清单(group/path/label/url),core 壳 boot 时拉取注册(无此文件静默跳过),
|
||||
如 ipass-all 的 数据总览/我的用量。
|
||||
3. **插件启用即出顶部菜单**(装载清单驱动);**插件自带左侧菜单**(契约 v2
|
||||
`{title, pages}`)。
|
||||
4. **与用户绑定的功能,插件可有自己的账号列表/按钮**(如 sso 身份页),但
|
||||
**添加/删除/启用等生命周期操作一律跳转 core 账号管理页**(sso.html 已改:
|
||||
`del-user`/`unlock`/创建表单 → `top.location = /manage/users`)。
|
||||
5. 插件页不与 core 页交叉读写——跨域数据靠 API(各插件认领各自路由)。
|
||||
|
||||
## 管理台壳(2026-09-26 排障定案)
|
||||
|
||||
v0.1.5 修复「/admin/ 打开空白」,根因五个、全在**上游仓**,curl 层全绿只有真浏览器
|
||||
才暴露(排障用 agent-browser,注意 intranet 要 `--proxy-bypass`):
|
||||
|
||||
1. 壳无认证接线 → `AppLayout.vue` 401 渲染 `Login.vue`(此前 Login 是死代码);
|
||||
2. API 信封层级 → 壳读 `data.plugins`,后端回 `{code,data:{plugins}}`,被 `?? []`
|
||||
静默吞掉(**登录了也空白**);
|
||||
3. 面板路由从不注册 → AppLayout 现在 `router.addRoute(panel.routes)`;`/` 路由
|
||||
此前指回 AppLayout 自嵌套 → 改 redirect;
|
||||
4. 插件 UMD 引用 `process.env.NODE_ENV` → 壳 main.ts 入口 `window.process ??= {env:{}}`;
|
||||
5. **浏览器原生 `import()` 不做 CJS 互操作**:UMD 包装的 `mod.default` 恒为
|
||||
undefined(契约对象落在 `window['ipass-plugin-<name>-admin']`)→ loader 按
|
||||
`plugin-<name>-admin.es.js → plugin-<name>-admin.umd.js → <name>-admin.umd.js`
|
||||
顺序试,必须拿到 `.default` 才算成功。插件仓一律用 vite **lib 模式**
|
||||
(formats `['umd','es']`,参考 ipass-plugin-domain/www)出双格式。
|
||||
|
||||
## 管理台 v0.2 布局与 legacy 集成(2026-09-26 拍板)
|
||||
|
||||
**布局**:顶部横向插件条切面板;多页面板自动出左侧页菜单,单页直显;右上退出登录。
|
||||
|
||||
**老管理台快速集成**:ssw 九页(git `bf2528d^` 提取)+ sso 五 tab 单页
|
||||
(git `9de4d67^`)+ waf.html(工作树)→ `scripts/collect-legacy.sh` 聚合进
|
||||
`deploy/legacy/`(sed 改 `base href=/legacy/` + 注入 `#shell{display:none}` 藏老
|
||||
导航),Dockerfile 拷到 `www/legacy/`,nginx `/legacy/` 纯静态。老页面 API 与插件
|
||||
路由 1:1 兼容、同源同 cookie,`IP.api` 原样工作。iframe 内 401 落 `login.html`
|
||||
独立兜底。
|
||||
⚠️ `deploy/legacy/` 自 2026-09-26 v0.2.6 起是**可维护副本**:collect 脚本只做
|
||||
首次引导(目标文件已存在则跳过),人工修改(多选弹窗 / 创建时间列 / https 复制 /
|
||||
WAF 免 Key 门禁等)不会被构建覆盖;要从上游重提某页,删掉对应文件再跑脚本。
|
||||
SPA 直链回退要同时覆盖 `/plugin-` 与 `/manage/` 两个前缀(nginx 缺一个就是
|
||||
直链 JSON 404——实测漏过 /manage/)。
|
||||
|
||||
**契约 v2**(`ipass-core/www/src/loader.ts`):插件 default export
|
||||
`{ title, pages: [{path, label, url?}] }`——`url` 给了壳就 iframe 原样嵌(老页
|
||||
/任意同源页),没给用 `component`。五个插件已全部迁到 v2(各仓 www/src/main.ts
|
||||
只是清单,面板 UI 全在壳)。壳内置「网关管理」分组(overview/users/me,core 域
|
||||
无插件 UMD)。⚠️ 聚合去重:内置路由必须带 `meta.builtin:true` 且 `builtinGroups()`
|
||||
只认它——插件页面路由同样带 `meta.group`,聚合若把 `getRoutes()` 全算一遍,
|
||||
每个页面出现两份(实测踩坑)。
|
||||
|
||||
**新插件接入管理台 = 三步**:插件仓 www 用 lib 模式出 es 产物 + main.ts 写
|
||||
`{title, pages}` 清单 + `plugin_registry::KNOWN_PLUGINS` 登记(上游 ipass-core)。
|
||||
|
||||
## WAF 插件三连坑(2026-09-26,v0.2.3 定案)
|
||||
|
||||
控制台「几乎不可用」的元凶是 WAF 默认 CC 限频误封管理员,连带修了两个上游 bug:
|
||||
|
||||
1. **默认 CC 太紧**:`cc_limit=100 次/60s、ban 600s`——管理台一屏 iframe + 每页
|
||||
十几个 API 全从同一 IP 出,几分钟必触发 `{"code":403,"msg":"cc-ban"}`;且
|
||||
**CC 封禁不受 observe 模式豁免**(handler 在 observe 判定之前短路)。
|
||||
现状:`cc_limit=100000`(实际不设限),签名规则仍 observe 记录。
|
||||
2. **`waf_conf.json` 只写不读**(上游 bug,已修):`config::store()` 写穿落盘,
|
||||
但没人读回 → 重启即回退默认值。修复 = `config::bootstrap_from_disk()` 挂在
|
||||
**`WafPlugin::zone_load`**。⚠️ 不能挂 postconfig——那个阶段 shm zone 还没
|
||||
初始化(zone init 在 fork 时才跑),`state::put` 静默无效(v0.2.2 实测踩坑)。
|
||||
3. **`cc_limit=0` 假确认**(上游 bug,已修):校验报错文案说「显式传 cc_window>0
|
||||
留档即可」,代码里没有该分支 → CC 永远关不掉。已按文案补上确认逻辑。
|
||||
|
||||
WAF 管理 API 鉴权用 `sk_` API Key(X-API-Key),**不认管理员会话 cookie**;
|
||||
读配置用 `GET /api/waf/status`(`/api/waf/config` 只有 PUT)。解封单个 IP:
|
||||
`DELETE /api/waf/blocks/<ip>`;pod 重启也会清 shm 里的 ban。
|
||||
|
||||
注意:ipass-sso(:30082)镜像里还是旧壳,复现同款空白;要用新壳需按 ipass-sso
|
||||
仓流程重建镜像重发。
|
||||
@@ -0,0 +1,34 @@
|
||||
# ipass-all
|
||||
|
||||
iPass 全插件一体网关:内核 + 6 插件(身份 sso / 域名 domain / LLM 代理 llm /
|
||||
API 代理 apix / 制品缓存 cache / WAF waf)编译期装配成**单个** nginx 动态模块,
|
||||
一个容器跑完。单进程、零外部依赖。
|
||||
|
||||
## 端口
|
||||
|
||||
| 端口 | 承载 |
|
||||
|---|---|
|
||||
| 18098 | 管理台 `/admin/` · OIDC(`/.well-known/openid-configuration`)· `/api/*` · apix(`/apix/`、`/apixp/`)· 制品缓存 8 路由(`/go/ /cargo/ /pypi/ /npm/ /maven/ /v2/…`)· 域名转发(`Host: *.ipass.local`) |
|
||||
| 18099 | LLM 数据面 `<base_url>/<slot>/...`(sk_ Key 鉴权,出站换上游真 Key) |
|
||||
|
||||
## 快速开始
|
||||
|
||||
```bash
|
||||
bash deploy/build.sh # 收集前端 + docker build + push 阿里云 CR
|
||||
kubectl apply -f deploy/k8s/ipass-all.yaml # wuguirong 租户 ns(NodePort 30083/30084)
|
||||
curl -fs http://192.168.1.26:30083/.well-known/openid-configuration
|
||||
```
|
||||
|
||||
首次登录 `admin / 123456`(立即改密)。各插件能力与配置见管理台对应页。
|
||||
|
||||
## 装配清单与顺序
|
||||
|
||||
`SsoPlugin → CorePlugin → DomainPlugin → LlmPlugin → ApixPlugin → CachePlugin → WafPlugin → trend`
|
||||
|
||||
顺序即端点认领顺序:sso 排第一接管身份端点;trend 组合端点由装配根自己提供。
|
||||
|
||||
## 文档
|
||||
|
||||
- 构建约束、踩坑与边界 → [`CLAUDE.md`](CLAUDE.md)
|
||||
- K8s 清单 → [`deploy/k8s/ipass-all.yaml`](deploy/k8s/ipass-all.yaml)
|
||||
- 各插件自身文档 → 兄弟仓 `ipass-plugin-*/CLAUDE.md`
|
||||
@@ -0,0 +1,97 @@
|
||||
[
|
||||
{
|
||||
"client_id": "grafana",
|
||||
"client_secret": "cs_grafana_demo_secret_0000000000000000",
|
||||
"name": "Grafana(演示机密客户端)",
|
||||
"redirect_uris": [
|
||||
"https://grafana.yourdomain.com/login/generic_oauth"
|
||||
],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token"
|
||||
],
|
||||
"scopes": [
|
||||
"openid",
|
||||
"profile",
|
||||
"email"
|
||||
],
|
||||
"confidential": true,
|
||||
"created_at": 1768000000
|
||||
},
|
||||
{
|
||||
"client_id": "spa-demo",
|
||||
"client_secret": "",
|
||||
"name": "单页应用(演示公开客户端,走 PKCE)",
|
||||
"redirect_uris": [
|
||||
"https://app.yourdomain.com/callback"
|
||||
],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token"
|
||||
],
|
||||
"scopes": [
|
||||
"openid",
|
||||
"profile",
|
||||
"email"
|
||||
],
|
||||
"confidential": false,
|
||||
"created_at": 1768000000
|
||||
},
|
||||
{
|
||||
"client_id": "machine",
|
||||
"client_secret": "cs_machine_demo_secret_0000000000000000",
|
||||
"name": "机器对机器(演示 client_credentials / password)",
|
||||
"redirect_uris": [],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token",
|
||||
"client_credentials",
|
||||
"password"
|
||||
],
|
||||
"scopes": [
|
||||
"openid",
|
||||
"profile",
|
||||
"email"
|
||||
],
|
||||
"confidential": true,
|
||||
"created_at": 1768000000
|
||||
},
|
||||
{
|
||||
"client_id": "demo-system",
|
||||
"client_secret": "cs_f5efeab3c50a6332b462561034c55401",
|
||||
"name": "demo-system 简单业务系统(机密客户端,MaxKey 风格表单传参)",
|
||||
"redirect_uris": [
|
||||
"http://127.0.0.1:18096/oauth2/callback"
|
||||
],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token"
|
||||
],
|
||||
"scopes": [
|
||||
"openid",
|
||||
"profile",
|
||||
"email"
|
||||
],
|
||||
"confidential": true,
|
||||
"created_at": 1789000000
|
||||
},
|
||||
{
|
||||
"client_id": "demo-spa",
|
||||
"client_secret": "",
|
||||
"name": "demo-system 公开客户端(PKCE / S256)",
|
||||
"redirect_uris": [
|
||||
"http://127.0.0.1:18096/oauth2/callback"
|
||||
],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token"
|
||||
],
|
||||
"scopes": [
|
||||
"openid",
|
||||
"profile",
|
||||
"email"
|
||||
],
|
||||
"confidential": false,
|
||||
"created_at": 1789000000
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"plugins": {
|
||||
"sso": {
|
||||
"enabled": true,
|
||||
"admin_prefix": "sso"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"admin": {
|
||||
"username": "admin",
|
||||
"display_name": "管理员",
|
||||
"role": "admin",
|
||||
"salt": "<openssl rand -hex 16>",
|
||||
"pwd_hash": "<sha256('<salt>:<password>') 后迭代 1000 轮 sha256(x + salt)>",
|
||||
"api_key": "sk_<随机>",
|
||||
"email": "[email protected]",
|
||||
"department": "平台",
|
||||
"groups": [
|
||||
"admin"
|
||||
],
|
||||
"created_at": 0
|
||||
},
|
||||
"alice": {
|
||||
"username": "alice",
|
||||
"display_name": "爱丽丝",
|
||||
"role": "user",
|
||||
"salt": "<openssl rand -hex 16>",
|
||||
"pwd_hash": "<sha256('<salt>:<password>') 后迭代 1000 轮 sha256(x + salt)>",
|
||||
"api_key": "sk_<随机>",
|
||||
"email": "[email protected]",
|
||||
"department": "研发部",
|
||||
"groups": [
|
||||
"rd",
|
||||
"eng"
|
||||
],
|
||||
"created_at": 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
# ipass-sso 网关镜像 —— 多阶段构建(复用 rust-nginx-base)
|
||||
#
|
||||
# 从 ipass-all 派生的精简发行版:只装 ipass-core + ipass-plugin-sso(2 个 named
|
||||
# context),gateway 装配层把内核 + sso 编进单个 libipass_sso_gateway.so。
|
||||
#
|
||||
# 设计:
|
||||
# - builder 阶段:FROM crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:1.30.4-rust-1.89
|
||||
# base 已含 rust 1.89 + 已 configure/make 的 nginx 1.30.4 + pcre 8.45 + 编译链,
|
||||
# 这里只跑 cargo build
|
||||
# - runtime 阶段:debian:bookworm-slim,只带 nginx 二进制 + 模块 + 静态资源
|
||||
#
|
||||
# 构建命令(context = 本仓根;2 个 named context 缺一不可):
|
||||
# bash scripts/collect-plugin-dist.sh # 前置:收集壳 + sso 管理页 dist
|
||||
# docker build \
|
||||
# --build-context ipass-core=../ipass-core \
|
||||
# --build-context ipass-plugin-sso=../ipass-plugin-sso \
|
||||
# -t crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:ipass-sso-v0.6.0 .
|
||||
#
|
||||
# named context 落位约定:COPY 到 /ipass-core、/ipass-plugin-sso —— builder 的
|
||||
# WORKDIR 是 /build,rust/Cargo.toml 里 `../../ipass-core/...` 与插件自己的
|
||||
# `../ipass-core/...` 等 path 依赖按宿主机「同级目录」布局原样解析。
|
||||
# ⚠️ 每个插件仓只 COPY 编译所需子集(Cargo.toml/src/vendor/rules),
|
||||
# 避开几百 MB 的 target/ 与 data/。
|
||||
#
|
||||
# 升级 nginx/rust 版本时:先跑 ipass-sso/deploy/build-base-image.sh 推新 base,
|
||||
# 再 bump 此处 FROM 的 tag。
|
||||
|
||||
# syntax=docker/dockerfile:1.7
|
||||
|
||||
ARG BASE_TAG=1.30.4-rust-1.89
|
||||
|
||||
# ===========================================================================
|
||||
# builder: 复用 rust-nginx-base + Rust 模块编译(nginx 二进制 base 里已 make 好)
|
||||
# ===========================================================================
|
||||
FROM crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:${BASE_TAG} AS builder
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# 内核(含 vendored ngx-rust 与 pcre 路径依赖的源码树约定)
|
||||
COPY --from=ipass-core /rust/Cargo.toml /rust/Cargo.lock /ipass-core/rust/
|
||||
COPY --from=ipass-core /rust/crates/ /ipass-core/rust/crates/
|
||||
COPY --from=ipass-core /rust/vendor/ /ipass-core/rust/vendor/
|
||||
|
||||
# SSO 插件 crate(只 1 个 plugin)
|
||||
COPY --from=ipass-plugin-sso /Cargo.toml /Cargo.lock /ipass-plugin-sso/
|
||||
COPY --from=ipass-plugin-sso /src/ /ipass-plugin-sso/src/
|
||||
|
||||
# 装配层(本仓)
|
||||
COPY rust/ /build/rust/
|
||||
|
||||
# cargo 国内镜像源(rsproxy):base 镜像没带 cargo 配置,直连 crates.io
|
||||
# 在国内网络下会长时间悬挂在下载阶段(ipass-sso 同款)。
|
||||
RUN printf '[source.crates-io]\nreplace-with = "rsproxy-sparse"\n\n[source.rsproxy-sparse]\nregistry = "sparse+https://rsproxy.cn/index/"\n\n[net]\ngit-fetch-with-cli = true\n' \
|
||||
> "${CARGO_HOME:-/usr/local/cargo}/config.toml"
|
||||
|
||||
WORKDIR /build/rust
|
||||
# nginx 源码树路径与 base 镜像内一致(bindgen 从 objs/Makefile 抽 -I)
|
||||
ENV NGINX_SOURCE_DIR=/src/nginx-1.30.4
|
||||
ENV NGINX_BUILD_DIR=/src/nginx-1.30.4/objs
|
||||
ENV CARGO_TARGET_DIR=/build/rust/target
|
||||
|
||||
RUN cargo build --release 2>&1 | tail -15
|
||||
|
||||
# ===========================================================================
|
||||
# runtime: 极简镜像,只带 nginx 二进制 + 模块 + 静态资源 + 种子数据
|
||||
# ===========================================================================
|
||||
FROM debian:bookworm-slim AS runtime
|
||||
|
||||
ARG NGINX_VERSION=1.30.4
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
libpcre2-8-0 \
|
||||
zlib1g \
|
||||
libssl3 \
|
||||
ca-certificates \
|
||||
curl \
|
||||
tzdata \
|
||||
python3 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 与 iPass 一致:TZ=Asia/Shanghai,util::day_id 按上海时区 0:00 切日(趋势/用量口径)
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
COPY --from=builder /src/nginx-${NGINX_VERSION}/objs/nginx /usr/local/nginx/sbin/nginx
|
||||
COPY --from=builder /src/nginx-${NGINX_VERSION}/conf/mime.types /usr/local/nginx/conf/mime.types
|
||||
COPY --from=builder /build/rust/target/release/libipass_sso_gateway.so /usr/local/nginx/modules/
|
||||
|
||||
# 生产配置(deploy/nginx.conf:全插件 location 布局 + 非 root 适配)
|
||||
COPY deploy/nginx.conf /usr/local/nginx/conf/nginx.conf
|
||||
|
||||
# 管理台前端(Ruling 16):构建前由 scripts/collect-plugin-dist.sh 把
|
||||
# ipass-core 壳收集进 deploy/shell、各插件 dist 收集进 deploy/static-plugins
|
||||
COPY deploy/shell/ /usr/local/nginx/www/
|
||||
COPY deploy/static-plugins/ /usr/local/nginx/static-plugins/
|
||||
# 装配层自定义页清单(core 壳 boot 时拉取并注册为自定义页;其他装配可没有)
|
||||
COPY deploy/custom/ /usr/local/nginx/static-custom/
|
||||
# 老管理台多页应用(collect-legacy.sh 从各仓提取,壳内 iframe 原样集成)
|
||||
COPY deploy/legacy/ /usr/local/nginx/www/legacy/
|
||||
|
||||
# 种子数据(admin/alice + OAuth 接入方 + 插件启停注册表)
|
||||
COPY data-seed/ /usr/local/nginx/data-seed/
|
||||
|
||||
# 目录与属主:k8s 租户准入强制 runAsUser=10106,data 与 /tmp/nginx 必须
|
||||
# 预先可写(挂 PVC 后属主由 fsGroup=10106 接管,chown 只保无卷直跑)。
|
||||
# 日志走 /dev/std* 符号链接(kubectl logs 直接可见)。
|
||||
RUN mkdir -p /usr/local/nginx/data /usr/local/nginx/logs /tmp/nginx \
|
||||
&& chown -R 10106:10106 /usr/local/nginx/data /tmp/nginx \
|
||||
&& ln -sf /dev/stdout /usr/local/nginx/logs/access.log \
|
||||
&& ln -sf /dev/stderr /usr/local/nginx/logs/error.log
|
||||
|
||||
# 首次启动播种 admin/alice + 渲染配置 + 起 nginx
|
||||
COPY scripts/container-entrypoint.sh /usr/local/nginx/scripts/container-entrypoint.sh
|
||||
COPY scripts/seed-data.sh /usr/local/nginx/scripts/seed-data.sh
|
||||
RUN chmod +x /usr/local/nginx/scripts/container-entrypoint.sh /usr/local/nginx/scripts/seed-data.sh
|
||||
|
||||
WORKDIR /usr/local/nginx
|
||||
EXPOSE 18098 18099
|
||||
STOPSIGNAL SIGQUIT
|
||||
|
||||
ENTRYPOINT ["/usr/local/nginx/scripts/container-entrypoint.sh"]
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
# ============================================================================
|
||||
# build.sh —— 构建 + 推送 ipass-sso 镜像(可选手动 docker 命令见 Dockerfile 头注释)
|
||||
#
|
||||
# 从 ipass-all 派生的精简版,只装配 ipass-core + ipass-plugin-sso(2 个 named
|
||||
# context)。与 ipass-all/deploy/build.sh 同款外壳,去掉 5 个非 SSO 插件的
|
||||
# build-context。
|
||||
#
|
||||
# 用法:
|
||||
# bash deploy/build.sh # 收集 dist → docker build → push
|
||||
# bash deploy/build.sh --no-push # 只构建
|
||||
# bash deploy/build.sh --load # 构建后 load 进本地 docker(默认行为)
|
||||
# TAG=ipass-sso-v0.6.0 bash deploy/build.sh
|
||||
#
|
||||
# 镜像仓库约定(2026-09-26 拍板):只推 crpi-.../ssw188/nginx 这一个 repo 路径,
|
||||
# tag 用 <app>-vX.Y.Z;同 repo 加 tag 零成本,不要建新 repo。
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ROOT="$(cd "$HERE/.." && pwd)"
|
||||
IPASS_DIR="$(cd "$ROOT/.." && pwd)"
|
||||
|
||||
REGISTRY="${REGISTRY:-crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com}"
|
||||
REPO="${REPO:-ssw188/nginx}"
|
||||
TAG="${TAG:-ipass-sso-v0.6.0}"
|
||||
IMAGE="$REGISTRY/$REPO:$TAG"
|
||||
|
||||
PUSH=1
|
||||
NO_PUSH=0
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--no-push) NO_PUSH=1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
echo "== 1/2 收集前端产物(壳 + sso 管理页)=="
|
||||
bash "$ROOT/scripts/collect-plugin-dist.sh"
|
||||
|
||||
echo "== 2/2 docker build:$IMAGE =="
|
||||
cd "$ROOT"
|
||||
docker build \
|
||||
--build-context "ipass-core=$IPASS_DIR/ipass-core" \
|
||||
--build-context "ipass-plugin-sso=$IPASS_DIR/ipass-plugin-sso" \
|
||||
-f "$HERE/Dockerfile" \
|
||||
-t "$IMAGE" \
|
||||
.
|
||||
|
||||
if [ "$NO_PUSH" = "1" ]; then
|
||||
echo "== 跳过 push(--no-push)=="
|
||||
else
|
||||
echo "== docker push:$IMAGE =="
|
||||
docker push "$IMAGE"
|
||||
fi
|
||||
|
||||
echo "完成:$IMAGE"
|
||||
@@ -0,0 +1,128 @@
|
||||
# ============================================================================
|
||||
# ipass-sso K8s 部署清单(wuguirong 租户 ns)—— SSO-only 精简版
|
||||
#
|
||||
# 集群约束(与 ipass-all 同源,2026-09-26 摸底结论):
|
||||
# - 准入 tenant-storage-guard:容器级 securityContext.runAsUser=10106(pod 级无效)
|
||||
# - SC nfs-wuguirong 的 provisioner CrashLoopBackOff,新 PVC 供给不了
|
||||
# → 复用已 Bound 共享 PVC nginx-data,subPath sso/ 隔离数据
|
||||
# - 无 Ingress → NodePort:30082(TLS only,单端口;精简版没有 LLM/域名数据面)
|
||||
# - 无 secrets 权限 → 密钥类配置明文 env(与 docker-compose 同水平)
|
||||
#
|
||||
# 硬约束:replicas=1。运行期真源是进程内共享内存 + JSON 写穿,
|
||||
# 多副本各自持 shm 互不可见(脑裂),架构上不允许横向扩。
|
||||
#
|
||||
# 应用:kubectl apply -f deploy/k8s/ipass-sso.yaml
|
||||
# 验证:curl -fs http://192.168.1.26:30082/.well-known/openid-configuration
|
||||
# ============================================================================
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: ipass-sso
|
||||
namespace: wuguirong
|
||||
labels:
|
||||
app: ipass-sso
|
||||
spec:
|
||||
replicas: 1
|
||||
strategy:
|
||||
type: Recreate # 共享 PVC RWX 但数据语义单写:滚动时新旧并存会双写 JSON
|
||||
selector:
|
||||
matchLabels:
|
||||
app: ipass-sso
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: ipass-sso
|
||||
spec:
|
||||
securityContext:
|
||||
runAsUser: 10106
|
||||
runAsNonRoot: true
|
||||
fsGroup: 10106
|
||||
# 数据子目录 bootstrap:subPath 目录必须预先存在且属主 10106
|
||||
# (kubelet 对不存在的 subPath 按 root 属主建,10106 写不进——挂载成功
|
||||
# 之后业务写盘才炸,最难查)。幂等,每次滚动都会跑。
|
||||
initContainers:
|
||||
- name: init-data-dir
|
||||
image: crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:ipass-sso-v0.6.0
|
||||
imagePullPolicy: IfNotPresent
|
||||
command: ["bash", "-c", "mkdir -p /persist/sso && ls -la /persist"]
|
||||
securityContext:
|
||||
runAsUser: 10106
|
||||
runAsNonRoot: true
|
||||
allowPrivilegeEscalation: false
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /persist
|
||||
containers:
|
||||
- name: ipass-sso
|
||||
image: crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:ipass-sso-v0.6.0
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- name: tls
|
||||
containerPort: 18098 # TLS:NodePort 30082
|
||||
env:
|
||||
# issuer 必须与浏览器/第三方实际访问地址逐字一致
|
||||
- name: OAUTH_ISSUER
|
||||
value: "https://192.168.1.26:30082"
|
||||
# SSO cookie 域(共享 cookie 给同根域名)
|
||||
- name: OAUTH_COOKIE_DOMAIN
|
||||
value: "mayishangshu.cn"
|
||||
# 固定签名密钥:不设则 seed 随机生成,Pod 重启后已签发 token 全部失效
|
||||
- name: OAUTH_SIGNING_SECRET
|
||||
value: "ipass-sso-k8s-dev-signing-key-2026"
|
||||
securityContext:
|
||||
runAsUser: 10106
|
||||
runAsNonRoot: true
|
||||
allowPrivilegeEscalation: false
|
||||
capabilities:
|
||||
drop: ["ALL"]
|
||||
resources:
|
||||
requests:
|
||||
cpu: 100m
|
||||
memory: 128Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 512Mi
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
scheme: HTTPS
|
||||
# /admin/ 走 nginx 原生 try_files 兜底到 SPA index.html(与 ipass-all 同款止血方案)
|
||||
path: /admin/
|
||||
port: 18098
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
scheme: HTTPS
|
||||
path: /admin/
|
||||
port: 18098
|
||||
initialDelaySeconds: 10
|
||||
periodSeconds: 20
|
||||
failureThreshold: 3
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /usr/local/nginx/data
|
||||
subPath: sso
|
||||
volumes:
|
||||
# 复用已 Bound 的共享 PVC(nfs-wuguirong SC,新建 PVC 供给不了);
|
||||
# sso 数据经 subPath 隔离在 sso/ 子目录,与 ipass-all/ 互不干扰
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: nginx-data
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: ipass-sso
|
||||
namespace: wuguirong
|
||||
labels:
|
||||
app: ipass-sso
|
||||
spec:
|
||||
type: NodePort
|
||||
selector:
|
||||
app: ipass-sso
|
||||
ports:
|
||||
- name: tls
|
||||
port: 18098
|
||||
targetPort: tls
|
||||
nodePort: 30082
|
||||
@@ -0,0 +1,151 @@
|
||||
# ============================================================================
|
||||
# ipass-sso 容器运行配置(精简版:身份/IdP + 管理台)
|
||||
#
|
||||
# 从 ipass-all/deploy/nginx.conf 派生的 SSO-only 版(2026-09-30 拍板):
|
||||
# 剔除了 domain / llm / apix / cache / waf 5 个插件对应的 location 与
|
||||
# 整个 LLM 数据面 server、动态域名 server;只保留身份 / 管理台 / API 端点。
|
||||
#
|
||||
# 指令清单 = 本仓 gateway 注册的 4 条:
|
||||
# main : ipass_state / ipass_data_dir
|
||||
# loc : ipass_api / sso_iap
|
||||
#
|
||||
# 非 root 适配(k8s 租户准入强制 runAsUser=10106):
|
||||
# pid/lock/temp 全部挪 /tmp
|
||||
# ============================================================================
|
||||
|
||||
worker_processes auto;
|
||||
error_log /usr/local/nginx/logs/error.log info;
|
||||
pid /tmp/nginx/nginx.pid;
|
||||
lock_file /tmp/nginx/nginx.lock;
|
||||
|
||||
load_module /usr/local/nginx/modules/libipass_sso_gateway.so;
|
||||
|
||||
# 模块读取的 env 白名单(nginx 默认剥离全部环境变量;漏一条 = worker 里恒空)
|
||||
env ADMIN_INIT_PASSWORD;
|
||||
env OAUTH_SIGNING_SECRET;
|
||||
env OAUTH_ISSUER;
|
||||
env OAUTH_COOKIE_DOMAIN;
|
||||
env SYNC_DINGTALK_URL;
|
||||
env SYNC_WORKWECHAT_URL;
|
||||
env SYNC_FEISHU_URL;
|
||||
env PASSWORD_RESET_TOKEN_SINK;
|
||||
env PASSWORD_RESET_TTL;
|
||||
|
||||
events {
|
||||
worker_connections 2048;
|
||||
}
|
||||
|
||||
http {
|
||||
include /usr/local/nginx/conf/mime.types;
|
||||
default_type application/json; # 业务侧默认 JSON,静态页在对应 location 覆盖
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 非 root 适配:临时目录默认在编译前缀下(root 属主),10106 建不了
|
||||
# ------------------------------------------------------------------
|
||||
client_body_temp_path /tmp/nginx/client_body;
|
||||
proxy_temp_path /tmp/nginx/proxy;
|
||||
fastcgi_temp_path /tmp/nginx/fastcgi;
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 日志:带身份维度(WAF/缓存/域名等插件字段在精简版省略),排障不翻模块日志
|
||||
# ------------------------------------------------------------------
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" "$http_user_agent" '
|
||||
'"$upstream_addr" "$upstream_status" "$upstream_response_time" '
|
||||
'auth_user=$auth_user';
|
||||
access_log /usr/local/nginx/logs/access.log main;
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 内核:共享内存 + 数据目录(必须显式,理由见文件头)
|
||||
# ------------------------------------------------------------------
|
||||
ipass_state state 16m; # 身份会话+账号 Key(精简版无域名/用量/缓存/WAF)
|
||||
ipass_data_dir /usr/local/nginx/data;
|
||||
|
||||
# ==================================================================
|
||||
# 主 server(NodePort 30082):管理台 + IdP 全部端点
|
||||
# ==================================================================
|
||||
# 证书:多域名证书(SAN 含 *.mayishangshu.cn / mayishangshu.cn 等,
|
||||
# 2026-11-04 到期);证书放**数据目录**(PVC:/usr/local/nginx/data/tls/),
|
||||
# 换证书 = 换文件 + 重启,不用重打镜像。
|
||||
# ⚠️ nginx 同一端口不能既明文又 TLS:18098 = TLS only(NodePort 30082);
|
||||
# 明文入口 = 18097(同端口)。
|
||||
ssl_certificate /usr/local/nginx/data/tls/fullchain.cer;
|
||||
ssl_certificate_key /usr/local/nginx/data/tls/tls.key;
|
||||
ssl_trusted_certificate /usr/local/nginx/data/tls/ca.cer;
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
|
||||
server {
|
||||
listen 18098 ssl default_server; # TLS
|
||||
listen 18097; # 明文(内网调试)
|
||||
server_name _;
|
||||
|
||||
absolute_redirect off;
|
||||
port_in_redirect off;
|
||||
|
||||
# 管理台核心壳(ipass-core www dist)+ sso 管理页
|
||||
root /usr/local/nginx/www;
|
||||
index index.html;
|
||||
|
||||
client_max_body_size 10m;
|
||||
client_body_buffer_size 1m;
|
||||
|
||||
# ---- 管理/身份 API(content 阶段读体后按 PLUGINS 顺序分发)----
|
||||
# 认领方:sso(身份端点/OIDC/SCIM/CAS/门户)+ core(账号/插件启停注册表)
|
||||
location /api/ {
|
||||
ipass_api on;
|
||||
}
|
||||
|
||||
# OIDC/身份协议端点挂在根路径(/oauth/* /login /logout /userinfo
|
||||
# /.well-known/* /apps /reset /cas/*),由 sso 插件认领
|
||||
location / {
|
||||
ipass_api on;
|
||||
}
|
||||
|
||||
# ---- 管理台静态资源(这些 location 不开 ipass_api:静态文件走 nginx)----
|
||||
location = / {
|
||||
return 302 /admin/;
|
||||
}
|
||||
location = /admin {
|
||||
return 302 /admin/;
|
||||
}
|
||||
location = /index.html {
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
location /admin/ {
|
||||
try_files $uri $uri/ /index.html; # SPA history 深链兜底
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
# 插件面板路由(/plugin-<name>)走壳 SPA:直链/刷新时回 index.html
|
||||
location /plugin- {
|
||||
try_files $uri /index.html;
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
# 壳内置面板路由(/manage/*,网关管理三页)同上
|
||||
location /manage/ {
|
||||
try_files $uri /index.html;
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
location /assets/ {
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
|
||||
# 插件管理页 UMD 产物(壳按 /static/plugins/<name>/dist/<name>-admin.umd.js 拉取)
|
||||
location /static/plugins/ {
|
||||
alias /usr/local/nginx/static-plugins/;
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
# 装配层自定义页清单(壳 boot 时拉取 /static/custom/custom-pages.json 注册自定义页)
|
||||
location /static/custom/ {
|
||||
alias /usr/local/nginx/static-custom/;
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
|
||||
# 老管理台多页应用(壳内 iframe 原样集成;纯静态,不开 ipass_api)
|
||||
location /legacy/ {
|
||||
alias /usr/local/nginx/www/legacy/;
|
||||
add_header Cache-Control "no-store" always;
|
||||
}
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
Whitespace-only changes.
Generated
+2182
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,43 @@
|
||||
# ipass-sso —— iPass SSO-only 精简版网关 Cargo workspace
|
||||
#
|
||||
# 从 ipass-all 派生的精简发行版:只装 ipass-core + ipass-plugin-sso,
|
||||
# 编译期装配进单个 cdylib .so。剔除的插件(domain / llm / apix /
|
||||
# cache / waf)不在依赖里也不在编译路径上,PLUGINS 数组只剩 sso + core。
|
||||
#
|
||||
# ipass-core 内核(账号 / Key / 会话 / 共享内存原语 / plugin 契约)
|
||||
# ipass-plugin-sso 身份:OAuth2/OIDC + IAP(认领身份端点,排最前)
|
||||
#
|
||||
# ⚠️ 依赖必须用 vendored 源码(../ipass-core/rust/vendor/ngx-rust),不能走
|
||||
# crates.io(请求阶段 API 未发版)也不能走 git(submodule 克隆 nginx 仓库
|
||||
# 国内网络永久挂起)。
|
||||
#
|
||||
# 路径全部相对本文件(rust/)的上一级:宿主机与容器内都按
|
||||
# 「ipass-sso/rust 与各兄弟仓同级」布局落位(容器内由 Dockerfile named
|
||||
# context 把兄弟仓 COPY 到 /ipass-core、/ipass-plugin-sso)。
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["gateway"]
|
||||
exclude = ["target"]
|
||||
|
||||
[workspace.package]
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[workspace.dependencies]
|
||||
ngx = { path = "../../ipass-core/rust/vendor/ngx-rust" }
|
||||
nginx-sys = { path = "../../ipass-core/rust/vendor/ngx-rust/nginx-sys" }
|
||||
ipass-core = { path = "../../ipass-core/rust/crates/ipass-core" }
|
||||
ipass-plugin-sso = { path = "../../ipass-plugin-sso" }
|
||||
|
||||
# 业务层依赖:全部是纯 Rust 实现,不引入任何系统库或常驻依赖
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
[profile.release]
|
||||
opt-level = 3
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
# 模块被加载进 nginx 这个 C 进程,panic 绝不能跨 FFI 边界展开
|
||||
panic = "abort"
|
||||
strip = true
|
||||
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "ipass-sso-gateway"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
description = "iPass SSO-only 精简版网关装配层 —— module 壳 + sso+core 插件注册表,产出 nginx 动态模块"
|
||||
|
||||
[lib]
|
||||
# cdylib:给 nginx load_module 加载
|
||||
name = "ipass_sso_gateway"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[features]
|
||||
default = ["export-modules"]
|
||||
# 脱离 nginx 构建系统、单独产出动态模块时必须开启(生成 ngx_modules 表)
|
||||
export-modules = []
|
||||
|
||||
[dependencies]
|
||||
ngx.workspace = true
|
||||
nginx-sys.workspace = true
|
||||
ipass-core.workspace = true
|
||||
ipass-plugin-sso.workspace = true
|
||||
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
@@ -0,0 +1,20 @@
|
||||
//! 构建脚本:让 cdylib 允许「未定义符号」。
|
||||
//!
|
||||
//! nginx 动态模块的运行时符号(`ngx_palloc` / `ngx_http_add_variable` /
|
||||
//! `ngx_shared_memory_add` …)由 nginx 主程序在 `load_module` 时提供,
|
||||
//! 链接本模块时它们必然是未定义的——这是动态模块的固有属性,不是错误。
|
||||
//!
|
||||
//! * Linux:cdylib 默认允许未定义符号,无需处理。
|
||||
//! * macOS:必须显式 `-undefined dynamic_lookup`,否则 ld 直接报
|
||||
//! `symbol(s) not found for architecture x86_64`。
|
||||
//!
|
||||
//! 注意:vendored 的 `ngx` crate 的 build.rs 也设了这两项,但
|
||||
//! `rustc-link-arg` 只对「设置它的那个 crate」生效,不会传递给依赖它的
|
||||
//! crate,所以本 crate 必须自己再设一次。
|
||||
|
||||
fn main() {
|
||||
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("macos") {
|
||||
println!("cargo::rustc-link-arg=-undefined");
|
||||
println!("cargo::rustc-link-arg=dynamic_lookup");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,457 @@
|
||||
//! # ipass-sso —— iPass SSO-only 精简版网关装配层
|
||||
//!
|
||||
//! 从 ipass-all 派生的精简发行版:只装 ipass-core + ipass-plugin-sso,
|
||||
//! 编进单个 .so。剔除的插件(domain / llm / apix / cache / waf)不在
|
||||
//! 依赖里也不在编译路径上。
|
||||
//!
|
||||
//! ```text
|
||||
//! load_module modules/libipass_sso_gateway.so;
|
||||
//! │
|
||||
//! ipass-core sso
|
||||
//! (内核) (身份)
|
||||
//! ```
|
||||
//!
|
||||
//! ## 装配顺序(显式可控)
|
||||
//!
|
||||
//! `PLUGINS` 数组的顺序就是 preconfig → postconfig → dispatch_api 的顺序,
|
||||
//! dispatch 按顺序认领、先注册者先得。
|
||||
//!
|
||||
//! **SsoPlugin 排第一**:两侧重叠的身份端点(login/logout/session/users
|
||||
//! CRUD/me)由 sso 优先接管——它带 CAPTCHA、账号锁定、审计与身份字段;
|
||||
//! CorePlugin 只补 sso 没有的端点。
|
||||
|
||||
// 内核模块桥接:本 crate 用 ipass_core 的 users::seed_admin 播种管理员
|
||||
pub use ipass_core::{config, http, state, store, users, util};
|
||||
|
||||
use core::ffi::{c_char, c_void};
|
||||
use core::ptr;
|
||||
|
||||
use ngx::core::{NGX_CONF_ERROR, NGX_CONF_OK, NgxStr, NgxString, SlabPool, Status};
|
||||
use ngx::ffi::{
|
||||
NGX_CONF_TAKE1, NGX_CONF_TAKE2, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET,
|
||||
NGX_HTTP_MAIN_CONF, NGX_HTTP_MAIN_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG,
|
||||
ngx_command_t, ngx_conf_t, ngx_cycle_t, ngx_http_module_t, ngx_http_request_t,
|
||||
ngx_int_t, ngx_module_t, ngx_parse_size, ngx_shared_memory_add, ngx_shm_zone_t,
|
||||
ngx_str_t, ngx_uint_t,
|
||||
};
|
||||
use ngx::http::{
|
||||
HTTPStatus, HttpModule, HttpModuleLocationConf, HttpPhase, HttpRequestHandler, Merge,
|
||||
MergeConfigError, Request,
|
||||
};
|
||||
use ngx::{ngx_conf_log_error, ngx_string};
|
||||
use serde_json::Value;
|
||||
|
||||
use ipass_core::loc::{self, Switch};
|
||||
use ipass_core::plugin::Plugin;
|
||||
use ipass_plugin_sso::SsoPlugin;
|
||||
|
||||
// ===========================================================================
|
||||
// 插件装配(PLUGINS 顺序 = 认领顺序;身份端点必须排最前)
|
||||
// ===========================================================================
|
||||
|
||||
/// 插件注册表。**身份类端点(SsoPlugin)排最前**:dispatch 按顺序认领,
|
||||
/// 先注册者先得。精简版只装 sso + core。
|
||||
static PLUGINS: &[&dyn Plugin] = &[
|
||||
&SsoPlugin,
|
||||
&ipass_core::api::CorePlugin,
|
||||
];
|
||||
|
||||
/// 装配根的插件扩展:把身份端点需要的插件侧数据回填给内核
|
||||
/// (内核不认识任何插件,见 `ipass_core::api::CoreExt`)。
|
||||
/// 精简版不挂任何插件侧数据:sso 的身份数据随账号本表删除,无附加回收项;
|
||||
/// 无域名/LLM 用量装饰(domain/llm/apix 插件不在装配内)。
|
||||
struct GatewayExt;
|
||||
|
||||
impl ipass_core::api::CoreExt for GatewayExt {
|
||||
/// 管理台账号列表:精简版无 domain/llm 装饰。
|
||||
fn decorate_admin_users(&self, _views: &mut [Value]) {
|
||||
// SSO 精简版:账号只带身份字段,不挂域名数 / 用量摘要
|
||||
}
|
||||
|
||||
/// `/api/v1/me`:精简版只回基础字段。
|
||||
fn decorate_me(
|
||||
&self,
|
||||
_user: &ipass_core::models::User,
|
||||
_port: &str,
|
||||
_view: &mut Value,
|
||||
) {
|
||||
// SSO 精简版:不挂域名 / 用量 / 能力目录
|
||||
}
|
||||
|
||||
/// 删除账号:sso 的身份数据随账号本表删除,无附加回收项。
|
||||
fn reclaim_on_delete(&self, _username: &str) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
/// 账号「可用上游」白名单校验:精简版无 LLM 上游表,不校验。
|
||||
fn validate_slots(&self, _slots: &[String]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 管理 API 分发(壳统一收口)
|
||||
// ===========================================================================
|
||||
|
||||
/// 管理 API 分发:按 PLUGINS 顺序问每个插件,都不认领才统一 404。
|
||||
fn dispatch_api(r: *mut ngx_http_request_t) -> Status {
|
||||
for p in PLUGINS {
|
||||
// R19:被禁插件不认领路由(自然落到下面的统一 404)。
|
||||
// core 是网关身份底座,永不跳过。
|
||||
if p.name() != "core" && !ipass_core::plugin_registry::plugin_enabled(p.name()) {
|
||||
continue;
|
||||
}
|
||||
if let Some(s) = p.dispatch_api(r) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
let method = http::method_str(r);
|
||||
let uri = http::uri(r);
|
||||
http::fail(
|
||||
r,
|
||||
HTTPStatus::NOT_FOUND,
|
||||
&format!("接口不存在:{method} {uri}"),
|
||||
)
|
||||
}
|
||||
|
||||
/// 管理 API:content 阶段先读请求体,读完后分发。
|
||||
///
|
||||
/// 精简版只有 sso + core 两个插件处理 API,没有 GatewayRoutes 组合端点。
|
||||
struct ApiHandler;
|
||||
|
||||
impl HttpRequestHandler for ApiHandler {
|
||||
const PHASE: HttpPhase = HttpPhase::Content;
|
||||
type Output = Status;
|
||||
|
||||
fn handler(request: &mut Request) -> Self::Output {
|
||||
if !Module::location_conf(request).map(|c| c.api).unwrap_or(false) {
|
||||
return Status::NGX_DECLINED;
|
||||
}
|
||||
|
||||
let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t;
|
||||
|
||||
// SAFETY: r 有效;回调只会在请求内存池存活期间被调用
|
||||
let rc = unsafe { nginx_sys::ngx_http_read_client_request_body(r, Some(api_body_ready)) };
|
||||
|
||||
// 读体失败(请求体超限等):把原始状态码交回 nginx,由它生成对应错误页
|
||||
if rc >= nginx_sys::NGX_HTTP_SPECIAL_RESPONSE as ngx_int_t {
|
||||
return Status(rc);
|
||||
}
|
||||
|
||||
// 响应由回调发出,本阶段到此挂起
|
||||
Status::NGX_DONE
|
||||
}
|
||||
}
|
||||
|
||||
/// 请求体就绪回调:执行路由分发并终结请求。
|
||||
unsafe extern "C" fn api_body_ready(r: *mut ngx_http_request_t) {
|
||||
let status = dispatch_api(r);
|
||||
// SAFETY: r 由 nginx 传入且仍然有效;NGX_OK 表示响应已完整发出
|
||||
unsafe { nginx_sys::ngx_http_finalize_request(r, status.0) };
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 模块定义
|
||||
// ===========================================================================
|
||||
|
||||
struct Module;
|
||||
|
||||
/// 按 location 生效的开关。精简版只暴露 api + iap 两个开关。
|
||||
#[derive(Debug, Default)]
|
||||
struct ModuleConfig {
|
||||
/// 该 location 暴露管理 API + 身份协议端点(`/api/*`、`/oauth/*`、`/login`)
|
||||
api: bool,
|
||||
/// 该 location 开启身份感知代理(IAP):未登录 → 跳转登录 / 401,
|
||||
/// 已登录 → 注入 `$auth_*` 变量由 `proxy_set_header` 转成 `X-Auth-*` 头。
|
||||
iap: bool,
|
||||
}
|
||||
|
||||
unsafe impl HttpModuleLocationConf for Module {
|
||||
type LocationConf = ModuleConfig;
|
||||
}
|
||||
|
||||
impl Merge for ModuleConfig {
|
||||
fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> {
|
||||
if prev.api {
|
||||
self.api = true;
|
||||
}
|
||||
if prev.iap {
|
||||
self.iap = true;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 把本模块的 location 开关暴露给内核,插件据此判断自己是否生效。
|
||||
/// 未实现的变体(Domain/Proxy/Llm/Apix)**保守返回 false**,绝不误开。
|
||||
fn loc_switch(request: &mut Request, s: Switch) -> bool {
|
||||
let conf = Module::location_conf(request);
|
||||
match s {
|
||||
Switch::Api => conf.map(|c| c.api).unwrap_or(false),
|
||||
Switch::Iap => conf.map(|c| c.iap).unwrap_or(false),
|
||||
Switch::Domain | Switch::Proxy | Switch::Llm | Switch::Apix => false,
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpModule for Module {
|
||||
fn module() -> &'static ngx_module_t {
|
||||
// SAFETY: 模块定义是静态的,生命周期贯穿进程
|
||||
unsafe { &*ptr::addr_of!(ngx_http_ipass_sso_module) }
|
||||
}
|
||||
|
||||
/// 必须在配置解析前注册变量
|
||||
unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
|
||||
// 契约层:注入 location 开关查询实现(早于任何请求与 phase handler)
|
||||
loc::set_switch_fn(loc_switch);
|
||||
// 装配根注册插件扩展:身份端点所需的插件侧数据经它回填内核
|
||||
ipass_core::api::set_ext(&GatewayExt);
|
||||
|
||||
// 各插件注册自己的 nginx 变量($auth_* 归 sso)
|
||||
for p in PLUGINS {
|
||||
if !p.preconfig(cf) {
|
||||
ngx_conf_log_error!(
|
||||
NGX_LOG_EMERG,
|
||||
unsafe { &mut *cf },
|
||||
"插件 {} 变量注册失败",
|
||||
p.name()
|
||||
);
|
||||
return Status::NGX_ERROR.into();
|
||||
}
|
||||
}
|
||||
|
||||
Status::NGX_OK.into()
|
||||
}
|
||||
|
||||
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
|
||||
let cf = unsafe { &mut *cf };
|
||||
|
||||
// R19:装载插件启停注册表
|
||||
let cfg = ipass_core::plugin_registry::load(&format!(
|
||||
"{}/plugin-config.json",
|
||||
store::data_dir()
|
||||
));
|
||||
ipass_core::plugin_registry::set_current(cfg);
|
||||
|
||||
// 把本模块 ctx_index 注入内核契约
|
||||
ipass_core::plugin::set_ctx_index(Module::module().ctx_index as usize);
|
||||
|
||||
// 各插件注册自己的 phase handler(IAP 门禁归 sso)
|
||||
for p in PLUGINS {
|
||||
if !p.postconfig(cf) {
|
||||
ngx_conf_log_error!(
|
||||
NGX_LOG_EMERG,
|
||||
cf,
|
||||
"插件 {} 注册阶段处理器失败",
|
||||
p.name()
|
||||
);
|
||||
return Status::NGX_ERROR.into();
|
||||
}
|
||||
}
|
||||
|
||||
// 管理接口由壳统一收口,内部按插件顺序分发
|
||||
if ngx::http::add_phase_handler::<ApiHandler>(cf).is_err() {
|
||||
return Status::NGX_ERROR.into();
|
||||
}
|
||||
Status::NGX_OK.into()
|
||||
}
|
||||
}
|
||||
|
||||
// 精简版只装 sso + core:4 条指令
|
||||
static mut NGX_HTTP_IPASS_SSO_COMMANDS: [ngx_command_t; 5] = [
|
||||
ngx_command_t {
|
||||
name: ngx_string!("ipass_state"),
|
||||
type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2) as ngx_uint_t,
|
||||
set: Some(set_state_zone),
|
||||
conf: NGX_HTTP_MAIN_CONF_OFFSET,
|
||||
offset: 0,
|
||||
post: ptr::null_mut(),
|
||||
},
|
||||
ngx_command_t {
|
||||
name: ngx_string!("ipass_data_dir"),
|
||||
type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
|
||||
set: Some(set_data_dir),
|
||||
conf: NGX_HTTP_MAIN_CONF_OFFSET,
|
||||
offset: 0,
|
||||
post: ptr::null_mut(),
|
||||
},
|
||||
ngx_command_t {
|
||||
name: ngx_string!("ipass_api"),
|
||||
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
|
||||
set: Some(set_flag_api),
|
||||
conf: NGX_HTTP_LOC_CONF_OFFSET,
|
||||
offset: 0,
|
||||
post: ptr::null_mut(),
|
||||
},
|
||||
ngx_command_t {
|
||||
name: ngx_string!("sso_iap"),
|
||||
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
|
||||
set: Some(set_flag_iap),
|
||||
conf: NGX_HTTP_LOC_CONF_OFFSET,
|
||||
offset: 0,
|
||||
post: ptr::null_mut(),
|
||||
},
|
||||
ngx_command_t::empty(),
|
||||
];
|
||||
|
||||
static NGX_HTTP_IPASS_SSO_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
|
||||
preconfiguration: Some(Module::preconfiguration),
|
||||
postconfiguration: Some(Module::postconfiguration),
|
||||
create_main_conf: None,
|
||||
init_main_conf: None,
|
||||
create_srv_conf: None,
|
||||
merge_srv_conf: None,
|
||||
create_loc_conf: Some(Module::create_loc_conf),
|
||||
merge_loc_conf: Some(Module::merge_loc_conf),
|
||||
};
|
||||
|
||||
// 脱离 nginx 构建系统、单独产出 cdylib 时必须靠这个宏导出 ngx_modules 表
|
||||
#[cfg(feature = "export-modules")]
|
||||
ngx::ngx_modules!(ngx_http_ipass_sso_module);
|
||||
|
||||
#[used]
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub static mut ngx_http_ipass_sso_module: ngx_module_t = ngx_module_t {
|
||||
ctx: &raw const NGX_HTTP_IPASS_SSO_MODULE_CTX as _,
|
||||
commands: unsafe { &raw mut NGX_HTTP_IPASS_SSO_COMMANDS[0] },
|
||||
type_: NGX_HTTP_MODULE as _,
|
||||
init_process: Some(init_process),
|
||||
exit_process: Some(exit_process),
|
||||
..ngx_module_t::default()
|
||||
};
|
||||
|
||||
/// 每个 worker 启动后执行一次。管理员播种是**幂等**的。
|
||||
unsafe extern "C" fn init_process(_cycle: *mut ngx_cycle_t) -> ngx_int_t {
|
||||
match users::seed_admin() {
|
||||
Ok(true) => util::log_warn(
|
||||
"[ipass] 已创建初始管理员 admin,请首次登录后立即修改密码",
|
||||
),
|
||||
Ok(false) => {}
|
||||
Err(e) => util::log_err(&format!("[ipass] 初始化管理员失败: {e}")),
|
||||
}
|
||||
Status::NGX_OK.into()
|
||||
}
|
||||
|
||||
/// worker 退出前执行一次。精简版无运行期统计要落盘,钩子留空即可。
|
||||
unsafe extern "C" fn exit_process(_cycle: *mut ngx_cycle_t) {
|
||||
// SSO 精简版:共享内存里只有身份会话与账号 Key,重启即失效属已知代价,
|
||||
// 不需要 exit 钩子落盘。
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 指令处理
|
||||
// ===========================================================================
|
||||
|
||||
/// 读第 2 个参数并转成 bool(on/off/1/0/yes/no)。
|
||||
unsafe fn arg_flag(cf: *mut ngx_conf_t, idx: usize) -> Option<bool> {
|
||||
let cf_ref = unsafe { cf.as_mut() }?;
|
||||
let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() };
|
||||
let raw = args.get(idx)?.as_bytes();
|
||||
let s = core::str::from_utf8(raw).ok()?;
|
||||
Some(matches!(s.to_ascii_lowercase().as_str(), "on" | "1" | "true" | "yes"))
|
||||
}
|
||||
|
||||
extern "C" fn set_flag_api(
|
||||
cf: *mut ngx_conf_t,
|
||||
_cmd: *mut ngx_command_t,
|
||||
conf: *mut c_void,
|
||||
) -> *mut c_char {
|
||||
let Some(flag) = (unsafe { arg_flag(cf, 1) }) else {
|
||||
return NGX_CONF_ERROR;
|
||||
};
|
||||
// SAFETY: nginx 保证 conf 指向本模块的 loc conf 结构
|
||||
unsafe { (*(conf as *mut ModuleConfig)).api = flag };
|
||||
NGX_CONF_OK
|
||||
}
|
||||
|
||||
extern "C" fn set_flag_iap(
|
||||
cf: *mut ngx_conf_t,
|
||||
_cmd: *mut ngx_command_t,
|
||||
conf: *mut c_void,
|
||||
) -> *mut c_char {
|
||||
let Some(flag) = (unsafe { arg_flag(cf, 1) }) else {
|
||||
return NGX_CONF_ERROR;
|
||||
};
|
||||
// SAFETY: 同上
|
||||
unsafe { (*(conf as *mut ModuleConfig)).iap = flag };
|
||||
NGX_CONF_OK
|
||||
}
|
||||
|
||||
extern "C" fn set_state_zone(
|
||||
cf: *mut ngx_conf_t,
|
||||
_cmd: *mut ngx_command_t,
|
||||
_conf: *mut c_void,
|
||||
) -> *mut c_char {
|
||||
// SAFETY: 配置回调必然拿到有效的 cf
|
||||
let cf_ref = unsafe { cf.as_mut().unwrap() };
|
||||
let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() };
|
||||
|
||||
let mut name: ngx_str_t = args[1];
|
||||
let size = unsafe { ngx_parse_size(&raw mut args[2]) };
|
||||
if size == -1 {
|
||||
ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_state: 共享内存大小非法");
|
||||
return NGX_CONF_ERROR;
|
||||
}
|
||||
|
||||
let zone = unsafe {
|
||||
ngx_shared_memory_add(
|
||||
cf,
|
||||
&raw mut name,
|
||||
size as usize,
|
||||
(&raw mut ngx_http_ipass_sso_module).cast(),
|
||||
)
|
||||
};
|
||||
if zone.is_null() {
|
||||
return NGX_CONF_ERROR;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
(*zone).init = Some(state_zone_init);
|
||||
}
|
||||
state::set_zone(zone);
|
||||
NGX_CONF_OK
|
||||
}
|
||||
|
||||
/// 共享内存初始化:由 nginx 在 **fork 之前**调用一次。
|
||||
///
|
||||
/// 精简版只装载 sso 自持表(OAuth 接入方表由 SsoPlugin::zone_load 自己装)+
|
||||
/// 各插件 zone_load 回调;无 domain/llm/apix 数据面需要预装载。
|
||||
extern "C" fn state_zone_init(zone: *mut ngx_shm_zone_t, _data: *mut c_void) -> ngx_int_t {
|
||||
let status = state::init_zone(zone);
|
||||
if status != Status::NGX_OK {
|
||||
return status.into();
|
||||
}
|
||||
|
||||
// 各插件自持表的预装载(sso 的 OAuth 接入方等);
|
||||
// 没有自持表的插件返回 0,仅打日志。
|
||||
for p in PLUGINS {
|
||||
let n = p.zone_load();
|
||||
util::log_notice(&format!("[gateway] 插件 {} 预装载 {n} 条", p.name()));
|
||||
}
|
||||
|
||||
Status::NGX_OK.into()
|
||||
}
|
||||
|
||||
extern "C" fn set_data_dir(
|
||||
cf: *mut ngx_conf_t,
|
||||
_cmd: *mut ngx_command_t,
|
||||
_conf: *mut c_void,
|
||||
) -> *mut c_char {
|
||||
// SAFETY: 配置回调必然拿到有效的 cf
|
||||
let cf_ref = unsafe { cf.as_mut().unwrap() };
|
||||
let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() };
|
||||
|
||||
match core::str::from_utf8(args[1].as_bytes()) {
|
||||
Ok(dir) => {
|
||||
store::set_data_dir(dir);
|
||||
NGX_CONF_OK
|
||||
}
|
||||
Err(_) => {
|
||||
ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_data_dir: 路径不是合法 UTF-8");
|
||||
NGX_CONF_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 供测试与诊断使用:`NgxStr` 类型在模块里被多处引用,确保编译期链接到正确的符号
|
||||
#[allow(dead_code)]
|
||||
fn _type_witness(_: &NgxStr, _: &NgxString<SlabPool>) {}
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
# ============================================================================
|
||||
# collect-plugin-dist.sh —— 收集管理台前端产物到可部署目录
|
||||
#
|
||||
# 形状与 ipass-sso / ipass-ssw 的同名脚本一致(Ruling 16/19):
|
||||
# - 核心壳来自 ipass-core/www/dist(SPA:index.html + assets)
|
||||
# - 插件管理页是 UMD 产物,壳按 /static/plugins/<name>/dist/<name>-admin.umd.js
|
||||
# 动态加载(寻址约定见 ipass-core www loader.ts;<name> = Plugin::name() 短名)
|
||||
#
|
||||
# Docker 构建上下文只覆盖本仓,COPY 不到兄弟仓库,故先把产物聚合进
|
||||
# deploy/,再由 Dockerfile COPY 进镜像。
|
||||
#
|
||||
# 输出布局:
|
||||
# deploy/shell/ ← ../ipass-core/www/dist
|
||||
# deploy/static-plugins/<name>/dist/ ← ../ipass-plugin-<name>/www/dist
|
||||
#
|
||||
# 插件清单 = 本仓装配的全部插件(精简版只装 sso;其他插件仓不存在时自动跳过并告警)。
|
||||
# 用法:bash scripts/collect-plugin-dist.sh # → deploy/
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ROOT="$(cd "$HERE/.." && pwd)"
|
||||
IPASS_DIR="${IPASS_DIR:-$(cd "$ROOT/.." && pwd)}"
|
||||
ASSEMBLY_DIR="${ASSEMBLY_DIR:-deploy}"
|
||||
|
||||
case "$ASSEMBLY_DIR" in
|
||||
/*) OUT="$ASSEMBLY_DIR" ;;
|
||||
*) OUT="$ROOT/$ASSEMBLY_DIR" ;;
|
||||
esac
|
||||
|
||||
SHELL_SUB="${SHELL_SUB:-shell}"
|
||||
PLUGINS_SUB="${PLUGINS_SUB:-static-plugins}"
|
||||
|
||||
# 本仓 gateway 装配的插件短名(顺序无关;缺 dist 自动跳过并告警)
|
||||
PLUGINS="${PLUGINS:-sso}"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1) 核心壳(默认 ipass-core www dist;**装配仓可用自己的 shell/dist 覆写**——
|
||||
# 2026-09-26 架构定案第 2 条:各装配默认用 core 框架,也可整体重写壳)
|
||||
# ---------------------------------------------------------------------------
|
||||
SHELL_SRC="$IPASS_DIR/ipass-core/www/dist"
|
||||
if [ -d "$ROOT/shell/dist" ] && [ -f "$ROOT/shell/dist/index.html" ]; then
|
||||
SHELL_SRC="$ROOT/shell/dist"
|
||||
echo "装配仓自带壳,覆盖 core 默认:$SHELL_SRC"
|
||||
fi
|
||||
SHELL_OUT="$OUT/$SHELL_SUB"
|
||||
[ -d "$SHELL_SRC" ] || { echo "!! core shell dist 缺失:$SHELL_SRC(先在 ipass-core/www 跑 npm run build)" >&2; exit 1; }
|
||||
mkdir -p "$SHELL_OUT"
|
||||
rsync -a --delete --exclude .gitkeep "$SHELL_SRC/" "$SHELL_OUT/"
|
||||
echo "shell : $SHELL_SRC → $SHELL_OUT"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2) 各插件的管理页 dist
|
||||
# ---------------------------------------------------------------------------
|
||||
PLUGINS_OUT="$OUT/$PLUGINS_SUB"
|
||||
mkdir -p "$PLUGINS_OUT"
|
||||
# 清空旧收集产物但保留 .gitkeep(Dockerfile COPY 依赖目录存在)
|
||||
find "$PLUGINS_OUT" -mindepth 1 -maxdepth 1 ! -name .gitkeep -exec rm -rf {} +
|
||||
|
||||
count=0
|
||||
for short in $PLUGINS; do
|
||||
src="$IPASS_DIR/ipass-plugin-${short}/www/dist"
|
||||
if [ ! -d "$src" ]; then
|
||||
echo "!! ${short}:$src 缺失(先在 ipass-plugin-${short}/www 跑 npm run build),跳过" >&2
|
||||
continue
|
||||
fi
|
||||
mkdir -p "$PLUGINS_OUT/$short/dist"
|
||||
rsync -a --delete "$src/" "$PLUGINS_OUT/$short/dist/"
|
||||
# 产物原文件名是 plugin-<短名>-admin.umd.js;壳按短名请求
|
||||
# /static/plugins/<短名>/dist/<短名>-admin.umd.js → 补一份短名别名
|
||||
if [ ! -f "$PLUGINS_OUT/$short/dist/${short}-admin.umd.js" ] \
|
||||
&& [ -f "$PLUGINS_OUT/$short/dist/plugin-${short}-admin.umd.js" ]; then
|
||||
cp "$PLUGINS_OUT/$short/dist/plugin-${short}-admin.umd.js" \
|
||||
"$PLUGINS_OUT/$short/dist/${short}-admin.umd.js"
|
||||
fi
|
||||
echo "plugin: $src → $PLUGINS_OUT/$short/dist"
|
||||
count=$((count + 1))
|
||||
done
|
||||
|
||||
echo "完成:shell + ${count} 个插件 → $OUT"
|
||||
Executable
+74
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bash
|
||||
# ============================================================================
|
||||
# 容器入口脚本(首发 + 每次启动都跑)
|
||||
#
|
||||
# 干的事:
|
||||
# 1. 首次启动:data/ 为空时跑 seed-data.sh,生成 admin/123456 + alice/alice123
|
||||
# 2. 每次启动:把容器外的 env(OAUTH_ISSUER 等)打印出来供排障
|
||||
# 3. nginx -t 语法预检
|
||||
# 4. exec nginx(让 nginx 拿到 PID 1,否则 stop signal 收不到)
|
||||
#
|
||||
# 设计:
|
||||
# - nginx.test.conf 用了 env 占位(OAUTH_ISSUER / OAUTH_SIGNING_SECRET 等),
|
||||
# nginx 启动时会从环境变量读取,无需重新渲染模板
|
||||
# - seed-data.sh 用 python3 算 SHA-256 散列(与 rust/src/util.rs::hash_password
|
||||
# 逐位一致:sha256("salt:password") 后迭代 1000 轮 sha256(x + salt))
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
DATA_DIR="/usr/local/nginx/data"
|
||||
SEED_DIR="/usr/local/nginx/data-seed"
|
||||
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
|
||||
LOG_DIR="/usr/local/nginx/logs"
|
||||
|
||||
c_ok() { printf '\033[32m✓\033[0m %s\n' "$*"; }
|
||||
c_warn() { printf '\033[33m!\033[0m %s\n' "$*"; }
|
||||
c_err() { printf '\033[31m✗\033[0m %s\n' "$*" >&2; }
|
||||
|
||||
# ---------------------------------------------------------------- 0. 基础目录
|
||||
mkdir -p "$DATA_DIR" "$LOG_DIR"
|
||||
|
||||
# ---------------------------------------------------------------- 1. 打印关键 env(排障)
|
||||
printf '\n== ipass-sso 启动参数 ==\n'
|
||||
c_ok "OAUTH_ISSUER = ${OAUTH_ISSUER:-<未设置,默认走 nginx.conf 占位>}"
|
||||
c_ok "OAUTH_COOKIE_DOMAIN= ${OAUTH_COOKIE_DOMAIN:-<未设置>}"
|
||||
c_ok "ADMIN_USER = ${ADMIN_USER:-admin}"
|
||||
if [ -n "${ADMIN_PASSWORD:-}" ]; then
|
||||
c_ok "ADMIN_PASSWORD = <已注入>"
|
||||
else
|
||||
c_warn "ADMIN_PASSWORD = <未注入>,seed 将使用默认 123456"
|
||||
fi
|
||||
if [ -n "${OAUTH_SIGNING_SECRET:-}" ]; then
|
||||
c_ok "OAUTH_SIGNING_SECRET= <已注入>"
|
||||
else
|
||||
c_warn "OAUTH_SIGNING_SECRET= <未注入>,seed 将生成随机值(重启即丢!)"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------- 2. 数据物化(首次启动)
|
||||
NEED_SEED=0
|
||||
[ ! -f "$DATA_DIR/users.json" ] && NEED_SEED=1
|
||||
[ ! -f "$DATA_DIR/oauth_clients.json" ] && NEED_SEED=1
|
||||
|
||||
if [ "$NEED_SEED" = "1" ]; then
|
||||
printf '\n== 首次启动:seed-data.sh 物化 admin/alice ==\n'
|
||||
# nginx worker 是 nobody(uid 65534),必须可写
|
||||
chown -R 65534:65534 "$DATA_DIR" 2>/dev/null || c_warn "chown 65534 失败(继续)"
|
||||
|
||||
ADMIN_PASSWORD="${ADMIN_PASSWORD:-123456}" \
|
||||
ALICE_PASSWORD="${ALICE_PASSWORD:-alice123}" \
|
||||
DATA_DIR="$DATA_DIR" \
|
||||
SEED_DIR="$SEED_DIR" \
|
||||
bash /usr/local/nginx/scripts/seed-data.sh || \
|
||||
{ c_err "seed-data.sh 失败"; exit 1; }
|
||||
c_ok "data 目录物化完成"
|
||||
else
|
||||
c_ok "data 目录已有,跳过 seed"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------- 3. nginx 语法预检
|
||||
printf '\n== nginx -t ==\n'
|
||||
/usr/local/nginx/sbin/nginx -t -c "$NGINX_CONF" 2>&1 || { c_err "nginx 配置语法错误"; exit 1; }
|
||||
|
||||
# ---------------------------------------------------------------- 4. exec nginx(PID 1 拿信号)
|
||||
printf '\n== 起 nginx ==\n'
|
||||
exec /usr/local/nginx/sbin/nginx -c "$NGINX_CONF" -g 'daemon off;'
|
||||
Executable
+65
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env bash
|
||||
# push-init.sh —— 读取 ~/.ssw.ini [credential-git-18] 建仓(Gitea API)并推送 ipass-all
|
||||
# 密钥只在脚本进程内流转,任何输出过滤掉口令字面量
|
||||
set -euo pipefail
|
||||
|
||||
INI="$HOME/ssw.ini"
|
||||
HOST="192.168.1.18:13300"
|
||||
ORG="ipass"
|
||||
REPO="ipass-all"
|
||||
|
||||
read_ini() { # read_ini <key>
|
||||
awk -F= -v sec="credential-git-18" -v key="$1" '
|
||||
/^\[/ { f = ($0 ~ "\\[" sec "\\]") ? 1 : 0; next }
|
||||
f && $1 ~ key { gsub(/\r/, "", $2); gsub(/^ +| +$/, "", $2); print $2; exit }
|
||||
' "$INI"
|
||||
}
|
||||
|
||||
U="$(read_ini username)"
|
||||
P="$(read_ini password)"
|
||||
[ -n "$U" ] && [ -n "$P" ] || { echo "FAIL: ini 解析不到凭证"; exit 1; }
|
||||
|
||||
sanitized() { # 吞掉可能夹带的口令字面量
|
||||
grep -v "$P" | head -c 300
|
||||
}
|
||||
|
||||
# 1) 若仓库已存在则跳过建仓
|
||||
CODE=$(curl -s -o /tmp/repo-check.json -w '%{http_code}' -u "$U:$P" "http://$HOST/api/v1/repos/$ORG/$REPO")
|
||||
if [ "$CODE" = "200" ]; then
|
||||
echo "仓库已存在: $ORG/$REPO"
|
||||
else
|
||||
echo "建仓 org/$REPO …"
|
||||
RES=$(curl -s -u "$U:$P" -X POST "http://$HOST/api/v1/org/$ORG/repos" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"name":"ipass-all","private":true,"auto_init":false,"description":"iPass 全插件一体网关(内核+6插件单.so单容器)"}')
|
||||
if echo "$RES" | grep -q '"full_name"'; then
|
||||
echo "建仓 OK"
|
||||
else
|
||||
echo "org 域建仓失败,尝试用户域 …"
|
||||
RES=$(curl -s -u "$U:$P" -X POST "http://$HOST/api/v1/user/repos" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"name":"ipass-all","private":true,"auto_init":false}')
|
||||
if echo "$RES" | grep -q '"full_name"'; then
|
||||
ORG="$U"
|
||||
echo "建仓 OK(用户域: $U/$REPO)"
|
||||
else
|
||||
echo "FAIL: 建仓被拒"; echo "$RES" | sanitized; exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# 2) 挂远端(干净 URL,凭证走一次性 credential helper,不写入 .git/config)
|
||||
cd "$(dirname "$0")"
|
||||
git remote remove origin 2>/dev/null || true
|
||||
git remote add origin "http://$HOST/$ORG/$REPO.git"
|
||||
|
||||
echo "推送 main …"
|
||||
if git \
|
||||
-c credential.helper= \
|
||||
-c "credential.helper=!f(){ printf 'username=%s\n' '$U'; printf 'password=%s\n' '$P'; }; f" \
|
||||
push -u origin main 2>&1 | sanitized | tail -3; then
|
||||
echo "PUSH OK: $ORG/$REPO (main)"
|
||||
else
|
||||
echo "PUSH FAIL(上方为过滤后输出)"
|
||||
exit 1
|
||||
fi
|
||||
Executable
+171
@@ -0,0 +1,171 @@
|
||||
#!/usr/bin/env bash
|
||||
# ============================================================================
|
||||
# 物化运行时数据目录:data-seed/ -> data/
|
||||
#
|
||||
# 为什么需要这一步
|
||||
# data/ 是**运行时**目录(可写、含真实口令散列),不进版本库;
|
||||
# data-seed/ 是**种子模板**(只放演示客户端与示例账号),进版本库。
|
||||
# 本脚本把两者接起来,并顺便把示例里的占位符换成真实值。
|
||||
#
|
||||
# 做的事:
|
||||
# 1. data/oauth_clients.json <- data-seed/oauth_clients.json
|
||||
# 2. data/users.json <- 由本脚本生成(admin / alice 两个账号)
|
||||
# 3. data/users.json.example <- 同步一份示例(占位符,不含真实散列)
|
||||
#
|
||||
# 幂等:users.json 已存在时**默认不动**(避免覆盖你手工改过的账号),要重建加 --force。
|
||||
#
|
||||
# 用法:
|
||||
# bash scripts/seed-data.sh # 缺什么补什么
|
||||
# bash scripts/seed-data.sh --force # 重建 users.json(会丢已有账号)
|
||||
# DATA_DIR=/path ADMIN_PASSWORD=xxx bash scripts/seed-data.sh
|
||||
#
|
||||
# 口令来源:ADMIN_PASSWORD(默认 123456)、ALICE_PASSWORD(默认 alice123)。
|
||||
# 这两个默认值只用于**本机/CI 演示**,生产部署必须覆盖。
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ROOT="$(cd "$HERE/.." && pwd)"
|
||||
SEED_DIR="${SEED_DIR:-$ROOT/data-seed}"
|
||||
DATA_DIR="${DATA_DIR:-$ROOT/data}"
|
||||
|
||||
ADMIN_PASSWORD="${ADMIN_PASSWORD:-123456}"
|
||||
ALICE_PASSWORD="${ALICE_PASSWORD:-alice123}"
|
||||
|
||||
FORCE=0
|
||||
for a in "$@"; do [ "$a" = "--force" ] && FORCE=1; done
|
||||
|
||||
PYTHON="${PYTHON:-python3}"
|
||||
|
||||
[ -d "$SEED_DIR" ] || { echo "!! 种子目录缺失:$SEED_DIR" >&2; exit 1; }
|
||||
mkdir -p "$DATA_DIR"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. OAuth 接入方注册表:直接拷贝(没有则视为空表)
|
||||
# ---------------------------------------------------------------------------
|
||||
if [ -f "$SEED_DIR/oauth_clients.json" ]; then
|
||||
if [ ! -f "$DATA_DIR/oauth_clients.json" ] || [ "$FORCE" = "1" ]; then
|
||||
cp "$SEED_DIR/oauth_clients.json" "$DATA_DIR/oauth_clients.json"
|
||||
echo " [seed] oauth_clients.json 已写入 $DATA_DIR/oauth_clients.json"
|
||||
else
|
||||
echo " [seed] oauth_clients.json 已存在,跳过"
|
||||
fi
|
||||
else
|
||||
echo " [seed] 警告:$SEED_DIR/oauth_clients.json 不存在,注册表留空"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1.5 插件启停注册表(R19):拷种子(缺省语义是「未列出 = 启用」,但显式
|
||||
# 注册表让管理台/运维一眼看清装配了哪些插件,禁用也只改这一个文件)
|
||||
# ---------------------------------------------------------------------------
|
||||
if [ -f "$SEED_DIR/plugin-config.json" ]; then
|
||||
if [ ! -f "$DATA_DIR/plugin-config.json" ] || [ "$FORCE" = "1" ]; then
|
||||
cp "$SEED_DIR/plugin-config.json" "$DATA_DIR/plugin-config.json"
|
||||
echo " [seed] plugin-config.json 已写入 $DATA_DIR/plugin-config.json"
|
||||
else
|
||||
echo " [seed] plugin-config.json 已存在,跳过"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. 账号表:生成真实散列。
|
||||
#
|
||||
# ⚠️ 散列算法必须与 rust/src/util.rs::hash_password 逐位一致,
|
||||
# 否则生成的账号永远登录不上:
|
||||
# x = sha256("salt:password") ← 注意中间是冒号
|
||||
# 重复 HASH_ROUNDS(=1000,见 config.rs) 次:x = sha256(x + salt)
|
||||
# 改了拼接顺序或轮数,存量 users.json 会全部失效。
|
||||
# ---------------------------------------------------------------------------
|
||||
if [ -f "$DATA_DIR/users.json" ] && [ "$FORCE" != "1" ]; then
|
||||
echo " [seed] users.json 已存在,跳过(要重建加 --force)"
|
||||
else
|
||||
ADMIN_PASSWORD="$ADMIN_PASSWORD" ALICE_PASSWORD="$ALICE_PASSWORD" \
|
||||
OUT="$DATA_DIR/users.json" USERS_EXAMPLE="$SEED_DIR/users.json.example" \
|
||||
"$PYTHON" - <<'PYEOF'
|
||||
import hashlib, json, os, secrets
|
||||
|
||||
HASH_ROUNDS = 1000 # 必须与 rust/src/config.rs 的 HASH_ROUNDS 一致
|
||||
|
||||
def pwd_hash(salt, password):
|
||||
x = hashlib.sha256(("%s:%s" % (salt, password)).encode()).hexdigest()
|
||||
for _ in range(HASH_ROUNDS):
|
||||
x = hashlib.sha256((x + salt).encode()).hexdigest()
|
||||
return x
|
||||
|
||||
def entry(username, display_name, password, role, email, department, groups,
|
||||
api_key, created_at=1768000000):
|
||||
salt = secrets.token_hex(16) # 32 位 hex
|
||||
return {
|
||||
"username": username,
|
||||
"display_name": display_name,
|
||||
"role": role,
|
||||
"salt": salt,
|
||||
"pwd_hash": pwd_hash(salt, password),
|
||||
"api_key": api_key,
|
||||
"email": email,
|
||||
"department": department,
|
||||
"groups": groups,
|
||||
"created_at": created_at,
|
||||
}
|
||||
|
||||
users = {
|
||||
"admin": entry(
|
||||
"admin", "管理员", os.environ["ADMIN_PASSWORD"], "admin",
|
||||
"[email protected]", "平台", ["admin"],
|
||||
"sk_" + secrets.token_hex(20),
|
||||
),
|
||||
"alice": entry(
|
||||
"alice", "爱丽丝", os.environ["ALICE_PASSWORD"], "user",
|
||||
"[email protected]", "研发部", ["rd", "eng"],
|
||||
"sk_" + secrets.token_hex(20),
|
||||
),
|
||||
}
|
||||
|
||||
out = os.environ["OUT"]
|
||||
with open(out, "w", encoding="utf-8") as f:
|
||||
json.dump(users, f, ensure_ascii=False, indent=2)
|
||||
f.write("\n")
|
||||
|
||||
# 同步一份「占位符版」示例回种子目录,让后来人一眼看懂字段含义
|
||||
HASH_NOTE = "sha256('<salt>:<password>') 后迭代 1000 轮 sha256(x + salt)"
|
||||
example = {
|
||||
"admin": {
|
||||
"username": "admin",
|
||||
"display_name": "管理员",
|
||||
"role": "admin",
|
||||
"salt": "<openssl rand -hex 16>",
|
||||
"pwd_hash": "<%s>" % HASH_NOTE,
|
||||
"api_key": "sk_<随机>",
|
||||
"email": "[email protected]",
|
||||
"department": "平台",
|
||||
"groups": ["admin"],
|
||||
"created_at": 0,
|
||||
},
|
||||
"alice": {
|
||||
"username": "alice",
|
||||
"display_name": "爱丽丝",
|
||||
"role": "user",
|
||||
"salt": "<openssl rand -hex 16>",
|
||||
"pwd_hash": "<%s>" % HASH_NOTE,
|
||||
"api_key": "sk_<随机>",
|
||||
"email": "[email protected]",
|
||||
"department": "研发部",
|
||||
"groups": ["rd", "eng"],
|
||||
"created_at": 0,
|
||||
},
|
||||
}
|
||||
# 回写占位符示例只对「仓库内 host 流程」有意义(容器里种子目录在只读镜像层,
|
||||
# 写不进也不该写——10106 非 root 跑会在这里 PermissionError 打断整个 seed)
|
||||
example_path = os.environ["USERS_EXAMPLE"]
|
||||
if os.access(example_path, os.W_OK):
|
||||
with open(example_path, "w", encoding="utf-8") as f:
|
||||
json.dump(example, f, ensure_ascii=False, indent=2)
|
||||
f.write("\n")
|
||||
else:
|
||||
print(" [seed] 种子目录不可写(容器镜像层),跳过示例回写:%s" % example_path)
|
||||
|
||||
print(" [seed] users.json 已生成 %s(admin + alice)" % out)
|
||||
PYEOF
|
||||
fi
|
||||
|
||||
echo " [seed] 数据目录就绪:$DATA_DIR"
|
||||
Reference in new issue
Block a user