@keyboxed/cli 0.3.0 → 0.3.2
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.
- package/assets/skill/SKILL.md +2 -2
- package/assets/worker.js +31 -3
- package/dist/keybox.js +798 -247
- package/package.json +1 -1
package/dist/keybox.js
CHANGED
|
@@ -41,48 +41,359 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
41
41
|
mod
|
|
42
42
|
));
|
|
43
43
|
|
|
44
|
+
// src/dove.ts
|
|
45
|
+
var DOVE_LARGE, DOVE_SMALL;
|
|
46
|
+
var init_dove = __esm({
|
|
47
|
+
"src/dove.ts"() {
|
|
48
|
+
"use strict";
|
|
49
|
+
DOVE_LARGE = [
|
|
50
|
+
"\u2820\u28C4\u2858\u28B6\u28C4\u2840\u28A0\u2840",
|
|
51
|
+
"\u2830\u28CC\u28FB\u28F2\u28F3\u289F\u28E6\u28BD\u28D6\u28C4",
|
|
52
|
+
"\u2830\u28EC\u2873\u28FD\u28DE\u28DF\u28BE\u289D\u286E\u28DE\u28B5\u28C2",
|
|
53
|
+
"\u2800\u28A8\u28DD\u28D7\u28B7\u28BD\u28DD\u28F5\u286B\u286E\u28D3\u288E\u2897\u28A4\u2800\u28C0",
|
|
54
|
+
"\u2800\u2800\u28A8\u287A\u286F\u2877\u28F3\u28A7\u28AF\u28AF\u28AE\u2873\u28F1\u28B1\u288D\u289E\u28A6",
|
|
55
|
+
"\u2800\u2800\u2800\u2829\u287F\u28DD\u28D7\u28AF\u28EB\u2897\u28D7\u28BD\u2878\u28F8\u28B8\u28B8\u28B8\u28B8\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28C0\u2864\u28DE\u2804",
|
|
56
|
+
"\u2800\u2800\u2800\u2800\u2808\u289F\u28EE\u289F\u286E\u28EF\u28FA\u28B5\u28EB\u28AA\u286A\u286A\u286A\u286A\u2873\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28E0\u28E6\u287E\u28EF\u28EF\u286F\u2816",
|
|
57
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u282B\u28DF\u287E\u28D5\u28B7\u289D\u286E\u28E3\u2873\u2879\u2878\u2878\u2878\u2878\u2872\u28C4\u2800\u2800\u2800\u28A0\u28DE\u2857\u286E\u2883\u2800\u2800\u2800\u2800\u28C0\u28F4\u28FE\u28F3\u28BF\u289E\u28DF\u28FD\u28BE\u281A\u2801",
|
|
58
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u28AB\u28EF\u28BE\u289D\u28EE\u2873\u28D5\u2887\u2887\u2887\u2887\u2887\u2897\u289C\u289D\u28B7\u28FE\u28BF\u2873\u28F1\u28C1\u28C0\u28C0\u2864\u2874\u285E\u285F\u28DD\u28AE\u287A\u28EA\u28D7\u28FF\u281A\u2801",
|
|
59
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u2837\u28FB\u287C\u28FA\u286A\u28C7\u2897\u2895\u2895\u2895\u2895\u2895\u28F5\u28BF\u28DD\u2887\u288F\u28D6\u2887\u2897\u289C\u28DC\u289C\u28CE\u28D7\u2897\u28D7\u28EF\u2837\u280B",
|
|
60
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2809\u281A\u2833\u28AF\u28FA\u28EA\u28E3\u28B3\u28B1\u28F5\u28FF\u28BD\u286B\u284E\u286E\u28E3\u2897\u28B5\u2879\u28D5\u28F5\u28EB\u28DE\u287E\u287D\u2813\u2801",
|
|
61
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2841\u2851\u28E3\u28EF\u28FF\u28DF\u289E\u2855\u28C7\u28AF\u287A\u28D5\u28AF\u28F3\u28BD\u28FA\u287A\u281E\u280A\u2801",
|
|
62
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28E4\u2896\u28D6\u28AE\u287A\u28EA\u28AB\u286A\u28EA\u2897\u28B5\u2857\u2847\u28D7\u28B5\u2833\u2819\u283A\u2819\u281D\u280E\u280B\u2808",
|
|
63
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28BE\u28FB\u28AE\u28D7\u28DD\u28AE\u286A\u287A\u28F8\u28B8\u28B8\u28B8\u28A8\u28B2\u2841",
|
|
64
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u283B\u28F3\u2897\u28D7\u2875\u28EB\u287A\u28F8\u28AA\u287A\u285C\u286E\u28FA\u287A\u28E6",
|
|
65
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u281B\u28DE\u28FD\u287A\u287C\u286E\u28F3\u28B5\u28EB\u28BE\u28B5\u28FB\u287A\u280B",
|
|
66
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u2819\u2839\u287D\u28B3\u28FB\u283A\u283D\u280B\u2803"
|
|
67
|
+
];
|
|
68
|
+
DOVE_SMALL = [
|
|
69
|
+
"\u2890\u28AC\u28F2\u28C4\u2862\u2840",
|
|
70
|
+
"\u2828\u28DF\u28DE\u286E\u286F\u286F\u2872\u2840",
|
|
71
|
+
"\u2800\u2893\u287D\u287E\u28DD\u286E\u28CF\u288E\u2887\u2896\u2840",
|
|
72
|
+
"\u2800\u2800\u2819\u28FD\u28AE\u28AF\u287A\u28D5\u28B5\u28B1\u28B1\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28E0\u28E4\u2876\u2841",
|
|
73
|
+
"\u2800\u2800\u2800\u2808\u282F\u28D7\u28AF\u2873\u2875\u2871\u2871\u28D9\u2826\u28C4\u28C0\u28E4\u28BE\u2831\u2800\u2800\u28E0\u2874\u287E\u287B\u28FD\u287A\u2803",
|
|
74
|
+
"\u2800\u2800\u2800\u2800\u2800\u2819\u2897\u28EF\u2873\u285D\u285C\u2854\u286D\u28E2\u287F\u284E\u2867\u2873\u286A\u28DD\u28BC\u28DC\u287E\u283D\u2809",
|
|
75
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u2813\u289D\u28EE\u28FE\u28AF\u28B3\u2879\u28EA\u28F3\u28DD\u286E\u2817\u280B\u2808",
|
|
76
|
+
"\u2800\u2800\u2800\u2800\u2820\u28D6\u28D6\u28AE\u287A\u2878\u285D\u287A\u2871\u2815\u2803\u280B\u2803\u2803\u2801",
|
|
77
|
+
"\u2800\u2800\u2800\u2800\u2800\u2811\u28EF\u2897\u2875\u28E3\u2873\u28F1\u2895\u286F\u28C2",
|
|
78
|
+
"\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u281B\u289E\u28AE\u2837\u282F\u281B\u2809"
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// src/ui.ts
|
|
84
|
+
function step(s) {
|
|
85
|
+
process.stderr.write(`${c.accent("\u25A0")} ${s}
|
|
86
|
+
`);
|
|
87
|
+
}
|
|
88
|
+
function note(s) {
|
|
89
|
+
process.stderr.write(`${c.dim(" " + s)}
|
|
90
|
+
`);
|
|
91
|
+
}
|
|
92
|
+
function ok(s) {
|
|
93
|
+
process.stderr.write(`${c.ok("\u2713")} ${s}
|
|
94
|
+
`);
|
|
95
|
+
}
|
|
96
|
+
function warn(s) {
|
|
97
|
+
process.stderr.write(`${c.warn("!")} ${s}
|
|
98
|
+
`);
|
|
99
|
+
}
|
|
100
|
+
function table(rows) {
|
|
101
|
+
if (rows.length === 0) return "";
|
|
102
|
+
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => width(r[i] ?? ""))));
|
|
103
|
+
return rows.map((r) => r.map((cell, i) => i === r.length - 1 ? cell : pad(cell, widths[i])).join(" ")).join("\n") + "\n";
|
|
104
|
+
}
|
|
105
|
+
function headed(rows) {
|
|
106
|
+
if (!rows.length) return "";
|
|
107
|
+
const tty = process.stdout.isTTY && color;
|
|
108
|
+
return table(tty ? [rows[0].map(label), ...rows.slice(1)] : rows);
|
|
109
|
+
}
|
|
110
|
+
function when(ms) {
|
|
111
|
+
return new Date(ms).toISOString().replace("T", " ").slice(0, 19);
|
|
112
|
+
}
|
|
113
|
+
function ago(ms) {
|
|
114
|
+
if (!ms) return "never";
|
|
115
|
+
const s = Math.round((Date.now() - ms) / 1e3);
|
|
116
|
+
if (s < 60) return `${s}s ago`;
|
|
117
|
+
if (s < 3600) return `${Math.round(s / 60)}m ago`;
|
|
118
|
+
if (s < 86400) return `${Math.round(s / 3600)}h ago`;
|
|
119
|
+
return `${Math.round(s / 86400)}d ago`;
|
|
120
|
+
}
|
|
121
|
+
function parseDuration(s) {
|
|
122
|
+
const m = /^(\d+)([smhd]?)$/.exec(s.trim());
|
|
123
|
+
if (!m) throw new Error(`bad duration: ${s}`);
|
|
124
|
+
return Number(m[1]) * { "": 1, s: 1, m: 60, h: 3600, d: 86400 }[m[2]];
|
|
125
|
+
}
|
|
126
|
+
function banner(lines = [], size = "large") {
|
|
127
|
+
if (!color) return lines.length ? lines.join("\n") + "\n" : "";
|
|
128
|
+
const cols = process.stderr.columns ?? 80;
|
|
129
|
+
const dove = size === "large" && cols >= 92 ? DOVE_LARGE : DOVE_SMALL;
|
|
130
|
+
const dw = Math.max(...dove.map((l) => [...l].length));
|
|
131
|
+
if (cols < dw + 20) return lines.join("\n") + "\n";
|
|
132
|
+
const squares = /* @__PURE__ */ new Map([
|
|
133
|
+
[1, [dw + 4, "\u25A0"]],
|
|
134
|
+
[Math.floor(dove.length * 0.7), [dw - 6, "\u25AA"]],
|
|
135
|
+
[dove.length - 2, [dw + 9, "\u25AB"]]
|
|
136
|
+
]);
|
|
137
|
+
const top = Math.max(0, Math.floor((dove.length - lines.length) / 2));
|
|
138
|
+
const out2 = [];
|
|
139
|
+
for (let i = 0; i < dove.length; i++) {
|
|
140
|
+
const d = dove[i];
|
|
141
|
+
let row = " " + d + " ".repeat(dw - [...d].length);
|
|
142
|
+
const text = lines[i - top];
|
|
143
|
+
const sq = squares.get(i);
|
|
144
|
+
if (text !== void 0) row += " " + text;
|
|
145
|
+
else if (sq) row = row.slice(0, 2 + sq[0]) + " ".repeat(Math.max(0, 2 + sq[0] - width(row))) + c.accent(sq[1]);
|
|
146
|
+
out2.push(row);
|
|
147
|
+
}
|
|
148
|
+
return "\n" + out2.join("\n") + "\n\n";
|
|
149
|
+
}
|
|
150
|
+
function spin(text) {
|
|
151
|
+
if (!color) {
|
|
152
|
+
process.stderr.write(`${text}
|
|
153
|
+
`);
|
|
154
|
+
return (final) => final && process.stderr.write(final + "\n");
|
|
155
|
+
}
|
|
156
|
+
const frames = "\u280B\u2819\u2839\u2838\u283C\u2834\u2826\u2827\u2807\u280F";
|
|
157
|
+
let i = 0;
|
|
158
|
+
const draw = () => process.stderr.write(`\r\x1B[2K${c.accent(frames[i++ % frames.length])} ${text}`);
|
|
159
|
+
draw();
|
|
160
|
+
const t = setInterval(draw, 80);
|
|
161
|
+
return (final) => {
|
|
162
|
+
clearInterval(t);
|
|
163
|
+
process.stderr.write(`\r\x1B[2K${final ? final + "\n" : ""}`);
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
async function task(label2, fn, done) {
|
|
167
|
+
const name = label2.padEnd(16);
|
|
168
|
+
const stop = color ? spin(name) : (final) => final && process.stderr.write(final + "\n");
|
|
169
|
+
try {
|
|
170
|
+
const r = await fn();
|
|
171
|
+
stop(`${c.ok("\u2713")} ${name}${c.dim(done ? done(r) : "")}`);
|
|
172
|
+
return r;
|
|
173
|
+
} catch (err) {
|
|
174
|
+
stop(`${c.bad("\u2717")} ${name}`);
|
|
175
|
+
const tail = err.stderr;
|
|
176
|
+
if (tail) {
|
|
177
|
+
const lines = tail.replace(/\x1b\[[0-9;]*m/g, "").split("\n").map((l) => l.trimEnd()).filter((l) => l.trim() && !/^[─┌└│├┐┘]/.test(l));
|
|
178
|
+
for (const l of lines.slice(-8)) process.stderr.write(c.dim(` ${l}
|
|
179
|
+
`));
|
|
180
|
+
}
|
|
181
|
+
throw err;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var env, color, truecolor, sgr, rgb, c, width, pad, label, wordmark;
|
|
185
|
+
var init_ui = __esm({
|
|
186
|
+
"src/ui.ts"() {
|
|
187
|
+
"use strict";
|
|
188
|
+
init_dove();
|
|
189
|
+
env = process.env;
|
|
190
|
+
color = Boolean(process.stderr.isTTY) && !env.NO_COLOR && env.TERM !== "dumb";
|
|
191
|
+
truecolor = color && /truecolor|24bit/i.test(env.COLORTERM ?? "");
|
|
192
|
+
sgr = (open, close = "\x1B[0m") => (s) => color ? `\x1B[${open}m${s}${close}` : s;
|
|
193
|
+
rgb = (r, g, b, fallback) => sgr(truecolor ? `38;2;${r};${g};${b}` : fallback, "\x1B[39m");
|
|
194
|
+
c = {
|
|
195
|
+
// Cobalt that reads on both light and dark terminals.
|
|
196
|
+
accent: rgb(96, 124, 240, "38;5;69"),
|
|
197
|
+
ok: rgb(111, 194, 141, "32"),
|
|
198
|
+
bad: rgb(232, 120, 100, "31"),
|
|
199
|
+
warn: rgb(222, 170, 90, "33"),
|
|
200
|
+
dim: sgr("2", "\x1B[22m"),
|
|
201
|
+
bold: sgr("1", "\x1B[22m"),
|
|
202
|
+
italic: sgr("3", "\x1B[23m")
|
|
203
|
+
};
|
|
204
|
+
width = (s) => s.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, "").length;
|
|
205
|
+
pad = (s, n) => s + " ".repeat(Math.max(0, n - width(s)));
|
|
206
|
+
label = (s) => c.dim(s.toUpperCase());
|
|
207
|
+
wordmark = () => `${c.accent("\u25A0")} ${c.bold("keybox")}`;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
44
211
|
// src/config.ts
|
|
45
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
212
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
46
213
|
import { homedir } from "node:os";
|
|
47
|
-
import { join } from "node:path";
|
|
214
|
+
import { dirname, join } from "node:path";
|
|
48
215
|
function configDir() {
|
|
49
216
|
return join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "keybox");
|
|
50
217
|
}
|
|
51
|
-
function
|
|
218
|
+
function deployDir(key) {
|
|
219
|
+
return join(configDir(), key ?? current()?.config.deploy ?? "deploy");
|
|
220
|
+
}
|
|
221
|
+
function nameFromUrl(url) {
|
|
222
|
+
let host = "";
|
|
223
|
+
try {
|
|
224
|
+
host = new URL(url).hostname;
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
const parts2 = host.split(".");
|
|
228
|
+
const first = parts2[0] === "keybox" && parts2.length > 2 ? parts2[1] : parts2[0];
|
|
229
|
+
const n = (first ?? "").toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/^-+|-+$/g, "").slice(0, 31);
|
|
230
|
+
return PROFILE.test(n) ? n : "default";
|
|
231
|
+
}
|
|
232
|
+
function readStore() {
|
|
233
|
+
if (!existsSync(configPath())) return { profiles: {} };
|
|
234
|
+
let raw;
|
|
235
|
+
try {
|
|
236
|
+
raw = JSON.parse(readFileSync(configPath(), "utf8"));
|
|
237
|
+
} catch {
|
|
238
|
+
throw new UserError(`${configPath()} is not valid JSON. Fix or remove it.`);
|
|
239
|
+
}
|
|
240
|
+
if (raw && typeof raw.profiles === "object" && raw.profiles) return { current: raw.current, profiles: raw.profiles };
|
|
241
|
+
if (raw && typeof raw.url === "string") {
|
|
242
|
+
const name = nameFromUrl(raw.url);
|
|
243
|
+
return { current: name, profiles: { [name]: { ...raw, deploy: raw.deploy ?? "deploy" } } };
|
|
244
|
+
}
|
|
245
|
+
return { profiles: {} };
|
|
246
|
+
}
|
|
247
|
+
function writeStore(s) {
|
|
248
|
+
mkdirSync(configDir(), { recursive: true, mode: 448 });
|
|
249
|
+
const tmp = `${configPath()}.tmp`;
|
|
250
|
+
writeFileSync(tmp, JSON.stringify(s, null, 2) + "\n", { mode: 384 });
|
|
251
|
+
chmodSync(tmp, 384);
|
|
252
|
+
renameSync(tmp, configPath());
|
|
253
|
+
}
|
|
254
|
+
function useProfile(name) {
|
|
255
|
+
flagProfile = name;
|
|
256
|
+
}
|
|
257
|
+
function projectPin(cwd) {
|
|
258
|
+
let dir = cwd;
|
|
259
|
+
for (let i = 0; i < 40; i++) {
|
|
260
|
+
const file = join(dir, PIN_FILE);
|
|
261
|
+
try {
|
|
262
|
+
if (existsSync(file) && statSync(file).size < 4096) {
|
|
263
|
+
const url = JSON.parse(readFileSync(file, "utf8")).url;
|
|
264
|
+
if (typeof url === "string" && /^https?:\/\/[^/\s]+$/.test(url.replace(/\/+$/, ""))) return { url, file };
|
|
265
|
+
}
|
|
266
|
+
} catch {
|
|
267
|
+
}
|
|
268
|
+
const up = dirname(dir);
|
|
269
|
+
if (up === dir) break;
|
|
270
|
+
dir = up;
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
function resolveProfile(cwd = process.cwd()) {
|
|
52
275
|
const env2 = process.env;
|
|
276
|
+
const store = readStore();
|
|
277
|
+
const named = (name, source) => {
|
|
278
|
+
const config = store.profiles[name];
|
|
279
|
+
if (!config) throw new UserError(`No connection called ${name}. See \`keybox profiles\`.`);
|
|
280
|
+
return { name, config, source };
|
|
281
|
+
};
|
|
282
|
+
if (flagProfile) return named(flagProfile, "flag");
|
|
283
|
+
if (env2.KEYBOX_PROFILE) return named(env2.KEYBOX_PROFILE, "env");
|
|
53
284
|
if (env2.KEYBOX_URL && env2.KEYBOX_DEVICE_ID && env2.KEYBOX_DEVICE_KEY) {
|
|
54
|
-
return {
|
|
285
|
+
return {
|
|
286
|
+
name: "(environment)",
|
|
287
|
+
source: "environment",
|
|
288
|
+
config: { url: env2.KEYBOX_URL, deviceId: env2.KEYBOX_DEVICE_ID, deviceKey: env2.KEYBOX_DEVICE_KEY, name: env2.KEYBOX_NAME ?? "keybox", d1Id: "" }
|
|
289
|
+
};
|
|
55
290
|
}
|
|
56
291
|
if (env2.KEYBOX_URL && env2.KEYBOX_ADMIN_TOKEN) {
|
|
57
292
|
return {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
name: env2.KEYBOX_NAME ?? "keybox",
|
|
61
|
-
d1Id: "",
|
|
62
|
-
accountId: env2.KEYBOX_ACCOUNT_ID ?? env2.CLOUDFLARE_ACCOUNT_ID
|
|
293
|
+
name: "(environment)",
|
|
294
|
+
source: "environment",
|
|
295
|
+
config: { url: env2.KEYBOX_URL, adminToken: env2.KEYBOX_ADMIN_TOKEN, name: env2.KEYBOX_NAME ?? "keybox", d1Id: "", accountId: env2.KEYBOX_ACCOUNT_ID ?? env2.CLOUDFLARE_ACCOUNT_ID }
|
|
63
296
|
};
|
|
64
297
|
}
|
|
65
|
-
|
|
66
|
-
|
|
298
|
+
const pin = projectPin(cwd);
|
|
299
|
+
if (pin) {
|
|
300
|
+
const hit = Object.entries(store.profiles).find(([, p]) => sameUrl(p.url, pin.url));
|
|
301
|
+
if (!hit) {
|
|
302
|
+
throw new UserError(`This folder uses ${pin.url} (from ${pin.file}), and this machine is not connected to it. Run \`keybox login ${pin.url}\`.`);
|
|
303
|
+
}
|
|
304
|
+
return { ...named(hit[0], "project"), file: pin.file };
|
|
305
|
+
}
|
|
306
|
+
const names = Object.keys(store.profiles);
|
|
307
|
+
if (!names.length) return null;
|
|
308
|
+
if (store.current && store.profiles[store.current]) return named(store.current, "current");
|
|
309
|
+
if (names.length === 1) return named(names[0], "current");
|
|
310
|
+
throw new UserError(`Several Keyboxes are saved (${names.join(", ")}). Pick one with \`keybox use NAME\`, or --profile NAME.`);
|
|
67
311
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
312
|
+
function current() {
|
|
313
|
+
try {
|
|
314
|
+
return resolveProfile();
|
|
315
|
+
} catch {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
72
318
|
}
|
|
73
|
-
function
|
|
74
|
-
|
|
75
|
-
writeFileSync(configPath(), JSON.stringify(c2, null, 2) + "\n", { mode: 384 });
|
|
76
|
-
chmodSync(configPath(), 384);
|
|
319
|
+
function loadConfig() {
|
|
320
|
+
return resolveProfile()?.config ?? null;
|
|
77
321
|
}
|
|
78
|
-
|
|
322
|
+
function requireConfig() {
|
|
323
|
+
const r = resolveProfile();
|
|
324
|
+
if (!r && process.env.KEYBOX_ISOLATED === "1") {
|
|
325
|
+
throw new UserError("This runs inside `keybox run`, which hides Keybox credentials from the agent on purpose. Run Keybox admin commands in your own terminal.");
|
|
326
|
+
}
|
|
327
|
+
if (!r) throw new UserError("Not set up yet. Run `keybox init` to deploy one, or `keybox login` to connect to one.");
|
|
328
|
+
if (!announced && r.source !== "environment" && process.stderr.isTTY && Object.keys(readStore().profiles).length > 1) {
|
|
329
|
+
announced = true;
|
|
330
|
+
const why = { flag: "--profile", env: "KEYBOX_PROFILE", project: r.file ?? PIN_FILE, current: "default", environment: "" }[r.source];
|
|
331
|
+
process.stderr.write(c.dim(`using ${r.name} (${r.config.url}) \xB7 ${why}`) + "\n");
|
|
332
|
+
}
|
|
333
|
+
return r.config;
|
|
334
|
+
}
|
|
335
|
+
function listProfiles() {
|
|
336
|
+
return { store: readStore(), resolved: current() };
|
|
337
|
+
}
|
|
338
|
+
function saveConfig(cfg, opts = {}) {
|
|
339
|
+
const s = readStore();
|
|
340
|
+
const existing = Object.entries(s.profiles).find(([, p]) => sameUrl(p.url, cfg.url))?.[0];
|
|
341
|
+
let name = existing;
|
|
342
|
+
const wanted = opts.as?.toLowerCase();
|
|
343
|
+
if (wanted && !PROFILE.test(wanted)) throw new UserError("A name is lowercase letters, digits and dashes (up to 31 characters).");
|
|
344
|
+
if (!name) {
|
|
345
|
+
name = wanted ?? nameFromUrl(cfg.url);
|
|
346
|
+
if (s.profiles[name]) {
|
|
347
|
+
if (wanted) throw new UserError(`There is already a connection called ${wanted}, for ${s.profiles[name].url}. Pick another name with --as.`);
|
|
348
|
+
for (let i = 2; s.profiles[name]; i++) name = `${nameFromUrl(cfg.url)}-${i}`;
|
|
349
|
+
}
|
|
350
|
+
s.current = name;
|
|
351
|
+
} else if (wanted && wanted !== name) {
|
|
352
|
+
if (s.profiles[wanted]) throw new UserError(`There is already a connection called ${wanted}, for ${s.profiles[wanted].url}.`);
|
|
353
|
+
delete s.profiles[name];
|
|
354
|
+
if (s.current === name) s.current = wanted;
|
|
355
|
+
name = wanted;
|
|
356
|
+
}
|
|
357
|
+
s.profiles[name] = cfg;
|
|
358
|
+
if (!s.current || !s.profiles[s.current]) s.current = name;
|
|
359
|
+
writeStore(s);
|
|
360
|
+
return name;
|
|
361
|
+
}
|
|
362
|
+
function setCurrent(name) {
|
|
363
|
+
const s = readStore();
|
|
364
|
+
const p = s.profiles[name];
|
|
365
|
+
if (!p) throw new UserError(`No connection called ${name}. See \`keybox profiles\`.`);
|
|
366
|
+
s.current = name;
|
|
367
|
+
writeStore(s);
|
|
368
|
+
return p;
|
|
369
|
+
}
|
|
370
|
+
function removeProfile(name) {
|
|
371
|
+
const s = readStore();
|
|
372
|
+
const p = s.profiles[name];
|
|
373
|
+
if (!p) throw new UserError(`No connection called ${name}. See \`keybox profiles\`.`);
|
|
374
|
+
delete s.profiles[name];
|
|
375
|
+
if (s.current === name) s.current = Object.keys(s.profiles).length === 1 ? Object.keys(s.profiles)[0] : void 0;
|
|
376
|
+
writeStore(s);
|
|
377
|
+
return p;
|
|
378
|
+
}
|
|
379
|
+
function pinFolder(dir, url) {
|
|
380
|
+
const file = join(dir, PIN_FILE);
|
|
381
|
+
writeFileSync(file, JSON.stringify({ url: url.replace(/\/+$/, "") }, null, 2) + "\n");
|
|
382
|
+
return file;
|
|
383
|
+
}
|
|
384
|
+
var PROFILE, PIN_FILE, UserError, configPath, sameUrl, flagProfile, announced, isOwnDeployment;
|
|
79
385
|
var init_config = __esm({
|
|
80
386
|
"src/config.ts"() {
|
|
81
387
|
"use strict";
|
|
82
|
-
|
|
83
|
-
|
|
388
|
+
init_ui();
|
|
389
|
+
PROFILE = /^[a-z0-9][a-z0-9-]{0,30}$/;
|
|
390
|
+
PIN_FILE = ".keybox.json";
|
|
84
391
|
UserError = class extends Error {
|
|
85
392
|
};
|
|
393
|
+
configPath = () => join(configDir(), "config.json");
|
|
394
|
+
sameUrl = (a, b) => a.replace(/\/+$/, "").toLowerCase() === b.replace(/\/+$/, "").toLowerCase();
|
|
395
|
+
announced = false;
|
|
396
|
+
isOwnDeployment = (cfg) => Boolean(cfg.name && cfg.d1Id);
|
|
86
397
|
}
|
|
87
398
|
});
|
|
88
399
|
|
|
@@ -180,6 +491,79 @@ var init_grants = __esm({
|
|
|
180
491
|
}
|
|
181
492
|
});
|
|
182
493
|
|
|
494
|
+
// src/isolate.ts
|
|
495
|
+
import { spawnSync } from "node:child_process";
|
|
496
|
+
import { existsSync as existsSync2, realpathSync } from "node:fs";
|
|
497
|
+
import { delimiter, join as join2 } from "node:path";
|
|
498
|
+
function onPath(bin2) {
|
|
499
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
500
|
+
const p = join2(dir, bin2);
|
|
501
|
+
if (dir && existsSync2(p)) return p;
|
|
502
|
+
}
|
|
503
|
+
return null;
|
|
504
|
+
}
|
|
505
|
+
function real(dirs) {
|
|
506
|
+
return [...new Set(dirs.filter((d) => existsSync2(d)).map((d) => realpathSync(d)))];
|
|
507
|
+
}
|
|
508
|
+
function bwrapArgs(hide, cwd, cmd, args) {
|
|
509
|
+
return [
|
|
510
|
+
"--dev-bind",
|
|
511
|
+
"/",
|
|
512
|
+
"/",
|
|
513
|
+
// A fresh /proc in its own pid namespace: otherwise /proc/<pid>/environ of keybox itself, or of the
|
|
514
|
+
// shell that started it, would hand back a KEYBOX_DEVICE_KEY set in the environment.
|
|
515
|
+
"--unshare-pid",
|
|
516
|
+
"--proc",
|
|
517
|
+
"/proc",
|
|
518
|
+
...hide.flatMap((d) => ["--tmpfs", d, "--remount-ro", d]),
|
|
519
|
+
"--die-with-parent",
|
|
520
|
+
"--chdir",
|
|
521
|
+
cwd,
|
|
522
|
+
"--",
|
|
523
|
+
cmd,
|
|
524
|
+
...args
|
|
525
|
+
];
|
|
526
|
+
}
|
|
527
|
+
function sandboxProfile(hide) {
|
|
528
|
+
return ["(version 1)", "(allow default)", ...hide.map((d) => `(deny file-read* file-write* (subpath ${sbpl(d)}))`)].join("\n");
|
|
529
|
+
}
|
|
530
|
+
function isolate(cmd, args, hideDirs, platform = process.platform) {
|
|
531
|
+
const hide = real(hideDirs);
|
|
532
|
+
if (!hide.length) return { ok: true, cmd, args, how: "nothing to hide" };
|
|
533
|
+
if (platform === "linux") {
|
|
534
|
+
const bwrap = onPath("bwrap");
|
|
535
|
+
if (!bwrap) {
|
|
536
|
+
return {
|
|
537
|
+
ok: false,
|
|
538
|
+
why: "bubblewrap (bwrap) is not installed",
|
|
539
|
+
hint: "Install it (apt install bubblewrap \xB7 dnf install bubblewrap \xB7 pacman -S bubblewrap \xB7 emerge sys-apps/bubblewrap)."
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
const probe = spawnSync(bwrap, bwrapArgs(hide, "/", "true", []), { stdio: "ignore", timeout: 5e3 });
|
|
543
|
+
if (probe.status !== 0) {
|
|
544
|
+
return {
|
|
545
|
+
ok: false,
|
|
546
|
+
why: "bubblewrap could not create a sandbox here (unprivileged user namespaces are off?)",
|
|
547
|
+
hint: "Run the agent in a container or as another OS user instead."
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
return { ok: true, cmd: bwrap, args: bwrapArgs(hide, process.cwd(), cmd, args), how: "bubblewrap" };
|
|
551
|
+
}
|
|
552
|
+
if (platform === "darwin") {
|
|
553
|
+
const se = "/usr/bin/sandbox-exec";
|
|
554
|
+
if (!existsSync2(se)) return { ok: false, why: "sandbox-exec is missing" };
|
|
555
|
+
return { ok: true, cmd: se, args: ["-p", sandboxProfile(hide), cmd, ...args], how: "sandbox-exec" };
|
|
556
|
+
}
|
|
557
|
+
return { ok: false, why: `hiding files is not supported on ${platform}`, hint: "Run the agent in a container or as another OS user." };
|
|
558
|
+
}
|
|
559
|
+
var sbpl;
|
|
560
|
+
var init_isolate = __esm({
|
|
561
|
+
"src/isolate.ts"() {
|
|
562
|
+
"use strict";
|
|
563
|
+
sbpl = (s) => `"${s.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
|
|
183
567
|
// ../shared/providers.ts
|
|
184
568
|
function byName(name) {
|
|
185
569
|
const n = name.toUpperCase();
|
|
@@ -300,170 +684,125 @@ var init_providers = __esm({
|
|
|
300
684
|
}
|
|
301
685
|
});
|
|
302
686
|
|
|
303
|
-
//
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
687
|
+
// ../shared/suggest.ts
|
|
688
|
+
function idLike(segment) {
|
|
689
|
+
return segment.includes("*") || ID_LIKE.some((re) => re.test(segment));
|
|
690
|
+
}
|
|
691
|
+
function suggestRules(refusals) {
|
|
692
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
693
|
+
const add = (p) => {
|
|
694
|
+
const key = `${p.method} ${p.host}/${p.segs.join("/")}`;
|
|
695
|
+
const prev = byKey.get(key);
|
|
696
|
+
if (prev) prev.calls += p.calls;
|
|
697
|
+
else byKey.set(key, { ...p, segs: [...p.segs] });
|
|
698
|
+
};
|
|
699
|
+
for (const r of refusals) {
|
|
700
|
+
if (!r.host || !r.path) continue;
|
|
701
|
+
const segs = r.path.split("/").slice(1).map((s) => idLike(s) ? "*" : s);
|
|
702
|
+
add({ method: r.method.toUpperCase(), host: r.host, segs, calls: 1, example: `${r.method.toUpperCase()} ${r.host}${r.path}` });
|
|
703
|
+
}
|
|
704
|
+
for (let changed = true; changed; ) {
|
|
705
|
+
changed = false;
|
|
706
|
+
const patterns = [...byKey.values()];
|
|
707
|
+
const maxLen = Math.max(0, ...patterns.map((p) => p.segs.length));
|
|
708
|
+
for (let i = 0; i < maxLen && !changed; i++) {
|
|
709
|
+
const groups = /* @__PURE__ */ new Map();
|
|
710
|
+
for (const p of patterns) {
|
|
711
|
+
if (p.segs.length <= i || p.segs[i] === "*") continue;
|
|
712
|
+
const shape = `${p.method} ${p.host} ${p.segs.length} ${p.segs.map((s, j) => j === i ? "\0" : s).join("/")}`;
|
|
713
|
+
groups.set(shape, [...groups.get(shape) ?? [], p]);
|
|
714
|
+
}
|
|
715
|
+
for (const group of groups.values()) {
|
|
716
|
+
if (new Set(group.map((p) => p.segs[i])).size < MERGE_AT) continue;
|
|
717
|
+
for (const p of group) {
|
|
718
|
+
byKey.delete(`${p.method} ${p.host}/${p.segs.join("/")}`);
|
|
719
|
+
p.segs[i] = "*";
|
|
720
|
+
add(p);
|
|
721
|
+
}
|
|
722
|
+
changed = true;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
727
|
+
for (const p of byKey.values()) {
|
|
728
|
+
const target = `${p.host}/${p.segs.join("/")}`;
|
|
729
|
+
const prev = byPath.get(target);
|
|
730
|
+
if (prev) {
|
|
731
|
+
prev.methods.add(p.method);
|
|
732
|
+
prev.calls += p.calls;
|
|
733
|
+
} else byPath.set(target, { methods: /* @__PURE__ */ new Set([p.method]), calls: p.calls, example: p.example });
|
|
734
|
+
}
|
|
735
|
+
return [...byPath.entries()].map(([target, v]) => ({ rule: `${[...v.methods].sort().join(",")} ${target}`, calls: v.calls, example: v.example })).sort((a, b) => b.calls - a.calls || a.rule.localeCompare(b.rule));
|
|
736
|
+
}
|
|
737
|
+
var ID_LIKE, MERGE_AT;
|
|
738
|
+
var init_suggest = __esm({
|
|
739
|
+
"../shared/suggest.ts"() {
|
|
740
|
+
ID_LIKE = [
|
|
741
|
+
/^\d+$/,
|
|
742
|
+
// 12345
|
|
743
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
744
|
+
// uuid
|
|
745
|
+
/^(?=.*\d)[0-9a-f]{7,}$/i,
|
|
746
|
+
// commit sha, hex id
|
|
747
|
+
/^(?=.*\d)(?=.*[a-z])[A-Za-z0-9_-]{20,}$/i,
|
|
748
|
+
// long opaque id (base64url, ulid, ...)
|
|
749
|
+
/^[a-z]{2,8}_(?=.*\d)[A-Za-z0-9]{8,}$/,
|
|
750
|
+
// prefixed ids: cus_9s8d7f6g, msg_01abc...
|
|
751
|
+
/^\d{4}-\d{2}-\d{2}/,
|
|
752
|
+
// dates
|
|
753
|
+
/@/
|
|
754
|
+
// emails, user@host
|
|
338
755
|
];
|
|
756
|
+
MERGE_AT = 3;
|
|
339
757
|
}
|
|
340
758
|
});
|
|
341
759
|
|
|
342
|
-
// src/
|
|
343
|
-
function
|
|
344
|
-
|
|
345
|
-
`);
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
`);
|
|
354
|
-
}
|
|
355
|
-
function warn(s) {
|
|
356
|
-
process.stderr.write(`${c.warn("!")} ${s}
|
|
357
|
-
`);
|
|
358
|
-
}
|
|
359
|
-
function table(rows) {
|
|
360
|
-
if (rows.length === 0) return "";
|
|
361
|
-
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => width(r[i] ?? ""))));
|
|
362
|
-
return rows.map((r) => r.map((cell, i) => i === r.length - 1 ? cell : pad(cell, widths[i])).join(" ")).join("\n") + "\n";
|
|
363
|
-
}
|
|
364
|
-
function headed(rows) {
|
|
365
|
-
if (!rows.length) return "";
|
|
366
|
-
const tty = process.stdout.isTTY && color;
|
|
367
|
-
return table(tty ? [rows[0].map(label), ...rows.slice(1)] : rows);
|
|
368
|
-
}
|
|
369
|
-
function when(ms) {
|
|
370
|
-
return new Date(ms).toISOString().replace("T", " ").slice(0, 19);
|
|
371
|
-
}
|
|
372
|
-
function ago(ms) {
|
|
373
|
-
if (!ms) return "never";
|
|
374
|
-
const s = Math.round((Date.now() - ms) / 1e3);
|
|
375
|
-
if (s < 60) return `${s}s ago`;
|
|
376
|
-
if (s < 3600) return `${Math.round(s / 60)}m ago`;
|
|
377
|
-
if (s < 86400) return `${Math.round(s / 3600)}h ago`;
|
|
378
|
-
return `${Math.round(s / 86400)}d ago`;
|
|
379
|
-
}
|
|
380
|
-
function parseDuration(s) {
|
|
381
|
-
const m = /^(\d+)([smhd]?)$/.exec(s.trim());
|
|
382
|
-
if (!m) throw new Error(`bad duration: ${s}`);
|
|
383
|
-
return Number(m[1]) * { "": 1, s: 1, m: 60, h: 3600, d: 86400 }[m[2]];
|
|
384
|
-
}
|
|
385
|
-
function banner(lines = [], size = "large") {
|
|
386
|
-
if (!color) return lines.length ? lines.join("\n") + "\n" : "";
|
|
387
|
-
const cols = process.stderr.columns ?? 80;
|
|
388
|
-
const dove = size === "large" && cols >= 92 ? DOVE_LARGE : DOVE_SMALL;
|
|
389
|
-
const dw = Math.max(...dove.map((l) => [...l].length));
|
|
390
|
-
if (cols < dw + 20) return lines.join("\n") + "\n";
|
|
391
|
-
const squares = /* @__PURE__ */ new Map([
|
|
392
|
-
[1, [dw + 4, "\u25A0"]],
|
|
393
|
-
[Math.floor(dove.length * 0.7), [dw - 6, "\u25AA"]],
|
|
394
|
-
[dove.length - 2, [dw + 9, "\u25AB"]]
|
|
395
|
-
]);
|
|
396
|
-
const top = Math.max(0, Math.floor((dove.length - lines.length) / 2));
|
|
397
|
-
const out2 = [];
|
|
398
|
-
for (let i = 0; i < dove.length; i++) {
|
|
399
|
-
const d = dove[i];
|
|
400
|
-
let row = " " + d + " ".repeat(dw - [...d].length);
|
|
401
|
-
const text = lines[i - top];
|
|
402
|
-
const sq = squares.get(i);
|
|
403
|
-
if (text !== void 0) row += " " + text;
|
|
404
|
-
else if (sq) row = row.slice(0, 2 + sq[0]) + " ".repeat(Math.max(0, 2 + sq[0] - width(row))) + c.accent(sq[1]);
|
|
405
|
-
out2.push(row);
|
|
406
|
-
}
|
|
407
|
-
return "\n" + out2.join("\n") + "\n\n";
|
|
408
|
-
}
|
|
409
|
-
function spin(text) {
|
|
410
|
-
if (!color) {
|
|
411
|
-
process.stderr.write(`${text}
|
|
412
|
-
`);
|
|
413
|
-
return (final) => final && process.stderr.write(final + "\n");
|
|
414
|
-
}
|
|
415
|
-
const frames = "\u280B\u2819\u2839\u2838\u283C\u2834\u2826\u2827\u2807\u280F";
|
|
416
|
-
let i = 0;
|
|
417
|
-
const draw = () => process.stderr.write(`\r\x1B[2K${c.accent(frames[i++ % frames.length])} ${text}`);
|
|
418
|
-
draw();
|
|
419
|
-
const t = setInterval(draw, 80);
|
|
420
|
-
return (final) => {
|
|
421
|
-
clearInterval(t);
|
|
422
|
-
process.stderr.write(`\r\x1B[2K${final ? final + "\n" : ""}`);
|
|
760
|
+
// ../worker/src/policy.ts
|
|
761
|
+
function parseRule(source, opts = {}) {
|
|
762
|
+
const m = RULE.exec(source.trim());
|
|
763
|
+
if (!m) throw new Error(`bad rule: ${source}`);
|
|
764
|
+
const [, method, host, path = "/**"] = m;
|
|
765
|
+
const methods = method === "*" ? null : new Set(method.toUpperCase().split(","));
|
|
766
|
+
return {
|
|
767
|
+
source,
|
|
768
|
+
methods,
|
|
769
|
+
host: host === "*" ? /^.+$/ : new RegExp(`^${host.split("*").map(escape).join("[a-z0-9-]+")}$`),
|
|
770
|
+
path: globToRegExp(path, opts.caseInsensitive)
|
|
423
771
|
};
|
|
424
772
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
773
|
+
function matches(rule, method, host, path) {
|
|
774
|
+
return (!rule.methods || rule.methods.has(method)) && rule.host.test(host) && rule.path.test(path);
|
|
775
|
+
}
|
|
776
|
+
function globToRegExp(glob, caseInsensitive = false) {
|
|
777
|
+
let re = "";
|
|
778
|
+
for (let i = 0; i < glob.length; ) {
|
|
779
|
+
if (glob.startsWith("/**/", i)) {
|
|
780
|
+
re += "(?:/|/.*/)";
|
|
781
|
+
i += 4;
|
|
782
|
+
} else if (glob.startsWith("/**", i) && i + 3 === glob.length) {
|
|
783
|
+
re += "(?:/.*)?";
|
|
784
|
+
i += 3;
|
|
785
|
+
} else if (glob.startsWith("**", i)) {
|
|
786
|
+
re += ".*";
|
|
787
|
+
i += 2;
|
|
788
|
+
} else if (glob[i] === "*") {
|
|
789
|
+
re += "[^/]*";
|
|
790
|
+
i += 1;
|
|
791
|
+
} else {
|
|
792
|
+
re += escape(glob[i]);
|
|
793
|
+
i += 1;
|
|
439
794
|
}
|
|
440
|
-
throw err;
|
|
441
795
|
}
|
|
796
|
+
return new RegExp(`^${re}$`, caseInsensitive ? "i" : "");
|
|
442
797
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
798
|
+
function escape(s) {
|
|
799
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
800
|
+
}
|
|
801
|
+
var RULE;
|
|
802
|
+
var init_policy = __esm({
|
|
803
|
+
"../worker/src/policy.ts"() {
|
|
446
804
|
"use strict";
|
|
447
|
-
|
|
448
|
-
env = process.env;
|
|
449
|
-
color = Boolean(process.stderr.isTTY) && !env.NO_COLOR && env.TERM !== "dumb";
|
|
450
|
-
truecolor = color && /truecolor|24bit/i.test(env.COLORTERM ?? "");
|
|
451
|
-
sgr = (open, close = "\x1B[0m") => (s) => color ? `\x1B[${open}m${s}${close}` : s;
|
|
452
|
-
rgb = (r, g, b, fallback) => sgr(truecolor ? `38;2;${r};${g};${b}` : fallback, "\x1B[39m");
|
|
453
|
-
c = {
|
|
454
|
-
// Cobalt that reads on both light and dark terminals.
|
|
455
|
-
accent: rgb(96, 124, 240, "38;5;69"),
|
|
456
|
-
ok: rgb(111, 194, 141, "32"),
|
|
457
|
-
bad: rgb(232, 120, 100, "31"),
|
|
458
|
-
warn: rgb(222, 170, 90, "33"),
|
|
459
|
-
dim: sgr("2", "\x1B[22m"),
|
|
460
|
-
bold: sgr("1", "\x1B[22m"),
|
|
461
|
-
italic: sgr("3", "\x1B[23m")
|
|
462
|
-
};
|
|
463
|
-
width = (s) => s.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, "").length;
|
|
464
|
-
pad = (s, n) => s + " ".repeat(Math.max(0, n - width(s)));
|
|
465
|
-
label = (s) => c.dim(s.toUpperCase());
|
|
466
|
-
wordmark = () => `${c.accent("\u25A0")} ${c.bold("keybox")}`;
|
|
805
|
+
RULE = /^([A-Za-z*]+(?:,[A-Za-z]+)*)\s+([a-z0-9*.-]+)(\/\S*)?$/;
|
|
467
806
|
}
|
|
468
807
|
});
|
|
469
808
|
|
|
@@ -768,12 +1107,12 @@ var init_prompt = __esm({
|
|
|
768
1107
|
});
|
|
769
1108
|
|
|
770
1109
|
// src/migrate.ts
|
|
771
|
-
import { spawn, spawnSync } from "node:child_process";
|
|
772
|
-
import { existsSync as
|
|
1110
|
+
import { spawn, spawnSync as spawnSync2 } from "node:child_process";
|
|
1111
|
+
import { existsSync as existsSync3, readdirSync, readFileSync as readFileSync2 } from "node:fs";
|
|
773
1112
|
import { homedir as homedir2 } from "node:os";
|
|
774
|
-
import { join as
|
|
1113
|
+
import { join as join3 } from "node:path";
|
|
775
1114
|
function has(bin2) {
|
|
776
|
-
return
|
|
1115
|
+
return spawnSync2(process.platform === "win32" ? "where" : "which", [bin2], { stdio: "ignore" }).status === 0;
|
|
777
1116
|
}
|
|
778
1117
|
function capture(bin2, args) {
|
|
779
1118
|
if (!has(bin2)) throw new UserError(`${bin2} is not installed or not on PATH.`);
|
|
@@ -812,7 +1151,7 @@ function infisicalBase() {
|
|
|
812
1151
|
const fromEnv = process.env.INFISICAL_DOMAIN?.replace(/\/+$/, "");
|
|
813
1152
|
if (fromEnv) return /\/api$/.test(fromEnv) ? fromEnv : `${fromEnv}/api`;
|
|
814
1153
|
try {
|
|
815
|
-
const cfg = JSON.parse(readFileSync2(
|
|
1154
|
+
const cfg = JSON.parse(readFileSync2(join3(homedir2(), ".infisical", "infisical-config.json"), "utf8"));
|
|
816
1155
|
const p = cfg.profiles?.find((x) => x.name === cfg.activeProfile) ?? cfg.profiles?.[0];
|
|
817
1156
|
if (p?.domain) return p.domain.replace(/\/+$/, "");
|
|
818
1157
|
} catch {
|
|
@@ -1169,7 +1508,7 @@ async function migrate(opts) {
|
|
|
1169
1508
|
const [id, ...rest] = from.split(":");
|
|
1170
1509
|
const arg = rest.join(":") || void 0;
|
|
1171
1510
|
if (NOT_READABLE[id]) throw new UserError(NOT_READABLE[id]);
|
|
1172
|
-
const source = SOURCES.find((s) => s.id === id) ?? (
|
|
1511
|
+
const source = SOURCES.find((s) => s.id === id) ?? (existsSync3(from) ? SOURCES[0] : void 0);
|
|
1173
1512
|
if (!source) throw new UserError(`Unknown source ${id}. Sources: ${SOURCES.map((s) => s.id).join(", ")}.`);
|
|
1174
1513
|
const fileArg = source === SOURCES[0] && !arg ? from : arg;
|
|
1175
1514
|
const interactive = Boolean(process.stdin.isTTY) && !opts.yes && !opts.dryRun;
|
|
@@ -1541,9 +1880,11 @@ var init_presets = __esm({
|
|
|
1541
1880
|
// src/commands.ts
|
|
1542
1881
|
var commands_exports = {};
|
|
1543
1882
|
__export(commands_exports, {
|
|
1883
|
+
agentsAdd: () => agentsAdd,
|
|
1544
1884
|
agentsCreate: () => agentsCreate,
|
|
1545
1885
|
agentsList: () => agentsList,
|
|
1546
1886
|
agentsRm: () => agentsRm,
|
|
1887
|
+
agentsSuggest: () => agentsSuggest,
|
|
1547
1888
|
agentsToken: () => agentsToken,
|
|
1548
1889
|
audit: () => audit,
|
|
1549
1890
|
blocked: () => blocked,
|
|
@@ -1674,6 +2015,90 @@ async function agentsCreate(name, opts) {
|
|
|
1674
2015
|
}
|
|
1675
2016
|
note(`Everything else is denied. Start it with: keybox run ${name} -- <command>`);
|
|
1676
2017
|
}
|
|
2018
|
+
function namedRules(grants) {
|
|
2019
|
+
return {
|
|
2020
|
+
allow: grants.flatMap((g) => g.allow.map((r) => `${g.secret}=${r}`)),
|
|
2021
|
+
deny: grants.flatMap((g) => (g.deny ?? []).map((r) => `${g.secret}=${r}`))
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
async function agentsAdd(name, opts) {
|
|
2025
|
+
const allow = opts.allow ?? [];
|
|
2026
|
+
const deny = opts.deny ?? [];
|
|
2027
|
+
if (!allow.length && !deny.length) throw new UserError(`Give a rule, e.g. keybox agents allow ${name} "GET api.github.com/repos/acme/**".`);
|
|
2028
|
+
const [agents, secrets] = await Promise.all([api("GET", "/agents"), api("GET", "/secrets")]);
|
|
2029
|
+
const agent = agents.find((a) => a.name === name);
|
|
2030
|
+
if (!agent && !allow.length) throw new UserError(`No agent named ${name}. Create it with an allow rule first.`);
|
|
2031
|
+
const had = namedRules(agent?.grants ?? []);
|
|
2032
|
+
const grants = buildGrants2([...had.allow, ...allow], [...had.deny, ...deny], secrets).map((g) => ({
|
|
2033
|
+
...g,
|
|
2034
|
+
allow: [...new Set(g.allow)],
|
|
2035
|
+
...g.deny ? { deny: [...new Set(g.deny)] } : {}
|
|
2036
|
+
}));
|
|
2037
|
+
const before = /* @__PURE__ */ new Set([...had.allow, ...had.deny.map((r) => `deny ${r}`)]);
|
|
2038
|
+
await api("PUT", `/agents/${name}`, { grants });
|
|
2039
|
+
let added = 0;
|
|
2040
|
+
for (const g of grants) {
|
|
2041
|
+
for (const r of g.allow) {
|
|
2042
|
+
if (before.has(`${g.secret}=${r}`)) continue;
|
|
2043
|
+
process.stderr.write(` ${c.ok("+ allow")} ${r} ${c.dim(`(with ${g.secret})`)}
|
|
2044
|
+
`);
|
|
2045
|
+
added++;
|
|
2046
|
+
}
|
|
2047
|
+
for (const r of g.deny ?? []) {
|
|
2048
|
+
if (before.has(`deny ${g.secret}=${r}`)) continue;
|
|
2049
|
+
process.stderr.write(` ${c.bad("+ deny ")} ${r}
|
|
2050
|
+
`);
|
|
2051
|
+
added++;
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
if (!added) return note(`${name} already had ${allow.length + deny.length === 1 ? "that rule" : "those rules"}.`);
|
|
2055
|
+
ok(`${agent ? "Updated" : "Created"} ${name}. It applies on the next call; running tokens keep working.`);
|
|
2056
|
+
}
|
|
2057
|
+
function shellArg(s) {
|
|
2058
|
+
return /^[A-Za-z0-9._~\-/%:@,+=* ]*$/.test(s) ? `'${s}'` : null;
|
|
2059
|
+
}
|
|
2060
|
+
async function agentsSuggest(name, opts) {
|
|
2061
|
+
const [agents, secrets, log] = await Promise.all([
|
|
2062
|
+
api("GET", "/agents"),
|
|
2063
|
+
api("GET", "/secrets"),
|
|
2064
|
+
api("GET", `/audit?agent=${encodeURIComponent(name)}&reason=not_allowed&limit=${Math.min(Number(opts.limit) || 500, 500)}`)
|
|
2065
|
+
]);
|
|
2066
|
+
const agent = agents.find((a) => a.name === name);
|
|
2067
|
+
if (!agent) throw new UserError(`No agent named ${name}.`);
|
|
2068
|
+
const rules = agent.grants.flatMap((g) => g.allow.map((r) => parseRule(r)));
|
|
2069
|
+
const refused = log.filter(
|
|
2070
|
+
(r) => r.agent === name && r.reason === "not_allowed" && r.host && r.path && !rules.some((rule) => matches(rule, r.method, r.host, r.path))
|
|
2071
|
+
);
|
|
2072
|
+
if (!refused.length) return note(`No refused calls for ${name} that its rules do not already cover.`);
|
|
2073
|
+
const bound = new Set(secrets.flatMap((s) => s.hosts));
|
|
2074
|
+
const all = suggestRules(refused.map((r) => ({ method: r.method, host: r.host, path: r.path })));
|
|
2075
|
+
const usable = all.filter((s) => bound.has(s.rule.split(" ")[1].split("/")[0]));
|
|
2076
|
+
const unbound = [...new Set(all.filter((s) => !usable.includes(s)).map((s) => s.rule.split(" ")[1].split("/")[0]))];
|
|
2077
|
+
if (unbound.length) note(`No stored key is bound to ${unbound.join(", ")}, so no rule for ${unbound.length === 1 ? "it" : "them"} would help. Add or bind a key first.`);
|
|
2078
|
+
if (!usable.length) return;
|
|
2079
|
+
step(`${refused.length} refused call${refused.length === 1 ? "" : "s"} by ${c.bold(name)}. Suggested rules (ids replaced by *):`);
|
|
2080
|
+
process.stderr.write(
|
|
2081
|
+
table(usable.map((s) => [` ${s.rule}`, c.dim(`${s.calls} call${s.calls === 1 ? "" : "s"}`), c.dim(s.example.length > 70 ? s.example.slice(0, 69) + "\u2026" : s.example)]))
|
|
2082
|
+
);
|
|
2083
|
+
let picked;
|
|
2084
|
+
if (opts.yes) picked = usable.map((s) => s.rule);
|
|
2085
|
+
else if (process.stdin.isTTY && process.stderr.isTTY) {
|
|
2086
|
+
picked = await checklist(
|
|
2087
|
+
"Allow which? (space toggles, a picks all, enter applies)",
|
|
2088
|
+
usable.map((s) => ({ value: s.rule, label: s.rule, hint: `${s.calls}\xD7` })),
|
|
2089
|
+
() => false
|
|
2090
|
+
);
|
|
2091
|
+
} else {
|
|
2092
|
+
const args = usable.map((s) => shellArg(s.rule));
|
|
2093
|
+
const safe = args.filter((a) => a !== null);
|
|
2094
|
+
if (safe.length) process.stdout.write(`keybox agents allow ${name} ${safe.join(" ")}
|
|
2095
|
+
`);
|
|
2096
|
+
if (safe.length < args.length) note(`${args.length - safe.length} suggestion(s) hold characters unsafe to paste into a shell; review them at a terminal.`);
|
|
2097
|
+
return note("Nothing applied. Run the command above, or re-run at a terminal to pick.");
|
|
2098
|
+
}
|
|
2099
|
+
if (!picked.length) return note("Nothing applied.");
|
|
2100
|
+
await agentsAdd(name, { allow: picked });
|
|
2101
|
+
}
|
|
1677
2102
|
async function agentsList() {
|
|
1678
2103
|
const rows = await api("GET", "/agents");
|
|
1679
2104
|
if (!rows.length) return note("No agents yet. Create one with `keybox agents create NAME --allow RULE`.");
|
|
@@ -1718,15 +2143,26 @@ async function run(name, cmd, opts) {
|
|
|
1718
2143
|
const removed = [];
|
|
1719
2144
|
const stored = new Set(secrets.map((s) => s.name));
|
|
1720
2145
|
for (const k of Object.keys(env2)) {
|
|
1721
|
-
if (isKeyVar(k) || stored.has(k)) {
|
|
2146
|
+
if (isKeyVar(k) || stored.has(k) || k.startsWith("KEYBOX_")) {
|
|
1722
2147
|
delete env2[k];
|
|
1723
2148
|
removed.push(k);
|
|
1724
2149
|
}
|
|
1725
2150
|
}
|
|
1726
2151
|
if (removed.length) note(`Removed ${removed.join(", ")} from ${name}'s environment.`);
|
|
1727
2152
|
Object.assign(env2, envFor(hosts, c2.url.replace(/\/$/, ""), minted.token));
|
|
2153
|
+
let launch = { cmd: cmd[0], args: cmd.slice(1) };
|
|
2154
|
+
if (opts.isolate) {
|
|
2155
|
+
const iso = isolate(cmd[0], cmd.slice(1), [configDir()]);
|
|
2156
|
+
if (iso.ok) {
|
|
2157
|
+
launch = { cmd: iso.cmd, args: iso.args };
|
|
2158
|
+
env2.KEYBOX_ISOLATED = "1";
|
|
2159
|
+
} else {
|
|
2160
|
+
warn(`Could not hide this machine's Keybox credentials from ${cmd[0]}: ${iso.why}. It can read ${configDir()}.`);
|
|
2161
|
+
if (iso.hint) note(iso.hint);
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
1728
2164
|
const revoke = () => api("DELETE", `/tokens/${minted.id}`).catch(() => note(`Could not revoke token ${minted.id}; it expires ${when(minted.expires_at)} UTC.`));
|
|
1729
|
-
const child = spawn2(cmd
|
|
2165
|
+
const child = spawn2(launch.cmd, launch.args, { stdio: "inherit", env: env2 });
|
|
1730
2166
|
const ignore = () => {
|
|
1731
2167
|
};
|
|
1732
2168
|
process.on("SIGINT", ignore);
|
|
@@ -1781,7 +2217,10 @@ var init_commands = __esm({
|
|
|
1781
2217
|
init_api();
|
|
1782
2218
|
init_grants();
|
|
1783
2219
|
init_config();
|
|
2220
|
+
init_isolate();
|
|
1784
2221
|
init_providers();
|
|
2222
|
+
init_suggest();
|
|
2223
|
+
init_policy();
|
|
1785
2224
|
init_migrate();
|
|
1786
2225
|
init_presets();
|
|
1787
2226
|
init_prompt();
|
|
@@ -1794,11 +2233,11 @@ var init_commands = __esm({
|
|
|
1794
2233
|
import { spawn as spawn3 } from "node:child_process";
|
|
1795
2234
|
import { readFileSync as readFileSync3 } from "node:fs";
|
|
1796
2235
|
import { createRequire } from "node:module";
|
|
1797
|
-
import { dirname, join as
|
|
2236
|
+
import { dirname as dirname2, join as join4 } from "node:path";
|
|
1798
2237
|
function bin() {
|
|
1799
2238
|
const pkgPath = createRequire(import.meta.url).resolve("wrangler/package.json");
|
|
1800
2239
|
const pkg = JSON.parse(readFileSync3(pkgPath, "utf8"));
|
|
1801
|
-
return
|
|
2240
|
+
return join4(dirname2(pkgPath), pkg.bin.wrangler);
|
|
1802
2241
|
}
|
|
1803
2242
|
function useAccount(id) {
|
|
1804
2243
|
accountId = id;
|
|
@@ -1932,7 +2371,7 @@ var VERSION;
|
|
|
1932
2371
|
var init_version = __esm({
|
|
1933
2372
|
"src/version.ts"() {
|
|
1934
2373
|
"use strict";
|
|
1935
|
-
VERSION = true ? "0.3.
|
|
2374
|
+
VERSION = true ? "0.3.2" : "0.0.0-dev";
|
|
1936
2375
|
}
|
|
1937
2376
|
});
|
|
1938
2377
|
|
|
@@ -2062,6 +2501,7 @@ function printChecks(list2) {
|
|
|
2062
2501
|
}
|
|
2063
2502
|
async function verify(opts) {
|
|
2064
2503
|
const cfg = requireConfig();
|
|
2504
|
+
if (!isOwnDeployment(cfg)) throw new UserError(`\`keybox verify\` checks the Worker in your own Cloudflare account. ${cfg.url} is run by someone else (for a hosted team, the operator updates it).`);
|
|
2065
2505
|
const account = await resolveAccount(cfg);
|
|
2066
2506
|
const list2 = await checks(cfg);
|
|
2067
2507
|
if (opts.json) {
|
|
@@ -2108,8 +2548,8 @@ __export(deploy_exports, {
|
|
|
2108
2548
|
});
|
|
2109
2549
|
import { spawn as spawn4 } from "node:child_process";
|
|
2110
2550
|
import { createHash as createHash3, randomBytes as randomBytes2 } from "node:crypto";
|
|
2111
|
-
import { copyFileSync, existsSync as
|
|
2112
|
-
import { join as
|
|
2551
|
+
import { copyFileSync, existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "node:fs";
|
|
2552
|
+
import { join as join5 } from "node:path";
|
|
2113
2553
|
async function deployWorker(cfg) {
|
|
2114
2554
|
for (let attempt = 1; ; attempt++) {
|
|
2115
2555
|
try {
|
|
@@ -2121,12 +2561,12 @@ async function deployWorker(cfg) {
|
|
|
2121
2561
|
}
|
|
2122
2562
|
}
|
|
2123
2563
|
}
|
|
2124
|
-
function writeDeployFiles(name, d1Id, accountId2) {
|
|
2125
|
-
const dir = deployDir();
|
|
2126
|
-
mkdirSync2(
|
|
2127
|
-
copyFileSync(asset("worker.js"),
|
|
2128
|
-
copyFileSync(asset("schema.sql"),
|
|
2129
|
-
const cfg =
|
|
2564
|
+
function writeDeployFiles(name, d1Id, accountId2, key) {
|
|
2565
|
+
const dir = deployDir(key);
|
|
2566
|
+
mkdirSync2(join5(dir, "worker"), { recursive: true, mode: 448 });
|
|
2567
|
+
copyFileSync(asset("worker.js"), join5(dir, "worker", "worker.js"));
|
|
2568
|
+
copyFileSync(asset("schema.sql"), join5(dir, "schema.sql"));
|
|
2569
|
+
const cfg = join5(dir, "wrangler.json");
|
|
2130
2570
|
writeFileSync2(
|
|
2131
2571
|
cfg,
|
|
2132
2572
|
JSON.stringify(
|
|
@@ -2175,7 +2615,6 @@ async function pickAccount(accounts, wanted) {
|
|
|
2175
2615
|
async function init(opts) {
|
|
2176
2616
|
const { name } = opts;
|
|
2177
2617
|
if (!/^[a-z0-9][a-z0-9-]{0,62}$/.test(name)) throw new UserError("--name must be lowercase letters, digits and dashes.");
|
|
2178
|
-
if (loadConfig()) throw new UserError(`Already set up (${configPath()}). Use \`keybox deploy\` to upgrade, or \`keybox verify\` to check it.`);
|
|
2179
2618
|
process.stderr.write(banner([wordmark() + c.dim(` v${VERSION}`), c.dim("deploying to your own Cloudflare account"), "", c.dim("your keys never pass through keybox.space")], "small"));
|
|
2180
2619
|
const major = Number(process.versions.node.split(".")[0]);
|
|
2181
2620
|
if (major < 20) throw new UserError(`Keybox needs Node 20 or later (you have ${process.versions.node}).`);
|
|
@@ -2189,6 +2628,10 @@ async function init(opts) {
|
|
|
2189
2628
|
}
|
|
2190
2629
|
const account = await pickAccount(me.accounts, opts.account);
|
|
2191
2630
|
useAccount(account.id);
|
|
2631
|
+
const { store } = listProfiles();
|
|
2632
|
+
const dup = Object.entries(store.profiles).find(([, p]) => p.name === name && p.accountId === account.id);
|
|
2633
|
+
if (dup) throw new UserError(`Already set up as "${dup[0]}" (${dup[1].url}). Use \`keybox deploy\` to upgrade, or \`keybox verify\` to check it.`);
|
|
2634
|
+
const deployKey = Object.keys(store.profiles).length ? `deploy-${name}-${account.id.slice(0, 8)}` : "deploy";
|
|
2192
2635
|
ok(`${"Account".padEnd(16)}${c.dim(`${account.name} ${account.id}`)}`);
|
|
2193
2636
|
const sub = await cf("/workers/subdomain").catch(() => ({ subdomain: void 0 }));
|
|
2194
2637
|
if (!sub.subdomain && !opts.url) {
|
|
@@ -2223,10 +2666,10 @@ async function init(opts) {
|
|
|
2223
2666
|
},
|
|
2224
2667
|
(r) => `${r.reused ? "reusing" : "created"} ${name}`
|
|
2225
2668
|
).then((r) => r.id);
|
|
2226
|
-
const cfgPath = writeDeployFiles(name, d1Id, account.id);
|
|
2669
|
+
const cfgPath = writeDeployFiles(name, d1Id, account.id, deployKey);
|
|
2227
2670
|
await task(
|
|
2228
2671
|
"Schema",
|
|
2229
|
-
() => wrangler(["d1", "execute", name, "--remote", "--yes", "--file",
|
|
2672
|
+
() => wrangler(["d1", "execute", name, "--remote", "--yes", "--file", join5(deployDir(), "schema.sql"), "--config", cfgPath], { capture: true, quiet: true }),
|
|
2230
2673
|
() => "applied"
|
|
2231
2674
|
);
|
|
2232
2675
|
const url = await task(
|
|
@@ -2255,7 +2698,7 @@ async function init(opts) {
|
|
|
2255
2698
|
async () => {
|
|
2256
2699
|
const hash = createHash3("sha256").update(adminToken).digest("hex");
|
|
2257
2700
|
await wrangler(["secret", "put", "KEYBOX_ADMIN_TOKEN_HASH", "--config", cfgPath], { input: hash, capture: true, quiet: true });
|
|
2258
|
-
saveConfig({ url, adminToken, name, d1Id, accountId: account.id });
|
|
2701
|
+
saveConfig({ url, adminToken, name, d1Id, accountId: account.id, deploy: deployKey }, { as: opts.as });
|
|
2259
2702
|
},
|
|
2260
2703
|
() => `saved to ${configPath().replace(process.env.HOME ?? "~", "~")} (mode 600)`
|
|
2261
2704
|
);
|
|
@@ -2302,12 +2745,12 @@ ${c.accent("\u25A0")} Next
|
|
|
2302
2745
|
}
|
|
2303
2746
|
async function deploy(opts) {
|
|
2304
2747
|
const cfg = requireConfig();
|
|
2305
|
-
if (!cfg
|
|
2748
|
+
if (!isOwnDeployment(cfg)) throw new UserError(`\`keybox deploy\` replaces a Worker in your own Cloudflare account. ${cfg.url} is run by someone else (for a hosted team, the operator updates it).`);
|
|
2306
2749
|
await resolveAccount(cfg);
|
|
2307
2750
|
const dir = deployDir();
|
|
2308
|
-
const
|
|
2751
|
+
const current2 = existsSync4(join5(dir, "worker", "worker.js")) ? sha(join5(dir, "worker", "worker.js")) : null;
|
|
2309
2752
|
const next = shippedWorkerHash();
|
|
2310
|
-
if (
|
|
2753
|
+
if (current2 === next && !opts.force) {
|
|
2311
2754
|
ok("The deployed Worker already matches this CLI version. Run `keybox verify` to check what Cloudflare is serving.");
|
|
2312
2755
|
return;
|
|
2313
2756
|
}
|
|
@@ -2315,17 +2758,17 @@ async function deploy(opts) {
|
|
|
2315
2758
|
${c.accent("\u25A0")} This updates the Worker's code. Your stored keys, agents and logs are not touched.
|
|
2316
2759
|
They live in your D1 database and under your encryption key, and a deploy changes neither.
|
|
2317
2760
|
The only thing that changes is the code that runs.
|
|
2318
|
-
deployed ${c.dim(
|
|
2761
|
+
deployed ${c.dim(current2 ?? "(unknown)")}
|
|
2319
2762
|
new ${next} ${c.dim(`@keyboxed/cli ${VERSION}`)}
|
|
2320
2763
|
The new code comes from this copy of @keyboxed/cli. Only continue if you trust it.
|
|
2321
2764
|
|
|
2322
2765
|
`);
|
|
2323
2766
|
if (!opts.yes && !await confirm("Deploy?")) throw new UserError("Cancelled.");
|
|
2324
2767
|
const fresh = requireConfig();
|
|
2325
|
-
const wranglerCfg = writeDeployFiles(fresh.name, fresh.d1Id, fresh.accountId);
|
|
2768
|
+
const wranglerCfg = writeDeployFiles(fresh.name, fresh.d1Id, fresh.accountId, fresh.deploy);
|
|
2326
2769
|
await task(
|
|
2327
2770
|
"Schema",
|
|
2328
|
-
() => wrangler(["d1", "execute", fresh.name, "--remote", "--yes", "--file",
|
|
2771
|
+
() => wrangler(["d1", "execute", fresh.name, "--remote", "--yes", "--file", join5(dir, "schema.sql"), "--config", wranglerCfg], { capture: true, quiet: true }),
|
|
2329
2772
|
() => "applied"
|
|
2330
2773
|
);
|
|
2331
2774
|
await task("Worker", () => deployWorker(wranglerCfg), () => "deployed");
|
|
@@ -4195,20 +4638,24 @@ __export(extra_exports, {
|
|
|
4195
4638
|
membersList: () => membersList,
|
|
4196
4639
|
membersLog: () => membersLog,
|
|
4197
4640
|
membersRm: () => membersRm,
|
|
4641
|
+
membersRole: () => membersRole,
|
|
4198
4642
|
pair: () => pair,
|
|
4643
|
+
profilesList: () => profilesList,
|
|
4644
|
+
profilesRm: () => profilesRm,
|
|
4199
4645
|
qrBlocks: () => qrBlocks,
|
|
4200
4646
|
skill: () => skill,
|
|
4201
4647
|
tokenOff: () => tokenOff,
|
|
4202
|
-
ui: () => ui
|
|
4648
|
+
ui: () => ui,
|
|
4649
|
+
use: () => use
|
|
4203
4650
|
});
|
|
4204
4651
|
import { spawn as spawn5 } from "node:child_process";
|
|
4205
|
-
import { existsSync as
|
|
4652
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync3 } from "node:fs";
|
|
4206
4653
|
import { homedir as homedir3 } from "node:os";
|
|
4207
|
-
import { dirname as
|
|
4654
|
+
import { dirname as dirname3, join as join6 } from "node:path";
|
|
4208
4655
|
async function login(urlArg, opts = {}) {
|
|
4209
4656
|
const url = checkUrl(urlArg ?? await ask("Keybox address (https://\u2026)"));
|
|
4210
4657
|
const code = (opts.code ?? process.env.KEYBOX_JOIN_CODE ?? "").trim();
|
|
4211
|
-
if (code) return joinWithCode(url, code);
|
|
4658
|
+
if (code) return joinWithCode(url, code, opts.as);
|
|
4212
4659
|
const token2 = (process.env.KEYBOX_ADMIN_TOKEN ?? await readSecret("Admin token")).trim();
|
|
4213
4660
|
if (!token2.startsWith("kb_adm_")) throw new UserError("That does not look like an admin token (they start with kb_adm_).");
|
|
4214
4661
|
let res;
|
|
@@ -4219,11 +4666,12 @@ async function login(urlArg, opts = {}) {
|
|
|
4219
4666
|
}
|
|
4220
4667
|
if (res.status === 401 || res.status === 403) throw new UserError("That token was not accepted by this Keybox.");
|
|
4221
4668
|
if (!res.ok) throw new UserError(`${url} answered HTTP ${res.status}. Is that a Keybox?`);
|
|
4222
|
-
saveConfig({ url, adminToken: token2, name: "", d1Id: "" });
|
|
4223
|
-
ok(`Connected to ${url}. The token is saved in ~/.config/keybox (mode 600).`);
|
|
4669
|
+
const profile = saveConfig({ url, adminToken: token2, name: "", d1Id: "" }, { as: opts.as });
|
|
4670
|
+
ok(`Connected to ${url} as ${c.bold(profile)}. The token is saved in ~/.config/keybox (mode 600).`);
|
|
4671
|
+
afterConnect(profile);
|
|
4224
4672
|
note("Next: `keybox claim` gives this machine its own key, so the shared token can be turned off.");
|
|
4225
4673
|
}
|
|
4226
|
-
async function joinWithCode(url, code) {
|
|
4674
|
+
async function joinWithCode(url, code, as) {
|
|
4227
4675
|
const kind = /^kb_(pair|inv|clm)_[A-Za-z0-9_-]{20,}$/.exec(code)?.[1];
|
|
4228
4676
|
if (!kind) throw new UserError("That does not look like a join code (they start with kb_pair_, kb_inv_ or kb_clm_).");
|
|
4229
4677
|
const memberName = kind === "clm" && process.stdin.isTTY ? await ask("Your name", "owner") : void 0;
|
|
@@ -4240,16 +4688,24 @@ async function joinWithCode(url, code) {
|
|
|
4240
4688
|
}
|
|
4241
4689
|
const data = await res.json().catch(() => null);
|
|
4242
4690
|
if (!res.ok || !data?.device) throw new UserError(data?.error?.message ?? `${url} answered HTTP ${res.status}. Is that a Keybox?`);
|
|
4243
|
-
saveConfig({ url, deviceId: data.device, deviceKey: key.privateKey, name: "", d1Id: "" });
|
|
4691
|
+
const profile = saveConfig({ url, deviceId: data.device, deviceKey: key.privateKey, name: "", d1Id: "" }, { as });
|
|
4244
4692
|
if (data.pending) {
|
|
4245
|
-
ok(`Joined ${url} as ${c.bold(deviceName())}. This machine's key never left it.`);
|
|
4693
|
+
ok(`Joined ${url} as ${c.bold(deviceName())}, saved as ${c.bold(profile)}. This machine's key never left it.`);
|
|
4694
|
+
afterConnect(profile);
|
|
4246
4695
|
warn(`Waiting for approval. Ask an owner or admin to run: keybox devices approve ${data.device}`);
|
|
4247
4696
|
note("Until then every command here will say the machine is waiting.");
|
|
4248
4697
|
return;
|
|
4249
4698
|
}
|
|
4250
|
-
ok(`Connected to ${url} as ${c.bold(deviceName())}. This machine's key never left it.`);
|
|
4699
|
+
ok(`Connected to ${url} as ${c.bold(deviceName())}, saved as ${c.bold(profile)}. This machine's key never left it.`);
|
|
4700
|
+
afterConnect(profile);
|
|
4251
4701
|
note("Try `keybox ui` for the dashboard, or `keybox secrets add NAME`.");
|
|
4252
4702
|
}
|
|
4703
|
+
function afterConnect(profile) {
|
|
4704
|
+
const { store } = listProfiles();
|
|
4705
|
+
const others = Object.keys(store.profiles).filter((n) => n !== profile);
|
|
4706
|
+
if (!others.length) return;
|
|
4707
|
+
note(`${c.bold(profile)} is now the default. \`keybox use ${others[0]}\` switches back; \`--profile NAME\` picks one for a single command.`);
|
|
4708
|
+
}
|
|
4253
4709
|
async function claim() {
|
|
4254
4710
|
const cfg = requireConfig();
|
|
4255
4711
|
if (cfg.deviceId) throw new UserError("This machine already has a device key. See `keybox devices`.");
|
|
@@ -4314,6 +4770,52 @@ async function tokenOff(opts) {
|
|
|
4314
4770
|
ok("The shared admin token is off, and removed from this machine.");
|
|
4315
4771
|
note("To bring it back (break-glass), set the Worker secret KEYBOX_ALLOW_ADMIN_TOKEN to 1.");
|
|
4316
4772
|
}
|
|
4773
|
+
function profilesList() {
|
|
4774
|
+
const { store, resolved } = listProfiles();
|
|
4775
|
+
const names = Object.keys(store.profiles).sort();
|
|
4776
|
+
if (!names.length) return note("No connections yet. `keybox init` deploys a Keybox, `keybox login URL` connects to one.");
|
|
4777
|
+
process.stdout.write(
|
|
4778
|
+
headed([
|
|
4779
|
+
["", "NAME", "ADDRESS", "SIGNED IN WITH", "RUNS ON"],
|
|
4780
|
+
...names.map((n) => {
|
|
4781
|
+
const p = store.profiles[n];
|
|
4782
|
+
return [n === resolved?.name ? c.accent("\u2192") : "", n, p.url, signedInWith(p), isOwnDeployment(p) ? "your Cloudflare account" : "someone else"];
|
|
4783
|
+
})
|
|
4784
|
+
])
|
|
4785
|
+
);
|
|
4786
|
+
if (resolved?.source === "project") note(`This folder pins ${resolved.name} (${resolved.file}).`);
|
|
4787
|
+
else if (resolved?.source === "flag" || resolved?.source === "env") note(`${resolved.name} is chosen for this command by ${resolved.source === "flag" ? "--profile" : "KEYBOX_PROFILE"}.`);
|
|
4788
|
+
else note("\u2192 is the default. `keybox use NAME` changes it, `keybox use --here` pins this folder, `--profile NAME` picks one for one command.");
|
|
4789
|
+
}
|
|
4790
|
+
function use(name, opts) {
|
|
4791
|
+
const { store, resolved } = listProfiles();
|
|
4792
|
+
const pick = name ?? (opts.here ? resolved?.name : void 0);
|
|
4793
|
+
if (!pick || !store.profiles[pick]) {
|
|
4794
|
+
if (pick) throw new UserError(`No connection called ${pick}. See \`keybox profiles\`.`);
|
|
4795
|
+
throw new UserError("Usage: keybox use NAME (or keybox use --here NAME to pin this folder)");
|
|
4796
|
+
}
|
|
4797
|
+
const p = store.profiles[pick];
|
|
4798
|
+
if (opts.here) {
|
|
4799
|
+
const file = pinFolder(process.cwd(), p.url);
|
|
4800
|
+
ok(`This folder uses ${c.bold(pick)} (${p.url}).`);
|
|
4801
|
+
note(`Written to ${file}. It holds only the address, so commit it and everyone who is connected to that Keybox gets the same one here. It can never connect anyone to a new Keybox.`);
|
|
4802
|
+
return;
|
|
4803
|
+
}
|
|
4804
|
+
setCurrent(pick);
|
|
4805
|
+
ok(`Using ${c.bold(pick)} (${p.url}) everywhere except folders that pin another.`);
|
|
4806
|
+
}
|
|
4807
|
+
async function profilesRm(name, yes) {
|
|
4808
|
+
if (!name) throw new UserError("Usage: keybox profiles rm NAME");
|
|
4809
|
+
const { store } = listProfiles();
|
|
4810
|
+
const p = store.profiles[name];
|
|
4811
|
+
if (!p) throw new UserError(`No connection called ${name}. See \`keybox profiles\`.`);
|
|
4812
|
+
if (!yes) {
|
|
4813
|
+
warn(`This forgets ${p.url} on this machine. The ${p.deviceKey ? "device key" : "admin token"} saved for it may exist nowhere else. The Keybox itself is not touched.`);
|
|
4814
|
+
if (!process.stdin.isTTY || !await confirm(`Forget ${name}?`, false)) throw new UserError("Nothing changed. Use --yes to forget it without asking.");
|
|
4815
|
+
}
|
|
4816
|
+
removeProfile(name);
|
|
4817
|
+
ok(`Forgot ${name}.`);
|
|
4818
|
+
}
|
|
4317
4819
|
function openBrowser(url) {
|
|
4318
4820
|
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open";
|
|
4319
4821
|
const args = process.platform === "win32" ? ["/c", "start", '""', url] : [url];
|
|
@@ -4444,16 +4946,16 @@ async function skill(opts) {
|
|
|
4444
4946
|
if (opts.print) return void process.stdout.write(text);
|
|
4445
4947
|
const base = opts.project ? process.cwd() : homedir3();
|
|
4446
4948
|
const targets = {
|
|
4447
|
-
claude:
|
|
4448
|
-
codex:
|
|
4449
|
-
agents:
|
|
4949
|
+
claude: join6(base, ".claude", "skills", "keybox", "SKILL.md"),
|
|
4950
|
+
codex: join6(opts.project ? join6(base, ".codex") : join6(homedir3(), ".codex"), "skills", "keybox", "SKILL.md"),
|
|
4951
|
+
agents: join6(base, ".agents", "skills", "keybox", "SKILL.md")
|
|
4450
4952
|
};
|
|
4451
4953
|
const pick = opts.target === "all" ? Object.keys(targets) : opts.target.split(",");
|
|
4452
4954
|
for (const t of pick) {
|
|
4453
4955
|
const path = targets[t];
|
|
4454
4956
|
if (!path) throw new UserError(`Unknown target ${t}. Use claude, codex, agents or all (or --print).`);
|
|
4455
|
-
const existed =
|
|
4456
|
-
mkdirSync3(
|
|
4957
|
+
const existed = existsSync5(path);
|
|
4958
|
+
mkdirSync3(dirname3(path), { recursive: true });
|
|
4457
4959
|
writeFileSync3(path, text);
|
|
4458
4960
|
ok(`${existed ? "Updated" : "Installed"} ${path}`);
|
|
4459
4961
|
}
|
|
@@ -4480,6 +4982,15 @@ async function membersAdd(name, opts) {
|
|
|
4480
4982
|
`);
|
|
4481
4983
|
note("Valid 24 hours, works once. Their machine then waits for you: `keybox devices` shows it, `keybox devices approve ID` lets it in.");
|
|
4482
4984
|
}
|
|
4985
|
+
async function membersRole(who, role) {
|
|
4986
|
+
if (role !== "admin" && role !== "member") throw new UserError("Usage: keybox members role NAME admin|member");
|
|
4987
|
+
const list2 = await api("GET", "/members");
|
|
4988
|
+
const m = list2.find((x) => x.id === who || x.name.toLowerCase() === who.toLowerCase());
|
|
4989
|
+
if (!m) throw new UserError(`No member called ${who}. See \`keybox members\`.`);
|
|
4990
|
+
await api("PATCH", `/members/${encodeURIComponent(m.id)}`, { role });
|
|
4991
|
+
ok(`${m.name} is now ${role === "admin" ? "an admin: can add and change keys and policy" : "a member: can run their own agents and tokens, not change keys"}.`);
|
|
4992
|
+
note("Their open dashboard sessions ended; they sign in again with `keybox ui`.");
|
|
4993
|
+
}
|
|
4483
4994
|
async function membersRm(who) {
|
|
4484
4995
|
const list2 = await api("GET", "/members");
|
|
4485
4996
|
const m = list2.find((x) => x.id === who || x.name.toLowerCase() === who.toLowerCase());
|
|
@@ -4497,7 +5008,7 @@ async function membersLog(limit) {
|
|
|
4497
5008
|
])
|
|
4498
5009
|
);
|
|
4499
5010
|
}
|
|
4500
|
-
var import_qrcode, checkUrl, ago2, describe, skillAsset;
|
|
5011
|
+
var import_qrcode, checkUrl, ago2, signedInWith, describe, skillAsset;
|
|
4501
5012
|
var init_extra = __esm({
|
|
4502
5013
|
"src/extra.ts"() {
|
|
4503
5014
|
"use strict";
|
|
@@ -4517,6 +5028,7 @@ var init_extra = __esm({
|
|
|
4517
5028
|
const s = Math.round((Date.now() - ms) / 1e3);
|
|
4518
5029
|
return s < 60 ? "just now" : s < 3600 ? `${Math.round(s / 60)}m ago` : s < 86400 ? `${Math.round(s / 3600)}h ago` : `${Math.round(s / 86400)}d ago`;
|
|
4519
5030
|
};
|
|
5031
|
+
signedInWith = (p) => p.deviceKey ? "device key" : p.adminToken ? "admin token" : c.dim("nothing saved");
|
|
4520
5032
|
describe = (r) => {
|
|
4521
5033
|
if (!r || !Object.keys(r).length) return c.dim("any network");
|
|
4522
5034
|
const parts2 = [];
|
|
@@ -4620,8 +5132,8 @@ init_config();
|
|
|
4620
5132
|
init_dove();
|
|
4621
5133
|
init_ui();
|
|
4622
5134
|
import { spawn as spawn6 } from "node:child_process";
|
|
4623
|
-
import { existsSync as
|
|
4624
|
-
import { delimiter, join as
|
|
5135
|
+
import { existsSync as existsSync6 } from "node:fs";
|
|
5136
|
+
import { delimiter as delimiter2, join as join7 } from "node:path";
|
|
4625
5137
|
var TABS = ["Activity", "Secrets", "Agents"];
|
|
4626
5138
|
var out = process.stdout;
|
|
4627
5139
|
var ago3 = (ms) => {
|
|
@@ -4647,11 +5159,11 @@ function fit(s, n) {
|
|
|
4647
5159
|
}
|
|
4648
5160
|
function nativeTui() {
|
|
4649
5161
|
const explicit = process.env.KEYBOX_TUI;
|
|
4650
|
-
if (explicit &&
|
|
4651
|
-
for (const dir of (process.env.PATH ?? "").split(
|
|
5162
|
+
if (explicit && existsSync6(explicit)) return explicit;
|
|
5163
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter2)) {
|
|
4652
5164
|
for (const name of process.platform === "win32" ? ["keybox-tui.exe"] : ["keybox-tui"]) {
|
|
4653
|
-
const p =
|
|
4654
|
-
if (dir &&
|
|
5165
|
+
const p = join7(dir, name);
|
|
5166
|
+
if (dir && existsSync6(p)) return p;
|
|
4655
5167
|
}
|
|
4656
5168
|
}
|
|
4657
5169
|
return null;
|
|
@@ -4661,7 +5173,7 @@ async function tui(opts = {}) {
|
|
|
4661
5173
|
const native = opts.basic || !requireConfig().adminToken ? null : nativeTui();
|
|
4662
5174
|
if (native) {
|
|
4663
5175
|
const code = await new Promise((resolve) => {
|
|
4664
|
-
const child = spawn6(native, [], { stdio: "inherit" });
|
|
5176
|
+
const child = spawn6(native, [], { stdio: "inherit", env: { ...process.env, KEYBOX_PROFILE: resolveProfile()?.name ?? "" } });
|
|
4665
5177
|
child.on("error", () => resolve(-1));
|
|
4666
5178
|
child.on("close", (c2) => resolve(c2 ?? 1));
|
|
4667
5179
|
});
|
|
@@ -4846,16 +5358,23 @@ init_prompt();
|
|
|
4846
5358
|
init_ui();
|
|
4847
5359
|
init_verify();
|
|
4848
5360
|
init_version();
|
|
4849
|
-
import { spawn as spawn7, spawnSync as
|
|
4850
|
-
import { existsSync as
|
|
4851
|
-
import { dirname as
|
|
5361
|
+
import { spawn as spawn7, spawnSync as spawnSync3 } from "node:child_process";
|
|
5362
|
+
import { existsSync as existsSync7, realpathSync as realpathSync2 } from "node:fs";
|
|
5363
|
+
import { dirname as dirname4, join as join8 } from "node:path";
|
|
4852
5364
|
import { fileURLToPath } from "node:url";
|
|
5365
|
+
function npmPrefix(self, platform = process.platform) {
|
|
5366
|
+
if (platform === "win32") return void 0;
|
|
5367
|
+
const i = self.lastIndexOf("/node_modules/@keyboxed/cli/");
|
|
5368
|
+
if (i < 0) return void 0;
|
|
5369
|
+
const lib = self.slice(0, i);
|
|
5370
|
+
return lib.endsWith("/lib") ? lib.slice(0, -4) || "/" : void 0;
|
|
5371
|
+
}
|
|
4853
5372
|
function howInstalled() {
|
|
4854
|
-
const self =
|
|
4855
|
-
if (self.includes(`${
|
|
4856
|
-
const root =
|
|
4857
|
-
if (
|
|
4858
|
-
if (self.includes(
|
|
5373
|
+
const self = realpathSync2(fileURLToPath(import.meta.url));
|
|
5374
|
+
if (self.includes(`${join8("_npx", "")}`) || self.includes("/_npx/")) return { kind: "npx" };
|
|
5375
|
+
const root = dirname4(dirname4(dirname4(self)));
|
|
5376
|
+
if (existsSync7(join8(root, ".git")) && existsSync7(join8(root, "cli", "package.json"))) return { kind: "checkout", root };
|
|
5377
|
+
if (self.includes(join8("node_modules", "@keyboxed", "cli"))) return { kind: "npm-global", prefix: npmPrefix(self) };
|
|
4859
5378
|
return { kind: "unknown", path: self };
|
|
4860
5379
|
}
|
|
4861
5380
|
function newer(a, b) {
|
|
@@ -4891,7 +5410,7 @@ async function update(opts) {
|
|
|
4891
5410
|
let cliUpdated = false;
|
|
4892
5411
|
if (install.kind === "checkout") {
|
|
4893
5412
|
note(`Running from a git checkout (${install.root}).`);
|
|
4894
|
-
const dirty =
|
|
5413
|
+
const dirty = spawnSync3("git", ["status", "--porcelain"], { cwd: install.root, encoding: "utf8" }).stdout.trim();
|
|
4895
5414
|
if (opts.check) note("Update it with: git pull && npm install && npm run build");
|
|
4896
5415
|
else if (dirty) warn("The checkout has local changes; skipping git pull. Update it yourself, then run keybox update again.");
|
|
4897
5416
|
else if (opts.yes || await confirm("Pull and rebuild the checkout?", true)) {
|
|
@@ -4907,7 +5426,8 @@ async function update(opts) {
|
|
|
4907
5426
|
else if (install.kind === "npx") note(`You run Keybox through npx. Use npx @keyboxed/cli@${v} \u2026 from now on.`);
|
|
4908
5427
|
else if (install.kind === "unknown") note(`Installed somewhere unusual (${install.path}). Install ${v} the way you installed this one.`);
|
|
4909
5428
|
else if (opts.yes || await confirm(`Install @keyboxed/cli ${v}?`, true)) {
|
|
4910
|
-
const
|
|
5429
|
+
const prefix = install.kind === "npm-global" ? install.prefix : void 0;
|
|
5430
|
+
const args = ["install", "-g", "--prefer-online", ...prefix ? ["--prefix", prefix] : [], `@keyboxed/cli@${v}`];
|
|
4911
5431
|
let code = await run2("npm", args);
|
|
4912
5432
|
if (code !== 0) {
|
|
4913
5433
|
note("Retrying in 20 seconds in case the release has not reached every npm mirror yet\u2026");
|
|
@@ -4921,7 +5441,7 @@ async function update(opts) {
|
|
|
4921
5441
|
}
|
|
4922
5442
|
if (cliUpdated) {
|
|
4923
5443
|
ok("CLI updated. Continuing with the new version.");
|
|
4924
|
-
const bin2 = install.kind === "checkout" ? [process.execPath,
|
|
5444
|
+
const bin2 = install.kind === "checkout" ? [process.execPath, join8(install.root, "cli", "dist", "keybox.js")] : ["keybox"];
|
|
4925
5445
|
const code = await run2(bin2[0], [...bin2.slice(1), "update", "--worker-only", ...opts.yes ? ["--yes"] : []]);
|
|
4926
5446
|
process.exitCode = code;
|
|
4927
5447
|
return;
|
|
@@ -4931,6 +5451,7 @@ async function update(opts) {
|
|
|
4931
5451
|
async function updateWorker(opts) {
|
|
4932
5452
|
const cfg = loadConfig();
|
|
4933
5453
|
if (!cfg) return note("No Keybox set up on this machine yet. Run `keybox init`.");
|
|
5454
|
+
if (!isOwnDeployment(cfg)) return note(`${cfg.url} is run by someone else, so there is no Worker of yours to update (for a hosted team, the operator updates it). \`keybox profiles\` lists the others.`);
|
|
4934
5455
|
let deployedVersion = "?";
|
|
4935
5456
|
try {
|
|
4936
5457
|
deployedVersion = (await (await fetch(`${cfg.url}/_keybox/health`)).json()).version;
|
|
@@ -4954,10 +5475,15 @@ init_verify();
|
|
|
4954
5475
|
init_ui();
|
|
4955
5476
|
init_version();
|
|
4956
5477
|
var HELP_BODY = `Setup
|
|
4957
|
-
keybox init [--account ID] [--name keybox]
|
|
4958
|
-
|
|
5478
|
+
keybox init [--account ID] [--name keybox] [--as NAME]
|
|
5479
|
+
Deploy Keybox to your Cloudflare account
|
|
5480
|
+
keybox login [URL] [--code CODE] [--as NAME]
|
|
5481
|
+
Connect to a Keybox someone else runs (a hosted team)
|
|
5482
|
+
keybox profiles [rm NAME] The Keyboxes this machine knows; \u2192 marks the default
|
|
5483
|
+
keybox use NAME | --here [NAME] Switch the default, or pin this folder to one (.keybox.json)
|
|
5484
|
+
(any command) --profile NAME Use another Keybox for just that command
|
|
4959
5485
|
keybox claim Give this machine its own key (becomes the owner)
|
|
4960
|
-
keybox members [add NAME [--role member|admin] | rm NAME | log]
|
|
5486
|
+
keybox members [add NAME [--role member|admin] | role NAME admin|member | rm NAME | log]
|
|
4961
5487
|
People on this Keybox: invite, remove, and who changed what
|
|
4962
5488
|
keybox devices [list|add|approve ID|revoke ID]
|
|
4963
5489
|
Machines that can manage this Keybox; add one with a one-time code
|
|
@@ -4984,6 +5510,10 @@ Moving keys in (values are never printed, logged or put on a command line)
|
|
|
4984
5510
|
|
|
4985
5511
|
Agents
|
|
4986
5512
|
keybox agents create NAME --allow "GET api.github.com/repos/**" [--allow \u2026] [--deny \u2026]
|
|
5513
|
+
keybox agents allow NAME RULE [RULE \u2026] [--deny RULE]
|
|
5514
|
+
Add rules to an agent; keeps the ones it has
|
|
5515
|
+
keybox agents deny NAME RULE [RULE \u2026] Add deny rules to an agent
|
|
5516
|
+
keybox agents suggest NAME [--yes] Turn the calls it was refused into tight rules to pick from
|
|
4987
5517
|
keybox agents list | rm NAME
|
|
4988
5518
|
keybox agents token NAME [--ttl 30d] Long-lived token for a deployed agent
|
|
4989
5519
|
keybox tokens revoke ID
|
|
@@ -5001,7 +5531,8 @@ Sharing (access to a key, never the key)
|
|
|
5001
5531
|
keybox share list | rm ID
|
|
5002
5532
|
|
|
5003
5533
|
Running
|
|
5004
|
-
keybox run AGENT [--ttl 12h] [--clean-env] -- command [args]
|
|
5534
|
+
keybox run AGENT [--ttl 12h] [--clean-env] [--no-isolate] -- command [args]
|
|
5535
|
+
Hides this machine's Keybox credentials from the command
|
|
5005
5536
|
keybox audit [--follow] [--limit 50]
|
|
5006
5537
|
keybox blocked Calls Keybox never proxies
|
|
5007
5538
|
|
|
@@ -5038,6 +5569,7 @@ async function main(argv) {
|
|
|
5038
5569
|
deny: { type: "string", multiple: true },
|
|
5039
5570
|
ttl: { type: "string" },
|
|
5040
5571
|
"clean-env": { type: "boolean", default: false },
|
|
5572
|
+
"no-isolate": { type: "boolean", default: false },
|
|
5041
5573
|
from: { type: "string" },
|
|
5042
5574
|
only: { type: "string" },
|
|
5043
5575
|
vault: { type: "string" },
|
|
@@ -5073,6 +5605,9 @@ async function main(argv) {
|
|
|
5073
5605
|
limit: { type: "string", default: "50" },
|
|
5074
5606
|
code: { type: "string" },
|
|
5075
5607
|
role: { type: "string" },
|
|
5608
|
+
profile: { type: "string", short: "p" },
|
|
5609
|
+
as: { type: "string" },
|
|
5610
|
+
here: { type: "boolean", default: false },
|
|
5076
5611
|
help: { type: "boolean", short: "h", default: false }
|
|
5077
5612
|
}
|
|
5078
5613
|
});
|
|
@@ -5082,11 +5617,12 @@ async function main(argv) {
|
|
|
5082
5617
|
return v;
|
|
5083
5618
|
};
|
|
5084
5619
|
if (o.version) return void process.stdout.write(VERSION + "\n");
|
|
5620
|
+
useProfile(o.profile);
|
|
5085
5621
|
if (o.verbose) process.env.KEYBOX_VERBOSE = "1";
|
|
5086
5622
|
if (o.help || !group) return void process.stdout.write(help());
|
|
5087
|
-
switch (`${group} ${["secrets", "agents", "tokens", "access", "skill", "devices", "token", "members"].includes(group) ? sub ?? "" : ""}`.trim()) {
|
|
5623
|
+
switch (`${group} ${["secrets", "agents", "tokens", "access", "skill", "devices", "token", "members", "profiles"].includes(group) ? sub ?? "" : ""}`.trim()) {
|
|
5088
5624
|
case "init":
|
|
5089
|
-
return init({ name: o.name, url: o.url, account: o.account, yes: o.yes });
|
|
5625
|
+
return init({ name: o.name, url: o.url, account: o.account, yes: o.yes, as: o.as });
|
|
5090
5626
|
case "verify":
|
|
5091
5627
|
return verify({ json: o.json });
|
|
5092
5628
|
case "update":
|
|
@@ -5094,7 +5630,14 @@ async function main(argv) {
|
|
|
5094
5630
|
case "deploy":
|
|
5095
5631
|
return deploy({ yes: o.yes, force: o.force });
|
|
5096
5632
|
case "login":
|
|
5097
|
-
return login(sub, { code: o.code });
|
|
5633
|
+
return login(sub, { code: o.code, as: o.as });
|
|
5634
|
+
case "profiles":
|
|
5635
|
+
case "profiles list":
|
|
5636
|
+
return profilesList();
|
|
5637
|
+
case "profiles rm":
|
|
5638
|
+
return profilesRm(arg, o.yes);
|
|
5639
|
+
case "use":
|
|
5640
|
+
return use(sub, { here: o.here });
|
|
5098
5641
|
case "claim":
|
|
5099
5642
|
return claim();
|
|
5100
5643
|
case "devices":
|
|
@@ -5109,6 +5652,8 @@ async function main(argv) {
|
|
|
5109
5652
|
return membersList();
|
|
5110
5653
|
case "members add":
|
|
5111
5654
|
return membersAdd(need(arg, "name"), { role: o.role });
|
|
5655
|
+
case "members role":
|
|
5656
|
+
return membersRole(need(arg, "name"), p[3]);
|
|
5112
5657
|
case "members rm":
|
|
5113
5658
|
return membersRm(need(arg, "name or id"));
|
|
5114
5659
|
case "members log":
|
|
@@ -5151,6 +5696,12 @@ async function main(argv) {
|
|
|
5151
5696
|
return secretsRm(need(arg, "secret name"));
|
|
5152
5697
|
case "agents create":
|
|
5153
5698
|
return agentsCreate(need(arg, "agent name"), { allow: o.allow, deny: o.deny });
|
|
5699
|
+
case "agents allow":
|
|
5700
|
+
return agentsAdd(need(arg, "agent name"), { allow: [...p.slice(3), ...o.allow ?? []], deny: o.deny });
|
|
5701
|
+
case "agents deny":
|
|
5702
|
+
return agentsAdd(need(arg, "agent name"), { deny: [...p.slice(3), ...o.deny ?? []] });
|
|
5703
|
+
case "agents suggest":
|
|
5704
|
+
return agentsSuggest(need(arg, "agent name"), { yes: o.yes, limit: o.limit === "50" ? "500" : o.limit });
|
|
5154
5705
|
case "agents list":
|
|
5155
5706
|
return agentsList();
|
|
5156
5707
|
case "agents rm":
|
|
@@ -5164,7 +5715,7 @@ async function main(argv) {
|
|
|
5164
5715
|
if (sub === "rm" || sub === "revoke") return shareRm(need(arg, "share id"));
|
|
5165
5716
|
return shareCreate(sub, { allow: o.allow, deny: o.deny, uses: o.uses, ip: o.ip, country: o.country, expires: o.expires, label: o.label });
|
|
5166
5717
|
case "run":
|
|
5167
|
-
return run(need(sub, "agent name"), rest, { ttl: o.ttl ?? "12h", cleanEnv: o["clean-env"] });
|
|
5718
|
+
return run(need(sub, "agent name"), rest, { ttl: o.ttl ?? "12h", cleanEnv: o["clean-env"], isolate: !o["no-isolate"] });
|
|
5168
5719
|
case "audit":
|
|
5169
5720
|
return audit({ follow: o.follow, limit: o.limit });
|
|
5170
5721
|
case "blocked":
|