Repository navigation
Expand file tree
/
Copy pathServiceFactory.ts
More file actions
419 lines (365 loc) · 21.1 KB
/
Copy pathServiceFactory.ts
File metadata and controls
419 lines (365 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { Message } from "rs-core/Message.ts";
import { MessageFunction, Service } from "rs-core/Service.ts";
import { Source } from "rs-core/Source.ts";
import { Url } from "rs-core/Url.ts";
import { adapterConfigFromInfra, assertInfraAvailableForTenant, config, infraAvailableForTenant, Infra } from "./config.ts";
import { IAccessControl, IServiceConfigTemplate, IServiceConfig } from "rs-core/IServiceConfig.ts";
import { ServiceWrapper } from "./ServiceWrapper.ts";
import { applyServiceConfigTemplate } from "./Modules.ts";
import { IAdapter, IDisposableAdapter } from "rs-core/adapter/IAdapter.ts";
import { getErrors } from "rs-core/utility/errors.ts";
import { IAdapterManifest, IServiceManifest } from "rs-core/IManifest.ts";
import { BaseStateClass, ServiceContext, StateFunction } from "rs-core/ServiceContext.ts";
import { handleIncomingRequest, handleOutgoingRequestWithPrivateServices } from "./handleRequest.ts";
import { pathCombine } from "rs-core/utility/utility.ts";
interface ITemplateConfigFromManifest {
serviceConfigTemplates?: Record<string, IServiceConfigTemplate>;
prePipeline?: Record<string, unknown>;
postPipeline?: Record<string, unknown>;
}
type GetPrivateManifestsOutput = (string | IServiceManifest)[];
type AccessRoleProperty = keyof IAccessControl;
const accessRoleProperties: AccessRoleProperty[] = [ "readRoles", "writeRoles", "manageRoles", "createRoles" ];
const roleTokens = (roleSpec?: string) => (roleSpec || "")
.split(" ")
.map(role => role.trim())
.filter(role => !!role);
/** Service message function creation and manifest caching for a tenant */
export class ServiceFactory {
// souce is url with domain or file path
serviceManifestsBySource = {} as { [ manifestSource: string ]: IServiceManifest };
adapterManifestsBySource = {} as { [ manifestSource: string ]: IAdapterManifest };
serviceConfigs = null as Record<string, IServiceConfig> | null;
constructor(public tenant: string, public primaryDomain: string) {
}
private async closeDisposableAdapters(serviceContext: ServiceContext<IAdapter>) {
const requestContext = serviceContext as ServiceContext<IAdapter> & { __disposables?: IDisposableAdapter[] };
const disposables = requestContext.__disposables;
if (!disposables || disposables.length === 0) return;
const closed = new Set<IDisposableAdapter>();
for (let i = disposables.length - 1; i >= 0; i--) {
const adapter = disposables[i];
if (closed.has(adapter)) continue;
closed.add(adapter);
try {
await adapter.close();
} catch (err) {
serviceContext.logger.error(`Failed to close adapter: ${err}`);
}
}
disposables.length = 0;
}
/** loads all manifests required by serviceConfigs and resolves private services */
async loadServiceManifests(serviceManifestSources: string[]) {
config.logger.debug(`Start -- loading manifests`, this.tenant);
// get promises to get service manifests
const uniqueServiceManifestSources = serviceManifestSources.filter((ms, i) => serviceManifestSources.indexOf(ms) === i);
const getServiceManifestPromises = uniqueServiceManifestSources.map(source => config.modules.getServiceManifest(source, this.tenant, this.primaryDomain));
const serviceManifests = await Promise.all<string | IServiceManifest>(getServiceManifestPromises);
const errors = serviceManifests.filter(m => typeof m === 'string') as string[];
if (errors.length) throw new Error('failed to load service manifests: ' + errors.join('; '));
uniqueServiceManifestSources.forEach((source, i) =>
this.serviceManifestsBySource[source] = serviceManifests[i] as IServiceManifest);
// get private service manifests
const privateServiceManifests = await this.getPrivateServiceManifests(
Object.keys(this.serviceManifestsBySource),
Object.values(this.serviceManifestsBySource)
);
const privateServiceErrors = privateServiceManifests.filter(m => typeof m === 'string') as string[];
if (privateServiceErrors.length) throw new Error('failed to load manifests: ' + privateServiceErrors.join('; '));
}
async loadAdapterManifests() {
// get promises to get adapter manifests
const adapterManifestSources = Object.values(this.serviceConfigs!)
.filter(sc => sc.adapterSource)
.map(sc => sc.adapterSource) as string[];
const infraNames = Object.values(this.serviceConfigs!)
.filter(sc => sc.infraName)
.map(sc => sc.infraName);
const missingInfraNames = infraNames.filter(i => !config.server.infra[i as string]);
if (missingInfraNames.length) {
throw new Error(`tenant ${this.tenant} has infra names that don't exist: ${missingInfraNames.join(', ')}`);
}
const unavailableInfraNames = infraNames
.filter(i => !infraAvailableForTenant(config.server.infra[i as string], this.tenant));
if (unavailableInfraNames.length) {
throw new Error(`tenant ${this.tenant} cannot use infra names: ${unavailableInfraNames.join(', ')}`);
}
const adapterInfraManifestSources = infraNames
.map(i => config.server.infra[i as string].adapterSource) as string[];
const allAdapterManifestSources = [ ...adapterManifestSources, ...adapterInfraManifestSources];
const uniqueAdapterManifestSources = allAdapterManifestSources
.filter((ms, i) => allAdapterManifestSources.indexOf(ms) === i);
const getAdapterManifestPromises = uniqueAdapterManifestSources
.map(source => config.modules.getAdapterManifest(source, this.tenant, this.primaryDomain));
// get all the manifests
const adapterManifests = await Promise.all<string | IAdapterManifest>(getAdapterManifestPromises);
const errors = adapterManifests.filter(m => typeof m === 'string') as string[];
if (errors.length) throw new Error('failed to load adapter manifests: ' + errors.join('; '));
uniqueAdapterManifestSources.forEach((source, i) =>
this.adapterManifestsBySource[source] = adapterManifests[i] as IAdapterManifest);
config.logger.debug(`End -- loading manifests`, this.tenant);
}
/** Get the manifests for all the private services of the given list of service manifests */
private async getPrivateServiceManifests(existingServiceSources: string[], serviceManifests: IServiceManifest[]): Promise<GetPrivateManifestsOutput> {
if (serviceManifests.length === 0) return [];
// get manifest sources for all the private services of all the serviceManifests
const privateServiceSources = serviceManifests
.flatMap(sc => sc.privateServices
? Object.values(sc.privateServices).map(ps => ps.source)
: [])
.filter(s => !existingServiceSources.includes(s));
const manifestsLayer0 = await Promise.all(privateServiceSources.map(pss => config.modules.getServiceManifest(pss, this.tenant, this.primaryDomain)));
// bail on any error
if (manifestsLayer0.some(m => typeof m === 'string')) return manifestsLayer0;
privateServiceSources.forEach((source, i) => this.serviceManifestsBySource[source] = manifestsLayer0[i] as IServiceManifest);
const manifestsOtherLayers = await this.getPrivateServiceManifests(
[ ...existingServiceSources, ...privateServiceSources ],
manifestsLayer0 as IServiceManifest[]
);
return manifestsLayer0.concat(manifestsOtherLayers);
}
private addPrivateServiceConfig(serviceConfig: IServiceConfig, manifest: IServiceManifest): IServiceConfig {
if (!manifest.privateServices) return serviceConfig;
const privateServiceConfigs = {} as Record<string, IServiceConfig>;
Object.entries(manifest.privateServices).forEach(([ name, configTemplate ]) => {
let innerServiceConfig = applyServiceConfigTemplate(serviceConfig, configTemplate);
innerServiceConfig.source = configTemplate.source;
// Convention is the base path of a private service is the parent's basePath as the root and the private service name prefixed by '*'
innerServiceConfig.basePath = pathCombine(serviceConfig.basePath, '*' + name);
const innerManifest = this.serviceManifestsBySource[innerServiceConfig.source];
innerServiceConfig = this.addPrivateServiceConfig(innerServiceConfig, innerManifest);
privateServiceConfigs[name] = innerServiceConfig;
});
const newServiceConfig = {
...serviceConfig,
manifestConfig: {
prePipeline: manifest.prePipeline,
postPipeline: manifest.postPipeline,
privateServiceConfigs
}
} as IServiceConfig;
return newServiceConfig;
}
async infraForAdapterInterface(adapterInterface: string) {
let infraName = '';
for (const [ name, infra ] of Object.entries(config.server.infra)) {
if (!infraAvailableForTenant(infra, this.tenant)) continue;
const adapterManifest = await config.modules.getAdapterManifest(infra.adapterSource, this.tenant);
if (typeof adapterManifest === 'string') {
config.logger.error('Failed to load adapter manifest: ' + adapterManifest);
} else if (adapterManifest.adapterInterfaces.includes(adapterInterface)) {
if (adapterManifest.moduleUrl?.startsWith('./')) {
return name;
} else {
infraName = name;
}
}
}
return infraName;
}
private async getAdapterManifestForSource(adapterSource: string) {
const adapterManifest = this.adapterManifestsBySource[adapterSource]
|| await config.modules.getAdapterManifest(adapterSource, this.tenant, this.primaryDomain);
if (typeof adapterManifest === 'string') {
throw new Error(`failed to load adapter manifest ${adapterSource}: ${adapterManifest}`);
}
return adapterManifest;
}
private async rejectInfraOnlyServiceAdapterConfig(
serviceConfig: IServiceConfig,
adapterSource: string,
adapterConfig: Record<string, unknown>,
) {
const adapterManifest = await this.getAdapterManifestForSource(adapterSource);
const infraOnlyProperties = adapterManifest.infraOnlyConfigProperties || [];
const suppliedInfraOnlyProperties = Object.keys(adapterConfig)
.filter(prop => infraOnlyProperties.includes(prop));
if (suppliedInfraOnlyProperties.length) {
throw new Error(
`service ${serviceConfig.name} adapterConfig cannot set infra-only adapter config properties: ${suppliedInfraOnlyProperties.join(', ')}`
);
}
}
private isElasticsearchAdapter(adapterManifest: IAdapterManifest) {
const moduleUrl = adapterManifest.moduleUrl;
if (!moduleUrl) return false;
return [
"./adapter/ElasticDataAdapter.ts",
"./adapter/ElasticQueryAdapter.ts",
].includes(moduleUrl);
}
private async rejectUnsafeElasticsearchTenantIndexes(infraName: string, infra: Infra) {
if (infra.tenantIndexes !== false) return;
const adapterManifest = await this.getAdapterManifestForSource(infra.adapterSource);
if (!this.isElasticsearchAdapter(adapterManifest)) return;
const allowedTenants = infra.allowedTenants;
const hasNonEmptyAllowedTenants = Array.isArray(allowedTenants) &&
allowedTenants.length > 0 &&
allowedTenants.every(tenant => typeof tenant === "string" && tenant.length > 0);
if (!hasNonEmptyAllowedTenants) {
throw new Error(
`infra ${infraName} sets tenantIndexes false for Elasticsearch and must specify a non-empty allowedTenants string array`
);
}
}
private assertRequiredAccess(serviceConfig: IServiceConfig, manifest: IServiceManifest) {
if (!manifest.requiredAccess) return;
for (const property of accessRoleProperties) {
const requiredRoles = roleTokens(manifest.requiredAccess[property]);
if (requiredRoles.length === 0) continue;
const configuredRoles = roleTokens(serviceConfig.access?.[property]);
if (configuredRoles.includes("all")) {
throw new Error(`service ${serviceConfig.name} access.${property} must not be all; manifest requires one of: ${requiredRoles.join(", ")}`);
}
if (!requiredRoles.some(role => configuredRoles.includes(role))) {
throw new Error(`service ${serviceConfig.name} access.${property} must include one of: ${requiredRoles.join(", ")}`);
}
}
}
async extendContextWithAdapter(serviceConfig: IServiceConfig, serviceContext: ServiceContext<IAdapter>) {
let adapter: IAdapter | undefined = undefined;
if (serviceConfig.adapterSource || serviceConfig.infraName) {
const adapterConfig = { ...serviceConfig.adapterConfig };
let adapterSource = serviceConfig.adapterSource;
if (serviceConfig.infraName) {
const infra = config.server.infra[serviceConfig.infraName];
assertInfraAvailableForTenant(serviceConfig.infraName, infra, this.tenant);
adapterSource = infra.adapterSource;
await this.rejectInfraOnlyServiceAdapterConfig(serviceConfig, adapterSource, adapterConfig);
await this.rejectUnsafeElasticsearchTenantIndexes(serviceConfig.infraName, infra);
Object.assign(adapterConfig, adapterConfigFromInfra(infra));
} else {
await this.rejectInfraOnlyServiceAdapterConfig(serviceConfig, adapterSource as string, adapterConfig);
}
const canonicalAdapterSource = config.canonicaliseUrl(adapterSource as string, this.tenant, this.primaryDomain);
const validator = config.modules.validateAdapterConfig[canonicalAdapterSource];
if (!validator(adapterConfig as any)) {
throw new Error(`failed to validate adapter config for service ${serviceConfig.name}: ${getErrors(validator)}`);
}
adapter = await serviceContext.getAdapter(adapterSource as string, adapterConfig);
return { ...serviceContext, adapter } as ServiceContext<IAdapter>;
}
return serviceContext;
}
async initService(serviceConfig: IServiceConfig, serviceContext: ServiceContext<IAdapter>, oldState?: BaseStateClass): Promise<void> {
const service = await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain);
const manifest = this.serviceManifestsBySource[serviceConfig.source];
this.assertRequiredAccess(serviceConfig, manifest);
serviceContext.manifest = manifest;
await service.initFunc(serviceContext, serviceConfig, oldState);
}
async getMessageFunctionForService(serviceConfig: IServiceConfig, serviceContext: ServiceContext<IAdapter>, source: Source): Promise<MessageFunction> {
const service = await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain);
const manifest = this.serviceManifestsBySource[serviceConfig.source];
serviceConfig = this.addPrivateServiceConfig(serviceConfig, manifest);
const handlerWithPrivateServices = (msg: Message, source?: Source, retry?: IServiceConfig["retry"] | null) => {
if (!msg.url.domain) msg.url.domain = config.tenants[this.tenant].primaryDomain;
return source === Source.External
? handleIncomingRequest(msg)
: handleOutgoingRequestWithPrivateServices(
serviceConfig.basePath,
serviceConfig.manifestConfig?.privateServiceConfigs || {},
this.tenant,
serviceContext
)(msg, source, retry);
}
serviceContext = { ...serviceContext, manifest, makeRequest: handlerWithPrivateServices };
const canonicalSource = config.canonicaliseUrl(serviceConfig.source, this.tenant, this.primaryDomain);
const configValidator = config.modules.validateServiceConfig[canonicalSource];
const serviceName = serviceConfig.name;
if (!configValidator(serviceConfig as any)) {
throw new Error(`failed to validate config for service ${serviceName}: ${getErrors(configValidator)}`);
}
this.assertRequiredAccess(serviceConfig, manifest);
serviceContext = await this.extendContextWithAdapter(serviceConfig, serviceContext);
const serviceWrapper = new ServiceWrapper(service);
const sourceServiceFunc = source === Source.External || source === Source.Outer ? serviceWrapper.external(source) : serviceWrapper.internal;
// protect data sent to func against modification within it
serviceContext.manifest = structuredClone(serviceContext.manifest);
const copyServiceConfig = structuredClone(serviceConfig);
return async (msg: Message) => {
try {
return await sourceServiceFunc(msg, serviceContext, copyServiceConfig);
} finally {
await this.closeDisposableAdapters(serviceContext);
}
};
}
attachFilter(url: Url, func: MessageFunction, context: ServiceContext<IAdapter>): MessageFunction {
const filterUrl = url.query['$filter']?.[0];
if (filterUrl) {
const url = new Url(filterUrl);
const newFunc: MessageFunction = async (msg: Message, retry) => {
const msg2 = await func(msg);
msg2.setUrl(url).setMethod('POST');
return context.makeRequest(msg2, Source.Outer, retry);
};
return newFunc;
} else {
return func;
}
}
basePathFromUrl(url: Url) {
if (!this.serviceConfigs) return null;
const pathParts = [ ...url.pathElements ];
let basePath = '/' + pathParts.join('/') + '.';
let serviceConfig = this.serviceConfigs[basePath];
if (serviceConfig) return basePath;
while (true) {
basePath = '/' + pathParts.join('/');
serviceConfig = this.serviceConfigs[basePath];
if (serviceConfig) return basePath;
if (pathParts.length === 0) break;
pathParts.pop();
}
return null;
}
/** select service with longest path match */
async getMessageFunctionByUrl(url: Url, serviceContext: ServiceContext<IAdapter>, stateByBasePath: (basePath: string) => StateFunction, source: Source): Promise<[MessageFunction, string]> {
if (this.serviceConfigs!['()'] && source === Source.External) {
// the service with outer basePath (i.e. "()") can only make requests with Source.Outer.
// Source.External would cause it to recurse. This service should be accessible by anyone who
// can access the site: Source.Outer delegates auth checking to services it goes on to call.
const newServiceContext = {
...serviceContext,
makeRequest: (msg: Message, _source?: Source, retry?: IServiceConfig["retry"] | null) => serviceContext.makeRequest(msg, Source.Outer, retry)
} as ServiceContext<IAdapter>;
return [
await this.getMessageFunctionForService(this.serviceConfigs!['()'], newServiceContext, source),
'()'
];
}
const configPath = this.basePathFromUrl(url);
if (configPath) {
const serviceConfig = this.serviceConfigs![configPath];
serviceContext.state = stateByBasePath(configPath);
const innerFunc = await this.getMessageFunctionForService(serviceConfig, serviceContext, source);
return [
this.attachFilter(url, innerFunc, serviceContext),
serviceConfig.name
];
}
return [
(msg: Message) =>
Promise.resolve(
msg.method === 'OPTIONS'
? config.server.setServerCors(msg).setStatus(204)
: config.server.setServerCors(msg).setStatus(404, 'Not found')
),
'?'
];
}
getServiceConfigByApi(api: string): IServiceConfig | undefined {
const apiManifests = Object.entries(this.serviceManifestsBySource).filter(([, m]) => (m.apis || []).some(mApi => mApi === api));
if (apiManifests.length === 0) return undefined;
const [ manifestSource, ] = apiManifests[0];
if (!manifestSource) return undefined;
return Object.values(this.serviceConfigs!).find(config => config.source === manifestSource);
}
async getServiceAndConfigByApi(api: string): Promise<[ Service, IServiceConfig ] | null> {
const serviceConfig = this.getServiceConfigByApi(api);
if (!serviceConfig) return null;
return [ await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain), serviceConfig ];
}
}