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.

82 lines (81 loc) • 7.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.managedRuleDesc = exports.aclReconcilerFnName = exports.SITE_ID_RE = exports.aclReconcilerRelayRoleName = exports.aclReconcilerRelayFnName = exports.aclReconcilerRoleName = exports.tailnetResolverFnName = exports.connectorKeyMinterFnName = exports.deviceKeyMinterFnName = exports.sgManagerFnName = exports.connectorHostname = exports.parseConnectorVpcs = exports.assertConnectorName = exports.CONNECTOR_NAME_RE = exports.DEFAULT_RESOURCE_PREFIX = exports.nodeAnswersTo = exports.nodeDisplayName = exports.NODE_NAME_RE = exports.NODE_NAME_LABEL = exports.headscaleUserName = void 0; /** Headscale rejects digit-first user names and ~half of Cognito subs (random hex) start with one — the prefix * keeps names letter-first. The keyminter (creates users) and ACL reconciler (matches rules) MUST share this mapping. */ const headscaleUserName = (cognitoSub) => `u-${cognitoSub}`; exports.headscaleUserName = headscaleUserName; /** A node's name on a site is a DNS label. The device routes, the share form (device + hostname label) and the * keyminter MUST share this rule. */ exports.NODE_NAME_LABEL = "[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?"; exports.NODE_NAME_RE = new RegExp(`^${exports.NODE_NAME_LABEL}$`); /** The one name a device is listed under: its DNS name, or the hostname it enrolled with when Headscale has * given it none. Device delete removes exactly the node listed under the name — never a lookalike. */ const nodeDisplayName = (node) => (node.givenName || node.name || "").toLowerCase(); exports.nodeDisplayName = nodeDisplayName; /** A node answers to its DNS name and to the hostname it enrolled with: Headscale suffixes a duplicate's DNS * name, so a machine that re-enrolls under the same name while its old node lingers is found by either. * Device-bound shares (reconciler) use this so the port follows a re-enrolled machine; device delete does * NOT (nodeDisplayName) — removing the stale duplicate must not take the live node with it. */ const nodeAnswersTo = (node, name) => (node.givenName ?? "").toLowerCase() === name || (node.name ?? "").toLowerCase() === name; exports.nodeAnswersTo = nodeAnswersTo; /** SET-ONCE product prefix for every by-name contract (Lambda/role/secret names, the SG rule marker). Config * `resourcePrefix` may override it only BEFORE the first deploy — renaming later breaks every cross-account by-name invocation and orphans managed SG rules until all stacks in all accounts redeploy in lockstep. */ exports.DEFAULT_RESOURCE_PREFIX = "infra-client"; /** A connector VPC's `name` (config `connectorVpcs`): letters and digits, letter-first, at most 10 — it * suffixes the connector hostname AND both per-VPC function names, and the longest of those (a 15-char * prefix, the resolver stem, a 20-char site id, the name) must fit Lambda's 64-char limit. */ exports.CONNECTOR_NAME_RE = /^[a-z][a-z0-9]{0,9}$/; const assertConnectorName = (name, where) => { if (!exports.CONNECTOR_NAME_RE.test(name)) { throw new Error(`${where}: name "${name}" is invalid — letters and digits, letter-first, at most 10 ` + `characters (it suffixes the connector's hostname and its function names).`); } }; exports.assertConnectorName = assertConnectorName; const parseConnectorVpcs = (json) => new Map(Object.entries(JSON.parse(json ?? "{}")).filter(([, v]) => Array.isArray(v))); exports.parseConnectorVpcs = parseConnectorVpcs; /** `connector-<account>[-<name>]` is a CONTRACT: the ACL reconciler maps a node's routes to grants in the * account it names. The name tells one VPC of an account from another; it coexists with headscale's own * `-<random>` dedupe suffix, which the reconciler's parser tolerates either way. */ const connectorHostname = (account, name) => name ? `connector-${account}-${name}` : `connector-${account}`; exports.connectorHostname = connectorHostname; /** Well-known SG-manager function name — one per connector VPC (`-<name>` for a named VPC; an account's * one unnamed VPC keeps the bare name), invoked BY NAME across accounts (as the key minters are). The * reconciler builds `arn:aws:lambda:<region>:<account>:function:<this>` from it. */ const sgManagerFnName = (prefix, name) => name ? `${prefix}-sg-manager-${name}` : `${prefix}-sg-manager`; exports.sgManagerFnName = sgManagerFnName; /** Well-known key-minter names — invoked BY NAME so stacks deploy in any order. SECURITY SPLIT: the device minter * (per-user keys, takes a siteId) is granted only to the vending API role; each site's connector minter (machine keys, ignores input) may be granted cross-account — site selection rides on WHICH minter an account may invoke, never on an argument, so a connector can neither impersonate a user nor enroll into another site. */ const deviceKeyMinterFnName = (prefix) => `${prefix}-tailnet-device-keyminter`; exports.deviceKeyMinterFnName = deviceKeyMinterFnName; const connectorKeyMinterFnName = (prefix, siteId) => `${prefix}-tailnet-connector-keyminter-${siteId}`; exports.connectorKeyMinterFnName = connectorKeyMinterFnName; /** Well-known in-VPC DNS resolver — one per connector VPC, named PER SITE (an account may host connector * VPCs in two sites — colliding CIDRs are the whole reason sites exist — and each VPC has its own private-DNS * view, so a per-account name would answer with the wrong VPC's IPs) and, for a named VPC, per VPC. Invoked * BY NAME across accounts like the SG-manager; private-zone names (Cloud Map, RDS) are invisible outside * their VPC. */ const tailnetResolverFnName = (prefix, siteId, name) => name ? `${prefix}-tailnet-resolver-${siteId}-${name}` : `${prefix}-tailnet-resolver-${siteId}`; exports.tailnetResolverFnName = tailnetResolverFnName; /** Fixed reconciler role name: connector accounts pin their SG-manager's invoke trust to this exact ARN. Lambda * resolves the principal at permission-attach time, so recreating this role means redeploying every ConnectorStack. */ const aclReconcilerRoleName = (prefix) => `${prefix}-acl-reconciler`; exports.aclReconcilerRoleName = aclReconcilerRoleName; /** The reconciler's out-of-VPC relay (function AND its invoke-only role share the name) — the hub's way * into connector accounts in OTHER regions. Those accounts pin their SG-manager/resolver trust to the ROLE, * the way hub-region ones pin it to the reconciler role; same recreate caveat. */ const aclReconcilerRelayFnName = (prefix) => `${prefix}-acl-reconciler-relay`; exports.aclReconcilerRelayFnName = aclReconcilerRelayFnName; const aclReconcilerRelayRoleName = (prefix) => `${prefix}-acl-reconciler-relay`; exports.aclReconcilerRelayRoleName = aclReconcilerRelayRoleName; /** Site ids: DNS label, letter-first, ≤20 chars — also the per-site resolver name's suffix. */ exports.SITE_ID_RE = /^[a-z][a-z0-9-]{0,19}$/; /** Fixed reconciler FUNCTION name: the API invokes it by name for the admin "Sync now" button * (same by-name pattern as the key minters, so CredsStack never references NetworkStack). */ const aclReconcilerFnName = (prefix) => `${prefix}-acl-reconciler`; exports.aclReconcilerFnName = aclReconcilerFnName; /** Ownership marker on every SG rule the manager writes — it only ever * revokes rules carrying it. */ const managedRuleDesc = (prefix) => `${prefix}:managed`; exports.managedRuleDesc = managedRuleDesc;