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.

110 lines (109 loc) • 5.64 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.PluginCatalogActivation = void 0; const cdk = __importStar(require("aws-cdk-lib")); const s3deploy = __importStar(require("aws-cdk-lib/aws-s3-deployment")); const cr = __importStar(require("aws-cdk-lib/custom-resources")); const constructs_1 = require("constructs"); const app_function_1 = require("../constructs/app-function"); const runtime_1 = require("./runtime"); const audit_log_1 = require("../audit-log"); /** Uploads each plugin's UI and writes its catalog record (Custom::PluginCatalogRecord) after the asset upload, * the API stage and the plugin's methods. Delete tombstones the record; the provider raises loss notices. */ class PluginCatalogActivation extends constructs_1.Construct { records = []; constructor(scope, id, props) { super(scope, id); const providerFn = new app_function_1.AppFunction(this, "CatalogFn", { description: "Deploy-time helper: writes each plugin's record into the plugin catalog.", asset: "plugin-catalog", // Delete/Update diff every holder's access before and after (sequential reads); generous on purpose. timeout: cdk.Duration.minutes(5), environment: { ...(0, runtime_1.pluginRuntimeEnv)({ catalogTable: props.pluginCatalogTable, accountsTable: props.accountsTable }), ACCESS_TABLE_NAME: props.accessTable.tableName, REVOCATIONS_TABLE_NAME: props.revocationsTable.tableName, ...(props.auditLog ? (0, audit_log_1.auditLogEnv)(props.auditLog) : {}), NODE_ENV: "production", }, }); if (props.auditLog) (0, audit_log_1.grantAuditLogWrite)(providerFn, props.auditLog); props.pluginCatalogTable.grantReadWriteData(providerFn); props.accountsTable.grantReadData(providerFn); props.accessTable.grantReadData(providerFn); props.revocationsTable.grantReadWriteData(providerFn); const provider = new cr.Provider(this, "CatalogProvider", { onEventHandler: providerFn }); for (const plugin of props.plugins) { const { manifest } = plugin; let assets; if (plugin.assetDirectory && plugin.assetPrefix) { assets = new s3deploy.BucketDeployment(this, `Assets-${manifest.id}`, { sources: [s3deploy.Source.asset(plugin.assetDirectory)], destinationBucket: props.assetsBucket, // The key must equal the URL path: the CloudFront behavior matches /plugin-assets/* and forwards // the whole path to this bucket, so an object stored without that prefix answers 403. destinationKeyPrefix: plugin.assetPrefix.replace(/^\//, ""), // Earlier revisions stay for open sessions and rollback (no GC in v1). prune: false, // The prefix carries the content hash, so a revision's bytes never change under their URL. cacheControl: [s3deploy.CacheControl.maxAge(cdk.Duration.days(365)), s3deploy.CacheControl.immutable()], }); } const record = new cdk.CustomResource(this, `Catalog-${manifest.id}`, { serviceToken: provider.serviceToken, resourceType: "Custom::PluginCatalogRecord", properties: { PluginId: manifest.id, Owner: props.owner, Revision: plugin.revision, Manifest: JSON.stringify(manifest), Ui: JSON.stringify(plugin.ui ?? {}), Routes: JSON.stringify(plugin.routes), AssetPrefix: plugin.assetPrefix ?? "", SchemaVersion: "1", }, }); if (assets) record.node.addDependency(assets); record.node.addDependency(props.apiStage); for (const method of props.methodsByPlugin.get(manifest.id) ?? []) record.node.addDependency(method); this.records.push(record); } } } exports.PluginCatalogActivation = PluginCatalogActivation;