@orgsdk/cli 2.1.0 → 2.3.0
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/bin/options.mjs +10 -3
- package/bin/orgsdk.mjs +68 -12
- package/bin/supervision.mjs +9 -0
- package/dist/launcher/updates.js +264 -0
- package/dist/runtime/client.js +19 -0
- package/dist/runtime/contract.js +1 -0
- package/dist/runtime/plugins.js +16 -4
- package/dist/runtime/runner.js +16 -13
- package/dist/runtime/shell-policy.js +31 -0
- package/dist/server/{bun-driver-DTZUUCI3.js → bun-driver-UOZIWBHK.js} +1 -1
- package/dist/server/{chunk-S5SFX3JM.js → chunk-EXZNZYIJ.js} +13 -0
- package/dist/server/index.js +1337 -950
- package/dist/server/migrations/0004_shell_executable_policy.sql +9 -0
- package/dist/server/migrations/meta/0004_snapshot.json +3455 -0
- package/dist/server/migrations/meta/_journal.json +7 -0
- package/dist/server/{node-driver-HDJKKWXQ.js → node-driver-75PFAG2L.js} +1 -1
- package/dist/server/out/_next/static/chunks/0tlox6w8yf345.css +1 -0
- package/dist/server/out/accounts/2fa/__next._full.txt +2 -2
- package/dist/server/out/accounts/2fa/__next._tree.txt +2 -2
- package/dist/server/out/accounts/2fa/__next.accounts.2fa.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/2fa/index.html +1 -1
- package/dist/server/out/accounts/2fa/index.txt +2 -2
- package/dist/server/out/accounts/email/__next._full.txt +2 -2
- package/dist/server/out/accounts/email/__next._tree.txt +2 -2
- package/dist/server/out/accounts/email/__next.accounts.email.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/email/index.html +1 -1
- package/dist/server/out/accounts/email/index.txt +2 -2
- package/dist/server/out/accounts/login/__next._full.txt +2 -2
- package/dist/server/out/accounts/login/__next._tree.txt +2 -2
- package/dist/server/out/accounts/login/__next.accounts.login.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/login/index.html +1 -1
- package/dist/server/out/accounts/login/index.txt +2 -2
- package/dist/server/out/accounts/logout/__next._full.txt +2 -2
- package/dist/server/out/accounts/logout/__next._tree.txt +2 -2
- package/dist/server/out/accounts/logout/__next.accounts.logout.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/logout/index.html +1 -1
- package/dist/server/out/accounts/logout/index.txt +2 -2
- package/dist/server/out/accounts/password/change/__next._full.txt +2 -2
- package/dist/server/out/accounts/password/change/__next._tree.txt +2 -2
- package/dist/server/out/accounts/password/change/__next.accounts.password.change.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/password/change/index.html +1 -1
- package/dist/server/out/accounts/password/change/index.txt +2 -2
- package/dist/server/out/accounts/password/reset/__next._full.txt +2 -2
- package/dist/server/out/accounts/password/reset/__next._tree.txt +2 -2
- package/dist/server/out/accounts/password/reset/__next.accounts.password.reset.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/password/reset/index.html +1 -1
- package/dist/server/out/accounts/password/reset/index.txt +2 -2
- package/dist/server/out/accounts/signup/__next._full.txt +2 -2
- package/dist/server/out/accounts/signup/__next._tree.txt +2 -2
- package/dist/server/out/accounts/signup/__next.accounts.signup.__PAGE__.txt +2 -2
- package/dist/server/out/accounts/signup/index.html +1 -1
- package/dist/server/out/accounts/signup/index.txt +2 -2
- package/dist/server/out/setup/__next._full.txt +2 -2
- package/dist/server/out/setup/__next._tree.txt +2 -2
- package/dist/server/out/setup/__next.setup.__PAGE__.txt +2 -2
- package/dist/server/out/setup/index.html +1 -1
- package/dist/server/out/setup/index.txt +2 -2
- package/dist/server/out/studio/approve/__next._full.txt +2 -2
- package/dist/server/out/studio/approve/__next._tree.txt +2 -2
- package/dist/server/out/studio/approve/__next.studio.approve.__PAGE__.txt +2 -2
- package/dist/server/out/studio/approve/index.html +1 -1
- package/dist/server/out/studio/approve/index.txt +2 -2
- package/dist/server/out/workspaces/invite/__next._full.txt +2 -2
- package/dist/server/out/workspaces/invite/__next._tree.txt +2 -2
- package/dist/server/out/workspaces/invite/__next.workspaces.invite.__PAGE__.txt +2 -2
- package/dist/server/out/workspaces/invite/index.html +1 -1
- package/dist/server/out/workspaces/invite/index.txt +2 -2
- package/docs/npm-distribution.md +8 -0
- package/docs/studio.md +9 -1
- package/package.json +2 -1
- package/dist/server/out/_next/static/chunks/22w_ipn94al45.css +0 -1
- /package/dist/server/out/_next/static/{CkCrHy-m0W4Wo6_2T25_g → LKRuw_R6wl92CsuMfKtPZ}/_buildManifest.js +0 -0
- /package/dist/server/out/_next/static/{CkCrHy-m0W4Wo6_2T25_g → LKRuw_R6wl92CsuMfKtPZ}/_clientMiddlewareManifest.js +0 -0
- /package/dist/server/out/_next/static/{CkCrHy-m0W4Wo6_2T25_g → LKRuw_R6wl92CsuMfKtPZ}/_ssgManifest.js +0 -0
package/bin/options.mjs
CHANGED
|
@@ -41,6 +41,15 @@ const commands = new Set([
|
|
|
41
41
|
]);
|
|
42
42
|
const withArgument = new Set(["backup", "restore", "reset-password"]);
|
|
43
43
|
|
|
44
|
+
// The npm bootstrap must hand future commands and flags to the selected release.
|
|
45
|
+
export function installationDirectory(args, env = process.env) {
|
|
46
|
+
const flag = args[0] === "executor" ? -1 : args.indexOf("--data");
|
|
47
|
+
const override = flag < 0 ? undefined : args[flag + 1];
|
|
48
|
+
return resolve(
|
|
49
|
+
override || env.ORGSDK_DATA_DIR || join(homedir(), ".local/share/orgsdk"),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
export function parseArguments(args, env = process.env) {
|
|
45
54
|
if (args.length === 1 && ["--help", "-h"].includes(args[0])) {
|
|
46
55
|
return { command: "help" };
|
|
@@ -53,9 +62,7 @@ export function parseArguments(args, env = process.env) {
|
|
|
53
62
|
throw new Error(`Unknown command: ${command}. Use orgsdk --help.`);
|
|
54
63
|
}
|
|
55
64
|
const rest = command === args[0] ? args.slice(1) : [...args];
|
|
56
|
-
const data =
|
|
57
|
-
env.ORGSDK_DATA_DIR || join(homedir(), ".local/share/orgsdk"),
|
|
58
|
-
);
|
|
65
|
+
const data = installationDirectory(args, env);
|
|
59
66
|
if (command === "executor") {
|
|
60
67
|
return { command, data, args: rest };
|
|
61
68
|
}
|
package/bin/orgsdk.mjs
CHANGED
|
@@ -3,24 +3,38 @@ import { existsSync } from "node:fs";
|
|
|
3
3
|
import { readFile } from "node:fs/promises";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import process from "node:process";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
7
7
|
import {
|
|
8
8
|
bootstrapToken,
|
|
9
9
|
requireDockerConfiguration,
|
|
10
10
|
} from "./configuration.mjs";
|
|
11
|
-
import { help, parseArguments } from "./options.mjs";
|
|
11
|
+
import { help, installationDirectory, parseArguments } from "./options.mjs";
|
|
12
12
|
import { supervise } from "./supervision.mjs";
|
|
13
13
|
|
|
14
14
|
async function main() {
|
|
15
|
-
const
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
const manifest = JSON.parse(
|
|
17
|
+
await readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
18
|
+
);
|
|
19
|
+
const data = installationDirectory(args);
|
|
20
|
+
const launcher = new URL("../dist/launcher/updates.js", import.meta.url);
|
|
21
|
+
const updates = existsSync(launcher)
|
|
22
|
+
? await import(launcher.href)
|
|
23
|
+
: undefined;
|
|
24
|
+
const cli = fileURLToPath(import.meta.url);
|
|
25
|
+
if (updates && cli.endsWith("/node_modules/@orgsdk/cli/bin/orgsdk.mjs")) {
|
|
26
|
+
const selected = await updates.selectedCli(data, manifest.version);
|
|
27
|
+
if (selected) {
|
|
28
|
+
await import(pathToFileURL(selected).href);
|
|
29
|
+
return process.exitCode ?? 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const options = parseArguments(args);
|
|
16
33
|
if (options.command === "help") {
|
|
17
34
|
console.log(help);
|
|
18
35
|
return 0;
|
|
19
36
|
}
|
|
20
37
|
if (options.command === "version") {
|
|
21
|
-
const manifest = JSON.parse(
|
|
22
|
-
await readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
23
|
-
);
|
|
24
38
|
console.log(manifest.version);
|
|
25
39
|
return 0;
|
|
26
40
|
}
|
|
@@ -77,12 +91,54 @@ async function main() {
|
|
|
77
91
|
);
|
|
78
92
|
}
|
|
79
93
|
console.log(`Installation data: ${options.data}`);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
94
|
+
const manager = await updates?.updateManager(
|
|
95
|
+
data,
|
|
96
|
+
options.host,
|
|
97
|
+
env.EXECUTOR_DATA_DIR,
|
|
98
|
+
options.mode,
|
|
99
|
+
cli,
|
|
100
|
+
);
|
|
101
|
+
env.ORGSDK_MANAGED_UPDATES = manager ? "1" : "";
|
|
102
|
+
for (;;) {
|
|
103
|
+
let target;
|
|
104
|
+
const status = await supervise({
|
|
105
|
+
server,
|
|
106
|
+
executor,
|
|
107
|
+
executorArgs: ["manage", "--mode", options.mode],
|
|
108
|
+
env,
|
|
109
|
+
onUpdate: manager
|
|
110
|
+
? (version, port) => {
|
|
111
|
+
target = version;
|
|
112
|
+
env.PORT = String(port);
|
|
113
|
+
}
|
|
114
|
+
: undefined,
|
|
115
|
+
});
|
|
116
|
+
if (!target || status !== 0) {
|
|
117
|
+
return status;
|
|
118
|
+
}
|
|
119
|
+
let selected;
|
|
120
|
+
try {
|
|
121
|
+
selected = await updates.installSelectedRelease(
|
|
122
|
+
manager,
|
|
123
|
+
data,
|
|
124
|
+
server,
|
|
125
|
+
manifest.version,
|
|
126
|
+
target,
|
|
127
|
+
);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error(
|
|
130
|
+
"Update failed. Restarting the current release.",
|
|
131
|
+
error.message,
|
|
132
|
+
);
|
|
133
|
+
env.ORGSDK_FAILED_UPDATE = target;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
process.env.PORT = env.PORT;
|
|
137
|
+
delete process.env.ORGSDK_FAILED_UPDATE;
|
|
138
|
+
console.log("Starting OrgSDK " + target);
|
|
139
|
+
await import(pathToFileURL(selected).href);
|
|
140
|
+
return process.exitCode ?? 0;
|
|
141
|
+
}
|
|
86
142
|
}
|
|
87
143
|
|
|
88
144
|
try {
|
package/bin/supervision.mjs
CHANGED
|
@@ -45,6 +45,7 @@ export async function supervise({
|
|
|
45
45
|
executor,
|
|
46
46
|
serverArgs = ["server"],
|
|
47
47
|
executorArgs = ["manage"],
|
|
48
|
+
onUpdate,
|
|
48
49
|
env,
|
|
49
50
|
}) {
|
|
50
51
|
const children = [];
|
|
@@ -107,6 +108,7 @@ export async function supervise({
|
|
|
107
108
|
}
|
|
108
109
|
return {
|
|
109
110
|
address: localAddress(env.HOST ?? "127.0.0.1", message.port),
|
|
111
|
+
port: message.port,
|
|
110
112
|
};
|
|
111
113
|
}),
|
|
112
114
|
platform.closed,
|
|
@@ -118,6 +120,13 @@ export async function supervise({
|
|
|
118
120
|
return requestedStatus ?? (exitStatus(readiness) || 1);
|
|
119
121
|
}
|
|
120
122
|
controller.signal.throwIfAborted();
|
|
123
|
+
platform.child.on("message", (message) => {
|
|
124
|
+
if (!stopping && onUpdate && typeof message?.update === "string") {
|
|
125
|
+
onUpdate(message.update, readiness.port);
|
|
126
|
+
requestedStatus = 0;
|
|
127
|
+
void stop();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
121
130
|
children.push(
|
|
122
131
|
launch(executor, executorArgs, {
|
|
123
132
|
...env,
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// scripts/installation-updater.ts
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { constants, existsSync as existsSync2 } from "node:fs";
|
|
5
|
+
import {
|
|
6
|
+
access,
|
|
7
|
+
cp,
|
|
8
|
+
lstat,
|
|
9
|
+
mkdir,
|
|
10
|
+
mkdtemp,
|
|
11
|
+
readFile,
|
|
12
|
+
readdir,
|
|
13
|
+
realpath,
|
|
14
|
+
rename,
|
|
15
|
+
rm,
|
|
16
|
+
stat,
|
|
17
|
+
writeFile
|
|
18
|
+
} from "node:fs/promises";
|
|
19
|
+
import { homedir } from "node:os";
|
|
20
|
+
import { delimiter, join as join2, resolve, sep } from "node:path";
|
|
21
|
+
import { promisify } from "node:util";
|
|
22
|
+
import { z as z2 } from "zod";
|
|
23
|
+
|
|
24
|
+
// server/platform/stable-release.ts
|
|
25
|
+
import { z } from "zod";
|
|
26
|
+
var stableVersion = z.string().max(32).regex(/^(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})\.(0|[1-9]\d{0,8})$/);
|
|
27
|
+
function newerStable(candidate, installed) {
|
|
28
|
+
if (!stableVersion.safeParse(candidate).success || !stableVersion.safeParse(installed).success) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const current = installed.split(".");
|
|
32
|
+
for (const [index, part] of candidate.split(".").entries()) {
|
|
33
|
+
const difference = Number(part) - Number(current[index]);
|
|
34
|
+
if (difference !== 0) {
|
|
35
|
+
return difference > 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
var release = z.object({
|
|
41
|
+
name: z.literal("@orgsdk/cli"),
|
|
42
|
+
version: stableVersion
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// server/platform/server-lock.ts
|
|
46
|
+
import {
|
|
47
|
+
existsSync,
|
|
48
|
+
mkdirSync,
|
|
49
|
+
readFileSync,
|
|
50
|
+
writeFileSync,
|
|
51
|
+
unlinkSync
|
|
52
|
+
} from "node:fs";
|
|
53
|
+
import { join } from "node:path";
|
|
54
|
+
function serverLock(dataDir) {
|
|
55
|
+
mkdirSync(dataDir, { recursive: true, mode: 448 });
|
|
56
|
+
const file = join(dataDir, "server.pid");
|
|
57
|
+
if (existsSync(file)) {
|
|
58
|
+
const pid = Number(readFileSync(file, "utf8"));
|
|
59
|
+
if (!Number.isInteger(pid) || pid < 1) {
|
|
60
|
+
throw new Error("Invalid server lock; inspect the data directory.");
|
|
61
|
+
}
|
|
62
|
+
let alive = false;
|
|
63
|
+
try {
|
|
64
|
+
process.kill(pid, 0);
|
|
65
|
+
alive = true;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (!(error instanceof Error) || !("code" in error) || error.code !== "ESRCH") {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (alive) {
|
|
72
|
+
throw new Error("An OrgSDK server already owns this data directory.");
|
|
73
|
+
}
|
|
74
|
+
unlinkSync(file);
|
|
75
|
+
}
|
|
76
|
+
writeFileSync(file, String(process.pid), { flag: "wx", mode: 384 });
|
|
77
|
+
return () => {
|
|
78
|
+
if (existsSync(file) && readFileSync(file, "utf8") === String(process.pid)) {
|
|
79
|
+
unlinkSync(file);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// scripts/installation-updater.ts
|
|
85
|
+
var run = promisify(execFile);
|
|
86
|
+
var versionFile = "active-version";
|
|
87
|
+
var packageDirectory = "node_modules/@orgsdk/cli";
|
|
88
|
+
var packageEntry = "bin/orgsdk.mjs";
|
|
89
|
+
async function installationStorage(data) {
|
|
90
|
+
const directory = await realpath(data);
|
|
91
|
+
const key = createHash("sha256").update(directory).digest("hex");
|
|
92
|
+
const storage = join2(homedir(), ".local/share/orgsdk-updates", key);
|
|
93
|
+
if (storage === directory || storage.startsWith(directory + sep) || directory === sep) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
return storage;
|
|
97
|
+
}
|
|
98
|
+
async function selectedCli(data, installed) {
|
|
99
|
+
if (process.getuid?.() === 0) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
let storage;
|
|
103
|
+
let active;
|
|
104
|
+
try {
|
|
105
|
+
const directory = await installationStorage(data);
|
|
106
|
+
if (!directory) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
storage = directory;
|
|
110
|
+
active = (await readFile(join2(storage, versionFile), "utf8")).trim();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
stableVersion.parse(active);
|
|
118
|
+
if (newerStable(active, installed)) {
|
|
119
|
+
return join2(storage, active, packageDirectory, packageEntry);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function updateManager(data, host, executorData, mode, cli) {
|
|
123
|
+
if (process.getuid?.() === 0 || process.versions.bun || existsSync2("/.dockerenv") || existsSync2("/run/.containerenv") || !["localhost", "127.0.0.1", "::1"].includes(host) || mode !== "host" || resolve(executorData) !== join2(data, "executor") || !cli.endsWith("/" + packageDirectory + "/" + packageEntry) || process.env.ORGSDK_PUBLIC_ORIGIN || process.env.ORGSDK_DISABLE_UPDATES === "1") {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
for (const directory of (process.env.PATH ?? "").split(delimiter).filter((path) => path.startsWith("/"))) {
|
|
127
|
+
const npm = join2(directory, "npm");
|
|
128
|
+
try {
|
|
129
|
+
await access(npm, constants.X_OK);
|
|
130
|
+
} catch {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const storage = await installationStorage(data);
|
|
135
|
+
if (!storage) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
await mkdir(storage, { recursive: true, mode: 448 });
|
|
139
|
+
const info = await lstat(storage);
|
|
140
|
+
if (!info.isDirectory() || info.uid !== process.getuid?.() || (info.mode & 63) !== 0) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
await access(storage, constants.W_OK);
|
|
144
|
+
return { storage, npm };
|
|
145
|
+
} catch {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function preserveInstallation(data, storage, server) {
|
|
151
|
+
const backup = await mkdtemp(join2(storage, "backup-"));
|
|
152
|
+
console.log("Saving installation backup in " + backup);
|
|
153
|
+
await run(
|
|
154
|
+
process.execPath,
|
|
155
|
+
[server, "backup", join2(backup, "orgsdk.sqlite3")],
|
|
156
|
+
{ env: { ...process.env, ORGSDK_DATA_DIR: data } }
|
|
157
|
+
);
|
|
158
|
+
for (const name of await readdir(data)) {
|
|
159
|
+
if ([
|
|
160
|
+
"server.pid",
|
|
161
|
+
"orgsdk.sqlite3",
|
|
162
|
+
"orgsdk.sqlite3-wal",
|
|
163
|
+
"orgsdk.sqlite3-shm",
|
|
164
|
+
"orgsdk.sqlite3-journal"
|
|
165
|
+
].includes(name)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
await cp(join2(data, name), join2(backup, name), {
|
|
169
|
+
recursive: true,
|
|
170
|
+
verbatimSymlinks: true,
|
|
171
|
+
preserveTimestamps: true
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
await access(join2(backup, "encryption.key"));
|
|
175
|
+
return backup;
|
|
176
|
+
}
|
|
177
|
+
async function installSelectedRelease(manager, data, server, installed, target) {
|
|
178
|
+
if (!newerStable(target, installed)) {
|
|
179
|
+
throw new Error("Select a newer stable release.");
|
|
180
|
+
}
|
|
181
|
+
const unlock = serverLock(data);
|
|
182
|
+
try {
|
|
183
|
+
const { storage, npm } = manager;
|
|
184
|
+
for (const name of ["encryption.key", "executor"]) {
|
|
185
|
+
const info = await lstat(join2(data, name)).catch((error) => {
|
|
186
|
+
if (name === "executor" && error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
187
|
+
return void 0;
|
|
188
|
+
}
|
|
189
|
+
throw error;
|
|
190
|
+
});
|
|
191
|
+
if (info?.isSymbolicLink()) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
"Back up externally linked installation data before updating manually."
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
await preserveInstallation(data, storage, server);
|
|
198
|
+
const staging = await mkdtemp(join2(storage, "download-"));
|
|
199
|
+
const destination = join2(storage, target);
|
|
200
|
+
try {
|
|
201
|
+
console.log("Installing OrgSDK " + target);
|
|
202
|
+
await run(
|
|
203
|
+
npm,
|
|
204
|
+
[
|
|
205
|
+
"install",
|
|
206
|
+
"--global=false",
|
|
207
|
+
"--prefix",
|
|
208
|
+
staging,
|
|
209
|
+
"--registry=https://registry.npmjs.org/",
|
|
210
|
+
"--@orgsdk:registry=https://registry.npmjs.org/",
|
|
211
|
+
"--engine-strict",
|
|
212
|
+
"--no-audit",
|
|
213
|
+
"--no-fund",
|
|
214
|
+
"--no-save",
|
|
215
|
+
"@orgsdk/cli@" + target
|
|
216
|
+
],
|
|
217
|
+
{ cwd: staging, timeout: 6e5 }
|
|
218
|
+
);
|
|
219
|
+
const cli = join2(staging, packageDirectory, packageEntry);
|
|
220
|
+
for (const args of [["--version"], ["executor", "--version"]]) {
|
|
221
|
+
const { stdout } = await run(process.execPath, [cli, ...args], {
|
|
222
|
+
env: { ...process.env, ORGSDK_DATA_DIR: staging },
|
|
223
|
+
timeout: 15e3
|
|
224
|
+
});
|
|
225
|
+
if (stdout.trim() !== target) {
|
|
226
|
+
throw new Error("Downloaded package version mismatch.");
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
await rename(staging, destination);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (!(error instanceof Error && "code" in error && ["ENOTEMPTY", "EEXIST"].includes(String(error.code)))) {
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
const manifest = z2.object({
|
|
236
|
+
name: z2.literal("@orgsdk/cli"),
|
|
237
|
+
version: z2.literal(target)
|
|
238
|
+
});
|
|
239
|
+
manifest.parse(
|
|
240
|
+
JSON.parse(
|
|
241
|
+
await readFile(
|
|
242
|
+
join2(destination, packageDirectory, "package.json"),
|
|
243
|
+
"utf8"
|
|
244
|
+
)
|
|
245
|
+
)
|
|
246
|
+
);
|
|
247
|
+
await stat(join2(destination, packageDirectory, packageEntry));
|
|
248
|
+
}
|
|
249
|
+
const marker = join2(storage, "selection-" + randomUUID());
|
|
250
|
+
await writeFile(marker, target + "\n", { mode: 384, flag: "wx" });
|
|
251
|
+
await rename(marker, join2(storage, versionFile));
|
|
252
|
+
} finally {
|
|
253
|
+
await rm(staging, { recursive: true, force: true });
|
|
254
|
+
}
|
|
255
|
+
return join2(destination, packageDirectory, packageEntry);
|
|
256
|
+
} finally {
|
|
257
|
+
unlock();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
export {
|
|
261
|
+
installSelectedRelease,
|
|
262
|
+
selectedCli,
|
|
263
|
+
updateManager
|
|
264
|
+
};
|
package/dist/runtime/client.js
CHANGED
|
@@ -46,6 +46,25 @@ var Client = class {
|
|
|
46
46
|
result
|
|
47
47
|
);
|
|
48
48
|
}
|
|
49
|
+
shellRules(pluginId) {
|
|
50
|
+
return this.#request(
|
|
51
|
+
`/internal/execution/v1/product/shell/${identifier(pluginId)}/`,
|
|
52
|
+
"GET"
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
requestShellApproval(pluginId, executable, args) {
|
|
56
|
+
return this.#request(
|
|
57
|
+
`/internal/execution/v1/product/shell/${identifier(pluginId)}/`,
|
|
58
|
+
"POST",
|
|
59
|
+
{ executable, args }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
shellApproval(pluginId, id) {
|
|
63
|
+
return this.#request(
|
|
64
|
+
`/internal/execution/v1/product/shell/${identifier(pluginId)}/${identifier(id)}/`,
|
|
65
|
+
"GET"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
49
68
|
createApproval(input) {
|
|
50
69
|
return this.#request(
|
|
51
70
|
"/internal/execution/v1/product/approvals/",
|
package/dist/runtime/contract.js
CHANGED
|
@@ -8,6 +8,7 @@ import { connectionSchema } from "./mcp/contract.js";
|
|
|
8
8
|
var pluginSchema = z.object({
|
|
9
9
|
distribution: z.enum(["inline", "external", "mcp"]).optional(),
|
|
10
10
|
mcp: connectionSchema.optional(),
|
|
11
|
+
shell_policy_id: z.uuid().optional(),
|
|
11
12
|
package_url: z.string().optional(),
|
|
12
13
|
configuration: z.record(z.string(), z.unknown()).optional(),
|
|
13
14
|
credential_provider: z.string().nullable().optional(),
|
package/dist/runtime/plugins.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/plugins.ts
|
|
2
|
+
import { shellPolicy } from "./shell-policy.js";
|
|
2
3
|
import { importPluginModule } from "./plugin-module.js";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
import { installPlugin } from "./plugin-store.js";
|
|
@@ -79,14 +80,23 @@ function snapshotContext(plugin, secret) {
|
|
|
79
80
|
}
|
|
80
81
|
};
|
|
81
82
|
}
|
|
82
|
-
async function loadPlugin(plugin, client, agentId) {
|
|
83
|
+
async function loadPlugin(plugin, client, agentId, onApprovalWait) {
|
|
83
84
|
const file = await installPlugin(plugin, agentId, void 0, client.signal);
|
|
84
85
|
const secret = plugin.credential_id ? await client.secret(plugin.credential_id) : void 0;
|
|
85
86
|
if (secret?.source === "first-party")
|
|
86
87
|
throw new Error(
|
|
87
88
|
"Reconnect this credential using your own OAuth application; the former first-party relay is retired"
|
|
88
89
|
);
|
|
89
|
-
const context =
|
|
90
|
+
const context = {
|
|
91
|
+
...snapshotContext(plugin, secret),
|
|
92
|
+
...plugin.shell_policy_id && plugin.slug === "shell" && plugin.distribution === "external" ? {
|
|
93
|
+
shellPolicy: shellPolicy(
|
|
94
|
+
client,
|
|
95
|
+
plugin.shell_policy_id,
|
|
96
|
+
onApprovalWait
|
|
97
|
+
)
|
|
98
|
+
} : {}
|
|
99
|
+
};
|
|
90
100
|
const exports = await importPluginModule(
|
|
91
101
|
file,
|
|
92
102
|
plugin.distribution !== "external"
|
|
@@ -111,10 +121,12 @@ async function loadPlugin(plugin, client, agentId) {
|
|
|
111
121
|
};
|
|
112
122
|
});
|
|
113
123
|
}
|
|
114
|
-
async function loadPlugins(plugins, client, agentId) {
|
|
124
|
+
async function loadPlugins(plugins, client, agentId, onApprovalWait) {
|
|
115
125
|
const functions = [];
|
|
116
126
|
for (const plugin of plugins)
|
|
117
|
-
functions.push(
|
|
127
|
+
functions.push(
|
|
128
|
+
...await loadPlugin(plugin, client, agentId, onApprovalWait)
|
|
129
|
+
);
|
|
118
130
|
return functions;
|
|
119
131
|
}
|
|
120
132
|
export {
|
package/dist/runtime/runner.js
CHANGED
|
@@ -122,13 +122,20 @@ var Runner = class {
|
|
|
122
122
|
);
|
|
123
123
|
}
|
|
124
124
|
const executor = new SandboxExecutor();
|
|
125
|
+
const toolContext = {
|
|
126
|
+
agent,
|
|
127
|
+
stores: {},
|
|
128
|
+
actionRunner: {},
|
|
129
|
+
runId: this.job.run_id
|
|
130
|
+
};
|
|
125
131
|
executor.registerBuiltins(this.builtins());
|
|
126
132
|
const remote = agent.plugins.filter((plugin) => plugin.distribution === "mcp").map((plugin) => mcpPlugin(plugin, this.client, this.signal));
|
|
127
133
|
executor.registerMultiple(
|
|
128
134
|
await loadPlugins(
|
|
129
135
|
agent.plugins.filter((plugin) => plugin.distribution !== "mcp"),
|
|
130
136
|
this.client,
|
|
131
|
-
agent.id
|
|
137
|
+
agent.id,
|
|
138
|
+
() => toolContext.resetExecutionTimeout?.()
|
|
132
139
|
)
|
|
133
140
|
);
|
|
134
141
|
executor.registerMultiple(remote.flatMap((entry) => entry.functions));
|
|
@@ -166,18 +173,14 @@ var Runner = class {
|
|
|
166
173
|
append(kind, data);
|
|
167
174
|
};
|
|
168
175
|
try {
|
|
169
|
-
const result = await executor.execute(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
onTraceTruncated: markTruncated,
|
|
178
|
-
onStep: (step) => enqueue("execution.step", step)
|
|
179
|
-
}
|
|
180
|
-
);
|
|
176
|
+
const result = await executor.execute(source, toolContext, {
|
|
177
|
+
timeout: 3e5,
|
|
178
|
+
parameters: inputs,
|
|
179
|
+
signal: this.signal,
|
|
180
|
+
onLog: (line) => enqueue("execution.log", { message: line }),
|
|
181
|
+
onTraceTruncated: markTruncated,
|
|
182
|
+
onStep: (step) => enqueue("execution.step", step)
|
|
183
|
+
});
|
|
181
184
|
enqueue("action.result", {
|
|
182
185
|
logs: result.logs,
|
|
183
186
|
steps: result.steps,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/shell-policy.ts
|
|
2
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
var decision = z.discriminatedUnion("status", [
|
|
5
|
+
z.object({ status: z.literal("pending"), id: z.uuid() }),
|
|
6
|
+
z.object({ status: z.enum(["approved", "denied", "interrupted"]) })
|
|
7
|
+
]);
|
|
8
|
+
function shellPolicy(client, pluginId, onApprovalWait) {
|
|
9
|
+
return {
|
|
10
|
+
listAllowed: async () => z.array(z.string()).parse(await client.shellRules(pluginId)),
|
|
11
|
+
async authorize(executable, args) {
|
|
12
|
+
let result = decision.parse(
|
|
13
|
+
await client.requestShellApproval(pluginId, executable, args)
|
|
14
|
+
);
|
|
15
|
+
while (result.status === "pending") {
|
|
16
|
+
onApprovalWait?.();
|
|
17
|
+
await sleep(500, void 0, { signal: client.signal });
|
|
18
|
+
result = decision.parse(
|
|
19
|
+
await client.shellApproval(pluginId, result.id)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
client.signal?.throwIfAborted();
|
|
23
|
+
if (result.status !== "approved") {
|
|
24
|
+
throw new Error("Shell execution blocked or interrupted.");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
shellPolicy
|
|
31
|
+
};
|
|
@@ -30,6 +30,7 @@ __export(schema_exports, {
|
|
|
30
30
|
runs: () => runs,
|
|
31
31
|
schedules: () => schedules,
|
|
32
32
|
sessions: () => sessions,
|
|
33
|
+
shellRules: () => shellRules,
|
|
33
34
|
tableRows: () => tableRows,
|
|
34
35
|
users: () => users,
|
|
35
36
|
webhookDeliveries: () => webhookDeliveries,
|
|
@@ -347,6 +348,7 @@ var approvals = sqliteTable4("approvals", {
|
|
|
347
348
|
workspaceId: integer4("workspace_id").notNull().references(() => workspaces.id, { onDelete: "cascade" }),
|
|
348
349
|
runId: text4("run_id").notNull().references(() => runs.id, { onDelete: "cascade" }),
|
|
349
350
|
prompt: text4("prompt").notNull(),
|
|
351
|
+
shell: text4("shell", { mode: "json" }).$type(),
|
|
350
352
|
status: text4("status").notNull().default(sql4`'pending'`),
|
|
351
353
|
decidedBy: integer4("decided_by").references(() => users.id, {
|
|
352
354
|
onDelete: "restrict"
|
|
@@ -593,6 +595,7 @@ import {
|
|
|
593
595
|
import { sql as sql10 } from "drizzle-orm";
|
|
594
596
|
import {
|
|
595
597
|
integer as integer10,
|
|
598
|
+
primaryKey,
|
|
596
599
|
sqliteTable as sqliteTable10,
|
|
597
600
|
text as text10,
|
|
598
601
|
uniqueIndex as uniqueIndex7
|
|
@@ -638,6 +641,15 @@ var credentials = sqliteTable10("credentials", {
|
|
|
638
641
|
createdAt: text10("created_at").notNull(),
|
|
639
642
|
updatedAt: text10("updated_at").notNull()
|
|
640
643
|
});
|
|
644
|
+
var shellRules = sqliteTable10(
|
|
645
|
+
"shell_rules",
|
|
646
|
+
{
|
|
647
|
+
pluginId: text10("plugin_id").notNull().references(() => plugins.id, { onDelete: "cascade" }),
|
|
648
|
+
executable: text10("executable").notNull(),
|
|
649
|
+
decision: text10("decision", { enum: ["allow", "block"] }).notNull()
|
|
650
|
+
},
|
|
651
|
+
(table) => [primaryKey({ columns: [table.pluginId, table.executable] })]
|
|
652
|
+
);
|
|
641
653
|
|
|
642
654
|
// server/db/schema/oauth.ts
|
|
643
655
|
var oauthAttempts = sqliteTable11("oauth_attempts", {
|
|
@@ -703,6 +715,7 @@ export {
|
|
|
703
715
|
authAttempts,
|
|
704
716
|
plugins,
|
|
705
717
|
credentials,
|
|
718
|
+
shellRules,
|
|
706
719
|
oauthAttempts,
|
|
707
720
|
mcpConnections,
|
|
708
721
|
schema_exports
|