Multiplayer agent harness for work.
Align AWS app routing validation with Terraform (#1239)
Regan Bell02aed9329b8f
diff --git a/cli/src/aws-routing.ts b/cli/src/aws-routing.tsnew file mode 100644index 000000000..9a6829e5f--- /dev/null+++ b/cli/src/aws-routing.ts@@ -0,0 +1,52 @@+import type { QmConfig } from "./config.ts";++export const validAlbHostname = (value: string): boolean =>+ value.length <= 253 &&+ value.includes(".") &&+ value+ .split(".")+ .every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label));++export function awsPortalAppsDomain(config: QmConfig): string | undefined {+ if (!config.aws?.services.portal) return undefined;+ const domain = config.env.portal?.PORTAL_APPS_DOMAIN?.trim().toLowerCase().replace(/\.$/, "");+ if (!domain) return undefined;+ const coreDomain = (config.env.core?.DEPLOY_APPS_DOMAIN || config.env.core?.AWS_DEPLOY_APPS_DOMAIN)+ ?.trim()+ .toLowerCase()+ .replace(/\.$/, "");+ const company = new URL(config.publicUrl).hostname.toLowerCase();+ if (!validAlbHostname(domain) || domain !== coreDomain || !domain.endsWith(`.${company}`)) {+ throw new Error("portal apps domain must match core and remain beneath the company public hostname");+ }+ return domain;+}++export function awsCoreHostnames(config: QmConfig): string[] {+ const hosts: string[] = [];+ const normalize = (value: string, source: string): string => {+ const host = value.trim().toLowerCase().replace(/\.$/, "");+ if (!validAlbHostname(host)) {+ throw new Error(`${source} ${JSON.stringify(value)} does not derive a valid ALB host-header hostname`);+ }+ return host;+ };+ const api = config.apiUrl?.trim();+ if (api) {+ let hostname: string;+ try {+ hostname = new URL(api).hostname;+ } catch {+ throw new Error(+ `apiUrl ${JSON.stringify(api)} is not a valid URL, so the ALB host rule for the core API cannot be derived`,+ );+ }+ const apiHost = normalize(hostname, "apiUrl");+ if (apiHost !== new URL(config.publicUrl).hostname.toLowerCase().replace(/\.$/, "")) hosts.push(apiHost);+ }+ const apps = config.env.core?.DEPLOY_APPS_DOMAIN?.trim() || config.env.core?.AWS_DEPLOY_APPS_DOMAIN?.trim();+ const portalAppsDomain = awsPortalAppsDomain(config);+ if (apps && (!config.aws?.sharedAlb || apps.trim().toLowerCase().replace(/\.$/, "") !== portalAppsDomain))+ hosts.push(`*.${normalize(apps, "the apps domain (env.core.DEPLOY_APPS_DOMAIN or AWS_DEPLOY_APPS_DOMAIN)")}`);+ return [...new Set(hosts)];+}diff --git a/cli/src/backends/aws.ts b/cli/src/backends/aws.tsindex c3f6e2d0a..372f98ea5 100644--- a/cli/src/backends/aws.ts+++ b/cli/src/backends/aws.ts@@ -1,3 +1,4 @@+import { awsCoreHostnames, awsPortalAppsDomain, validAlbHostname } from "../aws-routing.ts";import https from "node:https";import { createHash, randomUUID } from "node:crypto";import { spawnSync } from "node:child_process";@@ -3237,13 +3238,6 @@ function awsPublicFrontDoor(config: QmConfig): AwsPublicFrontDoor {return { loadBalancerArn: loadBalancer.LoadBalancerArn, dnsName: loadBalancer.DNSName, listener };}-const validAlbHostname = (value: string): boolean =>- value.length <= 253 &&- value.includes(".") &&- value- .split(".")- .every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label));-function assertCloudFrontLayerTarget(config: QmConfig, url: URL, target: string): void {interface Distribution {DomainName?: string;@@ -3295,49 +3289,6 @@ function assertCloudFrontLayerTarget(config: QmConfig, url: URL, target: string)}}-function awsPortalAppsDomain(config: QmConfig): string | undefined {- if (!config.aws?.services.portal) return undefined;- const domain = config.env.portal?.PORTAL_APPS_DOMAIN?.trim().toLowerCase().replace(/\.$/, "");- if (!domain) return undefined;- const coreDomain = (config.env.core?.DEPLOY_APPS_DOMAIN || config.env.core?.AWS_DEPLOY_APPS_DOMAIN)- ?.trim()- .toLowerCase()- .replace(/\.$/, "");- const company = new URL(config.publicUrl).hostname.toLowerCase();- if (!validAlbHostname(domain) || domain !== coreDomain || !domain.endsWith(`.${company}`)) {- throw new Error("portal apps domain must match core and remain beneath the company public hostname");- }- return domain;-}--function awsCoreHostnames(config: QmConfig): string[] {- const hosts: string[] = [];- const normalize = (value: string, source: string): string => {- const host = value.trim().toLowerCase().replace(/\.$/, "");- if (!validAlbHostname(host)) {- throw new Error(`${source} ${JSON.stringify(value)} does not derive a valid ALB host-header hostname`);- }- return host;- };- const api = config.apiUrl?.trim();- if (api) {- let hostname: string;- try {- hostname = new URL(api).hostname;- } catch {- throw new Error(- `apiUrl ${JSON.stringify(api)} is not a valid URL, so the ALB host rule for the core API cannot be derived`,- );- }- const apiHost = normalize(hostname, "apiUrl");- if (apiHost !== new URL(config.publicUrl).hostname.toLowerCase().replace(/\.$/, "")) hosts.push(apiHost);- }- const apps = config.env.core?.DEPLOY_APPS_DOMAIN?.trim() || config.env.core?.AWS_DEPLOY_APPS_DOMAIN?.trim();- if (apps && apps.trim().toLowerCase().replace(/\.$/, "") !== awsPortalAppsDomain(config))- hosts.push(`*.${normalize(apps, "the apps domain (env.core.DEPLOY_APPS_DOMAIN or AWS_DEPLOY_APPS_DOMAIN)")}`);- return [...new Set(hosts)];-}-export function assertAwsPublicRouting(config: QmConfig,ecsServices?: ReadonlyMap<string, AwsEcsRoutingService>,diff --git a/cli/src/terraform.ts b/cli/src/terraform.tsindex ba4214907..2172f8baf 100644--- a/cli/src/terraform.ts+++ b/cli/src/terraform.ts@@ -1,3 +1,4 @@+import { awsCoreHostnames } from "./aws-routing.ts";import { existsSync, readFileSync, writeFileSync } from "node:fs";import { createHash } from "node:crypto";import { join } from "node:path";@@ -155,21 +156,7 @@ function derivedValues("the vendored AWS scaffold predates aws.deployEnvironment; update infra/variables.tf and infra/main.tf from the current scaffold before configuring it",);}- const corePublicHosts: string[] = [];- if (config.services.includes("portal")) {- const publicHost = new URL(config.publicUrl).hostname.toLowerCase();- if (config.apiUrl) {- const apiHost = new URL(config.apiUrl).hostname.toLowerCase();- if (apiHost !== publicHost) corePublicHosts.push(apiHost);- }- const appsDomain = config.env.core?.AWS_DEPLOY_APPS_DOMAIN?.trim().toLowerCase().replace(/\.$/, "");- if (appsDomain) {- if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/.test(appsDomain)) {- throw new CliError(`env.core.AWS_DEPLOY_APPS_DOMAIN ${JSON.stringify(appsDomain)} is not a valid DNS domain`);- }- corePublicHosts.push(`*.${appsDomain}`);- }- }+ const corePublicHosts = config.services.includes("portal") ? awsCoreHostnames(config) : [];if (corePublicHosts.length && !declared.includes("core_public_hosts")) {throw new CliError("the vendored AWS scaffold predates split portal/core host routing; update infra/variables.tf and infra/main.tf from the current scaffold before configuring apiUrl or AWS_DEPLOY_APPS_DOMAIN",diff --git a/cli/test/aws.test.ts b/cli/test/aws.test.tsindex c34341445..883cc1b42 100644--- a/cli/test/aws.test.ts+++ b/cli/test/aws.test.ts@@ -1188,6 +1188,9 @@ test("AWS portal ALB adopts pinned target groups and requires exactly the env-de};try {await run(hostSplitConfig(bothHosts));+ const portalApps = hostSplitConfig(bothHosts);+ portalApps.env.portal = { ...portalApps.env.portal, PORTAL_APPS_DOMAIN: bothHosts.appsDomain };+ await run(portalApps);await run(hostSplitConfig({ apiUrl: bothHosts.apiUrl }));await run(hostSplitConfig({ appsDomain: bothHosts.appsDomain }));await run(hostSplitConfig({ apiUrl: "https://API.agent.acme.example", appsDomain: "APPS.agent.acme.example." }));diff --git a/cli/test/terraform.test.ts b/cli/test/terraform.test.tsindex 45920ee74..08192df6e 100644--- a/cli/test/terraform.test.ts+++ b/cli/test/terraform.test.ts@@ -765,3 +765,84 @@ test("the deploy role lists tasks only within its cluster", () => {assert.match(listing, /ecs:ListTasks/);assert.match(listing, /"ecs:cluster" = aws_ecs_cluster\.this\.arn/);});++test("terraform routes the app wildcard through portal when both services use the same app domain", () => {+ for (const key of ["AWS_DEPLOY_APPS_DOMAIN", "DEPLOY_APPS_DOMAIN"]) {+ const rendered = terraformVars(+ {+ ...config,+ apiUrl: "https://api.agent.acme.example",+ services: ["core", "portal"],+ aws: {+ ...config.aws!,+ sharedAlb: true,+ services: {+ ...config.aws!.services,+ portal: { ecrRepository: "qm-portal", ecsService: "acme-portal", cpu: 256, memory: 512 },+ },+ },+ env: {+ core: { [key]: "apps.agent.acme.example" },+ portal: { PORTAL_APPS_DOMAIN: "APPS.AGENT.ACME.EXAMPLE." },+ },+ },+ "",+ declared,+ );+ assert.match(rendered, /core_public_hosts\s+= \[\s+"api\.agent\.acme\.example"\s+\]/);+ assert.doesNotMatch(rendered, /\*\.apps/);+ }+});++test("terraform rejects an app domain that differs between portal and core", () => {+ assert.throws(+ () =>+ terraformVars(+ {+ ...config,+ services: ["core", "portal"],+ aws: {+ ...config.aws!,+ services: {+ ...config.aws!.services,+ portal: { ecrRepository: "qm-portal", ecsService: "acme-portal", cpu: 256, memory: 512 },+ },+ },+ env: {+ core: { DEPLOY_APPS_DOMAIN: "apps.agent.acme.example" },+ portal: { PORTAL_APPS_DOMAIN: "other.agent.acme.example" },+ },+ },+ "",+ declared,+ ),+ /portal apps domain must match core/,+ );+});++test("dedicated ALBs retain direct core app routing when portal declares the same domain", () => {+ const rendered = terraformVars(+ {+ ...config,+ apiUrl: "https://api.agent.acme.example",+ services: ["core", "portal"],+ aws: {+ ...config.aws!,+ services: {+ ...config.aws!.services,+ portal: { ecrRepository: "qm-portal", ecsService: "acme-portal", cpu: 256, memory: 512 },+ },+ },+ env: {+ core: { DEPLOY_APPS_DOMAIN: "apps.agent.acme.example" },+ portal: { PORTAL_APPS_DOMAIN: "apps.agent.acme.example" },+ },+ },+ "",+ declared,+ );+ assert.match(+ rendered,+ /core_public_hosts\s+= \[\s+"\*\.apps\.agent\.acme\.example",\s+"api\.agent\.acme\.example"\s+\]/,+ );+});