@celljs/ta-gateway 4.16.0 → 4.18.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 +13 -6
- 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 +35 -1
- package/esm/node/gateway-server.js +150 -16
- 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 +13 -6
- 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 +35 -1
- package/lib/node/gateway-server.js +148 -14
- 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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
2
|
-
import { type TaAccessInfo, type TaAgentActivity, type
|
|
2
|
+
import { type TaAccessInfo, type TaAgentActivity, type TaGatewayConsole, type TaGatewayInfo, type TaGatewayLimits, type TaGatewayPolicy, type TaGatewayRole, type TaGatewayTenant, type TaUsageTrend } from '@celljs/ta-protocol';
|
|
3
|
+
import { type EgressProxy } from './egress-proxy.js';
|
|
3
4
|
import type { Environments } from './environments.js';
|
|
4
5
|
import type { GatewayAudit } from './gateway-audit.js';
|
|
5
6
|
import { type GatewayPolicy } from './gateway-policy.js';
|
|
@@ -17,11 +18,6 @@ export interface GatewayCaller {
|
|
|
17
18
|
/** An owner or an admin. */
|
|
18
19
|
admin: boolean;
|
|
19
20
|
}
|
|
20
|
-
export declare class GatewayCallError extends Error {
|
|
21
|
-
readonly code: TaErrorCode;
|
|
22
|
-
readonly hint: string;
|
|
23
|
-
constructor(message: string, code: TaErrorCode, hint: string);
|
|
24
|
-
}
|
|
25
21
|
/** What the console says of the gateway itself. */
|
|
26
22
|
export interface GatewayMeta {
|
|
27
23
|
links: SignInLinks;
|
|
@@ -44,40 +40,7 @@ export interface GatewayParts {
|
|
|
44
40
|
};
|
|
45
41
|
meta: GatewayMeta;
|
|
46
42
|
models?: ModelProxy;
|
|
47
|
-
|
|
48
|
-
type Need = TaGatewayRole | 'anyone';
|
|
49
|
-
export declare namespace GatewayServiceUtil {
|
|
50
|
-
const DEFAULT_LOG_LINES = 200;
|
|
51
|
-
const MAX_LOG_LINES = 2000;
|
|
52
|
-
/**
|
|
53
|
-
* Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
|
|
54
|
-
* `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
|
|
55
|
-
* (GatewayService.manage).
|
|
56
|
-
*/
|
|
57
|
-
const RULES: Record<string, Record<string, Need>>;
|
|
58
|
-
/** What an administrator did or looked at of others: kept in the audit. */
|
|
59
|
-
const AUDITED: Set<string>;
|
|
60
|
-
function need(service: string, name: string): Need | undefined;
|
|
61
|
-
function allowed(role: TaGatewayRole, need: Need): boolean;
|
|
62
|
-
/** As many lines as asked, within bounds. */
|
|
63
|
-
function lines(value: unknown): number;
|
|
64
|
-
/** Days of a trend, 1 to 31. */
|
|
65
|
-
function days(value: unknown): number;
|
|
66
|
-
/** The services the gateway answers itself, not the Ta of the tenant. */
|
|
67
|
-
const SERVICES: string[];
|
|
68
|
-
function owns(method: unknown): method is string;
|
|
69
|
-
/** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
|
|
70
|
-
function mayOwn(text: string): boolean;
|
|
71
|
-
/** The calls that send the agents a message, which the limit of messages a minute counts. */
|
|
72
|
-
const PROMPTS: string[];
|
|
73
|
-
function mayPrompt(text: string): boolean;
|
|
74
|
-
/** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
|
|
75
|
-
function error(id: unknown, message: string, code: TaErrorCode, hint: string): string;
|
|
76
|
-
function result(id: unknown, result: unknown): string;
|
|
77
|
-
function forbidden(message: string, hint: string): GatewayCallError;
|
|
78
|
-
function choice(value: unknown, own: boolean, providers: string[]): TaEnvironmentChoice | undefined;
|
|
79
|
-
/** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
|
|
80
|
-
function detail(method: string, params: unknown[]): string | undefined;
|
|
43
|
+
egress?: EgressProxy;
|
|
81
44
|
}
|
|
82
45
|
/**
|
|
83
46
|
* The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
|
|
@@ -95,6 +58,7 @@ export declare class GatewayService {
|
|
|
95
58
|
protected readonly hooks: GatewayParts['hooks'];
|
|
96
59
|
protected readonly meta: GatewayMeta;
|
|
97
60
|
protected readonly models?: ModelProxy;
|
|
61
|
+
protected readonly egress?: EgressProxy;
|
|
98
62
|
constructor(parts: GatewayParts);
|
|
99
63
|
call(caller: GatewayCaller, method: string, params: unknown[]): Promise<unknown>;
|
|
100
64
|
protected handle(caller: GatewayCaller, service: string, name: string, params: unknown[]): Promise<unknown>;
|
|
@@ -106,6 +70,7 @@ export declare class GatewayService {
|
|
|
106
70
|
/** The tenants with what their sandboxes take and their agents do, read together: a slow one holds the console a second, not a second each. */
|
|
107
71
|
protected console(caller: GatewayCaller): Promise<TaGatewayConsole>;
|
|
108
72
|
protected policyView(): TaGatewayPolicy;
|
|
73
|
+
protected setEgressHosts(value: unknown): TaGatewayPolicy;
|
|
109
74
|
protected tenant(id: unknown): Tenant;
|
|
110
75
|
/** A tenant the caller may change: an owner any; an admin a member's only, not their own, nor another admin's or an owner's. */
|
|
111
76
|
protected manage(caller: GatewayCaller, id: unknown): Tenant;
|
|
@@ -138,4 +103,3 @@ export declare class GatewayService {
|
|
|
138
103
|
*/
|
|
139
104
|
protected signInLink(account: unknown, name: unknown): string;
|
|
140
105
|
}
|
|
141
|
-
export {};
|
|
@@ -1,181 +1,12 @@
|
|
|
1
1
|
import { AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
|
|
2
|
-
import {
|
|
2
|
+
import { TaErrorCode, } from '@celljs/ta-protocol';
|
|
3
3
|
import { Redactor } from '@celljs/ta-protocol/lib/common/redactor';
|
|
4
|
+
import { EgressConfigError, EgressProxyUtil } from './egress-proxy.js';
|
|
4
5
|
import { GatewayPolicyUtil, PolicyError } from './gateway-policy.js';
|
|
6
|
+
import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
|
|
5
7
|
import { GatewayUsageUtil } from './gateway-usage.js';
|
|
6
8
|
import { ModelProxyUtil } from './model-proxy.js';
|
|
7
|
-
import { TaSandboxUtil } from './ta-sandbox.js';
|
|
8
|
-
export class GatewayCallError extends Error {
|
|
9
|
-
constructor(message, code, hint) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.code = code;
|
|
12
|
-
this.hint = hint;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
9
|
const DAY = 24 * 3600000;
|
|
16
|
-
export var GatewayServiceUtil;
|
|
17
|
-
(function (GatewayServiceUtil) {
|
|
18
|
-
GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
|
|
19
|
-
GatewayServiceUtil.MAX_LOG_LINES = 2000;
|
|
20
|
-
/**
|
|
21
|
-
* Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
|
|
22
|
-
* `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
|
|
23
|
-
* (GatewayService.manage).
|
|
24
|
-
*/
|
|
25
|
-
GatewayServiceUtil.RULES = {
|
|
26
|
-
TaGatewayServer: {
|
|
27
|
-
getGateway: 'anyone',
|
|
28
|
-
setEnvironment: 'anyone',
|
|
29
|
-
restartEnvironment: 'anyone',
|
|
30
|
-
listTenants: 'admin',
|
|
31
|
-
getConsole: 'admin',
|
|
32
|
-
getTenantAgents: 'admin',
|
|
33
|
-
getAgentActivity: 'admin',
|
|
34
|
-
getTenantLogs: 'admin',
|
|
35
|
-
getUsageTrend: 'admin',
|
|
36
|
-
getPolicy: 'admin',
|
|
37
|
-
getAudit: 'admin',
|
|
38
|
-
startTenant: 'admin',
|
|
39
|
-
stopTenant: 'admin',
|
|
40
|
-
restartTenant: 'admin',
|
|
41
|
-
createSignInLink: 'admin',
|
|
42
|
-
setTenantLimits: 'admin',
|
|
43
|
-
grantMinutes: 'admin',
|
|
44
|
-
setSuspended: 'admin',
|
|
45
|
-
setDefaultLimits: 'owner',
|
|
46
|
-
setRole: 'owner',
|
|
47
|
-
createAccessToken: 'anyone',
|
|
48
|
-
},
|
|
49
|
-
TaAccessServer: {
|
|
50
|
-
getAccess: 'anyone',
|
|
51
|
-
signOut: 'anyone',
|
|
52
|
-
bindUrl: 'admin',
|
|
53
|
-
allow: 'admin',
|
|
54
|
-
dismiss: 'admin',
|
|
55
|
-
disallow: 'admin',
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
/** What an administrator did or looked at of others: kept in the audit. */
|
|
59
|
-
GatewayServiceUtil.AUDITED = new Set([
|
|
60
|
-
'startTenant',
|
|
61
|
-
'stopTenant',
|
|
62
|
-
'restartTenant',
|
|
63
|
-
'getTenantLogs',
|
|
64
|
-
'getAgentActivity',
|
|
65
|
-
'createSignInLink',
|
|
66
|
-
'setTenantLimits',
|
|
67
|
-
'grantMinutes',
|
|
68
|
-
'setSuspended',
|
|
69
|
-
'setDefaultLimits',
|
|
70
|
-
'setRole',
|
|
71
|
-
'allow',
|
|
72
|
-
'dismiss',
|
|
73
|
-
'disallow',
|
|
74
|
-
]);
|
|
75
|
-
/** By name, own keys only: a method such as `toString` is no rule. */
|
|
76
|
-
const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
|
|
77
|
-
function need(service, name) {
|
|
78
|
-
var _a;
|
|
79
|
-
return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
|
|
80
|
-
}
|
|
81
|
-
GatewayServiceUtil.need = need;
|
|
82
|
-
function allowed(role, need) {
|
|
83
|
-
return need === 'anyone' || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need);
|
|
84
|
-
}
|
|
85
|
-
GatewayServiceUtil.allowed = allowed;
|
|
86
|
-
/** As many lines as asked, within bounds. */
|
|
87
|
-
function lines(value) {
|
|
88
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
|
|
89
|
-
return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
|
|
90
|
-
}
|
|
91
|
-
GatewayServiceUtil.lines = lines;
|
|
92
|
-
/** Days of a trend, 1 to 31. */
|
|
93
|
-
function days(value) {
|
|
94
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
|
|
95
|
-
return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
|
|
96
|
-
}
|
|
97
|
-
GatewayServiceUtil.days = days;
|
|
98
|
-
/** The services the gateway answers itself, not the Ta of the tenant. */
|
|
99
|
-
GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
|
|
100
|
-
function owns(method) {
|
|
101
|
-
return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
|
|
102
|
-
}
|
|
103
|
-
GatewayServiceUtil.owns = owns;
|
|
104
|
-
/** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
|
|
105
|
-
function mayOwn(text) {
|
|
106
|
-
return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
|
|
107
|
-
}
|
|
108
|
-
GatewayServiceUtil.mayOwn = mayOwn;
|
|
109
|
-
/** The calls that send the agents a message, which the limit of messages a minute counts. */
|
|
110
|
-
GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
|
|
111
|
-
function mayPrompt(text) {
|
|
112
|
-
return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
|
|
113
|
-
}
|
|
114
|
-
GatewayServiceUtil.mayPrompt = mayPrompt;
|
|
115
|
-
/** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
|
|
116
|
-
function error(id, message, code, hint) {
|
|
117
|
-
return JSON.stringify({ jsonrpc: '2.0', id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
|
|
118
|
-
}
|
|
119
|
-
GatewayServiceUtil.error = error;
|
|
120
|
-
function result(id, result) {
|
|
121
|
-
return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
|
|
122
|
-
}
|
|
123
|
-
GatewayServiceUtil.result = result;
|
|
124
|
-
function invalid(message, hint) {
|
|
125
|
-
return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
|
|
126
|
-
}
|
|
127
|
-
function forbidden(message, hint) {
|
|
128
|
-
return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
|
|
129
|
-
}
|
|
130
|
-
GatewayServiceUtil.forbidden = forbidden;
|
|
131
|
-
function choice(value, own, providers) {
|
|
132
|
-
var _a;
|
|
133
|
-
const given = (value !== null && value !== void 0 ? value : {});
|
|
134
|
-
if (typeof given.provider === 'string') {
|
|
135
|
-
if (!providers.includes(given.provider)) {
|
|
136
|
-
throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
|
|
137
|
-
}
|
|
138
|
-
return { provider: given.provider };
|
|
139
|
-
}
|
|
140
|
-
if (given.type === 'ta') {
|
|
141
|
-
if (!own) {
|
|
142
|
-
throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
|
|
143
|
-
}
|
|
144
|
-
let url;
|
|
145
|
-
try {
|
|
146
|
-
url = TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
|
|
150
|
-
}
|
|
151
|
-
if (typeof given.token !== 'string' || !given.token.trim()) {
|
|
152
|
-
throw invalid('A Ta service of your own is reached with its access token.', 'Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.');
|
|
153
|
-
}
|
|
154
|
-
return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
|
|
155
|
-
}
|
|
156
|
-
throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
|
|
157
|
-
}
|
|
158
|
-
GatewayServiceUtil.choice = choice;
|
|
159
|
-
/** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
|
|
160
|
-
function detail(method, params) {
|
|
161
|
-
const [, second] = params;
|
|
162
|
-
switch (method) {
|
|
163
|
-
case 'setTenantLimits':
|
|
164
|
-
case 'setDefaultLimits':
|
|
165
|
-
return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
|
|
166
|
-
case 'grantMinutes':
|
|
167
|
-
case 'getTenantLogs':
|
|
168
|
-
return String(second !== null && second !== void 0 ? second : '');
|
|
169
|
-
case 'setSuspended':
|
|
170
|
-
return second ? 'suspended' : 'let go';
|
|
171
|
-
case 'setRole':
|
|
172
|
-
return String(second);
|
|
173
|
-
default:
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
GatewayServiceUtil.detail = detail;
|
|
178
|
-
})(GatewayServiceUtil || (GatewayServiceUtil = {}));
|
|
179
10
|
/**
|
|
180
11
|
* The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
|
|
181
12
|
* accounts are those that may have a tenant here. Each method needs a role (GatewayServiceUtil.RULES); what an
|
|
@@ -184,6 +15,7 @@ export var GatewayServiceUtil;
|
|
|
184
15
|
export class GatewayService {
|
|
185
16
|
constructor(parts) {
|
|
186
17
|
this.models = parts.models;
|
|
18
|
+
this.egress = parts.egress;
|
|
187
19
|
this.flow = parts.flow;
|
|
188
20
|
this.tenants = parts.tenants;
|
|
189
21
|
this.environments = parts.environments;
|
|
@@ -235,6 +67,7 @@ export class GatewayService {
|
|
|
235
67
|
setSuspended: () => this.setSuspended(caller, first, second),
|
|
236
68
|
setDefaultLimits: () => this.setDefaultLimits(first),
|
|
237
69
|
setRole: () => this.setRole(caller, first, second),
|
|
70
|
+
setEgressHosts: () => this.setEgressHosts(first),
|
|
238
71
|
createAccessToken: () => this.accessToken(caller, first),
|
|
239
72
|
}
|
|
240
73
|
: {
|
|
@@ -352,7 +185,20 @@ export class GatewayService {
|
|
|
352
185
|
};
|
|
353
186
|
}
|
|
354
187
|
policyView() {
|
|
355
|
-
|
|
188
|
+
var _a;
|
|
189
|
+
return this.policy.view(this.environments.settings.idleMinutes, (_a = this.egress) === null || _a === void 0 ? void 0 : _a.settings);
|
|
190
|
+
}
|
|
191
|
+
setEgressHosts(value) {
|
|
192
|
+
try {
|
|
193
|
+
this.policy.setEgressHosts(EgressProxyUtil.hosts(value));
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (error instanceof EgressConfigError) {
|
|
197
|
+
throw new GatewayCallError(error.message, TaErrorCode.INVALID_ARGUMENT, 'Send hosts such as registry.npmjs.org, *.github.com or example.com:8443.');
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
return this.policyView();
|
|
356
202
|
}
|
|
357
203
|
tenant(id) {
|
|
358
204
|
const tenant = typeof id === 'string' ? this.tenants.get(id) : undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
2
2
|
import type { Environments } from './environments.js';
|
|
3
3
|
import type { GatewayQuotas } from './gateway-quotas.js';
|
|
4
|
-
import {
|
|
4
|
+
import type { GatewayCaller, GatewayService } from './gateway-service.js';
|
|
5
5
|
/**
|
|
6
6
|
* One browser's WebSocket: the calls of the gateway's services answered here, everything else forwarded to
|
|
7
7
|
* the Ta of its tenant, whose token only the gateway has, and back. Until that Ta answers, what the browser
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TA_WEBSOCKET_PATH, TaErrorCode } from '@celljs/ta-protocol';
|
|
2
|
-
import { GatewayCallError, GatewayServiceUtil } from './gateway-service.js';
|
|
2
|
+
import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
|
|
3
3
|
/** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
|
|
4
4
|
const MAX_QUEUED = 256;
|
|
5
5
|
/** And as many bytes at most: a message may be 32 MB, and 256 of them would hold the gateway's memory for one browser. */
|
package/esm/node/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './command-sandbox.js';
|
|
2
2
|
export * from './docker-engine.js';
|
|
3
3
|
export * from './docker-sandbox.js';
|
|
4
|
+
export * from './egress-proxy.js';
|
|
4
5
|
export * from './environments.js';
|
|
5
6
|
export * from './gateway-cli.js';
|
|
6
7
|
export * from './gateway-server.js';
|
|
7
8
|
export * from './gateway-service.js';
|
|
9
|
+
export * from './gateway-service-util.js';
|
|
8
10
|
export * from './gateway-socket.js';
|
|
11
|
+
export * from './kubernetes-api.js';
|
|
12
|
+
export * from './kubernetes-sandbox.js';
|
|
9
13
|
export * from './local-sandbox.js';
|
|
10
14
|
export * from './sandbox-provider.js';
|
|
11
15
|
export * from './sign-in-links.js';
|
package/esm/node/index.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './command-sandbox.js';
|
|
2
2
|
export * from './docker-engine.js';
|
|
3
3
|
export * from './docker-sandbox.js';
|
|
4
|
+
export * from './egress-proxy.js';
|
|
4
5
|
export * from './environments.js';
|
|
5
6
|
export * from './gateway-cli.js';
|
|
6
7
|
export * from './gateway-server.js';
|
|
7
8
|
export * from './gateway-service.js';
|
|
9
|
+
export * from './gateway-service-util.js';
|
|
8
10
|
export * from './gateway-socket.js';
|
|
11
|
+
export * from './kubernetes-api.js';
|
|
12
|
+
export * from './kubernetes-sandbox.js';
|
|
9
13
|
export * from './local-sandbox.js';
|
|
10
14
|
export * from './sandbox-provider.js';
|
|
11
15
|
export * from './sign-in-links.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare class KubernetesError extends Error {
|
|
2
|
+
readonly status: number;
|
|
3
|
+
constructor(status: number, message: string);
|
|
4
|
+
}
|
|
5
|
+
/** The calls a provider makes: a JSON body or text back; KubernetesError for an answer not 2xx. */
|
|
6
|
+
export interface KubernetesCalls {
|
|
7
|
+
call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
|
|
8
|
+
text(path: string): Promise<string>;
|
|
9
|
+
}
|
|
10
|
+
export interface KubernetesApiOptions {
|
|
11
|
+
/** https://<host>:<port> of the API server. */
|
|
12
|
+
server: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
/** The CA of the server, PEM. */
|
|
15
|
+
ca?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The API of a cluster over HTTPS, without a client library: what a provider of sandboxes needs is a few
|
|
19
|
+
* resources by their paths.
|
|
20
|
+
*/
|
|
21
|
+
export declare class KubernetesApi implements KubernetesCalls {
|
|
22
|
+
protected readonly options: KubernetesApiOptions;
|
|
23
|
+
/** In a pod of the cluster: its service account, and the namespace it runs in. */
|
|
24
|
+
static inCluster(env?: Record<string, string | undefined>, read?: (path: string) => string): KubernetesApiOptions & {
|
|
25
|
+
namespace: string;
|
|
26
|
+
};
|
|
27
|
+
constructor(options: KubernetesApiOptions);
|
|
28
|
+
call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
|
|
29
|
+
text(path: string): Promise<string>;
|
|
30
|
+
protected send(method: string, path: string, body?: string, type?: string): Promise<string>;
|
|
31
|
+
}
|
|
32
|
+
export declare namespace KubernetesUtil {
|
|
33
|
+
/** A quantity of Kubernetes (`4Gi`, `500M`, `250m`, `123456n`) as a number of its unit (bytes, CPUs). */
|
|
34
|
+
function quantity(value: string | number | undefined): number | undefined;
|
|
35
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { request as httpRequest } from 'node:http';
|
|
3
|
+
import { request as httpsRequest } from 'node:https';
|
|
4
|
+
/** Where a pod finds the API of its cluster: its service account's token, CA and namespace. */
|
|
5
|
+
const ACCOUNT = '/var/run/secrets/kubernetes.io/serviceaccount';
|
|
6
|
+
export class KubernetesError extends Error {
|
|
7
|
+
constructor(status, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The API of a cluster over HTTPS, without a client library: what a provider of sandboxes needs is a few
|
|
14
|
+
* resources by their paths.
|
|
15
|
+
*/
|
|
16
|
+
export class KubernetesApi {
|
|
17
|
+
/** In a pod of the cluster: its service account, and the namespace it runs in. */
|
|
18
|
+
static inCluster(env = process.env, read = (path) => readFileSync(path, 'utf8')) {
|
|
19
|
+
var _a;
|
|
20
|
+
const host = env.KUBERNETES_SERVICE_HOST;
|
|
21
|
+
if (!host) {
|
|
22
|
+
throw new Error('Not in a pod of Kubernetes (no KUBERNETES_SERVICE_HOST): give "server" and "token" of the cluster.');
|
|
23
|
+
}
|
|
24
|
+
const port = (_a = env.KUBERNETES_SERVICE_PORT) !== null && _a !== void 0 ? _a : '443';
|
|
25
|
+
return {
|
|
26
|
+
server: `https://${host.includes(':') ? `[${host}]` : host}:${port}`,
|
|
27
|
+
token: read(`${ACCOUNT}/token`).trim(),
|
|
28
|
+
ca: read(`${ACCOUNT}/ca.crt`),
|
|
29
|
+
namespace: read(`${ACCOUNT}/namespace`).trim(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
}
|
|
35
|
+
async call(method, path, body, type = 'application/json') {
|
|
36
|
+
const text = await this.send(method, path, body === undefined ? undefined : JSON.stringify(body), type);
|
|
37
|
+
return (text ? JSON.parse(text) : undefined);
|
|
38
|
+
}
|
|
39
|
+
text(path) {
|
|
40
|
+
return this.send('GET', path);
|
|
41
|
+
}
|
|
42
|
+
send(method, path, body, type) {
|
|
43
|
+
const url = new URL(path, this.options.server);
|
|
44
|
+
const options = {
|
|
45
|
+
method,
|
|
46
|
+
headers: {
|
|
47
|
+
accept: 'application/json, */*',
|
|
48
|
+
...(this.options.token ? { authorization: `Bearer ${this.options.token}` } : {}),
|
|
49
|
+
...(body !== undefined ? { 'content-type': type !== null && type !== void 0 ? type : 'application/json', 'content-length': Buffer.byteLength(body) } : {}),
|
|
50
|
+
},
|
|
51
|
+
...(this.options.ca ? { ca: this.options.ca } : {}),
|
|
52
|
+
timeout: 30000,
|
|
53
|
+
};
|
|
54
|
+
const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const request = send(url, options, response => {
|
|
57
|
+
const chunks = [];
|
|
58
|
+
response.on('data', chunk => chunks.push(chunk));
|
|
59
|
+
response.on('end', () => {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
62
|
+
const status = (_a = response.statusCode) !== null && _a !== void 0 ? _a : 0;
|
|
63
|
+
if (status >= 200 && status < 300) {
|
|
64
|
+
resolve(text);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let message = text.slice(0, 300);
|
|
68
|
+
try {
|
|
69
|
+
message = (_b = JSON.parse(text).message) !== null && _b !== void 0 ? _b : message;
|
|
70
|
+
}
|
|
71
|
+
catch (_c) {
|
|
72
|
+
// Not JSON.
|
|
73
|
+
}
|
|
74
|
+
reject(new KubernetesError(status, `${method} ${url.pathname}: ${status} ${message}`));
|
|
75
|
+
});
|
|
76
|
+
response.on('error', reject);
|
|
77
|
+
});
|
|
78
|
+
request.on('timeout', () => request.destroy(new Error(`${method} ${url.pathname}: no answer in time`)));
|
|
79
|
+
request.on('error', reject);
|
|
80
|
+
request.end(body);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export var KubernetesUtil;
|
|
85
|
+
(function (KubernetesUtil) {
|
|
86
|
+
/** A quantity of Kubernetes (`4Gi`, `500M`, `250m`, `123456n`) as a number of its unit (bytes, CPUs). */
|
|
87
|
+
function quantity(value) {
|
|
88
|
+
if (typeof value === 'number') {
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
const match = /^([0-9.]+)([a-zA-Z]*)$/.exec(String(value !== null && value !== void 0 ? value : '').trim());
|
|
92
|
+
if (!match) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const scale = {
|
|
96
|
+
'': 1,
|
|
97
|
+
n: 1e-9,
|
|
98
|
+
u: 1e-6,
|
|
99
|
+
m: 1e-3,
|
|
100
|
+
k: 1e3,
|
|
101
|
+
K: 1e3,
|
|
102
|
+
M: 1e6,
|
|
103
|
+
G: 1e9,
|
|
104
|
+
T: 1e12,
|
|
105
|
+
Ki: 2 ** 10,
|
|
106
|
+
Mi: 2 ** 20,
|
|
107
|
+
Gi: 2 ** 30,
|
|
108
|
+
Ti: 2 ** 40,
|
|
109
|
+
};
|
|
110
|
+
const factor = scale[match[2]];
|
|
111
|
+
return factor === undefined ? undefined : Number(match[1]) * factor;
|
|
112
|
+
}
|
|
113
|
+
KubernetesUtil.quantity = quantity;
|
|
114
|
+
})(KubernetesUtil || (KubernetesUtil = {}));
|
|
115
|
+
//# sourceMappingURL=kubernetes-api.js.map
|