UNPKG

@fntio/infra-client

Version:

Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.

122 lines (121 loc) • 5.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.resolvePlugins = resolvePlugins; exports.resolvePlugin = resolvePlugin; exports.hashDirectory = hashDirectory; exports.pluginRevision = pluginRevision; const crypto = __importStar(require("node:crypto")); const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); const cdk = __importStar(require("aws-cdk-lib")); const infra_client_plugin_sdk_1 = require("@fntio/infra-client-plugin-sdk"); /** Synth-time validation of the feature block + registrations; undefined when the feature is off. * Registrations without the feature are an error, never a silent no-op. */ function resolvePlugins(config, registrations = []) { if (!config?.enabled) { if (registrations.length) { throw new Error(`plugins: ${registrations.length} plugin registration(s) supplied but project.plugins.enabled is not true — ` + "enable the feature or drop the registrations."); } return undefined; } const refresh = config?.discoveryRefreshSeconds ?? infra_client_plugin_sdk_1.PLUGIN_DISCOVERY_DEFAULT_REFRESH_SECONDS; if (!Number.isInteger(refresh) || refresh < infra_client_plugin_sdk_1.PLUGIN_DISCOVERY_MIN_REFRESH_SECONDS || refresh > infra_client_plugin_sdk_1.PLUGIN_DISCOVERY_MAX_REFRESH_SECONDS) { throw new Error(`plugins.discoveryRefreshSeconds must be an integer between ${infra_client_plugin_sdk_1.PLUGIN_DISCOVERY_MIN_REFRESH_SECONDS} and ${infra_client_plugin_sdk_1.PLUGIN_DISCOVERY_MAX_REFRESH_SECONDS} (got ${String(refresh)}).`); } const seen = new Set(); const plugins = registrations.map((r) => { const resolved = resolvePlugin(r); if (seen.has(resolved.manifest.id)) throw new Error(`plugins: plugin id "${resolved.manifest.id}" is registered twice.`); seen.add(resolved.manifest.id); return resolved; }); return { discoveryRefreshSeconds: refresh, plugins }; } function resolvePlugin(registration) { const result = (0, infra_client_plugin_sdk_1.validatePluginManifest)(registration.manifest, { handlerKeys: Object.keys(registration.handlers) }); if (!result.ok) throw new infra_client_plugin_sdk_1.PluginValidationError(result.issues, `Plugin "${String(registration.manifest?.id ?? "?")}"`); const manifest = result.manifest; let assetHash; let assetPrefix; let ui; if (manifest.ui) { const dir = registration.assetDirectory; if (!dir || !fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) { throw new Error(`Plugin "${manifest.id}": UI entries need an asset directory (got ${dir ?? "none"}).`); } for (const kind of ["user", "admin"]) { const entry = manifest.ui[kind]; if (entry && !fs.existsSync(path.join(dir, (0, infra_client_plugin_sdk_1.pluginEntryFile)(entry.entry)))) { throw new Error(`Plugin "${manifest.id}": ui.${kind}.entry "${entry.entry}" not found under ${dir}.`); } } assetHash = hashDirectory(dir); assetPrefix = (0, infra_client_plugin_sdk_1.pluginAssetPrefix)(manifest.id, manifest.version, assetHash); const resolve = (entry) => entry && { label: entry.label, ...(entry.icon ? { icon: entry.icon } : {}), ...(entry.order !== undefined ? { order: entry.order } : {}), url: (0, infra_client_plugin_sdk_1.pluginUiUrl)(assetPrefix, entry.entry), }; ui = { ...(manifest.ui.user ? { user: resolve(manifest.ui.user) } : {}), ...(manifest.ui.admin ? { admin: resolve(manifest.ui.admin) } : {}), }; } const routes = manifest.routes.map((route) => ({ ...route, resourcePath: (0, infra_client_plugin_sdk_1.pluginRouteResourcePath)(manifest.id, route.path) })); return { manifest, assetDirectory: registration.assetDirectory, assetHash, assetPrefix, handlers: registration.handlers, ui, routes, revision: pluginRevision(manifest, assetHash), }; } /** Deterministic content hash of a directory (CDK's own asset fingerprint), 16 hex chars. */ function hashDirectory(dir) { return cdk.FileSystem.fingerprint(dir).slice(0, 16); } function pluginRevision(manifest, assetHash) { return crypto.createHash("sha256").update((0, infra_client_plugin_sdk_1.canonicalJson)({ manifest, assetHash: assetHash ?? null })).digest("hex").slice(0, 16); }