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.

89 lines (88 loc) • 3.98 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.SgManager = void 0; const cdk = __importStar(require("aws-cdk-lib")); const constructs_1 = require("constructs"); const iam = __importStar(require("aws-cdk-lib/aws-iam")); const app_function_1 = require("./app-function"); const naming_1 = require("../../lambdacode/network/naming"); /** Lives in the target account so the reconciler never holds EC2 permissions * anywhere — it may only ask this fn, which only opens this VPC's groups. */ class SgManager extends constructs_1.Construct { fn; constructor(scope, id, props) { super(scope, id); const stack = cdk.Stack.of(this); this.fn = new app_function_1.AppFunction(this, "Fn", { functionName: (0, naming_1.sgManagerFnName)(props.resourcePrefix, props.name), description: "Opens and closes security-group rules so the VPN connector reaches granted resources in this VPC.", asset: "sg-manager", handler: "handler", timeout: cdk.Duration.seconds(30), environment: { CONNECTOR_SG_ID: props.connectorSecurityGroup.securityGroupId, RESOURCE_PREFIX: props.resourcePrefix, // managed-rule marker derivation VPC_ID: props.vpc.vpcId, // groups elsewhere are answered `other-vpc`, not touched }, }); // Describe has no resource-level scoping; the mutating pair is pinned to // the connector's VPC so the blast radius is "this VPC", not "the account". this.fn.addToRolePolicy(new iam.PolicyStatement({ actions: ["ec2:DescribeSecurityGroups"], resources: ["*"], })); this.fn.addToRolePolicy(new iam.PolicyStatement({ actions: [ "ec2:AuthorizeSecurityGroupIngress", "ec2:RevokeSecurityGroupIngress", ], resources: [ `arn:aws:ec2:${stack.region}:${stack.account}:security-group/*`, ], conditions: { StringEquals: { "ec2:Vpc": props.vpc.vpcArn } }, })); if (props.trustedInvoker) { // Lambda validates the principal exists — NetworkStack deploys first, // and redeploy here if the reconciler role is ever recreated. this.fn.addPermission("ReconcilerInvoke", { principal: props.trustedInvoker, action: "lambda:InvokeFunction", }); } new cdk.CfnOutput(this, "SgManagerArn", { value: this.fn.functionArn }); } } exports.SgManager = SgManager;