Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Validate Service Connect using deployed discovery registrations (#1211)

Regan Bellf1b32b67a4ff
diff --git a/cli/src/backends/aws.ts b/cli/src/backends/aws.ts
index f5f7a86d1..46ba01590 100644
--- a/cli/src/backends/aws.ts
+++ b/cli/src/backends/aws.ts
@@ -3120,6 +3120,53 @@ function awsServiceConnectConfiguration(
);
}
+export function assertAwsServiceDiscovery(
+ config: QmConfig,
+ ecsServices: ReadonlyMap<string, AwsEcsRoutingService>,
+): void {
+ const aws = requireAws(config);
+ const namespaces =
+ awsJson<{ Namespaces?: Array<{ Id?: string; Name?: string; Arn?: string }> }>(aws, [
+ "servicediscovery",
+ "list-namespaces",
+ ]).Namespaces ?? [];
+ let namespaceId: string | undefined;
+ let registrations: Array<{ Arn?: string; Name?: string }> = [];
+ for (const name of deployedAwsServices(aws)) {
+ const ecsService = ecsServices.get(name);
+ const connect = ecsService ? awsServiceConnectConfiguration(ecsService) : undefined;
+ if (!connect?.enabled) throw new Error(`ECS service ${name} does not have Service Connect enabled`);
+ const namespace = namespaces.find((item) => item.Arn === connect.namespace || item.Name === connect.namespace);
+ if (!connect.namespace || !namespace?.Id)
+ throw new Error(`ECS service ${name} references a missing Cloud Map namespace`);
+ if (namespaceId && namespaceId !== namespace.Id)
+ throw new Error("ECS services must share their Service Connect namespace");
+ if (!namespaceId) {
+ namespaceId = namespace.Id;
+ registrations =
+ awsJson<{ Services?: Array<{ Arn?: string; Name?: string }> }>(aws, [
+ "servicediscovery",
+ "list-services",
+ "--filters",
+ `Name=NAMESPACE_ID,Values=${namespaceId},Condition=EQ`,
+ ]).Services ?? [];
+ }
+ const endpoints = connect.services?.filter((service) => service.portName === name) ?? [];
+ if (endpoints.length !== 1)
+ throw new Error(`ECS service ${name} does not publish its named ${name} port through Service Connect`);
+ const endpoint = endpoints[0]!;
+ const discoveryName = endpoint.discoveryName ?? endpoint.portName;
+ if (!registrations.some((service) => service.Name === discoveryName && service.Arn)) {
+ throw new Error(`service ${discoveryName} is missing from ${namespace.Name}`);
+ }
+ const expectedDns = `${name}.${aws.networking.cloudMapNamespace}`;
+ const expectedPort = isServiceName(name) ? serviceDef(name).docker.internalPort : 8080;
+ if (!endpoint.clientAliases?.some((alias) => alias.dnsName === expectedDns && alias.port === expectedPort)) {
+ throw new Error(`ECS service ${name} does not publish the ${expectedDns}:${expectedPort} client alias`);
+ }
+ }
+}
+
function awsEcsRoutingServices(config: QmConfig): ReadonlyMap<string, AwsEcsRoutingService> {
const aws = requireAws(config);
const entries = deployedAwsServices(aws).map((name) => [name, aws.services[name]!] as const);
@@ -3853,36 +3900,7 @@ export async function awsDoctor(config: QmConfig, configDir: string): Promise<vo
}
});
}
- check("Cloud Map routing", () => {
- const namespaces =
- awsJson<{ Namespaces?: Array<{ Id?: string; Name?: string }> }>(aws, ["servicediscovery", "list-namespaces"])
- .Namespaces ?? [];
- const namespace = namespaces.find((item) => item.Name === aws.networking.cloudMapNamespace);
- if (!namespace?.Id) throw new Error(`namespace ${aws.networking.cloudMapNamespace} is missing`);
- const services =
- awsJson<{ Services?: Array<{ Arn?: string; Name?: string }> }>(aws, [
- "servicediscovery",
- "list-services",
- "--filters",
- `Name=NAMESPACE_ID,Values=${namespace.Id},Condition=EQ`,
- ]).Services ?? [];
- for (const name of deployedAwsServices(aws)) {
- const discovery = services.find((service) => service.Name === name);
- if (!discovery?.Arn) throw new Error(`service ${name} is missing from ${aws.networking.cloudMapNamespace}`);
- const ecsService = ecsServices.get(name);
- const connect = ecsService ? awsServiceConnectConfiguration(ecsService) : undefined;
- if (!connect?.enabled) throw new Error(`ECS service ${name} does not have Service Connect enabled`);
- const endpoint = connect.services?.find((service) => service.discoveryName === name);
- const expectedDns = `${name}.${aws.networking.cloudMapNamespace}`;
- const expectedPort = isServiceName(name) ? serviceDef(name).docker.internalPort : 8080;
- if (endpoint?.portName !== name) {
- throw new Error(`ECS service ${name} does not publish its named ${name} port through Service Connect`);
- }
- if (!endpoint.clientAliases?.some((alias) => alias.dnsName === expectedDns && alias.port === expectedPort)) {
- throw new Error(`ECS service ${name} does not publish the ${expectedDns}:${expectedPort} client alias`);
- }
- }
- });
+ check("Cloud Map routing", () => assertAwsServiceDiscovery(config, ecsServices));
check("ALB routing", () => assertAwsPublicRouting(config, ecsServices));
await checkAsync("public URL DNS and TLS", () => assertAwsPublicNetwork(config));
const runtimeSecrets = new Map<string, string>();
diff --git a/cli/test/aws.test.ts b/cli/test/aws.test.ts
index 299e0734f..11391b0ec 100644
--- a/cli/test/aws.test.ts
+++ b/cli/test/aws.test.ts
@@ -9,6 +9,7 @@ import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import {
assertAwsDeploymentStorage,
+ assertAwsServiceDiscovery,
assertAwsPublicListener,
assertAwsPublicRouting,
assertGithubDeployTrust,
@@ -5091,3 +5092,43 @@ test("shared ALB validates company routes while rejecting sibling overlap", () =
}
}
});
+
+for (const shared of [false, true]) {
+ test(`AWS doctor checks ${shared ? "shared" : "dedicated"} discovery through ECS registrations`, () => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-aws-discovery-"));
+ const namespace = shared ? "shared.internal" : "acme.internal";
+ const discoveryName = shared ? "acme-core" : "core";
+ const arn = "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-shared";
+ const fake = fakeAws(
+ dir,
+ `
+if (a.includes("list-namespaces")) console.log(JSON.stringify({Namespaces:[{Id:"ns-shared",Name:${JSON.stringify(namespace)},Arn:${JSON.stringify(arn)}}]}));
+else if (a.includes("list-services")) console.log(JSON.stringify({Services:[{Name:${JSON.stringify(discoveryName)},Arn:"arn:registration"}]}));
+else console.log("");`,
+ );
+ try {
+ const endpoint = {
+ portName: "core",
+ discoveryName,
+ clientAliases: [{ dnsName: "core.acme.internal", port: 8080 }],
+ };
+ const connect = { enabled: true, namespace: arn, services: [endpoint] };
+ const services = new Map([
+ ["core", { deployments: [{ status: "PRIMARY", serviceConnectConfiguration: connect }] }],
+ ]);
+ assert.doesNotThrow(() => assertAwsServiceDiscovery(oneServiceConfig(), services));
+ endpoint.clientAliases[0]!.dnsName = "core.other.internal";
+ assert.throws(() => assertAwsServiceDiscovery(oneServiceConfig(), services), /client alias/);
+ endpoint.clientAliases[0]!.dnsName = "core.acme.internal";
+ endpoint.discoveryName = "missing";
+ assert.throws(() => assertAwsServiceDiscovery(oneServiceConfig(), services), /is missing from/);
+ endpoint.discoveryName = discoveryName;
+ connect.namespace = "missing.internal";
+ assert.throws(() => assertAwsServiceDiscovery(oneServiceConfig(), services), /missing Cloud Map namespace/);
+ assert.match(readFileSync(fake.log, "utf8"), /Values=ns-shared/);
+ } finally {
+ fake.restore();
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+}