Skip to content

Commit 9595a12

Browse files
weastlerCopilot
andcommitted
fix(installer): protect deployment inputs and unattended consent
Isolate normalized installation targets, preserve validated operator settings, reject unsafe workspace paths, and keep command cleanup and private diagnostics outside blanket deployment approval. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 3301038 commit 9595a12

35 files changed

Lines changed: 1292 additions & 93 deletions

File tree

‎infrastructure/README.md‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Before provisioning, it shows the target and asks permission for billable resour
3939

4040
The guided flow creates a tagged state resource group/account first, with shared-key authorization and anonymous blob access disabled, TLS, identity-based data access, versioning, and soft deletion. Its public network endpoint accepts requests from any network, but reading or changing state still requires Azure authentication and Blob data permissions. The wizard does not require an IP address, CIDR, or external IP lookup. These bootstrap resources remain outside application Terraform state and are never deleted by the normal application destroy command. Do not reuse this backend for unrelated installations.
4141

42-
If an earlier installer created an IP-restricted state account, the wizard verifies its ownership and authentication settings, then asks before switching to authenticated access without the network restriction. It verifies the resulting settings before using state storage. It will not override disabled public access, a network perimeter, or an account with different ownership or unsafe authentication settings. If your organization requires private networking, use an authorized deployment path rather than bypassing that policy.
42+
If an earlier installer created an IP-restricted state account, the wizard verifies its ownership and authentication settings, then asks interactively before switching to authenticated access without the network restriction. Unattended runs refuse this change; `--approve-deployment` is not consent to remove network restrictions. It verifies the resulting settings before using state storage. It will not override disabled public access, a network perimeter, or an account with different ownership or unsafe authentication settings. If your organization requires private networking, use an authorized deployment path rather than bypassing that policy.
4343

4444
It then uses separate protected Terraform state files for the foundation, application, and temporary provisioning modules. Foundation resources include an ACR and an RBAC-enabled, purge-protected Key Vault. Passwords are generated independently and written to Key Vault using private temporary files, never command-line values. Existing credentials are reused, not silently rotated. The PostgreSQL administrator password still enters protected application Terraform state.
4545

@@ -49,7 +49,13 @@ The final release stops old revisions, runs migrations and schedule setup, start
4949

5050
### Saved configuration and repeat runs
5151

52-
By default, non-secret choices are saved in ignored `infrastructure/.local/installation.json`, with private generated inputs and Terraform working directories under `.local/INSTALLATION_NAME/`. Keep these files private and preserve the configuration for later releases. Start from [`installation.example.json`](installation.example.json) if you prefer to fill out the JSON yourself. Real configuration files belong under the ignored `.local/` directory, not beside the tracked example. The root shortcut runs the wizard from `infrastructure/`, so relative `--config` paths are resolved from that directory.
52+
By default, non-secret choices are saved in ignored `infrastructure/.local/installation.json`. Private generated inputs and Terraform working directories live beside the configuration in `INSTALLATION_NAME-TARGET_HASH/`. The hash identifies the cloud, normalized tenant and subscription IDs, installation name, region, and case-normalized app resource group. Interactive and unattended runs use the same path. Separate targets cannot share generated files; adjacent configurations for the same target share a local workspace writer lock in addition to the Azure leases.
53+
54+
Keep the configuration directory private and owned by the current user (0700). Existing generated files must be private regular files (0600), not symlinks or hard links. Setup refuses shared, untrusted, or redirected paths without changing their permissions or contents. Start from [`installation.example.json`](installation.example.json) if you prefer to fill out the JSON yourself. Real configuration files belong under the ignored `.local/` directory, not beside the tracked example. The root shortcut runs the wizard from `infrastructure/`, so relative `--config` paths are resolved from that directory.
55+
56+
An existing name-only workspace such as `.local/INSTALLATION_NAME/` blocks deployment before cloud changes. Do not delete it or start with empty state. After stopping prior local installer processes, independently verify the saved subscription, tenant, installation, owner, and backend state keys against the intended configuration. Preserve a private backup and move that verified workspace to the target-specific path printed by setup. Review its private inputs and Terraform backend before rerunning. A mismatch or ambiguous workspace needs manual recovery rather than automatic adoption. This local migration does not clear any cloud leases.
57+
58+
Updates preserve validated optional inputs already stored in the workspace's `installation.tfvars.json`: `runtime_secret_references`, PostgreSQL SKU and major version, backup retention, and the custom hostname/certificate pair. 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.
5359

5460
```sh
5561
# Collect and save configuration without creating cloud resources.
@@ -65,7 +71,7 @@ npm run setup:server -- \
6571
--non-interactive --approve-deployment
6672
```
6773

68-
Non-interactive execution requires complete configuration and fails with instructions when a prerequisite requires human action. It does not answer missing configuration questions or escalate access. Interactive runs refuse to overwrite a saved installation with a different target or owner; use a new configuration file for another installation. Normal updates use the existing release orchestrator; the wizard never directly applies the full application configuration to an installed Hub.
74+
Non-interactive execution requires complete configuration and a clean source checkout, and fails with instructions when a prerequisite requires human action. Deployment approval does not authorize publishing uncommitted source or changing state network restrictions. Rerun interactively for those separate decisions. It does not answer missing configuration questions or escalate access. Interactive runs refuse to overwrite a saved installation with a different target or owner; use a new configuration file for another installation. Tenant and subscription UUIDs are canonicalized to lowercase across configuration, drafts, discovery, and deployment. Normal updates use the existing release orchestrator; the wizard never directly applies the full application configuration to an installed Hub.
6975

7076
You can reopen the wizard while Hub is running, but a deployment or update is a maintenance-window operation, not a no-downtime rerun. It requires fresh approval before stopping old revisions and running setup. A failed attempt may have left Hub running with retained leases; those leases still require the [interrupted-installation recovery checks](#interrupted-installation). Restarting does not clear them or inherit permission to stop workloads.
7177

@@ -103,7 +109,9 @@ OpenTelemetry records the run, its phases, and individual command or prompt step
103109

104110
Telemetry never contains configuration answers, resource names, command arguments, environment values, UI messages, command output, exception messages or stacks, credentials, or deployment consent. Command names come from a fixed catalog; unknown commands use a generic identifier. No collector, network export, or environment-driven remote exporter is enabled. Files remain until you remove them; they are separate from drafts, Terraform state, and cloud leases.
105111

106-
Failed commands show their actual stderr to the local operator, limited to 64 KiB with terminal-control sequences removed. Those details may be sensitive: review them locally before sharing terminal captures. Successful command output is returned only to the caller, including token and secret lookups, and is not automatically printed. Neither successful output nor failed diagnostics enter telemetry or drafts.
112+
Failed commands report a safe operation label and exit status by default, including in unattended output. Raw stderr is held privately in memory, limited to 64 KiB with terminal-control sequences removed. It is shown only after an interactive **Show full error details** choice in a supported recovery menu. Those details may be sensitive: review them locally before sharing terminal captures. Successful command output is returned only to the caller, including token and secret lookups, and is not automatically printed. Neither successful output nor failed diagnostics enter telemetry or drafts.
113+
114+
On cancellation or output-limit termination, setup signals the owned command process group and waits for its exit or bounded kill escalation even if the direct child exits first. If signalling or inspection fails, it reports a cleanup failure rather than claiming work stopped. Verify that owned commands and descendants have stopped before retrying.
107115

108116
The serialized release tool keeps raw child errors and output private because they can contain credentials or lease IDs. Its errors identify the failed operation through the shared static command catalog and include a nonzero exit code when available.
109117

‎infrastructure/azure/install/backend/index.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ async function scenario({
104104
const ui: InstallerUI = {
105105
status: vi.fn(),
106106
confirm: vi.fn(async () => true),
107+
select: vi.fn(async (_message, options) => options[0]!.value),
107108
wait: vi.fn(async () => {
108109
throw new Error("Unexpected retry")
109110
}),
@@ -221,6 +222,23 @@ it("leaves an existing account unchanged when the operator cancels network migra
221222
).toBe(false)
222223
})
223224

225+
it("does not treat unattended deployment approval as consent to relax legacy state networking", async () => {
226+
const s = await scenario({ existing: true })
227+
s.storage.networkRuleSet.defaultAction = "Deny"
228+
delete s.ui.select
229+
const before = structuredClone(s.storage)
230+
231+
await expect(prepareBackend(s.context)).rejects.toThrow(/interactive/i)
232+
233+
expect(s.ui.confirm).not.toHaveBeenCalled()
234+
expect(s.storage).toEqual(before)
235+
expect(
236+
s.commands.some(
237+
(args) => args.includes("update") || args.includes("create") || args.includes("add"),
238+
),
239+
).toBe(false)
240+
})
241+
224242
it.each([undefined, { owner: "another-team" }])(
225243
"refuses to relax an account without matching account ownership tags even when its resource group is owned",
226244
async (tags) => {

‎infrastructure/azure/install/backend/index.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { writeFile } from "node:fs/promises"
21
import { join } from "node:path"
32

43
import { installationNames } from "../settings.ts"
54
import type { AzureInstallation } from "../types.ts"
5+
import { writePrivateFile } from "../../../installer/private-files/index.ts"
66

77
type StateAccount = {
88
id: string
@@ -115,6 +115,10 @@ export async function prepareBackend(context: AzureInstallation) {
115115
let storage = await readStorage()
116116
verifyStorage(storage)
117117
if (storage.networkRuleSet.defaultAction === "Deny") {
118+
if (!ui.select)
119+
throw new Error(
120+
"Changing the existing state account's network restriction requires separate interactive consent. Rerun setup interactively; deployment approval does not authorize this change. Account network settings were not changed.",
121+
)
118122
if (
119123
!(await ui.confirm(
120124
`State account ${names.stateAccount} restricts network access. Remove its network restriction and use Azure authentication and Blob data permissions instead? Shared-key and anonymous access will remain disabled.`,
@@ -209,7 +213,7 @@ export async function prepareBackend(context: AzureInstallation) {
209213
}
210214
const backend = async (key: string) => {
211215
const path = join(directory, `${key}-backend.hcl`)
212-
await writeFile(
216+
await writePrivateFile(
213217
path,
214218
[
215219
`resource_group_name = ${JSON.stringify(names.stateGroup)}`,
@@ -221,7 +225,6 @@ export async function prepareBackend(context: AzureInstallation) {
221225
`subscription_id = ${JSON.stringify(config.azure.subscriptionId)}`,
222226
`tenant_id = ${JSON.stringify(config.azure.tenantId)}`,
223227
].join("\n") + "\n",
224-
{ mode: 0o600 },
225228
)
226229
return path
227230
}

‎infrastructure/azure/install/configure/index.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,40 @@ it("rejects an unavailable tenant before asking for a subscription", async () =>
379379
false,
380380
)
381381
})
382+
383+
it("normalizes mixed-case account identifiers before selection and scoped Azure discovery", async () => {
384+
const subscriptionId = "bbbbbbbb-cccc-4ddd-8eee-ffffffffffff"
385+
const tenantId = "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"
386+
const { accounts, ui, run } = setup(subscriptionId)
387+
accounts.splice(1)
388+
accounts[0]!.id = subscriptionId.toUpperCase()
389+
accounts[0]!.tenantId = tenantId.toUpperCase()
390+
391+
const config = await configureAzure(ui, run)
392+
393+
expect(config.azure).toMatchObject({ subscriptionId, tenantId })
394+
const selections = vi.mocked(ui.select).mock.calls
395+
expect(selections[0]![1].map(({ value }) => value)).toEqual([tenantId])
396+
expect(selections[1]![1].map(({ value }) => value)).toEqual([subscriptionId])
397+
const discovery = run.mock.calls.filter(
398+
([, args]) => args[0] === "rest" || args[0] === "provider",
399+
)
400+
expect(discovery).toHaveLength(5)
401+
for (const [, args] of discovery)
402+
expect(args[args.indexOf("--subscription") + 1]).toBe(subscriptionId)
403+
})
404+
405+
it.each(["id", "tenantId"] as const)(
406+
"rejects malformed discovered %s before offering it as a deployment target",
407+
async (field) => {
408+
const { accounts, ui, run } = setup()
409+
accounts[0]![field] = `${accounts[0]![field]}\n`
410+
411+
await expect(configureAzure(ui, run)).rejects.toThrow()
412+
413+
expect(ui.select).not.toHaveBeenCalled()
414+
expect(run.mock.calls.some(([, args]) => args[0] === "rest" || args[0] === "provider")).toBe(
415+
false,
416+
)
417+
},
418+
)

‎infrastructure/azure/install/configure/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export async function configureAzure(
1111
existing?: DeploymentConfig,
1212
session?: WizardSession,
1313
): Promise<DeploymentConfig> {
14+
if (existing) existing = parseConfig(existing)
1415
const remember = async <K extends keyof DraftAnswers>(key: K, value: DraftAnswers[K]) => {
1516
await session?.answer(key, value)
1617
return value

‎infrastructure/azure/install/image/index.test.ts‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,37 @@ it("requires fresh dirty-source consent after retry even when the previous build
286286
expect(await readdir(s.context.directory)).toEqual([])
287287
})
288288

289+
it("cancels dirty-source publication without ACR login or a build when only unattended approval is available", async () => {
290+
const build = vi.fn(async (args: string[]) => {
291+
await writeDigest(args, digest)
292+
return ""
293+
})
294+
const s = await scenario(build)
295+
const run = s.context.run
296+
s.context.run = async (executable, args, options) => {
297+
const result = await run(executable, args, options)
298+
return executable === "git" && args[0] === "status" ? " M Dockerfile" : result
299+
}
300+
301+
await expect(publishImage(s.context, foundation)).rejects.toMatchObject({ name: "AbortError" })
302+
303+
expect(s.context.ui.confirm).not.toHaveBeenCalled()
304+
expect(s.azure).not.toHaveBeenCalled()
305+
expect(build).not.toHaveBeenCalled()
306+
expect(await readdir(s.context.directory)).toEqual([])
307+
})
308+
289309
it.each(["retry", "cancel"] as const)(
290310
"shows full error details without another attempt before choosing to %s",
291311
async (next) => {
292312
const registry = "https://packages.example/npm/"
293313
const headline = "Docker build failed (exit code 1)."
294-
const failure = new CommandExecutionError(
295-
[
296-
headline,
297-
...Array.from({ length: 80 }, (_, index) => `Synthetic build output ${index}`),
298-
"npm ERR! ECONNRESET registry.npmjs.org",
299-
].join("\n"),
300-
false,
301-
1,
302-
)
314+
const diagnostic = [
315+
...Array.from({ length: 80 }, (_, index) => `Synthetic build output ${index}`),
316+
"npm ERR! ECONNRESET registry.npmjs.org",
317+
].join("\n")
318+
const failure = new CommandExecutionError(headline, false, 1, diagnostic)
319+
const details = vi.spyOn(failure, "details")
303320
const cancelled = new DOMException("Cancelled while inspecting build details", "AbortError")
304321
let attempts = 0
305322
const s = await scenario(async (args) => {
@@ -310,7 +327,7 @@ it.each(["retry", "cancel"] as const)(
310327
s.context.config.npmRegistry = registry
311328
let detailsRead = false
312329
s.context.ui.wait = vi.fn(async (message) => {
313-
expect(message).toContain(failure.message)
330+
expect(message).toContain(diagnostic)
314331
expect(message).toContain(registry)
315332
expect(s.run).toHaveBeenCalledTimes(3)
316333
expect(s.azure).toHaveBeenCalledTimes(1)
@@ -330,7 +347,10 @@ it.each(["retry", "cancel"] as const)(
330347
expect(notice).toContain(headline)
331348
expect(notice).toMatch(/proxy.*configured/i)
332349
expect(notice).not.toContain("Synthetic build output")
333-
if (++menus === 1) return "details"
350+
if (++menus === 1) {
351+
expect(details).not.toHaveBeenCalled()
352+
return "details"
353+
}
334354
expect(detailsRead).toBe(true)
335355
return menus === 2 ? "retry" : "exit"
336356
})
@@ -346,6 +366,7 @@ it.each(["retry", "cancel"] as const)(
346366
expect(s.context.ui.select).toHaveBeenCalledTimes(2)
347367
}
348368
expect(s.context.ui.wait).toHaveBeenCalledTimes(1)
369+
expect(details).toHaveBeenCalled()
349370
expect(await readdir(s.context.directory)).toEqual([])
350371
},
351372
)

‎infrastructure/azure/install/image/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export async function publishImage(
2121
const dirty = await run("git", ["status", "--porcelain", "--untracked-files=normal"], {
2222
cwd: repository,
2323
})
24+
if (dirty && !ui.select)
25+
throw new DOMException(
26+
"Unattended image publication requires a clean source checkout. Commit or remove the intended changes, or rerun interactively for separate working-tree consent. Deployment approval does not authorize dirty-source publication.",
27+
"AbortError",
28+
)
2429
if (
2530
dirty &&
2631
!(await ui.confirm(

0 commit comments

Comments
 (0)