@contember/engine-http 1.3.0-alpha.1 → 1.3.0-alpha.4
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/dist/src/MasterContainer.d.ts +138 -0
- package/dist/src/MasterContainer.d.ts.map +1 -0
- package/dist/src/MasterContainer.js +94 -0
- package/dist/src/MasterContainer.js.map +1 -0
- package/dist/src/application/application.d.ts +63 -0
- package/dist/src/application/application.d.ts.map +1 -0
- package/dist/src/application/application.js +343 -0
- package/dist/src/application/application.js.map +1 -0
- package/dist/src/application/index.d.ts +3 -0
- package/dist/src/application/index.d.ts.map +1 -0
- package/dist/src/{koa → application}/index.js +1 -2
- package/dist/src/application/index.js.map +1 -0
- package/dist/src/application/types.d.ts.map +1 -0
- package/dist/src/{koa → application}/types.js.map +1 -1
- package/dist/src/bootstrap/Initializer.d.ts +7 -0
- package/dist/src/bootstrap/Initializer.d.ts.map +1 -0
- package/dist/src/bootstrap/Initializer.js +27 -0
- package/dist/src/bootstrap/Initializer.js.map +1 -0
- package/dist/src/bootstrap/index.d.ts +2 -0
- package/dist/src/bootstrap/index.d.ts.map +1 -0
- package/dist/src/bootstrap/index.js +18 -0
- package/dist/src/bootstrap/index.js.map +1 -0
- package/dist/src/common/Authorizator.d.ts +5 -8
- package/dist/src/common/Authorizator.d.ts.map +1 -1
- package/dist/src/common/Authorizator.js +11 -6
- package/dist/src/common/Authorizator.js.map +1 -1
- package/dist/src/common/HttpResponse.d.ts +12 -0
- package/dist/src/common/HttpResponse.d.ts.map +1 -0
- package/dist/src/common/HttpResponse.js +20 -0
- package/dist/src/common/HttpResponse.js.map +1 -0
- package/dist/src/common/ModuleInfoMiddleware.d.ts +1 -1
- package/dist/src/common/ModuleInfoMiddleware.d.ts.map +1 -1
- package/dist/src/common/index.d.ts +1 -5
- package/dist/src/common/index.d.ts.map +1 -1
- package/dist/src/common/index.js +1 -5
- package/dist/src/common/index.js.map +1 -1
- package/dist/src/config/ConfigProcessor.d.ts +13 -0
- package/dist/src/config/ConfigProcessor.d.ts.map +1 -0
- package/dist/src/{ProjectContainer.js → config/ConfigProcessor.js} +1 -1
- package/dist/src/config/ConfigProcessor.js.map +1 -0
- package/dist/src/config/config.d.ts +18 -0
- package/dist/src/config/config.d.ts.map +1 -0
- package/dist/src/config/config.js +30 -0
- package/dist/src/config/config.js.map +1 -0
- package/dist/src/config/configSchema.d.ts +201 -0
- package/dist/src/config/configSchema.d.ts.map +1 -0
- package/dist/src/config/configSchema.js +119 -0
- package/dist/src/config/configSchema.js.map +1 -0
- package/dist/src/config/configTemplate.d.ts +2 -0
- package/dist/src/config/configTemplate.d.ts.map +1 -0
- package/dist/src/config/configTemplate.js +91 -0
- package/dist/src/config/configTemplate.js.map +1 -0
- package/dist/src/config/projectConfigResolver.d.ts +8 -0
- package/dist/src/config/projectConfigResolver.d.ts.map +1 -0
- package/dist/src/config/projectConfigResolver.js +91 -0
- package/dist/src/config/projectConfigResolver.js.map +1 -0
- package/dist/src/config/tenantConfigResolver.d.ts +8 -0
- package/dist/src/config/tenantConfigResolver.d.ts.map +1 -0
- package/dist/src/config/tenantConfigResolver.js +31 -0
- package/dist/src/config/tenantConfigResolver.js.map +1 -0
- package/dist/src/content/ContentApiControllerFactory.d.ts +14 -0
- package/dist/src/content/ContentApiControllerFactory.d.ts.map +1 -0
- package/dist/src/content/{ContentApiMiddlewareFactory.js → ContentApiControllerFactory.js} +32 -41
- package/dist/src/content/ContentApiControllerFactory.js.map +1 -0
- package/dist/src/content/ContentGraphQLContextFactory.d.ts +14 -6
- package/dist/src/content/ContentGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/content/ContentGraphQLContextFactory.js +17 -11
- package/dist/src/content/ContentGraphQLContextFactory.js.map +1 -1
- package/dist/src/content/GraphQLSchemaContributor.d.ts +14 -0
- package/dist/src/content/GraphQLSchemaContributor.d.ts.map +1 -0
- package/dist/src/{ProjectGroupContainer.js → content/GraphQLSchemaContributor.js} +1 -1
- package/dist/src/content/GraphQLSchemaContributor.js.map +1 -0
- package/dist/src/content/GraphQlSchemaFactory.d.ts +1 -1
- package/dist/src/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/src/content/NotModifiedChecker.d.ts +7 -6
- package/dist/src/content/NotModifiedChecker.d.ts.map +1 -1
- package/dist/src/content/NotModifiedChecker.js +5 -6
- package/dist/src/content/NotModifiedChecker.js.map +1 -1
- package/dist/src/content/ProjectMembershipResolver.js +5 -5
- package/dist/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/src/content/index.d.ts +2 -1
- package/dist/src/content/index.d.ts.map +1 -1
- package/dist/src/content/index.js +2 -1
- package/dist/src/content/index.js.map +1 -1
- package/dist/src/graphql/state.d.ts +1 -1
- package/dist/src/graphql/state.d.ts.map +1 -1
- package/dist/src/index.d.ts +18 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +24 -7
- package/dist/src/index.js.map +1 -1
- package/dist/src/misc/HomepageMiddlewareFactory.d.ts +2 -1
- package/dist/src/misc/HomepageMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/misc/HomepageMiddlewareFactory.js +4 -8
- package/dist/src/misc/HomepageMiddlewareFactory.js.map +1 -1
- package/dist/src/misc/PlaygroundMiddlewareFactory.d.ts +2 -2
- package/dist/src/misc/PlaygroundMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/misc/PlaygroundMiddlewareFactory.js +6 -22
- package/dist/src/misc/PlaygroundMiddlewareFactory.js.map +1 -1
- package/dist/src/plugin/Plugin.d.ts +21 -0
- package/dist/src/plugin/Plugin.d.ts.map +1 -0
- package/dist/src/{project-common/ProjectGroupResolver.js → plugin/Plugin.js} +1 -1
- package/dist/src/plugin/Plugin.js.map +1 -0
- package/dist/src/project/ProjectContainer.d.ts +80 -0
- package/dist/src/project/ProjectContainer.d.ts.map +1 -0
- package/dist/src/project/ProjectContainer.js +70 -0
- package/dist/src/project/ProjectContainer.js.map +1 -0
- package/dist/src/project/ProjectContainerResolver.d.ts +31 -0
- package/dist/src/project/ProjectContainerResolver.d.ts.map +1 -0
- package/dist/src/project/ProjectContainerResolver.js +77 -0
- package/dist/src/project/ProjectContainerResolver.js.map +1 -0
- package/dist/src/project/ProjectContainerStore.d.ts +18 -0
- package/dist/src/project/ProjectContainerStore.d.ts.map +1 -0
- package/dist/src/project/ProjectContainerStore.js +30 -0
- package/dist/src/project/ProjectContainerStore.js.map +1 -0
- package/dist/src/project/ProjectInitializer.d.ts +14 -0
- package/dist/src/project/ProjectInitializer.d.ts.map +1 -0
- package/dist/src/project/ProjectInitializer.js +28 -0
- package/dist/src/project/ProjectInitializer.js.map +1 -0
- package/dist/src/project/ProjectSchemaResolver.d.ts +14 -0
- package/dist/src/project/ProjectSchemaResolver.d.ts.map +1 -0
- package/dist/src/project/ProjectSchemaResolver.js +30 -0
- package/dist/src/project/ProjectSchemaResolver.js.map +1 -0
- package/dist/src/project/config.d.ts.map +1 -0
- package/dist/src/{config.js.map → project/config.js.map} +1 -1
- package/dist/src/project/index.d.ts +5 -0
- package/dist/src/project/index.d.ts.map +1 -0
- package/dist/src/project/index.js +21 -0
- package/dist/src/project/index.js.map +1 -0
- package/dist/src/project-common/ProjectContextResolver.d.ts +8 -0
- package/dist/src/project-common/ProjectContextResolver.d.ts.map +1 -0
- package/dist/src/project-common/ProjectContextResolver.js +17 -0
- package/dist/src/project-common/ProjectContextResolver.js.map +1 -0
- package/dist/src/project-common/index.d.ts +1 -1
- package/dist/src/project-common/index.d.ts.map +1 -1
- package/dist/src/project-common/index.js +1 -1
- package/dist/src/project-common/index.js.map +1 -1
- package/dist/src/projectGroup/ProjectGroupContainer.d.ts +44 -0
- package/dist/src/projectGroup/ProjectGroupContainer.d.ts.map +1 -0
- package/dist/src/projectGroup/ProjectGroupContainer.js +72 -0
- package/dist/src/projectGroup/ProjectGroupContainer.js.map +1 -0
- package/dist/src/projectGroup/ProjectGroupResolver.d.ts +14 -0
- package/dist/src/projectGroup/ProjectGroupResolver.d.ts.map +1 -0
- package/dist/src/projectGroup/ProjectGroupResolver.js +13 -0
- package/dist/src/projectGroup/ProjectGroupResolver.js.map +1 -0
- package/dist/src/system/SystemApiMiddlewareFactory.d.ts +5 -11
- package/dist/src/system/SystemApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/system/SystemApiMiddlewareFactory.js +28 -44
- package/dist/src/system/SystemApiMiddlewareFactory.js.map +1 -1
- package/dist/src/system/SystemGraphQLContextFactory.d.ts +4 -4
- package/dist/src/system/SystemGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/system/SystemGraphQLHandlerFactory.d.ts +1 -1
- package/dist/src/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantApiMiddlewareFactory.d.ts +3 -9
- package/dist/src/tenant/TenantApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantApiMiddlewareFactory.js +15 -24
- package/dist/src/tenant/TenantApiMiddlewareFactory.js.map +1 -1
- package/dist/src/tenant/TenantGraphQLContextFactory.d.ts +1 -1
- package/dist/src/tenant/TenantGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts +1 -1
- package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/src/transfer/ExportApiControllerFactory.d.ts +10 -0
- package/dist/src/transfer/ExportApiControllerFactory.d.ts.map +1 -0
- package/dist/src/transfer/{ExportApiMiddlewareFactory.js → ExportApiControllerFactory.js} +16 -20
- package/dist/src/transfer/ExportApiControllerFactory.js.map +1 -0
- package/dist/src/transfer/ExportExecutor.d.ts +1 -1
- package/dist/src/transfer/ExportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts +3 -8
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.js +9 -9
- package/dist/src/transfer/ImportApiMiddlewareFactory.js.map +1 -1
- package/dist/src/transfer/ImportExecutor.d.ts +2 -2
- package/dist/src/transfer/ImportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ImportExecutor.js +2 -2
- package/dist/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/src/transfer/index.d.ts +1 -1
- package/dist/src/transfer/index.js +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/utils/sentry.d.ts +10 -0
- package/dist/src/utils/sentry.d.ts.map +1 -0
- package/dist/src/utils/sentry.js +80 -0
- package/dist/src/utils/sentry.js.map +1 -0
- package/dist/src/utils/serverStartup.d.ts +33 -0
- package/dist/src/utils/serverStartup.d.ts.map +1 -0
- package/dist/src/utils/serverStartup.js +79 -0
- package/dist/src/utils/serverStartup.js.map +1 -0
- package/dist/src/utils/serverTermination.d.ts +13 -0
- package/dist/src/utils/serverTermination.d.ts.map +1 -0
- package/dist/src/utils/serverTermination.js +24 -0
- package/dist/src/utils/serverTermination.js.map +1 -0
- package/dist/src/utils/strings.d.ts +2 -0
- package/dist/src/utils/strings.d.ts.map +1 -0
- package/dist/src/utils/strings.js +6 -0
- package/dist/src/utils/strings.js.map +1 -0
- package/dist/src/workers/ApplicationWorkerManager.d.ts +9 -0
- package/dist/src/workers/ApplicationWorkerManager.d.ts.map +1 -0
- package/dist/src/workers/ApplicationWorkerManager.js +28 -0
- package/dist/src/workers/ApplicationWorkerManager.js.map +1 -0
- package/dist/src/workers/index.d.ts +2 -0
- package/dist/src/workers/index.d.ts.map +1 -0
- package/dist/src/workers/index.js +18 -0
- package/dist/src/workers/index.js.map +1 -0
- package/dist/tests/cases/unit/configResolver.test.d.ts +2 -0
- package/dist/tests/cases/unit/configResolver.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/configResolver.test.js +67 -0
- package/dist/tests/cases/unit/configResolver.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -13
- package/src/MasterContainer.ts +182 -0
- package/src/application/application.ts +396 -0
- package/src/application/index.ts +2 -0
- package/src/bootstrap/Initializer.ts +27 -0
- package/src/bootstrap/index.ts +1 -0
- package/src/common/Authorizator.ts +13 -13
- package/src/common/HttpResponse.ts +21 -0
- package/src/common/ModuleInfoMiddleware.ts +1 -1
- package/src/common/index.ts +1 -5
- package/src/config/ConfigProcessor.ts +17 -0
- package/src/config/config.ts +57 -0
- package/src/config/configSchema.ts +135 -0
- package/src/config/configTemplate.ts +87 -0
- package/src/config/projectConfigResolver.ts +86 -0
- package/src/config/tenantConfigResolver.ts +41 -0
- package/src/content/{ContentApiMiddlewareFactory.ts → ContentApiControllerFactory.ts} +32 -60
- package/src/content/ContentGraphQLContextFactory.ts +29 -17
- package/src/content/GraphQLSchemaContributor.ts +14 -0
- package/src/content/GraphQlSchemaFactory.ts +1 -1
- package/src/content/NotModifiedChecker.ts +10 -10
- package/src/content/ProjectMembershipResolver.ts +6 -6
- package/src/content/index.ts +2 -1
- package/src/graphql/state.ts +1 -1
- package/src/index.ts +22 -5
- package/src/misc/HomepageMiddlewareFactory.ts +4 -7
- package/src/misc/PlaygroundMiddlewareFactory.ts +11 -22
- package/src/plugin/Plugin.ts +27 -0
- package/src/project/ProjectContainer.ts +118 -0
- package/src/project/ProjectContainerResolver.ts +89 -0
- package/src/project/ProjectContainerStore.ts +33 -0
- package/src/project/ProjectInitializer.ts +28 -0
- package/src/project/ProjectSchemaResolver.ts +33 -0
- package/src/project/index.ts +4 -0
- package/src/project-common/ProjectContextResolver.ts +17 -0
- package/src/project-common/index.ts +1 -1
- package/src/projectGroup/ProjectGroupContainer.ts +146 -0
- package/src/projectGroup/ProjectGroupResolver.ts +17 -0
- package/src/system/SystemApiMiddlewareFactory.ts +32 -61
- package/src/system/SystemGraphQLContextFactory.ts +4 -4
- package/src/system/SystemGraphQLHandlerFactory.ts +1 -1
- package/src/tenant/TenantApiMiddlewareFactory.ts +18 -39
- package/src/tenant/TenantGraphQLContextFactory.ts +1 -1
- package/src/tenant/TenantGraphQLHandlerFactory.ts +1 -1
- package/src/transfer/{ExportApiMiddlewareFactory.ts → ExportApiControllerFactory.ts} +18 -31
- package/src/transfer/ExportExecutor.ts +1 -1
- package/src/transfer/ImportApiMiddlewareFactory.ts +13 -21
- package/src/transfer/ImportExecutor.ts +3 -3
- package/src/transfer/index.ts +1 -1
- package/src/tsconfig.json +17 -34
- package/src/utils/sentry.ts +56 -0
- package/src/utils/serverStartup.ts +79 -0
- package/src/utils/serverTermination.ts +19 -0
- package/src/utils/strings.ts +1 -0
- package/src/workers/ApplicationWorkerManager.ts +27 -0
- package/src/workers/index.ts +1 -0
- package/tests/cases/unit/configResolver.test.ts +67 -0
- package/tests/tsconfig.json +1 -3
- package/tsconfig.json +2 -6
- package/dist/src/ProjectContainer.d.ts +0 -21
- package/dist/src/ProjectContainer.d.ts.map +0 -1
- package/dist/src/ProjectContainer.js.map +0 -1
- package/dist/src/ProjectGroupContainer.d.ts +0 -23
- package/dist/src/ProjectGroupContainer.d.ts.map +0 -1
- package/dist/src/ProjectGroupContainer.js.map +0 -1
- package/dist/src/common/ErrorMiddleware.d.ts +0 -11
- package/dist/src/common/ErrorMiddleware.d.ts.map +0 -1
- package/dist/src/common/ErrorMiddleware.js +0 -36
- package/dist/src/common/ErrorMiddleware.js.map +0 -1
- package/dist/src/common/HttpError.d.ts +0 -5
- package/dist/src/common/HttpError.d.ts.map +0 -1
- package/dist/src/common/HttpError.js +0 -11
- package/dist/src/common/HttpError.js.map +0 -1
- package/dist/src/common/LoggerMiddleware.d.ts +0 -8
- package/dist/src/common/LoggerMiddleware.d.ts.map +0 -1
- package/dist/src/common/LoggerMiddleware.js +0 -32
- package/dist/src/common/LoggerMiddleware.js.map +0 -1
- package/dist/src/common/PoweredByHeaderMiddleware.d.ts +0 -3
- package/dist/src/common/PoweredByHeaderMiddleware.d.ts.map +0 -1
- package/dist/src/common/PoweredByHeaderMiddleware.js +0 -15
- package/dist/src/common/PoweredByHeaderMiddleware.js.map +0 -1
- package/dist/src/common/TimerMiddleware.d.ts +0 -11
- package/dist/src/common/TimerMiddleware.d.ts.map +0 -1
- package/dist/src/common/TimerMiddleware.js +0 -53
- package/dist/src/common/TimerMiddleware.js.map +0 -1
- package/dist/src/config.d.ts.map +0 -1
- package/dist/src/content/ContentApiMiddlewareFactory.d.ts +0 -20
- package/dist/src/content/ContentApiMiddlewareFactory.d.ts.map +0 -1
- package/dist/src/content/ContentApiMiddlewareFactory.js.map +0 -1
- package/dist/src/koa/compose.d.ts +0 -4
- package/dist/src/koa/compose.d.ts.map +0 -1
- package/dist/src/koa/compose.js +0 -18
- package/dist/src/koa/compose.js.map +0 -1
- package/dist/src/koa/index.d.ts +0 -4
- package/dist/src/koa/index.d.ts.map +0 -1
- package/dist/src/koa/index.js.map +0 -1
- package/dist/src/koa/router.d.ts +0 -14
- package/dist/src/koa/router.d.ts.map +0 -1
- package/dist/src/koa/router.js +0 -54
- package/dist/src/koa/router.js.map +0 -1
- package/dist/src/koa/types.d.ts.map +0 -1
- package/dist/src/project-common/ProjectGroupResolver.d.ts +0 -9
- package/dist/src/project-common/ProjectGroupResolver.d.ts.map +0 -1
- package/dist/src/project-common/ProjectGroupResolver.js.map +0 -1
- package/dist/src/transfer/ExportApiMiddlewareFactory.d.ts +0 -15
- package/dist/src/transfer/ExportApiMiddlewareFactory.d.ts.map +0 -1
- package/dist/src/transfer/ExportApiMiddlewareFactory.js.map +0 -1
- package/src/ProjectContainer.ts +0 -22
- package/src/ProjectGroupContainer.ts +0 -32
- package/src/common/ErrorMiddleware.ts +0 -35
- package/src/common/HttpError.ts +0 -8
- package/src/common/LoggerMiddleware.ts +0 -33
- package/src/common/PoweredByHeaderMiddleware.ts +0 -12
- package/src/common/TimerMiddleware.ts +0 -67
- package/src/koa/compose.ts +0 -16
- package/src/koa/index.ts +0 -3
- package/src/koa/router.ts +0 -72
- package/src/project-common/ProjectGroupResolver.ts +0 -6
- /package/dist/src/{koa → application}/types.d.ts +0 -0
- /package/dist/src/{koa → application}/types.js +0 -0
- /package/dist/src/{config.d.ts → project/config.d.ts} +0 -0
- /package/dist/src/{config.js → project/config.js} +0 -0
- /package/src/{koa → application}/types.ts +0 -0
- /package/src/{config.ts → project/config.ts} +0 -0
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import { ApiKeyManager, DatabaseContext, VerifyResult } from '@contember/engine-tenant-api'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { HttpErrorResponse } from './HttpResponse'
|
|
3
|
+
import { Timer } from '../application'
|
|
4
|
+
import { IncomingMessage } from 'node:http'
|
|
5
5
|
|
|
6
6
|
export type AuthResult =
|
|
7
7
|
& VerifyResult
|
|
8
8
|
& { assumedIdentityId?: string }
|
|
9
9
|
|
|
10
|
-
export interface AuthMiddlewareState {
|
|
11
|
-
authResult: AuthResult
|
|
12
|
-
}
|
|
13
|
-
|
|
14
10
|
const assumeIdentityHeader = 'x-contember-assume-identity'
|
|
15
11
|
|
|
16
12
|
export class Authenticator {
|
|
17
|
-
private createAuthError = (message: string) => new
|
|
13
|
+
private createAuthError = (message: string) => new HttpErrorResponse(401, `Authorization failure: ${message}`)
|
|
18
14
|
|
|
19
15
|
constructor(
|
|
20
16
|
private readonly tenantDatabase: DatabaseContext,
|
|
@@ -22,10 +18,10 @@ export class Authenticator {
|
|
|
22
18
|
) {
|
|
23
19
|
}
|
|
24
20
|
|
|
25
|
-
public async authenticate({ request, timer }: { request:
|
|
26
|
-
const authHeader = request.
|
|
27
|
-
if (
|
|
28
|
-
|
|
21
|
+
public async authenticate({ request, timer }: { request: IncomingMessage; timer: Timer }): Promise<AuthResult | null> {
|
|
22
|
+
const authHeader = request.headers.authorization
|
|
23
|
+
if (!authHeader) {
|
|
24
|
+
return null
|
|
29
25
|
}
|
|
30
26
|
|
|
31
27
|
const authHeaderPattern = /^Bearer\s+(\w+)$/i
|
|
@@ -39,9 +35,13 @@ export class Authenticator {
|
|
|
39
35
|
if (!authResult.ok) {
|
|
40
36
|
throw this.createAuthError(authResult.errorMessage)
|
|
41
37
|
}
|
|
38
|
+
const assumedIdentityId = request.headers[assumeIdentityHeader] ?? undefined
|
|
39
|
+
if (Array.isArray(assumedIdentityId)) {
|
|
40
|
+
throw new HttpErrorResponse(400, `Invalid ${assumedIdentityId} header format`)
|
|
41
|
+
}
|
|
42
42
|
return {
|
|
43
43
|
...authResult.result,
|
|
44
|
-
assumedIdentityId
|
|
44
|
+
assumedIdentityId,
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class HttpResponse {
|
|
2
|
+
constructor(
|
|
3
|
+
public readonly code: number,
|
|
4
|
+
public readonly body?: string,
|
|
5
|
+
public readonly contentType?: string,
|
|
6
|
+
) {
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class HttpErrorResponse extends HttpResponse {
|
|
11
|
+
constructor(
|
|
12
|
+
public readonly code: number,
|
|
13
|
+
public readonly message?: string,
|
|
14
|
+
) {
|
|
15
|
+
super(
|
|
16
|
+
code,
|
|
17
|
+
message ? JSON.stringify({ errors: [{ code, message }] }) : undefined,
|
|
18
|
+
'application/json',
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/common/index.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
export * from './Authorizator'
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './LoggerMiddleware'
|
|
4
|
-
export * from './ErrorMiddleware'
|
|
2
|
+
export * from './HttpResponse'
|
|
5
3
|
export * from './ModuleInfoMiddleware'
|
|
6
|
-
export * from './PoweredByHeaderMiddleware'
|
|
7
|
-
export * from './TimerMiddleware'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as Typesafe from '@contember/typesafe'
|
|
2
|
+
|
|
3
|
+
import { Env } from './config'
|
|
4
|
+
|
|
5
|
+
export type ConfigTemplate = any // fixme
|
|
6
|
+
|
|
7
|
+
export type Config = Record<string, unknown>
|
|
8
|
+
|
|
9
|
+
export type ConfigTemplateContext = { env: Env }
|
|
10
|
+
|
|
11
|
+
export interface ConfigProcessor<CustomConfig extends Typesafe.JsonObject> {
|
|
12
|
+
getDefaultEnv?(): Env
|
|
13
|
+
|
|
14
|
+
prepareConfigTemplate?(template: ConfigTemplate, context: ConfigTemplateContext): ConfigTemplate
|
|
15
|
+
|
|
16
|
+
getProjectConfigSchema?(slug: string): Typesafe.Type<CustomConfig>
|
|
17
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ConfigLoader, createObjectParametersResolver, Merger, resolveParameters } from '@contember/config-loader'
|
|
2
|
+
|
|
3
|
+
import { serverConfigSchema as defaultServerConfigSchema, tenantConfigSchema } from './configSchema'
|
|
4
|
+
import { configTemplate } from './configTemplate'
|
|
5
|
+
import { createProjectConfigResolver, ProjectConfigResolver } from './projectConfigResolver'
|
|
6
|
+
import { createTenantConfigResolver, TenantConfigResolver } from './tenantConfigResolver'
|
|
7
|
+
import { Type } from '@contember/typesafe'
|
|
8
|
+
import { ConfigProcessor } from './ConfigProcessor'
|
|
9
|
+
|
|
10
|
+
export type ServerConfig = ReturnType<typeof defaultServerConfigSchema>
|
|
11
|
+
export type TenantConfig = ReturnType<typeof tenantConfigSchema>
|
|
12
|
+
|
|
13
|
+
export type ConfigSource = { data: string; type: 'file' | 'json' | 'yaml' }
|
|
14
|
+
|
|
15
|
+
export type Env = Record<string, string>
|
|
16
|
+
|
|
17
|
+
export async function readConfig<T extends ServerConfig>(
|
|
18
|
+
configSources: ConfigSource[] = [],
|
|
19
|
+
configProcessors: ConfigProcessor<any>[] = [],
|
|
20
|
+
serverConfigSchema: Type<T> = defaultServerConfigSchema as Type<T>,
|
|
21
|
+
): Promise<{
|
|
22
|
+
serverConfig: T
|
|
23
|
+
projectConfigResolver: ProjectConfigResolver
|
|
24
|
+
tenantConfigResolver: TenantConfigResolver
|
|
25
|
+
}> {
|
|
26
|
+
const loader = new ConfigLoader()
|
|
27
|
+
const configs = await Promise.all(
|
|
28
|
+
configSources.map(it => (it.type === 'file' ? loader.load(it.data) : loader.loadString(it.data, it.type))),
|
|
29
|
+
)
|
|
30
|
+
const env: Env = {
|
|
31
|
+
...configProcessors.reduce((acc, curr) => ({ ...acc, ...(curr.getDefaultEnv?.() || {}) }), {
|
|
32
|
+
DEFAULT_DB_PORT: '5432',
|
|
33
|
+
}),
|
|
34
|
+
...Object.fromEntries(Object.entries(process.env).filter((it): it is [string, string] => it[1] !== undefined)),
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const template = configProcessors.reduce(
|
|
38
|
+
(tpl, processor) => processor.prepareConfigTemplate?.(tpl, { env }) || tpl,
|
|
39
|
+
configTemplate,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
const rawConfig = Merger.merge(template, ...configs)
|
|
43
|
+
|
|
44
|
+
const parametersResolver = createObjectParametersResolver({ env })
|
|
45
|
+
const config = resolveParameters(rawConfig.server, parametersResolver)
|
|
46
|
+
|
|
47
|
+
const serverConfig = serverConfigSchema(config)
|
|
48
|
+
return {
|
|
49
|
+
serverConfig,
|
|
50
|
+
projectConfigResolver: createProjectConfigResolver(env, rawConfig, configProcessors),
|
|
51
|
+
tenantConfigResolver: createTenantConfigResolver(env, rawConfig.tenant),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import * as Typesafe from '@contember/typesafe'
|
|
2
|
+
import { MailerOptions } from '@contember/engine-tenant-api'
|
|
3
|
+
import { upperCaseFirst } from '../utils/strings'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const dbConfigRequired = {
|
|
7
|
+
host: Typesafe.string,
|
|
8
|
+
port: Typesafe.number,
|
|
9
|
+
user: Typesafe.string,
|
|
10
|
+
password: Typesafe.string,
|
|
11
|
+
database: Typesafe.string,
|
|
12
|
+
}
|
|
13
|
+
const dbConfigOptional = {
|
|
14
|
+
ssl: Typesafe.boolean,
|
|
15
|
+
queryTimeoutMs: Typesafe.number,
|
|
16
|
+
statementTimeoutMs: Typesafe.number,
|
|
17
|
+
connectionTimeoutMs: Typesafe.number,
|
|
18
|
+
pool: Typesafe.partial({
|
|
19
|
+
maxConnections: Typesafe.number,
|
|
20
|
+
maxConnecting: Typesafe.number,
|
|
21
|
+
maxIdle: Typesafe.number,
|
|
22
|
+
reconnectIntervalMs: Typesafe.number,
|
|
23
|
+
rateLimitCount: Typesafe.number,
|
|
24
|
+
rateLimitPeriodMs: Typesafe.number,
|
|
25
|
+
idleTimeoutMs: Typesafe.number,
|
|
26
|
+
acquireTimeoutMs: Typesafe.number,
|
|
27
|
+
maxUses: Typesafe.number,
|
|
28
|
+
maxAgeMs: Typesafe.number,
|
|
29
|
+
}),
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const dbConfigSchemaInner = Typesafe.intersection(
|
|
33
|
+
Typesafe.object(dbConfigRequired),
|
|
34
|
+
Typesafe.partial(dbConfigOptional),
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
const readDbSchema = Typesafe.intersection(
|
|
38
|
+
Typesafe.partial(dbConfigRequired),
|
|
39
|
+
Typesafe.partial(dbConfigOptional),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
export const dbConfigSchema = Typesafe.intersection(
|
|
43
|
+
dbConfigSchemaInner,
|
|
44
|
+
Typesafe.partial({
|
|
45
|
+
read: (val: unknown) => {
|
|
46
|
+
const readDb = readDbSchema(val)
|
|
47
|
+
if ('host' in readDb) {
|
|
48
|
+
return readDb
|
|
49
|
+
}
|
|
50
|
+
return undefined
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
export const tenantConfigSchema = Typesafe.intersection(
|
|
56
|
+
Typesafe.object({
|
|
57
|
+
db: dbConfigSchema,
|
|
58
|
+
mailer: Typesafe.transform(Typesafe.partial({
|
|
59
|
+
from: Typesafe.string,
|
|
60
|
+
host: Typesafe.string,
|
|
61
|
+
port: Typesafe.number,
|
|
62
|
+
user: Typesafe.string,
|
|
63
|
+
password: Typesafe.string,
|
|
64
|
+
}), (value, input): MailerOptions & Typesafe.JsonObject => {
|
|
65
|
+
return {
|
|
66
|
+
...(input as any),
|
|
67
|
+
...value,
|
|
68
|
+
...(value.user ? { auth: { user: value.user, pass: value.password } } : {}),
|
|
69
|
+
}
|
|
70
|
+
}),
|
|
71
|
+
credentials: Typesafe.partial({
|
|
72
|
+
rootToken: Typesafe.string,
|
|
73
|
+
rootTokenHash: Typesafe.string,
|
|
74
|
+
rootPassword: Typesafe.string,
|
|
75
|
+
rootEmail: Typesafe.string,
|
|
76
|
+
loginToken: Typesafe.string,
|
|
77
|
+
}),
|
|
78
|
+
}),
|
|
79
|
+
Typesafe.object({
|
|
80
|
+
secrets: Typesafe.partial({
|
|
81
|
+
encryptionKey: Typesafe.string,
|
|
82
|
+
}),
|
|
83
|
+
}),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
export const serverConfigSchema = Typesafe.object({
|
|
87
|
+
port: Typesafe.number,
|
|
88
|
+
http: Typesafe.partial({
|
|
89
|
+
requestBodySize: Typesafe.string,
|
|
90
|
+
}),
|
|
91
|
+
logging: Typesafe.union(
|
|
92
|
+
(val): { sentry?: { dsn: string } } => Typesafe.valueAt(val, ['sentry', 'dsn']) === undefined ? {} : Typesafe.fail([]),
|
|
93
|
+
Typesafe.partial({
|
|
94
|
+
sentry: Typesafe.object({
|
|
95
|
+
dsn: Typesafe.string,
|
|
96
|
+
}),
|
|
97
|
+
}),
|
|
98
|
+
),
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
export const stageConfig = Typesafe.record(
|
|
102
|
+
Typesafe.string,
|
|
103
|
+
Typesafe.intersection(
|
|
104
|
+
Typesafe.object({
|
|
105
|
+
name: Typesafe.union(
|
|
106
|
+
Typesafe.string,
|
|
107
|
+
(_: unknown, path: PropertyKey[] = []) => upperCaseFirst(String(path[path.length - 2])),
|
|
108
|
+
),
|
|
109
|
+
slug: (_: unknown, path: PropertyKey[] = []) => String(path[path.length - 2]),
|
|
110
|
+
}),
|
|
111
|
+
Typesafe.partial({
|
|
112
|
+
schema: Typesafe.string,
|
|
113
|
+
}),
|
|
114
|
+
),
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
export const projectConfigSchema = Typesafe.object({
|
|
118
|
+
name: Typesafe.union(
|
|
119
|
+
Typesafe.string,
|
|
120
|
+
(_: unknown, path: PropertyKey[] = []) => upperCaseFirst(String(path[path.length - 2])).replace(/-/g, ' '),
|
|
121
|
+
),
|
|
122
|
+
slug: (_, path = []) => String(path[path.length - 2]),
|
|
123
|
+
stages: (input, path = []) => Object.values(stageConfig(input, path)),
|
|
124
|
+
db: Typesafe.intersection(
|
|
125
|
+
Typesafe.union(
|
|
126
|
+
Typesafe.object({
|
|
127
|
+
useTenantDb: Typesafe.literal(true),
|
|
128
|
+
}),
|
|
129
|
+
dbConfigSchema,
|
|
130
|
+
),
|
|
131
|
+
Typesafe.partial({
|
|
132
|
+
systemSchema: Typesafe.string,
|
|
133
|
+
}),
|
|
134
|
+
),
|
|
135
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const createDbConfigTemplate = (prefix: string) => {
|
|
2
|
+
return {
|
|
3
|
+
ssl: `%?${prefix}_SSL::bool%`,
|
|
4
|
+
queryTimeoutMs: `%?${prefix}_QUERY_TIMEOUT_MS::number%`,
|
|
5
|
+
statementTimeoutMs: `%?${prefix}_STATEMENT_TIMEOUT_MS::number%`,
|
|
6
|
+
connectionTimeoutMs: `%?${prefix}_CONNECTION_TIMEOUT_MS::number%`,
|
|
7
|
+
pool: {
|
|
8
|
+
maxConnections: `%?${prefix}_POOL_MAX_CONNECTIONS::number%`,
|
|
9
|
+
maxConnecting: `%?${prefix}_POOL_MAX_CONNECTING::number%`,
|
|
10
|
+
maxIdle: `%?${prefix}_POOL_MAX_IDLE::number%`,
|
|
11
|
+
reconnectIntervalMs: `%?${prefix}_POOL_RECONNECT_INTERVAL_MS::number%`,
|
|
12
|
+
idleTimeoutMs: `%?${prefix}_POOL_IDLE_TIMEOUT_MS::number%`,
|
|
13
|
+
acquireTimeoutMs: `%?${prefix}_POOL_ACQUIRE_TIMEOUT_MS::number%`,
|
|
14
|
+
maxUses: `%?${prefix}_POOL_MAX_USES::number%`,
|
|
15
|
+
maxAgeMs: `%?${prefix}_POOL_MAX_AGE_MS::number%`,
|
|
16
|
+
rateLimitCount: `%?${prefix}_POOL_RATE_LIMIT_COUNT::number%`,
|
|
17
|
+
rateLimitPeriodMs: `%?${prefix}_POOL_RATE_LIMIT_PERIOD_MS::number%`,
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export const configTemplate: any = {
|
|
22
|
+
tenant: {
|
|
23
|
+
db: {
|
|
24
|
+
host: `%tenant.env.DB_HOST%`,
|
|
25
|
+
port: `%tenant.env.DB_PORT::number%`,
|
|
26
|
+
user: `%tenant.env.DB_USER%`,
|
|
27
|
+
password: `%tenant.env.DB_PASSWORD%`,
|
|
28
|
+
database: `%tenant.env.DB_NAME%`,
|
|
29
|
+
...createDbConfigTemplate('tenant.env.DB'),
|
|
30
|
+
},
|
|
31
|
+
mailer: {
|
|
32
|
+
from: '%?tenant.env.MAILER_FROM%',
|
|
33
|
+
host: '%?tenant.env.MAILER_HOST::string%',
|
|
34
|
+
port: '%?tenant.env.MAILER_PORT::number%',
|
|
35
|
+
secure: '%?tenant.env.MAILER_SECURE::bool%',
|
|
36
|
+
user: '%?tenant.env.MAILER_USER%',
|
|
37
|
+
password: '%?tenant.env.MAILER_PASSWORD%',
|
|
38
|
+
},
|
|
39
|
+
credentials: {
|
|
40
|
+
rootEmail: '%?env.CONTEMBER_ROOT_EMAIL%',
|
|
41
|
+
rootToken: '%?env.CONTEMBER_ROOT_TOKEN%',
|
|
42
|
+
rootTokenHash: '%?env.CONTEMBER_ROOT_TOKEN_HASH%',
|
|
43
|
+
rootPassword: '%?env.CONTEMBER_ROOT_PASSWORD%',
|
|
44
|
+
loginToken: '%?env.CONTEMBER_LOGIN_TOKEN%',
|
|
45
|
+
},
|
|
46
|
+
secrets: {
|
|
47
|
+
encryptionKey: '%?env.CONTEMBER_ENCRYPTION_KEY%',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
projectDefaults: {
|
|
51
|
+
db: {
|
|
52
|
+
host: `%?project.env.DB_HOST%`,
|
|
53
|
+
port: `%?project.env.DB_PORT::number%`,
|
|
54
|
+
user: `%?project.env.DB_USER%`,
|
|
55
|
+
password: `%?project.secret.db.password||project.env.DB_PASSWORD%`,
|
|
56
|
+
database: `%?project.env.DB_NAME||project.slug%`,
|
|
57
|
+
...createDbConfigTemplate('project.env.DB'),
|
|
58
|
+
read: {
|
|
59
|
+
host: `%?project.env.DB_READ_HOST%`,
|
|
60
|
+
port: `%?project.env.DB_READ_PORT::number%`,
|
|
61
|
+
user: `%?project.env.DB_READ_USER%`,
|
|
62
|
+
password: `%?project.secret.db.read.password||project.env.DB_READ_PASSWORD%`,
|
|
63
|
+
database: `%?project.env.DB_READ_NAME%`,
|
|
64
|
+
...createDbConfigTemplate('project.env.DB_READ'),
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
server: {
|
|
69
|
+
port: '%env.CONTEMBER_PORT::number%',
|
|
70
|
+
monitoringPort: '%env.CONTEMBER_MONITORING_PORT::number%',
|
|
71
|
+
workerCount: '%?env.CONTEMBER_WORKER_COUNT::string%',
|
|
72
|
+
applicationWorker: '%?env.CONTEMBER_APPLICATION_WORKER::string%',
|
|
73
|
+
http: {
|
|
74
|
+
requestBodySize: '%?env.CONTEMBER_HTTP_REQUEST_BODY_SIZE::string%',
|
|
75
|
+
},
|
|
76
|
+
projectGroup: {
|
|
77
|
+
domainMapping: '%?env.CONTEMBER_PROJECT_GROUP_DOMAIN_MAPPING%',
|
|
78
|
+
configHeader: '%?env.CONTEMBER_PROJECT_GROUP_CONFIG_HEADER%',
|
|
79
|
+
configEncryptionKey: '%?env.CONTEMBER_PROJECT_GROUP_CONFIG_ENCRYPTION_KEY%',
|
|
80
|
+
},
|
|
81
|
+
logging: {
|
|
82
|
+
sentry: {
|
|
83
|
+
dsn: '%?env.SENTRY_DSN%',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Merger, ParametersResolver, resolveParameters, UndefinedParameterError } from '@contember/config-loader'
|
|
2
|
+
import * as Typesafe from '@contember/typesafe'
|
|
3
|
+
import { projectConfigSchema } from './configSchema'
|
|
4
|
+
import { TenantConfig } from './config'
|
|
5
|
+
import { ProjectConfig, ProjectSecrets } from '../project/config'
|
|
6
|
+
import { ConfigProcessor } from './ConfigProcessor'
|
|
7
|
+
|
|
8
|
+
export type ProjectConfigResolver = (
|
|
9
|
+
slug: string,
|
|
10
|
+
additionalConfig: any,
|
|
11
|
+
secrets: ProjectSecrets,
|
|
12
|
+
tenantConfig: TenantConfig,
|
|
13
|
+
) => ProjectConfig
|
|
14
|
+
|
|
15
|
+
type Env = Record<string, string>
|
|
16
|
+
|
|
17
|
+
export const createProjectConfigResolver = (env: Env, config: any, configProcessors: ConfigProcessor<any>[]): ProjectConfigResolver =>
|
|
18
|
+
(slug, additionalConfig, secrets, tenantConfig): ProjectConfig => {
|
|
19
|
+
const mergedConfig = Merger.merge(
|
|
20
|
+
config.projectDefaults as any,
|
|
21
|
+
(config?.projects as any)?.[slug] as any,
|
|
22
|
+
additionalConfig,
|
|
23
|
+
)
|
|
24
|
+
if (!mergedConfig.stages) {
|
|
25
|
+
mergedConfig.stages = { live: {} }
|
|
26
|
+
}
|
|
27
|
+
const resolvedConfig = resolveParameters(mergedConfig, createProjectParametersResolver(slug, env, secrets))
|
|
28
|
+
|
|
29
|
+
const projectConfigSchemaWithPlugins = configProcessors.reduce(
|
|
30
|
+
(schema, current) => {
|
|
31
|
+
if (!current.getProjectConfigSchema) {
|
|
32
|
+
return schema
|
|
33
|
+
}
|
|
34
|
+
return Typesafe.intersection(schema, current.getProjectConfigSchema(slug))
|
|
35
|
+
},
|
|
36
|
+
projectConfigSchema,
|
|
37
|
+
)
|
|
38
|
+
const projectConfig = projectConfigSchemaWithPlugins(resolvedConfig, ['project', slug])
|
|
39
|
+
if (!('host' in projectConfig.db)) {
|
|
40
|
+
return {
|
|
41
|
+
...projectConfig,
|
|
42
|
+
db: {
|
|
43
|
+
...tenantConfig.db,
|
|
44
|
+
...projectConfig.db,
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
...projectConfig,
|
|
50
|
+
db: projectConfig.db,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
const createProjectParametersResolver = (slug: string, env: Env, secrets: ProjectSecrets): ParametersResolver =>
|
|
56
|
+
parts => {
|
|
57
|
+
if (parts[0] === 'project') {
|
|
58
|
+
if (parts[1] === 'env') {
|
|
59
|
+
const envName = parts[2]
|
|
60
|
+
const projectEnvName = projectNameToEnvName(slug)
|
|
61
|
+
const envValue = env[projectEnvName + '_' + envName] || env['DEFAULT_' + envName]
|
|
62
|
+
if (envValue === undefined) {
|
|
63
|
+
throw new UndefinedParameterError(`ENV variable "${projectEnvName + '_' + envName}" not found.`)
|
|
64
|
+
}
|
|
65
|
+
return envValue
|
|
66
|
+
} else if (parts[1] === 'slug') {
|
|
67
|
+
return slug
|
|
68
|
+
} else if (parts[1] === 'secret') {
|
|
69
|
+
const key = parts.slice(2).join('.')
|
|
70
|
+
const value = secrets[key]
|
|
71
|
+
if (value === undefined) {
|
|
72
|
+
throw new UndefinedParameterError(`Project secret ${parts[2]} not found`)
|
|
73
|
+
}
|
|
74
|
+
return value
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
throw new UndefinedParameterError(`Parameter "${parts.join('.')}" not found.`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const projectNameToEnvName = (projectName: string): string => {
|
|
81
|
+
const envName = projectName.toUpperCase().replace(/-/g, '_')
|
|
82
|
+
if (envName === 'TENANT') {
|
|
83
|
+
throw new Error('Forbidden project name')
|
|
84
|
+
}
|
|
85
|
+
return envName
|
|
86
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { TenantConfig } from './config'
|
|
2
|
+
import {
|
|
3
|
+
createObjectParametersResolver,
|
|
4
|
+
Merger,
|
|
5
|
+
ParametersResolver,
|
|
6
|
+
resolveParameters,
|
|
7
|
+
UndefinedParameterError,
|
|
8
|
+
} from '@contember/config-loader'
|
|
9
|
+
import { tenantConfigSchema } from './configSchema'
|
|
10
|
+
|
|
11
|
+
export type TenantConfigResolver = (slug: string | undefined, additionalConfig: any) => TenantConfig
|
|
12
|
+
|
|
13
|
+
type Env = Record<string, string>
|
|
14
|
+
|
|
15
|
+
export const createTenantConfigResolver = (env: Env, tenantConfig: any): TenantConfigResolver =>
|
|
16
|
+
(slug, additionalConfig) => {
|
|
17
|
+
const mergedConfig = Merger.merge(tenantConfig, additionalConfig)
|
|
18
|
+
const parametersResolver = createObjectParametersResolver({ env })
|
|
19
|
+
const tenantParametersResolver = createTenantParametersResolver(env)
|
|
20
|
+
const resolvedConfig = resolveParameters(mergedConfig, (parts, path, dataResolver) => {
|
|
21
|
+
if (parts[0] === 'tenant') {
|
|
22
|
+
return tenantParametersResolver(parts, path, dataResolver)
|
|
23
|
+
}
|
|
24
|
+
return parametersResolver(parts, path, dataResolver)
|
|
25
|
+
})
|
|
26
|
+
return tenantConfigSchema(resolvedConfig)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export const createTenantParametersResolver = (env: Env): ParametersResolver =>
|
|
31
|
+
parts => {
|
|
32
|
+
if (parts[1] === 'env') {
|
|
33
|
+
const envName = parts[2]
|
|
34
|
+
const envValue = env['TENANT_' + envName] || env['DEFAULT_' + envName]
|
|
35
|
+
if (envValue === undefined) {
|
|
36
|
+
throw new UndefinedParameterError(`ENV variable "${'TENANT_' + envName}" not found.`)
|
|
37
|
+
}
|
|
38
|
+
return envValue
|
|
39
|
+
}
|
|
40
|
+
throw new UndefinedParameterError(`Parameter "${parts.join('.')}" not found.`)
|
|
41
|
+
}
|
|
@@ -1,87 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { HttpController } from '../application'
|
|
2
|
+
import { ProjectContextResolver } from '../project-common'
|
|
3
|
+
import { HttpErrorResponse, HttpResponse } from '../common'
|
|
4
4
|
import { StageBySlugQuery } from '@contember/engine-system-api'
|
|
5
5
|
import { NotModifiedChecker } from './NotModifiedChecker'
|
|
6
6
|
import { ContentGraphQLContextFactory } from './ContentGraphQLContextFactory'
|
|
7
7
|
import { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory'
|
|
8
8
|
import { GraphQLSchema } from 'graphql'
|
|
9
|
-
import { GraphQLKoaState } from '../graphql'
|
|
10
|
-
|
|
11
|
-
type ContentApiMiddlewareKoaState =
|
|
12
|
-
& TimerMiddlewareState
|
|
13
|
-
& KoaRequestState
|
|
14
|
-
& GraphQLKoaState
|
|
15
|
-
& ProjectInfoMiddlewareState
|
|
16
|
-
& LoggerMiddlewareState
|
|
17
|
-
& {
|
|
18
|
-
authResult: AuthResult
|
|
19
|
-
}
|
|
20
|
-
|
|
21
9
|
|
|
22
10
|
const debugHeader = 'x-contember-debug'
|
|
23
11
|
|
|
24
|
-
export class
|
|
12
|
+
export class ContentApiControllerFactory {
|
|
25
13
|
constructor(
|
|
26
|
-
private readonly projectGroupResolver: ProjectGroupResolver,
|
|
27
14
|
private readonly notModifiedChecker: NotModifiedChecker,
|
|
28
15
|
private readonly contentGraphqlContextFactory: ContentGraphQLContextFactory,
|
|
29
16
|
private readonly handlerFactory: ContentQueryHandlerFactory,
|
|
17
|
+
private readonly projectContextResolver: ProjectContextResolver,
|
|
30
18
|
) {
|
|
31
19
|
}
|
|
32
20
|
|
|
33
|
-
create():
|
|
21
|
+
create(): HttpController {
|
|
34
22
|
const handlerCache = new WeakMap<GraphQLSchema, ContentQueryHandler>()
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
koaContext.state.projectGroup = groupContainer.slug
|
|
40
|
-
|
|
41
|
-
const authResult = await groupContainer.authenticator.authenticate({ request, timer })
|
|
42
|
-
koaContext.state.logger.debug('User authenticated', { authResult })
|
|
43
|
-
koaContext.state.authResult = authResult
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const projectContainer = await groupContainer.projectContainerResolver.getProjectContainer(params.projectSlug, {
|
|
47
|
-
alias: true,
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
if (projectContainer === undefined) {
|
|
51
|
-
throw new HttpError(`Project ${params.projectSlug} NOT found`, 404)
|
|
23
|
+
return async context => {
|
|
24
|
+
const { params, timer, projectGroup, authResult, request, logger, koa } = context
|
|
25
|
+
if (!authResult) {
|
|
26
|
+
return new HttpErrorResponse(401, 'Authentication required')
|
|
52
27
|
}
|
|
53
|
-
const
|
|
54
|
-
...projectContainer.logger.attributes,
|
|
55
|
-
module: 'content',
|
|
56
|
-
user: authResult.identityId,
|
|
57
|
-
})
|
|
58
|
-
koaContext.state.logger = requestLogger
|
|
59
|
-
|
|
60
|
-
const project = projectContainer.project
|
|
61
|
-
koaContext.state.project = project.slug
|
|
28
|
+
const { projectContainer, project } = await this.projectContextResolver.resolve(context)
|
|
62
29
|
|
|
63
30
|
const systemDatabase = projectContainer.systemDatabaseContextFactory.create()
|
|
64
31
|
const stage = await systemDatabase.queryHandler.fetch(new StageBySlugQuery(params.stageSlug))
|
|
65
32
|
if (!stage) {
|
|
66
|
-
|
|
33
|
+
return new HttpErrorResponse(404, `Stage ${params.stageSlug} NOT found`)
|
|
67
34
|
}
|
|
68
35
|
const notModifiedRes = await this.notModifiedChecker.checkNotModified({
|
|
69
|
-
request,
|
|
70
|
-
|
|
36
|
+
request: context.request,
|
|
37
|
+
body: context.body,
|
|
38
|
+
timer: context.timer,
|
|
71
39
|
systemDatabase,
|
|
72
40
|
stageId: stage.id,
|
|
73
41
|
})
|
|
74
42
|
if (notModifiedRes?.isModified === false) {
|
|
75
|
-
|
|
76
|
-
return
|
|
43
|
+
return new HttpResponse(304)
|
|
77
44
|
}
|
|
78
45
|
|
|
79
46
|
const schema = await projectContainer.contentSchemaResolver.getSchema(systemDatabase, stage.slug)
|
|
80
47
|
|
|
81
48
|
const { effective: memberships, fetched: fetchedMemberships } = await timer(
|
|
82
49
|
'MembershipFetch',
|
|
83
|
-
() =>
|
|
84
|
-
request,
|
|
50
|
+
() => projectGroup.projectMembershipResolver.resolveMemberships({
|
|
51
|
+
request: koa,
|
|
85
52
|
acl: schema.acl,
|
|
86
53
|
projectSlug: project.slug,
|
|
87
54
|
identity: {
|
|
@@ -91,17 +58,19 @@ export class ContentApiMiddlewareFactory {
|
|
|
91
58
|
},
|
|
92
59
|
}),
|
|
93
60
|
)
|
|
94
|
-
|
|
61
|
+
logger.debug('Memberships fetched', { memberships })
|
|
95
62
|
|
|
96
|
-
const debugHeaderValue = request.
|
|
63
|
+
const debugHeaderValue = request.headers[debugHeader]
|
|
64
|
+
if (Array.isArray(debugHeaderValue)) {
|
|
65
|
+
return new HttpErrorResponse(400, `Invalid format of ${debugHeader}`)
|
|
66
|
+
}
|
|
97
67
|
const requestDebug = debugHeaderValue === '1' && fetchedMemberships.some(it => schema.acl.roles[it.role]?.debug)
|
|
98
68
|
if (requestDebug) {
|
|
99
|
-
|
|
69
|
+
context.requestDebugMode = true
|
|
100
70
|
}
|
|
101
71
|
|
|
102
72
|
const projectRoles = memberships.map(it => it.role)
|
|
103
73
|
|
|
104
|
-
|
|
105
74
|
const [graphQlSchema, permissions] = await timer('GraphQLSchemaCreate', () => projectContainer.graphQlSchemaFactory.create(schema, {
|
|
106
75
|
projectRoles: projectRoles,
|
|
107
76
|
}))
|
|
@@ -116,12 +85,12 @@ export class ContentApiMiddlewareFactory {
|
|
|
116
85
|
return newHandler
|
|
117
86
|
})()
|
|
118
87
|
|
|
119
|
-
await
|
|
88
|
+
await logger.scope(async logger => {
|
|
120
89
|
logger.debug('Content query processing started')
|
|
121
90
|
|
|
122
91
|
await timer('GraphQL', () => handler({
|
|
123
|
-
request,
|
|
124
|
-
response,
|
|
92
|
+
request: koa.request,
|
|
93
|
+
response: koa.response,
|
|
125
94
|
createContext: ({ operation }) => {
|
|
126
95
|
const connection = operation === 'query' ? projectContainer.readConnection : projectContainer.connection
|
|
127
96
|
const contentDatabase = connection.createClient(stage.schema, { module: 'content' })
|
|
@@ -133,8 +102,11 @@ export class ContentApiMiddlewareFactory {
|
|
|
133
102
|
permissions,
|
|
134
103
|
schema,
|
|
135
104
|
timer,
|
|
136
|
-
koaContext,
|
|
105
|
+
koaContext: koa,
|
|
137
106
|
requestDebug,
|
|
107
|
+
systemSchema: projectContainer.systemDatabaseContextFactory.schemaName,
|
|
108
|
+
stage,
|
|
109
|
+
project,
|
|
138
110
|
})
|
|
139
111
|
},
|
|
140
112
|
}))
|
|
@@ -142,7 +114,7 @@ export class ContentApiMiddlewareFactory {
|
|
|
142
114
|
})
|
|
143
115
|
|
|
144
116
|
|
|
145
|
-
notModifiedRes?.setResponseHeader(response)
|
|
117
|
+
notModifiedRes?.setResponseHeader(context.response)
|
|
146
118
|
}
|
|
147
119
|
}
|
|
148
120
|
}
|