@mandateops/cli-linux-x64 0.0.0-stage → 0.7.1
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 +55 -0
- package/README.md +175 -2
- package/audit-lab/README.md +194 -0
- package/audit-lab/bin/mandateops +0 -0
- package/audit-lab/contracts/contact-plain-text.vectors.json +85 -0
- package/audit-lab/contracts/history-reading.md +185 -0
- package/audit-lab/contracts/history-reading.vectors.json +284 -0
- package/audit-lab/contracts/profiles.md +161 -0
- package/audit-lab/contracts/properties.md +512 -0
- package/audit-lab/contracts/sdk-config.md +155 -0
- package/audit-lab/contracts/system-under-test.md +385 -0
- package/audit-lab/contracts/test-provider.md +350 -0
- package/audit-lab/lab/compose.audit.yml +181 -0
- package/audit-lab/lab/config/fault-control.json +14 -0
- package/audit-lab/lab/examples/agents/Dockerfile +28 -0
- package/audit-lab/lab/examples/agents/codeloc.cjs +199 -0
- package/audit-lab/lab/examples/agents/load.cjs +9 -0
- package/audit-lab/lab/examples/agents/package-lock.json +2840 -0
- package/audit-lab/lab/examples/agents/package.json +9 -0
- package/audit-lab/lab/examples/agents/python/mandateops_codeloc.py +271 -0
- package/audit-lab/lab/examples/agents/python/sitecustomize.py +51 -0
- package/audit-lab/lab/examples/agents/requirements-python.txt +522 -0
- package/audit-lab/lab/examples/agents/telemetry-router.cjs +272 -0
- package/audit-lab/lab/examples/test-provider/.dockerignore +4 -0
- package/audit-lab/lab/examples/test-provider/Dockerfile +28 -0
- package/audit-lab/lab/examples/test-provider/bun.lock +405 -0
- package/audit-lab/lab/examples/test-provider/package.json +42 -0
- package/audit-lab/lab/examples/test-provider/src/amount.ts +32 -0
- package/audit-lab/lab/examples/test-provider/src/app.ts +538 -0
- package/audit-lab/lab/examples/test-provider/src/auth.ts +30 -0
- package/audit-lab/lab/examples/test-provider/src/barrier.ts +262 -0
- package/audit-lab/lab/examples/test-provider/src/codeloc.ts +27 -0
- package/audit-lab/lab/examples/test-provider/src/config.ts +311 -0
- package/audit-lab/lab/examples/test-provider/src/connect.ts +204 -0
- package/audit-lab/lab/examples/test-provider/src/declaration.ts +270 -0
- package/audit-lab/lab/examples/test-provider/src/declared.ts +198 -0
- package/audit-lab/lab/examples/test-provider/src/domain.ts +193 -0
- package/audit-lab/lab/examples/test-provider/src/egress.ts +396 -0
- package/audit-lab/lab/examples/test-provider/src/flow.ts +263 -0
- package/audit-lab/lab/examples/test-provider/src/gate.ts +301 -0
- package/audit-lab/lab/examples/test-provider/src/main.ts +31 -0
- package/audit-lab/lab/examples/test-provider/src/notify.ts +157 -0
- package/audit-lab/lab/examples/test-provider/src/owner.ts +60 -0
- package/audit-lab/lab/examples/test-provider/src/payment.ts +103 -0
- package/audit-lab/lab/examples/test-provider/src/profile.ts +417 -0
- package/audit-lab/lab/examples/test-provider/src/request.ts +201 -0
- package/audit-lab/lab/examples/test-provider/src/rows.ts +173 -0
- package/audit-lab/lab/examples/test-provider/src/schema.ts +114 -0
- package/audit-lab/lab/examples/test-provider/src/signature.ts +86 -0
- package/audit-lab/lab/examples/test-provider/src/staged.ts +318 -0
- package/audit-lab/lab/examples/test-provider/src/store.ts +432 -0
- package/audit-lab/lab/examples/test-provider/src/traffic.ts +50 -0
- package/audit-lab/lab/examples/test-provider/src/webauthn.d.ts +11 -0
- package/audit-lab/lab/examples/test-provider/src/wire.ts +153 -0
- package/audit-lab/lab/examples/test-provider/tsconfig.build.json +9 -0
- package/audit-lab/lab/examples/test-provider/tsconfig.json +24 -0
- package/audit-lab/lab/images/capture/Dockerfile +15 -0
- package/audit-lab/lab/images/capture/LICENSE +201 -0
- package/audit-lab/lab/images/fault-control/Dockerfile +13 -0
- package/audit-lab/lab/images/fault-control/LICENSE +21 -0
- package/audit-lab/lab/images/provider-store/Dockerfile +4 -0
- package/audit-lab/lab/images/provider-store/LICENSE +23 -0
- package/audit-lab/lab/images/telemetry/Dockerfile +14 -0
- package/audit-lab/lab/images/telemetry/LICENSE +201 -0
- package/audit-lab/lab/scripts/compose-down.sh +143 -0
- package/audit-lab/lab/scripts/compose-lib.sh +632 -0
- package/audit-lab/lab/scripts/compose-up.sh +240 -0
- package/package.json +16 -4
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { CONTROL_PREFIX, routeSchema } from "./domain.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Most money routes a flow declares. Each lists as one `unsupported` entry,
|
|
7
|
+
* so the declaration bounds that list and it is never capped.
|
|
8
|
+
*/
|
|
9
|
+
export const MAX_MONEY_ROUTES = 32;
|
|
10
|
+
|
|
11
|
+
/** Words a flow is described in: short, plain and bounded. */
|
|
12
|
+
const operationSchema = z.string().regex(/^[a-z][a-z -]{0,63}$/);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A format's declared flow: the money-moving call it imitates and the
|
|
16
|
+
* provider's other money-moving `POST` routes. The steps it answers are its
|
|
17
|
+
* own declared routes, so they are not repeated here.
|
|
18
|
+
*/
|
|
19
|
+
export const financialFlowSchema = z
|
|
20
|
+
.object({
|
|
21
|
+
operation: operationSchema,
|
|
22
|
+
moneyRoutes: z
|
|
23
|
+
.record(routeSchema, operationSchema)
|
|
24
|
+
.refine(
|
|
25
|
+
(routes) => Object.keys(routes).length <= MAX_MONEY_ROUTES,
|
|
26
|
+
`at most ${String(MAX_MONEY_ROUTES)} money routes`,
|
|
27
|
+
),
|
|
28
|
+
})
|
|
29
|
+
.strict();
|
|
30
|
+
export type FinancialFlow = z.infer<typeof financialFlowSchema>;
|
|
31
|
+
|
|
32
|
+
/** Distinct unplaced requests listed; requests beyond them are counted. */
|
|
33
|
+
export const MAX_AMBIGUOUS_ENTRIES = 32;
|
|
34
|
+
/** The longest request path a listed entry keeps. */
|
|
35
|
+
const MAX_LISTED_PATH = 128;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A money-moving call the format of the provider it reached does not
|
|
39
|
+
* imitate, and how often it came.
|
|
40
|
+
*/
|
|
41
|
+
export type UnsupportedCall = Readonly<{
|
|
42
|
+
/** The provider in the run's list the call reached. */
|
|
43
|
+
provider: string;
|
|
44
|
+
method: string;
|
|
45
|
+
route: string;
|
|
46
|
+
operation: string;
|
|
47
|
+
count: number;
|
|
48
|
+
}>;
|
|
49
|
+
|
|
50
|
+
/** A request the flow of the provider it reached cannot place, and how often. */
|
|
51
|
+
export type UnplacedCall = Readonly<{
|
|
52
|
+
/** The provider in the run's list the request reached. */
|
|
53
|
+
provider: string;
|
|
54
|
+
method: string;
|
|
55
|
+
path: string;
|
|
56
|
+
count: number;
|
|
57
|
+
}>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Every request the provider received, placed against the declared flow.
|
|
61
|
+
* The provider counts it, independently of the backend and of the test.
|
|
62
|
+
* Recognised requests do not show the backend routed every call here.
|
|
63
|
+
*/
|
|
64
|
+
export type FlowView = Readonly<{
|
|
65
|
+
/** The provider in the run's list this flow belongs to. */
|
|
66
|
+
provider: string;
|
|
67
|
+
operation: string;
|
|
68
|
+
expected: number;
|
|
69
|
+
steps: number;
|
|
70
|
+
unsupported: readonly UnsupportedCall[];
|
|
71
|
+
ambiguous: readonly UnplacedCall[];
|
|
72
|
+
/** Unplaced requests whose entry was beyond {@link MAX_AMBIGUOUS_ENTRIES}. */
|
|
73
|
+
omittedRequests: number;
|
|
74
|
+
}>;
|
|
75
|
+
|
|
76
|
+
/** What one request was, against the declared flow. */
|
|
77
|
+
export type Placement =
|
|
78
|
+
| Readonly<{ kind: "expected" }>
|
|
79
|
+
| Readonly<{ kind: "step" }>
|
|
80
|
+
| Readonly<{ kind: "unsupported"; route: string; operation: string }>
|
|
81
|
+
| Readonly<{ kind: "ambiguous" }>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* `path` with repeated slashes collapsed and any trailing slash removed, so
|
|
85
|
+
* `/v1//refunds/` places and lists as `/v1/refunds`.
|
|
86
|
+
*/
|
|
87
|
+
export function normalisedPath(path: string): string {
|
|
88
|
+
const collapsed = path.replace(/\/{2,}/g, "/");
|
|
89
|
+
return collapsed.length > 1 ? collapsed.replace(/\/$/, "") : collapsed;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Whether `path` sits on `route`, whose `:name` steps match one segment. */
|
|
93
|
+
function onRoute(route: string, path: string): boolean {
|
|
94
|
+
const routeSteps = route.split("/");
|
|
95
|
+
const pathSteps = path.split("/");
|
|
96
|
+
return (
|
|
97
|
+
routeSteps.length === pathSteps.length &&
|
|
98
|
+
routeSteps.every(
|
|
99
|
+
(step, index) =>
|
|
100
|
+
step === pathSteps[index] ||
|
|
101
|
+
(step.startsWith(":") && (pathSteps[index] ?? "") !== ""),
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** What the provider declares it serves, for placing a request. */
|
|
107
|
+
export type ServedRoutes = Readonly<{
|
|
108
|
+
payment: string;
|
|
109
|
+
steps: readonly string[];
|
|
110
|
+
flow: FinancialFlow;
|
|
111
|
+
}>;
|
|
112
|
+
|
|
113
|
+
/** One request as the provider received it. */
|
|
114
|
+
export type ReceivedRequest = Readonly<{
|
|
115
|
+
method: string;
|
|
116
|
+
/** The served route Fastify matched, if any. */
|
|
117
|
+
matched: string | undefined;
|
|
118
|
+
/** The normalised request path. */
|
|
119
|
+
path: string;
|
|
120
|
+
}>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Places one request. The payment route counts only as a `POST`, the one
|
|
124
|
+
* method it is served on.
|
|
125
|
+
*/
|
|
126
|
+
export function placed(
|
|
127
|
+
served: ServedRoutes,
|
|
128
|
+
{ method, matched, path }: ReceivedRequest,
|
|
129
|
+
): Placement {
|
|
130
|
+
if (matched === served.payment && method === "POST")
|
|
131
|
+
return { kind: "expected" };
|
|
132
|
+
if (matched !== undefined && served.steps.includes(matched))
|
|
133
|
+
return { kind: "step" };
|
|
134
|
+
if (method === "POST") {
|
|
135
|
+
const money = Object.entries(served.flow.moneyRoutes).find(([route]) =>
|
|
136
|
+
onRoute(route, path),
|
|
137
|
+
);
|
|
138
|
+
if (money)
|
|
139
|
+
return { kind: "unsupported", route: money[0], operation: money[1] };
|
|
140
|
+
}
|
|
141
|
+
return { kind: "ambiguous" };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Counts each request by placement, bounded, for `GET /_control/traffic`. */
|
|
145
|
+
export class FlowLog {
|
|
146
|
+
private expected = 0;
|
|
147
|
+
private steps = 0;
|
|
148
|
+
private omittedRequests = 0;
|
|
149
|
+
private readonly unsupported = new Map<string, UnsupportedCall>();
|
|
150
|
+
private readonly ambiguous = new Map<string, UnplacedCall>();
|
|
151
|
+
|
|
152
|
+
constructor(
|
|
153
|
+
private readonly provider: string,
|
|
154
|
+
private readonly served: ServedRoutes,
|
|
155
|
+
) {}
|
|
156
|
+
|
|
157
|
+
/** Records one request; a control request is the runner's and not placed. */
|
|
158
|
+
record(method: string, matched: string | undefined, url: string): void {
|
|
159
|
+
const path = normalisedPath(url.split("?")[0] ?? "");
|
|
160
|
+
if (path.startsWith(CONTROL_PREFIX)) return;
|
|
161
|
+
const placement = placed(this.served, { method, matched, path });
|
|
162
|
+
if (placement.kind === "expected") this.expected += 1;
|
|
163
|
+
else if (placement.kind === "step") this.steps += 1;
|
|
164
|
+
else if (placement.kind === "unsupported") {
|
|
165
|
+
const entry = `${method} ${placement.route}`;
|
|
166
|
+
const earlier = this.unsupported.get(entry);
|
|
167
|
+
this.unsupported.set(entry, {
|
|
168
|
+
provider: this.provider,
|
|
169
|
+
method,
|
|
170
|
+
route: placement.route,
|
|
171
|
+
operation: placement.operation,
|
|
172
|
+
count: (earlier?.count ?? 0) + 1,
|
|
173
|
+
});
|
|
174
|
+
} else {
|
|
175
|
+
const shown = path.slice(0, MAX_LISTED_PATH);
|
|
176
|
+
const entry = `${method} ${shown}`;
|
|
177
|
+
const earlier = this.ambiguous.get(entry);
|
|
178
|
+
if (earlier === undefined && this.ambiguous.size >= MAX_AMBIGUOUS_ENTRIES)
|
|
179
|
+
this.omittedRequests += 1;
|
|
180
|
+
else
|
|
181
|
+
this.ambiguous.set(entry, {
|
|
182
|
+
provider: this.provider,
|
|
183
|
+
method,
|
|
184
|
+
path: shown,
|
|
185
|
+
count: (earlier?.count ?? 0) + 1,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
view(): FlowView {
|
|
191
|
+
return {
|
|
192
|
+
provider: this.provider,
|
|
193
|
+
operation: this.served.flow.operation,
|
|
194
|
+
expected: this.expected,
|
|
195
|
+
steps: this.steps,
|
|
196
|
+
unsupported: [...this.unsupported.values()],
|
|
197
|
+
ambiguous: [...this.ambiguous.values()],
|
|
198
|
+
omittedRequests: this.omittedRequests,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
reset(): void {
|
|
203
|
+
this.expected = 0;
|
|
204
|
+
this.steps = 0;
|
|
205
|
+
this.omittedRequests = 0;
|
|
206
|
+
this.unsupported.clear();
|
|
207
|
+
this.ambiguous.clear();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** One provider's share of the run's flow: its format's call and its counts. */
|
|
212
|
+
export type ProviderFlowShare = Readonly<{
|
|
213
|
+
provider: string;
|
|
214
|
+
operation: string;
|
|
215
|
+
expected: number;
|
|
216
|
+
steps: number;
|
|
217
|
+
}>;
|
|
218
|
+
|
|
219
|
+
/** Every provider's flow as one reading, for `GET /_control/traffic`. */
|
|
220
|
+
export type RunFlowView = Readonly<{
|
|
221
|
+
/** Each provider's own operation and counts, in the run's order. */
|
|
222
|
+
providers: readonly ProviderFlowShare[];
|
|
223
|
+
expected: number;
|
|
224
|
+
steps: number;
|
|
225
|
+
unsupported: readonly UnsupportedCall[];
|
|
226
|
+
ambiguous: readonly UnplacedCall[];
|
|
227
|
+
omittedRequests: number;
|
|
228
|
+
}>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Every provider's flow as one view: each provider's operation and counts,
|
|
232
|
+
* the totals summed, and each listed call naming its provider. Requests to a
|
|
233
|
+
* host naming no provider join the unplaced list. That list keeps its one
|
|
234
|
+
* bound across providers; entries beyond it are counted, never dropped.
|
|
235
|
+
*/
|
|
236
|
+
export function combinedFlow(
|
|
237
|
+
views: readonly FlowView[],
|
|
238
|
+
unaddressed: FlowView,
|
|
239
|
+
): RunFlowView {
|
|
240
|
+
const ambiguous = [...views, unaddressed].flatMap((view) => view.ambiguous);
|
|
241
|
+
const listed = ambiguous.slice(0, MAX_AMBIGUOUS_ENTRIES);
|
|
242
|
+
const beyond = ambiguous
|
|
243
|
+
.slice(MAX_AMBIGUOUS_ENTRIES)
|
|
244
|
+
.reduce((total, call) => total + call.count, 0);
|
|
245
|
+
return {
|
|
246
|
+
providers: views.map(({ provider, operation, expected, steps }) => ({
|
|
247
|
+
provider,
|
|
248
|
+
operation,
|
|
249
|
+
expected,
|
|
250
|
+
steps,
|
|
251
|
+
})),
|
|
252
|
+
expected: views.reduce((total, view) => total + view.expected, 0),
|
|
253
|
+
steps: views.reduce((total, view) => total + view.steps, 0),
|
|
254
|
+
unsupported: views.flatMap((view) => view.unsupported),
|
|
255
|
+
ambiguous: listed,
|
|
256
|
+
omittedRequests:
|
|
257
|
+
beyond +
|
|
258
|
+
[...views, unaddressed].reduce(
|
|
259
|
+
(total, view) => total + view.omittedRequests,
|
|
260
|
+
0,
|
|
261
|
+
),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { BlockList, connect, createServer } from "node:net";
|
|
2
|
+
import type { Socket } from "node:net";
|
|
3
|
+
import { lookup } from "node:dns/promises";
|
|
4
|
+
import { networkInterfaces } from "node:os";
|
|
5
|
+
import {
|
|
6
|
+
HelloReader,
|
|
7
|
+
ReplyFaults,
|
|
8
|
+
relay,
|
|
9
|
+
toxicsAt,
|
|
10
|
+
type Hello,
|
|
11
|
+
} from "./egress.js";
|
|
12
|
+
|
|
13
|
+
/** The gate's two networks. */
|
|
14
|
+
type Side = "lab" | "customer";
|
|
15
|
+
/** Where a route listens and the one network its upstream may be on. */
|
|
16
|
+
type Route = Readonly<{
|
|
17
|
+
listenSide: Side;
|
|
18
|
+
listen: number;
|
|
19
|
+
upstreamSide: Side;
|
|
20
|
+
host: string;
|
|
21
|
+
port: number;
|
|
22
|
+
}>;
|
|
23
|
+
/** A name that resolves to the gate's own address on that side alone. */
|
|
24
|
+
type SideAlias = Readonly<{ side: Side; alias: string }>;
|
|
25
|
+
/** The gate's address on one network, as `os.networkInterfaces` gives it. */
|
|
26
|
+
type OwnAddress = Readonly<{ address: string; cidr: string }>;
|
|
27
|
+
/** The gate's address on a network, and which addresses lie on that network. */
|
|
28
|
+
type Attachment = Readonly<{ address: string; subnet: BlockList }>;
|
|
29
|
+
type Attachments = ReadonlyMap<Side, Attachment>;
|
|
30
|
+
|
|
31
|
+
const ROUTE =
|
|
32
|
+
/^(lab|customer):(\d+)=(lab|customer):([A-Za-z0-9][A-Za-z0-9._-]*):(\d+)$/;
|
|
33
|
+
const SIDE_ALIAS = /^(lab|customer)=([A-Za-z0-9][A-Za-z0-9._-]*)$/;
|
|
34
|
+
const EGRESS =
|
|
35
|
+
/^([a-z0-9][a-z0-9.-]*[a-z0-9])=lab:([A-Za-z0-9][A-Za-z0-9._-]*):(\d+)\/([A-Za-z0-9][A-Za-z0-9._-]*)$/;
|
|
36
|
+
/** The port a backend's HTTPS URL names; the gate listens on it for egress. */
|
|
37
|
+
const EGRESS_PORT = 443;
|
|
38
|
+
/** Longest wait for a client's ClientHello. */
|
|
39
|
+
const HELLO_TIMEOUT_MS = 10_000;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One allowed sandbox host: the exact name its ClientHello must carry, the
|
|
43
|
+
* lab-side fault-control proxy that alone connects to it, and that proxy's
|
|
44
|
+
* name, whose toxics carry the lab's reply faults.
|
|
45
|
+
*/
|
|
46
|
+
export type Egress = Readonly<{
|
|
47
|
+
host: string;
|
|
48
|
+
relay: string;
|
|
49
|
+
port: number;
|
|
50
|
+
proxy: string;
|
|
51
|
+
}>;
|
|
52
|
+
|
|
53
|
+
export function parseRoutes(text: string): Route[] {
|
|
54
|
+
return text.split(",").map((entry) => {
|
|
55
|
+
const match = ROUTE.exec(entry);
|
|
56
|
+
if (!match?.[1] || !match[2] || !match[3] || !match[4] || !match[5]) {
|
|
57
|
+
throw new Error(`invalid route ${entry}`);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
listenSide: match[1] as Side,
|
|
61
|
+
listen: Number(match[2]),
|
|
62
|
+
upstreamSide: match[3] as Side,
|
|
63
|
+
host: match[4],
|
|
64
|
+
port: Number(match[5]),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function parseSides(text: string): SideAlias[] {
|
|
70
|
+
return text.split(",").map((entry) => {
|
|
71
|
+
const match = SIDE_ALIAS.exec(entry);
|
|
72
|
+
if (!match?.[1] || !match[2]) throw new Error(`invalid side ${entry}`);
|
|
73
|
+
return { side: match[1] as Side, alias: match[2] };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `host=lab:relay:port/proxy` entries; empty text declares no egress. */
|
|
78
|
+
export function parseEgress(text: string): Egress[] {
|
|
79
|
+
if (text === "") return [];
|
|
80
|
+
const hosts = new Set<string>();
|
|
81
|
+
return text.split(",").map((entry) => {
|
|
82
|
+
const match = EGRESS.exec(entry);
|
|
83
|
+
if (!match?.[1] || !match[2] || !match[3] || !match[4]) {
|
|
84
|
+
throw new Error(`invalid egress ${entry}`);
|
|
85
|
+
}
|
|
86
|
+
if (hosts.has(match[1]))
|
|
87
|
+
throw new Error(`egress ${match[1]} declared twice`);
|
|
88
|
+
hosts.add(match[1]);
|
|
89
|
+
return {
|
|
90
|
+
host: match[1],
|
|
91
|
+
relay: match[2],
|
|
92
|
+
port: Number(match[3]),
|
|
93
|
+
proxy: match[4],
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The one of `own` that `answers` names; a name that is not the gate's fails. */
|
|
99
|
+
export function attachment(
|
|
100
|
+
answers: readonly string[],
|
|
101
|
+
own: readonly OwnAddress[],
|
|
102
|
+
): Attachment {
|
|
103
|
+
const found = own.find((entry) => answers.includes(entry.address));
|
|
104
|
+
const [network, prefix] = found?.cidr.split("/") ?? [];
|
|
105
|
+
if (!found || !network || !prefix) {
|
|
106
|
+
throw new Error(`no address of this gate among ${answers.join(" ")}`);
|
|
107
|
+
}
|
|
108
|
+
const subnet = new BlockList();
|
|
109
|
+
subnet.addSubnet(network, Number(prefix), "ipv4");
|
|
110
|
+
return { address: found.address, subnet };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The first of `answers` on the upstream's network. Docker's resolver answers
|
|
115
|
+
* from every network the gate is on and ignores case, so a name alone cannot
|
|
116
|
+
* say which side it reached; the address can.
|
|
117
|
+
*/
|
|
118
|
+
export function onSide(
|
|
119
|
+
answers: readonly string[],
|
|
120
|
+
side: Attachment,
|
|
121
|
+
): string | undefined {
|
|
122
|
+
return answers.find((address) => side.subnet.check(address, "ipv4"));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function addresses(host: string): Promise<string[]> {
|
|
126
|
+
const answers = await lookup(host, { all: true, family: 4 });
|
|
127
|
+
return answers.map((answer) => answer.address);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Learns each side before any customer container exists to answer for it. */
|
|
131
|
+
async function attach(sides: readonly SideAlias[]): Promise<Attachments> {
|
|
132
|
+
const own = Object.values(networkInterfaces())
|
|
133
|
+
.flat()
|
|
134
|
+
.flatMap((entry) =>
|
|
135
|
+
entry?.family === "IPv4" && !entry.internal && entry.cidr
|
|
136
|
+
? [{ address: entry.address, cidr: entry.cidr }]
|
|
137
|
+
: [],
|
|
138
|
+
);
|
|
139
|
+
const found = await Promise.all(
|
|
140
|
+
sides.map(
|
|
141
|
+
async ({ side, alias }) =>
|
|
142
|
+
[side, attachment(await addresses(alias), own)] as const,
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
return new Map(found);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Either side closing closes the other, so a cut reaches the client. */
|
|
149
|
+
function joined(client: Socket, upstream: Socket): void {
|
|
150
|
+
client.pipe(upstream);
|
|
151
|
+
upstream.pipe(client);
|
|
152
|
+
for (const [side, other] of [
|
|
153
|
+
[client, upstream],
|
|
154
|
+
[upstream, client],
|
|
155
|
+
] as const) {
|
|
156
|
+
side.on("error", () => other.destroy());
|
|
157
|
+
side.on("close", () => other.destroy());
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function sideOf(attachments: Attachments, side: Side): Attachment {
|
|
162
|
+
const found = attachments.get(side);
|
|
163
|
+
if (!found) throw new Error(`no address on the ${side} network`);
|
|
164
|
+
return found;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function forward(
|
|
168
|
+
client: Socket,
|
|
169
|
+
route: Route,
|
|
170
|
+
upstreamSide: Attachment,
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
client.on("error", () => client.destroy());
|
|
173
|
+
const answers = await addresses(route.host).catch(() => []);
|
|
174
|
+
const address = onSide(answers, upstreamSide);
|
|
175
|
+
if (!address) {
|
|
176
|
+
console.error(`${route.host} is not on the ${route.upstreamSide} network`);
|
|
177
|
+
client.destroy();
|
|
178
|
+
} else if (!client.destroyed) {
|
|
179
|
+
joined(client, connect(route.port, address));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** One allowed host's route and the reply faults its proxy carries. */
|
|
184
|
+
export type AllowedEgress = Readonly<{ egress: Egress; faults: ReplyFaults }>;
|
|
185
|
+
/** Opens the upstream of an allowed host's route, or says it cannot. */
|
|
186
|
+
export type Dial = (egress: Egress) => Promise<Socket | undefined>;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Accepts one egress connection: reads its ClientHello, forwards it only when
|
|
190
|
+
* it names an allowed host exactly, through that host's fault-control proxy
|
|
191
|
+
* on the lab network, and refuses anything else without connecting.
|
|
192
|
+
*/
|
|
193
|
+
export function admitEgress(
|
|
194
|
+
client: Socket,
|
|
195
|
+
allowed: ReadonlyMap<string, AllowedEgress>,
|
|
196
|
+
dial: Dial,
|
|
197
|
+
): void {
|
|
198
|
+
const reader = new HelloReader();
|
|
199
|
+
const refuse = (reason: string): void => {
|
|
200
|
+
console.error(`egress refused: ${reason}`);
|
|
201
|
+
client.destroy();
|
|
202
|
+
};
|
|
203
|
+
client.setTimeout(HELLO_TIMEOUT_MS, () => {
|
|
204
|
+
refuse("no ClientHello in time");
|
|
205
|
+
});
|
|
206
|
+
client.on("error", () => client.destroy());
|
|
207
|
+
const onData = (chunk: Buffer): void => {
|
|
208
|
+
let hello: Hello | undefined;
|
|
209
|
+
try {
|
|
210
|
+
hello = reader.push(chunk);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
refuse(error instanceof Error ? error.message : "malformed ClientHello");
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (!hello) return;
|
|
216
|
+
client.off("data", onData);
|
|
217
|
+
client.pause();
|
|
218
|
+
client.setTimeout(0);
|
|
219
|
+
const route = allowed.get(hello.host);
|
|
220
|
+
if (!route) {
|
|
221
|
+
refuse(`${hello.host} is not an allowed sandbox host`);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const { bytes } = hello;
|
|
225
|
+
void dial(route.egress).then((upstream) => {
|
|
226
|
+
if (!upstream) {
|
|
227
|
+
refuse(`${route.egress.relay} is not on the lab network`);
|
|
228
|
+
} else if (client.destroyed) {
|
|
229
|
+
upstream.destroy();
|
|
230
|
+
} else {
|
|
231
|
+
upstream.write(bytes);
|
|
232
|
+
relay(client, upstream, route.faults);
|
|
233
|
+
client.resume();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
client.on("data", onData);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Listens for egress on the customer network, when the deployment declares
|
|
242
|
+
* any. Reply faults are read from the egress proxy's own toxics.
|
|
243
|
+
*/
|
|
244
|
+
export function serveEgress(
|
|
245
|
+
egress: readonly Egress[],
|
|
246
|
+
faultControl: string,
|
|
247
|
+
attachments: Attachments,
|
|
248
|
+
): void {
|
|
249
|
+
if (egress.length === 0) return;
|
|
250
|
+
if (!/^http:\/\/[A-Za-z0-9][A-Za-z0-9._-]*:\d+$/.test(faultControl)) {
|
|
251
|
+
throw new Error("egress needs GATE_FAULT_CONTROL, the fault-control API");
|
|
252
|
+
}
|
|
253
|
+
const allowed = new Map(
|
|
254
|
+
egress.map((entry) => [
|
|
255
|
+
entry.host,
|
|
256
|
+
{
|
|
257
|
+
egress: entry,
|
|
258
|
+
faults: new ReplyFaults(
|
|
259
|
+
toxicsAt(`${faultControl}/proxies/${entry.proxy}/toxics`),
|
|
260
|
+
),
|
|
261
|
+
},
|
|
262
|
+
]),
|
|
263
|
+
);
|
|
264
|
+
const lab = sideOf(attachments, "lab");
|
|
265
|
+
// The relay only, and only at its address on the lab network.
|
|
266
|
+
const dial: Dial = async (route) => {
|
|
267
|
+
const answers = await addresses(route.relay).catch(() => []);
|
|
268
|
+
const address = onSide(answers, lab);
|
|
269
|
+
return address ? connect(route.port, address) : undefined;
|
|
270
|
+
};
|
|
271
|
+
createServer((client) => {
|
|
272
|
+
admitEgress(client, allowed, dial);
|
|
273
|
+
}).listen(EGRESS_PORT, sideOf(attachments, "customer").address);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The only way between a customer's isolated network and the lab: declared
|
|
278
|
+
* TCP routes, and egress to declared sandbox hosts, and nothing else. It has no control surface of its own. Each
|
|
279
|
+
* route listens on one network's address, so the other network cannot reach
|
|
280
|
+
* it, and connects only to an address on its declared upstream network.
|
|
281
|
+
*/
|
|
282
|
+
export function serve(
|
|
283
|
+
routes: readonly Route[],
|
|
284
|
+
attachments: Attachments,
|
|
285
|
+
): void {
|
|
286
|
+
for (const route of routes) {
|
|
287
|
+
const upstreamSide = sideOf(attachments, route.upstreamSide);
|
|
288
|
+
createServer((client) => {
|
|
289
|
+
void forward(client, route, upstreamSide);
|
|
290
|
+
}).listen(route.listen, sideOf(attachments, route.listenSide).address);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (import.meta.main) {
|
|
295
|
+
const routes = parseRoutes(process.env["GATE_ROUTES"] ?? "");
|
|
296
|
+
const sides = parseSides(process.env["GATE_SIDES"] ?? "");
|
|
297
|
+
const egress = parseEgress(process.env["GATE_EGRESS"] ?? "");
|
|
298
|
+
const attachments = await attach(sides);
|
|
299
|
+
serve(routes, attachments);
|
|
300
|
+
serveEgress(egress, process.env["GATE_FAULT_CONTROL"] ?? "", attachments);
|
|
301
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createApp } from "./app.js";
|
|
2
|
+
import { parseConfig } from "./config.js";
|
|
3
|
+
import { Store } from "./store.js";
|
|
4
|
+
|
|
5
|
+
import type { FastifyInstance } from "fastify";
|
|
6
|
+
|
|
7
|
+
let app: FastifyInstance | undefined;
|
|
8
|
+
try {
|
|
9
|
+
const config = parseConfig(process.env);
|
|
10
|
+
app = await createApp(config, await Store.open(config));
|
|
11
|
+
const runningApp = app;
|
|
12
|
+
for (const signal of ["SIGTERM", "SIGINT"] as const) {
|
|
13
|
+
process.once(signal, () => {
|
|
14
|
+
void runningApp.close().catch(() => {
|
|
15
|
+
process.exitCode = 1;
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
await app.listen({ host: config.listenHost, port: config.port });
|
|
20
|
+
console.log("Test provider ready");
|
|
21
|
+
} catch {
|
|
22
|
+
try {
|
|
23
|
+
await app?.close();
|
|
24
|
+
} catch {
|
|
25
|
+
console.error("Test provider cleanup failed");
|
|
26
|
+
}
|
|
27
|
+
console.error(
|
|
28
|
+
"Test provider could not start; check local configuration and dependencies",
|
|
29
|
+
);
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|