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.

126 lines (125 loc) • 6.99 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.PluginApi = void 0; const cdk = __importStar(require("aws-cdk-lib")); const apigw = __importStar(require("aws-cdk-lib/aws-apigateway")); 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"); /** Plugin routes on the core REST API. Explicit resources under /api stop the root {proxy+} from covering * /api/…, so the core keeps /api, /api/{proxy+}, ANY /api/plugins and /api/plugins/{proxy+} explicitly — created as soon as the * feature is on, even with no plugin, so the routing change is deployed and verified on its own first. * Each plugin's resource sets the shared REQUEST authorizer (result cache 0) as the default method option, * so everything under /api/plugins/<id> is authorized by construction. */ class PluginApi extends constructs_1.Construct { /** Present when at least one plugin has routes (an unattached authorizer fails synth). */ authorizer; authorizerFunction; /** Plugin id → its methods; the catalog activation orders itself after (and, on delete, before) them. */ methodsByPlugin = new Map(); get methods() { return [...this.methodsByPlugin.values()].flat(); } constructor(scope, id, props) { super(scope, id); const core = new apigw.LambdaIntegration(props.coreHandler); const apiRoot = props.api.root.addResource("api"); apiRoot.addMethod("ANY", core); apiRoot.addProxy({ defaultIntegration: core, anyMethod: true }); // Express owns every verb on /api/plugins and every path under it that no registered method claims — an // unknown plugin id, and an unmatched path under a registered one, both fall through to this greedy proxy // and get the app's JSON 404. Verified live: no plugin handler is reachable except through its own // registered method, which is the property that matters. const pluginsRoot = apiRoot.addResource("plugins"); pluginsRoot.addMethod("ANY", core); pluginsRoot.addProxy({ defaultIntegration: core, anyMethod: true }); if (props.plugins.some((p) => p.routes.length > 0)) { const { runtime } = props; this.authorizerFunction = new app_function_1.AppFunction(this, "AuthorizerFn", { description: "Authorizes plugin API calls: checks the portal sign-in token and the caller's plugin permission.", asset: "plugin-authorizer", timeout: cdk.Duration.seconds(10), environment: { NODE_ENV: "production", USER_POOL_ID: runtime.userPoolId, ACCESS_TABLE_NAME: runtime.accessTable.tableName, SERVICE_USERS_TABLE_NAME: runtime.serviceUsersTable.tableName, ...(0, runtime_1.pluginRuntimeEnv)(runtime), ...(props.auditLog ? (0, audit_log_1.auditLogEnv)(props.auditLog) : {}), }, }); runtime.accessTable.grantReadData(this.authorizerFunction); runtime.serviceUsersTable.grantReadData(this.authorizerFunction); (0, runtime_1.grantPluginRuntimeRead)(this.authorizerFunction, runtime); if (props.auditLog) (0, audit_log_1.grantAuditLogWrite)(this.authorizerFunction, props.auditLog); this.authorizer = new apigw.RequestAuthorizer(this, "Authorizer", { handler: this.authorizerFunction, identitySources: [apigw.IdentitySource.header("Authorization")], // Decisions are never cached: a completed revoke must bite on the next request. resultsCacheTtl: cdk.Duration.seconds(0), }); } for (const plugin of props.plugins) { if (!plugin.routes.length) continue; // UI-only: nothing to mount const pluginScope = new constructs_1.Construct(this, `Plugin-${plugin.manifest.id}`); const handlers = new Map(); for (const [key, source] of Object.entries(plugin.handlers)) { handlers.set(key, typeof source === "function" ? source(pluginScope) : source); } const accountRoot = pluginsRoot .addResource(plugin.manifest.id, { defaultMethodOptions: { authorizer: this.authorizer, authorizationType: apigw.AuthorizationType.CUSTOM }, }) .addResource("accounts") .addResource("{accountNumber}"); const methods = plugin.routes.map((route) => { // "/" is the account root itself; resourceForPath creates each segment once. const resource = accountRoot.resourceForPath(route.path.replace(/^\/+/, "")); // The authorizer matches this template against the one the catalog stores. Nothing else ties // the two spellings together, so a drift is caught here rather than as a 403 on every call. if (resource.path !== route.resourcePath) { throw new Error(`plugin ${plugin.manifest.id} route ${route.id} mounts at ${resource.path}, but the catalog records ${route.resourcePath}`); } return resource.addMethod(route.method, new apigw.LambdaIntegration(handlers.get(route.handlerKey))); }); this.methodsByPlugin.set(plugin.manifest.id, methods); } } } exports.PluginApi = PluginApi;