Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Consolidate web/admin and portal/auth into three application workloads (#1051)

Regan Bellb82f52b44a0c
diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml
index 5dc027c03..ce78b5920 100644
--- a/.github/workflows/cicd.yml
+++ b/.github/workflows/cicd.yml
@@ -261,6 +261,8 @@ jobs:
node-version-file: .node-version
cache: npm
cache-dependency-path: plugins/portal/package-lock.json
+ - name: Install embedded auth dependencies
+ run: npm ci --prefix plugins/auth
- name: Install, typecheck, test
working-directory: plugins/portal
run: |
diff --git a/README.md b/README.md
index 8b655e676..4977984e0 100644
--- a/README.md
+++ b/README.md
@@ -78,8 +78,9 @@ Every turn runs through a central core, which can use a variety of models and ha
to generate the response. A Postgres persistence layer holds user data, session history,
and other durable state. The agent has a small, fixed tool surface; one of those tools is
`execute`, which runs commands in the scope's own isolated sandbox — its durable computer,
-where installed tools stay installed. The web UI, the admin panel, and the public portal
-are optional plugins over the core's HTTP API;
+where installed tools stay installed. The web UI and admin panel share one service; the portal and optional built-in
+auth broker share another. These modules communicate with core over its HTTP API.
+See [combined services](docs/combined-services.md) for configuration and migration;
Slack is an optional in-process plugin that core starts
and supervises through a direct service client.
diff --git a/cli/src/backends/aws.ts b/cli/src/backends/aws.ts
index 5cd921f4e..3e38c924f 100644
--- a/cli/src/backends/aws.ts
+++ b/cli/src/backends/aws.ts
@@ -22,15 +22,22 @@ import {
type QmConfig,
} from "../config.ts";
import { manifestRef } from "../manifest.ts";
-import { computedSecrets, runtimeSecretNames, secretsForService, type ComputedSecret } from "../secrets.ts";
+import {
+ computedSecrets,
+ validatedSecrets,
+ runtimeSecretNames,
+ secretsForService,
+ type ComputedSecret,
+} from "../secrets.ts";
import {
brokerWiring,
brandEnvOf,
orgEnv,
runnableServices,
+ hostedServiceEnv,
+ serviceHost,
serviceDef,
isServiceName,
- isVirtualService,
virtualServiceEnv,
type LogOpts,
type ServiceName,
@@ -171,6 +178,10 @@ function rdsInstanceIdentifier(aws: AwsConfig): string {
return aws.rdsInstance ?? `${aws.cluster}-core`;
}
+function deployedAwsServices(aws: AwsConfig): string[] {
+ return Object.keys(aws.services).filter((name) => serviceHost(name) === name);
+}
+
function awsTopology(
config: QmConfig,
configDir: string,
@@ -182,7 +193,7 @@ function awsTopology(
const workloads = [...runnableServices(config.services), ...discovered.plugins.map((plugin) => plugin.name)];
const enabled = new Set(workloads);
const stale = Object.keys(aws.services)
- .filter((workload) => !enabled.has(workload))
+ .filter((workload) => !enabled.has(workload) && !enabled.has(serviceHost(workload)))
.sort();
const missing = workloads.filter((workload) => !aws.services[workload]);
if (stale.length || missing.length) {
@@ -306,7 +317,7 @@ export function serviceEnvironment(config: QmConfig, service: ServiceName): Reco
...orgEnv(service, config.orgId, config.publicUrl, config.services.includes("portal"), brandEnvOf(config)),
...(service === "core" ? {} : { CORE_API_URL: coreUrl }),
...coreEnv,
- ...config.env[service],
+ ...hostedServiceEnv(config.services, config.env, service),
...(service === "core" ? securityScreenEnv(config) : {}),
};
if (service === "core") {
@@ -341,7 +352,7 @@ export function serviceEnvironment(config: QmConfig, service: ServiceName): Reco
}
if (service === "portal") {
env.WEB_UI_UPSTREAM = `http://web-ui.${aws.networking.cloudMapNamespace}:8080`;
- env.ADMIN_UPSTREAM = `http://admin.${aws.networking.cloudMapNamespace}:8080`;
+ env.ADMIN_UPSTREAM = `http://web-ui.${aws.networking.cloudMapNamespace}:8080/admin`;
env.PORTAL_XFF_TRUSTED_HOPS = "1";
}
if (config.services.includes("auth")) {
@@ -1694,7 +1705,12 @@ async function applyServiceTargets(
config: QmConfig,
targets: Record<string, string>,
desiredCounts?: Record<string, number>,
- options: { waitForDrain?: boolean; waitForCompensationDrain?: boolean; timeoutMs?: number } = {},
+ options: {
+ waitForDrain?: boolean;
+ waitForCompensationDrain?: boolean;
+ timeoutMs?: number;
+ webBeforePortal?: boolean;
+ } = {},
): Promise<void> {
const aws = requireAws(config);
const workloads = Object.keys(targets);
@@ -1706,7 +1722,22 @@ async function applyServiceTargets(
);
const changed: string[] = [];
try {
- for (const workload of workloads) {
+ for (const workload of options.webBeforePortal
+ ? [...workloads].sort((a, b) => Number(b === "web-ui") - Number(a === "web-ui"))
+ : workloads) {
+ if (options.webBeforePortal && workload === "portal" && targets["web-ui"]) {
+ await awaitServiceTargets(
+ config,
+ {
+ "web-ui": {
+ taskDefinition: targets["web-ui"]!,
+ desiredCount: expectedCounts["web-ui"]!,
+ waitForDrain: true,
+ },
+ },
+ options.timeoutMs,
+ );
+ }
const args = [
"ecs",
"update-service",
@@ -1928,7 +1959,7 @@ export async function awsUp(config: QmConfig, _configDir: string, opts: AwsUpOpt
);
}
const plugins = new Map(topology.plugins.map((plugin) => [plugin.name, plugin]));
- const services = opts.only ?? topology.workloads;
+ const services = opts.only ? [...new Set(opts.only.map(serviceHost))] : topology.workloads;
const buildConcurrency = opts.buildConcurrency ?? 1;
if (!Number.isSafeInteger(buildConcurrency) || buildConcurrency < 1)
throw new CliError("--build-concurrency requires a positive integer");
@@ -1939,7 +1970,7 @@ export async function awsUp(config: QmConfig, _configDir: string, opts: AwsUpOpt
new Set(services.map((service) => aws.services[service]?.ecrRepository)).size !== services.length
)
throw new CliError("--build-concurrency requires distinct ECR repositories for selected workloads");
- const restart = new Set(opts.restart ?? []);
+ const restart = new Set((opts.restart ?? []).map(serviceHost));
if (restart.size && opts.buildOnly) throw new CliError("--restart cannot be used with --build-only");
for (const service of restart) {
if (!services.includes(service)) throw new CliError(`--restart workload ${service} is not selected for deployment`);
@@ -2025,7 +2056,7 @@ export async function awsUp(config: QmConfig, _configDir: string, opts: AwsUpOpt
assertAwsPublicApiUrl(config);
assertAwsDeployImage(config);
header(`qm ${opts.dryRun ? "plan" : "up"} — ${config.orgId} (aws)`);
- const allServices = Object.keys(aws.services);
+ const allServices = topology.workloads;
assertOwnedServices(config, describedServices(config, allServices), allServices);
const arns = secretArns(config);
if (opts.dryRun) {
@@ -2204,6 +2235,7 @@ export async function awsUp(config: QmConfig, _configDir: string, opts: AwsUpOpt
Object.fromEntries(
Object.keys(rolloutTargets).map((service) => [service, workloadDesiredCount(config, service)]),
),
+ { webBeforePortal: true },
);
applied = true;
}
@@ -2354,8 +2386,8 @@ export function awsLogs(
]);
};
if (service) {
- const resolved = isVirtualService(service) ? "core" : service;
- if (isVirtualService(service)) note(`${service} is a virtual service; showing core logs`);
+ const resolved = serviceHost(service);
+ if (resolved !== service) note(`${service} runs in ${resolved}; showing ${resolved} logs`);
runInherit(process.env.AWS_BIN ?? "aws", logArgs(resolved));
return;
}
@@ -2693,6 +2725,7 @@ function envValues(configDir: string, path: string | undefined): Map<string, str
}
export async function awsSecretsPush(config: QmConfig, configDir: string, envFile?: string): Promise<void> {
+ validatedSecrets(config);
const { aws, workloads } = awsTopology(config, configDir);
assertAwsCallerAccount(aws);
const values = envValues(configDir, envFile);
@@ -2767,6 +2800,27 @@ export async function awsSecretsPush(config: QmConfig, configDir: string, envFil
if (affected.length) step("secret activation deferred to the first complete AWS deployment");
return;
}
+ for (const [component, host, marker, expected] of [
+ ["admin", "web-ui", "ADMIN_ENABLED", "1"],
+ ["auth", "portal", "AUTH_EMBEDDED", "1"],
+ ["admin", "portal", "ADMIN_UPSTREAM", serviceEnvironment(config, "portal").ADMIN_UPSTREAM],
+ ] as const) {
+ if (!config.services.includes(component) || !workloads.includes(host)) continue;
+ const task = awsJson<{ taskDefinition?: Record<string, unknown> }>(aws, [
+ "ecs",
+ "describe-task-definition",
+ "--task-definition",
+ before.tasks[host]!,
+ ]).taskDefinition;
+ const container = (task?.containerDefinitions as Array<Record<string, unknown>> | undefined)?.find(
+ (item) => item.name === host,
+ );
+ const environment = (container?.environment ?? []) as Array<{ name: string; value: string }>;
+ if (!environment.some((entry) => entry.name === marker && entry.value === expected)) {
+ step("secret activation deferred until combined web-ui and portal images are deployed with qm up");
+ return;
+ }
+ }
const arns = secretArns(config);
const targets = { ...before.tasks };
const changed: Record<string, string> = {};
@@ -3062,7 +3116,7 @@ function awsServiceConnectConfiguration(
function awsEcsRoutingServices(config: QmConfig): ReadonlyMap<string, AwsEcsRoutingService> {
const aws = requireAws(config);
- const entries = Object.entries(aws.services);
+ const entries = deployedAwsServices(aws).map((name) => [name, aws.services[name]!] as const);
const services: AwsEcsRoutingService[] = [];
const failures: Array<{ arn?: string; reason?: string }> = [];
for (const batch of chunks(
@@ -3222,7 +3276,7 @@ export function assertAwsPublicRouting(
expectedTargetArns.add(alternate);
}
}
- for (const name of Object.keys(aws.services)) {
+ for (const name of deployedAwsServices(aws)) {
if (!ingress.includes(name) && (routingServices.get(name)?.loadBalancers ?? []).length) {
throw new Error(`private ECS service ${name} is attached to a load balancer`);
}
@@ -3664,7 +3718,7 @@ export async function awsDoctor(config: QmConfig, configDir: string): Promise<vo
throw new Error("DATABASE_URL does not point at the configured RDS endpoint");
});
const ecsServices = new Map<string, AwsEcsRoutingService>();
- for (const service of Object.keys(aws.services)) {
+ for (const service of deployedAwsServices(aws)) {
const spec = aws.services[service]!;
check(`ECS service ${spec.ecsService}`, () => {
const found = awsJson<{ services?: AwsEcsRoutingService[] }>(aws, [
@@ -3722,7 +3776,7 @@ export async function awsDoctor(config: QmConfig, configDir: string): Promise<vo
"--filters",
`Name=NAMESPACE_ID,Values=${namespace.Id},Condition=EQ`,
]).Services ?? [];
- for (const name of Object.keys(aws.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);
diff --git a/cli/src/backends/docker.ts b/cli/src/backends/docker.ts
index 3e0ca5811..e963a2b23 100644
--- a/cli/src/backends/docker.ts
+++ b/cli/src/backends/docker.ts
@@ -25,6 +25,8 @@ import {
brandEnvOf,
orgEnv,
runnableServices,
+ hostedServiceEnv,
+ serviceHost,
serviceDef,
teardownOrdered,
virtualServiceEnv,
@@ -359,7 +361,7 @@ export function dockerServiceEnv(config: QmConfig, service: ServiceName): Record
}
if (service === "portal") {
if (config.services.includes("web-ui")) out.WEB_UI_UPSTREAM = "http://web-ui:8080";
- if (config.services.includes("admin")) out.ADMIN_UPSTREAM = "http://admin:8080";
+ if (config.services.includes("admin")) out.ADMIN_UPSTREAM = "http://web-ui:8080/admin";
}
if (config.services.includes("auth")) {
Object.assign(
@@ -406,7 +408,7 @@ function serviceEnv(ctx: DockerCtx, service: ServiceName): Record<string, string
const env = {
...out,
...virtualEnv,
- ...config.env[service],
+ ...hostedServiceEnv(config.services, config.env, service),
...(service === "core" ? securityScreenEnv(config) : {}),
...secretValues(ctx, service),
};
@@ -780,8 +782,8 @@ export async function dockerLogs(config: QmConfig, service: string | undefined,
const tail = String(opts.tail ?? 200);
if (service) {
- const resolved = service === "slack" ? "core" : service;
- if (service === "slack") note("slack is a virtual service; showing core logs");
+ const resolved = serviceHost(service);
+ if (resolved !== service) note(`${service} runs in ${resolved}; showing ${resolved} logs`);
const name = `${prefix}-${resolved}`;
if (!containerExists(name)) die(`no container ${name} (is the stack up? services: ${config.services.join(", ")})`);
const args = ["logs", "--tail", tail];
diff --git a/cli/src/backends/fly.ts b/cli/src/backends/fly.ts
index d374ff158..74b6ec81a 100644
--- a/cli/src/backends/fly.ts
+++ b/cli/src/backends/fly.ts
@@ -22,6 +22,8 @@ import {
brandEnvOf,
orgEnv,
runnableServices,
+ hostedServiceEnv,
+ serviceHost,
serviceDef,
virtualServiceEnv,
type ServiceCtx,
@@ -37,7 +39,13 @@ import {
type QmConfig,
} from "../config.ts";
import { discoverPlugins, type ResolvedPlugin } from "../plugins.ts";
-import { computedSecrets, runtimeSecretNames, secretDestinations, secretsForService } from "../secrets.ts";
+import {
+ computedSecrets,
+ validatedSecrets,
+ runtimeSecretNames,
+ secretDestinations,
+ secretsForService,
+} from "../secrets.ts";
import { manifestRef } from "../manifest.ts";
import { CONNECTIVITY_CODES, CoreUnreachableError, type DeploymentLayerTransport } from "../deployment-layer.ts";
@@ -272,7 +280,7 @@ function deriveToml(ctx: FlyCtx, service: ServiceName): string {
...(ctx.config.modelProvider ? { MODEL_PROVIDER: ctx.config.modelProvider } : {}),
}
: {};
- const configuredEnv = { ...ctx.config.env[service] };
+ const configuredEnv = hostedServiceEnv(ctx.config.services, ctx.config.env, service);
if (service === "core") delete configuredEnv.FLY_ORG;
const deploymentEnv: Record<string, string> = service === "core" && ctx.flyOrg ? { FLY_ORG: ctx.flyOrg } : {};
const overrides: Record<string, string> = {
@@ -675,7 +683,7 @@ function imageSourceFor(ctx: FlyCtx, opts: FlyUpOpts): ImageSource | undefined {
return { kind: "manifest" };
}
-const SAFE_AFTER_CORE = new Set<ServiceName>(["web-ui", "admin", "portal", "auth"]);
+const SAFE_AFTER_CORE = new Set<ServiceName>(["web-ui", "admin", "auth"]);
export function flyDeployPhases(services: ServiceName[]): ServiceName[][] {
const phases: ServiceName[][] = [];
@@ -1060,7 +1068,7 @@ export async function flyUp(config: QmConfig, configDir: string, opts: FlyUpOpts
`--only "${name}": ${name} is a virtual service — it runs in-process on the core, so deploy it with --only core`,
);
}
- svc.push(name as ServiceName);
+ svc.push(serviceHost(name) as ServiceName);
} else if (pluginSet.has(name)) plg.add(name);
else {
throw new CliError(
@@ -1328,8 +1336,8 @@ export function flyLogs(
const logArgs = (app: string): string[] => ["logs", "-a", app, ...(opts.follow ? [] : ["--no-tail"])];
if (service) {
- const resolved = service === "slack" ? "core" : service;
- if (service === "slack") note("slack is a virtual service; showing core logs");
+ const resolved = serviceHost(service);
+ if (resolved !== service) note(`${service} runs in ${resolved}; showing ${resolved} logs`);
const app = `${appPrefix}-${resolved}`;
if (!which(flyBin())) {
note(`flyctl not found — run:\n ${flyBin()} ${logArgs(app).join(" ")}`);
@@ -1661,6 +1669,7 @@ export async function flyCheckLive(
}
export async function flySecretsPush(config: QmConfig, configDir: string, envFile?: string): Promise<void> {
+ validatedSecrets(config);
const path = resolve(envFile ?? join(configDir, ".env"));
const values = existsSync(path) ? readEnvFile(path) : new Map<string, string>();
const prefix = appPrefixOf(config);
diff --git a/cli/src/backends/registry.ts b/cli/src/backends/registry.ts
index d76d2818b..96245a651 100644
--- a/cli/src/backends/registry.ts
+++ b/cli/src/backends/registry.ts
@@ -7,7 +7,7 @@ import { renderTerraformVars } from "../terraform.ts";
import { buildAwsMicrovmImage, deleteAwsMicrovmImage, deleteAwsTaskDefinitions } from "../commands/infra.ts";
import { awsScaffold, dockerScaffold, flyScaffold, type ProviderScaffold } from "../provider-scaffold.ts";
import type { ResolvedPlugin } from "../plugins.ts";
-import { runnableServices } from "../services.ts";
+import { runnableServices, serviceHost } from "../services.ts";
import {
awsCheckLive,
awsDoctor,
@@ -253,7 +253,7 @@ const aws: HostingProvider = {
],
upOptions: (ctx, flags, dryRun) => {
const only = workloadOptions(flags);
- const unknown = only?.filter((name) => !ctx.config.aws?.services[name]) ?? [];
+ const unknown = only?.filter((name) => !ctx.config.aws?.services[serviceHost(name)]) ?? [];
if (unknown.length) throw new CliError(`--only has unknown AWS workload(s): ${unknown.join(", ")}`);
const imageLabel = stringFlag(flags, "image-label");
const candidate = stringFlag(flags, "candidate");
diff --git a/cli/src/commands/check.ts b/cli/src/commands/check.ts
index e0dab577d..6f9fd8708 100644
--- a/cli/src/commands/check.ts
+++ b/cli/src/commands/check.ts
@@ -7,7 +7,7 @@ import { validateSandboxLayer, type SandboxValidation } from "../sandbox-layer.t
import { discoverPlugins, type ResolvedPlugin } from "../plugins.ts";
import { mockHarnessWarning, type QmConfig } from "../config.ts";
import { computedSecrets, runtimeSecretNames, type ComputedSecret } from "../secrets.ts";
-import { isVirtualService, runnableServices } from "../services.ts";
+import { hostedServiceEnv, isVirtualService, runnableServices } from "../services.ts";
import { serviceEnvironment } from "../backends/aws.ts";
import { hostingProvider } from "../backends/registry.ts";
@@ -64,7 +64,7 @@ export function runChecks(
if (config.target === "aws" && config.aws) {
for (const workload of runnableServices(config.services)) {
if (!config.aws.services[workload]) continue;
- const configured = new Set(Object.keys(config.env[workload] ?? {}));
+ const configured = new Set(Object.keys(hostedServiceEnv(config.services, config.env, workload)));
if (workload === "core") {
for (const service of config.services.filter(isVirtualService)) {
for (const name of Object.keys(config.env[service] ?? {})) configured.add(name);
diff --git a/cli/src/config.ts b/cli/src/config.ts
index da9ff4212..d71494a3a 100644
--- a/cli/src/config.ts
+++ b/cli/src/config.ts
@@ -581,6 +581,9 @@ function validate(raw: unknown, path: string): QmConfig {
);
}
+ if (services.includes("admin") && !services.includes("portal") && target === "aws") {
+ throw new CliError(`${path}: admin requires the authenticated portal`);
+ }
const plugins = validatePlugins(o["plugins"], path);
const skills = validateStringArray(o["skills"], path, "skills");
const env = validateServiceMap(o["env"], path, "env", (v, k) => validateStringMap(v, path, `env.${k}`));
diff --git a/cli/src/provider-scaffold.ts b/cli/src/provider-scaffold.ts
index 01abd9e5c..c5c4c8361 100644
--- a/cli/src/provider-scaffold.ts
+++ b/cli/src/provider-scaffold.ts
@@ -213,9 +213,7 @@ export const awsScaffold: ProviderScaffold = {
[
["core", 2048, 4096],
["web-ui", 512, 1024],
- ["admin", 512, 1024],
["portal", 512, 1024],
- ["auth", 256, 512],
].map(([name, cpu, memory]) => [
name,
{
diff --git a/cli/src/secrets.ts b/cli/src/secrets.ts
index a1b411963..c9c1d9fa9 100644
--- a/cli/src/secrets.ts
+++ b/cli/src/secrets.ts
@@ -1,4 +1,4 @@
-import { isVirtualService, type DeclaredServiceName } from "./services.ts";
+import { serviceHost, type DeclaredServiceName } from "./services.ts";
import { effectiveModelProvider, type ModelProvider, type QmConfig } from "./config.ts";
import { TARGET_ENV_DEFAULTS } from "./target-env-defaults.ts";
import { deploymentSecretValue } from "./util.ts";
@@ -600,7 +600,7 @@ export function computedSecrets(config: QmConfig): ComputedSecret[] {
});
}
}
- return [...byName.values()]
+ const secrets = [...byName.values()]
.map((secret) => ({
...secret,
services: [...secret.services].sort(),
@@ -613,6 +613,25 @@ export function computedSecrets(config: QmConfig): ComputedSecret[] {
: {}),
}))
.sort((a, b) => a.name.localeCompare(b.name));
+ return secrets;
+}
+
+export function validatedSecrets(config: QmConfig): ComputedSecret[] {
+ const secrets = computedSecrets(config);
+ const delivered = new Map<string, string>();
+ for (const secret of secrets) {
+ for (const [workload, names] of secretDestinations(secret)) {
+ for (const name of names) {
+ const key = `${workload}:${name}`;
+ const prior = delivered.get(key);
+ if (prior !== undefined && prior !== secret.name) {
+ throw new Error(`${workload} would receive env ${name} from both ${prior} and ${secret.name}`);
+ }
+ delivered.set(key, secret.name);
+ }
+ }
+ }
+ return secrets;
}
export function secretDestinations(
@@ -625,11 +644,10 @@ export function secretDestinations(
};
for (const service of secret.services) {
if (service === "sandbox") add("core", `FLY_RESIDENT_ENV_${secret.name}`);
- else if (isVirtualService(service)) add("core", secret.name);
- else add(service, secret.name);
+ else add(serviceHost(service), secret.name);
}
for (const alias of secret.aliases ?? []) {
- add(isVirtualService(alias.service) ? "core" : alias.service, alias.name);
+ add(serviceHost(alias.service), alias.name);
}
if (secret.name === "CORE_SIGNING_SECRET") {
for (const plugin of pluginNames) add(plugin, secret.name);
@@ -642,7 +660,7 @@ export function runtimeSecretNames(
secret: ComputedSecret,
pluginNames: readonly string[] = [],
): string[] {
- return [...(secretDestinations(secret, pluginNames).get(workload) ?? [])];
+ return [...(secretDestinations(secret, pluginNames).get(serviceHost(workload)) ?? [])];
}
export function secretsForService(
@@ -650,7 +668,7 @@ export function secretsForService(
service: string,
pluginNames: readonly string[] = [],
): ComputedSecret[] {
- return computedSecrets(config).filter((secret) => secretDestinations(secret, pluginNames).has(service));
+ return computedSecrets(config).filter((secret) => secretDestinations(secret, pluginNames).has(serviceHost(service)));
}
export function serviceSecretValue(
@@ -660,7 +678,7 @@ export function serviceSecretValue(
values: ReadonlyMap<string, string>,
): string | undefined {
let value = config.env[service]?.[name];
- for (const secret of secretsForService(config, service)) {
+ for (const secret of validatedSecrets(config)) {
if (!runtimeSecretNames(service, secret).includes(name)) continue;
const supplied = deploymentSecretValue(secret.name, values.get(secret.name));
if (supplied !== undefined) value = supplied;
diff --git a/cli/src/services.ts b/cli/src/services.ts
index 1f4795358..7588c7197 100644
--- a/cli/src/services.ts
+++ b/cli/src/services.ts
@@ -11,7 +11,35 @@ export const isVirtualService = (s: string): s is VirtualServiceName =>
(VIRTUAL_SERVICE_NAMES as readonly string[]).includes(s);
export const isDeclaredService = (s: string): s is DeclaredServiceName => isServiceName(s) || isVirtualService(s);
-export const runnableServices = (names: readonly DeclaredServiceName[]): ServiceName[] => names.filter(isServiceName);
+export function serviceHost(name: string): string {
+ if (name === "admin") return "web-ui";
+ if (name === "auth") return "portal";
+ if (name === "slack") return "core";
+ return name;
+}
+
+export const runnableServices = (names: readonly DeclaredServiceName[]): ServiceName[] =>
+ [...new Set(names.map(serviceHost))].filter(isServiceName);
+
+export function hostedServiceEnv(
+ services: readonly DeclaredServiceName[],
+ env: Partial<Record<DeclaredServiceName, Record<string, string>>>,
+ host: string,
+): Record<string, string> {
+ if (host === "core" || serviceHost(host) !== host) return { ...env[host as DeclaredServiceName] };
+ const out: Record<string, string> =
+ host === "web-ui" ? { ADMIN_ENABLED: services.includes("admin") ? "1" : "0" } : {};
+ for (const service of services.filter((name) => serviceHost(name) === host)) {
+ for (const [name, value] of Object.entries(env[service] ?? {})) {
+ if (name === "PORT") continue;
+ if (out[name] !== undefined && out[name] !== value) {
+ throw new Error(`Conflicting ${name} settings in services hosted by ${host}`);
+ }
+ out[name] = value;
+ }
+ }
+ return out;
+}
export function virtualServiceEnv(
services: readonly DeclaredServiceName[],
@@ -108,8 +136,9 @@ export function orgEnv(
...(brand?.orgName ? { ORG_BRAND_ORG_NAME: brand.orgName } : {}),
};
}
- if (service === "web-ui") return { ...identity, WEB_UI_PUBLIC_URL: webUiUrl };
- if (service === "portal") return { ...identity, PORTAL_PUBLIC_URL: base };
+ if (service === "web-ui") return { ...identity, WEB_UI_PUBLIC_URL: webUiUrl, ADMIN_BASE_PATH: "/admin" };
+ if (service === "portal")
+ return { ...identity, PORTAL_PUBLIC_URL: base, ...(brand?.botName ? { AUTH_BRAND_NAME: brand.botName } : {}) };
if (service === "admin" && hasPortal) return { ...identity, ADMIN_BASE_PATH: "/admin" };
if (service === "auth") return { ...identity, ...(brand?.botName ? { AUTH_BRAND_NAME: brand.botName } : {}) };
return identity;
@@ -137,18 +166,21 @@ export function brokerWiring(
o: { publicUrl: string; authBaseUrl: string; allowedEmailDomain?: string },
): Record<string, string> {
const base = o.publicUrl.replace(/\/$/, "");
- const internal = o.authBaseUrl.replace(/\/$/, "");
const issuer = `${base}${AUTH_PATH_PREFIX}`;
if (service === "portal") {
return {
- AUTH_BROKER_UPSTREAM: internal,
+ AUTH_BROKER_UPSTREAM: "http://127.0.0.1:8099",
+ AUTH_EMBEDDED: "1",
+ AUTH_ISSUER: issuer,
+ AUTH_CLIENT_ID,
+ AUTH_REDIRECT_URI: `${base}/auth/callback`,
AUTH_BROKER_PREFIX: AUTH_PATH_PREFIX,
OIDC_CLIENT_ID: AUTH_CLIENT_ID,
OIDC_ISSUER: issuer,
OIDC_AUTH_ENDPOINT: `${issuer}/authorize`,
- OIDC_TOKEN_ENDPOINT: `${internal}/token`,
- OIDC_USERINFO_ENDPOINT: `${internal}/userinfo`,
- OIDC_JWKS_URI: `${internal}/.well-known/jwks.json`,
+ OIDC_TOKEN_ENDPOINT: "http://127.0.0.1:8099/token",
+ OIDC_USERINFO_ENDPOINT: "http://127.0.0.1:8099/userinfo",
+ OIDC_JWKS_URI: "http://127.0.0.1:8099/.well-known/jwks.json",
OIDC_SCOPES: "openid email",
OIDC_PRINCIPAL_CLAIM: "email",
...(o.allowedEmailDomain ? { OIDC_ALLOWED_EMAIL_DOMAIN: o.allowedEmailDomain } : {}),
@@ -245,7 +277,7 @@ const CATALOG: Record<ServiceName, ServiceDef> = {
managed: (s) => ({
...pluginWiring("portal", s),
WEB_UI_UPSTREAM: `http://${s.appPrefix}-web-ui.flycast`,
- ADMIN_UPSTREAM: `http://${s.appPrefix}-admin.internal:8080`,
+ ADMIN_UPSTREAM: `http://${s.appPrefix}-web-ui.flycast/admin`,
}),
stackKeys: [
"PORTAL_PUBLIC_URL",
diff --git a/cli/templates/fly/portal.toml b/cli/templates/fly/portal.toml
index a864d708e..75a4be8a9 100644
--- a/cli/templates/fly/portal.toml
+++ b/cli/templates/fly/portal.toml
@@ -8,7 +8,7 @@ primary_region = "sjc"
CORE_ORG_ID = "acme"
PORTAL_PUBLIC_URL = "https://agent.example.com"
WEB_UI_UPSTREAM = "http://qm-web-ui.flycast"
- ADMIN_UPSTREAM = "http://qm-admin.internal:8080"
+ ADMIN_UPSTREAM = "http://qm-web-ui.flycast/admin"
OIDC_AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth"
OIDC_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
OIDC_USERINFO_ENDPOINT = "https://openidconnect.googleapis.com/v1/userinfo"
diff --git a/cli/templates/fly/web-ui.toml b/cli/templates/fly/web-ui.toml
index 4e95852e5..73e101279 100644
--- a/cli/templates/fly/web-ui.toml
+++ b/cli/templates/fly/web-ui.toml
@@ -2,6 +2,8 @@ app = "qm-web-ui"
primary_region = "sjc"
[env]
+ ADMIN_BASE_PATH = "/admin"
+ ADMIN_ENABLED = "1"
QM_DEPLOYMENT_ID = "qm-v2:personal:acme:qm"
PORT = "8080"
CORE_API_URL = "http://qm-core.internal:8080"
diff --git a/cli/test/auth-broker.test.ts b/cli/test/auth-broker.test.ts
index a7f312ef7..d4f9bb900 100644
--- a/cli/test/auth-broker.test.ts
+++ b/cli/test/auth-broker.test.ts
@@ -58,10 +58,10 @@ test("fly derives the portal's whole OIDC block from the broker, over the privat
const portal = derivedTomlFor(brokerConfig(), "portal", repoRoot);
assert.match(portal, /OIDC_ISSUER = "https:\/\/agent\.example\.com\/idp"/);
assert.match(portal, /OIDC_AUTH_ENDPOINT = "https:\/\/agent\.example\.com\/idp\/authorize"/);
- assert.match(portal, /OIDC_TOKEN_ENDPOINT = "http:\/\/qm-auth\.flycast\/token"/);
- assert.match(portal, /OIDC_USERINFO_ENDPOINT = "http:\/\/qm-auth\.flycast\/userinfo"/);
- assert.match(portal, /OIDC_JWKS_URI = "http:\/\/qm-auth\.flycast\/\.well-known\/jwks\.json"/);
- assert.match(portal, /AUTH_BROKER_UPSTREAM = "http:\/\/qm-auth\.flycast"/);
+ assert.match(portal, /OIDC_TOKEN_ENDPOINT = "http:\/\/127\.0\.0\.1:8099\/token"/);
+ assert.match(portal, /OIDC_USERINFO_ENDPOINT = "http:\/\/127\.0\.0\.1:8099\/userinfo"/);
+ assert.match(portal, /OIDC_JWKS_URI = "http:\/\/127\.0\.0\.1:8099\/\.well-known\/jwks\.json"/);
+ assert.match(portal, /AUTH_BROKER_UPSTREAM = "http:\/\/127\.0\.0\.1:8099"/);
assert.match(portal, /AUTH_BROKER_PREFIX = "\/idp"/);
assert.match(portal, /OIDC_PRINCIPAL_CLAIM = "email"/);
assert.match(portal, /OIDC_ALLOWED_EMAIL_DOMAIN = "example\.com"/);
@@ -100,8 +100,8 @@ test("a configured botName brands the docker service env for core and auth", ()
test("docker and AWS wire the broker with parity", () => {
const docker = configWith(configText());
const dockerPortal = dockerServiceEnv(docker, "portal");
- assert.equal(dockerPortal.AUTH_BROKER_UPSTREAM, "http://qm-acme-auth.internal:8080");
- assert.equal(dockerPortal.OIDC_TOKEN_ENDPOINT, "http://qm-acme-auth.internal:8080/token");
+ assert.equal(dockerPortal.AUTH_BROKER_UPSTREAM, "http://127.0.0.1:8099");
+ assert.equal(dockerPortal.OIDC_TOKEN_ENDPOINT, "http://127.0.0.1:8099/token");
assert.equal(dockerPortal.OIDC_ISSUER, "https://agent.example.com/idp");
assert.equal(dockerServiceEnv(docker, "auth").AUTH_REDIRECT_URI, "https://agent.example.com/auth/callback");
assert.equal(dockerServiceEnv(docker, "web-ui").AUTH_BROKER_UPSTREAM, undefined);
@@ -125,8 +125,8 @@ test("docker and AWS wire the broker with parity", () => {
}
}`);
const awsPortal = serviceEnvironment(aws, "portal");
- assert.equal(awsPortal.AUTH_BROKER_UPSTREAM, "http://auth.acme.internal:8080");
- assert.equal(awsPortal.OIDC_JWKS_URI, "http://auth.acme.internal:8080/.well-known/jwks.json");
+ assert.equal(awsPortal.AUTH_BROKER_UPSTREAM, "http://127.0.0.1:8099");
+ assert.equal(awsPortal.OIDC_JWKS_URI, "http://127.0.0.1:8099/.well-known/jwks.json");
assert.equal(awsPortal.OIDC_ALLOWED_EMAIL_DOMAIN, "example.com");
assert.equal(serviceEnvironment(aws, "core").AUTH_ALLOWED_EMAIL_DOMAIN, "example.com");
assert.equal(
@@ -154,13 +154,13 @@ test("the broker's generated secrets reach both sides under the right names", ()
const config = brokerConfig();
const secrets = computedSecrets(config);
const clientSecret = secrets.find((secret) => secret.name === "AUTH_CLIENT_SECRET")!;
- assert.deepEqual(runtimeSecretNames("auth", clientSecret), ["AUTH_CLIENT_SECRET"]);
- assert.deepEqual(runtimeSecretNames("portal", clientSecret), ["OIDC_CLIENT_SECRET"]);
+ assert.deepEqual(runtimeSecretNames("auth", clientSecret), ["AUTH_CLIENT_SECRET", "OIDC_CLIENT_SECRET"]);
+ assert.deepEqual(runtimeSecretNames("portal", clientSecret), ["AUTH_CLIENT_SECRET", "OIDC_CLIENT_SECRET"]);
assert.ok(clientSecret.generate, "the CLI mints it rather than asking the operator");
const jwk = secrets.find((secret) => secret.name === "AUTH_SIGNING_JWK")!;
assert.deepEqual(runtimeSecretNames("auth", jwk), ["AUTH_SIGNING_JWK"]);
- assert.deepEqual(runtimeSecretNames("portal", jwk), []);
+ assert.deepEqual(runtimeSecretNames("portal", jwk), ["AUTH_SIGNING_JWK"]);
assert.match(jwk.generate ?? "", /P-256/);
const names = new Set(secrets.map((secret) => secret.name));
@@ -183,8 +183,8 @@ test("without a configured domain the allowlist becomes a required secret on bot
const config = configWith(configText({ env: `{ "auth": { "AUTH_EMAIL_TRANSPORT": "smtp" } }` }));
const allowed = computedSecrets(config).find((secret) => secret.name === "AUTH_ALLOWED_EMAILS")!;
assert.ok(allowed.required);
- assert.deepEqual(runtimeSecretNames("auth", allowed), ["AUTH_ALLOWED_EMAILS"]);
- assert.deepEqual(runtimeSecretNames("portal", allowed), ["OIDC_ALLOWED_EMAILS"]);
+ assert.deepEqual(runtimeSecretNames("auth", allowed), ["AUTH_ALLOWED_EMAILS", "OIDC_ALLOWED_EMAILS"]);
+ assert.deepEqual(runtimeSecretNames("portal", allowed), ["AUTH_ALLOWED_EMAILS", "OIDC_ALLOWED_EMAILS"]);
assert.deepEqual(runtimeSecretNames("core", allowed), ["AUTH_ALLOWED_EMAILS"]);
const names = new Set(computedSecrets(config).map((secret) => secret.name));
for (const name of ["SMTP_HOST", "SMTP_USERNAME", "SMTP_PASSWORD"]) assert.ok(names.has(name), name);
@@ -267,6 +267,6 @@ const fs=require("node:fs"); fs.appendFileSync(${JSON.stringify(log)}, process.a
const calls = readFileSync(log, "utf8");
const prefix = "acme";
assert.match(calls, new RegExp(`-a ${prefix}-core AUTH_ALLOWED_EMAILS=- [email protected],[email protected]`));
- assert.match(calls, new RegExp(`-a ${prefix}-auth AUTH_ALLOWED_EMAILS=- [email protected],[email protected]`));
+ assert.match(calls, new RegExp(`-a ${prefix}-portal AUTH_ALLOWED_EMAILS=- [email protected],[email protected]`));
assert.match(calls, new RegExp(`-a ${prefix}-portal OIDC_ALLOWED_EMAILS=- [email protected],[email protected]`));
});
diff --git a/cli/test/aws.test.ts b/cli/test/aws.test.ts
index 743b8015a..84cb4e294 100644
--- a/cli/test/aws.test.ts
+++ b/cli/test/aws.test.ts
@@ -33,6 +33,7 @@ import {
taskDefinitionDiff,
} from "../src/backends/aws.ts";
import type { QmConfig } from "../src/config.ts";
+import { runnableServices } from "../src/services.ts";
import { computedSecrets } from "../src/secrets.ts";
import { awsObjectStoreBucket } from "../src/terraform.ts";
import { withAwsLease } from "../src/aws-lease.ts";
@@ -520,6 +521,8 @@ const config: QmConfig = {
test("AWS environment derives identity, public URLs, private wiring, and MicroVM coordinates", () => {
assert.deepEqual(serviceEnvironment(config, "web-ui"), {
+ ADMIN_BASE_PATH: "/admin",
+ ADMIN_ENABLED: "1",
CORE_API_URL: "http://core.acme.internal:8080",
CORE_ORG_ID: "acme",
PORT: "8080",
@@ -2746,8 +2749,8 @@ fs.writeFileSync(path.join(dir, "started-" + name), "");
} else {
await result;
const candidate = JSON.parse(readFileSync(candidatePath, "utf8"));
- assert.deepEqual(Object.keys(candidate.images).sort(), Object.keys(config.aws!.services).sort());
- assert.deepEqual(Object.keys(candidate.imageProvenance).sort(), Object.keys(config.aws!.services).sort());
+ assert.deepEqual(Object.keys(candidate.images).sort(), runnableServices(config.services).sort());
+ assert.deepEqual(Object.keys(candidate.imageProvenance).sort(), runnableServices(config.services).sort());
assert.ok(Object.values(candidate.images).every((image) => /@sha256:[a-f0-9]{64}$/.test(String(image))));
}
const lines = readFileSync(events, "utf8").trim().split("\n");
@@ -4639,6 +4642,11 @@ test("AWS private canary reaches core without a core ingress target and refuses
process.env.PATH = `${dir}:${priorPath}`;
try {
await awsUp(config, dir, { yes: true });
+ const rolloutCalls = readFileSync(fake.log, "utf8");
+ const webUpdate = rolloutCalls.indexOf("ecs update-service --cluster acme-qm --service acme-web-ui");
+ const portalUpdate = rolloutCalls.indexOf("ecs update-service --cluster acme-qm --service acme-portal");
+ const webPoll = rolloutCalls.indexOf("ecs describe-services", webUpdate);
+ assert.ok(webUpdate >= 0 && webUpdate < webPoll && webPoll < portalUpdate);
await awsCheckLive(config, { report: false });
assert.match(
readFileSync(fake.log, "utf8"),
@@ -4826,3 +4834,42 @@ test("AWS layer deadline aborts a native response body that never finishes", asy
rmSync(dir, { recursive: true, force: true });
}
});
+
+test("AWS secrets push defers activation against pre-consolidation images", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-aws-secrets-combined-"));
+ const operator = computedSecrets(config).filter((secret) => secret.managedBy === "operator" && secret.required);
+ writeFileSync(join(dir, ".env"), operator.map((secret) => `${secret.name}=${TEST_SECRET_VALUE}`).join("\n"));
+ const fake = statefulAws(dir, config);
+ const state = JSON.parse(readFileSync(fake.state, "utf8"));
+ const tasks = Object.fromEntries(
+ runnableServices(config.services).map((name) => [name, state.services[`acme-${name}`].taskDefinition]),
+ );
+ state.definitions[tasks["web-ui"]] = {
+ containerDefinitions: [{ name: "web-ui", image: "old-web-image", environment: [] }],
+ };
+ state.dynamo = manifestItems([{ id: "current", imageLabel: "release", tasks }], "current");
+ writeFileSync(fake.state, JSON.stringify(state));
+ try {
+ await awsSecretsPush(config, dir);
+ const calls = readFileSync(fake.log, "utf8");
+ assert.match(calls, /secretsmanager put-secret-value/);
+ assert.doesNotMatch(calls, /ecs (?:register-task-definition|update-service)/);
+ state.definitions[tasks["web-ui"]].containerDefinitions[0].environment = [{ name: "ADMIN_ENABLED", value: "1" }];
+ state.definitions[tasks.portal] = {
+ containerDefinitions: [
+ {
+ name: "portal",
+ image: "old-portal-image",
+ environment: [{ name: "ADMIN_UPSTREAM", value: "http://admin.acme.internal:8080" }],
+ },
+ ],
+ };
+ writeFileSync(fake.state, JSON.stringify(state));
+ writeFileSync(fake.log, "");
+ await awsSecretsPush(config, dir);
+ assert.doesNotMatch(readFileSync(fake.log, "utf8"), /ecs (?:register-task-definition|update-service)/);
+ } finally {
+ fake.restore();
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
diff --git a/cli/test/combined-services.test.ts b/cli/test/combined-services.test.ts
new file mode 100644
index 000000000..8c3941cd2
--- /dev/null
+++ b/cli/test/combined-services.test.ts
@@ -0,0 +1,48 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { brokerWiring, hostedServiceEnv, runnableServices } from "../src/services.ts";
+import { secretDestinations } from "../src/secrets.ts";
+
+test("five logical components deploy as three services without losing core-only deployments", () => {
+ assert.deepEqual(runnableServices(["core", "slack", "web-ui", "admin", "portal", "auth"]), [
+ "core",
+ "web-ui",
+ "portal",
+ ]);
+ assert.deepEqual(runnableServices(["core"]), ["core"]);
+});
+
+test("combined settings preserve component configuration and reject conflicting values", () => {
+ assert.deepEqual(hostedServiceEnv(["portal", "auth"], { portal: { A: "1" }, auth: { B: "2" } }, "portal"), {
+ A: "1",
+ B: "2",
+ });
+ assert.throws(
+ () => hostedServiceEnv(["portal", "auth"], { portal: { A: "1" }, auth: { A: "2" } }, "portal"),
+ /Conflicting A/,
+ );
+});
+
+test("broker stays private inside portal and keeps its public issuer and callback", () => {
+ const env = brokerWiring("portal", {
+ publicUrl: "https://agent.example.com",
+ authBaseUrl: "http://retired-auth:8080",
+ });
+ assert.equal(env.AUTH_EMBEDDED, "1");
+ assert.equal(env.AUTH_BROKER_UPSTREAM, "http://127.0.0.1:8099");
+ assert.equal(env.AUTH_ISSUER, "https://agent.example.com/idp");
+ assert.equal(env.AUTH_REDIRECT_URI, "https://agent.example.com/auth/callback");
+ assert.equal(env.OIDC_TOKEN_ENDPOINT, "http://127.0.0.1:8099/token");
+});
+
+test("broker client secret keeps both required names on portal", () => {
+ const destinations = secretDestinations({
+ name: "AUTH_CLIENT_SECRET",
+ services: ["auth"],
+ aliases: [{ service: "portal", name: "OIDC_CLIENT_SECRET" }],
+ description: "test",
+ required: true,
+ managedBy: "operator",
+ });
+ assert.deepEqual([...destinations], [["portal", new Set(["AUTH_CLIENT_SECRET", "OIDC_CLIENT_SECRET"])]]);
+});
diff --git a/cli/test/e2e/docker-lifecycle.e2e.test.ts b/cli/test/e2e/docker-lifecycle.e2e.test.ts
index 01b94aa11..ed19f485b 100644
--- a/cli/test/e2e/docker-lifecycle.e2e.test.ts
+++ b/cli/test/e2e/docker-lifecycle.e2e.test.ts
@@ -20,6 +20,7 @@ import {
} from "./harness.ts";
const SERVICES = ["core", "web-ui", "admin", "portal"] as const;
+const WORKLOADS = ["core", "web-ui", "portal"] as const;
const suffix = (names: string[], end: string): string | undefined => names.find((n) => n.endsWith(`-${end}`));
function lifecycleSkip(): string | false {
@@ -78,7 +79,7 @@ test(
assert.match(r.out, /plugin widget running/);
const names = deploymentContainers(org);
- for (const svc of [...SERVICES, "widget", "pg"]) {
+ for (const svc of [...WORKLOADS, "widget", "pg"]) {
assert.ok(suffix(names, svc), `expected a running container for ${svc}; got ${names.join(", ")}`);
}
});
@@ -151,7 +152,7 @@ test(
const all = runCli(["logs", "--tail", "3"], { cwd: dep });
assert.equal(all.code, 0, all.out);
- for (const label of [...SERVICES, "widget", "pg"]) {
+ for (const label of [...WORKLOADS, "widget", "pg"]) {
assert.match(all.out, new RegExp(`${label}\\s+\\|`), `interleaved logs missing ${label}`);
}
});
diff --git a/cli/test/fly-deploy-phases.test.ts b/cli/test/fly-deploy-phases.test.ts
index db6f3aa09..9c9d11a03 100644
--- a/cli/test/fly-deploy-phases.test.ts
+++ b/cli/test/fly-deploy-phases.test.ts
@@ -4,10 +4,10 @@ import { flyDeployPhases } from "../src/backends/fly.ts";
import type { ServiceName } from "../src/services.ts";
test("fly deploy phases put core first and safe plugins in parallel", () => {
- assert.deepEqual(flyDeployPhases(["core", "admin", "web-ui", "portal"]), [["core"], ["admin", "web-ui", "portal"]]);
+ assert.deepEqual(flyDeployPhases(["core", "admin", "web-ui", "portal"]), [["core"], ["admin", "web-ui"], ["portal"]]);
});
test("fly deploy phases preserve --only filtering without forcing core", () => {
- assert.deepEqual(flyDeployPhases(["admin", "portal"]), [["admin", "portal"]]);
+ assert.deepEqual(flyDeployPhases(["admin", "portal"]), [["admin"], ["portal"]]);
assert.deepEqual(flyDeployPhases(["core"] as ServiceName[]), [["core"]]);
});
diff --git a/cli/test/init.test.ts b/cli/test/init.test.ts
index 72fa1e30c..249b42b4a 100644
--- a/cli/test/init.test.ts
+++ b/cli/test/init.test.ts
@@ -248,7 +248,7 @@ test("init --target aws scaffolds the full hosted topology, Terraform, and the o
cpu: 2048,
memory: 4096,
});
- assert.deepEqual(Object.keys(config.aws?.services ?? {}), ["core", "web-ui", "admin", "portal", "auth"]);
+ assert.deepEqual(Object.keys(config.aws?.services ?? {}), ["core", "web-ui", "portal"]);
for (const name of ["main.tf", "outputs.tf", "variables.tf", "versions.tf", "terraform.tfvars"]) {
assert.ok(existsSync(join(dir, "infra", name)), `infra/${name} is scaffolded`);
}
diff --git a/cli/test/layer-wiring.test.ts b/cli/test/layer-wiring.test.ts
index 5cdc1f6ed..1e7d5cb67 100644
--- a/cli/test/layer-wiring.test.ts
+++ b/cli/test/layer-wiring.test.ts
@@ -143,7 +143,7 @@ test("a missing secretEnv value is warned, not invented", async () => {
}
});
-test("model → PI_MODEL and host ports follow the offset map (core+0, portal+1, web-ui+2, admin+3)", async () => {
+test("model → PI_MODEL and host ports follow the offset map (core+0, portal+1, combined web-ui+2)", async () => {
const dir = makeDeployment({ model: "claude-opus-4-8", services: ["core", "portal", "web-ui", "admin"] });
try {
const out = await plan(dir);
@@ -151,7 +151,7 @@ test("model → PI_MODEL and host ports follow the offset map (core+0, portal+1,
assert.match(out, /host :8080/);
assert.match(out, /host :8081/);
assert.match(out, /host :8082/);
- assert.match(out, /host :8083/);
+ assert.doesNotMatch(out, /host :8083/);
} finally {
rmSync(dir, { recursive: true, force: true });
}
diff --git a/cli/test/preflight.test.ts b/cli/test/preflight.test.ts
index 1a967384d..0f1cd3b52 100644
--- a/cli/test/preflight.test.ts
+++ b/cli/test/preflight.test.ts
@@ -222,19 +222,20 @@ test("missing email credentials disable email without failing deployment checks"
}
});
-test("email preflight resolves delivered secret aliases and settings supplied directly in config", async () => {
+test("email preflight rejects conflicting aliases and resolves settings supplied directly in config", async () => {
const aliased = {
...CONFIG,
env: { auth: { AUTH_EMAIL_TRANSPORT: "resend" } },
- secretEnv: { auth: { AUTH_EMAIL_FROM: "AUTH_SENDER", RESEND_API_KEY: "MAIL_KEY" } },
+ secretEnv: {
+ auth: { AUTH_EMAIL_FROM: "AUTH_SENDER", RESEND_API_KEY: "MAIL_KEY" },
+ },
};
const secrets = new Map([
["AUTH_SENDER", "[email protected]"],
["MAIL_KEY", "re_configured"],
]);
- assert.equal(emailTransportConfigured(aliased, secrets), true);
- await assert.doesNotReject(emailTransportPreflight(aliased, secrets));
- assert.equal(emailTransportConfigured(aliased, new Map([["MAIL_KEY", "re_configured"]])), false);
+ assert.throws(() => emailTransportConfigured(aliased, secrets), /would receive env AUTH_EMAIL_FROM from both/);
+ await assert.rejects(emailTransportPreflight(aliased, secrets), /would receive env AUTH_EMAIL_FROM from both/);
const direct = {
...CONFIG,
env: { auth: { AUTH_EMAIL_TRANSPORT: "resend", AUTH_EMAIL_FROM: "[email protected]" } },
diff --git a/cli/test/secrets.test.ts b/cli/test/secrets.test.ts
index ddda24495..c3471f93f 100644
--- a/cli/test/secrets.test.ts
+++ b/cli/test/secrets.test.ts
@@ -7,6 +7,7 @@ import { type QmConfig } from "../src/config.ts";
import { FLY_TEMPLATE_ENV_DEFAULTS } from "../src/target-env-defaults.ts";
import {
computedSecrets,
+ serviceSecretValue,
renderEnvExample,
runtimeSecretNames,
secretDestinations,
@@ -331,7 +332,7 @@ test("PORTAL_IDENTITY_SECRET reaches every service that signs or verifies a port
const secret = secretByName(config, "PORTAL_IDENTITY_SECRET");
assert.deepEqual(
[...secretDestinations(secret).keys()].sort(),
- ["admin", "core", "portal", "web-ui"],
+ ["core", "portal", "web-ui"],
"core verifies with PORTAL_IDENTITY_SECRET, so a surface left without it signs with a different key and every request it makes is rejected",
);
});
@@ -350,7 +351,7 @@ test("the invitation-email pair reaches core as optional secrets on every topolo
});
const shared = secretByName(broker, name);
assert.equal(shared.required, false);
- assert.deepEqual([...secretDestinations(shared).keys()].sort(), ["auth", "core"]);
+ assert.deepEqual([...secretDestinations(shared).keys()].sort(), ["core", "portal"]);
}
});
@@ -374,3 +375,17 @@ test("runtime model provider override controls the required billing key", () =>
assert.equal(secretByName(config, "OPENROUTER_API_KEY").required, true);
assert.equal(secretByName(config, "ANTHROPIC_API_KEY").required, false);
});
+
+test("combined auth rejects two source secrets for the same environment name", () => {
+ const config = makeConfig({
+ services: ["portal", "auth"],
+ secretEnv: {
+ portal: { RESEND_API_KEY: "PORTAL_RESEND" },
+ auth: { RESEND_API_KEY: "AUTH_RESEND" },
+ },
+ });
+ assert.throws(
+ () => serviceSecretValue(config, "auth", "RESEND_API_KEY", new Map()),
+ /would receive env RESEND_API_KEY from both/,
+ );
+});
diff --git a/deploy/helm/templates/deployment.yaml b/deploy/helm/templates/deployment.yaml
index 4d44e48d4..6e80e5633 100644
--- a/deploy/helm/templates/deployment.yaml
+++ b/deploy/helm/templates/deployment.yaml
@@ -1,6 +1,11 @@
+{{- range $component, $host := dict "admin" "web-ui" "auth" "portal" }}
+{{- if and (index $.Values.services $component).enabled (not (index $.Values.services $host).enabled) }}
+{{- fail (printf "%s requires services.%s.enabled=true" $component $host) }}
+{{- end }}
+{{- end }}
{{- $root := . -}}
{{- range $name, $svc := .Values.services }}
-{{- if $svc.enabled }}
+{{- if and $svc.enabled (ne $name "admin") (ne $name "auth") }}
---
apiVersion: apps/v1
kind: Deployment
@@ -44,14 +49,15 @@ spec:
{{- $_ := set $wired "CORE_API_URL" (printf "http://%s:%v" (printf $svcHost "core") (index $root.Values.services "core").port) }}
{{- end }}
{{- $authEnabled := (index $root.Values.services "auth").enabled }}
- {{- $authUpstream := printf "http://%s:%v" (printf $svcHost "auth") (index $root.Values.services "auth").port }}
+ {{- $authUpstream := "http://127.0.0.1:8099" }}
{{- $base := trimSuffix "/" ($root.Values.publicUrl | default "") }}
{{- $issuer := printf "%s/idp" $base }}
{{- if eq $name "portal" }}
{{- $_ := set $wired "WEB_UI_UPSTREAM" (printf "http://%s:%v" (printf $svcHost "web-ui") (index $root.Values.services "web-ui").port) }}
- {{- $_ := set $wired "ADMIN_UPSTREAM" (printf "http://%s:%v" (printf $svcHost "admin") (index $root.Values.services "admin").port) }}
+ {{- $_ := set $wired "ADMIN_UPSTREAM" (printf "http://%s:%v/admin" (printf $svcHost "web-ui") (index $root.Values.services "web-ui").port) }}
{{- if $authEnabled }}
{{- $_ := set $wired "AUTH_BROKER_UPSTREAM" $authUpstream }}
+ {{- $_ := set $wired "AUTH_EMBEDDED" "1" }}
{{- $_ := set $wired "AUTH_BROKER_PREFIX" "/idp" }}
{{- end }}
{{- end }}
@@ -75,11 +81,17 @@ spec:
{{- end }}
{{- end }}
{{- end }}
- {{- if and (eq $name "auth") $authEnabled }}
+ {{- if and (eq $name "portal") $authEnabled }}
{{- $_ := set $wired "AUTH_ISSUER" $issuer }}
{{- $_ := set $wired "AUTH_REDIRECT_URI" (printf "%s/auth/callback" $base) }}
{{- end }}
- {{- if eq $name "web-ui" }}{{- $_ := set $wired "WEB_UI_PUBLIC_URL" $root.Values.publicUrl }}{{- end }}
+ {{- if eq $name "web-ui" }}
+ {{- $_ := set $wired "WEB_UI_PUBLIC_URL" $root.Values.publicUrl }}
+ {{- end }}
+ {{- end }}
+ {{- if eq $name "web-ui" }}
+ {{- $_ := set $wired "ADMIN_BASE_PATH" "/admin" }}
+ {{- $_ := set $wired "ADMIN_ENABLED" (ternary "1" "0" (index $root.Values.services "admin").enabled) }}
{{- end }}
{{- if or $wired $root.Values.env $svc.env }}
env:
@@ -91,7 +103,21 @@ spec:
- name: {{ $k }}
value: {{ $v | quote }}
{{- end }}
- {{- range $k, $v := $svc.env }}
+ {{- $componentEnv := deepCopy ($svc.env | default dict) }}
+ {{- $component := "" }}
+ {{- if eq $name "portal" }}{{- $component = "auth" }}{{- end }}
+ {{- if eq $name "web-ui" }}{{- $component = "admin" }}{{- end }}
+ {{- if and $component (index $root.Values.services $component).enabled }}
+ {{- range $k, $v := (index $root.Values.services $component).env }}
+ {{- if ne $k "PORT" }}
+ {{- if and (hasKey $componentEnv $k) (ne (toString (index $componentEnv $k)) (toString $v)) }}
+ {{- fail (printf "Conflicting %s settings in %s and %s" $k $name $component) }}
+ {{- end }}
+ {{- $_ := set $componentEnv $k $v }}
+ {{- end }}
+ {{- end }}
+ {{- end }}
+ {{- range $k, $v := $componentEnv }}
- name: {{ $k }}
value: {{ $v | quote }}
{{- end }}
diff --git a/deploy/helm/templates/service.yaml b/deploy/helm/templates/service.yaml
index bbfe22b95..25dcc4e73 100644
--- a/deploy/helm/templates/service.yaml
+++ b/deploy/helm/templates/service.yaml
@@ -1,6 +1,6 @@
{{- $root := . -}}
{{- range $name, $svc := .Values.services }}
-{{- if $svc.enabled }}
+{{- if and $svc.enabled (ne $name "admin") (ne $name "auth") }}
---
apiVersion: v1
kind: Service
diff --git a/deploy/portal/Dockerfile b/deploy/portal/Dockerfile
index 6abb4b3ab..10643529e 100644
--- a/deploy/portal/Dockerfile
+++ b/deploy/portal/Dockerfile
@@ -6,6 +6,8 @@ COPY plugins/portal/package.json plugins/portal/package-lock.json ./
RUN npm ci --omit=dev && rm -rf /root/.npm /tmp/node-compile-cache
COPY plugins/portal/src ./src
+COPY plugins/auth/src /auth/src
+RUN ln -s /app/node_modules /auth/node_modules
COPY plugins/chassis/src /chassis/src
COPY plugins/portal/tsconfig.json ./
diff --git a/deploy/portal/fly.toml b/deploy/portal/fly.toml
index a864d708e..75a4be8a9 100644
--- a/deploy/portal/fly.toml
+++ b/deploy/portal/fly.toml
@@ -8,7 +8,7 @@ primary_region = "sjc"
CORE_ORG_ID = "acme"
PORTAL_PUBLIC_URL = "https://agent.example.com"
WEB_UI_UPSTREAM = "http://qm-web-ui.flycast"
- ADMIN_UPSTREAM = "http://qm-admin.internal:8080"
+ ADMIN_UPSTREAM = "http://qm-web-ui.flycast/admin"
OIDC_AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth"
OIDC_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
OIDC_USERINFO_ENDPOINT = "https://openidconnect.googleapis.com/v1/userinfo"
diff --git a/deploy/web-ui/Dockerfile b/deploy/web-ui/Dockerfile
index 28d215a57..8629e0c28 100644
--- a/deploy/web-ui/Dockerfile
+++ b/deploy/web-ui/Dockerfile
@@ -15,6 +15,8 @@ COPY plugins/web-ui/package.json plugins/web-ui/package-lock.json ./
RUN npm ci --omit=dev && rm package-lock.json \
&& rm -rf /root/.npm /tmp/node-compile-cache
COPY plugins/web-ui/server ./server
+COPY plugins/admin/src /admin/src
+COPY plugins/admin/public /admin/public
COPY plugins/chassis/src /chassis/src
COPY --from=build /app/dist-web ./dist-web
diff --git a/deploy/web-ui/fly.toml b/deploy/web-ui/fly.toml
index 4e95852e5..73e101279 100644
--- a/deploy/web-ui/fly.toml
+++ b/deploy/web-ui/fly.toml
@@ -2,6 +2,8 @@ app = "qm-web-ui"
primary_region = "sjc"
[env]
+ ADMIN_BASE_PATH = "/admin"
+ ADMIN_ENABLED = "1"
QM_DEPLOYMENT_ID = "qm-v2:personal:acme:qm"
PORT = "8080"
CORE_API_URL = "http://qm-core.internal:8080"
diff --git a/docs/combined-services.md b/docs/combined-services.md
new file mode 100644
index 000000000..7c27222c7
--- /dev/null
+++ b/docs/combined-services.md
@@ -0,0 +1,24 @@
+# Combined web and sign-in services
+
+QM deploys three application workloads: core, web-ui, and portal. The web-ui workload serves the chat interface and the admin module under `/admin`. Portal remains the public entry point and hosts the built-in auth broker when enabled. Slack remains in core.
+
+The deployment's `services` list still declares capabilities. Keep `admin` to enable the admin module and `auth` to enable email sign-in. Their configuration remains under `env.admin`, `env.auth`, and the corresponding secret mappings. The CLI combines these into web-ui and portal respectively, rejects conflicting environment values, and preserves secret aliases. Omitting auth retains external OIDC sign-in. Logs and selective deployments for admin or auth resolve to the containing workload; the components cannot be released independently within a combined workload.
+
+The modules retain separate handlers. Admin continues verifying signed portal identities and forwarding them to core, which authorizes every privileged action. Portal continues filtering inbound identity headers, checking sessions and CSRF, and gating admin access. The embedded auth broker listens only on `127.0.0.1:8099`, with its existing browser route allowlist exposed through portal. Token and JWKS endpoints stay on loopback. Auth signing keys are now available to the portal process; core's cloud permissions do not move into either surface workload.
+
+## Upgrade an existing deployment
+
+1. Record the existing release's source, image digests, configuration, secret versions, and workload sizes. Preserve database backups and the previous workloads for rollback.
+2. Review the combined workloads' CPU and memory allocations, execution-role access to the union of their component secrets, and network access needed by the auth mail transport. Resolve conflicting environment or secret mappings before deployment.
+3. Build web-ui and portal from the same source release. The CLI orders AWS and Fly deployments with web-ui before portal. Deploy the combined web-ui before changing portal's admin upstream to its `/admin` endpoint. Deploy portal with its embedded auth configuration and both names for the broker client secret. Keep the public issuer, callback URL, session keys, token secret, and signing JWK unchanged.
+ On AWS, a secrets push uploads values but defers task activation when the live tasks still use the separate-service configuration. Complete the image rollout with `qm up`; keep existing signing and session secret values during this migration.
+
+4. Verify signed-in chat, administrator and non-administrator access, login, logout, remembered sessions, and a live admin operation. Verify forged admin cookies are rejected, token endpoints are not publicly exposed, and streaming responses still work.
+5. After qualification, scale the old admin and auth workloads to zero through the deployment provider. Docker containers can be stopped; Fly Machines and ECS services must have their old desired capacity explicitly reduced. The CLI does not destroy legacy workloads during this transition. Retain their images, configuration, and resource definitions until the rollback window closes.
+6. Retire the obsolete infrastructure in a separate reviewed change. On AWS, new scaffolds contain only core, web-ui, and portal; existing `aws.services.admin` and `aws.services.auth` entries may remain temporarily to retain their Terraform resources. Do not blindly render and apply a plan that deletes rollback resources.
+
+Rollback restores the old admin/auth capacity first, then the prior portal/web-ui image digests and routing configuration. This change introduces no database migration and does not rotate authentication keys.
+
+The Helm chart treats admin/auth entries as module configuration and renders only their hosting workloads. Existing Helm releases require a reviewed upgrade because removing old Deployment resources happens with that upgrade. The separately deployable legacy image entrypoints remain available for rollback.
+
+For source development, install root and web-ui dependencies. Portal-only package checks also need auth dependencies (`npm ci --prefix plugins/auth`). The local dev supervisor runs core, combined web, and portal; its localhost login bypass is for local wiring checks only.
diff --git a/docs/porter.md b/docs/porter.md
index cde5ac848..2f85cb8f1 100644
--- a/docs/porter.md
+++ b/docs/porter.md
@@ -89,7 +89,7 @@ any other way.
## Hosting the qm surfaces
-`porter/apps/` declares the six services (core, auth, web-ui, admin, portal,
+`porter/apps/` declares four services (core, web-ui, portal,
egress-proxy) as one Porter app per file, built from `deploy/*/Dockerfile` — `porter
apply` takes exactly one app per invocation and silently ignores extra YAML documents,
which is why they are separate files:
@@ -101,7 +101,7 @@ for f in porter/apps/*.yaml; do porter apply -f "$f"; done
Porter runs the services and assigns each **public** web service an `onporter.run`
hostname with a Let's Encrypt certificate, so this path needs no DNS record, no TLS
certificate, and no ingress controller. Only the portal is meant to be Internet-facing;
-core, auth, web-ui, and admin carry `private: true` so Porter creates no ingress for them
+core and web-ui carry `private: true` so Porter creates no ingress for them
and they are reachable only at their in-cluster address.
Two things `porter apply` will not do for you:
@@ -137,6 +137,13 @@ Core also reads `RESEND_API_KEY` and `AUTH_EMAIL_FROM` when they are set — opt
they let admins email external-user invitations from the admin Users tab or by chatting
with QM.
+The admin module runs in web-ui at `/admin`; its environment belongs to web-ui.
+The auth module runs inside portal: move its environment and secrets to portal,
+set `AUTH_EMBEDDED=1`, and use `http://127.0.0.1:8099` for
+`AUTH_BROKER_UPSTREAM` and the OIDC token, userinfo, and JWKS endpoint base.
+Set `ADMIN_UPSTREAM` to the web-ui service URL followed by `/admin`.
+See [the combined-service migration](combined-services.md) before upgrading existing apps.
+
`src/deployment/secret-schema.ts` is the authoritative list of what each service
requires; when a boot refusal names a variable this table doesn't, that file is the place
to look.
diff --git a/docs/screenshots/combined-admin.png b/docs/screenshots/combined-admin.png
new file mode 100644
index 000000000..720a78f52
Binary files /dev/null and b/docs/screenshots/combined-admin.png differ
diff --git a/plugins/admin/src/index.ts b/plugins/admin/src/index.ts
index 23b43fe72..c3ab29ead 100644
--- a/plugins/admin/src/index.ts
+++ b/plugins/admin/src/index.ts
@@ -319,15 +319,19 @@ const READS = [
"custom-providers",
];
-const server = createServer((req, res) => {
+export async function handler(req: IncomingMessage, res: ServerResponse): Promise<void> {
const raw = req.headers[PORTAL_IDENTITY_HEADER];
const token = Array.isArray(raw) ? raw[0] : raw;
- void portalTokenStore
+ await portalTokenStore
.run(token, () => handle(req, res))
.catch((err: unknown) => {
console.error("[admin] unhandled request error:", String(err));
json(res, 500, { error: "internal_error", message: "internal server error" });
});
+}
+
+const server = createServer((req, res) => {
+ void handler(req, res);
});
async function handle(req: IncomingMessage, res: ServerResponse): Promise<void> {
diff --git a/plugins/auth/src/index.ts b/plugins/auth/src/index.ts
index eb8695e50..839e02acd 100644
--- a/plugins/auth/src/index.ts
+++ b/plugins/auth/src/index.ts
@@ -23,7 +23,7 @@ export function bootChecks(): void {
throw new Error(`auth broker refusing to start: ${problems.length} misconfiguration(s)`);
}
-export async function startServer(): Promise<void> {
+export async function startServer(options: { port?: number; host?: string } = {}): Promise<import("node:http").Server> {
bootChecks();
const signingKey = await loadSigningKey(CFG.signingJwk!);
const mailer = mailerFor(CFG);
@@ -56,15 +56,21 @@ export async function startServer(): Promise<void> {
else res.end();
});
});
- server.listen(PORT, () => {
- console.log(
- `[auth] sign-in broker on http://localhost:${PORT} (issuer ${CFG.issuer}, key ${signingKey.kid}, ${mailer ? `${CFG.transport} email` : "email not configured"})`,
- );
- if (!CFG.coreSigningSecret)
- console.warn(
- "[auth] CORE_SIGNING_SECRET unset, so core will reject the single-use claims that make links and codes one-shot",
- );
+ await new Promise<void>((resolve, reject) => {
+ server.once("error", reject);
+ server.listen(options.port ?? PORT, options.host, () => {
+ server.off("error", reject);
+ resolve();
+ });
});
+ console.log(
+ `[auth] sign-in broker on http://${options.host ?? "localhost"}:${options.port ?? PORT} (issuer ${CFG.issuer}, key ${signingKey.kid}, ${mailer ? `${CFG.transport} email` : "email not configured"})`,
+ );
+ if (!CFG.coreSigningSecret)
+ console.warn(
+ "[auth] CORE_SIGNING_SECRET unset, so core will reject the single-use claims that make links and codes one-shot",
+ );
+ return server;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index 9c3faea29..152a395a2 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -1484,32 +1484,51 @@ function validEmailDomain(value: string): boolean {
);
}
-export function startServer(): void {
+export async function startServer(): Promise<void> {
bootChecks();
- server.listen(PORT, () => {
- console.log(`[portal] public front door on http://localhost:${PORT} → web-ui/admin over 6PN (org ${ORG})`);
- if (!SESSION_SECRET)
- console.warn("[portal] PORTAL_SESSION_SECRET unset, using an INSECURE dev key (dev/test only)");
- if (!SECURE_COOKIES)
- console.warn("[portal] PORTAL_PUBLIC_URL is not https, cookies are NOT Secure (dev/test only)");
- if (LOCAL_AUTH_BYPASS)
- console.warn(
- `[portal] PORTAL_LOCAL_AUTH_BYPASS=1 -- using ${LOCAL_AUTH_PRINCIPAL} as the local session principal (dev/test only)`,
- );
- if (PLAYGROUND)
- console.warn(
- `[portal] PORTAL_PLAYGROUND=1 -- unauthenticated visitors get anonymous browser-pinned sessions (${PLAYGROUND_MINTS_PER_IP} mints per IP per ${PLAYGROUND_MINT_WINDOW_S}s); admin sign-in stays on /auth/login`,
- );
- if (PLAYGROUND && !ON_FLY && XFF_TRUSTED_HOPS === 0)
- console.warn(
- "[portal] playground mint limits key on the socket address — set PORTAL_XFF_TRUSTED_HOPS when behind a reverse proxy, or every visitor shares one bucket",
+ if (process.env.AUTH_EMBEDDED === "1") {
+ if (AUTH_BROKER_UPSTREAM !== "http://127.0.0.1:8099") {
+ throw new Error("Embedded auth requires the loopback broker upstream");
+ }
+ const auth = await import("../../auth/src/index.ts");
+ const broker = await auth.startServer({ port: 8099, host: "127.0.0.1" });
+ server.once("close", () => broker.close());
+ server.once("error", () => broker.close());
+ }
+ await new Promise<void>((resolve, reject) => {
+ server.once("error", reject);
+ server.listen(PORT, () => {
+ server.off("error", reject);
+ resolve();
+ console.log(`[portal] public front door on http://localhost:${PORT} → web-ui/admin over 6PN (org ${ORG})`);
+ if (!SESSION_SECRET)
+ console.warn("[portal] PORTAL_SESSION_SECRET unset, using an INSECURE dev key (dev/test only)");
+ if (!SECURE_COOKIES)
+ console.warn("[portal] PORTAL_PUBLIC_URL is not https, cookies are NOT Secure (dev/test only)");
+ if (LOCAL_AUTH_BYPASS)
+ console.warn(
+ `[portal] PORTAL_LOCAL_AUTH_BYPASS=1 -- using ${LOCAL_AUTH_PRINCIPAL} as the local session principal (dev/test only)`,
+ );
+ if (PLAYGROUND)
+ console.warn(
+ `[portal] PORTAL_PLAYGROUND=1 -- unauthenticated visitors get anonymous browser-pinned sessions (${PLAYGROUND_MINTS_PER_IP} mints per IP per ${PLAYGROUND_MINT_WINDOW_S}s); admin sign-in stays on /auth/login`,
+ );
+ if (PLAYGROUND && !ON_FLY && XFF_TRUSTED_HOPS === 0)
+ console.warn(
+ "[portal] playground mint limits key on the socket address — set PORTAL_XFF_TRUSTED_HOPS when behind a reverse proxy, or every visitor shares one bucket",
+ );
+ console.log(
+ "[portal] /admin access is derived (portal → admin surface /api/whoami over 6PN → core canAdminister); the core's ADMIN_GRANTS is the one source of admin identity",
);
- console.log(
- "[portal] /admin access is derived (portal → admin surface /api/whoami over 6PN → core canAdminister); the core's ADMIN_GRANTS is the one source of admin identity",
- );
+ });
});
}
export { handle, server };
-if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) startServer();
+if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
+ startServer().catch((error: unknown) => {
+ console.error("[portal] failed to start:", errMessage(error));
+ process.exitCode = 1;
+ });
+}
diff --git a/plugins/portal/src/proxy.ts b/plugins/portal/src/proxy.ts
index c7edf9e21..cebee5f45 100644
--- a/plugins/portal/src/proxy.ts
+++ b/plugins/portal/src/proxy.ts
@@ -132,7 +132,7 @@ export function proxyToSurface(req: IncomingMessage, res: ServerResponse, t: Sur
protocol: upstream.protocol,
hostname: upstream.hostname,
port: requestPort(upstream),
- path: `${t.forwardPath}${t.search}`,
+ path: `${upstream.pathname.replace(/\/$/, "")}${t.forwardPath}${t.search}`,
headers,
honorFramePolicy: true,
});
diff --git a/plugins/web-ui/server/index.ts b/plugins/web-ui/server/index.ts
index 4d900a66b..e37e3169b 100644
--- a/plugins/web-ui/server/index.ts
+++ b/plugins/web-ui/server/index.ts
@@ -2922,7 +2922,23 @@ const routeRequest = async (req: IncomingMessage, res: ServerResponse) => {
json(res, 404, { error: "not found" });
};
+let adminModule: Promise<typeof import("../../admin/src/index.ts")> | undefined;
+
export const handler = async (req: IncomingMessage, res: ServerResponse) => {
+ const originalUrl = req.url ?? "/";
+ const path = originalUrl.split("?")[0]!;
+ if (process.env.ADMIN_ENABLED !== "0" && (path === "/admin" || path.startsWith("/admin/"))) {
+ req.url = originalUrl.slice("/admin".length) || "/";
+ if (req.url.startsWith("?")) req.url = `/${req.url}`;
+ try {
+ process.env.ADMIN_BASE_PATH ??= "/admin";
+ adminModule ??= import("../../admin/src/index.ts");
+ await (await adminModule).handler(req, res);
+ } finally {
+ req.url = originalUrl;
+ }
+ return;
+ }
res.setHeader("strict-transport-security", "max-age=63072000; includeSubDomains");
res.setHeader("referrer-policy", "no-referrer");
res.setHeader("x-content-type-options", "nosniff");
diff --git a/plugins/web-ui/test/combined-admin.test.ts b/plugins/web-ui/test/combined-admin.test.ts
new file mode 100644
index 000000000..5ce21571d
--- /dev/null
+++ b/plugins/web-ui/test/combined-admin.test.ts
@@ -0,0 +1,77 @@
+import assert from "node:assert/strict";
+import { after, test } from "node:test";
+import { createServer } from "node:http";
+import type { AddressInfo } from "node:net";
+import { mintPortalIdentity, verifyPortalIdentity } from "../../chassis/src/portal-identity.ts";
+
+const identitySecret = "combined-admin-identity-secret-0123456789";
+const calls: Array<{ actor: string | undefined; principal: string | undefined; path: string }> = [];
+const core = createServer((req, res) => {
+ const raw = req.headers["x-portal-identity"];
+ const token = typeof raw === "string" ? raw : "";
+ const principal = verifyPortalIdentity(token, identitySecret, Date.now())?.p;
+ const actor = req.headers["x-admin-actor"] as string | undefined;
+ const path = new URL(req.url!, "http://core").pathname;
+ calls.push({ actor, principal, path });
+ res.setHeader("content-type", "application/json");
+ if (path === "/v1/admin/whoami") {
+ res.end(JSON.stringify({ isAdmin: principal === "[email protected]", principal }));
+ return;
+ }
+ res.statusCode = principal === "[email protected]" ? 200 : 403;
+ res.end(JSON.stringify({ principal }));
+});
+await new Promise<void>((resolve) => core.listen(0, "127.0.0.1", resolve));
+process.env.CORE_API_URL = `http://127.0.0.1:${(core.address() as AddressInfo).port}`;
+process.env.CORE_SIGNING_SECRET = "combined-admin-core-secret-0123456789";
+process.env.PORTAL_IDENTITY_SECRET = identitySecret;
+process.env.ALLOW_UNSIGNED_TEST_IDENTITY = "0";
+process.env.ADMIN_ENABLED = "1";
+const { handler } = await import("../server/index.ts");
+const server = createServer((req, res) => {
+ handler(req, res).catch(() => {
+ res.statusCode = 500;
+ res.end();
+ });
+});
+await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
+after(() => {
+ server.close();
+ core.close();
+});
+
+const headers = (principal: string) => ({
+ "x-portal-identity": mintPortalIdentity({ p: principal, exp: Date.now() + 60_000 }, identitySecret),
+});
+
+test("combined admin rejects a forged admin cookie before calling a privileged core API", async () => {
+ const before = calls.length;
+ const response = await fetch(`${base}/admin/api/scopes`, { headers: { cookie: "[email protected]" } });
+ assert.equal(response.status, 401);
+ assert.equal(calls.slice(before).filter((entry) => entry.path.startsWith("/v1/admin/")).length, 0);
+});
+
+test("combined admin preserves authorization and request-local identity", async () => {
+ const before = calls.length;
+ const results = await Promise.all(
+ ["[email protected]", "[email protected]"].map(async (principal) => {
+ const response = await fetch(`${base}/admin/api/scopes`, { headers: headers(principal) });
+ await response.text();
+ return response.status;
+ }),
+ );
+ assert.deepEqual(results, [200, 403]);
+ const privileged = calls.slice(before).filter((entry) => entry.path === "/v1/admin/scopes");
+ assert.equal(privileged.length, 2);
+ for (const call of privileged) {
+ assert.equal(call.actor, `${call.principal}@acme`);
+ assert.equal(call.path, "/v1/admin/scopes");
+ }
+});
+
+test("admin whoami is reachable at the mounted path", async () => {
+ const response = await fetch(`${base}/admin/api/whoami`, { headers: headers("[email protected]") });
+ assert.equal(response.status, 200);
+ assert.equal((await response.json()).isAdmin, true);
+});
diff --git a/porter/apps/admin.yaml b/porter/legacy/admin.yaml
similarity index 100%
rename from porter/apps/admin.yaml
rename to porter/legacy/admin.yaml
diff --git a/porter/apps/auth.yaml b/porter/legacy/auth.yaml
similarity index 100%
rename from porter/apps/auth.yaml
rename to porter/legacy/auth.yaml
diff --git a/scripts/dev/cli.ts b/scripts/dev/cli.ts
index a2821c750..db4474850 100644
--- a/scripts/dev/cli.ts
+++ b/scripts/dev/cli.ts
@@ -311,8 +311,8 @@ function printSuccess(result: BootResult, branch: string): void {
);
if (slackLive) out(` slack : @${result.handle} -> mention it in example.slack.com to test`);
out(` web : http://localhost:${ports.portal}/ (direct: http://localhost:${ports.web})`);
- out(` admin : http://localhost:${ports.portal}/admin/ (direct: http://localhost:${ports.admin})`);
- out(` logs : ${lock}/{core,web,admin,portal,supervisor}.log`);
+ out(` admin : http://localhost:${ports.portal}/admin/ (direct: http://localhost:${ports.web}/admin/)`);
+ out(` logs : ${lock}/{core,web,portal,supervisor}.log`);
out(` status : dev status | diagnose: dev doctor | apply env/code changes: dev up (reloads in place)`);
out(` down : dev down (auto-reaped if this worktree is removed)`);
}
diff --git a/scripts/dev/lib/types.ts b/scripts/dev/lib/types.ts
index 7c8b88ebc..7c5f167f1 100644
--- a/scripts/dev/lib/types.ts
+++ b/scripts/dev/lib/types.ts
@@ -13,7 +13,7 @@ export const EXIT = {
} as const;
export type ChildName = "core" | "web" | "admin" | "portal";
-export const CHILD_ORDER: ChildName[] = ["core", "web", "admin", "portal"];
+export const CHILD_ORDER: ChildName[] = ["core", "web", "portal"];
export interface SlotPorts {
core: number;
diff --git a/scripts/dev/supervisor/specs.ts b/scripts/dev/supervisor/specs.ts
index 5d8867f52..d162937a7 100644
--- a/scripts/dev/supervisor/specs.ts
+++ b/scripts/dev/supervisor/specs.ts
@@ -77,23 +77,6 @@ export function buildChildSpecs(i: SpecInputs): ChildSpec[] {
health: { kind: "tcp", port: i.ports.web },
stopGraceMs: 5_000,
},
- {
- name: "admin",
- cwd: join(i.worktree, "plugins/admin"),
- argv: ["node", `--env-file-if-exists=${join(i.worktree, ".env")}`, ...watchArgs, "src/index.ts"],
- env: {
- ...siblingBase,
- ...signing,
- PORT: String(i.ports.admin),
- CORE_API_URL: `http://localhost:${i.ports.core}`,
- CORE_ORG_ID: orgId,
- ADMIN_BASE_PATH: "/admin",
- },
- port: i.ports.admin,
- readiness: { kind: "log", pattern: `http://localhost:${i.ports.admin}` },
- health: { kind: "tcp", port: i.ports.admin },
- stopGraceMs: 5_000,
- },
{
name: "portal",
cwd: join(i.worktree, "plugins/portal"),
@@ -106,7 +89,7 @@ export function buildChildSpecs(i: SpecInputs): ChildSpec[] {
CORE_API_URL: `http://localhost:${i.ports.core}`,
CORE_ORG_ID: orgId,
WEB_UI_UPSTREAM: `http://localhost:${i.ports.web}`,
- ADMIN_UPSTREAM: `http://localhost:${i.ports.admin}`,
+ ADMIN_UPSTREAM: `http://localhost:${i.ports.web}/admin`,
PORTAL_SESSION_SECRET: i.portalSessionSecret,
NODE_ENV: "development",
PORTAL_LOCAL_AUTH_BYPASS: "1",
diff --git a/scripts/smoke-surface-image.sh b/scripts/smoke-surface-image.sh
index a7cd94c2a..263947ad8 100755
--- a/scripts/smoke-surface-image.sh
+++ b/scripts/smoke-surface-image.sh
@@ -23,11 +23,26 @@ cd "$root"
docker build -f "deploy/$service/Dockerfile" -t "$image" .
if [[ "$service" == "portal" ]]; then
+ signing_jwk="$(node -e "const {generateKeyPairSync}=require('node:crypto');process.stdout.write(JSON.stringify(generateKeyPairSync('ec',{namedCurve:'P-256'}).privateKey.export({format:'jwk'})))")"
docker run -d --name "$container" -p 127.0.0.1::"$container_port" \
-e PORTAL_SESSION_SECRET=runtime-smoke-portal-session-secret \
-e CORE_SIGNING_SECRET=runtime-smoke-core-signing-secret \
- -e OIDC_CLIENT_ID=runtime-smoke-client \
- -e OIDC_CLIENT_SECRET=runtime-smoke-client-secret \
+ -e AUTH_EMBEDDED=1 \
+ -e AUTH_BROKER_UPSTREAM=http://127.0.0.1:8099 \
+ -e AUTH_ISSUER=https://portal.example.com/idp \
+ -e AUTH_REDIRECT_URI=https://portal.example.com/auth/callback \
+ -e AUTH_CLIENT_ID=qm-portal \
+ -e AUTH_CLIENT_SECRET=runtime-smoke-client-secret-0123456789 \
+ -e AUTH_TOKEN_SECRET=runtime-smoke-auth-token-secret-0123456789 \
+ -e AUTH_SIGNING_JWK="$signing_jwk" \
+ -e AUTH_ALLOWED_EMAIL_DOMAIN=example.com \
+ -e OIDC_CLIENT_ID=qm-portal \
+ -e OIDC_ISSUER=https://portal.example.com/idp \
+ -e OIDC_AUTH_ENDPOINT=https://portal.example.com/idp/authorize \
+ -e OIDC_TOKEN_ENDPOINT=http://127.0.0.1:8099/token \
+ -e OIDC_USERINFO_ENDPOINT=http://127.0.0.1:8099/userinfo \
+ -e OIDC_JWKS_URI=http://127.0.0.1:8099/.well-known/jwks.json \
+ -e OIDC_CLIENT_SECRET=runtime-smoke-client-secret-0123456789 \
-e OIDC_ALLOWED_EMAIL_DOMAIN=example.com \
-e PORTAL_PUBLIC_URL=https://portal.example.com \
"$image" >/dev/null
@@ -53,7 +68,14 @@ port="$(docker port "$container" "$container_port/tcp" | sed 's/.*://')"
for _ in {1..30}; do
if curl -fs "http://127.0.0.1:$port/healthz" >/dev/null; then
- echo "ok: $service production image serves /healthz"
+ if [[ "$service" == "web-ui" ]]; then
+ curl -fs "http://127.0.0.1:$port/admin/" >/dev/null
+ elif [[ "$service" == "portal" ]]; then
+ docker exec "$container" node -e "fetch('http://127.0.0.1:8099/.well-known/jwks.json').then(async r=>{const j=await r.json();process.exit(r.ok&&j.keys?.length===1?0:1)}).catch(()=>process.exit(1))"
+ status="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$port/idp/.well-known/jwks.json")"
+ [[ "$status" != 200 ]]
+ fi
+ echo "ok: $service production image serves its combined routes"
exit 0
fi
[[ "$(docker inspect -f '{{.State.Running}}' "$container")" == true ]] || break