@optima-chat/dev-skills 0.16.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -212,6 +212,8 @@ optima-query-db gateway-core "SELECT key, value FROM app_configs" cn-prod
|
|
|
212
212
|
| `INFISICAL_CN_EMAIL` + `INFISICAL_CN_PASSWORD` | 登录 cn Infisical 取数据库凭证 | 1Password「Infisical cn-prod admin (secrets-cn.optima.chat)」 |
|
|
213
213
|
| `OPTIMA_CN_BUILDBOX_PASSWORD` | 经 buildbox 跳板建 RDS 隧道(本机已有健康隧道时可省) | 1Password「Aliyun cn-prod buildbox ECS (root)」 |
|
|
214
214
|
|
|
215
|
+
> **两条独立路径**:① 从上表的 1P 取值 `export` 到当前 session(不落盘,推荐;有 `op` CLI 更佳);或 ② 若你**自己**维护本地明文文件 `~/.infisical_cn_creds`(每行 `export KEY=val`)和 `~/.buildbox_pw`(纯密码一行),工具会自动读取、免手动 `export`/`source`(路径分别可用 `INFISICAL_CN_CREDS_FILE` / `OPTIMA_CN_BUILDBOX_PW_FILE` 覆盖)。②是明文落盘的便捷取舍(`chmod 600`,权限宽于 600 工具会打警告),非默认做法、别把 1P 密码抄进文件;没放这些文件时回退走 env(①)。creds 文件解析**不是 shell**:只认最朴素的 `KEY=值` 与成对引号,无引号值别写行尾注释。
|
|
216
|
+
|
|
215
217
|
## 🔧 技术架构
|
|
216
218
|
|
|
217
219
|
### Infisical 配置(v0.7.0+)
|
|
@@ -77,8 +77,10 @@ optima-show-env agentic-chat stage
|
|
|
77
77
|
- `shopify-backend` - Shopify 店铺管理服务
|
|
78
78
|
|
|
79
79
|
**支持的环境**:
|
|
80
|
-
- `stage` - Stage
|
|
81
|
-
- `prod` - Prod
|
|
80
|
+
- `stage` - Stage 预发布环境(AWS Infisical)
|
|
81
|
+
- `prod` - Prod 生产环境(AWS Infisical)
|
|
82
|
+
- `cn-stage` - 阿里云 cn-stage(读 cn Infisical,需凭据,见「故障排查 · 问题 2b」)
|
|
83
|
+
- `cn-prod` - 阿里云 cn-prod(同上)
|
|
82
84
|
|
|
83
85
|
### 3. 使用过滤和选项
|
|
84
86
|
|
|
@@ -198,6 +200,17 @@ npm install -g @optima-chat/dev-skills@latest
|
|
|
198
200
|
- 运行 `gh auth login` 登录 GitHub
|
|
199
201
|
- 确认有仓库访问权限
|
|
200
202
|
|
|
203
|
+
### 问题 2b:cn-stage / cn-prod 报「需要 INFISICAL_CN_EMAIL / PASSWORD」
|
|
204
|
+
|
|
205
|
+
`cn-stage` / `cn-prod` 读的是**阿里云 cn Infisical**(`secrets-cn.optima.chat`,与 AWS 那套完全独立),需要 admin 账密两个环境变量。**两条独立路径,按你的安全取向二选一**:
|
|
206
|
+
|
|
207
|
+
- **走 1Password(推荐,密码不落盘)**:从「Infisical cn-prod admin (secrets-cn.optima.chat)」取值,`export INFISICAL_CN_EMAIL=... INFISICAL_CN_PASSWORD=...` 到当前 session(装了 `op` CLI 可 `op run` / `op read` 注入,用完即散)。
|
|
208
|
+
- **本地凭据文件(便捷,安全性较弱)**:若你**自己**维护 `~/.infisical_cn_creds`(每行 `export KEY=val`),本工具会自动读取、免手动 `source`;路径用 `INFISICAL_CN_CREDS_FILE` 覆盖。⚠️ 明文落盘(务必 `chmod 600`),是「省事 vs 暴露面」的取舍——**不是让你把 1P 密码抄进文件当默认做法**,只是给已经这么存的人一个自动读取入口。
|
|
209
|
+
|
|
210
|
+
> 两者互不依赖:有本地文件就自动用,没有就回退读 env(即走 1P 那条)。`optima-query-db cn-stage/cn-prod` 的 buildbox 密码同理——`export OPTIMA_CN_BUILDBOX_PASSWORD=...`,或本地 `~/.buildbox_pw`(纯密码一行,自动读取;`OPTIMA_CN_BUILDBOX_PW_FILE` 可覆盖路径);密码在 1P「Aliyun cn-prod buildbox ECS (root)」。
|
|
211
|
+
>
|
|
212
|
+
> ⚠️ creds 文件的解析**不是 shell**:只认每行一条最朴素的 `KEY=值`(可带 `export `),值要么整体用成对引号包住、要么不含空格;引号值后的内容(如行尾注释)会被忽略,**无引号值别写行尾注释**(`#` 会被当成值的一部分)。文件权限宽于 600 时工具会打警告。
|
|
213
|
+
|
|
201
214
|
### 问题 3:服务不存在
|
|
202
215
|
|
|
203
216
|
**错误信息**: "Unknown service 'xxx'"
|
package/bin/helpers/db-utils.ts
CHANGED
|
@@ -109,15 +109,75 @@ function curlJson(args: string[]): any {
|
|
|
109
109
|
catch { throw new Error(`cn Infisical: non-JSON response: ${String(out).slice(0, 200)}`); }
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
/** 明文密码文件权限比 600 宽(组/其他用户可读)时往 stderr 警告一行。Windows 无此语义,跳过。 */
|
|
113
|
+
function warnIfLoosePerms(file: string): void {
|
|
114
|
+
if (process.platform === 'win32') return;
|
|
115
|
+
try {
|
|
116
|
+
const mode = fs.statSync(file).mode & 0o777;
|
|
117
|
+
if (mode & 0o077) console.error(`⚠️ ${file} 权限 ${mode.toString(8).padStart(3, '0')}(同机其他用户可读)——建议 chmod 600 ${file}`);
|
|
118
|
+
} catch { /* stat 失败不阻塞主流程 */ }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 若 creds 文件存在,把其中 `export KEY=val` / `KEY=val` 行注入 process.env(已存在的键不覆盖)。
|
|
123
|
+
* 免去每次手动 `source ~/.infisical_cn_creds`;文件不存在则静默跳过,回退到调用方的 env 检查。
|
|
124
|
+
* ⚠️ 不是 shell:只认最朴素的每行一条 `KEY=值`。值以引号开头时取到配对引号为止(其后内容
|
|
125
|
+
* 忽略,行尾注释因此无害);引号未闭合的行警告并跳过(真 shell 会语法错,静默注入 `"abc`
|
|
126
|
+
* 这类错值只会换来下游 401、更难查);无引号值取到行尾原样保留(别写行尾注释,`#` 会进值)。
|
|
127
|
+
* 导出仅供测试。
|
|
128
|
+
*/
|
|
129
|
+
const loadedCredsFiles = new Set<string>(); // 同一文件只加载/警告一次(getCnInfisicalToken 与 setupCnSSHTunnel 都会调)
|
|
130
|
+
export function loadCredsFileIntoEnv(file: string): void {
|
|
131
|
+
if (loadedCredsFiles.has(file)) return;
|
|
132
|
+
loadedCredsFiles.add(file);
|
|
133
|
+
let content: string;
|
|
134
|
+
try { content = fs.readFileSync(file, 'utf-8'); }
|
|
135
|
+
catch { return; } // 文件不存在/不可读 = 无操作,保持原有「读 env」行为
|
|
136
|
+
warnIfLoosePerms(file);
|
|
137
|
+
const lines = content.split(/\r?\n/);
|
|
138
|
+
for (let i = 0; i < lines.length; i++) {
|
|
139
|
+
const m = lines[i].match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
|
|
140
|
+
if (!m) continue;
|
|
141
|
+
const key = m[1];
|
|
142
|
+
if (process.env[key] !== undefined) continue; // 已有 env 优先,不覆盖
|
|
143
|
+
let val = m[2].trim();
|
|
144
|
+
const q = val[0];
|
|
145
|
+
if (q === '"' || q === "'") {
|
|
146
|
+
const close = val.indexOf(q, 1);
|
|
147
|
+
if (close === -1) {
|
|
148
|
+
console.error(`⚠️ ${file} 第 ${i + 1} 行(${key}=)引号未闭合,已跳过该行`);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
val = val.slice(1, close); // 配对引号截取,忽略其后(如行尾注释)
|
|
152
|
+
}
|
|
153
|
+
process.env[key] = val;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 读 buildbox 密码文件(纯密码一行;默认 ~/.buildbox_pw,OPTIMA_CN_BUILDBOX_PW_FILE 可覆盖)。
|
|
159
|
+
* 文件不存在/为空 → undefined,回退到调用方报错。导出仅供测试。
|
|
160
|
+
*/
|
|
161
|
+
export function readBuildboxPwFile(): string | undefined {
|
|
162
|
+
const file = process.env.OPTIMA_CN_BUILDBOX_PW_FILE || `${os.homedir()}/.buildbox_pw`;
|
|
163
|
+
let pw: string;
|
|
164
|
+
try { pw = fs.readFileSync(file, 'utf-8').trim(); }
|
|
165
|
+
catch { return undefined; }
|
|
166
|
+
warnIfLoosePerms(file); // 读到文件就查权限,空文件也警告
|
|
167
|
+
if (!pw) return undefined;
|
|
168
|
+
return pw;
|
|
169
|
+
}
|
|
170
|
+
|
|
112
171
|
/**
|
|
113
172
|
* 认证 cn Infisical(admin email/password → org-scoped token)。
|
|
114
173
|
* ⚠️ 字段名坑:login 返回 `accessToken`(不是 token),select-organization 才返回 `token`。
|
|
115
174
|
*/
|
|
116
175
|
export function getCnInfisicalToken(): string {
|
|
176
|
+
loadCredsFileIntoEnv(process.env.INFISICAL_CN_CREDS_FILE || `${os.homedir()}/.infisical_cn_creds`);
|
|
117
177
|
const email = process.env.INFISICAL_CN_EMAIL;
|
|
118
178
|
const password = process.env.INFISICAL_CN_PASSWORD;
|
|
119
179
|
if (!email || !password) {
|
|
120
|
-
throw new Error('cn Infisical 需要 INFISICAL_CN_EMAIL + INFISICAL_CN_PASSWORD
|
|
180
|
+
throw new Error('cn Infisical 需要 INFISICAL_CN_EMAIL + INFISICAL_CN_PASSWORD。① 从 1Password "Infisical cn-prod admin (secrets-cn.optima.chat)" 取值 export(不落盘);或 ② 本工具自动读取本地文件 ~/.infisical_cn_creds(每行 `export KEY=val`,INFISICAL_CN_CREDS_FILE 可覆盖)。见 optima-dev-skills#21。');
|
|
121
181
|
}
|
|
122
182
|
const login = curlJson([
|
|
123
183
|
'-X', 'POST', `${CN_INFISICAL_URL}/api/v3/auth/login`,
|
|
@@ -318,8 +378,10 @@ function setupSSMTunnel(dbHost: string, localPort: number): void {
|
|
|
318
378
|
* 密码经 SSHPASS 环境变量传给 `sshpass -e`:不进命令行(ps 不可见)、不经 shell 引号展开。
|
|
319
379
|
*/
|
|
320
380
|
function setupCnSSHTunnel(dbHost: string, localPort: number): void {
|
|
321
|
-
|
|
322
|
-
|
|
381
|
+
// creds 文件里也可能放 OPTIMA_CN_BUILDBOX_PASSWORD——自己加载一次(幂等),不依赖 getCnInfisicalToken 恰好先跑
|
|
382
|
+
loadCredsFileIntoEnv(process.env.INFISICAL_CN_CREDS_FILE || `${os.homedir()}/.infisical_cn_creds`);
|
|
383
|
+
const pw = process.env.OPTIMA_CN_BUILDBOX_PASSWORD || readBuildboxPwFile();
|
|
384
|
+
if (!pw) throw new Error('cn DB 隧道需要 OPTIMA_CN_BUILDBOX_PASSWORD。① 从 1Password "Aliyun cn-prod buildbox ECS (root)" 取值 export;或 ② 本工具自动读取本地文件 ~/.buildbox_pw(纯密码一行,OPTIMA_CN_BUILDBOX_PW_FILE 可覆盖路径)。见 optima-dev-skills#21。');
|
|
323
385
|
try { execSync('command -v sshpass', { stdio: 'ignore' }); }
|
|
324
386
|
catch { throw new Error('sshpass not found — cn buildbox 隧道需要(apt install sshpass / brew install esolitos/ipa/sshpass)。Windows 用 WSL。'); }
|
|
325
387
|
|
|
@@ -43,6 +43,8 @@ exports.getGitHubVariable = getGitHubVariable;
|
|
|
43
43
|
exports.getInfisicalConfig = getInfisicalConfig;
|
|
44
44
|
exports.getInfisicalToken = getInfisicalToken;
|
|
45
45
|
exports.getInfisicalSecrets = getInfisicalSecrets;
|
|
46
|
+
exports.loadCredsFileIntoEnv = loadCredsFileIntoEnv;
|
|
47
|
+
exports.readBuildboxPwFile = readBuildboxPwFile;
|
|
46
48
|
exports.getCnInfisicalToken = getCnInfisicalToken;
|
|
47
49
|
exports.getCnSecrets = getCnSecrets;
|
|
48
50
|
exports.parseDatabaseUrl = parseDatabaseUrl;
|
|
@@ -137,15 +139,87 @@ function curlJson(args) {
|
|
|
137
139
|
throw new Error(`cn Infisical: non-JSON response: ${String(out).slice(0, 200)}`);
|
|
138
140
|
}
|
|
139
141
|
}
|
|
142
|
+
/** 明文密码文件权限比 600 宽(组/其他用户可读)时往 stderr 警告一行。Windows 无此语义,跳过。 */
|
|
143
|
+
function warnIfLoosePerms(file) {
|
|
144
|
+
if (process.platform === 'win32')
|
|
145
|
+
return;
|
|
146
|
+
try {
|
|
147
|
+
const mode = fs.statSync(file).mode & 0o777;
|
|
148
|
+
if (mode & 0o077)
|
|
149
|
+
console.error(`⚠️ ${file} 权限 ${mode.toString(8).padStart(3, '0')}(同机其他用户可读)——建议 chmod 600 ${file}`);
|
|
150
|
+
}
|
|
151
|
+
catch { /* stat 失败不阻塞主流程 */ }
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 若 creds 文件存在,把其中 `export KEY=val` / `KEY=val` 行注入 process.env(已存在的键不覆盖)。
|
|
155
|
+
* 免去每次手动 `source ~/.infisical_cn_creds`;文件不存在则静默跳过,回退到调用方的 env 检查。
|
|
156
|
+
* ⚠️ 不是 shell:只认最朴素的每行一条 `KEY=值`。值以引号开头时取到配对引号为止(其后内容
|
|
157
|
+
* 忽略,行尾注释因此无害);引号未闭合的行警告并跳过(真 shell 会语法错,静默注入 `"abc`
|
|
158
|
+
* 这类错值只会换来下游 401、更难查);无引号值取到行尾原样保留(别写行尾注释,`#` 会进值)。
|
|
159
|
+
* 导出仅供测试。
|
|
160
|
+
*/
|
|
161
|
+
const loadedCredsFiles = new Set(); // 同一文件只加载/警告一次(getCnInfisicalToken 与 setupCnSSHTunnel 都会调)
|
|
162
|
+
function loadCredsFileIntoEnv(file) {
|
|
163
|
+
if (loadedCredsFiles.has(file))
|
|
164
|
+
return;
|
|
165
|
+
loadedCredsFiles.add(file);
|
|
166
|
+
let content;
|
|
167
|
+
try {
|
|
168
|
+
content = fs.readFileSync(file, 'utf-8');
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return;
|
|
172
|
+
} // 文件不存在/不可读 = 无操作,保持原有「读 env」行为
|
|
173
|
+
warnIfLoosePerms(file);
|
|
174
|
+
const lines = content.split(/\r?\n/);
|
|
175
|
+
for (let i = 0; i < lines.length; i++) {
|
|
176
|
+
const m = lines[i].match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
|
|
177
|
+
if (!m)
|
|
178
|
+
continue;
|
|
179
|
+
const key = m[1];
|
|
180
|
+
if (process.env[key] !== undefined)
|
|
181
|
+
continue; // 已有 env 优先,不覆盖
|
|
182
|
+
let val = m[2].trim();
|
|
183
|
+
const q = val[0];
|
|
184
|
+
if (q === '"' || q === "'") {
|
|
185
|
+
const close = val.indexOf(q, 1);
|
|
186
|
+
if (close === -1) {
|
|
187
|
+
console.error(`⚠️ ${file} 第 ${i + 1} 行(${key}=)引号未闭合,已跳过该行`);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
val = val.slice(1, close); // 配对引号截取,忽略其后(如行尾注释)
|
|
191
|
+
}
|
|
192
|
+
process.env[key] = val;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 读 buildbox 密码文件(纯密码一行;默认 ~/.buildbox_pw,OPTIMA_CN_BUILDBOX_PW_FILE 可覆盖)。
|
|
197
|
+
* 文件不存在/为空 → undefined,回退到调用方报错。导出仅供测试。
|
|
198
|
+
*/
|
|
199
|
+
function readBuildboxPwFile() {
|
|
200
|
+
const file = process.env.OPTIMA_CN_BUILDBOX_PW_FILE || `${os.homedir()}/.buildbox_pw`;
|
|
201
|
+
let pw;
|
|
202
|
+
try {
|
|
203
|
+
pw = fs.readFileSync(file, 'utf-8').trim();
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
warnIfLoosePerms(file); // 读到文件就查权限,空文件也警告
|
|
209
|
+
if (!pw)
|
|
210
|
+
return undefined;
|
|
211
|
+
return pw;
|
|
212
|
+
}
|
|
140
213
|
/**
|
|
141
214
|
* 认证 cn Infisical(admin email/password → org-scoped token)。
|
|
142
215
|
* ⚠️ 字段名坑:login 返回 `accessToken`(不是 token),select-organization 才返回 `token`。
|
|
143
216
|
*/
|
|
144
217
|
function getCnInfisicalToken() {
|
|
218
|
+
loadCredsFileIntoEnv(process.env.INFISICAL_CN_CREDS_FILE || `${os.homedir()}/.infisical_cn_creds`);
|
|
145
219
|
const email = process.env.INFISICAL_CN_EMAIL;
|
|
146
220
|
const password = process.env.INFISICAL_CN_PASSWORD;
|
|
147
221
|
if (!email || !password) {
|
|
148
|
-
throw new Error('cn Infisical 需要 INFISICAL_CN_EMAIL + INFISICAL_CN_PASSWORD
|
|
222
|
+
throw new Error('cn Infisical 需要 INFISICAL_CN_EMAIL + INFISICAL_CN_PASSWORD。① 从 1Password "Infisical cn-prod admin (secrets-cn.optima.chat)" 取值 export(不落盘);或 ② 本工具自动读取本地文件 ~/.infisical_cn_creds(每行 `export KEY=val`,INFISICAL_CN_CREDS_FILE 可覆盖)。见 optima-dev-skills#21。');
|
|
149
223
|
}
|
|
150
224
|
const login = curlJson([
|
|
151
225
|
'-X', 'POST', `${CN_INFISICAL_URL}/api/v3/auth/login`,
|
|
@@ -357,9 +431,11 @@ function setupSSMTunnel(dbHost, localPort) {
|
|
|
357
431
|
* 密码经 SSHPASS 环境变量传给 `sshpass -e`:不进命令行(ps 不可见)、不经 shell 引号展开。
|
|
358
432
|
*/
|
|
359
433
|
function setupCnSSHTunnel(dbHost, localPort) {
|
|
360
|
-
|
|
434
|
+
// creds 文件里也可能放 OPTIMA_CN_BUILDBOX_PASSWORD——自己加载一次(幂等),不依赖 getCnInfisicalToken 恰好先跑
|
|
435
|
+
loadCredsFileIntoEnv(process.env.INFISICAL_CN_CREDS_FILE || `${os.homedir()}/.infisical_cn_creds`);
|
|
436
|
+
const pw = process.env.OPTIMA_CN_BUILDBOX_PASSWORD || readBuildboxPwFile();
|
|
361
437
|
if (!pw)
|
|
362
|
-
throw new Error('cn DB 隧道需要 OPTIMA_CN_BUILDBOX_PASSWORD
|
|
438
|
+
throw new Error('cn DB 隧道需要 OPTIMA_CN_BUILDBOX_PASSWORD。① 从 1Password "Aliyun cn-prod buildbox ECS (root)" 取值 export;或 ② 本工具自动读取本地文件 ~/.buildbox_pw(纯密码一行,OPTIMA_CN_BUILDBOX_PW_FILE 可覆盖路径)。见 optima-dev-skills#21。');
|
|
363
439
|
try {
|
|
364
440
|
(0, child_process_1.execSync)('command -v sshpass', { stdio: 'ignore' });
|
|
365
441
|
}
|