@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.
Files changed (65) hide show
  1. package/README.en.md +5 -2
  2. package/README.md +5 -2
  3. package/esm/node/docker-engine.d.ts +5 -1
  4. package/esm/node/docker-engine.js +7 -1
  5. package/esm/node/docker-sandbox.d.ts +10 -1
  6. package/esm/node/docker-sandbox.js +24 -5
  7. package/esm/node/egress-proxy.d.ts +98 -0
  8. package/esm/node/egress-proxy.js +428 -0
  9. package/esm/node/environments.d.ts +5 -2
  10. package/esm/node/environments.js +4 -2
  11. package/esm/node/gateway-cli.js +13 -6
  12. package/esm/node/gateway-policy.d.ts +6 -1
  13. package/esm/node/gateway-policy.js +10 -1
  14. package/esm/node/gateway-quotas.d.ts +6 -1
  15. package/esm/node/gateway-quotas.js +17 -0
  16. package/esm/node/gateway-server.d.ts +35 -1
  17. package/esm/node/gateway-server.js +150 -16
  18. package/esm/node/gateway-service-util.d.ts +41 -0
  19. package/esm/node/gateway-service-util.js +178 -0
  20. package/esm/node/gateway-service.d.ts +5 -41
  21. package/esm/node/gateway-service.js +19 -173
  22. package/esm/node/gateway-socket.d.ts +1 -1
  23. package/esm/node/gateway-socket.js +1 -1
  24. package/esm/node/index.d.ts +4 -0
  25. package/esm/node/index.js +4 -0
  26. package/esm/node/kubernetes-api.d.ts +35 -0
  27. package/esm/node/kubernetes-api.js +115 -0
  28. package/esm/node/kubernetes-sandbox.d.ts +198 -0
  29. package/esm/node/kubernetes-sandbox.js +279 -0
  30. package/esm/node/sandbox-provider.d.ts +11 -1
  31. package/esm/node/sandbox-provider.js +2 -1
  32. package/esm/node/tenant-store.d.ts +15 -0
  33. package/esm/node/tenant-store.js +15 -0
  34. package/lib/node/docker-engine.d.ts +5 -1
  35. package/lib/node/docker-engine.js +7 -1
  36. package/lib/node/docker-sandbox.d.ts +10 -1
  37. package/lib/node/docker-sandbox.js +24 -5
  38. package/lib/node/egress-proxy.d.ts +99 -0
  39. package/lib/node/egress-proxy.js +433 -0
  40. package/lib/node/environments.d.ts +5 -2
  41. package/lib/node/environments.js +4 -2
  42. package/lib/node/gateway-cli.js +13 -6
  43. package/lib/node/gateway-policy.d.ts +6 -1
  44. package/lib/node/gateway-policy.js +10 -1
  45. package/lib/node/gateway-quotas.d.ts +6 -1
  46. package/lib/node/gateway-quotas.js +17 -0
  47. package/lib/node/gateway-server.d.ts +35 -1
  48. package/lib/node/gateway-server.js +148 -14
  49. package/lib/node/gateway-service-util.d.ts +42 -0
  50. package/lib/node/gateway-service-util.js +182 -0
  51. package/lib/node/gateway-service.d.ts +5 -41
  52. package/lib/node/gateway-service.js +41 -196
  53. package/lib/node/gateway-socket.d.ts +1 -1
  54. package/lib/node/gateway-socket.js +10 -10
  55. package/lib/node/index.d.ts +4 -0
  56. package/lib/node/index.js +4 -0
  57. package/lib/node/kubernetes-api.d.ts +36 -0
  58. package/lib/node/kubernetes-api.js +120 -0
  59. package/lib/node/kubernetes-sandbox.d.ts +199 -0
  60. package/lib/node/kubernetes-sandbox.js +283 -0
  61. package/lib/node/sandbox-provider.d.ts +11 -1
  62. package/lib/node/sandbox-provider.js +2 -1
  63. package/lib/node/tenant-store.d.ts +15 -0
  64. package/lib/node/tenant-store.js +15 -0
  65. package/package.json +4 -4
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GatewayServiceUtil = exports.GatewayCallError = void 0;
4
+ const ta_protocol_1 = require("@celljs/ta-protocol");
5
+ const gateway_policy_1 = require("./gateway-policy");
6
+ const ta_sandbox_1 = require("./ta-sandbox");
7
+ class GatewayCallError extends Error {
8
+ constructor(message, code, hint) {
9
+ super(message);
10
+ this.code = code;
11
+ this.hint = hint;
12
+ }
13
+ }
14
+ exports.GatewayCallError = GatewayCallError;
15
+ var GatewayServiceUtil;
16
+ (function (GatewayServiceUtil) {
17
+ GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
18
+ GatewayServiceUtil.MAX_LOG_LINES = 2000;
19
+ /**
20
+ * Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
21
+ * `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
22
+ * (GatewayService.manage).
23
+ */
24
+ GatewayServiceUtil.RULES = {
25
+ TaGatewayServer: {
26
+ getGateway: 'anyone',
27
+ setEnvironment: 'anyone',
28
+ restartEnvironment: 'anyone',
29
+ listTenants: 'admin',
30
+ getConsole: 'admin',
31
+ getTenantAgents: 'admin',
32
+ getAgentActivity: 'admin',
33
+ getTenantLogs: 'admin',
34
+ getUsageTrend: 'admin',
35
+ getPolicy: 'admin',
36
+ getAudit: 'admin',
37
+ startTenant: 'admin',
38
+ stopTenant: 'admin',
39
+ restartTenant: 'admin',
40
+ createSignInLink: 'admin',
41
+ setTenantLimits: 'admin',
42
+ grantMinutes: 'admin',
43
+ setSuspended: 'admin',
44
+ setDefaultLimits: 'owner',
45
+ setRole: 'owner',
46
+ setEgressHosts: '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
+ 'setEgressHosts',
72
+ 'allow',
73
+ 'dismiss',
74
+ 'disallow',
75
+ ]);
76
+ /** By name, own keys only: a method such as `toString` is no rule. */
77
+ const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
78
+ function need(service, name) {
79
+ var _a;
80
+ return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
81
+ }
82
+ GatewayServiceUtil.need = need;
83
+ function allowed(role, need) {
84
+ return need === 'anyone' || gateway_policy_1.GatewayPolicyUtil.rank(role) >= gateway_policy_1.GatewayPolicyUtil.rank(need);
85
+ }
86
+ GatewayServiceUtil.allowed = allowed;
87
+ /** As many lines as asked, within bounds. */
88
+ function lines(value) {
89
+ const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
90
+ return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
91
+ }
92
+ GatewayServiceUtil.lines = lines;
93
+ /** Days of a trend, 1 to 31. */
94
+ function days(value) {
95
+ const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
96
+ return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
97
+ }
98
+ GatewayServiceUtil.days = days;
99
+ /** The services the gateway answers itself, not the Ta of the tenant. */
100
+ GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
101
+ function owns(method) {
102
+ return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
103
+ }
104
+ GatewayServiceUtil.owns = owns;
105
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
106
+ function mayOwn(text) {
107
+ return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
108
+ }
109
+ GatewayServiceUtil.mayOwn = mayOwn;
110
+ /** The calls that send the agents a message, which the limit of messages a minute counts. */
111
+ GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
112
+ function mayPrompt(text) {
113
+ return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
114
+ }
115
+ GatewayServiceUtil.mayPrompt = mayPrompt;
116
+ /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
117
+ function error(id, message, code, hint) {
118
+ return JSON.stringify({ jsonrpc: '2.0', id, error: { code: ta_protocol_1.TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
119
+ }
120
+ GatewayServiceUtil.error = error;
121
+ function result(id, result) {
122
+ return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
123
+ }
124
+ GatewayServiceUtil.result = result;
125
+ function invalid(message, hint) {
126
+ return new GatewayCallError(message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, hint);
127
+ }
128
+ function forbidden(message, hint) {
129
+ return new GatewayCallError(message, ta_protocol_1.TaErrorCode.FORBIDDEN, hint);
130
+ }
131
+ GatewayServiceUtil.forbidden = forbidden;
132
+ function choice(value, own, providers) {
133
+ var _a;
134
+ const given = (value !== null && value !== void 0 ? value : {});
135
+ if (typeof given.provider === 'string') {
136
+ if (!providers.includes(given.provider)) {
137
+ throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
138
+ }
139
+ return { provider: given.provider };
140
+ }
141
+ if (given.type === 'ta') {
142
+ if (!own) {
143
+ throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
144
+ }
145
+ let url;
146
+ try {
147
+ url = ta_sandbox_1.TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
148
+ }
149
+ catch (error) {
150
+ throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
151
+ }
152
+ if (typeof given.token !== 'string' || !given.token.trim()) {
153
+ 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.');
154
+ }
155
+ return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
156
+ }
157
+ throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
158
+ }
159
+ GatewayServiceUtil.choice = choice;
160
+ /** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
161
+ function detail(method, params) {
162
+ const [, second] = params;
163
+ switch (method) {
164
+ case 'setTenantLimits':
165
+ case 'setDefaultLimits':
166
+ return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
167
+ case 'grantMinutes':
168
+ case 'getTenantLogs':
169
+ return String(second !== null && second !== void 0 ? second : '');
170
+ case 'setSuspended':
171
+ return second ? 'suspended' : 'let go';
172
+ case 'setRole':
173
+ return String(second);
174
+ case 'setEgressHosts':
175
+ return Array.isArray(params[0]) ? params[0].join(', ') || 'any public host' : undefined;
176
+ default:
177
+ return undefined;
178
+ }
179
+ }
180
+ GatewayServiceUtil.detail = detail;
181
+ })(GatewayServiceUtil || (exports.GatewayServiceUtil = GatewayServiceUtil = {}));
182
+ //# sourceMappingURL=gateway-service-util.js.map
@@ -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 TaEnvironmentChoice, TaErrorCode, type TaGatewayConsole, type TaGatewayInfo, type TaGatewayLimits, type TaGatewayPolicy, type TaGatewayRole, type TaGatewayTenant, type TaUsageTrend } from '@celljs/ta-protocol';
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';
3
4
  import type { Environments } from './environments';
4
5
  import type { GatewayAudit } from './gateway-audit';
5
6
  import { type GatewayPolicy } from './gateway-policy';
@@ -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,5 +103,4 @@ export declare class GatewayService {
138
103
  */
139
104
  protected signInLink(account: unknown, name: unknown): string;
140
105
  }
141
- export {};
142
106
  //# sourceMappingURL=gateway-service.d.ts.map
@@ -1,185 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GatewayService = exports.GatewayServiceUtil = exports.GatewayCallError = void 0;
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
11
  const model_proxy_1 = require("./model-proxy");
10
- const ta_sandbox_1 = require("./ta-sandbox");
11
- class GatewayCallError extends Error {
12
- constructor(message, code, hint) {
13
- super(message);
14
- this.code = code;
15
- this.hint = hint;
16
- }
17
- }
18
- exports.GatewayCallError = GatewayCallError;
19
12
  const DAY = 24 * 3600000;
20
- var GatewayServiceUtil;
21
- (function (GatewayServiceUtil) {
22
- GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
23
- GatewayServiceUtil.MAX_LOG_LINES = 2000;
24
- /**
25
- * Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
26
- * `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
27
- * (GatewayService.manage).
28
- */
29
- GatewayServiceUtil.RULES = {
30
- TaGatewayServer: {
31
- getGateway: 'anyone',
32
- setEnvironment: 'anyone',
33
- restartEnvironment: 'anyone',
34
- listTenants: 'admin',
35
- getConsole: 'admin',
36
- getTenantAgents: 'admin',
37
- getAgentActivity: 'admin',
38
- getTenantLogs: 'admin',
39
- getUsageTrend: 'admin',
40
- getPolicy: 'admin',
41
- getAudit: 'admin',
42
- startTenant: 'admin',
43
- stopTenant: 'admin',
44
- restartTenant: 'admin',
45
- createSignInLink: 'admin',
46
- setTenantLimits: 'admin',
47
- grantMinutes: 'admin',
48
- setSuspended: 'admin',
49
- setDefaultLimits: 'owner',
50
- setRole: 'owner',
51
- createAccessToken: 'anyone',
52
- },
53
- TaAccessServer: {
54
- getAccess: 'anyone',
55
- signOut: 'anyone',
56
- bindUrl: 'admin',
57
- allow: 'admin',
58
- dismiss: 'admin',
59
- disallow: 'admin',
60
- },
61
- };
62
- /** What an administrator did or looked at of others: kept in the audit. */
63
- GatewayServiceUtil.AUDITED = new Set([
64
- 'startTenant',
65
- 'stopTenant',
66
- 'restartTenant',
67
- 'getTenantLogs',
68
- 'getAgentActivity',
69
- 'createSignInLink',
70
- 'setTenantLimits',
71
- 'grantMinutes',
72
- 'setSuspended',
73
- 'setDefaultLimits',
74
- 'setRole',
75
- 'allow',
76
- 'dismiss',
77
- 'disallow',
78
- ]);
79
- /** By name, own keys only: a method such as `toString` is no rule. */
80
- const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
81
- function need(service, name) {
82
- var _a;
83
- return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
84
- }
85
- GatewayServiceUtil.need = need;
86
- function allowed(role, need) {
87
- return need === 'anyone' || gateway_policy_1.GatewayPolicyUtil.rank(role) >= gateway_policy_1.GatewayPolicyUtil.rank(need);
88
- }
89
- GatewayServiceUtil.allowed = allowed;
90
- /** As many lines as asked, within bounds. */
91
- function lines(value) {
92
- const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
93
- return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
94
- }
95
- GatewayServiceUtil.lines = lines;
96
- /** Days of a trend, 1 to 31. */
97
- function days(value) {
98
- const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
99
- return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
100
- }
101
- GatewayServiceUtil.days = days;
102
- /** The services the gateway answers itself, not the Ta of the tenant. */
103
- GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
104
- function owns(method) {
105
- return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
106
- }
107
- GatewayServiceUtil.owns = owns;
108
- /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
109
- function mayOwn(text) {
110
- return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
111
- }
112
- GatewayServiceUtil.mayOwn = mayOwn;
113
- /** The calls that send the agents a message, which the limit of messages a minute counts. */
114
- GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
115
- function mayPrompt(text) {
116
- return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
117
- }
118
- GatewayServiceUtil.mayPrompt = mayPrompt;
119
- /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
120
- function error(id, message, code, hint) {
121
- return JSON.stringify({ jsonrpc: '2.0', id, error: { code: ta_protocol_1.TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
122
- }
123
- GatewayServiceUtil.error = error;
124
- function result(id, result) {
125
- return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
126
- }
127
- GatewayServiceUtil.result = result;
128
- function invalid(message, hint) {
129
- return new GatewayCallError(message, ta_protocol_1.TaErrorCode.INVALID_ARGUMENT, hint);
130
- }
131
- function forbidden(message, hint) {
132
- return new GatewayCallError(message, ta_protocol_1.TaErrorCode.FORBIDDEN, hint);
133
- }
134
- GatewayServiceUtil.forbidden = forbidden;
135
- function choice(value, own, providers) {
136
- var _a;
137
- const given = (value !== null && value !== void 0 ? value : {});
138
- if (typeof given.provider === 'string') {
139
- if (!providers.includes(given.provider)) {
140
- throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
141
- }
142
- return { provider: given.provider };
143
- }
144
- if (given.type === 'ta') {
145
- if (!own) {
146
- throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
147
- }
148
- let url;
149
- try {
150
- url = ta_sandbox_1.TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
151
- }
152
- catch (error) {
153
- throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
154
- }
155
- if (typeof given.token !== 'string' || !given.token.trim()) {
156
- 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.');
157
- }
158
- return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
159
- }
160
- throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
161
- }
162
- GatewayServiceUtil.choice = choice;
163
- /** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
164
- function detail(method, params) {
165
- const [, second] = params;
166
- switch (method) {
167
- case 'setTenantLimits':
168
- case 'setDefaultLimits':
169
- return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
170
- case 'grantMinutes':
171
- case 'getTenantLogs':
172
- return String(second !== null && second !== void 0 ? second : '');
173
- case 'setSuspended':
174
- return second ? 'suspended' : 'let go';
175
- case 'setRole':
176
- return String(second);
177
- default:
178
- return undefined;
179
- }
180
- }
181
- GatewayServiceUtil.detail = detail;
182
- })(GatewayServiceUtil || (exports.GatewayServiceUtil = GatewayServiceUtil = {}));
183
13
  /**
184
14
  * The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
185
15
  * accounts are those that may have a tenant here. Each method needs a role (GatewayServiceUtil.RULES); what an
@@ -188,6 +18,7 @@ var GatewayServiceUtil;
188
18
  class GatewayService {
189
19
  constructor(parts) {
190
20
  this.models = parts.models;
21
+ this.egress = parts.egress;
191
22
  this.flow = parts.flow;
192
23
  this.tenants = parts.tenants;
193
24
  this.environments = parts.environments;
@@ -200,17 +31,17 @@ class GatewayService {
200
31
  }
201
32
  async call(caller, method, params) {
202
33
  const [service, name] = method.split('/');
203
- const need = GatewayServiceUtil.need(service, name);
34
+ const need = gateway_service_util_1.GatewayServiceUtil.need(service, name);
204
35
  if (!need) {
205
- 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.');
206
37
  }
207
- if (!GatewayServiceUtil.allowed(caller.role, need)) {
38
+ if (!gateway_service_util_1.GatewayServiceUtil.allowed(caller.role, need)) {
208
39
  throw need === 'owner'
209
- ? GatewayServiceUtil.forbidden('Only owners of the gateway may do that.', 'Ask one of those of TA_GATEWAY_ADMINS.')
210
- : 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.');
211
42
  }
212
43
  const result = await this.handle(caller, service, name, params);
213
- if (GatewayServiceUtil.AUDITED.has(name)) {
44
+ if (gateway_service_util_1.GatewayServiceUtil.AUDITED.has(name)) {
214
45
  this.record(caller, name, params);
215
46
  }
216
47
  return result;
@@ -239,6 +70,7 @@ class GatewayService {
239
70
  setSuspended: () => this.setSuspended(caller, first, second),
240
71
  setDefaultLimits: () => this.setDefaultLimits(first),
241
72
  setRole: () => this.setRole(caller, first, second),
73
+ setEgressHosts: () => this.setEgressHosts(first),
242
74
  createAccessToken: () => this.accessToken(caller, first),
243
75
  }
244
76
  : {
@@ -257,7 +89,7 @@ class GatewayService {
257
89
  const [first] = params;
258
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;
259
91
  try {
260
- 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) });
261
93
  }
262
94
  catch (error) {
263
95
  console.warn(`ta gateway: the audit could not be written: ${error.message}`);
@@ -284,7 +116,7 @@ class GatewayService {
284
116
  var _a;
285
117
  const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== void 0 ? _a : caller.tenant;
286
118
  const limits = this.quotas.limits(tenant);
287
- 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)));
288
120
  const next = await this.environments.change(tenant, choice);
289
121
  caller.tenant = next;
290
122
  // Everyone of the tenant connects again, once the browser that asked has its answer.
@@ -356,12 +188,25 @@ class GatewayService {
356
188
  };
357
189
  }
358
190
  policyView() {
359
- return this.policy.view(this.environments.settings.idleMinutes);
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();
360
205
  }
361
206
  tenant(id) {
362
207
  const tenant = typeof id === 'string' ? this.tenants.get(id) : undefined;
363
208
  if (!tenant) {
364
- 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.');
365
210
  }
366
211
  return tenant;
367
212
  }
@@ -369,7 +214,7 @@ class GatewayService {
369
214
  manage(caller, id) {
370
215
  const tenant = this.tenant(id);
371
216
  if (caller.role !== 'owner' && (this.quotas.role(tenant) !== 'member' || tenant.account === caller.account)) {
372
- 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).');
373
218
  }
374
219
  return tenant;
375
220
  }
@@ -389,14 +234,14 @@ class GatewayService {
389
234
  }
390
235
  /** What its sandbox wrote, its secrets hidden: tokens and keys printed by what ran there do not reach the page. */
391
236
  async logs(id, lines) {
392
- 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)));
393
238
  }
394
239
  activity(id, sessionId) {
395
240
  return this.environments.activity(this.tenant(id), String(sessionId));
396
241
  }
397
242
  async trend(daysValue, id) {
398
243
  const now = Date.now();
399
- const days = GatewayServiceUtil.days(daysValue);
244
+ const days = gateway_service_util_1.GatewayServiceUtil.days(daysValue);
400
245
  const tenants = id === undefined || id === null ? this.tenants.list() : [this.tenant(id)];
401
246
  const fresh = await Promise.all(tenants.map(tenant => this.usage.refresh(tenant.id, this.environments.usageReader(tenant), now, 0).catch(() => false)));
402
247
  const byTenant = new Map();
@@ -420,7 +265,7 @@ class GatewayService {
420
265
  }
421
266
  catch (error) {
422
267
  if (error instanceof gateway_policy_1.PolicyError) {
423
- 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.');
424
269
  }
425
270
  throw error;
426
271
  }
@@ -432,7 +277,7 @@ class GatewayService {
432
277
  grantMinutes(caller, id, minutes) {
433
278
  const tenant = this.manage(caller, id);
434
279
  if (typeof minutes !== 'number' || !Number.isInteger(minutes) || Math.abs(minutes) > 1000000) {
435
- 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.');
436
281
  }
437
282
  this.quotas.grant(tenant, minutes);
438
283
  }
@@ -451,13 +296,13 @@ class GatewayService {
451
296
  setRole(caller, account, role) {
452
297
  const id = typeof account === 'string' ? account.trim() : '';
453
298
  if (role !== 'admin' && role !== 'member') {
454
- 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.');
455
300
  }
456
301
  if (this.policy.role(id) === 'owner') {
457
- 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.');
458
303
  }
459
304
  if (id === caller.account || !this.tenants.list().some(tenant => tenant.account === id)) {
460
- 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.');
461
306
  }
462
307
  this.policy.setRole(id, role);
463
308
  return this.policyView();
@@ -481,7 +326,7 @@ class GatewayService {
481
326
  }
482
327
  allow(account, name) {
483
328
  if (typeof account !== 'string' || !/^[a-z][a-z0-9-]*:\S+$/.test(account.trim())) {
484
- 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.');
485
330
  }
486
331
  this.flow.store.allow(account.trim(), typeof name === 'string' ? name : undefined);
487
332
  }
@@ -490,7 +335,7 @@ class GatewayService {
490
335
  const normalized = access_store_1.AccessUtil.normalize(account);
491
336
  const role = this.policy.role(normalized);
492
337
  if (role === 'owner' || (role === 'admin' && caller.role !== 'owner')) {
493
- 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.');
494
339
  }
495
340
  if (role === 'admin') {
496
341
  this.policy.setRole(normalized, 'member');
@@ -508,7 +353,7 @@ class GatewayService {
508
353
  async signOut(caller, id) {
509
354
  const own = this.flow.store.info(caller.token).signIns.find(signIn => signIn.id === id);
510
355
  if (!caller.admin && (own === null || own === void 0 ? void 0 : own.account) !== caller.account) {
511
- 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.');
512
357
  }
513
358
  this.flow.store.signOut(id);
514
359
  this.hooks.cutOff();
@@ -520,10 +365,10 @@ class GatewayService {
520
365
  signInLink(account, name) {
521
366
  const id = typeof account === 'string' ? account.trim() : '';
522
367
  if (!/^[a-z][a-z0-9-]*:\S+$/.test(id)) {
523
- 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.');
524
369
  }
525
370
  if (this.tenants.list().some(tenant => tenant.account === id) || this.policy.role(id) !== 'member') {
526
- 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.');
527
372
  }
528
373
  return this.meta.links.create(id, typeof name === 'string' && name.trim() ? name.trim() : undefined);
529
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 { type GatewayCaller, type GatewayService } from './gateway-service';
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