feat: integrate module authorization
Wires composePlatformPermissionRegistry into apps/api's production startup: startApi() discovers every installed module, composes the one immutable platform permission registry (Core's own declarations plus every module's), and only then constructs the shared PostgreSQL pool, Better Auth, and the HTTP server. A discovery or composition failure — an invalid manifest, a reserved core.* namespace claim, or a duplicate declaration — propagates before anything exists to close; there is no Core-only registry fallback. apps/api/src/platform-permissions.ts resolves the repository root from its own file location, never process.cwd(), so package-scoped test runs discover the right modules/ directory. createApiApp/createGrantsRoutes now require the composed registry as an injected authorization option, replacing the previous hard-coded CORE_PERMISSION_REGISTRY; requirePermission continues to work identically for Core-owned and module-owned permissions, since it has no concept of the distinction. Extends pnpm doctor with a modules.permissions check (reusing the already-discovered module list, distinguishing reserved-namespace from generic-duplicate failures, never re-discovering modules) and pnpm modules:validate to compose the same registry before a module is considered installable. Adds focused coverage: startup-ordering and failure-isolation tests in apps/api/tests/lifecycle.test.ts, module-owned-declaration enforcement tests in apps/api/tests/authorization-middleware.test.ts, Doctor composition tests in core/tests/doctor.test.ts, and a live PostgreSQL end-to-end scenario proving a real installed module's permission is discoverable, grantable, and deniable once the module is absent again after a restart, without ever executing its server entrypoint.
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import path from "node:path";
|
||||
import { isVersionCompatible, moduleIdSchema, semanticVersionSchema } from "@ekklesia/contracts";
|
||||
import {
|
||||
isVersionCompatible,
|
||||
moduleIdSchema,
|
||||
permissionIdSchema,
|
||||
semanticVersionSchema,
|
||||
} from "@ekklesia/contracts";
|
||||
import { getTableName } from "drizzle-orm";
|
||||
import { type PermissionRegistry, PermissionRegistryError } from "../authorization/index.js";
|
||||
import {
|
||||
type CreateIdentityConfigOptions,
|
||||
createIdentityConfig,
|
||||
@@ -18,9 +24,11 @@ import {
|
||||
planModuleMigrations,
|
||||
} from "../migrations/index.js";
|
||||
import {
|
||||
composePlatformPermissionRegistry,
|
||||
type DiscoveredModule,
|
||||
discoverModuleSchemas,
|
||||
discoverModules,
|
||||
ModulePermissionCompositionError,
|
||||
type ModuleDiscoveryOptions,
|
||||
type ModuleMigrationAggregate,
|
||||
type ModuleSchemaAggregate,
|
||||
@@ -30,6 +38,7 @@ export type DoctorCheckId =
|
||||
| "platform.runtime"
|
||||
| "modules.discovery"
|
||||
| "modules.dependencies"
|
||||
| "modules.permissions"
|
||||
| "schemas.discovery"
|
||||
| "migrations.discovery"
|
||||
| "postgresql.connection"
|
||||
@@ -68,6 +77,9 @@ export interface DoctorOptions {
|
||||
}
|
||||
|
||||
export interface DoctorDependencies {
|
||||
/** Synchronous, matching the real implementation: pure in-memory composition over an
|
||||
* already-discovered module list, never re-discovering modules itself. */
|
||||
readonly composePermissions: (modules: readonly DiscoveredModule[]) => PermissionRegistry;
|
||||
readonly createMigrationDatabase: (
|
||||
connectionString: string,
|
||||
options: PostgresMigrationDatabaseOptions,
|
||||
@@ -101,6 +113,7 @@ async function discoverAllMigrationsForRuntime(
|
||||
}
|
||||
|
||||
const productionDependencies: DoctorDependencies = Object.freeze({
|
||||
composePermissions: composePlatformPermissionRegistry,
|
||||
createMigrationDatabase: createPostgresMigrationDatabase,
|
||||
discoverMigrations: discoverAllMigrationsForRuntime,
|
||||
discoverModules,
|
||||
@@ -163,6 +176,57 @@ function safeMigrationReference(identity: string, checksum: string): string {
|
||||
return `${identity} · ${checksum.slice(0, 12)}`;
|
||||
}
|
||||
|
||||
function safeModuleId(value: string): string {
|
||||
if (!moduleIdSchema.safeParse(value).success) {
|
||||
throw new TypeError("Unsafe module diagnostic value.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function safePermissionId(value: string): string {
|
||||
if (!permissionIdSchema.safeParse(value).success) {
|
||||
throw new TypeError("Unsafe permission diagnostic value.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Distinguishes the two composition-error families rather than describing a reserved `core.*`
|
||||
* declaration as a generic duplicate. Never includes a module directory, manifest path, caught
|
||||
* cause, or stack — only the module id and permission id the structured errors already carry,
|
||||
* each re-validated before being interpolated (defense in depth against a future drift bug,
|
||||
* matching `safeModuleDetails`/`safeMigrationReference` elsewhere in this file); if that
|
||||
* re-validation itself fails, this falls back to a fixed generic detail rather than throwing. */
|
||||
function permissionCompositionFailure(error: unknown): DoctorCheck {
|
||||
if (error instanceof ModulePermissionCompositionError) {
|
||||
try {
|
||||
const moduleId = safeModuleId(error.moduleId);
|
||||
const permissionId = safePermissionId(error.permissionId);
|
||||
return freezeCheck("modules.permissions", "failure", "Permission composition failed", [
|
||||
`Module "${moduleId}" declared permission "${permissionId}" in the reserved core.* namespace.`,
|
||||
]);
|
||||
} catch {
|
||||
return freezeCheck("modules.permissions", "failure", "Permission composition failed", [
|
||||
"A module declared a permission in the reserved core.* namespace.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (error instanceof PermissionRegistryError) {
|
||||
try {
|
||||
const permissionId = safePermissionId(error.permissionId);
|
||||
return freezeCheck("modules.permissions", "failure", "Permission composition failed", [
|
||||
`Permission "${permissionId}" is declared more than once.`,
|
||||
]);
|
||||
} catch {
|
||||
return freezeCheck("modules.permissions", "failure", "Permission composition failed", [
|
||||
"A permission is declared more than once.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
return freezeCheck("modules.permissions", "failure", "Permission composition failed", [
|
||||
"The installed permission declarations could not be composed safely.",
|
||||
]);
|
||||
}
|
||||
|
||||
function moduleDiscoveryFailure(error: unknown): DoctorCheck {
|
||||
const details =
|
||||
error instanceof Error && error.name === "ModuleDiscoveryError"
|
||||
@@ -386,6 +450,33 @@ export async function diagnoseEkklesia(
|
||||
]);
|
||||
}
|
||||
|
||||
// Reuses the already-discovered module list above — never re-discovers modules solely for
|
||||
// permission composition. Independent of the schema/migration checks below: a permission
|
||||
// composition failure here (a reserved-namespace or duplicate declaration) does not skip those,
|
||||
// since neither depends on the permission registry, only on discovery having succeeded.
|
||||
let permissionsCheck: DoctorCheck;
|
||||
if (!discoveredModules) {
|
||||
permissionsCheck = freezeCheck(
|
||||
"modules.permissions",
|
||||
"skipped",
|
||||
"Permission composition skipped",
|
||||
["Complete module discovery is required."],
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
const registry = dependencies.composePermissions(discoveredModules);
|
||||
const count = registry.declarations.length;
|
||||
permissionsCheck = freezeCheck(
|
||||
"modules.permissions",
|
||||
"pass",
|
||||
"Permission registry composed",
|
||||
[`${count} ${plural(count, "permission")} registered`],
|
||||
);
|
||||
} catch (error) {
|
||||
permissionsCheck = permissionCompositionFailure(error);
|
||||
}
|
||||
}
|
||||
|
||||
let schemaCheck: DoctorCheck;
|
||||
let migrationCheck: DoctorCheck;
|
||||
let migrations: ModuleMigrationAggregate | undefined;
|
||||
@@ -466,6 +557,7 @@ export async function diagnoseEkklesia(
|
||||
runtimeCheck,
|
||||
moduleCheck,
|
||||
dependencyCheck,
|
||||
permissionsCheck,
|
||||
schemaCheck,
|
||||
migrationCheck,
|
||||
postgresConnectionCheck,
|
||||
|
||||
Reference in New Issue
Block a user