@celljs/ta-gateway 4.15.2 → 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/command-sandbox.d.ts +1 -0
- package/esm/node/command-sandbox.js +10 -1
- package/esm/node/docker-engine.d.ts +10 -1
- package/esm/node/docker-engine.js +18 -1
- package/esm/node/docker-sandbox.d.ts +13 -1
- package/esm/node/docker-sandbox.js +35 -2
- package/esm/node/egress-proxy.d.ts +98 -0
- package/esm/node/egress-proxy.js +428 -0
- package/esm/node/environments.d.ts +5 -0
- package/esm/node/environments.js +7 -3
- package/esm/node/gateway-cli.js +12 -5
- package/esm/node/gateway-exec.d.ts +19 -0
- package/esm/node/gateway-exec.js +65 -0
- package/esm/node/gateway-policy.d.ts +7 -1
- package/esm/node/gateway-policy.js +12 -2
- package/esm/node/gateway-quotas.d.ts +15 -2
- package/esm/node/gateway-quotas.js +51 -3
- package/esm/node/gateway-server.d.ts +43 -1
- package/esm/node/gateway-server.js +179 -10
- 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 +13 -41
- package/esm/node/gateway-service.js +41 -176
- 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/local-sandbox.d.ts +1 -0
- package/esm/node/local-sandbox.js +2 -1
- package/esm/node/model-proxy.d.ts +105 -0
- package/esm/node/model-proxy.js +435 -0
- package/esm/node/sandbox-provider.d.ts +17 -0
- package/esm/node/sandbox-provider.js +2 -1
- package/esm/node/tenant-store.d.ts +22 -0
- package/esm/node/tenant-store.js +25 -0
- package/lib/node/command-sandbox.d.ts +1 -0
- package/lib/node/command-sandbox.js +10 -1
- package/lib/node/docker-engine.d.ts +10 -1
- package/lib/node/docker-engine.js +18 -1
- package/lib/node/docker-sandbox.d.ts +13 -1
- package/lib/node/docker-sandbox.js +35 -2
- package/lib/node/egress-proxy.d.ts +99 -0
- package/lib/node/egress-proxy.js +433 -0
- package/lib/node/environments.d.ts +5 -0
- package/lib/node/environments.js +7 -3
- package/lib/node/gateway-cli.js +12 -5
- package/lib/node/gateway-exec.d.ts +20 -0
- package/lib/node/gateway-exec.js +69 -0
- package/lib/node/gateway-policy.d.ts +7 -1
- package/lib/node/gateway-policy.js +12 -2
- package/lib/node/gateway-quotas.d.ts +15 -2
- package/lib/node/gateway-quotas.js +51 -3
- package/lib/node/gateway-server.d.ts +43 -1
- package/lib/node/gateway-server.js +177 -8
- 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 +13 -41
- package/lib/node/gateway-service.js +63 -199
- 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/local-sandbox.d.ts +1 -0
- package/lib/node/local-sandbox.js +2 -1
- package/lib/node/model-proxy.d.ts +106 -0
- package/lib/node/model-proxy.js +440 -0
- package/lib/node/sandbox-provider.d.ts +17 -0
- package/lib/node/sandbox-provider.js +2 -1
- package/lib/node/tenant-store.d.ts +22 -0
- package/lib/node/tenant-store.js +25 -0
- package/package.json +4 -4
|
@@ -1,183 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GatewayService =
|
|
3
|
+
exports.GatewayService = void 0;
|
|
4
4
|
const access_store_1 = require("@celljs/ta-host/lib/node/sign-in/access-store");
|
|
5
5
|
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
6
6
|
const redactor_1 = require("@celljs/ta-protocol/lib/common/redactor");
|
|
7
|
+
const egress_proxy_1 = require("./egress-proxy");
|
|
7
8
|
const gateway_policy_1 = require("./gateway-policy");
|
|
9
|
+
const gateway_service_util_1 = require("./gateway-service-util");
|
|
8
10
|
const gateway_usage_1 = require("./gateway-usage");
|
|
9
|
-
const
|
|
10
|
-
class GatewayCallError extends Error {
|
|
11
|
-
constructor(message, code, hint) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.code = code;
|
|
14
|
-
this.hint = hint;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
exports.GatewayCallError = GatewayCallError;
|
|
11
|
+
const model_proxy_1 = require("./model-proxy");
|
|
18
12
|
const DAY = 24 * 3600000;
|
|
19
|
-
var GatewayServiceUtil;
|
|
20
|
-
(function (GatewayServiceUtil) {
|
|
21
|
-
GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
|
|
22
|
-
GatewayServiceUtil.MAX_LOG_LINES = 2000;
|
|
23
|
-
/**
|
|
24
|
-
* Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
|
|
25
|
-
* `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
|
|
26
|
-
* (GatewayService.manage).
|
|
27
|
-
*/
|
|
28
|
-
GatewayServiceUtil.RULES = {
|
|
29
|
-
TaGatewayServer: {
|
|
30
|
-
getGateway: 'anyone',
|
|
31
|
-
setEnvironment: 'anyone',
|
|
32
|
-
restartEnvironment: 'anyone',
|
|
33
|
-
listTenants: 'admin',
|
|
34
|
-
getConsole: 'admin',
|
|
35
|
-
getTenantAgents: 'admin',
|
|
36
|
-
getAgentActivity: 'admin',
|
|
37
|
-
getTenantLogs: 'admin',
|
|
38
|
-
getUsageTrend: 'admin',
|
|
39
|
-
getPolicy: 'admin',
|
|
40
|
-
getAudit: 'admin',
|
|
41
|
-
startTenant: 'admin',
|
|
42
|
-
stopTenant: 'admin',
|
|
43
|
-
restartTenant: 'admin',
|
|
44
|
-
createSignInLink: 'admin',
|
|
45
|
-
setTenantLimits: 'admin',
|
|
46
|
-
grantMinutes: 'admin',
|
|
47
|
-
setSuspended: 'admin',
|
|
48
|
-
setDefaultLimits: 'owner',
|
|
49
|
-
setRole: 'owner',
|
|
50
|
-
},
|
|
51
|
-
TaAccessServer: {
|
|
52
|
-
getAccess: 'anyone',
|
|
53
|
-
signOut: 'anyone',
|
|
54
|
-
bindUrl: 'admin',
|
|
55
|
-
allow: 'admin',
|
|
56
|
-
dismiss: 'admin',
|
|
57
|
-
disallow: 'admin',
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
/** What an administrator did or looked at of others: kept in the audit. */
|
|
61
|
-
GatewayServiceUtil.AUDITED = new Set([
|
|
62
|
-
'startTenant',
|
|
63
|
-
'stopTenant',
|
|
64
|
-
'restartTenant',
|
|
65
|
-
'getTenantLogs',
|
|
66
|
-
'getAgentActivity',
|
|
67
|
-
'createSignInLink',
|
|
68
|
-
'setTenantLimits',
|
|
69
|
-
'grantMinutes',
|
|
70
|
-
'setSuspended',
|
|
71
|
-
'setDefaultLimits',
|
|
72
|
-
'setRole',
|
|
73
|
-
'allow',
|
|
74
|
-
'dismiss',
|
|
75
|
-
'disallow',
|
|
76
|
-
]);
|
|
77
|
-
/** By name, own keys only: a method such as `toString` is no rule. */
|
|
78
|
-
const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
|
|
79
|
-
function need(service, name) {
|
|
80
|
-
var _a;
|
|
81
|
-
return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
|
|
82
|
-
}
|
|
83
|
-
GatewayServiceUtil.need = need;
|
|
84
|
-
function allowed(role, need) {
|
|
85
|
-
return need === 'anyone' || gateway_policy_1.GatewayPolicyUtil.rank(role) >= gateway_policy_1.GatewayPolicyUtil.rank(need);
|
|
86
|
-
}
|
|
87
|
-
GatewayServiceUtil.allowed = allowed;
|
|
88
|
-
/** As many lines as asked, within bounds. */
|
|
89
|
-
function lines(value) {
|
|
90
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
|
|
91
|
-
return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
|
|
92
|
-
}
|
|
93
|
-
GatewayServiceUtil.lines = lines;
|
|
94
|
-
/** Days of a trend, 1 to 31. */
|
|
95
|
-
function days(value) {
|
|
96
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
|
|
97
|
-
return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
|
|
98
|
-
}
|
|
99
|
-
GatewayServiceUtil.days = days;
|
|
100
|
-
/** The services the gateway answers itself, not the Ta of the tenant. */
|
|
101
|
-
GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
|
|
102
|
-
function owns(method) {
|
|
103
|
-
return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
|
|
104
|
-
}
|
|
105
|
-
GatewayServiceUtil.owns = owns;
|
|
106
|
-
/** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
|
|
107
|
-
function mayOwn(text) {
|
|
108
|
-
return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
|
|
109
|
-
}
|
|
110
|
-
GatewayServiceUtil.mayOwn = mayOwn;
|
|
111
|
-
/** The calls that send the agents a message, which the limit of messages a minute counts. */
|
|
112
|
-
GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
|
|
113
|
-
function mayPrompt(text) {
|
|
114
|
-
return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
|
|
115
|
-
}
|
|
116
|
-
GatewayServiceUtil.mayPrompt = mayPrompt;
|
|
117
|
-
/** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
|
|
118
|
-
function error(id, message, code, hint) {
|
|
119
|
-
return JSON.stringify({ jsonrpc: '2.0', id, error: { code: ta_protocol_1.TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
|
|
120
|
-
}
|
|
121
|
-
GatewayServiceUtil.error = error;
|
|
122
|
-
function result(id, result) {
|
|
123
|
-
return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
|
|
124
|
-
}
|
|
125
|
-
GatewayServiceUtil.result = result;
|
|
126
|
-
function invalid(message, hint) {
|
|
127
|
-
return new GatewayCallError(message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, hint);
|
|
128
|
-
}
|
|
129
|
-
function forbidden(message, hint) {
|
|
130
|
-
return new GatewayCallError(message, ta_protocol_1.TaErrorCode.FORBIDDEN, hint);
|
|
131
|
-
}
|
|
132
|
-
GatewayServiceUtil.forbidden = forbidden;
|
|
133
|
-
function choice(value, own, providers) {
|
|
134
|
-
var _a;
|
|
135
|
-
const given = (value !== null && value !== void 0 ? value : {});
|
|
136
|
-
if (typeof given.provider === 'string') {
|
|
137
|
-
if (!providers.includes(given.provider)) {
|
|
138
|
-
throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
|
|
139
|
-
}
|
|
140
|
-
return { provider: given.provider };
|
|
141
|
-
}
|
|
142
|
-
if (given.type === 'ta') {
|
|
143
|
-
if (!own) {
|
|
144
|
-
throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
|
|
145
|
-
}
|
|
146
|
-
let url;
|
|
147
|
-
try {
|
|
148
|
-
url = ta_sandbox_1.TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
|
|
152
|
-
}
|
|
153
|
-
if (typeof given.token !== 'string' || !given.token.trim()) {
|
|
154
|
-
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.');
|
|
155
|
-
}
|
|
156
|
-
return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
|
|
157
|
-
}
|
|
158
|
-
throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
|
|
159
|
-
}
|
|
160
|
-
GatewayServiceUtil.choice = choice;
|
|
161
|
-
/** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
|
|
162
|
-
function detail(method, params) {
|
|
163
|
-
const [, second] = params;
|
|
164
|
-
switch (method) {
|
|
165
|
-
case 'setTenantLimits':
|
|
166
|
-
case 'setDefaultLimits':
|
|
167
|
-
return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
|
|
168
|
-
case 'grantMinutes':
|
|
169
|
-
case 'getTenantLogs':
|
|
170
|
-
return String(second !== null && second !== void 0 ? second : '');
|
|
171
|
-
case 'setSuspended':
|
|
172
|
-
return second ? 'suspended' : 'let go';
|
|
173
|
-
case 'setRole':
|
|
174
|
-
return String(second);
|
|
175
|
-
default:
|
|
176
|
-
return undefined;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
GatewayServiceUtil.detail = detail;
|
|
180
|
-
})(GatewayServiceUtil || (exports.GatewayServiceUtil = GatewayServiceUtil = {}));
|
|
181
13
|
/**
|
|
182
14
|
* The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
|
|
183
15
|
* accounts are those that may have a tenant here. Each method needs a role (GatewayServiceUtil.RULES); what an
|
|
@@ -185,6 +17,8 @@ var GatewayServiceUtil;
|
|
|
185
17
|
*/
|
|
186
18
|
class GatewayService {
|
|
187
19
|
constructor(parts) {
|
|
20
|
+
this.models = parts.models;
|
|
21
|
+
this.egress = parts.egress;
|
|
188
22
|
this.flow = parts.flow;
|
|
189
23
|
this.tenants = parts.tenants;
|
|
190
24
|
this.environments = parts.environments;
|
|
@@ -197,17 +31,17 @@ class GatewayService {
|
|
|
197
31
|
}
|
|
198
32
|
async call(caller, method, params) {
|
|
199
33
|
const [service, name] = method.split('/');
|
|
200
|
-
const need = GatewayServiceUtil.need(service, name);
|
|
34
|
+
const need = gateway_service_util_1.GatewayServiceUtil.need(service, name);
|
|
201
35
|
if (!need) {
|
|
202
|
-
throw new GatewayCallError(`${method} is not a method of the gateway.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Call a method of TaGatewayServer or TaAccessServer.');
|
|
36
|
+
throw new gateway_service_util_1.GatewayCallError(`${method} is not a method of the gateway.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Call a method of TaGatewayServer or TaAccessServer.');
|
|
203
37
|
}
|
|
204
|
-
if (!GatewayServiceUtil.allowed(caller.role, need)) {
|
|
38
|
+
if (!gateway_service_util_1.GatewayServiceUtil.allowed(caller.role, need)) {
|
|
205
39
|
throw need === 'owner'
|
|
206
|
-
? GatewayServiceUtil.forbidden('Only owners of the gateway may do that.', 'Ask one of those of TA_GATEWAY_ADMINS.')
|
|
207
|
-
: GatewayServiceUtil.forbidden('Only administrators of the gateway may do that.', 'Ask one of its administrators.');
|
|
40
|
+
? gateway_service_util_1.GatewayServiceUtil.forbidden('Only owners of the gateway may do that.', 'Ask one of those of TA_GATEWAY_ADMINS.')
|
|
41
|
+
: gateway_service_util_1.GatewayServiceUtil.forbidden('Only administrators of the gateway may do that.', 'Ask one of its administrators.');
|
|
208
42
|
}
|
|
209
43
|
const result = await this.handle(caller, service, name, params);
|
|
210
|
-
if (GatewayServiceUtil.AUDITED.has(name)) {
|
|
44
|
+
if (gateway_service_util_1.GatewayServiceUtil.AUDITED.has(name)) {
|
|
211
45
|
this.record(caller, name, params);
|
|
212
46
|
}
|
|
213
47
|
return result;
|
|
@@ -236,6 +70,8 @@ class GatewayService {
|
|
|
236
70
|
setSuspended: () => this.setSuspended(caller, first, second),
|
|
237
71
|
setDefaultLimits: () => this.setDefaultLimits(first),
|
|
238
72
|
setRole: () => this.setRole(caller, first, second),
|
|
73
|
+
setEgressHosts: () => this.setEgressHosts(first),
|
|
74
|
+
createAccessToken: () => this.accessToken(caller, first),
|
|
239
75
|
}
|
|
240
76
|
: {
|
|
241
77
|
getAccess: () => this.access(caller),
|
|
@@ -253,7 +89,7 @@ class GatewayService {
|
|
|
253
89
|
const [first] = params;
|
|
254
90
|
const target = typeof first === 'string' ? ((_b = (_a = this.tenants.get(first)) === null || _a === void 0 ? void 0 : _a.account) !== null && _b !== void 0 ? _b : (/^[a-z][a-z0-9-]*:\S+$/.test(first) ? first : undefined)) : undefined;
|
|
255
91
|
try {
|
|
256
|
-
this.audit.add({ actor: caller.account, action, target, detail: GatewayServiceUtil.detail(action, params) });
|
|
92
|
+
this.audit.add({ actor: caller.account, action, target, detail: gateway_service_util_1.GatewayServiceUtil.detail(action, params) });
|
|
257
93
|
}
|
|
258
94
|
catch (error) {
|
|
259
95
|
console.warn(`ta gateway: the audit could not be written: ${error.message}`);
|
|
@@ -280,7 +116,7 @@ class GatewayService {
|
|
|
280
116
|
var _a;
|
|
281
117
|
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== void 0 ? _a : caller.tenant;
|
|
282
118
|
const limits = this.quotas.limits(tenant);
|
|
283
|
-
const choice = GatewayServiceUtil.choice(value, this.environments.settings.own && limits.own, this.environments.providers.map(provider => provider.id).filter(id => !limits.providers || limits.providers.includes(id)));
|
|
119
|
+
const choice = gateway_service_util_1.GatewayServiceUtil.choice(value, this.environments.settings.own && limits.own, this.environments.providers.map(provider => provider.id).filter(id => !limits.providers || limits.providers.includes(id)));
|
|
284
120
|
const next = await this.environments.change(tenant, choice);
|
|
285
121
|
caller.tenant = next;
|
|
286
122
|
// Everyone of the tenant connects again, once the browser that asked has its answer.
|
|
@@ -310,8 +146,8 @@ class GatewayService {
|
|
|
310
146
|
}
|
|
311
147
|
/** 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. */
|
|
312
148
|
async console(caller) {
|
|
313
|
-
var _a;
|
|
314
|
-
var
|
|
149
|
+
var _a, _b;
|
|
150
|
+
var _c, _d, _e;
|
|
315
151
|
const tenants = this.tenants.list();
|
|
316
152
|
const [usages, agents] = await Promise.all([
|
|
317
153
|
Promise.all(tenants.map(tenant => this.environments.usage(tenant).catch(() => undefined))),
|
|
@@ -320,9 +156,9 @@ class GatewayService {
|
|
|
320
156
|
const counts = new Map();
|
|
321
157
|
for (const tenant of tenants) {
|
|
322
158
|
const { provider } = this.environments.info(tenant);
|
|
323
|
-
counts.set(provider, ((
|
|
159
|
+
counts.set(provider, ((_c = counts.get(provider)) !== null && _c !== void 0 ? _c : 0) + 1);
|
|
324
160
|
}
|
|
325
|
-
const fallback = (
|
|
161
|
+
const fallback = (_d = this.environments.settings.default) !== null && _d !== void 0 ? _d : (_a = this.environments.providers[0]) === null || _a === void 0 ? void 0 : _a.id;
|
|
326
162
|
return {
|
|
327
163
|
version: this.meta.version,
|
|
328
164
|
startedAt: this.meta.startedAt,
|
|
@@ -343,15 +179,34 @@ class GatewayService {
|
|
|
343
179
|
requests: this.flow.store.info(caller.token).requests.length,
|
|
344
180
|
role: caller.role,
|
|
345
181
|
policy: this.policyView(),
|
|
182
|
+
models: model_proxy_1.ModelProxyUtil.catalog((_e = (_b = this.models) === null || _b === void 0 ? void 0 : _b.config) !== null && _e !== void 0 ? _e : {}).providers.map(provider => ({
|
|
183
|
+
id: provider.id,
|
|
184
|
+
name: provider.name,
|
|
185
|
+
api: provider.api,
|
|
186
|
+
models: provider.models.length,
|
|
187
|
+
})),
|
|
346
188
|
};
|
|
347
189
|
}
|
|
348
190
|
policyView() {
|
|
349
|
-
|
|
191
|
+
var _a;
|
|
192
|
+
return this.policy.view(this.environments.settings.idleMinutes, (_a = this.egress) === null || _a === void 0 ? void 0 : _a.settings);
|
|
193
|
+
}
|
|
194
|
+
setEgressHosts(value) {
|
|
195
|
+
try {
|
|
196
|
+
this.policy.setEgressHosts(egress_proxy_1.EgressProxyUtil.hosts(value));
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
if (error instanceof egress_proxy_1.EgressConfigError) {
|
|
200
|
+
throw new gateway_service_util_1.GatewayCallError(error.message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Send hosts such as registry.npmjs.org, *.github.com or example.com:8443.');
|
|
201
|
+
}
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
return this.policyView();
|
|
350
205
|
}
|
|
351
206
|
tenant(id) {
|
|
352
207
|
const tenant = typeof id === 'string' ? this.tenants.get(id) : undefined;
|
|
353
208
|
if (!tenant) {
|
|
354
|
-
throw new GatewayCallError(`The gateway has no tenant ${id}.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Pick one of the tenants of getConsole.');
|
|
209
|
+
throw new gateway_service_util_1.GatewayCallError(`The gateway has no tenant ${id}.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Pick one of the tenants of getConsole.');
|
|
355
210
|
}
|
|
356
211
|
return tenant;
|
|
357
212
|
}
|
|
@@ -359,7 +214,7 @@ class GatewayService {
|
|
|
359
214
|
manage(caller, id) {
|
|
360
215
|
const tenant = this.tenant(id);
|
|
361
216
|
if (caller.role !== 'owner' && (this.quotas.role(tenant) !== 'member' || tenant.account === caller.account)) {
|
|
362
|
-
throw GatewayServiceUtil.forbidden('Admins manage members only, not themselves, other admins or owners.', 'Ask an owner of the gateway (TA_GATEWAY_ADMINS).');
|
|
217
|
+
throw gateway_service_util_1.GatewayServiceUtil.forbidden('Admins manage members only, not themselves, other admins or owners.', 'Ask an owner of the gateway (TA_GATEWAY_ADMINS).');
|
|
363
218
|
}
|
|
364
219
|
return tenant;
|
|
365
220
|
}
|
|
@@ -379,14 +234,14 @@ class GatewayService {
|
|
|
379
234
|
}
|
|
380
235
|
/** What its sandbox wrote, its secrets hidden: tokens and keys printed by what ran there do not reach the page. */
|
|
381
236
|
async logs(id, lines) {
|
|
382
|
-
return new redactor_1.Redactor().text(await this.environments.logs(this.tenant(id), GatewayServiceUtil.lines(lines)));
|
|
237
|
+
return new redactor_1.Redactor().text(await this.environments.logs(this.tenant(id), gateway_service_util_1.GatewayServiceUtil.lines(lines)));
|
|
383
238
|
}
|
|
384
239
|
activity(id, sessionId) {
|
|
385
240
|
return this.environments.activity(this.tenant(id), String(sessionId));
|
|
386
241
|
}
|
|
387
242
|
async trend(daysValue, id) {
|
|
388
243
|
const now = Date.now();
|
|
389
|
-
const days = GatewayServiceUtil.days(daysValue);
|
|
244
|
+
const days = gateway_service_util_1.GatewayServiceUtil.days(daysValue);
|
|
390
245
|
const tenants = id === undefined || id === null ? this.tenants.list() : [this.tenant(id)];
|
|
391
246
|
const fresh = await Promise.all(tenants.map(tenant => this.usage.refresh(tenant.id, this.environments.usageReader(tenant), now, 0).catch(() => false)));
|
|
392
247
|
const byTenant = new Map();
|
|
@@ -410,7 +265,7 @@ class GatewayService {
|
|
|
410
265
|
}
|
|
411
266
|
catch (error) {
|
|
412
267
|
if (error instanceof gateway_policy_1.PolicyError) {
|
|
413
|
-
throw new GatewayCallError(error.message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Send the limits again, written well.');
|
|
268
|
+
throw new gateway_service_util_1.GatewayCallError(error.message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Send the limits again, written well.');
|
|
414
269
|
}
|
|
415
270
|
throw error;
|
|
416
271
|
}
|
|
@@ -422,7 +277,7 @@ class GatewayService {
|
|
|
422
277
|
grantMinutes(caller, id, minutes) {
|
|
423
278
|
const tenant = this.manage(caller, id);
|
|
424
279
|
if (typeof minutes !== 'number' || !Number.isInteger(minutes) || Math.abs(minutes) > 1000000) {
|
|
425
|
-
throw new GatewayCallError('Minutes are a whole number.', ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Give, say, 60; a negative number takes some back.');
|
|
280
|
+
throw new gateway_service_util_1.GatewayCallError('Minutes are a whole number.', ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Give, say, 60; a negative number takes some back.');
|
|
426
281
|
}
|
|
427
282
|
this.quotas.grant(tenant, minutes);
|
|
428
283
|
}
|
|
@@ -441,17 +296,26 @@ class GatewayService {
|
|
|
441
296
|
setRole(caller, account, role) {
|
|
442
297
|
const id = typeof account === 'string' ? account.trim() : '';
|
|
443
298
|
if (role !== 'admin' && role !== 'member') {
|
|
444
|
-
throw new GatewayCallError('A role is admin or member.', ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Owners are those of TA_GATEWAY_ADMINS.');
|
|
299
|
+
throw new gateway_service_util_1.GatewayCallError('A role is admin or member.', ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Owners are those of TA_GATEWAY_ADMINS.');
|
|
445
300
|
}
|
|
446
301
|
if (this.policy.role(id) === 'owner') {
|
|
447
|
-
throw GatewayServiceUtil.forbidden('An owner stays one: they are of TA_GATEWAY_ADMINS.', 'Change TA_GATEWAY_ADMINS where the gateway runs.');
|
|
302
|
+
throw gateway_service_util_1.GatewayServiceUtil.forbidden('An owner stays one: they are of TA_GATEWAY_ADMINS.', 'Change TA_GATEWAY_ADMINS where the gateway runs.');
|
|
448
303
|
}
|
|
449
304
|
if (id === caller.account || !this.tenants.list().some(tenant => tenant.account === id)) {
|
|
450
|
-
throw new GatewayCallError(`${id} has not signed in to the gateway.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Make an account that signed in an admin.');
|
|
305
|
+
throw new gateway_service_util_1.GatewayCallError(`${id} has not signed in to the gateway.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Make an account that signed in an admin.');
|
|
451
306
|
}
|
|
452
307
|
this.policy.setRole(id, role);
|
|
453
308
|
return this.policyView();
|
|
454
309
|
}
|
|
310
|
+
/** A sign-in of the caller's own account, named for where it is used. */
|
|
311
|
+
accessToken(caller, label) {
|
|
312
|
+
var _a;
|
|
313
|
+
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== void 0 ? _a : caller.tenant;
|
|
314
|
+
const device = typeof label === 'string' && label.trim() ? label.trim().slice(0, 60) : 'Another Ta';
|
|
315
|
+
const token = this.flow.store.signIn({ id: caller.account, name: tenant.name, avatar: tenant.avatar }, device);
|
|
316
|
+
console.log(`ta gateway: ${caller.account} made a token for ${device}`);
|
|
317
|
+
return { token, url: this.meta.publicUrl };
|
|
318
|
+
}
|
|
455
319
|
access(caller) {
|
|
456
320
|
const providers = this.flow.providers.map(({ id, name }) => ({ id, name }));
|
|
457
321
|
const info = this.flow.store.info(caller.token);
|
|
@@ -462,7 +326,7 @@ class GatewayService {
|
|
|
462
326
|
}
|
|
463
327
|
allow(account, name) {
|
|
464
328
|
if (typeof account !== 'string' || !/^[a-z][a-z0-9-]*:\S+$/.test(account.trim())) {
|
|
465
|
-
throw new GatewayCallError(`${account} is not an account.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Write it <provider>:<login or address>, such as github:octocat.');
|
|
329
|
+
throw new gateway_service_util_1.GatewayCallError(`${account} is not an account.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Write it <provider>:<login or address>, such as github:octocat.');
|
|
466
330
|
}
|
|
467
331
|
this.flow.store.allow(account.trim(), typeof name === 'string' ? name : undefined);
|
|
468
332
|
}
|
|
@@ -471,7 +335,7 @@ class GatewayService {
|
|
|
471
335
|
const normalized = access_store_1.AccessUtil.normalize(account);
|
|
472
336
|
const role = this.policy.role(normalized);
|
|
473
337
|
if (role === 'owner' || (role === 'admin' && caller.role !== 'owner')) {
|
|
474
|
-
throw GatewayServiceUtil.forbidden(role === 'owner' ? 'Owners are let in where the gateway runs (TA_GATEWAY_ADMINS).' : 'Only owners remove an admin.', 'Ask an owner of the gateway.');
|
|
338
|
+
throw gateway_service_util_1.GatewayServiceUtil.forbidden(role === 'owner' ? 'Owners are let in where the gateway runs (TA_GATEWAY_ADMINS).' : 'Only owners remove an admin.', 'Ask an owner of the gateway.');
|
|
475
339
|
}
|
|
476
340
|
if (role === 'admin') {
|
|
477
341
|
this.policy.setRole(normalized, 'member');
|
|
@@ -489,7 +353,7 @@ class GatewayService {
|
|
|
489
353
|
async signOut(caller, id) {
|
|
490
354
|
const own = this.flow.store.info(caller.token).signIns.find(signIn => signIn.id === id);
|
|
491
355
|
if (!caller.admin && (own === null || own === void 0 ? void 0 : own.account) !== caller.account) {
|
|
492
|
-
throw GatewayServiceUtil.forbidden('That sign-in is not yours.', 'Sign out of the browsers of your own account.');
|
|
356
|
+
throw gateway_service_util_1.GatewayServiceUtil.forbidden('That sign-in is not yours.', 'Sign out of the browsers of your own account.');
|
|
493
357
|
}
|
|
494
358
|
this.flow.store.signOut(id);
|
|
495
359
|
this.hooks.cutOff();
|
|
@@ -501,10 +365,10 @@ class GatewayService {
|
|
|
501
365
|
signInLink(account, name) {
|
|
502
366
|
const id = typeof account === 'string' ? account.trim() : '';
|
|
503
367
|
if (!/^[a-z][a-z0-9-]*:\S+$/.test(id)) {
|
|
504
|
-
throw new GatewayCallError(`${account} is not an account.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Write it <kind>:<name>, such as github:octocat or user:alice.');
|
|
368
|
+
throw new gateway_service_util_1.GatewayCallError(`${account} is not an account.`, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, 'Write it <kind>:<name>, such as github:octocat or user:alice.');
|
|
505
369
|
}
|
|
506
370
|
if (this.tenants.list().some(tenant => tenant.account === id) || this.policy.role(id) !== 'member') {
|
|
507
|
-
throw GatewayServiceUtil.forbidden(`${id} has been here already: a link would sign whoever opens it in as them.`, 'Invite accounts that are new; one that has been here signs in its own way.');
|
|
371
|
+
throw gateway_service_util_1.GatewayServiceUtil.forbidden(`${id} has been here already: a link would sign whoever opens it in as them.`, 'Invite accounts that are new; one that has been here signs in its own way.');
|
|
508
372
|
}
|
|
509
373
|
return this.meta.links.create(id, typeof name === 'string' && name.trim() ? name.trim() : undefined);
|
|
510
374
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
2
2
|
import type { Environments } from './environments';
|
|
3
3
|
import type { GatewayQuotas } from './gateway-quotas';
|
|
4
|
-
import {
|
|
4
|
+
import type { GatewayCaller, GatewayService } from './gateway-service';
|
|
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
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GatewaySocket = void 0;
|
|
4
4
|
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
5
|
-
const
|
|
5
|
+
const gateway_service_util_1 = require("./gateway-service-util");
|
|
6
6
|
/** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
|
|
7
7
|
const MAX_QUEUED = 256;
|
|
8
8
|
/** 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. */
|
|
@@ -90,11 +90,11 @@ class GatewaySocket {
|
|
|
90
90
|
this.close(1008, 'Too many messages.');
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
|
-
if (this.failure === undefined &&
|
|
93
|
+
if (this.failure === undefined && gateway_service_util_1.GatewayServiceUtil.mayPrompt(text) && !this.admitPrompt(text)) {
|
|
94
94
|
return;
|
|
95
95
|
}
|
|
96
96
|
// Most messages are the Ta's, some large (pictures): forwarded without being read.
|
|
97
|
-
if (this.failure === undefined && !
|
|
97
|
+
if (this.failure === undefined && !gateway_service_util_1.GatewayServiceUtil.mayOwn(text)) {
|
|
98
98
|
this.forward(text);
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
@@ -106,13 +106,13 @@ class GatewaySocket {
|
|
|
106
106
|
this.close(1007, 'Not JSON.');
|
|
107
107
|
return;
|
|
108
108
|
}
|
|
109
|
-
if (
|
|
109
|
+
if (gateway_service_util_1.GatewayServiceUtil.owns(message.method)) {
|
|
110
110
|
void this.answer(message.id, message.method, Array.isArray(message.params) ? message.params : []);
|
|
111
111
|
return;
|
|
112
112
|
}
|
|
113
113
|
if (this.failure !== undefined) {
|
|
114
114
|
if (message.method !== undefined && message.id !== undefined) {
|
|
115
|
-
this.browser.send(
|
|
115
|
+
this.browser.send(gateway_service_util_1.GatewayServiceUtil.error(message.id, `Your environment did not start: ${this.failure}`, ta_protocol_1.TaErrorCode.ENVIRONMENT_FAILED, 'Restart it, or pick another (TaGatewayServer).'));
|
|
116
116
|
}
|
|
117
117
|
return;
|
|
118
118
|
}
|
|
@@ -127,7 +127,7 @@ class GatewaySocket {
|
|
|
127
127
|
catch (_a) {
|
|
128
128
|
return true;
|
|
129
129
|
}
|
|
130
|
-
if (typeof message.method !== 'string' || !
|
|
130
|
+
if (typeof message.method !== 'string' || !gateway_service_util_1.GatewayServiceUtil.PROMPTS.includes(message.method) || !this.quotas) {
|
|
131
131
|
return true;
|
|
132
132
|
}
|
|
133
133
|
if (this.quotas.takePrompt(this.caller.tenant)) {
|
|
@@ -135,7 +135,7 @@ class GatewaySocket {
|
|
|
135
135
|
}
|
|
136
136
|
const seconds = Math.ceil(this.quotas.retryAfter(this.caller.tenant) / 1000);
|
|
137
137
|
if (message.id !== undefined) {
|
|
138
|
-
this.browser.send(
|
|
138
|
+
this.browser.send(gateway_service_util_1.GatewayServiceUtil.error(message.id, `You sent as many messages this minute as your account may (${this.quotas.limits(this.caller.tenant).promptsPerMinute}).`, ta_protocol_1.TaErrorCode.RATE_LIMITED, `Send it again in ${seconds} s.`));
|
|
139
139
|
}
|
|
140
140
|
return false;
|
|
141
141
|
}
|
|
@@ -156,11 +156,11 @@ class GatewaySocket {
|
|
|
156
156
|
let reply;
|
|
157
157
|
this.answering++;
|
|
158
158
|
try {
|
|
159
|
-
reply =
|
|
159
|
+
reply = gateway_service_util_1.GatewayServiceUtil.result(id, await this.service.call(this.caller, method, params));
|
|
160
160
|
}
|
|
161
161
|
catch (error) {
|
|
162
|
-
const known = error instanceof
|
|
163
|
-
reply =
|
|
162
|
+
const known = error instanceof gateway_service_util_1.GatewayCallError;
|
|
163
|
+
reply = gateway_service_util_1.GatewayServiceUtil.error(id, error.message, known ? error.code : ta_protocol_1.TaErrorCode.COMMAND_FAILED, known ? error.hint : 'The gateway could not do it; try again.');
|
|
164
164
|
}
|
|
165
165
|
this.answering--;
|
|
166
166
|
if (id !== undefined && this.browser.open) {
|
package/lib/node/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './command-sandbox';
|
|
2
2
|
export * from './docker-engine';
|
|
3
3
|
export * from './docker-sandbox';
|
|
4
|
+
export * from './egress-proxy';
|
|
4
5
|
export * from './environments';
|
|
5
6
|
export * from './gateway-cli';
|
|
6
7
|
export * from './gateway-server';
|
|
7
8
|
export * from './gateway-service';
|
|
9
|
+
export * from './gateway-service-util';
|
|
8
10
|
export * from './gateway-socket';
|
|
11
|
+
export * from './kubernetes-api';
|
|
12
|
+
export * from './kubernetes-sandbox';
|
|
9
13
|
export * from './local-sandbox';
|
|
10
14
|
export * from './sandbox-provider';
|
|
11
15
|
export * from './sign-in-links';
|
package/lib/node/index.js
CHANGED
|
@@ -17,11 +17,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./command-sandbox"), exports);
|
|
18
18
|
__exportStar(require("./docker-engine"), exports);
|
|
19
19
|
__exportStar(require("./docker-sandbox"), exports);
|
|
20
|
+
__exportStar(require("./egress-proxy"), exports);
|
|
20
21
|
__exportStar(require("./environments"), exports);
|
|
21
22
|
__exportStar(require("./gateway-cli"), exports);
|
|
22
23
|
__exportStar(require("./gateway-server"), exports);
|
|
23
24
|
__exportStar(require("./gateway-service"), exports);
|
|
25
|
+
__exportStar(require("./gateway-service-util"), exports);
|
|
24
26
|
__exportStar(require("./gateway-socket"), exports);
|
|
27
|
+
__exportStar(require("./kubernetes-api"), exports);
|
|
28
|
+
__exportStar(require("./kubernetes-sandbox"), exports);
|
|
25
29
|
__exportStar(require("./local-sandbox"), exports);
|
|
26
30
|
__exportStar(require("./sandbox-provider"), exports);
|
|
27
31
|
__exportStar(require("./sign-in-links"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
36
|
+
//# sourceMappingURL=kubernetes-api.d.ts.map
|