Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ describe("Docker managed-bootstrap shared-state helper environment", () => {
helpers.forEach(expectCleanRunNodeHelper);
});

it("grants only the capabilities needed to restore exact Hermes root metadata (#9486)", () => {
const fake = fixture({ sharedState: "pending" });
finalizeDockerManagedStartupSharedState(
{
transaction: sharedStateTransaction(),
retainContainerAfterRollback: true,
supervisorReady: false,
},
fake.deps,
);

const rollbackHelper = nodeHelperCalls(fake.deps).find((args) =>
args.includes("--rollback-shared-state-transaction"),
);
expect(rollbackHelper).toBeDefined();
const capabilities = rollbackHelper!.flatMap((value, index, args) =>
value === "--cap-add" ? [args[index + 1]] : [],
);
expect(capabilities).toEqual(["CHOWN", "DAC_OVERRIDE", "FOWNER", "FSETID"]);
expect(rollbackHelper).toEqual(
expect.arrayContaining([
"--cap-drop",
"ALL",
"--network",
"none",
"--read-only",
"--volumes-from",
NEW_ID,
]),
);
expect(rollbackHelper).not.toContain("--privileged");
});

it("clears arbitrary container environment before the durable receipt-clear helper", () => {
const fake = fixture({ sharedState: "committed" });
clearDockerManagedStartupSharedStateCommitReceipt(sharedStateTransaction(), fake.deps);
Expand Down
13 changes: 12 additions & 1 deletion src/lib/onboard/managed-bootstrap/docker-shared-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export class DockerManagedStartupSharedStateCommitIndeterminateError extends Err
}
}

export class DockerManagedStartupSharedStateRestoreError extends Error {
constructor(detail: string) {
super(detail);
this.name = "DockerManagedStartupSharedStateRestoreError";
}
}

export function probeDockerManagedStartupSharedState(
input: {
readonly transaction: DockerManagedBootstrapSharedStateTransaction;
Expand Down Expand Up @@ -511,6 +518,10 @@ function rollbackManagedStartupSharedState(
"DAC_OVERRIDE",
"--cap-add",
"FOWNER",
// Hermes keeps its shared state root setgid. FSETID lets this isolated
// helper restore that bit after CHOWN changes the directory group.
"--cap-add",
"FSETID",
...NEUTRALIZED_PRE_ENTRYPOINT_ENV,
"--volumes-from",
transaction.containerId,
Expand All @@ -526,7 +537,7 @@ function rollbackManagedStartupSharedState(
DOCKER_MUTATION_OPTIONS,
);
if (!hasZeroDockerExitStatus(helper)) {
throw new Error(
throw new DockerManagedStartupSharedStateRestoreError(
`Immutable managed-startup helper could not restore and verify shared state: ${commandDetail(helper)}. ` +
`Protected receipt retained at ${receiptPath}`,
);
Expand Down
9 changes: 6 additions & 3 deletions src/lib/onboard/managed-bootstrap/docker-test-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type DockerFixtureOptions = {
readonly replacementEnvironment?: (environment: readonly string[]) => readonly string[];
readonly sharedState?: "committed" | "none" | "pending";
readonly sharedStateCommitResult?: FixtureCommandResult;
readonly sharedStateRollbackResult?: FixtureCommandResult;
readonly sharedReceiptClearFailures?: readonly Error[];
};

Expand Down Expand Up @@ -456,10 +457,12 @@ export function fixture(options: DockerFixtureOptions = {}) {
switch (true) {
case args.includes("--shared-state-transaction-status"):
return ok(`${sharedState}\n`);
case args.includes("--rollback-shared-state-transaction"):
sharedState = "none";
case args.includes("--rollback-shared-state-transaction"): {
const result = options.sharedStateRollbackResult ?? ok();
if (result.status === 0) sharedState = "none";
events.push("shared:rollback");
return ok();
return result;
}
}
break;
case "exec":
Expand Down
152 changes: 152 additions & 0 deletions src/lib/onboard/managed-bootstrap/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,158 @@ describe("Docker managed bootstrap adapter", () => {
expect(fake.events).not.toContain("shared:rollback");
});

it("removes only the exact replacement after Hermes metadata restoration fails (#9486)", async () => {
const metadataFailure =
"Managed startup shared-state transaction failed: managed directory metadata was not restored exactly: /sandbox/.hermes";
const fake = fixture({
sharedState: "pending",
sharedStateCommitResult: { status: 1, stderr: "injected commit failure" },
sharedStateRollbackResult: { status: 1, stderr: metadataFailure },
});
const adapter = createDockerManagedBootstrapAdapter(fake.deps);
const { handle, request, snapshot } = authority("hermes");
const prepared = await adapter.prepareBootstrapReplacement({
handle,
snapshot,
request,
replacementOptions: { values: {} },
});
const durable = durablePreparation(handle, snapshot, prepared);
const replacement = await adapter.activateBootstrapReplacement({
handle,
snapshot,
prepared,
durablePreparation: durable,
});
const commitReceipt = await adapter.awaitBootstrap({
handle,
snapshot,
replacement,
timeoutSecs: 1,
});

const failure = (await adapter
.finalizeBootstrap({
outcome: "commit",
handle,
snapshot,
prepared,
durablePreparation: durable,
replacement,
completion: commitReceipt,
})
.catch((error: unknown) => error)) as Error & {
managedBootstrapRollbackError?: unknown;
};

expect(failure).toBeInstanceOf(Error);
expect(failure.message).toContain(metadataFailure);
expect(failure.managedBootstrapRollbackError).toBeInstanceOf(
ManagedBootstrapOwnerCleanupRequiredError,
);
expect(failure.managedBootstrapRollbackError).toMatchObject({ runtimeId: OLD_ID });
expect(vi.mocked(fake.deps.dockerRm!)).toHaveBeenCalledTimes(1);
expect(vi.mocked(fake.deps.dockerRm!)).toHaveBeenCalledWith(NEW_ID, expect.any(Object));
expect(fake.replacement).toBeNull();
expect(fake.original).toMatchObject({
Id: OLD_ID,
Name: "/openshell-alpha",
State: { Running: false },
});
expect(fake.events).not.toContain(`rm:${OLD_ID}`);
expect(fake.events).not.toContain(`start:${OLD_ID}`);
expectEventBefore(fake.events, "journal:rollback-authorized", `rm:${NEW_ID}`);
expectEventBefore(fake.events, `rm:${NEW_ID}`, `rename:${OLD_ID}:openshell-alpha`);
expectEventBefore(
fake.events,
`rename:${OLD_ID}:openshell-alpha`,
"journal:owner-cleanup-required",
);
expect(fake.journal).toMatchObject({
phase: "owner-cleanup-required",
originalRuntimeId: OLD_ID,
replacementRuntimeId: NEW_ID,
});
expect(fake.finalization).toBeNull();
});

it.each([
{
driftedRuntime: "original",
drift: (fake: ReturnType<typeof fixture>) => {
fake.original!.Config!.Env = [...(fake.original!.Config!.Env ?? []), "DRIFTED=1"];
},
},
{
driftedRuntime: "replacement",
drift: (fake: ReturnType<typeof fixture>) => {
fake.replacement!.Name = "/unowned-replacement";
},
},
])(
"fails closed when the exact $driftedRuntime identity changes before restoration-failure cleanup (#9486)",
async ({ drift }) => {
const fake = fixture({
sharedState: "pending",
sharedStateCommitResult: { status: 1, stderr: "injected commit failure" },
sharedStateRollbackResult: {
status: 1,
stderr: "managed directory metadata was not restored exactly: /sandbox/.hermes",
},
});
const adapter = createDockerManagedBootstrapAdapter(fake.deps);
const { handle, request, snapshot } = authority("hermes");
const prepared = await adapter.prepareBootstrapReplacement({
handle,
snapshot,
request,
replacementOptions: { values: {} },
});
const durable = durablePreparation(handle, snapshot, prepared);
const replacement = await adapter.activateBootstrapReplacement({
handle,
snapshot,
prepared,
durablePreparation: durable,
});
const commitReceipt = await adapter.awaitBootstrap({
handle,
snapshot,
replacement,
timeoutSecs: 1,
});
drift(fake);

const failure = (await adapter
.finalizeBootstrap({
outcome: "commit",
handle,
snapshot,
prepared,
durablePreparation: durable,
replacement,
completion: commitReceipt,
})
.catch((error: unknown) => error)) as Error & {
managedBootstrapRollbackError?: Error;
};

expect(failure.message).toContain(
"managed directory metadata was not restored exactly: /sandbox/.hermes",
);
expect(failure.managedBootstrapRollbackError?.message).toMatch(
/exact original launch spec changed|replacement container has an unexpected transaction name/u,
);
expect(fake.journal?.phase).toBe("rollback-authorized");
expect(fake.original).not.toBeNull();
expect(fake.replacement).not.toBeNull();
expect(fake.events).not.toContain(`rm:${OLD_ID}`);
expect(fake.events).not.toContain(`rm:${NEW_ID}`);
expect(fake.events).not.toContain(`rename:${OLD_ID}:openshell-alpha`);
expect(fake.events).not.toContain(`start:${OLD_ID}`);
},
);

it("publishes durable rollback authority before deleting the replacement after restart", async () => {
const fake = fixture();
const secret = "post-start-provider-secret";
Expand Down
Loading
Loading