Skip to content

feat(db): DB CLI 增加 cn-prod(阿里云)支持 (#21)#22

Open
xbfool wants to merge 2 commits into
mainfrom
feat/cn-prod-db-support
Open

feat(db): DB CLI 增加 cn-prod(阿里云)支持 (#21)#22
xbfool wants to merge 2 commits into
mainfrom
feat/cn-prod-db-support

Conversation

@xbfool
Copy link
Copy Markdown
Contributor

@xbfool xbfool commented Jun 5, 2026

实现 #21query-db / grant-balance / grant-subscription 现在接受 --env cn(别名 cn-prod),路由到阿里云 cn-prod(与海外 AWS 完全独立)。

处理的 cn 特性

  • 独立 Infisical secrets-cn.optima.chat:admin email/password → accessToken → select-organization → org-scoped token(字段名坑已内建处理)
  • cred 取自 /shared-secrets/database-users + /database-names(按 prefix:AUTH/BILLING/…);cn RDS 无公网端点
  • 内网 RDS 经 buildbox ECS 跳板 SSH -L 隧道(sshpass),复用现有 warm-reuse / 僵尸隧道检测框架
  • curl 用 execFileSync(无 shell 引号坑,跨平台)

用法

optima-query-db user-auth "SELECT count(*) FROM users" cn
optima-grant-balance user@x.com --amount 5 --env cn
optima-grant-subscription user@x.com --plan pro --env cn

cn 专用环境变量:

var 来源
INFISICAL_CN_EMAIL / INFISICAL_CN_PASSWORD 1P Infisical cn-prod admin (secrets-cn.optima.chat)
OPTIMA_CN_BUILDBOX_PASSWORD 1P Aliyun cn-prod buildbox ECS (root)
OPTIMA_CN_BUILDBOX_HOST(可选) 默认 47.94.105.163

依赖 sshpass(Windows 用 WSL)。

实测(对 cn-prod RDS)

  • query-db user-auth "SELECT count(*) FROM users" cn31(真实)
  • connectBillingDB('cn') 读 plans(count=0,cn billing 一期未 seed,但隧道+认证+查询路径全绿)

Scope / 已知边界

关联

🤖 Generated with Claude Code

xbfool and others added 2 commits June 5, 2026 15:39
query-db / grant-balance / grant-subscription now accept `--env cn`
(alias `cn-prod`), routing to the Aliyun cn-prod environment which is
fully separate from overseas AWS.

cn-prod specifics handled:
- separate Infisical `secrets-cn.optima.chat` (admin email/password →
  accessToken → select-organization → org-scoped token; the field-name
  trap baked in)
- creds from /shared-secrets/database-users + /database-names by prefix
  (AUTH/BILLING/…); the cn RDS has no public endpoint
- private RDS reached through the buildbox ECS via an SSH `-L` tunnel
  (sshpass), reusing the existing warm-reuse / zombie-tunnel framework
- curl calls use execFileSync (no shell-quoting, cross-platform)

Env vars (cn only): INFISICAL_CN_EMAIL / INFISICAL_CN_PASSWORD (1P
"Infisical cn-prod admin"), OPTIMA_CN_BUILDBOX_PASSWORD (1P "Aliyun
cn-prod buildbox ECS (root)"). Requires sshpass + aws-less; Windows → WSL.

Tested against cn-prod RDS:
- `query-db user-auth "SELECT count(*) FROM users" cn` → 31 (real)
- connectBillingDB('cn') reads plans (count 0 — cn billing not yet
  seeded, but tunnel + auth + query path all green)

Scope notes:
- query-db cn covers services whose creds live in shared-secrets
  /database-users (prefix from userKey). Services with a literal
  DATABASE_URL in /services/<svc> (e.g. gateway-core) error with a
  pointer to #21 — follow-up.
- grant-subscription on cn needs the plans table seeded first.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
cn-prod 的 cred 有两类:① /shared-secrets/database-users(按 prefix)
② 服务自己 /services/<svc> 的 DATABASE_URL(gateway-core 等)。本次补全
②,query-db cn 现在覆盖全部服务。

- getCnSecrets() 加 expand 参数 → expandSecretReferences=true 让 cn
  Infisical 服务端解析 `${...}` 引用为字面值(user-auth 等是引用式)
- connectCnDBFromUrl():取展开后的 DATABASE_URL → 解析 → buildbox 隧道
- parseDatabaseUrl() 容忍驱动后缀(postgresql+asyncpg://)
- query-db cn 分支:有 userKey 走 prefix;有 databaseUrlPath 走 URL

实测:`query-db optima-sentinel "SELECT 1" cn` → 1(绿)。

发现(非本 PR):cn Infisical /services/gateway-core 的 DATABASE_URL
口令(32 字符)与 RDS 实际 gateway_user 口令(db-init tfvars,28 字符)
不一致,psql 认证失败 → cn gateway-core Infisical cred 疑似过时,需 cn
侧单独核对/修复。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@xbfool
Copy link
Copy Markdown
Contributor Author

xbfool commented Jun 5, 2026

更新:cn query-db 现已覆盖全部服务

补了 databaseUrlPath 类服务(cred 不在 database-users、用服务自己 DATABASE_URL 的,如 gateway-core / sentinel / generation)。做法:getCnSecretsexpandSecretReferences=true 让 cn Infisical 服务端把 ${...} 引用解析成字面值,再 connectCnDBFromUrl 解析连接;parseDatabaseUrl 容忍 postgresql+asyncpg:// 驱动后缀。

实测:

  • query-db user-auth ... cn → 31(prefix 路径)
  • query-db optima-sentinel "SELECT 1" cn → 1(databaseUrlPath 路径)

⚠️ 顺带发现一个 cn 数据问题(非本 PR,需 cn 侧核对)

query-db gateway-core ... cnpassword authentication failed for user "gateway_user"。排查后不是代码问题:

  • cn Infisical /services/gateway-core 的 DATABASE_URL 口令 = 32 字符
  • db-init terraform.tfvarsgateway_db_password(RDS 实际) = 28 字符
  • 两者不一致,psql 用 Infisical 那个认证失败 → cn gateway-core 的 Infisical DATABASE_URL 口令疑似过时

含义:不止 CLI 用不了,线上 gateway-core 服务若用同一 secret 也可能连不上自己的库。建议 cn 侧核对 RDS 实际口令 ↔ Infisical /services/gateway-core ↔ db-init tfvars 三者对齐。要的话我单独开个 issue 跟。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant