diff --git a/infrastructure/README.md b/infrastructure/README.md index 397c4929..a42f6fbb 100644 --- a/infrastructure/README.md +++ b/infrastructure/README.md @@ -77,7 +77,9 @@ An existing name-only workspace such as `.local/INSTALLATION_NAME/` blocks deplo Updates preserve validated optional inputs already stored in the workspace's `installation.tfvars.json`: Hub's public origin, GitHub App identifiers and provider revision, `runtime_environment_variables`, `runtime_secret_references`, PostgreSQL SKU and major version, backup retention, and the custom hostname/certificate pair. Runtime environment values must be strings with valid names and cannot override deployment-owned settings or share names with secret references or named GitHub settings. Runtime references must match the main module's environment-name and Key Vault rules and must not reference the migration credential. Other customizations or invalid inputs stop setup before cloud changes or input rewrites. Review changes to immutable infrastructure through the existing Terraform guards; retaining a setting is not approval to replace resources. -Set `hub_application_origin` to an exact HTTPS origin such as `https://hub.example.com`, with a lowercase host and no default `:443` port, path, query, fragment, credentials, or trailing slash. A non-default port is allowed. Hub authentication requires this origin, including when using the default Container Apps hostname. An unauthenticated or Copilot-only installation can leave it unset. +Set `hub_application_origin` to an exact HTTPS origin such as `https://hub.example.com`, with a lowercase host and no default `:443` port, path, query, fragment, credentials, or trailing slash. A non-default port is allowed. Hub authentication requires this origin, including when using the default Container Apps hostname. The origin, `HUB_AUTHENTICATION_ENCRYPTION_KEY`, and `HUB_BOOTSTRAP_SECRET` must be configured together; supply the shared keys through `runtime_secret_references`. An unauthenticated or Copilot-only installation can leave all three unset. + +With `REPLICA_COPILOT_AUTHENTICATION=github-app-installation`, supply `GITHUB_APP_ID`, `REPLICA_COPILOT_GITHUB_INSTALLATION_ID`, `REPLICA_COPILOT_GITHUB_ORGANIZATION`, and a secret reference for `GITHUB_APP_PRIVATE_KEY` or `GITHUB_APP_PRIVATE_KEY_BASE64`. The Copilot installation ID must be a positive number and the organization must be a valid GitHub organization login. This mode does not require GitHub sign-in settings or `GITHUB_APP_INSTALLATION_ID`, which belongs to sign-in. Secret references satisfy presence checks; Hub validates their resolved values at startup. ```sh # Collect and save configuration without creating cloud resources. diff --git a/infrastructure/azure/install/index.test.ts b/infrastructure/azure/install/index.test.ts index e7c28101..1fb03969 100644 --- a/infrastructure/azure/install/index.test.ts +++ b/infrastructure/azure/install/index.test.ts @@ -426,6 +426,14 @@ it("preserves supported GitHub, runtime, database and TLS customization in priva github_app_installation_id: "456", github_provider_revision: 2, runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: { + key_vault_id: `/subscriptions/${s.config.azure.subscriptionId}/resourceGroups/secrets/providers/Microsoft.KeyVault/vaults/service`, + secret_id: "https://service.vault.azure.net/secrets/authentication-encryption-key", + }, + HUB_BOOTSTRAP_SECRET: { + key_vault_id: `/subscriptions/${s.config.azure.subscriptionId}/resourceGroups/secrets/providers/Microsoft.KeyVault/vaults/service`, + secret_id: "https://service.vault.azure.net/secrets/bootstrap-secret", + }, GITHUB_APP_PRIVATE_KEY_BASE64: { key_vault_id: `/subscriptions/${s.config.azure.subscriptionId}/resourceGroups/secrets/providers/Microsoft.KeyVault/vaults/service`, secret_id: `https://service.vault.azure.net/secrets/github-app-private-key/${"d".repeat(32)}`, diff --git a/infrastructure/azure/install/inputs/index.test.ts b/infrastructure/azure/install/inputs/index.test.ts index a116d6a8..9686d5a9 100644 --- a/infrastructure/azure/install/inputs/index.test.ts +++ b/infrastructure/azure/install/inputs/index.test.ts @@ -26,7 +26,7 @@ describe("readInstallationInputs()", () => { }) it("retains public runtime values, empty strings, and boundary names without a secret-reference count limit", async () => { - const { config, inputs } = boundaryInputs() + const { config, inputs, reference } = boundaryInputs() const environment = { ...Object.fromEntries(Array.from({ length: 61 }, (_, index) => [`PUBLIC_${index}`, "public"])), GITHUB_CLIENT_ID: "synthetic-public-client", @@ -35,7 +35,14 @@ describe("readInstallationInputs()", () => { [`A${"B".repeat(40)}`]: "maximum-length-name", } vi.mocked(readPrivateFile).mockResolvedValue( - JSON.stringify({ ...inputs, runtime_environment_variables: environment }), + JSON.stringify({ + ...inputs, + runtime_environment_variables: environment, + runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), + }, + }), ) await expect(readInstallationInputs("synthetic-installation", config)).resolves.toMatchObject({ @@ -55,6 +62,8 @@ describe("readInstallationInputs()", () => { PUBLIC_SETTING: "public", }, runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), GITHUB_CLIENT_SECRET: reference(6, 24, "client"), GITHUB_APP_PRIVATE_KEY_BASE64: reference(6, 24, "private"), }, @@ -76,14 +85,22 @@ describe("readInstallationInputs()", () => { origin: `https://${Array(7).fill("a".repeat(61)).join(".")}.${"a".repeat(58)}`, }, ])("retains an exact HTTPS application origin with $form", async ({ origin }) => { - const { config, inputs } = boundaryInputs() + const { config, inputs, reference } = boundaryInputs() + const secrets = { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), + } vi.mocked(readPrivateFile).mockResolvedValue( - JSON.stringify({ ...inputs, hub_application_origin: origin }), + JSON.stringify({ + ...inputs, + hub_application_origin: origin, + runtime_secret_references: secrets, + }), ) await expect(readInstallationInputs("synthetic-installation", config)).resolves.toEqual({ hub_application_origin: origin, - runtime_secret_references: {}, + runtime_secret_references: secrets, }) }) @@ -163,9 +180,11 @@ describe("readInstallationInputs()", () => { ...(source === "environment" && { runtime_environment_variables: { HUB_APPLICATION_ORIGIN: origin }, }), - ...(source === "secret" && { - runtime_secret_references: { HUB_APPLICATION_ORIGIN: reference(6, 24, "origin") }, - }), + runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), + ...(source === "secret" && { HUB_APPLICATION_ORIGIN: reference(6, 24, "origin") }), + }, } vi.mocked(readPrivateFile).mockResolvedValue(JSON.stringify({ ...inputs, ...settings })) @@ -225,6 +244,8 @@ describe("readInstallationInputs()", () => { GITHUB_PROVIDER_REVISION: "2", }, runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), GITHUB_CLIENT_SECRET: reference(6, 24, "client"), GITHUB_APP_PRIVATE_KEY_BASE64: reference(6, 24, "private"), }, @@ -235,9 +256,19 @@ describe("readInstallationInputs()", () => { }) it.each([ - { source: "a named App ID", appId: { github_app_id: "123456" }, environment: {} }, - { source: "a generic App ID", appId: {}, environment: { GITHUB_APP_ID: "123456" } }, - ])("accepts a Copilot-only App private key with $source", async ({ appId, environment }) => { + { + source: "a named App ID and encoded key", + appId: { github_app_id: "123456" }, + environment: {}, + key: "GITHUB_APP_PRIVATE_KEY_BASE64", + }, + { + source: "a generic App ID and PEM key", + appId: {}, + environment: { GITHUB_APP_ID: "123456" }, + key: "GITHUB_APP_PRIVATE_KEY", + }, + ])("accepts a Copilot-only App private key with $source", async ({ appId, environment, key }) => { const { config, inputs, reference } = boundaryInputs() const settings = { ...appId, @@ -245,8 +276,27 @@ describe("readInstallationInputs()", () => { ...environment, REPLICA_COPILOT_AUTHENTICATION: "github-app-installation", REPLICA_COPILOT_GITHUB_INSTALLATION_ID: "7890123", + REPLICA_COPILOT_GITHUB_ORGANIZATION: "fixture-org", }, runtime_secret_references: { + [key]: reference(6, 24, "private"), + }, + } + vi.mocked(readPrivateFile).mockResolvedValue(JSON.stringify({ ...inputs, ...settings })) + + await expect(readInstallationInputs("synthetic-installation", config)).resolves.toEqual(settings) + }) + + it("accepts Copilot identity values supplied by secret references without sign-in settings", async () => { + const { config, inputs, reference } = boundaryInputs() + const settings = { + github_app_id: "123456", + runtime_environment_variables: { + REPLICA_COPILOT_AUTHENTICATION: "github-app-installation", + }, + runtime_secret_references: { + REPLICA_COPILOT_GITHUB_INSTALLATION_ID: reference(6, 24, "installation"), + REPLICA_COPILOT_GITHUB_ORGANIZATION: reference(6, 24, "organization"), GITHUB_APP_PRIVATE_KEY_BASE64: reference(6, 24, "private"), }, } @@ -255,10 +305,119 @@ describe("readInstallationInputs()", () => { await expect(readInstallationInputs("synthetic-installation", config)).resolves.toEqual(settings) }) + it.each([ + { missing: "GITHUB_APP_ID", value: undefined }, + { missing: "REPLICA_COPILOT_GITHUB_INSTALLATION_ID", value: undefined }, + { missing: "REPLICA_COPILOT_GITHUB_ORGANIZATION", value: undefined }, + { missing: "GITHUB_APP_PRIVATE_KEY_BASE64", value: undefined }, + { missing: "REPLICA_COPILOT_GITHUB_INSTALLATION_ID", value: " " }, + { missing: "REPLICA_COPILOT_GITHUB_INSTALLATION_ID", value: "0" }, + { missing: "REPLICA_COPILOT_GITHUB_ORGANIZATION", value: " " }, + { missing: "REPLICA_COPILOT_GITHUB_ORGANIZATION", value: "-invalid" }, + ])("rejects a missing or invalid $missing partner for Copilot ($value)", async ({ + missing, + value, + }) => { + const { config, inputs, reference } = boundaryInputs() + const environment: Record = { + REPLICA_COPILOT_AUTHENTICATION: "github-app-installation", + GITHUB_APP_ID: "123456", + REPLICA_COPILOT_GITHUB_INSTALLATION_ID: "7890123", + REPLICA_COPILOT_GITHUB_ORGANIZATION: "fixture-org", + } + if (missing !== "GITHUB_APP_PRIVATE_KEY_BASE64") environment[missing] = value + vi.mocked(readPrivateFile).mockResolvedValue( + JSON.stringify({ + ...inputs, + runtime_environment_variables: environment, + runtime_secret_references: + missing === "GITHUB_APP_PRIVATE_KEY_BASE64" + ? {} + : { GITHUB_APP_PRIVATE_KEY_BASE64: reference(6, 24, "private") }, + }), + ) + + const failure = await readInstallationInputs("synthetic-installation", config).catch( + (error: unknown) => error, + ) + + expect(failure).toBeInstanceOf(Error) + expect(String(failure)).toContain(missing) + expect(String(failure)).not.toMatch(/7890123|fixture-org|vault\.azure\.net|-invalid/) + }) + + it.each([ + { configured: ["HUB_APPLICATION_ORIGIN"], missing: "HUB_AUTHENTICATION_ENCRYPTION_KEY" }, + { + configured: ["HUB_APPLICATION_ORIGIN", "HUB_AUTHENTICATION_ENCRYPTION_KEY"], + missing: "HUB_BOOTSTRAP_SECRET", + }, + { + configured: ["HUB_APPLICATION_ORIGIN", "HUB_BOOTSTRAP_SECRET"], + missing: "HUB_AUTHENTICATION_ENCRYPTION_KEY", + }, + { + configured: ["HUB_AUTHENTICATION_ENCRYPTION_KEY", "HUB_BOOTSTRAP_SECRET"], + missing: "HUB_APPLICATION_ORIGIN", + }, + ])("rejects shared authentication without $missing", async ({ configured, missing }) => { + const { config, inputs, reference } = boundaryInputs() + vi.mocked(readPrivateFile).mockResolvedValue( + JSON.stringify({ + ...inputs, + runtime_secret_references: Object.fromEntries( + configured.map((name) => [name, reference(6, 24, "auth")]), + ), + }), + ) + + const failure = await readInstallationInputs("synthetic-installation", config).catch( + (error: unknown) => error, + ) + + expect(failure).toBeInstanceOf(Error) + expect(String(failure)).toContain(missing) + expect(String(failure)).not.toContain("vault.azure.net") + }) + + it("allows sign-in setup to repair only a missing origin, not a missing shared key", async () => { + const { config, inputs, reference } = boundaryInputs() + const settings = { + ...inputs, + runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), + }, + } + vi.mocked(readPrivateFile).mockResolvedValue(JSON.stringify(settings)) + + await expect( + readInstallationInputs("synthetic-installation", config, { + allowMissingAuthenticationOrigin: true, + }), + ).resolves.toMatchObject({ runtime_secret_references: settings.runtime_secret_references }) + + vi.mocked(readPrivateFile).mockResolvedValue( + JSON.stringify({ + ...settings, + runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: + settings.runtime_secret_references.HUB_AUTHENTICATION_ENCRYPTION_KEY, + }, + }), + ) + + await expect( + readInstallationInputs("synthetic-installation", config, { + allowMissingAuthenticationOrigin: true, + }), + ).rejects.toThrow("HUB_BOOTSTRAP_SECRET") + }) + it.each(["named", "environment"] as const)( "accepts the minimum and maximum GitHub input limits through %s", async (source) => { - const { config, inputs } = boundaryInputs() + const { config, inputs, reference } = boundaryInputs() for (const [id, client, revision] of [ ["1", "c", 1], ["9007199254740991", "c".repeat(200), 2_147_483_647], @@ -284,6 +443,10 @@ describe("readInstallationInputs()", () => { JSON.stringify({ ...inputs, hub_application_origin: "https://hub.example.com", + runtime_secret_references: { + HUB_AUTHENTICATION_ENCRYPTION_KEY: reference(6, 24, "encryption"), + HUB_BOOTSTRAP_SECRET: reference(6, 24, "bootstrap"), + }, ...settings, }), ) diff --git a/infrastructure/azure/install/inputs/index.ts b/infrastructure/azure/install/inputs/index.ts index 0ebdbfcf..8718806b 100644 --- a/infrastructure/azure/install/inputs/index.ts +++ b/infrastructure/azure/install/inputs/index.ts @@ -273,7 +273,32 @@ export async function readInstallationInputs( !parseHttpsOrigin(environment.HUB_APPLICATION_ORIGIN) ) throw invalid() - // Mirrors Hub: an App used only for Copilot needs no sign-in, installation, or webhook settings. + const configured = (name: string) => Boolean(environment[name]?.trim() || secrets[name]) + if (environment.REPLICA_COPILOT_AUTHENTICATION?.trim() === "github-app-installation") { + const missing = [ + "GITHUB_APP_ID", + "REPLICA_COPILOT_GITHUB_INSTALLATION_ID", + "REPLICA_COPILOT_GITHUB_ORGANIZATION", + ].filter((name) => !configured(name)) + for (const [name, pattern] of [ + ["REPLICA_COPILOT_GITHUB_INSTALLATION_ID", /^[1-9]\d*$/], + ["REPLICA_COPILOT_GITHUB_ORGANIZATION", /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/], + ] as const) { + if ( + configured(name) && + environment[name] !== undefined && + !pattern.test(environment[name].trim()) + ) + missing.push(name) + } + if (!configured("GITHUB_APP_PRIVATE_KEY") && !configured("GITHUB_APP_PRIVATE_KEY_BASE64")) + missing.push("GITHUB_APP_PRIVATE_KEY or GITHUB_APP_PRIVATE_KEY_BASE64") + if (missing.length) + throw new Error( + `Saved Terraform inputs require valid ${missing.join(", ")} when REPLICA_COPILOT_AUTHENTICATION=github-app-installation. Supply runtime values or secret references before retrying; no cloud changes or input rewrites were started.`, + ) + } + // Copilot-only Apps need no GitHub sign-in, sign-in installation, or webhook settings. const copilotOnly = environment.REPLICA_COPILOT_AUTHENTICATION?.trim() === "github-app-installation" && [ @@ -292,7 +317,6 @@ export async function readInstallationInputs( (!environment.GITHUB_CLIENT_ID || !environment.GITHUB_APP_INSTALLATION_ID)))) ) throw invalid() - const configured = (name: string) => Boolean(environment[name]?.trim() || secrets[name]) const githubSettings = [ "GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY_BASE64", @@ -323,5 +347,10 @@ export async function readInstallationInputs( throw new Error( "Saved Terraform inputs require HUB_APPLICATION_ORIGIN when authentication is configured. Supply an exact HTTPS origin or a secret reference before retrying; no cloud changes or input rewrites were started.", ) + const sharedKeys = ["HUB_AUTHENTICATION_ENCRYPTION_KEY", "HUB_BOOTSTRAP_SECRET"] + if (["HUB_APPLICATION_ORIGIN", ...sharedKeys].some(configured) && !sharedKeys.every(configured)) + throw new Error( + `Saved Terraform inputs require ${sharedKeys.filter((name) => !configured(name)).join(", ")} together with HUB_APPLICATION_ORIGIN for authentication. Supply secret references before retrying; no cloud changes or input rewrites were started.`, + ) return result } diff --git a/infrastructure/azure/post/index.test.ts b/infrastructure/azure/post/index.test.ts index ac18b101..2a8cfc7e 100644 --- a/infrastructure/azure/post/index.test.ts +++ b/infrastructure/azure/post/index.test.ts @@ -854,15 +854,8 @@ it("blocks an origin change with Entra configured before creating shared secrets ENTRA_OIDC_CLIENT_AUTHENTICATION: "managed-identity", ENTRA_OIDC_MANAGED_IDENTITY_CLIENT_ID: "33333333-3333-4333-8333-333333333333", }) - delete test.inputs.runtime_secret_references!.HUB_AUTHENTICATION_ENCRYPTION_KEY - delete test.inputs.runtime_secret_references!.HUB_BOOTSTRAP_SECRET - test.secretValues.clear() Object.assign(test.state.environment, test.inputs.runtime_environment_variables, { HUB_APPLICATION_ORIGIN: test.origin, - AZURE_KEY_VAULT_SECRET_REFERENCES: JSON.stringify({ - DATABASE_URL: test.reference("database-runtime").secret_id, - SERVICE_API_KEY: test.reference("service-api-key").secret_id, - }), }) test.state.customHosts = ["bound.example.com"] const saved = JSON.stringify(test.inputs) @@ -1226,6 +1219,8 @@ it("retains desired inputs and owned leases after a failed apply and resumes the it("prints the final partial-change recovery summary and generated bootstrap path after a command failure", async () => { const test = await scenario() + delete test.inputs.hub_application_origin + delete test.state.environment.HUB_APPLICATION_ORIGIN delete test.inputs.runtime_secret_references!.HUB_AUTHENTICATION_ENCRYPTION_KEY delete test.inputs.runtime_secret_references!.HUB_BOOTSTRAP_SECRET test.secretValues.clear() @@ -1320,6 +1315,8 @@ it.each([ const terminal = await import("../../installer/terminal/index.tsx") const command = await import("../../installer/command/index.ts") if (partial) { + delete test.inputs.hub_application_origin + delete test.state.environment.HUB_APPLICATION_ORIGIN delete test.inputs.runtime_secret_references!.HUB_AUTHENTICATION_ENCRYPTION_KEY delete test.inputs.runtime_secret_references!.HUB_BOOTSTRAP_SECRET test.secretValues.clear()