@contember/engine-http 2.2.0-alpha.13 → 2.2.0-alpha.14
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/development/src/MasterContainer.cjs +2 -1
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +2 -1
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/config/configSchema.cjs +23 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +23 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +5 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +5 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.cjs +3 -2
- package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.js +3 -2
- package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.cjs +3 -6
- package/dist/development/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.js +3 -6
- package/dist/development/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/development/src/project/ProjectContainerResolver.cjs +23 -8
- package/dist/development/src/project/ProjectContainerResolver.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainerResolver.js +23 -8
- package/dist/development/src/project/ProjectContainerResolver.js.map +1 -1
- package/dist/development/src/project/ProjectContainerStore.cjs +3 -0
- package/dist/development/src/project/ProjectContainerStore.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainerStore.js +3 -0
- package/dist/development/src/project/ProjectContainerStore.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.cjs +1 -3
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.js +1 -3
- package/dist/development/src/projectGroup/ProjectGroupContainerResolver.js.map +1 -1
- package/dist/development/src/prometheus/dbMetrics.cjs +1 -1
- package/dist/development/src/prometheus/dbMetrics.cjs.map +1 -1
- package/dist/development/src/prometheus/dbMetrics.js +1 -1
- package/dist/development/src/prometheus/dbMetrics.js.map +1 -1
- package/dist/development/src/system/migrationLockOptions.cjs +14 -0
- package/dist/development/src/system/migrationLockOptions.cjs.map +1 -0
- package/dist/development/src/system/migrationLockOptions.js +14 -0
- package/dist/development/src/system/migrationLockOptions.js.map +1 -0
- package/dist/production/src/MasterContainer.cjs +2 -1
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +2 -1
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/config/configSchema.cjs +23 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +23 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +5 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +5 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.cjs +3 -2
- package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.js +3 -2
- package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.cjs +3 -6
- package/dist/production/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.js +3 -6
- package/dist/production/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/production/src/project/ProjectContainerResolver.cjs +23 -8
- package/dist/production/src/project/ProjectContainerResolver.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainerResolver.js +23 -8
- package/dist/production/src/project/ProjectContainerResolver.js.map +1 -1
- package/dist/production/src/project/ProjectContainerStore.cjs +3 -0
- package/dist/production/src/project/ProjectContainerStore.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainerStore.js +3 -0
- package/dist/production/src/project/ProjectContainerStore.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.cjs +1 -3
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.js +1 -3
- package/dist/production/src/projectGroup/ProjectGroupContainerResolver.js.map +1 -1
- package/dist/production/src/prometheus/dbMetrics.cjs +1 -1
- package/dist/production/src/prometheus/dbMetrics.cjs.map +1 -1
- package/dist/production/src/prometheus/dbMetrics.js +1 -1
- package/dist/production/src/prometheus/dbMetrics.js.map +1 -1
- package/dist/production/src/system/migrationLockOptions.cjs +14 -0
- package/dist/production/src/system/migrationLockOptions.cjs.map +1 -0
- package/dist/production/src/system/migrationLockOptions.js +14 -0
- package/dist/production/src/system/migrationLockOptions.js.map +1 -0
- package/dist/tests/cases/unit/dbMetrics.test.d.ts +2 -0
- package/dist/tests/cases/unit/dbMetrics.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/projectContainerResolver.test.d.ts +2 -0
- package/dist/tests/cases/unit/projectContainerResolver.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +5 -0
- package/dist/types/MasterContainer.d.ts.map +1 -1
- package/dist/types/config/configSchema.d.ts +10 -0
- package/dist/types/config/configSchema.d.ts.map +1 -1
- package/dist/types/config/configTemplate.d.ts.map +1 -1
- package/dist/types/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/types/content/ProjectMembershipResolver.d.ts.map +1 -1
- package/dist/types/project/ProjectContainerResolver.d.ts +7 -1
- package/dist/types/project/ProjectContainerResolver.d.ts.map +1 -1
- package/dist/types/project/ProjectContainerStore.d.ts +1 -0
- package/dist/types/project/ProjectContainerStore.d.ts.map +1 -1
- package/dist/types/projectGroup/ProjectGroupContainerResolver.d.ts.map +1 -1
- package/dist/types/system/migrationLockOptions.d.ts +4 -0
- package/dist/types/system/migrationLockOptions.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -16
- package/src/MasterContainer.ts +3 -2
- package/src/config/configSchema.ts +24 -0
- package/src/config/configTemplate.ts +5 -0
- package/src/content/GraphQlSchemaFactory.ts +5 -2
- package/src/content/ProjectMembershipResolver.ts +5 -6
- package/src/project/ProjectContainerResolver.ts +27 -9
- package/src/project/ProjectContainerStore.ts +4 -0
- package/src/projectGroup/ProjectGroupContainerResolver.ts +1 -4
- package/src/prometheus/dbMetrics.ts +1 -1
- package/src/system/migrationLockOptions.ts +13 -0
- package/tests/cases/unit/dbMetrics.test.ts +40 -0
- package/tests/cases/unit/graphQlSchemaFactory.test.ts +43 -0
- package/tests/cases/unit/projectContainerResolver.test.ts +150 -0
- package/tests/cases/unit/projectMembershipResolver.test.ts +44 -0
- package/tests/cases/unit/serverConfig.test.ts +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.14",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "./dist/production/index.js",
|
|
6
6
|
"typings": "./dist/types/index.d.ts",
|
|
@@ -26,21 +26,21 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@contember/config-loader": "2.2.0-alpha.
|
|
30
|
-
"@contember/database": "2.2.0-alpha.
|
|
31
|
-
"@contember/database-migrations": "2.2.0-alpha.
|
|
32
|
-
"@contember/dic": "2.2.0-alpha.
|
|
33
|
-
"@contember/engine-common": "2.2.0-alpha.
|
|
34
|
-
"@contember/engine-content-api": "2.2.0-alpha.
|
|
35
|
-
"@contember/engine-system-api": "2.2.0-alpha.
|
|
36
|
-
"@contember/engine-tenant-api": "2.2.0-alpha.
|
|
37
|
-
"@contember/graphql-utils": "2.2.0-alpha.
|
|
38
|
-
"@contember/logger": "2.2.0-alpha.
|
|
39
|
-
"@contember/schema": "2.2.0-alpha.
|
|
40
|
-
"@contember/schema-migrations": "2.2.0-alpha.
|
|
41
|
-
"@contember/schema-utils": "2.2.0-alpha.
|
|
42
|
-
"@contember/telemetry": "2.2.0-alpha.
|
|
43
|
-
"@contember/typesafe": "2.2.0-alpha.
|
|
29
|
+
"@contember/config-loader": "2.2.0-alpha.14",
|
|
30
|
+
"@contember/database": "2.2.0-alpha.14",
|
|
31
|
+
"@contember/database-migrations": "2.2.0-alpha.14",
|
|
32
|
+
"@contember/dic": "2.2.0-alpha.14",
|
|
33
|
+
"@contember/engine-common": "2.2.0-alpha.14",
|
|
34
|
+
"@contember/engine-content-api": "2.2.0-alpha.14",
|
|
35
|
+
"@contember/engine-system-api": "2.2.0-alpha.14",
|
|
36
|
+
"@contember/engine-tenant-api": "2.2.0-alpha.14",
|
|
37
|
+
"@contember/graphql-utils": "2.2.0-alpha.14",
|
|
38
|
+
"@contember/logger": "2.2.0-alpha.14",
|
|
39
|
+
"@contember/schema": "2.2.0-alpha.14",
|
|
40
|
+
"@contember/schema-migrations": "2.2.0-alpha.14",
|
|
41
|
+
"@contember/schema-utils": "2.2.0-alpha.14",
|
|
42
|
+
"@contember/telemetry": "2.2.0-alpha.14",
|
|
43
|
+
"@contember/typesafe": "2.2.0-alpha.14",
|
|
44
44
|
"@graphql-tools/merge": "^9.1.7",
|
|
45
45
|
"@graphql-tools/schema": "^10.0.31",
|
|
46
46
|
"@koa/cors": "^5.0.0",
|
package/src/MasterContainer.ts
CHANGED
|
@@ -38,6 +38,7 @@ import { PrometheusRegistryFactory } from './prometheus/PrometheusRegistryFactor
|
|
|
38
38
|
import { ProjectGroupContainerTelemetryHook } from './telemetry/ProjectGroupContainerTelemetryHook.js'
|
|
39
39
|
import { createProviders, Providers } from './providers.js'
|
|
40
40
|
import { SystemApiMiddlewareFactory, SystemGraphQLContextFactory, SystemGraphQLHandlerFactory } from './system/index.js'
|
|
41
|
+
import { createMigrationLockOptions } from './system/migrationLockOptions.js'
|
|
41
42
|
import { ContentQueryExecutorImpl } from './system/ContentQueryExecutor.js'
|
|
42
43
|
import { OidcBackchannelLogoutMiddlewareFactory, TenantApiMiddlewareFactory, TenantGraphQLHandlerFactory } from './tenant/index.js'
|
|
43
44
|
import {
|
|
@@ -128,8 +129,8 @@ export class MasterContainerFactory {
|
|
|
128
129
|
)
|
|
129
130
|
.addService(
|
|
130
131
|
'systemContainerFactory',
|
|
131
|
-
({ providers, modificationHandlerFactory, contentQueryExecutor }) =>
|
|
132
|
-
new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor),
|
|
132
|
+
({ providers, modificationHandlerFactory, contentQueryExecutor, serverConfig }) =>
|
|
133
|
+
new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor, createMigrationLockOptions(serverConfig.systemApi)),
|
|
133
134
|
)
|
|
134
135
|
.addService('contentPermissionFactory', ({}) => new PermissionFactory())
|
|
135
136
|
.addService('databaseMetadataResolver', () => new DatabaseMetadataResolver())
|
|
@@ -111,6 +111,11 @@ const positiveInteger: Typesafe.Type<number> = (input, path = []) => {
|
|
|
111
111
|
return value > 0 ? value : Typesafe.fail(path, 'must be a positive integer')
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
const nonNegativeInteger: Typesafe.Type<number> = (input, path = []) => {
|
|
115
|
+
const value = Typesafe.integer(input, path)
|
|
116
|
+
return value >= 0 ? value : Typesafe.fail(path, 'must be a non-negative integer')
|
|
117
|
+
}
|
|
118
|
+
|
|
114
119
|
export const telemetryConfigSchema = Typesafe.partial({
|
|
115
120
|
resource: Typesafe.partial({
|
|
116
121
|
serviceName: Typesafe.string,
|
|
@@ -233,6 +238,25 @@ export const serverConfigSchema = Typesafe.partial({
|
|
|
233
238
|
return Typesafe.fail([])
|
|
234
239
|
},
|
|
235
240
|
}),
|
|
241
|
+
systemApi: Typesafe.transform(
|
|
242
|
+
Typesafe.partial({
|
|
243
|
+
// Opt-in: unset, a migration waits for its locks as long as PostgreSQL lets it and is never retried.
|
|
244
|
+
// Zero is rejected rather than passed on: PostgreSQL reads `lock_timeout = 0` as no limit.
|
|
245
|
+
migrationLockTimeoutMs: positiveInteger,
|
|
246
|
+
migrationMaxAttempts: positiveInteger,
|
|
247
|
+
migrationRetryDelayMs: nonNegativeInteger,
|
|
248
|
+
}),
|
|
249
|
+
(value, input, path) => {
|
|
250
|
+
const hasRetrySettings = value.migrationMaxAttempts !== undefined || value.migrationRetryDelayMs !== undefined
|
|
251
|
+
if (hasRetrySettings && value.migrationLockTimeoutMs === undefined) {
|
|
252
|
+
return Typesafe.fail(
|
|
253
|
+
path,
|
|
254
|
+
'migrationMaxAttempts and migrationRetryDelayMs require migrationLockTimeoutMs (CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS), without it migrations are not retried',
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
return value
|
|
258
|
+
},
|
|
259
|
+
),
|
|
236
260
|
contentApi: Typesafe.partial({
|
|
237
261
|
schemaCacheTtlSeconds: Typesafe.integer,
|
|
238
262
|
schemaCacheMaxEntries: (input: unknown, path: PropertyKey[] = []): number | undefined => {
|
|
@@ -103,6 +103,11 @@ export const configTemplate: any = {
|
|
|
103
103
|
enabled: '%?env.CONTEMBER_PANEL_ENABLED::bool%',
|
|
104
104
|
path: '%?env.CONTEMBER_PANEL_PATH%',
|
|
105
105
|
},
|
|
106
|
+
systemApi: {
|
|
107
|
+
migrationLockTimeoutMs: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS::number%',
|
|
108
|
+
migrationMaxAttempts: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_MAX_ATTEMPTS::number%',
|
|
109
|
+
migrationRetryDelayMs: '%?env.CONTEMBER_SYSTEM_API_MIGRATION_RETRY_DELAY_MS::number%',
|
|
110
|
+
},
|
|
106
111
|
contentApi: {
|
|
107
112
|
schemaCacheTtlSeconds: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_TTL_SECONDS::number%',
|
|
108
113
|
schemaCacheMaxEntries: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_MAX_ENTRIES::number%',
|
|
@@ -41,7 +41,7 @@ export class GraphQlSchemaFactory {
|
|
|
41
41
|
const { root: permissions, all: allPermissions } = computedPermissions ?? this.permissionFactory.createContextual(schema, identity.projectRoles)
|
|
42
42
|
|
|
43
43
|
const customPrimary = schema.acl.customPrimary ?? false
|
|
44
|
-
const refreshMaterializedView =
|
|
44
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, identity.projectRoles)
|
|
45
45
|
// Types are shaped by the nested set, so a field granted only through a relation still exists
|
|
46
46
|
// on the shared type; the root query and mutation fields are gated on the root set alone.
|
|
47
47
|
const authorizator = new Authorizator(allPermissions, customPrimary, refreshMaterializedView)
|
|
@@ -110,7 +110,10 @@ export class GraphQlSchemaFactory {
|
|
|
110
110
|
return { permissionsKey: cachedKey }
|
|
111
111
|
}
|
|
112
112
|
const computedPermissions = this.permissionFactory.createContextual(schema, roles)
|
|
113
|
-
const
|
|
113
|
+
const refreshMaterializedView = this.permissionFactory.isRefreshMaterializedViewAllowed(schema, roles)
|
|
114
|
+
const permissionsKey = createHash('sha256')
|
|
115
|
+
.update(JSON.stringify([computedPermissions.root, computedPermissions.all, refreshMaterializedView]))
|
|
116
|
+
.digest('hex')
|
|
114
117
|
keysByRoles.set(rolesKey, permissionsKey)
|
|
115
118
|
return { permissionsKey, computedPermissions }
|
|
116
119
|
}
|
|
@@ -65,14 +65,13 @@ export class ProjectMembershipResolver {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
const explicitProjectRoles = explicitMemberships.map(it => it.role)
|
|
68
|
-
const
|
|
68
|
+
const implicitMemberships = implicitRoles
|
|
69
|
+
.filter(it => !explicitProjectRoles.includes(it))
|
|
70
|
+
.map((role): Acl.Membership => ({ role, variables: [] }))
|
|
69
71
|
|
|
70
72
|
return {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
...membershipResolver.resolve(acl, explicitMemberships, identity, false).memberships,
|
|
74
|
-
...implicitRolesToAssign.map(it => ({ role: it, variables: [] })),
|
|
75
|
-
],
|
|
73
|
+
// intentionally ignoring validation errors of stored memberships
|
|
74
|
+
effective: membershipResolver.resolve(acl, [...explicitMemberships, ...implicitMemberships], identity, false).memberships,
|
|
76
75
|
fetched: explicitMemberships,
|
|
77
76
|
}
|
|
78
77
|
}
|
|
@@ -14,13 +14,14 @@ export type ProjectContainerResolverEvents = {
|
|
|
14
14
|
export class ProjectContainerResolver implements EventEmitter<ProjectContainerResolverEvents> {
|
|
15
15
|
private projectContainers = new ProjectContainerStore()
|
|
16
16
|
private eventManager = new EventManager<ProjectContainerResolverEvents>()
|
|
17
|
+
private disposed = false
|
|
17
18
|
|
|
18
19
|
public readonly on = this.eventManager.on.bind(this.eventManager)
|
|
19
20
|
|
|
20
21
|
constructor(
|
|
21
22
|
private readonly projectContainerFactory: ProjectContainerFactory,
|
|
22
23
|
private readonly projectConfigResolver: ProjectConfigResolver,
|
|
23
|
-
private readonly projectManager: ProjectManager,
|
|
24
|
+
private readonly projectManager: Pick<ProjectManager, 'getProjectState' | 'getProjectWithSecretsBySlug'>,
|
|
24
25
|
private readonly tenantDatabase: DatabaseContext,
|
|
25
26
|
private readonly tenantConfig: TenantConfig,
|
|
26
27
|
) {}
|
|
@@ -30,7 +31,7 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
30
31
|
if (realSlug) {
|
|
31
32
|
slug = realSlug
|
|
32
33
|
}
|
|
33
|
-
const existing = this.projectContainers.getContainer(slug)
|
|
34
|
+
const existing = this.disposed ? undefined : this.projectContainers.getContainer(slug)
|
|
34
35
|
if (existing) {
|
|
35
36
|
const existingAwaited = await existing
|
|
36
37
|
const state = await this.projectManager.getProjectState(this.tenantDatabase, slug, existingAwaited.timestamp)
|
|
@@ -47,19 +48,19 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
47
48
|
return undefined
|
|
48
49
|
}
|
|
49
50
|
const container = await this.createProjectContainer(project)
|
|
50
|
-
if (slug !== project.slug) {
|
|
51
|
+
if (slug !== project.slug && !this.disposed) {
|
|
51
52
|
this.projectContainers.setAlias(project.slug, slug)
|
|
52
53
|
}
|
|
53
54
|
return container
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
public async createProjectContainer(project: ProjectWithSecrets, logger?: Logger): Promise<ProjectContainer> {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
await
|
|
58
|
+
if (this.disposed) {
|
|
59
|
+
// A request that still holds the disposed group gets a container nobody tracks; its pool closes on idle timeout.
|
|
60
|
+
return await this.initializeContainer(project, logger)
|
|
61
|
+
}
|
|
62
|
+
return (await this.projectContainers.fetchContainer(project.slug, async () => {
|
|
63
|
+
const projectContainer = await this.initializeContainer(project, logger)
|
|
63
64
|
|
|
64
65
|
const cleanups = this.eventManager.fire('create', { container: projectContainer }).map(it => it ?? (() => null))
|
|
65
66
|
return {
|
|
@@ -70,6 +71,23 @@ export class ProjectContainerResolver implements EventEmitter<ProjectContainerRe
|
|
|
70
71
|
})).container
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Destroys all cached containers. Containers requested afterwards are neither cached nor announced to 'create' listeners.
|
|
76
|
+
*/
|
|
77
|
+
public async dispose(): Promise<void> {
|
|
78
|
+
this.disposed = true
|
|
79
|
+
await Promise.allSettled(this.projectContainers.getSlugs().map(slug => this.destroyContainer(slug)))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private async initializeContainer(project: ProjectWithSecrets, logger?: Logger): Promise<ProjectContainer> {
|
|
83
|
+
const projectConfig = this.projectConfigResolver(project.slug, project.config, project.secrets, this.tenantConfig)
|
|
84
|
+
const projectContainer = this.projectContainerFactory.createContainer({
|
|
85
|
+
project: projectConfig,
|
|
86
|
+
})
|
|
87
|
+
await projectContainer.projectInitializer.initialize(logger ?? projectContainer.logger)
|
|
88
|
+
return projectContainer
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
public async destroyContainer(slug: string): Promise<void> {
|
|
74
92
|
const existing = this.projectContainers.getContainer(slug)
|
|
75
93
|
if (existing) {
|
|
@@ -27,6 +27,10 @@ export class ProjectContainerStore {
|
|
|
27
27
|
return await this.containers.fetch(slug, factory)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
public getSlugs(): string[] {
|
|
31
|
+
return Array.from(this.containers.keys())
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
public removeContainer(slug: string): void {
|
|
31
35
|
this.containers.delete(slug)
|
|
32
36
|
}
|
|
@@ -39,10 +39,7 @@ export class ProjectGroupContainerResolver implements EventEmitter<ProjectGroupC
|
|
|
39
39
|
slug,
|
|
40
40
|
})
|
|
41
41
|
const cleanups = this.eventManager.fire('create', { container, slug }).map(it => it ?? (() => null))
|
|
42
|
-
|
|
43
|
-
container.projectContainerResolver.on('create', ({ container: projectContainer }) => {
|
|
44
|
-
cleanups.push(() => container.projectContainerResolver.destroyContainer(projectContainer.project.slug))
|
|
45
|
-
})
|
|
42
|
+
cleanups.push(() => container.projectContainerResolver.dispose())
|
|
46
43
|
|
|
47
44
|
await container.tenantContainer.migrationsRunner.run(container.logger.child())
|
|
48
45
|
return { container, inputConfig: config, cleanups }
|
|
@@ -92,7 +92,7 @@ const createDbPoolMetricsCollector = (registry: prom.Registry) => {
|
|
|
92
92
|
const labels = entry.labels
|
|
93
93
|
const poolInfo = db.getPoolStatus()
|
|
94
94
|
if (!poolInfo) {
|
|
95
|
-
|
|
95
|
+
continue
|
|
96
96
|
}
|
|
97
97
|
totalCount.set(labels, poolInfo.active + poolInfo.idle + poolInfo.connecting)
|
|
98
98
|
idleCount.set(labels, poolInfo.idle)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MigrationLockOptions } from '@contember/engine-system-api'
|
|
2
|
+
import { ServerConfig } from '../config/config.js'
|
|
3
|
+
|
|
4
|
+
export const createMigrationLockOptions = (config: ServerConfig['systemApi']): MigrationLockOptions | undefined => {
|
|
5
|
+
if (config?.migrationLockTimeoutMs === undefined) {
|
|
6
|
+
return undefined
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
lockTimeoutMs: config.migrationLockTimeoutMs,
|
|
10
|
+
maxAttempts: config.migrationMaxAttempts ?? 5,
|
|
11
|
+
retryDelayMs: config.migrationRetryDelayMs ?? 1000,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { Connection, EventManager } from '@contember/database'
|
|
3
|
+
import { Registry } from 'prom-client'
|
|
4
|
+
import { createMock } from '../../utils.js'
|
|
5
|
+
import { createDbMetricsRegistrar } from '../../../src/prometheus/dbMetrics.js'
|
|
6
|
+
|
|
7
|
+
const labels = (project: string) => ({
|
|
8
|
+
contember_module: 'content',
|
|
9
|
+
contember_project: project,
|
|
10
|
+
contember_project_group: 'group',
|
|
11
|
+
database_instance: 'single',
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('a pool without status does not hide metrics of the other pools', async () => {
|
|
15
|
+
const registry = new Registry()
|
|
16
|
+
const register = createDbMetricsRegistrar(registry)
|
|
17
|
+
register({
|
|
18
|
+
connection: createMock<Connection.PoolStatusProvider & Connection.Queryable>({
|
|
19
|
+
getPoolStatus: () => undefined,
|
|
20
|
+
eventManager: new EventManager(null),
|
|
21
|
+
query: () => {
|
|
22
|
+
throw new Error('Unused test dependency')
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
labels: labels('without-status'),
|
|
26
|
+
})
|
|
27
|
+
register({
|
|
28
|
+
connection: Connection.create(
|
|
29
|
+
{ host: 'localhost', port: 5432, user: 'test', password: 'test', database: 'test', pool: { maxConnections: 7 } },
|
|
30
|
+
() => {},
|
|
31
|
+
),
|
|
32
|
+
labels: labels('with-status'),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const maxCount = (await registry.metrics()).split('\n').filter(it => it.startsWith('contember_db_pool_max_count{'))
|
|
36
|
+
|
|
37
|
+
expect(maxCount).toEqual([
|
|
38
|
+
'contember_db_pool_max_count{contember_module="content",contember_project="with-status",contember_project_group="group",database_instance="single"} 7',
|
|
39
|
+
])
|
|
40
|
+
})
|
|
@@ -85,3 +85,46 @@ test('a contributor cache key separates role combinations with identical permiss
|
|
|
85
85
|
|
|
86
86
|
expect(create(factory, ['editor'])).not.toBe(create(factory, ['admin']))
|
|
87
87
|
})
|
|
88
|
+
|
|
89
|
+
const stats: Model.Entity = {
|
|
90
|
+
name: 'Stats',
|
|
91
|
+
primary: 'id',
|
|
92
|
+
primaryColumn: 'id',
|
|
93
|
+
tableName: 'stats',
|
|
94
|
+
unique: [],
|
|
95
|
+
indexes: [],
|
|
96
|
+
eventLog: { enabled: false },
|
|
97
|
+
view: { sql: 'SELECT 1', materialized: true },
|
|
98
|
+
fields: {
|
|
99
|
+
id: { name: 'id', columnName: 'id', type: Model.ColumnType.Uuid, columnType: 'uuid', nullable: false },
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const readStats: Acl.Permissions = { Stats: { predicates: {}, operations: { read: { id: true } } } }
|
|
104
|
+
|
|
105
|
+
const materializedViewSchema: Schema = {
|
|
106
|
+
...emptySchema,
|
|
107
|
+
model: { entities: { Stats: stats }, enums: {} },
|
|
108
|
+
acl: {
|
|
109
|
+
roles: {
|
|
110
|
+
reader: role(readStats),
|
|
111
|
+
refresher: { ...role(readStats), content: { refreshMaterializedView: true } },
|
|
112
|
+
inheritedRefresher: { ...role(readStats), inherits: ['refresher'] },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const hasRefreshMutation = (factory: GraphQlSchemaFactory, projectRoles: string[]) =>
|
|
118
|
+
factory.create(materializedViewSchema, { projectRoles }, project).schema.getMutationType()?.getFields().refreshMaterializedView !== undefined
|
|
119
|
+
|
|
120
|
+
test('a role without the refresh permission cannot refresh a materialized view another role may refresh', () => {
|
|
121
|
+
const factory = createFactory()
|
|
122
|
+
|
|
123
|
+
expect(hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
124
|
+
expect(hasRefreshMutation(factory, ['refresher'])).toBe(true)
|
|
125
|
+
expect(hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('the refresh permission is inherited', () => {
|
|
129
|
+
expect(hasRefreshMutation(createFactory(), ['inheritedRefresher'])).toBe(true)
|
|
130
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Connection } from '@contember/database'
|
|
3
|
+
import { DatabaseContextFactory, ProjectInitializer } from '@contember/engine-system-api'
|
|
4
|
+
import { DatabaseContext, ProjectWithSecrets } from '@contember/engine-tenant-api'
|
|
5
|
+
import { createLogger, TestLoggerHandler } from '@contember/logger'
|
|
6
|
+
import { createMock } from '../../utils.js'
|
|
7
|
+
import { ContentSchemaResolver } from '../../../src/content/ContentSchemaResolver.js'
|
|
8
|
+
import { ProjectContainer, ProjectContainerFactory } from '../../../src/project/ProjectContainer.js'
|
|
9
|
+
import { ProjectContainerResolver } from '../../../src/project/ProjectContainerResolver.js'
|
|
10
|
+
import { ProjectDatabaseMetadataResolver } from '../../../src/project/ProjectDatabaseMetadataResolver.js'
|
|
11
|
+
import { ProjectConfig } from '../../../src/project/config.js'
|
|
12
|
+
import { tenantConfigSchema } from '../../../src/config/configSchema.js'
|
|
13
|
+
import { createProviders } from '../../../src/providers.js'
|
|
14
|
+
|
|
15
|
+
const unavailable = (): never => {
|
|
16
|
+
throw new Error('Unused test dependency')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const createHarness = () => {
|
|
20
|
+
const logger = createLogger(new TestLoggerHandler())
|
|
21
|
+
const providers = createProviders()
|
|
22
|
+
const db = { host: 'localhost', port: 5432, user: 'test', password: 'test', database: 'test' }
|
|
23
|
+
const connection = Connection.create(db, () => {})
|
|
24
|
+
const systemDatabaseContextFactory = new DatabaseContextFactory('system', providers)
|
|
25
|
+
const project: ProjectWithSecrets = {
|
|
26
|
+
id: 'b1f4c3e2-7d6a-4b5c-9e8f-0a1b2c3d4e5f',
|
|
27
|
+
slug: 'blog',
|
|
28
|
+
name: 'Blog',
|
|
29
|
+
config: {},
|
|
30
|
+
secrets: {},
|
|
31
|
+
updatedAt: new Date(),
|
|
32
|
+
}
|
|
33
|
+
const projectConfig: ProjectConfig = {
|
|
34
|
+
slug: project.slug,
|
|
35
|
+
name: project.name,
|
|
36
|
+
stages: [],
|
|
37
|
+
db,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let initialize = async () => {}
|
|
41
|
+
const factory = createMock<ProjectContainerFactory>({
|
|
42
|
+
createContainer: (): ProjectContainer => ({
|
|
43
|
+
project: projectConfig,
|
|
44
|
+
logger,
|
|
45
|
+
connection,
|
|
46
|
+
readConnection: connection,
|
|
47
|
+
systemDatabaseContextFactory,
|
|
48
|
+
systemDatabaseContext: systemDatabaseContextFactory.create(connection),
|
|
49
|
+
systemReadDatabaseContext: systemDatabaseContextFactory.create(connection),
|
|
50
|
+
contentSchemaResolver: createMock<ContentSchemaResolver>({ clearCache: unavailable, getSchema: unavailable }),
|
|
51
|
+
projectDatabaseMetadataResolver: createMock<ProjectDatabaseMetadataResolver>({ resolveDatabaseMetadata: unavailable }),
|
|
52
|
+
projectInitializer: createMock<ProjectInitializer>({ initialize: () => initialize() }),
|
|
53
|
+
}),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const resolver = new ProjectContainerResolver(
|
|
57
|
+
factory,
|
|
58
|
+
() => projectConfig,
|
|
59
|
+
{
|
|
60
|
+
getProjectState: async () => 'valid',
|
|
61
|
+
getProjectWithSecretsBySlug: async () => project,
|
|
62
|
+
},
|
|
63
|
+
new DatabaseContext(connection.createClient('tenant', {}), { ...providers, encrypt: unavailable, decrypt: unavailable, encryptionEnabled: false }),
|
|
64
|
+
tenantConfigSchema({ db, mailer: {}, credentials: {}, secrets: {} }),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
// Mimics a per-container registration such as the pool metrics: registered on 'create', released by its cleanup.
|
|
68
|
+
const registered = new Set<ProjectContainer>()
|
|
69
|
+
resolver.on('create', ({ container }) => {
|
|
70
|
+
registered.add(container)
|
|
71
|
+
return () => registered.delete(container)
|
|
72
|
+
})
|
|
73
|
+
const destroyed: ProjectContainer[] = []
|
|
74
|
+
resolver.on('destroy', ({ container }) => {
|
|
75
|
+
destroyed.push(container)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
project,
|
|
80
|
+
resolver,
|
|
81
|
+
registered,
|
|
82
|
+
destroyed,
|
|
83
|
+
setInitialize: (fn: () => Promise<void>) => {
|
|
84
|
+
initialize = fn
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
describe('project container resolver disposal', () => {
|
|
90
|
+
test('dispose destroys cached containers', async () => {
|
|
91
|
+
const { resolver, registered, destroyed } = createHarness()
|
|
92
|
+
const container = await resolver.getProjectContainer('blog')
|
|
93
|
+
expect(registered.size).toBe(1)
|
|
94
|
+
|
|
95
|
+
await resolver.dispose()
|
|
96
|
+
|
|
97
|
+
expect(registered.size).toBe(0)
|
|
98
|
+
expect(destroyed).toHaveLength(1)
|
|
99
|
+
expect(container).toBe(destroyed[0])
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('a request still holding a disposed resolver gets an untracked container', async () => {
|
|
103
|
+
const { resolver, registered } = createHarness()
|
|
104
|
+
const before = await resolver.getProjectContainer('blog')
|
|
105
|
+
await resolver.dispose()
|
|
106
|
+
|
|
107
|
+
const late = await resolver.getProjectContainer('blog')
|
|
108
|
+
const lateAgain = await resolver.getProjectContainer('blog')
|
|
109
|
+
|
|
110
|
+
expect(late).toBeDefined()
|
|
111
|
+
expect(late).not.toBe(before)
|
|
112
|
+
expect(lateAgain).not.toBe(late)
|
|
113
|
+
expect(registered.size).toBe(0)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('a container still initializing when the resolver is disposed is released once it is ready', async () => {
|
|
117
|
+
const { resolver, registered, destroyed, setInitialize } = createHarness()
|
|
118
|
+
let finishInitialization = () => {}
|
|
119
|
+
const initializationStarted = new Promise<void>(started => {
|
|
120
|
+
setInitialize(() =>
|
|
121
|
+
new Promise<void>(resolve => {
|
|
122
|
+
finishInitialization = resolve
|
|
123
|
+
started()
|
|
124
|
+
})
|
|
125
|
+
)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const pending = resolver.getProjectContainer('blog')
|
|
129
|
+
await initializationStarted
|
|
130
|
+
const disposed = resolver.dispose()
|
|
131
|
+
finishInitialization()
|
|
132
|
+
|
|
133
|
+
const container = await pending
|
|
134
|
+
await disposed
|
|
135
|
+
|
|
136
|
+
expect(container).toBeDefined()
|
|
137
|
+
expect(registered.size).toBe(0)
|
|
138
|
+
expect(destroyed).toHaveLength(1)
|
|
139
|
+
expect(container).toBe(destroyed[0])
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('createProjectContainer after dispose does not register the container', async () => {
|
|
143
|
+
const { project, resolver, registered } = createHarness()
|
|
144
|
+
await resolver.dispose()
|
|
145
|
+
|
|
146
|
+
await resolver.createProjectContainer(project)
|
|
147
|
+
|
|
148
|
+
expect(registered.size).toBe(0)
|
|
149
|
+
})
|
|
150
|
+
})
|
|
@@ -36,6 +36,50 @@ describe('membership resolver', () => {
|
|
|
36
36
|
expect(resolvedMembership.effective).toStrictEqual([{ role: 'test', variables: [] }])
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
+
it('should resolve the variables of an implicit role', async () => {
|
|
40
|
+
const membershipResolver = new ProjectMembershipResolver(
|
|
41
|
+
false,
|
|
42
|
+
createMock<ProjectMembershipFetcher>({
|
|
43
|
+
fetchMemberships(): Promise<readonly Acl.Membership[]> {
|
|
44
|
+
return Promise.resolve([])
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
)
|
|
48
|
+
const resolvedMembership = await membershipResolver.resolveMemberships({
|
|
49
|
+
request: {
|
|
50
|
+
body: {},
|
|
51
|
+
get: () => '',
|
|
52
|
+
},
|
|
53
|
+
projectSlug: 'test',
|
|
54
|
+
identity: {
|
|
55
|
+
identityId: 'd4141336-6512-41ef-a25a-374de35a2806',
|
|
56
|
+
personId: '4f1ac9f9-b647-4524-87d7-82c9a820fd87',
|
|
57
|
+
roles: [],
|
|
58
|
+
},
|
|
59
|
+
acl: {
|
|
60
|
+
roles: {
|
|
61
|
+
test: {
|
|
62
|
+
implicit: true,
|
|
63
|
+
variables: {
|
|
64
|
+
identity: { type: Acl.VariableType.predefined, value: 'identityID' },
|
|
65
|
+
person: { type: Acl.VariableType.predefined, value: 'personID' },
|
|
66
|
+
category: { type: Acl.VariableType.entity, entityName: 'Category', fallback: { eq: 'none' } },
|
|
67
|
+
},
|
|
68
|
+
entities: {},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
expect(resolvedMembership.effective).toStrictEqual([{
|
|
74
|
+
role: 'test',
|
|
75
|
+
variables: [
|
|
76
|
+
{ name: 'identity', condition: { in: ['d4141336-6512-41ef-a25a-374de35a2806'] } },
|
|
77
|
+
{ name: 'person', condition: { in: ['4f1ac9f9-b647-4524-87d7-82c9a820fd87'] } },
|
|
78
|
+
{ name: 'category', condition: { eq: 'none' } },
|
|
79
|
+
],
|
|
80
|
+
}])
|
|
81
|
+
})
|
|
82
|
+
|
|
39
83
|
it('should return fetched memberships', async () => {
|
|
40
84
|
const membershipResolver = new ProjectMembershipResolver(
|
|
41
85
|
false,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { expect, test } from 'bun:test'
|
|
2
2
|
import { readConfig } from '../../../src/config/config.js'
|
|
3
3
|
import { serverConfigSchema } from '../../../src/config/configSchema.js'
|
|
4
|
+
import { createMigrationLockOptions } from '../../../src/system/migrationLockOptions.js'
|
|
4
5
|
|
|
5
6
|
const trustedProxies = (val: unknown): unknown => serverConfigSchema({ http: { trustedProxies: val } }).http?.trustedProxies
|
|
6
7
|
|
|
@@ -103,3 +104,39 @@ test('schemaCacheMaxEntries: undefined when the variable is unset', async () =>
|
|
|
103
104
|
test('schemaCacheMaxEntries: zero is refused', () => {
|
|
104
105
|
expect(() => serverConfigSchema({ contentApi: { schemaCacheMaxEntries: 0 } })).toThrow()
|
|
105
106
|
})
|
|
107
|
+
|
|
108
|
+
test('migration lock options: read from CONTEMBER_SYSTEM_API_MIGRATION_* with defaults for the retry', async () => {
|
|
109
|
+
process.env.CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS = '2000'
|
|
110
|
+
try {
|
|
111
|
+
const { serverConfig } = await readConfig()
|
|
112
|
+
expect(createMigrationLockOptions(serverConfig.systemApi)).toStrictEqual({ lockTimeoutMs: 2000, maxAttempts: 5, retryDelayMs: 1000 })
|
|
113
|
+
} finally {
|
|
114
|
+
delete process.env.CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('migration lock options: unset when no variable is set', async () => {
|
|
119
|
+
const { serverConfig } = await readConfig()
|
|
120
|
+
expect(createMigrationLockOptions(serverConfig.systemApi)).toBeUndefined()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('migration lock options: retry settings without a lock timeout are refused', async () => {
|
|
124
|
+
process.env.CONTEMBER_SYSTEM_API_MIGRATION_MAX_ATTEMPTS = '3'
|
|
125
|
+
try {
|
|
126
|
+
await expect(readConfig()).rejects.toThrow('require migrationLockTimeoutMs')
|
|
127
|
+
} finally {
|
|
128
|
+
delete process.env.CONTEMBER_SYSTEM_API_MIGRATION_MAX_ATTEMPTS
|
|
129
|
+
}
|
|
130
|
+
expect(() => serverConfigSchema({ systemApi: { migrationRetryDelayMs: 0 } })).toThrow('require migrationLockTimeoutMs')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('migration lock options: retry settings with a lock timeout are accepted', () => {
|
|
134
|
+
const config = serverConfigSchema({ systemApi: { migrationLockTimeoutMs: 500, migrationMaxAttempts: 3, migrationRetryDelayMs: 0 } })
|
|
135
|
+
expect(createMigrationLockOptions(config.systemApi)).toStrictEqual({ lockTimeoutMs: 500, maxAttempts: 3, retryDelayMs: 0 })
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('migration lock options: zero and negative values are rejected', () => {
|
|
139
|
+
expect(() => serverConfigSchema({ systemApi: { migrationLockTimeoutMs: 0 } })).toThrow('must be a positive integer')
|
|
140
|
+
expect(() => serverConfigSchema({ systemApi: { migrationLockTimeoutMs: 500, migrationMaxAttempts: -1 } })).toThrow('must be a positive integer')
|
|
141
|
+
expect(() => serverConfigSchema({ systemApi: { migrationLockTimeoutMs: 500, migrationRetryDelayMs: -1 } })).toThrow('must be a non-negative integer')
|
|
142
|
+
})
|