@open-conduit/sdk 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +12 -2
- 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 +127 -0
- package/dist/errors.js +180 -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 +855 -0
- package/dist/extension.js +1822 -0
- 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 +295 -0
- package/dist/host.js +486 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +31 -0
- package/dist/jsonrpc.d.ts +39 -0
- package/dist/jsonrpc.js +350 -0
- 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/schema.d.ts +71 -0
- package/dist/schema.js +252 -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 +756 -0
- package/dist/testing.js +2165 -0
- 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 +5359 -0
- package/dist/wire.js +2211 -0
- package/package.json +29 -4
package/dist/backup.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Context } from "./extension.js";
|
|
2
|
+
import type { Host } from "./host.js";
|
|
3
|
+
import { type Infer, type Type } from "./schema.js";
|
|
4
|
+
import type { BackupAbortParams, BackupFilesParams, BackupFilesResult, BackupListParams, BackupListResult, BackupReadParams, BackupReadResult, BackupTargetParams, BackupWriteParams, FieldDecl } from "./wire.js";
|
|
5
|
+
/** The file whose presence makes a stored bundle complete (extension.BackupManifest). The host puts it last. */
|
|
6
|
+
export declare const BackupManifest = "manifest.json";
|
|
7
|
+
/** Whether name can name a stored bundle (extension.ValidBackupBundle). */
|
|
8
|
+
export declare function validBackupBundle(name: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Whether p is a safe relative path inside a bundle
|
|
11
|
+
* (extension.ValidBackupPath): slash-separated plain names, none of them
|
|
12
|
+
* "." or "..". Refuse a put to anything else, so nothing is stored outside
|
|
13
|
+
* the bundle it names.
|
|
14
|
+
*/
|
|
15
|
+
export declare function validBackupPath(p: string): boolean;
|
|
16
|
+
/** One configured destination (extension.BackupTarget). */
|
|
17
|
+
export interface BackupTarget<C = unknown> {
|
|
18
|
+
/** The declared destination type. */
|
|
19
|
+
type: string;
|
|
20
|
+
/** The operator's name for the destination. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Its config, as the operator wrote it and validate accepted it. */
|
|
23
|
+
config: C;
|
|
24
|
+
}
|
|
25
|
+
/** One file of a bundle: its slash-separated path inside the bundle and its size in bytes (extension.BackupFile). */
|
|
26
|
+
export interface BackupFile {
|
|
27
|
+
path: string;
|
|
28
|
+
size: number;
|
|
29
|
+
}
|
|
30
|
+
/** Part of a stored file, and whether it reaches the file's end. */
|
|
31
|
+
export interface BackupChunk {
|
|
32
|
+
chunk: Uint8Array;
|
|
33
|
+
eof: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A backup destination type (extension.BackupDestination), which
|
|
37
|
+
* settings.backup.destinations[].type names: it stores backup bundles
|
|
38
|
+
* somewhere other than the host's disk. The bytes are opaque to it, and
|
|
39
|
+
* the host authenticates what it fetches against the operator's trust
|
|
40
|
+
* keys, so a destination is never trusted for a bundle's integrity or
|
|
41
|
+
* origin. Retention, versioning, object lock and encryption are the
|
|
42
|
+
* destination owner's policy, not the package's.
|
|
43
|
+
*
|
|
44
|
+
* What is thrown crosses as its code when it is a ProtocolError or an
|
|
45
|
+
* OperationError, and as internal otherwise.
|
|
46
|
+
*/
|
|
47
|
+
export interface BackupDestinationSpec<S extends Type<unknown>> {
|
|
48
|
+
/** The destination config's schema: validate (kind backup_destination) refuses a config it doesn't match. */
|
|
49
|
+
config?: S;
|
|
50
|
+
/** The config's fields, for adapters and the config editor (manifest backup_destinations[].fields). */
|
|
51
|
+
fields?: FieldDecl[];
|
|
52
|
+
/**
|
|
53
|
+
* Checks the schema can't express; throw to refuse the config. A
|
|
54
|
+
* ConfigError says which field is wrong and why; anything else thrown is
|
|
55
|
+
* reported only as invalid.
|
|
56
|
+
*/
|
|
57
|
+
validate?: (config: Infer<S>) => void | Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Stores one file, reading body to its end: exactly file.size bytes,
|
|
60
|
+
* after which the loop over body finishes. The file must become visible
|
|
61
|
+
* only once all of it is stored, so if body throws (the host's reader
|
|
62
|
+
* failed, or it aborted the upload) nothing of it may show. Putting a
|
|
63
|
+
* path again replaces it. Refuse a bundle or path that isn't
|
|
64
|
+
* validBackupBundle or validBackupPath. ctx.signal aborts when the
|
|
65
|
+
* upload is abandoned.
|
|
66
|
+
*/
|
|
67
|
+
put: (t: BackupTarget<Infer<S>>, bundle: string, file: BackupFile, body: AsyncIterable<Uint8Array>, ctx: Context) => void | Promise<void>;
|
|
68
|
+
/** The complete bundles: those holding BackupManifest. */
|
|
69
|
+
list: (t: BackupTarget<Infer<S>>, ctx: Context) => string[] | Promise<string[]>;
|
|
70
|
+
/**
|
|
71
|
+
* A bundle's stored files. A bundle the destination doesn't hold has
|
|
72
|
+
* none: that is an empty list, not an error, because the host asks
|
|
73
|
+
* before uploading.
|
|
74
|
+
*/
|
|
75
|
+
files: (t: BackupTarget<Infer<S>>, bundle: string, ctx: Context) => BackupFile[] | Promise<BackupFile[]>;
|
|
76
|
+
/** Up to length bytes of a file from offset, and whether they reach its end. */
|
|
77
|
+
read: (t: BackupTarget<Infer<S>>, bundle: string, path: string, offset: number, length: number, ctx: Context) => BackupChunk | Promise<BackupChunk>;
|
|
78
|
+
}
|
|
79
|
+
/** @internal How the server reaches a registered destination type. */
|
|
80
|
+
export interface BackupHandler {
|
|
81
|
+
validate: (config: unknown) => Promise<void>;
|
|
82
|
+
/** The target a request names, with its config checked against the schema. */
|
|
83
|
+
target: (p: BackupTargetParams) => BackupTarget;
|
|
84
|
+
spec: BackupDestinationSpec<Type<unknown>>;
|
|
85
|
+
}
|
|
86
|
+
/** @internal The handler for one destination type. */
|
|
87
|
+
export declare function backupHandler<S extends Type<unknown>>(spec: BackupDestinationSpec<S>, validateConfig: (schema: Type<unknown>, config: unknown, extra?: (config: never) => void | Promise<void>) => Promise<void>): BackupHandler;
|
|
88
|
+
/** @internal The uploads in progress on one connection, and backup_destination.* on them. */
|
|
89
|
+
export declare class BackupUploads {
|
|
90
|
+
#private;
|
|
91
|
+
constructor(find: (type: string) => BackupHandler | undefined, host: Host);
|
|
92
|
+
/** Ends every put still in progress, as the connection closes. */
|
|
93
|
+
close(): void;
|
|
94
|
+
write(p: BackupWriteParams): Promise<object>;
|
|
95
|
+
abort(p: BackupAbortParams): Promise<object>;
|
|
96
|
+
list(p: BackupListParams, ctx: Context): Promise<BackupListResult>;
|
|
97
|
+
files(p: BackupFilesParams, ctx: Context): Promise<BackupFilesResult>;
|
|
98
|
+
read(p: BackupReadParams, ctx: Context): Promise<BackupReadResult>;
|
|
99
|
+
}
|
package/dist/backup.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Backup destinations: backup_destination.* as protocol/sdk serves
|
|
2
|
+
// sdk.Extension.BackupDestinations (protocol/sdk/backup.go). The host puts
|
|
3
|
+
// a file in chunks under an upload ID it chooses; the first chunk starts
|
|
4
|
+
// the destination's put, which reads the chunks as they arrive, and the
|
|
5
|
+
// answer to the final one says the file is stored. An error, or an abort,
|
|
6
|
+
// ends the upload, and the put sees its body fail.
|
|
7
|
+
import { Code, ConfigError, ProtocolError } from "./errors.js";
|
|
8
|
+
import { Feed } from "./feed.js";
|
|
9
|
+
import { MaxChunkBytes } from "./jsonrpc.js";
|
|
10
|
+
import { matches } from "./schema.js";
|
|
11
|
+
/** The file whose presence makes a stored bundle complete (extension.BackupManifest). The host puts it last. */
|
|
12
|
+
export const BackupManifest = "manifest.json";
|
|
13
|
+
const bundleName = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
14
|
+
const pathPart = /^[A-Za-z0-9._-]{1,255}$/;
|
|
15
|
+
/** Whether name can name a stored bundle (extension.ValidBackupBundle). */
|
|
16
|
+
export function validBackupBundle(name) {
|
|
17
|
+
return bundleName.test(name);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether p is a safe relative path inside a bundle
|
|
21
|
+
* (extension.ValidBackupPath): slash-separated plain names, none of them
|
|
22
|
+
* "." or "..". Refuse a put to anything else, so nothing is stored outside
|
|
23
|
+
* the bundle it names.
|
|
24
|
+
*/
|
|
25
|
+
export function validBackupPath(p) {
|
|
26
|
+
if (p === "" || p.length > 1024)
|
|
27
|
+
return false;
|
|
28
|
+
return p
|
|
29
|
+
.split("/")
|
|
30
|
+
.every((part) => part !== "." && part !== ".." && pathPart.test(part));
|
|
31
|
+
}
|
|
32
|
+
/** @internal The handler for one destination type. */
|
|
33
|
+
export function backupHandler(spec, validateConfig) {
|
|
34
|
+
const schema = spec.config;
|
|
35
|
+
return {
|
|
36
|
+
validate: async (c) => {
|
|
37
|
+
if (schema === undefined) {
|
|
38
|
+
try {
|
|
39
|
+
await spec.validate?.((c ?? {}));
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
if (err instanceof ConfigError)
|
|
43
|
+
throw err;
|
|
44
|
+
throw new ProtocolError(Code.Invalid, "invalid extension config");
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
await validateConfig(schema, c, spec.validate);
|
|
49
|
+
},
|
|
50
|
+
target: (p) => {
|
|
51
|
+
const config = p.config.data ?? {};
|
|
52
|
+
// A stored config passed validate; one that no longer matches (a
|
|
53
|
+
// package upgrade changed the schema) reaches no destination.
|
|
54
|
+
if (schema !== undefined && !matches(schema, config))
|
|
55
|
+
throw new ProtocolError(Code.Invalid, "the backup destination's config no longer matches its schema");
|
|
56
|
+
return { type: p.type, name: p.name, config };
|
|
57
|
+
},
|
|
58
|
+
spec: spec,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function base64(b) {
|
|
62
|
+
return Buffer.from(b.buffer, b.byteOffset, b.byteLength).toString("base64");
|
|
63
|
+
}
|
|
64
|
+
function fromBase64(s) {
|
|
65
|
+
return s === undefined || s === ""
|
|
66
|
+
? new Uint8Array()
|
|
67
|
+
: new Uint8Array(Buffer.from(s, "base64"));
|
|
68
|
+
}
|
|
69
|
+
/** @internal The uploads in progress on one connection, and backup_destination.* on them. */
|
|
70
|
+
export class BackupUploads {
|
|
71
|
+
#uploads = new Map();
|
|
72
|
+
#find;
|
|
73
|
+
#host;
|
|
74
|
+
constructor(find, host) {
|
|
75
|
+
this.#find = find;
|
|
76
|
+
this.#host = host;
|
|
77
|
+
}
|
|
78
|
+
#handler(type) {
|
|
79
|
+
const h = this.#find(type);
|
|
80
|
+
if (h === undefined)
|
|
81
|
+
throw new ProtocolError(Code.NotFound, `no backup destination of type ${type}`);
|
|
82
|
+
return h;
|
|
83
|
+
}
|
|
84
|
+
/** Ends every put still in progress, as the connection closes. */
|
|
85
|
+
close() {
|
|
86
|
+
for (const [id, u] of this.#uploads) {
|
|
87
|
+
this.#uploads.delete(id);
|
|
88
|
+
u.feed.fail(new ProtocolError(Code.Cancelled, "the host went away"));
|
|
89
|
+
u.abort.abort();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async write(p) {
|
|
93
|
+
const chunk = fromBase64(p.chunk);
|
|
94
|
+
if (chunk.length > MaxChunkBytes || p.upload === "" || p.size < 0)
|
|
95
|
+
throw new ProtocolError(Code.Invalid, "an invalid backup chunk");
|
|
96
|
+
let u = this.#uploads.get(p.upload);
|
|
97
|
+
if (u === undefined) {
|
|
98
|
+
if (p.offset !== 0)
|
|
99
|
+
throw new ProtocolError(Code.Conflict, "no backup upload in progress under that ID");
|
|
100
|
+
const h = this.#handler(p.target.type);
|
|
101
|
+
const target = h.target(p.target);
|
|
102
|
+
const feed = new Feed();
|
|
103
|
+
// The put outlives each write, so it runs until the final chunk or
|
|
104
|
+
// an abort, not the first write's request.
|
|
105
|
+
const abort = new AbortController();
|
|
106
|
+
const done = (async () => h.spec.put(target, p.bundle, { path: p.path, size: p.size }, feed, {
|
|
107
|
+
host: this.#host,
|
|
108
|
+
signal: abort.signal,
|
|
109
|
+
}))();
|
|
110
|
+
// A put that stops early must not leave the host's write waiting.
|
|
111
|
+
done.then(() => feed.stop(), (err) => feed.stop(err));
|
|
112
|
+
u = { feed, abort, done, next: 0, size: p.size };
|
|
113
|
+
this.#uploads.set(p.upload, u);
|
|
114
|
+
}
|
|
115
|
+
const upload = u;
|
|
116
|
+
const fail = async (err) => {
|
|
117
|
+
if (this.#uploads.delete(p.upload)) {
|
|
118
|
+
upload.feed.fail(err);
|
|
119
|
+
upload.abort.abort();
|
|
120
|
+
await upload.done.catch(() => { });
|
|
121
|
+
}
|
|
122
|
+
throw err;
|
|
123
|
+
};
|
|
124
|
+
if (p.offset !== upload.next || upload.next + chunk.length > upload.size)
|
|
125
|
+
return fail(new ProtocolError(Code.Conflict, "backup chunk out of order or past the file's size"));
|
|
126
|
+
if (chunk.length > 0) {
|
|
127
|
+
try {
|
|
128
|
+
await upload.feed.push(chunk);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
// The put stopped: its own error, or that it stopped reading.
|
|
132
|
+
this.#uploads.delete(p.upload);
|
|
133
|
+
upload.abort.abort();
|
|
134
|
+
await upload.done;
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
upload.next += chunk.length;
|
|
138
|
+
}
|
|
139
|
+
if (!p.final)
|
|
140
|
+
return {};
|
|
141
|
+
if (upload.next !== upload.size)
|
|
142
|
+
return fail(new ProtocolError(Code.Conflict, "backup file ended before its size"));
|
|
143
|
+
this.#uploads.delete(p.upload);
|
|
144
|
+
upload.feed.end();
|
|
145
|
+
await upload.done;
|
|
146
|
+
return {};
|
|
147
|
+
}
|
|
148
|
+
async abort(p) {
|
|
149
|
+
const u = this.#uploads.get(p.upload);
|
|
150
|
+
if (u !== undefined) {
|
|
151
|
+
this.#uploads.delete(p.upload);
|
|
152
|
+
u.feed.fail(new ProtocolError(Code.Cancelled, "backup upload aborted"));
|
|
153
|
+
u.abort.abort();
|
|
154
|
+
await u.done.catch(() => { });
|
|
155
|
+
}
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
async list(p, ctx) {
|
|
159
|
+
const h = this.#handler(p.target.type);
|
|
160
|
+
const bundles = await h.spec.list(h.target(p.target), ctx);
|
|
161
|
+
return { bundles: [...bundles] };
|
|
162
|
+
}
|
|
163
|
+
async files(p, ctx) {
|
|
164
|
+
const h = this.#handler(p.target.type);
|
|
165
|
+
const files = await h.spec.files(h.target(p.target), p.bundle, ctx);
|
|
166
|
+
return { files: files.map((f) => ({ path: f.path, size: f.size })) };
|
|
167
|
+
}
|
|
168
|
+
async read(p, ctx) {
|
|
169
|
+
if (p.length <= 0 || p.length > MaxChunkBytes || p.offset < 0)
|
|
170
|
+
throw new ProtocolError(Code.Invalid, "a read is 1 to MaxChunkBytes bytes from a non-negative offset");
|
|
171
|
+
const h = this.#handler(p.target.type);
|
|
172
|
+
const got = await h.spec.read(h.target(p.target), p.bundle, p.path, p.offset, p.length, ctx);
|
|
173
|
+
return {
|
|
174
|
+
...(got.chunk.length > 0 ? { chunk: base64(got.chunk) } : {}),
|
|
175
|
+
...(got.eof ? { eof: true } : {}),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type TestContainerPolicy, type TestContainerProvider } from "./testing.js";
|
|
2
|
+
import type { ContainerCreated, SandboxLaunch, SandboxNeeds } from "./wire.js";
|
|
3
|
+
/** A container provider to check, with a launch it can plan and run to an exit (conformance.ContainerFixture). */
|
|
4
|
+
export interface ContainerFixture {
|
|
5
|
+
provider: TestContainerProvider;
|
|
6
|
+
/** The host label the provider lists for: the test host's containerHost. */
|
|
7
|
+
host: string;
|
|
8
|
+
/** A launch the provider can plan. Once started, it exits with code. */
|
|
9
|
+
policy: TestContainerPolicy;
|
|
10
|
+
needs: SandboxNeeds;
|
|
11
|
+
launch: SandboxLaunch;
|
|
12
|
+
code: number;
|
|
13
|
+
/** Runs a created container's argv and env to its end, as an executor's wrapper does, and resolves its exit status. */
|
|
14
|
+
start: (c: ContainerCreated) => Promise<number>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Checks the container contract the host's lifecycle relies on
|
|
18
|
+
* (conformance.CheckContainerProvider): a created container isn't running
|
|
19
|
+
* and hasn't started; it is listed for its execution and the host, and
|
|
20
|
+
* not for another execution; once started it reports its exit and when it
|
|
21
|
+
* started; it can be stopped once exited; removed, it is gone from the
|
|
22
|
+
* daemon that still answers, from listings and from a second removal.
|
|
23
|
+
*/
|
|
24
|
+
export declare function checkContainerProvider(f: ContainerFixture): Promise<void>;
|
|
25
|
+
/** A provider that places launches, with a launch it can plan at a placement and a directory at every site (conformance.PlacerFixture). Its sites need a POSIX sh. */
|
|
26
|
+
export interface PlacerFixture {
|
|
27
|
+
provider: TestContainerProvider;
|
|
28
|
+
policy: TestContainerPolicy;
|
|
29
|
+
needs: SandboxNeeds;
|
|
30
|
+
launch: SandboxLaunch;
|
|
31
|
+
dir: string;
|
|
32
|
+
/** What a command run in dir reports as its directory there; dir itself when left out. */
|
|
33
|
+
pwd?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks the placement contract (conformance.CheckContainerPlacer): a
|
|
37
|
+
* placement has an ID without a slash and a daemon; placing again
|
|
38
|
+
* preferring it gives it back; a command runs there in its directory,
|
|
39
|
+
* with its environment and stdin, and one that fails is its exit code,
|
|
40
|
+
* not an error; a placement the provider can't name is site_unavailable;
|
|
41
|
+
* and a container planned at the placement is created on its daemon.
|
|
42
|
+
*/
|
|
43
|
+
export declare function checkContainerPlacer(f: PlacerFixture): Promise<void>;
|
|
44
|
+
/** A provider whose containers reach the toolbelt through a forward the host serves, with the means to call through it (conformance.ForwarderFixture). */
|
|
45
|
+
export interface ForwarderFixture {
|
|
46
|
+
provider: TestContainerProvider;
|
|
47
|
+
/** A binding's container config the provider forwards for. */
|
|
48
|
+
config: unknown;
|
|
49
|
+
/**
|
|
50
|
+
* The address to listen at for the one the provider named, as the
|
|
51
|
+
* host does (that address itself when left out). A test whose address
|
|
52
|
+
* isn't on this machine listens on loopback instead.
|
|
53
|
+
*/
|
|
54
|
+
listenAt?: (address: string) => string;
|
|
55
|
+
/**
|
|
56
|
+
* Makes one toolbelt call as the provider's container would, through
|
|
57
|
+
* the forward whose port file is portFile, with the files the host
|
|
58
|
+
* writes beside it (ForwardCAFile, ForwardCertFile), and resolves the
|
|
59
|
+
* response's body.
|
|
60
|
+
*/
|
|
61
|
+
call: (portFile: string) => Promise<string>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Checks the forward's channel (conformance.CheckContainerForwarder): the
|
|
65
|
+
* provider names a literal loopback or private IP; its container reaches
|
|
66
|
+
* a forward the host serves there over mutual TLS under the host's CA;
|
|
67
|
+
* and it never calls a forward served in cleartext, or one whose
|
|
68
|
+
* certificate another CA signed, so a run on another machine can't be
|
|
69
|
+
* read or impersonated on the way.
|
|
70
|
+
*/
|
|
71
|
+
export declare function checkContainerForwarder(f: ForwarderFixture): Promise<void>;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// The container provider conformance checks, as conformance/container.go
|
|
2
|
+
// and conformance/container_forward.go hold a Go provider to them, for a
|
|
3
|
+
// TypeScript provider reached through the test host
|
|
4
|
+
// (TestHost.containerProvider). Each resolves when the provider keeps the
|
|
5
|
+
// contract and rejects with what it broke. The Go suite, run against the
|
|
6
|
+
// packaged provider under a real host, is the contract; these let a
|
|
7
|
+
// package's own tests hold it there first.
|
|
8
|
+
import { randomBytes } from "node:crypto";
|
|
9
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
10
|
+
import { createServer, } from "node:net";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { createServer as createTLSServer } from "node:tls";
|
|
14
|
+
import { ContainerLabel } from "./extension.js";
|
|
15
|
+
import { ForwardPortFile, forwardAddress } from "./forward.js";
|
|
16
|
+
import { ForwardAuthority } from "./forwardca.js";
|
|
17
|
+
import { containerDigest, } from "./testing.js";
|
|
18
|
+
const fail = (message) => {
|
|
19
|
+
throw new Error(message);
|
|
20
|
+
};
|
|
21
|
+
const show = (v) => JSON.stringify(v);
|
|
22
|
+
async function listed(p, owner, id, want) {
|
|
23
|
+
const got = (await p.list(owner)).some((s) => s.ref.id === id);
|
|
24
|
+
if (got !== want)
|
|
25
|
+
fail(`the listing for execution ${show(owner.execution_id)} has the container: ${got}, want ${want}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Checks the container contract the host's lifecycle relies on
|
|
29
|
+
* (conformance.CheckContainerProvider): a created container isn't running
|
|
30
|
+
* and hasn't started; it is listed for its execution and the host, and
|
|
31
|
+
* not for another execution; once started it reports its exit and when it
|
|
32
|
+
* started; it can be stopped once exited; removed, it is gone from the
|
|
33
|
+
* daemon that still answers, from listings and from a second removal.
|
|
34
|
+
*/
|
|
35
|
+
export async function checkContainerProvider(f) {
|
|
36
|
+
const p = f.provider;
|
|
37
|
+
const execution = "conformance-execution";
|
|
38
|
+
const owner = {
|
|
39
|
+
scope_id: "conformance-scope",
|
|
40
|
+
work_id: "conformance-work",
|
|
41
|
+
execution_id: execution,
|
|
42
|
+
};
|
|
43
|
+
const plan = await p
|
|
44
|
+
.plan(owner, f.policy, f.needs, f.launch)
|
|
45
|
+
.catch((err) => fail(`plan: ${String(err)}`));
|
|
46
|
+
if (!plan.image || plan.data.data === undefined)
|
|
47
|
+
fail("plan: no image or data");
|
|
48
|
+
plan.digest = containerDigest(plan.image, plan.data);
|
|
49
|
+
const labels = {
|
|
50
|
+
[ContainerLabel.Host]: f.host,
|
|
51
|
+
[ContainerLabel.Execution]: execution,
|
|
52
|
+
[ContainerLabel.Segment]: "1",
|
|
53
|
+
[ContainerLabel.Digest]: plan.digest,
|
|
54
|
+
};
|
|
55
|
+
const before = Date.now();
|
|
56
|
+
const created = await p
|
|
57
|
+
.create(plan, labels)
|
|
58
|
+
.catch((err) => fail(`create: ${String(err)}`));
|
|
59
|
+
if (!created.ref.id || !created.daemon || !created.argv?.length)
|
|
60
|
+
fail(`create: incomplete container ${show(created)}`);
|
|
61
|
+
const ref = { id: created.ref.id };
|
|
62
|
+
let st = await p
|
|
63
|
+
.inspect(ref)
|
|
64
|
+
.catch((err) => fail(`inspect a created container: ${String(err)}`));
|
|
65
|
+
if (st.gone || st.running || st.started_at)
|
|
66
|
+
fail(`a created container must exist, unstarted: ${show(st)}`);
|
|
67
|
+
if (st.daemon !== created.daemon)
|
|
68
|
+
fail(`inspect names daemon ${show(st.daemon)}, create ${show(created.daemon)}`);
|
|
69
|
+
if (st.ref.id !== created.ref.id)
|
|
70
|
+
fail(`inspect names ${show(st.ref.id)} for ${show(created.ref.id)}`);
|
|
71
|
+
if (st.image !== plan.image)
|
|
72
|
+
fail(`inspect reports image ${show(st.image)}, not the plan's ${show(plan.image)}`);
|
|
73
|
+
for (const [k, v] of Object.entries(labels))
|
|
74
|
+
if (st.ref.labels?.[k] !== v)
|
|
75
|
+
fail(`label ${k} is ${show(st.ref.labels?.[k])}, not ${show(v)}`);
|
|
76
|
+
await listed(p, owner, created.ref.id, true);
|
|
77
|
+
await listed(p, { scope_id: "", work_id: "", execution_id: "" }, created.ref.id, true);
|
|
78
|
+
await listed(p, { scope_id: "", work_id: "", execution_id: `another-${execution}` }, created.ref.id, false);
|
|
79
|
+
const code = await f.start(created);
|
|
80
|
+
if (code !== f.code)
|
|
81
|
+
fail(`the container's wrapper exited ${code}, not ${f.code}`);
|
|
82
|
+
st = await p
|
|
83
|
+
.inspect(ref)
|
|
84
|
+
.catch((err) => fail(`inspect an exited container: ${String(err)}`));
|
|
85
|
+
if (st.gone || st.running)
|
|
86
|
+
fail(`an exited container must exist, not running: ${show(st)}`);
|
|
87
|
+
if ((st.exit_code ?? 0) !== f.code)
|
|
88
|
+
fail(`inspect reports exit ${st.exit_code ?? 0}, not ${f.code}`);
|
|
89
|
+
if (!st.started_at || !(Date.parse(st.started_at) > before))
|
|
90
|
+
fail(`inspect reports a start (${st.started_at ?? "none"}) before the container was created (${new Date(before).toISOString()})`);
|
|
91
|
+
await p
|
|
92
|
+
.stop(ref, 0)
|
|
93
|
+
.catch((err) => fail(`stop an exited container: ${String(err)}`));
|
|
94
|
+
await p.remove(ref).catch((err) => fail(`remove: ${String(err)}`));
|
|
95
|
+
st = await p
|
|
96
|
+
.inspect(ref)
|
|
97
|
+
.catch((err) => fail(`inspect a removed container on a daemon that answers must be gone, not an error: ${String(err)}`));
|
|
98
|
+
if (!st.gone)
|
|
99
|
+
fail(`a removed container must be gone: ${show(st)}`);
|
|
100
|
+
if (st.daemon !== created.daemon)
|
|
101
|
+
fail(`a gone container's daemon is ${show(st.daemon)}, not ${show(created.daemon)}`);
|
|
102
|
+
await p
|
|
103
|
+
.remove(ref)
|
|
104
|
+
.catch((err) => fail(`removing a gone container is not an error: ${String(err)}`));
|
|
105
|
+
await listed(p, owner, created.ref.id, false);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Checks the placement contract (conformance.CheckContainerPlacer): a
|
|
109
|
+
* placement has an ID without a slash and a daemon; placing again
|
|
110
|
+
* preferring it gives it back; a command runs there in its directory,
|
|
111
|
+
* with its environment and stdin, and one that fails is its exit code,
|
|
112
|
+
* not an error; a placement the provider can't name is site_unavailable;
|
|
113
|
+
* and a container planned at the placement is created on its daemon.
|
|
114
|
+
*/
|
|
115
|
+
export async function checkContainerPlacer(f) {
|
|
116
|
+
const p = f.provider;
|
|
117
|
+
const owner = {
|
|
118
|
+
scope_id: "conformance-scope",
|
|
119
|
+
work_id: "conformance-work",
|
|
120
|
+
execution_id: "conformance-placed",
|
|
121
|
+
};
|
|
122
|
+
const at = await p
|
|
123
|
+
.place(owner, f.policy.config)
|
|
124
|
+
.catch((err) => fail(`place: ${String(err)}`));
|
|
125
|
+
if (!at.id || !at.daemon || at.id.includes("/"))
|
|
126
|
+
fail(`place: incomplete placement ${show(at)}`);
|
|
127
|
+
const again = await p
|
|
128
|
+
.place(owner, f.policy.config, at.id)
|
|
129
|
+
.catch((err) => fail(`place again: ${String(err)}`));
|
|
130
|
+
if (again.id !== at.id || again.daemon !== at.daemon)
|
|
131
|
+
fail(`placing again preferring ${show(at.id)} gave ${show(again)}`);
|
|
132
|
+
const res = await p
|
|
133
|
+
.siteRun(at.id, {
|
|
134
|
+
argv: [
|
|
135
|
+
"/bin/sh",
|
|
136
|
+
"-c",
|
|
137
|
+
`printf '%s|%s|' "$(pwd)" "$CONFORMANCE"; cat; exit 3`,
|
|
138
|
+
],
|
|
139
|
+
dir: f.dir,
|
|
140
|
+
env: ["CONFORMANCE=site"],
|
|
141
|
+
stdin: "in",
|
|
142
|
+
})
|
|
143
|
+
.catch((err) => fail(`site run: a command that ran and failed is an error: ${String(err)}`));
|
|
144
|
+
const want = `${f.pwd ?? f.dir}|site|in`;
|
|
145
|
+
const stdout = new TextDecoder().decode(res.stdout);
|
|
146
|
+
if (res.exitCode !== 3 || stdout !== want)
|
|
147
|
+
fail(`site run: exit ${res.exitCode}, output ${show(stdout)}; want 3 and ${show(want)}`);
|
|
148
|
+
const unknown = await p
|
|
149
|
+
.siteRun(`${at.id}-unknown`, { argv: ["/bin/sh", "-c", "true"] })
|
|
150
|
+
.then(() => "no error", (err) => err.code === "site_unavailable"
|
|
151
|
+
? ""
|
|
152
|
+
: String(err));
|
|
153
|
+
if (unknown !== "")
|
|
154
|
+
fail(`site run at a placement the provider never gave: ${unknown}, want site_unavailable`);
|
|
155
|
+
const plan = await p
|
|
156
|
+
.plan(owner, { ...f.policy, placement: at.id }, f.needs, f.launch)
|
|
157
|
+
.catch((err) => fail(`plan at the placement: ${String(err)}`));
|
|
158
|
+
plan.digest = containerDigest(plan.image, plan.data);
|
|
159
|
+
const created = await p
|
|
160
|
+
.create(plan, {
|
|
161
|
+
[ContainerLabel.Execution]: owner.execution_id,
|
|
162
|
+
[ContainerLabel.Segment]: "1",
|
|
163
|
+
[ContainerLabel.Digest]: plan.digest,
|
|
164
|
+
})
|
|
165
|
+
.catch((err) => fail(`create at the placement: ${String(err)}`));
|
|
166
|
+
try {
|
|
167
|
+
if (created.daemon !== at.daemon)
|
|
168
|
+
fail(`created on ${show(created.daemon)}, not at the placement's ${show(at.daemon)}`);
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
await p.remove(created.ref).catch(() => undefined);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function listen(server, host) {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
server.once("error", reject);
|
|
177
|
+
server.listen(0, host, () => {
|
|
178
|
+
server.off("error", reject);
|
|
179
|
+
resolve(server.address().port);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function closeServer(server, sockets) {
|
|
184
|
+
for (const s of sockets)
|
|
185
|
+
s.destroy();
|
|
186
|
+
return new Promise((resolve) => server.close(() => resolve()));
|
|
187
|
+
}
|
|
188
|
+
function answer(socket, body) {
|
|
189
|
+
socket.on("error", () => undefined);
|
|
190
|
+
// One HTTP response once the request's head has arrived; what isn't
|
|
191
|
+
// HTTP (a TLS handshake sent to a cleartext forward) is refused at once,
|
|
192
|
+
// as an HTTP server refuses it.
|
|
193
|
+
let head = "";
|
|
194
|
+
socket.on("data", (d) => {
|
|
195
|
+
head += d.toString("latin1");
|
|
196
|
+
if (!/^[A-Z]{0,7}( |$)/.test(head)) {
|
|
197
|
+
socket.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (!head.includes("\r\n\r\n"))
|
|
201
|
+
return;
|
|
202
|
+
socket.end(`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${Buffer.byteLength(body)}\r\nConnection: close\r\n\r\n${body}`);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Checks the forward's channel (conformance.CheckContainerForwarder): the
|
|
207
|
+
* provider names a literal loopback or private IP; its container reaches
|
|
208
|
+
* a forward the host serves there over mutual TLS under the host's CA;
|
|
209
|
+
* and it never calls a forward served in cleartext, or one whose
|
|
210
|
+
* certificate another CA signed, so a run on another machine can't be
|
|
211
|
+
* read or impersonated on the way.
|
|
212
|
+
*/
|
|
213
|
+
export async function checkContainerForwarder(f) {
|
|
214
|
+
const addr = await f.provider
|
|
215
|
+
.forward(f.config)
|
|
216
|
+
.catch((err) => fail(`forward: ${String(err)}`));
|
|
217
|
+
if (addr === "")
|
|
218
|
+
fail("forward: the fixture's config names no address to forward on");
|
|
219
|
+
try {
|
|
220
|
+
forwardAddress(addr);
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
fail(`forward: ${String(err)}`);
|
|
224
|
+
}
|
|
225
|
+
const at = f.listenAt?.(addr) ?? addr;
|
|
226
|
+
const execution = "conformance-forward";
|
|
227
|
+
const ca = new ForwardAuthority();
|
|
228
|
+
const creds = ca.credentials(execution, addr);
|
|
229
|
+
const dir = await mkdtemp(join(tmpdir(), "conformance-forward"));
|
|
230
|
+
try {
|
|
231
|
+
for (const [name, content] of Object.entries(creds.files))
|
|
232
|
+
await writeFile(join(dir, name), content, { mode: 0o600 });
|
|
233
|
+
const portFile = join(dir, ForwardPortFile);
|
|
234
|
+
const want = randomBytes(16).toString("hex");
|
|
235
|
+
const sockets = new Set();
|
|
236
|
+
const track = (s) => {
|
|
237
|
+
sockets.add(s);
|
|
238
|
+
s.on("close", () => sockets.delete(s));
|
|
239
|
+
};
|
|
240
|
+
// Serves the toolbelt at the provider's address and names its port
|
|
241
|
+
// in the port file, as the host does; the call answers whether it got
|
|
242
|
+
// through and what it read.
|
|
243
|
+
const called = async (when, server) => {
|
|
244
|
+
server.on("connection", track);
|
|
245
|
+
const port = await listen(server, at);
|
|
246
|
+
try {
|
|
247
|
+
await writeFile(portFile, `${port}\n`, { mode: 0o600 });
|
|
248
|
+
let got;
|
|
249
|
+
try {
|
|
250
|
+
got = await f.call(portFile);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (got !== want)
|
|
256
|
+
fail(`${when}, the call answered ${show(got)}, not the toolbelt's ${show(want)}`);
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
finally {
|
|
260
|
+
await closeServer(server, sockets);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
const verified = createTLSServer({
|
|
264
|
+
key: creds.server.key,
|
|
265
|
+
cert: creds.server.cert,
|
|
266
|
+
ca: [ca.certificate],
|
|
267
|
+
requestCert: true,
|
|
268
|
+
rejectUnauthorized: true,
|
|
269
|
+
minVersion: "TLSv1.3",
|
|
270
|
+
}, (s) => {
|
|
271
|
+
// Only the execution's own client certificate is answered.
|
|
272
|
+
if (s.getPeerCertificate().subject?.CN !== execution) {
|
|
273
|
+
s.destroy();
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
answer(s, want);
|
|
277
|
+
});
|
|
278
|
+
verified.on("tlsClientError", () => undefined);
|
|
279
|
+
if (!(await called("over the host's mutual TLS", verified)))
|
|
280
|
+
fail(`the container could not call the forward the host serves at ${addr} over mutual TLS under its CA`);
|
|
281
|
+
if (await called("in cleartext", createServer((s) => answer(s, want))))
|
|
282
|
+
fail("the container called a forward served in cleartext: the channel isn't encrypted or verified");
|
|
283
|
+
// A server whose certificate no CA of the host's signed, and which
|
|
284
|
+
// asks nothing of the client: only a client that verifies refuses it.
|
|
285
|
+
const other = new ForwardAuthority().credentials(execution, addr);
|
|
286
|
+
const impostor = createTLSServer({ key: other.server.key, cert: other.server.cert, minVersion: "TLSv1.2" }, (s) => answer(s, want));
|
|
287
|
+
impostor.on("tlsClientError", () => undefined);
|
|
288
|
+
if (await called("from an impostor", impostor))
|
|
289
|
+
fail("the container called a forward whose certificate the host's CA didn't sign: it doesn't verify the host");
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
await rm(dir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
}
|