@contember/engine-http 1.1.0-rc.3 → 1.2.0-alpha.3
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/ProjectContainer.d.ts +5 -2
- package/dist/src/ProjectContainer.d.ts.map +1 -1
- package/dist/src/ProjectGroupContainer.d.ts +2 -3
- package/dist/src/ProjectGroupContainer.d.ts.map +1 -1
- package/dist/src/common/ErrorMiddleware.d.ts.map +1 -1
- package/dist/src/common/ErrorMiddleware.js +5 -2
- package/dist/src/common/ErrorMiddleware.js.map +1 -1
- package/dist/src/common/TimerMiddleware.d.ts.map +1 -1
- package/dist/src/common/TimerMiddleware.js +24 -7
- package/dist/src/common/TimerMiddleware.js.map +1 -1
- package/dist/src/config.d.ts +3 -2
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/content/ContentApiMiddlewareFactory.d.ts +1 -2
- package/dist/src/content/ContentApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/content/ContentApiMiddlewareFactory.js +21 -23
- package/dist/src/content/ContentApiMiddlewareFactory.js.map +1 -1
- package/dist/src/content/ContentSchemaResolver.d.ts +2 -3
- package/dist/src/content/ContentSchemaResolver.d.ts.map +1 -1
- package/dist/src/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/src/content/GraphQlSchemaFactory.js +1 -2
- package/dist/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/src/content/ProjectMembershipFetcher.d.ts +11 -0
- package/dist/src/content/ProjectMembershipFetcher.d.ts.map +1 -0
- package/dist/src/content/ProjectMembershipFetcher.js +14 -0
- package/dist/src/content/ProjectMembershipFetcher.js.map +1 -0
- package/dist/src/content/ProjectMembershipResolver.d.ts +24 -0
- package/dist/src/content/ProjectMembershipResolver.d.ts.map +1 -0
- package/dist/src/content/ProjectMembershipResolver.js +96 -0
- package/dist/src/content/ProjectMembershipResolver.js.map +1 -0
- package/dist/src/content/index.d.ts +2 -0
- package/dist/src/content/index.d.ts.map +1 -1
- package/dist/src/content/index.js +2 -0
- package/dist/src/content/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/koa/types.d.ts +1 -0
- package/dist/src/koa/types.d.ts.map +1 -1
- package/dist/src/project-common/ProjectGroupResolver.d.ts +2 -9
- package/dist/src/project-common/ProjectGroupResolver.d.ts.map +1 -1
- package/dist/src/project-common/ProjectGroupResolver.js +0 -47
- package/dist/src/project-common/ProjectGroupResolver.js.map +1 -1
- package/dist/src/system/SystemApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/system/SystemApiMiddlewareFactory.js +9 -1
- package/dist/src/system/SystemApiMiddlewareFactory.js.map +1 -1
- package/dist/src/transfer/Command.d.ts +23 -0
- package/dist/src/transfer/Command.d.ts.map +1 -0
- package/dist/src/transfer/Command.js +53 -0
- package/dist/src/transfer/Command.js.map +1 -0
- package/dist/src/transfer/CommandStream.d.ts +6 -0
- package/dist/src/transfer/CommandStream.d.ts.map +1 -0
- package/dist/src/transfer/CommandStream.js +90 -0
- package/dist/src/transfer/CommandStream.js.map +1 -0
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.d.ts +8 -0
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.d.ts.map +1 -0
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js +104 -0
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js.map +1 -0
- package/dist/src/transfer/ExportApiMiddlewareFactory.d.ts +15 -0
- package/dist/src/transfer/ExportApiMiddlewareFactory.d.ts.map +1 -0
- package/dist/src/transfer/ExportApiMiddlewareFactory.js +59 -0
- package/dist/src/transfer/ExportApiMiddlewareFactory.js.map +1 -0
- package/dist/src/transfer/ExportExecutor.d.ts +24 -0
- package/dist/src/transfer/ExportExecutor.d.ts.map +1 -0
- package/dist/src/transfer/ExportExecutor.js +130 -0
- package/dist/src/transfer/ExportExecutor.js.map +1 -0
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts +15 -0
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts.map +1 -0
- package/dist/src/transfer/ImportApiMiddlewareFactory.js +39 -0
- package/dist/src/transfer/ImportApiMiddlewareFactory.js.map +1 -0
- package/dist/src/transfer/ImportExecutor.d.ts +38 -0
- package/dist/src/transfer/ImportExecutor.d.ts.map +1 -0
- package/dist/src/transfer/ImportExecutor.js +296 -0
- package/dist/src/transfer/ImportExecutor.js.map +1 -0
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.d.ts +7 -0
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.d.ts.map +1 -0
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.js +135 -0
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.js.map +1 -0
- package/dist/src/transfer/TransferMapping.d.ts +38 -0
- package/dist/src/transfer/TransferMapping.d.ts.map +1 -0
- package/dist/src/transfer/TransferMapping.js +3 -0
- package/dist/src/transfer/TransferMapping.js.map +1 -0
- package/dist/src/transfer/index.d.ts +7 -0
- package/dist/src/transfer/index.d.ts.map +1 -0
- package/dist/src/transfer/index.js +23 -0
- package/dist/src/transfer/index.js.map +1 -0
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/utils/CryptoWrapper.d.ts +1 -0
- package/dist/src/utils/CryptoWrapper.d.ts.map +1 -1
- package/dist/tests/cases/unit/projectMembershipResolver.test.d.ts +2 -0
- package/dist/tests/cases/unit/projectMembershipResolver.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/projectMembershipResolver.test.js +91 -0
- package/dist/tests/cases/unit/projectMembershipResolver.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/utils.d.ts +6 -0
- package/dist/tests/utils.d.ts.map +1 -0
- package/dist/tests/utils.js +8 -0
- package/dist/tests/utils.js.map +1 -0
- package/package.json +14 -14
- package/src/ProjectContainer.ts +2 -2
- package/src/ProjectGroupContainer.ts +2 -4
- package/src/common/ErrorMiddleware.ts +5 -2
- package/src/common/TimerMiddleware.ts +30 -9
- package/src/config.ts +7 -2
- package/src/content/ContentApiMiddlewareFactory.ts +22 -30
- package/src/content/ContentSchemaResolver.ts +1 -1
- package/src/content/GraphQlSchemaFactory.ts +1 -2
- package/src/content/ProjectMembershipFetcher.ts +18 -0
- package/src/content/ProjectMembershipResolver.ts +95 -0
- package/src/content/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/koa/types.ts +1 -1
- package/src/project-common/ProjectGroupResolver.ts +3 -53
- package/src/system/SystemApiMiddlewareFactory.ts +9 -1
- package/src/transfer/Command.ts +42 -0
- package/src/transfer/CommandStream.ts +72 -0
- package/src/transfer/ContentSchemaTransferMappingFactory.ts +113 -0
- package/src/transfer/ExportApiMiddlewareFactory.ts +76 -0
- package/src/transfer/ExportExecutor.ts +132 -0
- package/src/transfer/ImportApiMiddlewareFactory.ts +53 -0
- package/src/transfer/ImportExecutor.ts +366 -0
- package/src/transfer/SystemSchemaTransferMappingFactory.ts +129 -0
- package/src/transfer/TransferMapping.ts +24 -0
- package/src/transfer/index.ts +8 -0
- package/tests/cases/unit/projectMembershipResolver.test.ts +94 -0
- package/tests/utils.ts +5 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../tests/utils.ts"],"names":[],"mappings":"AAAA,aAAK,SAAS,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAA;AAE5C,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAEtD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../tests/utils.ts"],"names":[],"mappings":";;;AAEA,SAAgB,UAAU,CAAI,OAAqB;IAClD,OAAO,OAAO,CAAA;AACf,CAAC;AAFD,gCAEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,21 +9,21 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
12
|
-
"@contember/database": "^1.
|
|
13
|
-
"@contember/engine-content-api": "^1.
|
|
14
|
-
"@contember/engine-plugins": "^1.
|
|
15
|
-
"@contember/engine-system-api": "^1.
|
|
16
|
-
"@contember/engine-tenant-api": "^1.
|
|
17
|
-
"@contember/graphql-utils": "^1.
|
|
18
|
-
"@contember/schema": "^1.
|
|
19
|
-
"@contember/schema-utils": "^1.
|
|
20
|
-
"@
|
|
12
|
+
"@contember/database": "^1.2.0-alpha.3",
|
|
13
|
+
"@contember/engine-content-api": "^1.2.0-alpha.3",
|
|
14
|
+
"@contember/engine-plugins": "^1.2.0-alpha.3",
|
|
15
|
+
"@contember/engine-system-api": "^1.2.0-alpha.3",
|
|
16
|
+
"@contember/engine-tenant-api": "^1.2.0-alpha.3",
|
|
17
|
+
"@contember/graphql-utils": "^1.2.0-alpha.3",
|
|
18
|
+
"@contember/schema": "^1.2.0-alpha.3",
|
|
19
|
+
"@contember/schema-utils": "^1.2.0-alpha.3",
|
|
20
|
+
"@contember/typesafe": "^1.2.0-alpha.3",
|
|
21
21
|
"@graphql-tools/schema": "^8.3.5",
|
|
22
|
-
"@types/lru-cache": "^
|
|
22
|
+
"@types/lru-cache": "^7.10.10",
|
|
23
23
|
"accepts": "^1.3.7",
|
|
24
24
|
"bcrypt": "^5.0.1",
|
|
25
25
|
"koa": "^2.11.0",
|
|
26
|
-
"lru-cache": "^
|
|
26
|
+
"lru-cache": "^7.10.1",
|
|
27
27
|
"path-to-regexp": "^6.2.0",
|
|
28
28
|
"uuid": "^8.3.1"
|
|
29
29
|
},
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"@types/koa-bodyparser": "^4.2.2",
|
|
34
34
|
"@types/koa__cors": "^3.0.2",
|
|
35
35
|
"@types/lru-cache": "^5.1.0",
|
|
36
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^16",
|
|
37
37
|
"@types/uuid": "^8.3.0",
|
|
38
|
-
"graphql": "^
|
|
38
|
+
"graphql": "^16.5.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"graphql": "^14.6.0",
|
package/src/ProjectContainer.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { DatabaseContextFactory } from '@contember/engine-system-api'
|
|
|
2
2
|
import { Connection } from '@contember/database'
|
|
3
3
|
import { ProjectConfig } from './config'
|
|
4
4
|
import { ContentSchemaResolver, GraphQlSchemaFactory } from './content'
|
|
5
|
+
import { Logger } from '@contember/engine-common'
|
|
5
6
|
|
|
6
7
|
export interface ProjectContainer {
|
|
7
8
|
systemDatabaseContextFactory: DatabaseContextFactory
|
|
@@ -14,7 +15,6 @@ export interface ProjectContainer {
|
|
|
14
15
|
export interface ProjectContainerResolver {
|
|
15
16
|
onCreate: ((container: ProjectContainer) => void | (() => void))[]
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
getProjectContainer(slug: string, aliasFallback?: boolean): Promise<ProjectContainer | undefined>
|
|
18
|
+
getProjectContainer(slug: string, options?: { alias?: boolean; logger?: Logger }): Promise<ProjectContainer | undefined>
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -9,11 +9,13 @@ import { SystemContainer } from '@contember/engine-system-api'
|
|
|
9
9
|
import { Authenticator } from './common'
|
|
10
10
|
import { TenantGraphQLHandler } from './tenant'
|
|
11
11
|
import { SystemGraphQLHandler } from './system'
|
|
12
|
+
import { ProjectMembershipResolver } from './content'
|
|
12
13
|
|
|
13
14
|
export interface ProjectGroupContainer {
|
|
14
15
|
slug: string | undefined
|
|
15
16
|
|
|
16
17
|
authenticator: Authenticator
|
|
18
|
+
projectMembershipResolver: ProjectMembershipResolver
|
|
17
19
|
|
|
18
20
|
projectContainerResolver: ProjectContainerResolver
|
|
19
21
|
projectSchemaResolver: ProjectSchemaResolver
|
|
@@ -26,7 +28,3 @@ export interface ProjectGroupContainer {
|
|
|
26
28
|
systemContainer: SystemContainer
|
|
27
29
|
systemGraphQLHandler: SystemGraphQLHandler
|
|
28
30
|
}
|
|
29
|
-
|
|
30
|
-
export interface ProjectGroupContainerResolver {
|
|
31
|
-
getProjectGroupContainer(slug: string | undefined, config: any): Promise<ProjectGroupContainer>
|
|
32
|
-
}
|
|
@@ -13,8 +13,8 @@ export class ErrorMiddlewareFactory {
|
|
|
13
13
|
try {
|
|
14
14
|
await next()
|
|
15
15
|
} catch (e) {
|
|
16
|
+
ctx.set('Content-type', 'application/json')
|
|
16
17
|
if (e instanceof HttpError) {
|
|
17
|
-
ctx.set('Content-type', 'application/json')
|
|
18
18
|
ctx.status = e.code
|
|
19
19
|
const body: any = { errors: [{ message: e.message, code: e.code }] }
|
|
20
20
|
if (this.debug && ctx.state.authResult) {
|
|
@@ -24,7 +24,10 @@ export class ErrorMiddlewareFactory {
|
|
|
24
24
|
}
|
|
25
25
|
ctx.body = JSON.stringify(body)
|
|
26
26
|
} else {
|
|
27
|
-
|
|
27
|
+
ctx.status = 500
|
|
28
|
+
ctx.body = JSON.stringify({ errors: [{ message: 'Internal server error' }] })
|
|
29
|
+
// eslint-disable-next-line no-console
|
|
30
|
+
console.error(e)
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { KoaMiddleware } from '../koa'
|
|
2
|
+
import { Readable } from 'stream'
|
|
2
3
|
|
|
3
4
|
type EventTime = { label: string; start: number; duration?: number }
|
|
4
5
|
|
|
@@ -25,17 +26,37 @@ export const createTimerMiddleware = (): KoaMiddleware<TimerMiddlewareState> =>
|
|
|
25
26
|
time.duration = new Date().getTime() - start
|
|
26
27
|
return res as any
|
|
27
28
|
}
|
|
29
|
+
|
|
28
30
|
await next()
|
|
29
31
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
const emit = () => {
|
|
33
|
+
const eventsDescription = times
|
|
34
|
+
.map(it => `${it.label} T+${it.start}ms ${it.duration !== undefined ? it.duration : '[unknown]'}ms`)
|
|
35
|
+
.join('; ')
|
|
36
|
+
|
|
37
|
+
const total = new Date().getTime() - globalStart
|
|
38
|
+
const timeLabel = total > 500 ? 'TIME_SLOW' : 'TIME_OK'
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.log(
|
|
41
|
+
`${ctx.request.method} ${ctx.request.url} [${ctx.response.status}] ${ctx.req.connection.remoteAddress} ${timeLabel} ${total}ms (${eventsDescription})`,
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ctx.response.body instanceof Readable) {
|
|
46
|
+
const body = ctx.response.body
|
|
47
|
+
const streamTimer = ctx.state.timer('Stream', () => {
|
|
48
|
+
return new Promise(resolve => {
|
|
49
|
+
body.addListener('close', () => {
|
|
50
|
+
resolve(null)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
streamTimer.then(emit).catch(emit)
|
|
56
|
+
|
|
57
|
+
} else {
|
|
58
|
+
emit()
|
|
59
|
+
}
|
|
39
60
|
}
|
|
40
61
|
return timer
|
|
41
62
|
}
|
package/src/config.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DatabaseConfig, PoolConfig } from '@contember/database'
|
|
2
2
|
|
|
3
3
|
export type ProjectConfig = {
|
|
4
4
|
readonly slug: string
|
|
5
5
|
readonly alias?: string[]
|
|
6
6
|
readonly name: string
|
|
7
7
|
readonly stages: StageConfig[]
|
|
8
|
-
readonly db:
|
|
8
|
+
readonly db:
|
|
9
|
+
& DatabaseConfig
|
|
10
|
+
& {
|
|
11
|
+
pool?: PoolConfig
|
|
12
|
+
systemSchema?: string
|
|
13
|
+
}
|
|
9
14
|
} & Record<string, unknown>
|
|
10
15
|
|
|
11
16
|
export interface StageConfig {
|
|
@@ -7,6 +7,7 @@ import { ContentGraphQLContextFactory } from './ContentGraphQLContextFactory'
|
|
|
7
7
|
import { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory'
|
|
8
8
|
import { GraphQLSchema } from 'graphql'
|
|
9
9
|
import { GraphQLKoaState } from '../graphql'
|
|
10
|
+
import { Logger } from '@contember/engine-common'
|
|
10
11
|
|
|
11
12
|
type ContentApiMiddlewareKoaState =
|
|
12
13
|
& TimerMiddlewareState
|
|
@@ -15,9 +16,9 @@ type ContentApiMiddlewareKoaState =
|
|
|
15
16
|
& ProjectInfoMiddlewareState
|
|
16
17
|
& { authResult: AuthResult }
|
|
17
18
|
|
|
19
|
+
|
|
18
20
|
export class ContentApiMiddlewareFactory {
|
|
19
21
|
constructor(
|
|
20
|
-
private readonly debug: boolean,
|
|
21
22
|
private readonly projectGroupResolver: ProjectGroupResolver,
|
|
22
23
|
private readonly notModifiedChecker: NotModifiedChecker,
|
|
23
24
|
private readonly contentGraphqlContextFactory: ContentGraphQLContextFactory,
|
|
@@ -36,10 +37,17 @@ export class ContentApiMiddlewareFactory {
|
|
|
36
37
|
const authResult = await groupContainer.authenticator.authenticate({ request, timer })
|
|
37
38
|
koaContext.state.authResult = authResult
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
const
|
|
40
|
+
// eslint-disable-next-line no-console
|
|
41
|
+
const logger = new Logger(console.log)
|
|
42
|
+
logger.group(`Initializing ${groupContainer.slug}/${params.projectSlug}`)
|
|
43
|
+
|
|
44
|
+
const projectContainer = await groupContainer.projectContainerResolver.getProjectContainer(params.projectSlug, {
|
|
45
|
+
alias: true,
|
|
46
|
+
logger,
|
|
47
|
+
})
|
|
48
|
+
logger.groupEnd()
|
|
41
49
|
if (projectContainer === undefined) {
|
|
42
|
-
throw new HttpError(`Project ${projectSlug} NOT found`, 404)
|
|
50
|
+
throw new HttpError(`Project ${params.projectSlug} NOT found`, 404)
|
|
43
51
|
}
|
|
44
52
|
const project = projectContainer.project
|
|
45
53
|
koaContext.state.project = project.slug
|
|
@@ -59,35 +67,19 @@ export class ContentApiMiddlewareFactory {
|
|
|
59
67
|
response.status = 304
|
|
60
68
|
return
|
|
61
69
|
}
|
|
62
|
-
|
|
63
|
-
const tenantContainer = groupContainer.tenantContainer
|
|
64
|
-
const explicitMemberships = await timer('MembershipFetch', () =>
|
|
65
|
-
tenantContainer.projectMemberManager.getProjectMemberships(
|
|
66
|
-
tenantContainer.databaseContext,
|
|
67
|
-
{ slug: project.slug },
|
|
68
|
-
{
|
|
69
|
-
id: authResult.identityId,
|
|
70
|
-
roles: authResult.roles,
|
|
71
|
-
},
|
|
72
|
-
undefined,
|
|
73
|
-
),
|
|
74
|
-
)
|
|
75
70
|
const schema = await projectContainer.contentSchemaResolver.getSchema(systemDatabase, stage.slug)
|
|
76
|
-
const implicitRoles = Object.entries(schema.acl.roles).filter(([, role]) => role.implicit).map(([name]) => name)
|
|
77
71
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
const memberships = await timer('MembershipFetch', () => groupContainer.projectMembershipResolver.resolveMemberships({
|
|
73
|
+
request,
|
|
74
|
+
acl: schema.acl,
|
|
75
|
+
projectSlug: project.slug,
|
|
76
|
+
identity: {
|
|
77
|
+
id: authResult.identityId,
|
|
78
|
+
roles: authResult.roles,
|
|
79
|
+
},
|
|
80
|
+
}))
|
|
81
|
+
const projectRoles = memberships.map(it => it.role)
|
|
83
82
|
|
|
84
|
-
const explicitProjectRoles = explicitMemberships.map(it => it.role)
|
|
85
|
-
const implicitRolesToAssign = implicitRoles.filter(it => !explicitProjectRoles.includes(it))
|
|
86
|
-
const memberships = [
|
|
87
|
-
...explicitMemberships,
|
|
88
|
-
...implicitRolesToAssign.map(it => ({ role: it, variables: [] })),
|
|
89
|
-
]
|
|
90
|
-
const projectRoles = [...explicitProjectRoles, ...implicitRolesToAssign]
|
|
91
83
|
const contentDatabase = projectContainer.connection.createClient(stage.schema, { module: 'content' })
|
|
92
84
|
|
|
93
85
|
const [graphQlSchema, permissions] = await timer('GraphQLSchemaCreate', () => projectContainer.graphQlSchemaFactory.create(schema, {
|
|
@@ -11,7 +11,7 @@ export class ContentSchemaResolver {
|
|
|
11
11
|
this.schemaCache = {}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
public async getSchema(db: DatabaseContext, stage: string): Promise<
|
|
14
|
+
public async getSchema(db: DatabaseContext, stage: string): Promise<VersionedSchema> {
|
|
15
15
|
const cachedSchema = this.schemaCache[stage]
|
|
16
16
|
let newSchema = await this.schemaVersionBuilder.buildSchema(db, cachedSchema)
|
|
17
17
|
if (newSchema !== cachedSchema) {
|
|
@@ -9,8 +9,7 @@ import {
|
|
|
9
9
|
} from '@contember/engine-content-api'
|
|
10
10
|
import { Acl, Schema } from '@contember/schema'
|
|
11
11
|
import { GraphQLSchema } from 'graphql'
|
|
12
|
-
import { mergeSchemas } from '@graphql-tools/
|
|
13
|
-
import { makeExecutableSchema } from '@graphql-tools/schema'
|
|
12
|
+
import { makeExecutableSchema, mergeSchemas } from '@graphql-tools/schema'
|
|
14
13
|
import { GraphQLSchemaContributor } from '@contember/engine-plugins'
|
|
15
14
|
import { JSONType } from '@contember/graphql-utils'
|
|
16
15
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DatabaseContext, Membership, ProjectMemberManager } from '@contember/engine-tenant-api'
|
|
2
|
+
|
|
3
|
+
export class ProjectMembershipFetcher {
|
|
4
|
+
constructor(
|
|
5
|
+
private readonly projectMemberManager: ProjectMemberManager,
|
|
6
|
+
private readonly databaseContext: DatabaseContext,
|
|
7
|
+
) {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public async fetchMemberships(projectSlug: string, identity: { id: string; roles?: readonly string[] }): Promise<readonly Membership[]> {
|
|
11
|
+
return await this.projectMemberManager.getProjectMemberships(
|
|
12
|
+
this.databaseContext,
|
|
13
|
+
{ slug: projectSlug },
|
|
14
|
+
identity,
|
|
15
|
+
undefined,
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Membership, MembershipMatcher } from '@contember/engine-tenant-api'
|
|
2
|
+
import * as Typesafe from '@contember/typesafe'
|
|
3
|
+
import { Acl } from '@contember/schema'
|
|
4
|
+
import { HttpError } from '../common'
|
|
5
|
+
import { ProjectMembershipFetcher } from './ProjectMembershipFetcher'
|
|
6
|
+
|
|
7
|
+
const assumeMembershipHeader = 'x-contember-assume-membership'
|
|
8
|
+
|
|
9
|
+
interface HeaderAccessor {
|
|
10
|
+
get(header: string): string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class ProjectMembershipResolver {
|
|
14
|
+
constructor(
|
|
15
|
+
private readonly debug: boolean,
|
|
16
|
+
private readonly projectMembershipFetcher: ProjectMembershipFetcher,
|
|
17
|
+
) {
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async resolveMemberships({ request, acl, projectSlug, identity }: {
|
|
21
|
+
acl: Acl.Schema
|
|
22
|
+
request: HeaderAccessor
|
|
23
|
+
projectSlug: string
|
|
24
|
+
identity: { id: string; roles?: readonly string[] }
|
|
25
|
+
}): Promise<readonly Membership[]> {
|
|
26
|
+
|
|
27
|
+
const explicitMemberships = await this.projectMembershipFetcher.fetchMemberships(projectSlug, identity)
|
|
28
|
+
|
|
29
|
+
const implicitRoles = Object.entries(acl.roles).filter(([, role]) => role.implicit).map(([name]) => name)
|
|
30
|
+
|
|
31
|
+
if (explicitMemberships.length === 0 && implicitRoles.length === 0) {
|
|
32
|
+
const errorMessage = this.debug
|
|
33
|
+
? `You are not allowed to access project ${projectSlug}`
|
|
34
|
+
: `Project ${projectSlug} NOT found`
|
|
35
|
+
throw new HttpError(errorMessage, 404)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const assumedMemberships = this.readAssumedMemberships(request)
|
|
39
|
+
if (assumedMemberships.length > 0) {
|
|
40
|
+
this.verifyAssumedRoles(explicitMemberships, acl, assumedMemberships)
|
|
41
|
+
|
|
42
|
+
return assumedMemberships
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
const explicitProjectRoles = explicitMemberships.map(it => it.role)
|
|
47
|
+
const implicitRolesToAssign = implicitRoles.filter(it => !explicitProjectRoles.includes(it))
|
|
48
|
+
|
|
49
|
+
return [
|
|
50
|
+
...explicitMemberships,
|
|
51
|
+
...implicitRolesToAssign.map(it => ({ role: it, variables: [] })),
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private readAssumedMemberships(req: HeaderAccessor): readonly Membership[] {
|
|
56
|
+
const value = req.get(assumeMembershipHeader).trim()
|
|
57
|
+
if (value === '') {
|
|
58
|
+
return []
|
|
59
|
+
}
|
|
60
|
+
let parsedValue: { memberships: readonly Membership[] }
|
|
61
|
+
try {
|
|
62
|
+
parsedValue = assumeMembershipValueType(JSON.parse(value))
|
|
63
|
+
} catch (e: any) {
|
|
64
|
+
throw new HttpError(`Invalid content of ${assumeMembershipHeader}: ${e.message}`, 400)
|
|
65
|
+
}
|
|
66
|
+
return parsedValue.memberships
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private verifyAssumedRoles(explicitMemberships: readonly Membership[], acl: Acl.Schema, assumedMemberships: readonly Membership[]) {
|
|
70
|
+
const membershipMatcher = new MembershipMatcher(explicitMemberships.map(it => ({
|
|
71
|
+
...it,
|
|
72
|
+
matchRule: acl.roles[it.role].content?.assumeMembership ?? {},
|
|
73
|
+
})))
|
|
74
|
+
|
|
75
|
+
for (const assumed of assumedMemberships) {
|
|
76
|
+
if (!membershipMatcher.matches(assumed)) {
|
|
77
|
+
throw new HttpError(`You are not allow to assume membership ${JSON.stringify(assumed)}`, 403)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const assumeMembershipValueType = Typesafe.object({
|
|
84
|
+
memberships: Typesafe.array(
|
|
85
|
+
Typesafe.object({
|
|
86
|
+
role: Typesafe.string,
|
|
87
|
+
variables: Typesafe.array(
|
|
88
|
+
Typesafe.object({
|
|
89
|
+
name: Typesafe.string,
|
|
90
|
+
values: Typesafe.array(Typesafe.string),
|
|
91
|
+
}),
|
|
92
|
+
),
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
})
|
package/src/content/index.ts
CHANGED
package/src/index.ts
CHANGED
package/src/koa/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Koa from 'koa'
|
|
2
2
|
|
|
3
|
-
export type KoaContext<State> = Koa.ExtendableContext & { state: State }
|
|
3
|
+
export type KoaContext<State> = Koa.ExtendableContext & { state: State; compress?: boolean }
|
|
4
4
|
type KoaNext = () => Promise<any>
|
|
5
5
|
export type KoaMiddleware<State> = (context: KoaContext<State>, next: KoaNext) => any
|
|
@@ -1,56 +1,6 @@
|
|
|
1
|
-
import { ProjectGroupContainer, ProjectGroupContainerResolver } from '../ProjectGroupContainer'
|
|
2
|
-
import { CryptoWrapper } from '../utils/CryptoWrapper'
|
|
3
|
-
import { HttpError } from '../common'
|
|
4
1
|
import { Request } from 'koa'
|
|
2
|
+
import { ProjectGroupContainer } from '../ProjectGroupContainer'
|
|
5
3
|
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
this.projectGroupDomainMapping
|
|
9
|
-
? new RegExp(
|
|
10
|
-
this.projectGroupDomainMapping.includes('{group}')
|
|
11
|
-
? regexpQuote(this.projectGroupDomainMapping).replace(regexpQuote('{group}'), '([^.]+)')
|
|
12
|
-
: this.projectGroupDomainMapping,
|
|
13
|
-
)
|
|
14
|
-
: undefined
|
|
15
|
-
)
|
|
16
|
-
private projectGroupConfigHeader
|
|
17
|
-
|
|
18
|
-
constructor(
|
|
19
|
-
private projectGroupDomainMapping: string | undefined,
|
|
20
|
-
projectGroupConfigHeader: string | undefined,
|
|
21
|
-
private projectGroupConfigCrypto: CryptoWrapper | undefined,
|
|
22
|
-
private projectGroupContainerResolver: ProjectGroupContainerResolver,
|
|
23
|
-
) {
|
|
24
|
-
this.projectGroupConfigHeader = projectGroupConfigHeader?.toLowerCase()
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async resolveContainer({ request }: { request: Request }): Promise<ProjectGroupContainer> {
|
|
28
|
-
let group: string | undefined = undefined
|
|
29
|
-
let config = {}
|
|
30
|
-
if (this.groupRegex) {
|
|
31
|
-
const match = request.host.match(this.groupRegex)
|
|
32
|
-
if (!match) {
|
|
33
|
-
throw new HttpError('Project group not found', 404)
|
|
34
|
-
}
|
|
35
|
-
group = match[1]
|
|
36
|
-
if (this.projectGroupConfigHeader) {
|
|
37
|
-
const configHeader = request.get(this.projectGroupConfigHeader.toLowerCase())
|
|
38
|
-
if (configHeader === '') {
|
|
39
|
-
throw new HttpError(`${this.projectGroupConfigHeader} header is missing`, 400)
|
|
40
|
-
}
|
|
41
|
-
const configValue = Buffer.from(configHeader, 'base64')
|
|
42
|
-
const decryptedValue = this.projectGroupConfigCrypto ?
|
|
43
|
-
(await this.projectGroupConfigCrypto?.decrypt(configValue, CryptoWrapper.cryptoVersion)).value
|
|
44
|
-
: configValue
|
|
45
|
-
try {
|
|
46
|
-
config = JSON.parse(decryptedValue.toString())
|
|
47
|
-
} catch (e: any) {
|
|
48
|
-
throw new HttpError(`Cannot parse config: ${e.message}`, 400)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return await this.projectGroupContainerResolver.getProjectGroupContainer(group, config)
|
|
53
|
-
}
|
|
4
|
+
export interface ProjectGroupResolver {
|
|
5
|
+
resolveContainer({ request }: { request: Request }): Promise<ProjectGroupContainer>
|
|
54
6
|
}
|
|
55
|
-
|
|
56
|
-
const regexpQuote = (regexp: string) => regexp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
@@ -3,6 +3,7 @@ import { ProjectGroupResolver, ProjectInfoMiddlewareState } from '../project-com
|
|
|
3
3
|
import { AuthResult, HttpError, TimerMiddlewareState } from '../common'
|
|
4
4
|
import { GraphQLKoaState } from '../graphql'
|
|
5
5
|
import { SystemGraphQLContextFactory } from './SystemGraphQLContextFactory'
|
|
6
|
+
import { Logger } from '@contember/engine-common'
|
|
6
7
|
|
|
7
8
|
type SystemApiMiddlewareKoaState =
|
|
8
9
|
& TimerMiddlewareState
|
|
@@ -28,7 +29,14 @@ export class SystemApiMiddlewareFactory {
|
|
|
28
29
|
koaContext.state.authResult = authResult
|
|
29
30
|
|
|
30
31
|
const projectSlug = params.projectSlug
|
|
31
|
-
|
|
32
|
+
// eslint-disable-next-line no-console
|
|
33
|
+
const logger = new Logger(console.log)
|
|
34
|
+
logger.group(`Initializing ${groupContainer.slug}/${params.projectSlug}`)
|
|
35
|
+
const projectContainer = await groupContainer.projectContainerResolver.getProjectContainer(projectSlug, {
|
|
36
|
+
alias: true,
|
|
37
|
+
logger,
|
|
38
|
+
})
|
|
39
|
+
logger.groupEnd()
|
|
32
40
|
if (projectContainer === undefined) {
|
|
33
41
|
throw new HttpError(`Project ${projectSlug} NOT found`, 404)
|
|
34
42
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as Typesafe from '@contember/typesafe'
|
|
2
|
+
|
|
3
|
+
const tuple = <T extends any[]>(...values: T) => values
|
|
4
|
+
|
|
5
|
+
export const Command = Typesafe.discriminatedTupleUnion({
|
|
6
|
+
importTenantSchemaBegin: tuple(),
|
|
7
|
+
importSystemSchemaBegin: tuple(
|
|
8
|
+
Typesafe.object({
|
|
9
|
+
project: Typesafe.string,
|
|
10
|
+
tables: Typesafe.array(Typesafe.string),
|
|
11
|
+
}),
|
|
12
|
+
),
|
|
13
|
+
importContentSchemaBegin: tuple(
|
|
14
|
+
Typesafe.object({
|
|
15
|
+
project: Typesafe.string,
|
|
16
|
+
stage: Typesafe.string,
|
|
17
|
+
schemaVersion: Typesafe.string,
|
|
18
|
+
tables: Typesafe.array(Typesafe.string),
|
|
19
|
+
}),
|
|
20
|
+
),
|
|
21
|
+
importSequence: tuple(
|
|
22
|
+
Typesafe.object({
|
|
23
|
+
table: Typesafe.string,
|
|
24
|
+
column: Typesafe.string,
|
|
25
|
+
value: Typesafe.number,
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
28
|
+
insertBegin: tuple(
|
|
29
|
+
Typesafe.object({
|
|
30
|
+
table: Typesafe.string,
|
|
31
|
+
columns: Typesafe.array(Typesafe.string),
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
insertRow: tuple(
|
|
35
|
+
Typesafe.array(Typesafe.anyJson), // values
|
|
36
|
+
),
|
|
37
|
+
insertEnd: tuple(),
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
export type Command = ReturnType<typeof Command>
|
|
41
|
+
export type CommandName = Command[0]
|
|
42
|
+
export type CommandArgsMap<S extends Command = Command> = { [K in S[0]]: S extends readonly [K, ...infer T] ? T : never }
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
import * as Typesafe from '@contember/typesafe'
|
|
3
|
+
import { Command } from './Command'
|
|
4
|
+
import { ImportError } from './ImportExecutor'
|
|
5
|
+
|
|
6
|
+
export async function* toBuffer(lines: AsyncIterable<any>, bufferSize: number = 16 * 1024): AsyncIterable<Buffer> {
|
|
7
|
+
let chunks = []
|
|
8
|
+
let chunksLength = 0
|
|
9
|
+
|
|
10
|
+
for await (const line of lines) {
|
|
11
|
+
const chunk = Buffer.from(JSON.stringify(line) + '\n')
|
|
12
|
+
chunks.push(chunk)
|
|
13
|
+
chunksLength += chunk.length
|
|
14
|
+
|
|
15
|
+
if (chunksLength >= bufferSize) {
|
|
16
|
+
yield Buffer.concat(chunks)
|
|
17
|
+
chunks = []
|
|
18
|
+
chunksLength = 0
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (chunksLength > 0) {
|
|
23
|
+
yield Buffer.concat(chunks)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function* fromBuffer(stream: AsyncIterable<Buffer>): AsyncIterable<Command> {
|
|
28
|
+
yield* readCommands(readLines(stream))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function* readCommands(lines: AsyncIterable<string>) {
|
|
32
|
+
for await (let line of lines) {
|
|
33
|
+
try {
|
|
34
|
+
yield Command(JSON.parse(line))
|
|
35
|
+
|
|
36
|
+
} catch (e) {
|
|
37
|
+
if (e instanceof SyntaxError) {
|
|
38
|
+
throw new ImportError(`Line ${line} is not valid JSON`)
|
|
39
|
+
|
|
40
|
+
} else if (e instanceof Typesafe.ParseError) {
|
|
41
|
+
throw new ImportError(`Line ${line} is not valid command`)
|
|
42
|
+
|
|
43
|
+
} else {
|
|
44
|
+
throw e
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function* readLines(stream: AsyncIterable<Buffer>) {
|
|
51
|
+
let chunks = []
|
|
52
|
+
|
|
53
|
+
for await (let chunk of stream) {
|
|
54
|
+
while (true) {
|
|
55
|
+
const eolIndex = chunk.indexOf('\n')
|
|
56
|
+
|
|
57
|
+
if (eolIndex < 0) {
|
|
58
|
+
chunks.push(chunk)
|
|
59
|
+
break
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
chunks.push(chunk.slice(0, eolIndex))
|
|
63
|
+
chunk = chunk.slice(eolIndex + 1)
|
|
64
|
+
yield Buffer.concat(chunks).toString('utf8')
|
|
65
|
+
chunks = []
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (Buffer.concat(chunks).length > 0) {
|
|
70
|
+
throw new ImportError(`Unexpected stream end`)
|
|
71
|
+
}
|
|
72
|
+
}
|