Skip to content

Commit 56e65fd

Browse files
committed
build(secrets): embed native keyring bindings in standalone binaries
1 parent ef4e6ce commit 56e65fd

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎.github/workflows/release-main-and-preview.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
registry-url: https://registry.npmjs.org
7777
# npm trusted publishing needs npm 11.5.1 or newer, Node 20 bundles npm 10.
7878
- run: npm install -g npm@11
79-
- run: bun install --frozen-lockfile
79+
- run: bun install --frozen-lockfile --os="*" --cpu="*"
8080

8181
- name: Resolve release
8282
id: release

‎scripts/build.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { existsSync } from "node:fs";
55
import { chmod } from "node:fs/promises";
66
import { join, resolve } from "node:path";
77
import { runWithExitCode } from "../src/runnable";
8+
import { nativeKeyringPlugin } from "./keyring";
89

910
const REPO_ROOT = resolve(import.meta.dir, "..");
1011
const ASSETS_DIR = join(REPO_ROOT, "src", "assets");
@@ -202,8 +203,7 @@ async function compile(target: string): Promise<void> {
202203
define: DEFINE,
203204
root: REPO_ROOT,
204205
naming: { asset: ASSET_NAMING },
205-
external: ["@napi-rs/keyring"],
206-
plugins: [assetLoaderPlugin(), runtimeShellSdkPlugin()],
206+
plugins: [assetLoaderPlugin(), runtimeShellSdkPlugin(), nativeKeyringPlugin(target)],
207207
});
208208
await assertTemplateIsEmbedded(outfile, template);
209209
console.log(

‎scripts/keyring.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect, test } from "bun:test";
2+
import { mkdtemp, rename, rm, writeFile } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { keyringBindingPath, nativeKeyringPlugin } from "./keyring";
6+
7+
test("compiled keyring loads without an adjacent node_modules directory", async () => {
8+
const root = await mkdtemp(join(tmpdir(), "agentcore-keyring-binary-"));
9+
try {
10+
const entry = join(root, "entry.ts"),
11+
binary = join(root, process.platform === "win32" ? "probe.exe" : "probe");
12+
const modulePath = join(import.meta.dir, "..", "src", "io", "keyring.ts");
13+
await writeFile(
14+
entry,
15+
"import { loadKeyring } from " +
16+
JSON.stringify(modulePath) +
17+
';\nif (typeof loadKeyring().Entry !== "function") throw new Error("Missing native Entry");\nconsole.log("embedded-keyring");\n',
18+
);
19+
const platform = process.platform === "win32" ? "windows" : process.platform;
20+
const target = "bun-" + platform + "-" + process.arch;
21+
await Bun.build({
22+
entrypoints: [entry],
23+
compile: { outfile: binary },
24+
plugins: [nativeKeyringPlugin(target)],
25+
});
26+
const binding = keyringBindingPath(target);
27+
const hidden = binding + ".probe-hidden";
28+
await rename(binding, hidden);
29+
try {
30+
const child = Bun.spawn([binary], { cwd: root, stdout: "pipe", stderr: "pipe" });
31+
const [stdout, stderr, status] = await Promise.all([
32+
new Response(child.stdout).text(),
33+
new Response(child.stderr).text(),
34+
child.exited,
35+
]);
36+
expect({ status, stderr }).toEqual({ status: 0, stderr: "" });
37+
expect(stdout.trim()).toBe("embedded-keyring");
38+
} finally {
39+
await rename(hidden, binding);
40+
}
41+
} finally {
42+
await rm(root, { recursive: true, force: true });
43+
}
44+
}, 30000);

‎scripts/keyring.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { dirname, join } from "node:path";
2+
3+
const REPO_ROOT = join(import.meta.dir, "..");
4+
5+
const TARGETS: Record<string, string> = {
6+
"bun-darwin-x64": "darwin-x64",
7+
"bun-darwin-arm64": "darwin-arm64",
8+
"bun-linux-x64": "linux-x64-gnu",
9+
"bun-linux-arm64": "linux-arm64-gnu",
10+
"bun-windows-x64": "win32-x64-msvc",
11+
"bun-windows-arm64": "win32-arm64-msvc",
12+
};
13+
14+
export function keyringBindingPath(target: string): string {
15+
const platform = TARGETS[target];
16+
if (!platform) throw new Error("Unsupported keyring target: " + target);
17+
try {
18+
const manifest = Bun.resolveSync("@napi-rs/keyring-" + platform + "/package.json", REPO_ROOT);
19+
return join(dirname(manifest), "keyring." + platform + ".node");
20+
} catch (cause) {
21+
throw new Error("Missing keyring binding for " + target + ". Run bun install --os=* --cpu=*.", {
22+
cause,
23+
});
24+
}
25+
}
26+
27+
export function nativeKeyringPlugin(target: string): Bun.BunPlugin {
28+
const path = keyringBindingPath(target);
29+
return {
30+
name: "embedded-keyring",
31+
setup(build) {
32+
build.onLoad({ filter: /[/\\]io[/\\]keyring\.ts$/ }, () => ({
33+
contents: "export function loadKeyring() { return require(" + JSON.stringify(path) + "); }",
34+
loader: "js",
35+
}));
36+
},
37+
};
38+
}

0 commit comments

Comments
 (0)