@celljs/ta-gateway 4.16.0 → 4.17.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/README.en.md +5 -2
- package/README.md +5 -2
- package/esm/node/docker-engine.d.ts +5 -1
- package/esm/node/docker-engine.js +7 -1
- package/esm/node/docker-sandbox.d.ts +10 -1
- package/esm/node/docker-sandbox.js +24 -5
- package/esm/node/egress-proxy.d.ts +98 -0
- package/esm/node/egress-proxy.js +428 -0
- package/esm/node/environments.d.ts +5 -2
- package/esm/node/environments.js +4 -2
- package/esm/node/gateway-cli.js +12 -5
- package/esm/node/gateway-policy.d.ts +6 -1
- package/esm/node/gateway-policy.js +10 -1
- package/esm/node/gateway-quotas.d.ts +6 -1
- package/esm/node/gateway-quotas.js +17 -0
- package/esm/node/gateway-server.d.ts +31 -0
- package/esm/node/gateway-server.js +131 -13
- package/esm/node/gateway-service-util.d.ts +41 -0
- package/esm/node/gateway-service-util.js +178 -0
- package/esm/node/gateway-service.d.ts +5 -41
- package/esm/node/gateway-service.js +19 -173
- package/esm/node/gateway-socket.d.ts +1 -1
- package/esm/node/gateway-socket.js +1 -1
- package/esm/node/index.d.ts +4 -0
- package/esm/node/index.js +4 -0
- package/esm/node/kubernetes-api.d.ts +35 -0
- package/esm/node/kubernetes-api.js +115 -0
- package/esm/node/kubernetes-sandbox.d.ts +198 -0
- package/esm/node/kubernetes-sandbox.js +279 -0
- package/esm/node/sandbox-provider.d.ts +11 -1
- package/esm/node/sandbox-provider.js +2 -1
- package/esm/node/tenant-store.d.ts +15 -0
- package/esm/node/tenant-store.js +15 -0
- package/lib/node/docker-engine.d.ts +5 -1
- package/lib/node/docker-engine.js +7 -1
- package/lib/node/docker-sandbox.d.ts +10 -1
- package/lib/node/docker-sandbox.js +24 -5
- package/lib/node/egress-proxy.d.ts +99 -0
- package/lib/node/egress-proxy.js +433 -0
- package/lib/node/environments.d.ts +5 -2
- package/lib/node/environments.js +4 -2
- package/lib/node/gateway-cli.js +12 -5
- package/lib/node/gateway-policy.d.ts +6 -1
- package/lib/node/gateway-policy.js +10 -1
- package/lib/node/gateway-quotas.d.ts +6 -1
- package/lib/node/gateway-quotas.js +17 -0
- package/lib/node/gateway-server.d.ts +31 -0
- package/lib/node/gateway-server.js +129 -11
- package/lib/node/gateway-service-util.d.ts +42 -0
- package/lib/node/gateway-service-util.js +182 -0
- package/lib/node/gateway-service.d.ts +5 -41
- package/lib/node/gateway-service.js +41 -196
- package/lib/node/gateway-socket.d.ts +1 -1
- package/lib/node/gateway-socket.js +10 -10
- package/lib/node/index.d.ts +4 -0
- package/lib/node/index.js +4 -0
- package/lib/node/kubernetes-api.d.ts +36 -0
- package/lib/node/kubernetes-api.js +120 -0
- package/lib/node/kubernetes-sandbox.d.ts +199 -0
- package/lib/node/kubernetes-sandbox.js +283 -0
- package/lib/node/sandbox-provider.d.ts +11 -1
- package/lib/node/sandbox-provider.js +2 -1
- package/lib/node/tenant-store.d.ts +15 -0
- package/lib/node/tenant-store.js +15 -0
- package/package.json +4 -4
package/README.en.md
CHANGED
|
@@ -31,7 +31,7 @@ As pi connects models: a few built-in types, a file to configure one, and a modu
|
|
|
31
31
|
"idleMinutes": 0,
|
|
32
32
|
"providers": {
|
|
33
33
|
"docker": { "type": "docker", "name": "Docker", "image": "ta:latest", "memory": "4g", "cpus": 2, "pids": 1024 },
|
|
34
|
-
"k8s": { "type": "
|
|
34
|
+
"k8s": { "type": "kubernetes", "namespace": "ta", "image": "<registry>/ta:4.16.0", "storage": "10Gi", "runtimeClass": "gvisor" },
|
|
35
35
|
"e2b": { "type": "module", "path": "./e2b-sandbox.mjs", "name": "E2B" }
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -42,7 +42,8 @@ Without the file, there is the `docker` above only.
|
|
|
42
42
|
| Type | What it does |
|
|
43
43
|
| --- | --- |
|
|
44
44
|
| `docker` | A container per tenant (`ta-<tenant>`), its home a volume of its own (`/home/ta`: sessions, model accounts, workspace), with limits of memory, CPU and processes (`memory` 4g, `cpus` 2, `pids` 1024 by default), in a network whose containers cannot reach each other (`network`, default `ta-sandbox`, made with icc off when missing), without the capability `NET_RAW` (a tenant may sudo in its container: without raw sockets it cannot forge ARP to reach a neighbour on the bridge). Its port is published on `bind` only (default 127.0.0.1): the gateway is the way in. It calls the Docker Engine API (`host`: `unix://` or `tcp://`, default `DOCKER_HOST` or `/var/run/docker.sock`): no Docker CLI needed. `env` adds to the environment of every sandbox |
|
|
45
|
-
| `
|
|
45
|
+
| `kubernetes` | A `Sandbox` of [agent-sandbox](https://github.com/kubernetes-sigs/agent-sandbox) per tenant (`agents.x-k8s.io/v1beta1`; install its controller first): a pod of `ta serve` (uid 1000, no ServiceAccount token), its home in a PVC of its own (`storage`, default 10Gi; `storageClass`), `memory` default 4Gi, `cpus` default 2, `runtimeClass` for gVisor or Kata. Stopping suspends it (`operatingMode: Suspended`: the pod goes, the volume stays); when the environment the gateway gives changes, its pod is made again on the same volume. Its token and environment are in a Secret of the same name, owned by the `Sandbox`. It calls the Kubernetes API itself: in the cluster with the pod's ServiceAccount, outside it with `server`, `token` (`$VARIABLE` allowed) and `caFile`. A Helm chart is in [deploy/helm/ta](../../deploy/helm/ta) |
|
|
46
|
+
| `command` | A platform the gateway has no type for (a cloud's CLI, a script of one's own), as models.json takes a compatible endpoint: `start` makes or starts the sandbox and prints the address of its Ta service last; `stop` and `state` (prints running, stopped or missing) are optional. The commands have `TA_TENANT_ID`, `TA_TENANT_ACCOUNT` and `TA_SERVE_TOKEN` (which the Ta service there must take) in their environment |
|
|
46
47
|
| `module` | A provider of the operator's, as an extension of pi: the default export of `path` (relative to the directory of the gateway) is `(id, config, context) => SandboxProvider` (`start` / `stop` / `state`, see `sandbox-provider.ts`) |
|
|
47
48
|
| `ta` | A Ta service that runs already (`url`, `token`), which the gateway neither starts nor stops. Shared by all, it isolates nothing |
|
|
48
49
|
| `local` | A `ta serve` process per tenant on the machine of the gateway (`<gateway>/local/<tenant>`). No isolation: for trying out and tests |
|
|
@@ -53,6 +54,8 @@ Without the file, there is the `docker` above only.
|
|
|
53
54
|
|
|
54
55
|
## Deployment
|
|
55
56
|
|
|
57
|
+
A Docker Compose project and a Helm chart are in [deploy/](../../deploy/README.md). `--config <dir>` (`TA_GATEWAY_CONFIG`) reads `sandboxes.json` and `models.json` from a directory mounted read-only, the data staying in `--dir`.
|
|
58
|
+
|
|
56
59
|
The gateway can run in the image of Ta too (the Dockerfile of [apps/ta](../../apps/ta/README.en.md)), on the network of the host, with Docker's socket:
|
|
57
60
|
|
|
58
61
|
```bash
|
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ ta gateway link user:alice --name Alice # 一次性登录链接(一
|
|
|
31
31
|
"idleMinutes": 0,
|
|
32
32
|
"providers": {
|
|
33
33
|
"docker": { "type": "docker", "name": "Docker", "image": "ta:latest", "memory": "4g", "cpus": 2, "pids": 1024 },
|
|
34
|
-
"k8s": { "type": "
|
|
34
|
+
"k8s": { "type": "kubernetes", "namespace": "ta", "image": "<仓库>/ta:4.16.0", "storage": "10Gi", "runtimeClass": "gvisor" },
|
|
35
35
|
"e2b": { "type": "module", "path": "./e2b-sandbox.mjs", "name": "E2B" }
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -42,7 +42,8 @@ ta gateway link user:alice --name Alice # 一次性登录链接(一
|
|
|
42
42
|
| 类型 | 做什么 |
|
|
43
43
|
| --- | --- |
|
|
44
44
|
| `docker` | 每个租户一个容器(`ta-<租户>`),家目录是它自己的卷(`/home/ta`:会话、模型账号、工作区),限制内存、CPU、进程数(`memory` 默认 4g、`cpus` 默认 2、`pids` 默认 1024),放在容器之间互不相通的网络里(`network`,默认 `ta-sandbox`,不存在时创建并关闭 icc),并去掉 `NET_RAW` 能力(容器里能 sudo,没有原始套接字就伪造不了 ARP 去够同一网桥上的邻居)。端口只发布在 `bind`(默认 127.0.0.1)上,网关是唯一入口。直接调用 Docker Engine API(`host`:`unix://` 或 `tcp://`,默认 `DOCKER_HOST` 或 `/var/run/docker.sock`),网关不需要 Docker 命令行。`env` 给每个沙箱加环境变量 |
|
|
45
|
-
| `
|
|
45
|
+
| `kubernetes` | 每个租户一个 [agent-sandbox](https://github.com/kubernetes-sigs/agent-sandbox) 的 `Sandbox`(`agents.x-k8s.io/v1beta1`,集群要先装它的控制器):Pod 跑 `ta serve`(uid 1000,不挂 ServiceAccount token),家目录是它自己的 PVC(`storage` 默认 10Gi、`storageClass`),`memory` 默认 4Gi、`cpus` 默认 2,`runtimeClass` 选 gVisor / Kata。停止是 `operatingMode: Suspended`(Pod 删、卷留),网关给的环境变了就删 Pod 重建、挂回同一个卷。token 和环境放在同名 Secret 里(属于这个 `Sandbox`,一起回收)。直接调 Kubernetes API:在集群里用 Pod 的 ServiceAccount,在集群外给 `server`、`token`(可写 `$环境变量`)、`caFile`。Helm chart 见 [deploy/helm/ta](../../deploy/helm/ta) |
|
|
46
|
+
| `command` | 网关没有对应类型的平台(各家云的命令行、自己的脚本),像 models.json 接一个兼容的端点:`start` 创建或启动沙箱,并把它的 Ta 服务地址打印在最后一行;`stop`、`state`(打印 running / stopped / missing)可选。命令的环境里有 `TA_TENANT_ID`、`TA_TENANT_ACCOUNT`、`TA_SERVE_TOKEN`(沙箱里的 Ta 服务必须用它) |
|
|
46
47
|
| `module` | 运营方自己的提供方,像 pi 的扩展:`path`(相对网关目录)的默认导出是 `(id, config, context) => SandboxProvider`(`start` / `stop` / `state`,见 `sandbox-provider.ts`) |
|
|
47
48
|
| `ta` | 一个已经在运行的 Ta 服务(`url`、`token`),网关不启动也不停止它。所有人共用时没有隔离 |
|
|
48
49
|
| `local` | 网关所在机器上每个租户一个 `ta serve` 进程(`<网关目录>/local/<租户>`)。没有隔离,只用于试用和测试 |
|
|
@@ -53,6 +54,8 @@ ta gateway link user:alice --name Alice # 一次性登录链接(一
|
|
|
53
54
|
|
|
54
55
|
## 部署
|
|
55
56
|
|
|
57
|
+
现成的 Docker Compose 和 Helm chart 见 [deploy/](../../deploy/README.md)。`--config <目录>`(`TA_GATEWAY_CONFIG`)让 `sandboxes.json`、`models.json` 从只读挂载的目录读,数据仍写在 `--dir`。
|
|
58
|
+
|
|
56
59
|
网关本身也可以跑在 Ta 的镜像里([apps/ta](../../apps/ta/README.md) 的 Dockerfile),用宿主网络,挂上 Docker 的套接字:
|
|
57
60
|
|
|
58
61
|
```bash
|
|
@@ -45,7 +45,10 @@ export declare class DockerEngine {
|
|
|
45
45
|
logs(name: string, lines: number): Promise<string>;
|
|
46
46
|
/** Removed, its volumes kept; nothing when it is not there. */
|
|
47
47
|
remove(name: string): Promise<void>;
|
|
48
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* A bridge network, made when missing; `isolated`: its containers cannot reach each other. Its interface on the
|
|
50
|
+
* host has its name (when it fits one), for the firewall to name it (deploy/compose/isolate.sh: `-i ta-sandbox`).
|
|
51
|
+
*/
|
|
49
52
|
ensureNetwork(name: string, isolated: boolean): Promise<void>;
|
|
50
53
|
/** Pulled when it is not here; one built here (ta:latest) is. */
|
|
51
54
|
ensureImage(image: string): Promise<void>;
|
|
@@ -54,6 +57,7 @@ export interface DockerContainer {
|
|
|
54
57
|
Id: string;
|
|
55
58
|
Config?: {
|
|
56
59
|
Env?: string[];
|
|
60
|
+
Labels?: Record<string, string>;
|
|
57
61
|
};
|
|
58
62
|
State: {
|
|
59
63
|
Running: boolean;
|
|
@@ -143,7 +143,10 @@ export class DockerEngine {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
/**
|
|
146
|
+
/**
|
|
147
|
+
* A bridge network, made when missing; `isolated`: its containers cannot reach each other. Its interface on the
|
|
148
|
+
* host has its name (when it fits one), for the firewall to name it (deploy/compose/isolate.sh: `-i ta-sandbox`).
|
|
149
|
+
*/
|
|
147
150
|
async ensureNetwork(name, isolated) {
|
|
148
151
|
try {
|
|
149
152
|
await this.call('GET', `/networks/${encodeURIComponent(name)}`);
|
|
@@ -155,6 +158,9 @@ export class DockerEngine {
|
|
|
155
158
|
}
|
|
156
159
|
}
|
|
157
160
|
const options = isolated ? { 'com.docker.network.bridge.enable_icc': 'false' } : {};
|
|
161
|
+
if (/^[a-zA-Z0-9_.-]{1,15}$/.test(name)) {
|
|
162
|
+
options['com.docker.network.bridge.name'] = name;
|
|
163
|
+
}
|
|
158
164
|
await this.call('POST', '/networks/create', { Name: name, Driver: 'bridge', Options: options, Labels: { 'cell.ta.gateway': '1' } });
|
|
159
165
|
}
|
|
160
166
|
/** Pulled when it is not here; one built here (ta:latest) is. */
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
import { DockerEngine } from './docker-engine.js';
|
|
1
|
+
import { type DockerContainer, DockerEngine } from './docker-engine.js';
|
|
2
2
|
import { type SandboxEndpoint, type SandboxProvider, type SandboxProviderConfig, type SandboxState, type SandboxTenant, type SandboxUsage } from './sandbox-provider.js';
|
|
3
3
|
export declare namespace DockerSandboxUtil {
|
|
4
|
+
/** The label of a container that tells the environment the gateway gave it. */
|
|
5
|
+
const ENV_LABEL = "cell.ta.env";
|
|
6
|
+
/** Of the environment the gateway gives a sandbox, whatever its order. */
|
|
7
|
+
function envHash(env: Record<string, string> | undefined): string;
|
|
8
|
+
/**
|
|
9
|
+
* Whether a container was made with the environment the gateway gives it now: by its label, so that a variable
|
|
10
|
+
* taken away (the way out turned off) counts too; one made before the label, by its variables.
|
|
11
|
+
*/
|
|
12
|
+
function sameEnv(config: DockerContainer['Config'], want: Record<string, string> | undefined): boolean;
|
|
4
13
|
/** Whether a container has each variable of `env` as it is. */
|
|
5
14
|
function hasEnv(have: string[] | undefined, want: Record<string, string> | undefined): boolean;
|
|
6
15
|
/** `github:Octo.Cat` → `octo-cat`: a name of a host from the account, `ta` when nothing is left. */
|
|
@@ -1,7 +1,26 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { DockerEngine, DockerEngineUtil } from './docker-engine.js';
|
|
2
3
|
import { SandboxProviders, } from './sandbox-provider.js';
|
|
3
4
|
export var DockerSandboxUtil;
|
|
4
5
|
(function (DockerSandboxUtil) {
|
|
6
|
+
/** The label of a container that tells the environment the gateway gave it. */
|
|
7
|
+
DockerSandboxUtil.ENV_LABEL = 'cell.ta.env';
|
|
8
|
+
/** Of the environment the gateway gives a sandbox, whatever its order. */
|
|
9
|
+
function envHash(env) {
|
|
10
|
+
const entries = Object.entries(env !== null && env !== void 0 ? env : {}).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
11
|
+
return createHash('sha256').update(JSON.stringify(entries)).digest('hex').slice(0, 16);
|
|
12
|
+
}
|
|
13
|
+
DockerSandboxUtil.envHash = envHash;
|
|
14
|
+
/**
|
|
15
|
+
* Whether a container was made with the environment the gateway gives it now: by its label, so that a variable
|
|
16
|
+
* taken away (the way out turned off) counts too; one made before the label, by its variables.
|
|
17
|
+
*/
|
|
18
|
+
function sameEnv(config, want) {
|
|
19
|
+
var _a;
|
|
20
|
+
const made = (_a = config === null || config === void 0 ? void 0 : config.Labels) === null || _a === void 0 ? void 0 : _a[DockerSandboxUtil.ENV_LABEL];
|
|
21
|
+
return made !== undefined ? made === envHash(want) : hasEnv(config === null || config === void 0 ? void 0 : config.Env, want);
|
|
22
|
+
}
|
|
23
|
+
DockerSandboxUtil.sameEnv = sameEnv;
|
|
5
24
|
/** Whether a container has each variable of `env` as it is. */
|
|
6
25
|
function hasEnv(have, want) {
|
|
7
26
|
const set = new Set(have !== null && have !== void 0 ? have : []);
|
|
@@ -62,11 +81,11 @@ export class DockerSandbox {
|
|
|
62
81
|
this.engine = engine !== null && engine !== void 0 ? engine : new DockerEngine({ host: options.host });
|
|
63
82
|
}
|
|
64
83
|
async start(tenant) {
|
|
65
|
-
var _a, _b, _c
|
|
84
|
+
var _a, _b, _c;
|
|
66
85
|
const name = this.containerName(tenant);
|
|
67
86
|
let container = await this.engine.inspect(name);
|
|
68
|
-
if (container && !DockerSandboxUtil.
|
|
69
|
-
// Made
|
|
87
|
+
if (container && !DockerSandboxUtil.sameEnv(container.Config, tenant.env)) {
|
|
88
|
+
// Made with another environment (the models of the gateway, its way out): made again, its volume kept.
|
|
70
89
|
await this.engine.remove(name);
|
|
71
90
|
container = undefined;
|
|
72
91
|
}
|
|
@@ -80,7 +99,7 @@ export class DockerSandbox {
|
|
|
80
99
|
await this.engine.start(name);
|
|
81
100
|
container = await this.engine.inspect(name);
|
|
82
101
|
}
|
|
83
|
-
const port = (
|
|
102
|
+
const port = (_c = (_b = (_a = container === null || container === void 0 ? void 0 : container.NetworkSettings.Ports) === null || _a === void 0 ? void 0 : _a[PORT]) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.HostPort;
|
|
84
103
|
if (!port) {
|
|
85
104
|
throw new Error(`The container ${name} publishes no port for ${PORT}.`);
|
|
86
105
|
}
|
|
@@ -107,7 +126,7 @@ export class DockerSandbox {
|
|
|
107
126
|
// What the Ta of the tenant tells its clients its machine is, as the list of computers of the phone shows it.
|
|
108
127
|
Hostname: DockerSandboxUtil.hostname(tenant.account),
|
|
109
128
|
Env: [`TA_SERVE_TOKEN=${tenant.token}`, ...Object.entries({ ...this.options.env, ...tenant.env }).map(([key, value]) => `${key}=${value}`)],
|
|
110
|
-
Labels: { 'cell.ta.tenant': tenant.id, 'cell.ta.account': tenant.account },
|
|
129
|
+
Labels: { 'cell.ta.tenant': tenant.id, 'cell.ta.account': tenant.account, [DockerSandboxUtil.ENV_LABEL]: DockerSandboxUtil.envHash(tenant.env) },
|
|
111
130
|
ExposedPorts: { [PORT]: {} },
|
|
112
131
|
HostConfig: {
|
|
113
132
|
PortBindings: { [PORT]: [{ HostIp: this.options.bind, HostPort: '' }] },
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
2
|
+
import { BlockList } from 'node:net';
|
|
3
|
+
import type { Duplex } from 'node:stream';
|
|
4
|
+
import type { TaEgressRefusal } from '@celljs/ta-protocol';
|
|
5
|
+
/** `egress` of sandboxes.json: true, or the private networks the sandboxes may reach too. */
|
|
6
|
+
export type EgressConfig = boolean | {
|
|
7
|
+
networks?: string[];
|
|
8
|
+
};
|
|
9
|
+
export interface EgressSettings {
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
/** Private networks (CIDR) the sandboxes may reach too: never this machine, loopback or link-local. */
|
|
12
|
+
networks: string[];
|
|
13
|
+
}
|
|
14
|
+
export declare class EgressConfigError extends Error {
|
|
15
|
+
}
|
|
16
|
+
/** Where a sandbox wants to go. */
|
|
17
|
+
export interface EgressTarget {
|
|
18
|
+
host: string;
|
|
19
|
+
port: number;
|
|
20
|
+
}
|
|
21
|
+
/** Whom the credentials of a request are: their hosts (none: any public host), or why they are refused. */
|
|
22
|
+
export type EgressAuthorization = {
|
|
23
|
+
tenantId: string;
|
|
24
|
+
hosts?: string[];
|
|
25
|
+
} | {
|
|
26
|
+
status: 403 | 407;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
29
|
+
export interface EgressHooks {
|
|
30
|
+
authorize(user: string, password: string): EgressAuthorization;
|
|
31
|
+
}
|
|
32
|
+
export declare namespace EgressProxyUtil {
|
|
33
|
+
function settings(config: EgressConfig | undefined): EgressSettings;
|
|
34
|
+
/** `10.0.0.0/8`, `fd00::/8` or one address, as a list to check addresses against. */
|
|
35
|
+
function networksOf(networks: readonly string[]): BlockList;
|
|
36
|
+
/** The hosts an owner lets members reach, cleaned: lower-case, one each, at most 200. */
|
|
37
|
+
function hosts(value: unknown): string[];
|
|
38
|
+
/** The user and password of `Proxy-Authorization: Basic …`. */
|
|
39
|
+
function credentials(header: string | undefined): {
|
|
40
|
+
user: string;
|
|
41
|
+
password: string;
|
|
42
|
+
} | undefined;
|
|
43
|
+
/** `host:port` of CONNECT, `[::1]:443` too. */
|
|
44
|
+
function target(authority: string, fallbackPort: number): EgressTarget | undefined;
|
|
45
|
+
/** Why the policy refuses the target: not among its hosts, or a port of none of them; none: any public host, on 80 and 443. */
|
|
46
|
+
function refusal(hosts: readonly string[] | undefined, target: EgressTarget): 'host' | 'port' | undefined;
|
|
47
|
+
/** `::ffff:10.0.0.1` is 10.0.0.1. */
|
|
48
|
+
function plain(address: string): string;
|
|
49
|
+
/** Whether a sandbox may reach the address: a public one, or one of `networks`; never this machine. */
|
|
50
|
+
function reachable(address: string, networks: BlockList, own?: ReadonlySet<string>): boolean;
|
|
51
|
+
function ownAddresses(): Set<string>;
|
|
52
|
+
/**
|
|
53
|
+
* What a sandbox goes out with: the proxy of the gateway at `url`, by the credentials of its epoch, for the
|
|
54
|
+
* commands that read the variables of a proxy (curl, git, npm, pip, apt, and Node.js with NODE_USE_ENV_PROXY);
|
|
55
|
+
* the gateway itself, for its models, directly.
|
|
56
|
+
*/
|
|
57
|
+
function env(url: string, user: string, password: string): Record<string, string>;
|
|
58
|
+
function sameSecret(given: string, known: string): boolean;
|
|
59
|
+
function headers(given: IncomingHttpHeaders): Record<string, string | string[]>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The way out of the sandboxes (docs/design/ta-cloud.md 3.5): an HTTP proxy on the port of the gateway, CONNECT
|
|
63
|
+
* for https and absolute URLs for http. A sandbox comes with the credentials of its tenant and epoch (those of
|
|
64
|
+
* a sandbox replaced since are refused); the gateway reaches only public addresses (and the private networks of
|
|
65
|
+
* the configuration), resolved once and checked, never this machine nor the metadata service; members only the
|
|
66
|
+
* hosts of the policy. It counts the bytes each tenant moved, and keeps the latest refusals for the console.
|
|
67
|
+
*/
|
|
68
|
+
export declare class EgressProxy {
|
|
69
|
+
readonly settings: EgressSettings;
|
|
70
|
+
protected readonly hooks: EgressHooks;
|
|
71
|
+
protected readonly networks: BlockList;
|
|
72
|
+
protected readonly pending: Map<string, number>;
|
|
73
|
+
protected readonly refusals: Map<string, TaEgressRefusal[]>;
|
|
74
|
+
constructor(settings: EgressSettings, hooks: EgressHooks);
|
|
75
|
+
get enabled(): boolean;
|
|
76
|
+
/** The bytes moved since the last time, by tenant. */
|
|
77
|
+
flush(): Map<string, number>;
|
|
78
|
+
/** The latest refusals of a tenant, newest first. */
|
|
79
|
+
refused(tenantId: string): TaEgressRefusal[];
|
|
80
|
+
/** CONNECT: a tunnel to the target, once its tenant may reach it. */
|
|
81
|
+
connect(request: IncomingMessage, socket: Duplex, head: Buffer): Promise<void>;
|
|
82
|
+
/** An absolute `http://` URL: the request forwarded, once its tenant may reach the host. */
|
|
83
|
+
request(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
84
|
+
protected authorize(request: IncomingMessage): EgressAuthorization;
|
|
85
|
+
/** The address to connect to, resolved once and checked (no rebinding between the check and the connection), or why not. */
|
|
86
|
+
protected resolve(who: {
|
|
87
|
+
tenantId: string;
|
|
88
|
+
hosts?: string[];
|
|
89
|
+
}, target: EgressTarget): Promise<string | {
|
|
90
|
+
status: number;
|
|
91
|
+
message: string;
|
|
92
|
+
}>;
|
|
93
|
+
protected reachable(address: string, own: ReadonlySet<string>): boolean;
|
|
94
|
+
protected refuse(tenantId: string, target: EgressTarget, reason: TaEgressRefusal['reason']): void;
|
|
95
|
+
protected count(tenantId: string, bytes: number): void;
|
|
96
|
+
protected reply(socket: Duplex, status: number, message: string): void;
|
|
97
|
+
protected end(response: ServerResponse, status: number, message: string): void;
|
|
98
|
+
}
|