@open-conduit/sdk 7.0.0 → 7.1.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/dist/actions.d.ts +145 -0
- package/dist/actions.js +281 -0
- package/dist/actiontest.d.ts +80 -0
- package/dist/actiontest.js +190 -0
- package/dist/artifactstore.d.ts +133 -0
- package/dist/artifactstore.js +364 -0
- package/dist/backup.d.ts +99 -0
- package/dist/backup.js +178 -0
- package/dist/containertest.d.ts +71 -0
- package/dist/containertest.js +294 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +37 -0
- package/dist/executor.d.ts +374 -0
- package/dist/executor.js +556 -0
- package/dist/executortest.d.ts +183 -0
- package/dist/executortest.js +534 -0
- package/dist/extension.d.ts +496 -1
- package/dist/extension.js +1152 -4
- package/dist/feed.d.ts +20 -0
- package/dist/feed.js +88 -0
- package/dist/forward.d.ts +38 -0
- package/dist/forward.js +163 -0
- package/dist/forwardca.d.ts +21 -0
- package/dist/forwardca.js +144 -0
- package/dist/hooks.d.ts +108 -0
- package/dist/hooks.js +13 -0
- package/dist/host.d.ts +166 -2
- package/dist/host.js +235 -2
- package/dist/index.d.ts +27 -3
- package/dist/index.js +14 -2
- package/dist/probe.d.ts +61 -0
- package/dist/probe.js +92 -0
- package/dist/providertest.d.ts +59 -0
- package/dist/providertest.js +168 -0
- package/dist/resources.d.ts +208 -0
- package/dist/resources.js +303 -0
- package/dist/sandbox.d.ts +84 -0
- package/dist/sandbox.js +85 -0
- package/dist/setup.d.ts +145 -0
- package/dist/setup.js +179 -0
- package/dist/setuptest.d.ts +90 -0
- package/dist/setuptest.js +319 -0
- package/dist/storetest.d.ts +64 -0
- package/dist/storetest.js +740 -0
- package/dist/telemetry.d.ts +86 -0
- package/dist/telemetry.js +46 -0
- package/dist/testing.d.ts +591 -2
- package/dist/testing.js +1460 -6
- package/dist/toolbelt.d.ts +91 -0
- package/dist/toolbelt.js +216 -0
- package/dist/tunnel.d.ts +62 -0
- package/dist/tunnel.js +93 -0
- package/dist/wire.d.ts +4060 -348
- package/dist/wire.js +1583 -0
- package/package.json +1 -1
package/dist/feed.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The reader stopped before the body ended (io.ErrClosedPipe). */
|
|
2
|
+
export declare class FeedClosedError extends Error {
|
|
3
|
+
constructor();
|
|
4
|
+
}
|
|
5
|
+
export declare class Feed implements AsyncIterable<Uint8Array> {
|
|
6
|
+
#private;
|
|
7
|
+
/** Hands the reader chunk, resolving once it has taken it. */
|
|
8
|
+
push(chunk: Uint8Array): Promise<void>;
|
|
9
|
+
/** Ends the body: the reader's loop finishes once it has taken everything. */
|
|
10
|
+
end(): void;
|
|
11
|
+
/** Fails the body: the reader's next read throws err. */
|
|
12
|
+
fail(err: Error): void;
|
|
13
|
+
/**
|
|
14
|
+
* The reader has stopped, with the outcome of what read it (a put or an
|
|
15
|
+
* import): chunks still waiting, and any pushed later, are refused with
|
|
16
|
+
* err, or FeedClosedError when it succeeded without reading them.
|
|
17
|
+
*/
|
|
18
|
+
stop(err?: unknown): void;
|
|
19
|
+
[Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
|
|
20
|
+
}
|
package/dist/feed.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// A body the host sends in chunks, one request each, that a package's code
|
|
2
|
+
// reads as an AsyncIterable: a backup destination's put and an artifact
|
|
3
|
+
// store's import. As io.Pipe does in the Go SDK, each chunk's request is
|
|
4
|
+
// answered only once the reader has taken the chunk, so a slow store holds
|
|
5
|
+
// the host back rather than the process buffering the whole file.
|
|
6
|
+
/** The reader stopped before the body ended (io.ErrClosedPipe). */
|
|
7
|
+
export class FeedClosedError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("the reader stopped before the body ended");
|
|
10
|
+
this.name = "FeedClosedError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class Feed {
|
|
14
|
+
#queue = [];
|
|
15
|
+
#wake;
|
|
16
|
+
#ended = false;
|
|
17
|
+
/** Why the body failed (its reader sees it), once it has. */
|
|
18
|
+
#failed;
|
|
19
|
+
/** The reader stopped (returned, threw or finished): later chunks are refused with this. */
|
|
20
|
+
#stopped;
|
|
21
|
+
/** Hands the reader chunk, resolving once it has taken it. */
|
|
22
|
+
push(chunk) {
|
|
23
|
+
if (this.#stopped !== undefined)
|
|
24
|
+
return Promise.reject(this.#stopped);
|
|
25
|
+
if (this.#ended || this.#failed !== undefined)
|
|
26
|
+
return Promise.reject(new FeedClosedError());
|
|
27
|
+
return new Promise((taken, refused) => {
|
|
28
|
+
this.#queue.push({ chunk, taken, refused });
|
|
29
|
+
this.#signal();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/** Ends the body: the reader's loop finishes once it has taken everything. */
|
|
33
|
+
end() {
|
|
34
|
+
this.#ended = true;
|
|
35
|
+
this.#signal();
|
|
36
|
+
}
|
|
37
|
+
/** Fails the body: the reader's next read throws err. */
|
|
38
|
+
fail(err) {
|
|
39
|
+
if (this.#failed === undefined)
|
|
40
|
+
this.#failed = err;
|
|
41
|
+
this.#signal();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The reader has stopped, with the outcome of what read it (a put or an
|
|
45
|
+
* import): chunks still waiting, and any pushed later, are refused with
|
|
46
|
+
* err, or FeedClosedError when it succeeded without reading them.
|
|
47
|
+
*/
|
|
48
|
+
stop(err) {
|
|
49
|
+
this.#stopped =
|
|
50
|
+
err instanceof Error
|
|
51
|
+
? err
|
|
52
|
+
: err === undefined
|
|
53
|
+
? new FeedClosedError()
|
|
54
|
+
: new Error(String(err));
|
|
55
|
+
for (const p of this.#queue.splice(0))
|
|
56
|
+
p.refused(this.#stopped);
|
|
57
|
+
}
|
|
58
|
+
#signal() {
|
|
59
|
+
const wake = this.#wake;
|
|
60
|
+
this.#wake = undefined;
|
|
61
|
+
wake?.();
|
|
62
|
+
}
|
|
63
|
+
async *[Symbol.asyncIterator]() {
|
|
64
|
+
try {
|
|
65
|
+
for (;;) {
|
|
66
|
+
if (this.#failed !== undefined)
|
|
67
|
+
throw this.#failed;
|
|
68
|
+
const next = this.#queue.shift();
|
|
69
|
+
if (next !== undefined) {
|
|
70
|
+
next.taken();
|
|
71
|
+
if (next.chunk.length > 0)
|
|
72
|
+
yield next.chunk;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (this.#ended)
|
|
76
|
+
return;
|
|
77
|
+
await new Promise((resolve) => {
|
|
78
|
+
this.#wake = resolve;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
// A reader that leaves early takes no more.
|
|
84
|
+
if (this.#stopped === undefined && !this.#ended)
|
|
85
|
+
this.stop();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type TLSSocket } from "node:tls";
|
|
2
|
+
/** The file, beside the toolbelt socket, holding the port the host serves the forward on. */
|
|
3
|
+
export declare const ForwardPortFile = "toolbelt.port";
|
|
4
|
+
/** Beside it, the PEM certificate of the CA that signs the forward's certificates. */
|
|
5
|
+
export declare const ForwardCAFile = "toolbelt-ca.pem";
|
|
6
|
+
/** Beside it, the execution's PEM client certificate and its key. */
|
|
7
|
+
export declare const ForwardCertFile = "toolbelt-client.pem";
|
|
8
|
+
/** The name a client verifies the host's certificate for, whatever address it dials. */
|
|
9
|
+
export declare const ForwardServerName = "toolbelt.open-conduit.invalid";
|
|
10
|
+
/**
|
|
11
|
+
* Checks an address a provider's forward names, as the host does
|
|
12
|
+
* (extension.ForwardAddress): a literal loopback or private IP, never a
|
|
13
|
+
* name, or a public or unspecified address. It throws otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export declare function forwardAddress(host: string): void;
|
|
16
|
+
/** The TLS options dialForward uses, from the CA and client certificate in dir, for a client that dials the forward itself (extension.ForwardClientConfig). */
|
|
17
|
+
export declare function forwardClientOptions(dir: string): Promise<{
|
|
18
|
+
ca: string[];
|
|
19
|
+
cert: string;
|
|
20
|
+
key: string;
|
|
21
|
+
servername: string;
|
|
22
|
+
minVersion: "TLSv1.3";
|
|
23
|
+
rejectUnauthorized: true;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Reaches an execution's toolbelt forward as a run does
|
|
27
|
+
* (extension.DialForward): the port in portFile at host, over TLS 1.3,
|
|
28
|
+
* verifying the host's certificate for ForwardServerName against the CA
|
|
29
|
+
* in ForwardCAFile beside it and presenting the client certificate in
|
|
30
|
+
* ForwardCertFile. It never falls back to cleartext: missing files, a
|
|
31
|
+
* host that doesn't speak TLS or one whose certificate the CA didn't sign
|
|
32
|
+
* reject. The socket it resolves is connected and verified; the host may
|
|
33
|
+
* still refuse the client certificate, which ends the socket.
|
|
34
|
+
*/
|
|
35
|
+
export declare function dialForward(portFile: string, host: string, o?: {
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
}): Promise<TLSSocket>;
|
package/dist/forward.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// The toolbelt forward's channel, as extension/forward.go defines it. A
|
|
2
|
+
// container provider declared with forward names an address
|
|
3
|
+
// (container_provider.forward); the host serves the execution's toolbelt
|
|
4
|
+
// there itself, always over mutual TLS under a CA that lives only in its
|
|
5
|
+
// memory, and writes the CA's certificate and the execution's client
|
|
6
|
+
// certificate and key beside the port file, in the run's directory. A run
|
|
7
|
+
// on another machine, or the provider's relay for it, reaches the toolbelt
|
|
8
|
+
// with dialForward, which verifies the host and presents the execution's
|
|
9
|
+
// certificate, and never falls back to cleartext: a forward that crosses a
|
|
10
|
+
// network can't be read or impersonated on the way.
|
|
11
|
+
import { readFile } from "node:fs/promises";
|
|
12
|
+
import { isIP, isIPv4 } from "node:net";
|
|
13
|
+
import { dirname, join } from "node:path";
|
|
14
|
+
import { connect } from "node:tls";
|
|
15
|
+
/** The file, beside the toolbelt socket, holding the port the host serves the forward on. */
|
|
16
|
+
export const ForwardPortFile = "toolbelt.port";
|
|
17
|
+
/** Beside it, the PEM certificate of the CA that signs the forward's certificates. */
|
|
18
|
+
export const ForwardCAFile = "toolbelt-ca.pem";
|
|
19
|
+
/** Beside it, the execution's PEM client certificate and its key. */
|
|
20
|
+
export const ForwardCertFile = "toolbelt-client.pem";
|
|
21
|
+
/** The name a client verifies the host's certificate for, whatever address it dials. */
|
|
22
|
+
export const ForwardServerName = "toolbelt.open-conduit.invalid";
|
|
23
|
+
/**
|
|
24
|
+
* Checks an address a provider's forward names, as the host does
|
|
25
|
+
* (extension.ForwardAddress): a literal loopback or private IP, never a
|
|
26
|
+
* name, or a public or unspecified address. It throws otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export function forwardAddress(host) {
|
|
29
|
+
// Node's isIP accepts an IPv6 zone ID ("fd00::1%eth0"); Go's
|
|
30
|
+
// net.ParseIP doesn't, so the host refuses one.
|
|
31
|
+
if (host.includes("%") ||
|
|
32
|
+
!(isIP(host) && (loopback(host) || privateIP(host))))
|
|
33
|
+
throw new Error(`the provider named ${JSON.stringify(host)}, not a loopback or private IP address`);
|
|
34
|
+
}
|
|
35
|
+
function v4(host) {
|
|
36
|
+
if (isIPv4(host))
|
|
37
|
+
return host.split(".").map(Number);
|
|
38
|
+
// An IPv4-mapped IPv6 address is the IPv4 address, as Go reads it.
|
|
39
|
+
const m = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(host);
|
|
40
|
+
return m && isIPv4(m[1]) ? m[1].split(".").map(Number) : undefined;
|
|
41
|
+
}
|
|
42
|
+
function loopback(host) {
|
|
43
|
+
const a = v4(host);
|
|
44
|
+
if (a !== undefined)
|
|
45
|
+
return a[0] === 127;
|
|
46
|
+
return expand(host) === "0000:0000:0000:0000:0000:0000:0000:0001";
|
|
47
|
+
}
|
|
48
|
+
function privateIP(host) {
|
|
49
|
+
const a = v4(host);
|
|
50
|
+
if (a !== undefined)
|
|
51
|
+
return (a[0] === 10 ||
|
|
52
|
+
(a[0] === 172 && a[1] >= 16 && a[1] < 32) ||
|
|
53
|
+
(a[0] === 192 && a[1] === 168));
|
|
54
|
+
// fc00::/7, the unique local addresses.
|
|
55
|
+
return (parseInt(expand(host).slice(0, 2), 16) & 0xfe) === 0xfc;
|
|
56
|
+
}
|
|
57
|
+
/** The eight groups of an IPv6 address, each four hex digits. */
|
|
58
|
+
function expand(host) {
|
|
59
|
+
const [head, tail] = host.toLowerCase().split("::");
|
|
60
|
+
const left = head === "" ? [] : head.split(":");
|
|
61
|
+
const right = tail === undefined || tail === "" ? [] : tail.split(":");
|
|
62
|
+
const groups = tail === undefined
|
|
63
|
+
? left
|
|
64
|
+
: [
|
|
65
|
+
...left,
|
|
66
|
+
...Array(8 - left.length - right.length).fill("0"),
|
|
67
|
+
...right,
|
|
68
|
+
];
|
|
69
|
+
return groups.map((g) => g.padStart(4, "0")).join(":");
|
|
70
|
+
}
|
|
71
|
+
/** The PEM blocks of a type in text, each with its armour. */
|
|
72
|
+
function blocks(pem, type) {
|
|
73
|
+
const re = new RegExp(`-----BEGIN ${type}-----[\\s\\S]*?-----END ${type}-----`, "g");
|
|
74
|
+
return pem.match(re) ?? [];
|
|
75
|
+
}
|
|
76
|
+
/** The TLS options dialForward uses, from the CA and client certificate in dir, for a client that dials the forward itself (extension.ForwardClientConfig). */
|
|
77
|
+
export async function forwardClientOptions(dir) {
|
|
78
|
+
let caPEM;
|
|
79
|
+
try {
|
|
80
|
+
caPEM = await readFile(join(dir, ForwardCAFile), "utf8");
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
throw new Error(`no toolbelt forward CA: ${String(err)}`);
|
|
84
|
+
}
|
|
85
|
+
const ca = blocks(caPEM, "CERTIFICATE");
|
|
86
|
+
if (ca.length === 0)
|
|
87
|
+
throw new Error(`toolbelt forward CA ${join(dir, ForwardCAFile)} holds no certificate`);
|
|
88
|
+
let pair;
|
|
89
|
+
try {
|
|
90
|
+
pair = await readFile(join(dir, ForwardCertFile), "utf8");
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
throw new Error(`no toolbelt forward client certificate: ${String(err)}`);
|
|
94
|
+
}
|
|
95
|
+
const cert = blocks(pair, "CERTIFICATE");
|
|
96
|
+
const key = [
|
|
97
|
+
...blocks(pair, "EC PRIVATE KEY"),
|
|
98
|
+
...blocks(pair, "PRIVATE KEY"),
|
|
99
|
+
];
|
|
100
|
+
if (cert.length === 0 || key.length === 0)
|
|
101
|
+
throw new Error("toolbelt forward client certificate holds no certificate and key");
|
|
102
|
+
return {
|
|
103
|
+
ca,
|
|
104
|
+
cert: cert.join("\n"),
|
|
105
|
+
key: key[0],
|
|
106
|
+
servername: ForwardServerName,
|
|
107
|
+
minVersion: "TLSv1.3",
|
|
108
|
+
rejectUnauthorized: true,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Reaches an execution's toolbelt forward as a run does
|
|
113
|
+
* (extension.DialForward): the port in portFile at host, over TLS 1.3,
|
|
114
|
+
* verifying the host's certificate for ForwardServerName against the CA
|
|
115
|
+
* in ForwardCAFile beside it and presenting the client certificate in
|
|
116
|
+
* ForwardCertFile. It never falls back to cleartext: missing files, a
|
|
117
|
+
* host that doesn't speak TLS or one whose certificate the CA didn't sign
|
|
118
|
+
* reject. The socket it resolves is connected and verified; the host may
|
|
119
|
+
* still refuse the client certificate, which ends the socket.
|
|
120
|
+
*/
|
|
121
|
+
export async function dialForward(portFile, host, o = {}) {
|
|
122
|
+
let raw;
|
|
123
|
+
try {
|
|
124
|
+
raw = await readFile(portFile, "utf8");
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
throw new Error(`no toolbelt port: ${String(err)}`);
|
|
128
|
+
}
|
|
129
|
+
const port = Number(raw.trim());
|
|
130
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535)
|
|
131
|
+
throw new Error(`toolbelt port file ${portFile} names no port`);
|
|
132
|
+
const tls = await forwardClientOptions(dirname(portFile));
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
const socket = connect({ host, port, ...tls });
|
|
135
|
+
const timer = setTimeout(() => fail(new Error("toolbelt forward: timed out")), o.timeoutMs ?? 10_000);
|
|
136
|
+
const abort = () => fail(new Error("toolbelt forward: cancelled"));
|
|
137
|
+
function fail(err) {
|
|
138
|
+
cleanup();
|
|
139
|
+
socket.destroy();
|
|
140
|
+
reject(err);
|
|
141
|
+
}
|
|
142
|
+
function cleanup() {
|
|
143
|
+
clearTimeout(timer);
|
|
144
|
+
o.signal?.removeEventListener("abort", abort);
|
|
145
|
+
socket.off("error", fail);
|
|
146
|
+
}
|
|
147
|
+
if (o.signal?.aborted)
|
|
148
|
+
return abort();
|
|
149
|
+
o.signal?.addEventListener("abort", abort);
|
|
150
|
+
socket.once("error", fail);
|
|
151
|
+
socket.once("secureConnect", () => {
|
|
152
|
+
cleanup();
|
|
153
|
+
// rejectUnauthorized already refused a host the CA didn't sign; a
|
|
154
|
+
// socket that got here without authorizing is refused all the same.
|
|
155
|
+
if (!socket.authorized) {
|
|
156
|
+
socket.destroy();
|
|
157
|
+
reject(new Error("toolbelt forward: the host is not verified"));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
resolve(socket);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface Signed {
|
|
2
|
+
cert: string;
|
|
3
|
+
key: string;
|
|
4
|
+
}
|
|
5
|
+
/** The host's CA for one test's forwards (extension.ForwardAuthority). */
|
|
6
|
+
export declare class ForwardAuthority {
|
|
7
|
+
#private;
|
|
8
|
+
/** The CA's PEM certificate. */
|
|
9
|
+
readonly certificate: string;
|
|
10
|
+
constructor();
|
|
11
|
+
/**
|
|
12
|
+
* One execution's forward (extension.ForwardAuthority.Credentials): the
|
|
13
|
+
* host's server certificate for ip and ForwardServerName, and the run's
|
|
14
|
+
* files, by name, to write beside the port file.
|
|
15
|
+
*/
|
|
16
|
+
credentials(execution: string, ip: string): {
|
|
17
|
+
server: Signed;
|
|
18
|
+
files: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// A toolbelt forward CA for tests, as extension.ForwardAuthority is the
|
|
2
|
+
// host's: an ECDSA P-256 CA that lives only in memory, signing a server
|
|
3
|
+
// certificate for the forward's IP and ForwardServerName and a client
|
|
4
|
+
// certificate naming one execution. Node can verify certificates but not
|
|
5
|
+
// make them, so this writes the few X.509 structures it needs in DER
|
|
6
|
+
// itself. It is for the test host's forward check, never for a host.
|
|
7
|
+
import { generateKeyPairSync, randomBytes, sign, } from "node:crypto";
|
|
8
|
+
import { isIPv4 } from "node:net";
|
|
9
|
+
import { ForwardCAFile, ForwardCertFile, ForwardServerName, } from "./forward.js";
|
|
10
|
+
function len(n) {
|
|
11
|
+
if (n < 0x80)
|
|
12
|
+
return Buffer.from([n]);
|
|
13
|
+
const bytes = [];
|
|
14
|
+
for (let v = n; v > 0; v >>= 8)
|
|
15
|
+
bytes.unshift(v & 0xff);
|
|
16
|
+
return Buffer.from([0x80 | bytes.length, ...bytes]);
|
|
17
|
+
}
|
|
18
|
+
function tlv(tag, ...content) {
|
|
19
|
+
const body = Buffer.concat(content);
|
|
20
|
+
return Buffer.concat([Buffer.from([tag]), len(body.length), body]);
|
|
21
|
+
}
|
|
22
|
+
const seq = (...c) => tlv(0x30, ...c);
|
|
23
|
+
const set = (...c) => tlv(0x31, ...c);
|
|
24
|
+
const explicit = (n, ...c) => tlv(0xa0 | n, ...c);
|
|
25
|
+
function integer(b) {
|
|
26
|
+
let i = 0;
|
|
27
|
+
while (i < b.length - 1 && b[i] === 0)
|
|
28
|
+
i++;
|
|
29
|
+
const v = b.subarray(i);
|
|
30
|
+
return tlv(0x02, v[0] & 0x80 ? Buffer.concat([Buffer.from([0]), v]) : v);
|
|
31
|
+
}
|
|
32
|
+
function oid(dotted) {
|
|
33
|
+
const [a, b, ...rest] = dotted.split(".").map(Number);
|
|
34
|
+
const out = [a * 40 + b];
|
|
35
|
+
for (const n of rest) {
|
|
36
|
+
const groups = [];
|
|
37
|
+
for (let v = n;; v = Math.floor(v / 128)) {
|
|
38
|
+
groups.unshift(v % 128);
|
|
39
|
+
if (v < 128)
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
out.push(...groups.map((g, i) => (i < groups.length - 1 ? g | 0x80 : g)));
|
|
43
|
+
}
|
|
44
|
+
return tlv(0x06, Buffer.from(out));
|
|
45
|
+
}
|
|
46
|
+
function time(d) {
|
|
47
|
+
const iso = d.toISOString().replace(/[-:T]/g, "").slice(0, 14) + "Z";
|
|
48
|
+
// UTCTime until 2050, GeneralizedTime after, as RFC 5280 says.
|
|
49
|
+
return d.getUTCFullYear() < 2050
|
|
50
|
+
? tlv(0x17, Buffer.from(iso.slice(2)))
|
|
51
|
+
: tlv(0x18, Buffer.from(iso));
|
|
52
|
+
}
|
|
53
|
+
function name(cn) {
|
|
54
|
+
return seq(set(seq(oid("2.5.4.3"), tlv(0x0c, Buffer.from(cn)))));
|
|
55
|
+
}
|
|
56
|
+
function extension(id, critical, value) {
|
|
57
|
+
return seq(oid(id), ...(critical ? [tlv(0x01, Buffer.from([0xff]))] : []), tlv(0x04, value));
|
|
58
|
+
}
|
|
59
|
+
function ipBytes(ip) {
|
|
60
|
+
if (isIPv4(ip))
|
|
61
|
+
return Buffer.from(ip.split(".").map(Number));
|
|
62
|
+
const [head, tail] = ip.split("::");
|
|
63
|
+
const left = head ? head.split(":") : [];
|
|
64
|
+
const right = tail ? tail.split(":") : [];
|
|
65
|
+
const groups = tail === undefined
|
|
66
|
+
? left
|
|
67
|
+
: [
|
|
68
|
+
...left,
|
|
69
|
+
...Array(8 - left.length - right.length).fill("0"),
|
|
70
|
+
...right,
|
|
71
|
+
];
|
|
72
|
+
return Buffer.concat(groups.map((g) => {
|
|
73
|
+
const b = Buffer.alloc(2);
|
|
74
|
+
b.writeUInt16BE(parseInt(g, 16));
|
|
75
|
+
return b;
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
const ecdsaWithSHA256 = seq(oid("1.2.840.10045.4.3.2"));
|
|
79
|
+
const validity = 10 * 365 * 24 * 3600 * 1000;
|
|
80
|
+
function pem(type, der) {
|
|
81
|
+
const b64 = der.toString("base64").replace(/(.{64})/g, "$1\n");
|
|
82
|
+
return `-----BEGIN ${type}-----\n${b64.trimEnd()}\n-----END ${type}-----\n`;
|
|
83
|
+
}
|
|
84
|
+
/** The host's CA for one test's forwards (extension.ForwardAuthority). */
|
|
85
|
+
export class ForwardAuthority {
|
|
86
|
+
#key;
|
|
87
|
+
/** The CA's PEM certificate. */
|
|
88
|
+
certificate;
|
|
89
|
+
constructor() {
|
|
90
|
+
const { privateKey, publicKey } = generateKeyPairSync("ec", {
|
|
91
|
+
namedCurve: "prime256v1",
|
|
92
|
+
});
|
|
93
|
+
this.#key = privateKey;
|
|
94
|
+
this.certificate = this.#sign("Open Conduit toolbelt forward CA", publicKey, [
|
|
95
|
+
// basicConstraints: a CA, signing no other CA.
|
|
96
|
+
extension("2.5.29.19", true, seq(tlv(0x01, Buffer.from([0xff])), tlv(0x02, Buffer.from([0])))),
|
|
97
|
+
// keyUsage: keyCertSign.
|
|
98
|
+
extension("2.5.29.15", true, tlv(0x03, Buffer.from([0x02, 0x04]))),
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
#sign(cn, key, extensions) {
|
|
102
|
+
const now = Date.now();
|
|
103
|
+
const tbs = seq(explicit(0, tlv(0x02, Buffer.from([2]))), integer(Buffer.concat([Buffer.from([0x01]), randomBytes(15)])), ecdsaWithSHA256, name("Open Conduit toolbelt forward CA"), seq(time(new Date(now - 3600_000)), time(new Date(now + validity))), name(cn), key.export({ type: "spki", format: "der" }), explicit(3, seq(...extensions)));
|
|
104
|
+
const signature = sign("sha256", tbs, this.#key);
|
|
105
|
+
return pem("CERTIFICATE", seq(tbs, ecdsaWithSHA256, tlv(0x03, Buffer.from([0]), signature)));
|
|
106
|
+
}
|
|
107
|
+
#leaf(cn, server, ip) {
|
|
108
|
+
const { privateKey, publicKey } = generateKeyPairSync("ec", {
|
|
109
|
+
namedCurve: "prime256v1",
|
|
110
|
+
});
|
|
111
|
+
const sans = server
|
|
112
|
+
? [
|
|
113
|
+
tlv(0x82, Buffer.from(ForwardServerName)),
|
|
114
|
+
...(ip ? [tlv(0x87, ipBytes(ip))] : []),
|
|
115
|
+
]
|
|
116
|
+
: [];
|
|
117
|
+
const cert = this.#sign(cn, publicKey, [
|
|
118
|
+
// keyUsage: digitalSignature.
|
|
119
|
+
extension("2.5.29.15", true, tlv(0x03, Buffer.from([0x07, 0x80]))),
|
|
120
|
+
// extKeyUsage: serverAuth or clientAuth.
|
|
121
|
+
extension("2.5.29.37", false, seq(oid(server ? "1.3.6.1.5.5.7.3.1" : "1.3.6.1.5.5.7.3.2"))),
|
|
122
|
+
...(sans.length > 0 ? [extension("2.5.29.17", false, seq(...sans))] : []),
|
|
123
|
+
]);
|
|
124
|
+
return {
|
|
125
|
+
cert,
|
|
126
|
+
key: privateKey.export({ type: "sec1", format: "pem" }).toString(),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* One execution's forward (extension.ForwardAuthority.Credentials): the
|
|
131
|
+
* host's server certificate for ip and ForwardServerName, and the run's
|
|
132
|
+
* files, by name, to write beside the port file.
|
|
133
|
+
*/
|
|
134
|
+
credentials(execution, ip) {
|
|
135
|
+
const client = this.#leaf(execution, false);
|
|
136
|
+
return {
|
|
137
|
+
server: this.#leaf(ForwardServerName, true, ip),
|
|
138
|
+
files: {
|
|
139
|
+
[ForwardCAFile]: this.certificate,
|
|
140
|
+
[ForwardCertFile]: client.cert + client.key,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { Context } from "./extension.js";
|
|
2
|
+
import type { Authority, Dependency, Event, Key, Payload, Timer, WaitRequest, Work } from "./wire.js";
|
|
3
|
+
/** One firing of a recurring timer, for its kind's recurrence to answer. */
|
|
4
|
+
export interface RecurrenceCall {
|
|
5
|
+
/** The spec timer.arm gave, any JSON value. */
|
|
6
|
+
spec: unknown;
|
|
7
|
+
/** The timer that fired. */
|
|
8
|
+
fired: Timer;
|
|
9
|
+
/** The host's now, RFC 3339. */
|
|
10
|
+
now: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A recurrence's answer: the Event this firing yields (of the extension's
|
|
14
|
+
* own families, sourced in its namespace), if any, and the timer that
|
|
15
|
+
* fires next, if any. Neither ends the recurrence.
|
|
16
|
+
*/
|
|
17
|
+
export interface RecurrenceNext {
|
|
18
|
+
occurrence?: Event;
|
|
19
|
+
next?: Timer;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A calendar recurrence kind (sdk.RecurrenceFunc). It runs inside the
|
|
23
|
+
* timer's fire-and-rearm transaction: synchronous, with no host, pure and
|
|
24
|
+
* deterministic. What it throws aborts the transaction, and the timer stays
|
|
25
|
+
* due and is fired again.
|
|
26
|
+
*/
|
|
27
|
+
export type RecurrenceFn = (c: RecurrenceCall) => RecurrenceNext | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Whether an Event on a wait's dependency matches the filter the wait
|
|
30
|
+
* operation put on it (sdk.Extension.Filter). It runs inside the host's
|
|
31
|
+
* transaction: synchronous, with no host, pure and deterministic. What it
|
|
32
|
+
* throws is reported as the filter failing about that Event, which the
|
|
33
|
+
* host asks about again within a bound.
|
|
34
|
+
*/
|
|
35
|
+
export type DependencyFilterFn = (filter: Payload, event: Event) => boolean;
|
|
36
|
+
/** A call to another extension's operation, about to be served (extension.GateCall). */
|
|
37
|
+
export interface GateCall {
|
|
38
|
+
operation: string;
|
|
39
|
+
/** The extension that owns the operation. */
|
|
40
|
+
owner: string;
|
|
41
|
+
/** The caller's authority. */
|
|
42
|
+
authority: Authority;
|
|
43
|
+
input: Payload;
|
|
44
|
+
/** The capability a client made the call for for the agent; "" for the agent's own call. */
|
|
45
|
+
capability: string;
|
|
46
|
+
/** The operation's sentence for a person, "" when it declares none. */
|
|
47
|
+
wants: string;
|
|
48
|
+
}
|
|
49
|
+
/** What a gate parks the caller on instead of letting the call through: a wait, as a wait operation's. */
|
|
50
|
+
export interface GateWait {
|
|
51
|
+
wait: WaitRequest;
|
|
52
|
+
dependencies?: Dependency[];
|
|
53
|
+
/** What the caller is told, as a payload of one of the extension's schemas. */
|
|
54
|
+
output: Payload;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The extension's gate on other extensions' operations (extension.Gate):
|
|
58
|
+
* undefined lets the call through, a GateWait parks the caller instead, and
|
|
59
|
+
* an OperationError refuses it, reaching the caller verbatim. The host asks
|
|
60
|
+
* only about calls in the extension's scopes, and fails closed: anything
|
|
61
|
+
* else thrown, or no answer in time, refuses the call.
|
|
62
|
+
*/
|
|
63
|
+
export type OperationGateFn = (call: GateCall, ctx: Context) => GateWait | undefined | Promise<GateWait | undefined>;
|
|
64
|
+
/** A Work whose first attempt is about to start (extension.StartCall). */
|
|
65
|
+
export interface StartCall {
|
|
66
|
+
work: Work;
|
|
67
|
+
/** The binding it would run on. */
|
|
68
|
+
binding: string;
|
|
69
|
+
/** The Event that would be its first input. */
|
|
70
|
+
event: Event;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The extension's start gate (extension.StartGate): undefined lets the
|
|
74
|
+
* start through, a hold holds the Work until host.settleHold admits or
|
|
75
|
+
* cancels it. What it throws means it could not decide, and the start is
|
|
76
|
+
* refused for now. The host asks only about Work in the extension's scopes.
|
|
77
|
+
*/
|
|
78
|
+
export type StartGateFn = (call: StartCall, ctx: Context) => {
|
|
79
|
+
reason?: string;
|
|
80
|
+
} | undefined | Promise<{
|
|
81
|
+
reason?: string;
|
|
82
|
+
} | undefined>;
|
|
83
|
+
/** The correlation key and Event types that end a wait target (all of the extension's own families). */
|
|
84
|
+
export interface ResolvedTarget {
|
|
85
|
+
key: Key;
|
|
86
|
+
types: string[];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Resolves a named wait target of one kind in a scope ("job" for a
|
|
90
|
+
* `job:<name>` condition): undefined when the extension has no such target
|
|
91
|
+
* there. The host asks only in the extension's scopes, and claims nothing
|
|
92
|
+
* if any type isn't one of its families.
|
|
93
|
+
*/
|
|
94
|
+
export type TargetFn = (target: {
|
|
95
|
+
scopeId: string;
|
|
96
|
+
name: string;
|
|
97
|
+
}, ctx: Context) => ResolvedTarget | undefined | Promise<ResolvedTarget | undefined>;
|
|
98
|
+
/**
|
|
99
|
+
* Told an execution's container segment ended, so the extension revokes
|
|
100
|
+
* what it issued for it. It is best effort: the host doesn't wait for it,
|
|
101
|
+
* and the extension sweeps what it misses itself.
|
|
102
|
+
*/
|
|
103
|
+
export type SegmentEndFn = (executionId: string, ctx: Context) => void | Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Renders an Event of the extension's own families for an agent's prompt;
|
|
106
|
+
* undefined leaves it to the host's generic rendering, as does a failure.
|
|
107
|
+
*/
|
|
108
|
+
export type RenderFn = (event: Event, ctx: Context) => string | undefined | Promise<string | undefined>;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Calendar recurrences (recurrence.next), dependency filters
|
|
2
|
+
// (dependency.filter), operation and start gates (operation.gate,
|
|
3
|
+
// work.admit), wait target resolvers (target.resolve), segment ends
|
|
4
|
+
// (segment.ended) and event rendering (event.render), as protocol/sdk
|
|
5
|
+
// serves sdk.Extension's Recurrences, Filter, Gate, Start,
|
|
6
|
+
// TargetResolvers, SegmentEnded and Render (protocol/sdk/sdk.go).
|
|
7
|
+
//
|
|
8
|
+
// recurrence.next and dependency.filter run inside one of the host's
|
|
9
|
+
// transactions, so their functions are synchronous and get no host: the
|
|
10
|
+
// protocol forbids calling the host back from them, and the host refuses a
|
|
11
|
+
// call that does (reentrant_call). They must be pure and deterministic, as
|
|
12
|
+
// the host may ask again when its transaction runs again.
|
|
13
|
+
export {};
|