Skip to content

Commit 08aed11

Browse files
committed
🧪 Make a marker this host cannot read a failure, and inject the faults
Continues d937a71. Findings 1, 3 and 4. **The marker correction really lands this time.** I reported it as done last round and it was not: a formatting pass had reflowed the expression my patch matched, the replacement silently did nothing, and every `stat()` failure still meant "not rejected". Only absence means that now. **The faults are injected rather than arranged.** Acquisition performs three host operations — install the launcher, read the marker, remove the working directory — through one replaceable set. Breaking any of them raises, and the test drives all three. Permissions would have been the operating system's answer rather than the test's: a privileged runner writes where it should not be able to, and Windows does not express the same modes. The removal boundary fails out of teardown rather than out of the body, so the test encloses the scope rather than sitting inside it. That is the shape of the contract: an invocation that could not clean up did not quietly succeed. **A2 now proves the question was asked safely**, not only exactly. The recording fixture reduces `GIT_TERMINAL_PROMPT`, `GIT_ASKPASS` and `SSH_ASKPASS` to labels and asserts them, so prompting is off and both askpass hooks are present-and- empty rather than naming whatever program an environment chose. Remaining: the broken launcher driven through a real transport, A6/A7's ambient helper operation log, ambient A4/A8/A11/A12, A14 and A10. Refs #522
1 parent d937a71 commit 08aed11

2 files changed

Lines changed: 178 additions & 13 deletions

File tree

‎packages/workflow/src/deno/composition/authentication.ts‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,59 @@ export interface GitAuthenticationObserver {
312312
step?: (name: string) => void;
313313
}
314314

315+
/**
316+
* The host operations acquisition performs, as one replaceable set.
317+
*
318+
* A seam rather than a set of filesystem calls, because the failures that have
319+
* to be proved are of the operations rather than of any particular directory.
320+
* Arranging them with permissions would be arranging the operating system: a
321+
* privileged runner can write where it should not be able to, and Windows does
322+
* not express the same modes at all, so the fault would be the environment's
323+
* rather than the test's.
324+
*/
325+
export interface CredentialOperations {
326+
/** Install the launcher Git will run. */
327+
writeLauncher(path: string, contents: string, executable: boolean): Operation<void>;
328+
/**
329+
* Whether the rejection marker is there.
330+
*
331+
* Absent is the one answer that means "not rejected". A marker this host
332+
* could not read is a marker it does not know about, and reporting that as
333+
* nothing-was-refused would turn a broken filesystem into a clean run.
334+
*/
335+
markerPresent(path: string): Operation<boolean>;
336+
/** Remove the invocation's working directory when it ends. */
337+
removeWorkingDirectory(path: string): Operation<void>;
338+
}
339+
340+
/** What this host actually does, when nothing substitutes one for a test. */
341+
export function denoCredentialOperations(): CredentialOperations {
342+
return {
343+
*writeLauncher(path: string, contents: string, executable: boolean): Operation<void> {
344+
yield* until(writeFile(path, contents, { mode: 0o700 }));
345+
if (executable) {
346+
yield* until(chmod(path, 0o700));
347+
}
348+
},
349+
*markerPresent(path: string): Operation<boolean> {
350+
return yield* until(
351+
stat(path).then(
352+
() => true,
353+
(error: NodeJS.ErrnoException) => {
354+
if (error.code === "ENOENT") {
355+
return false;
356+
}
357+
throw error;
358+
},
359+
),
360+
);
361+
},
362+
*removeWorkingDirectory(path: string): Operation<void> {
363+
yield* until(rm(path, { recursive: true, force: true }));
364+
},
365+
};
366+
}
367+
315368
export interface GitAuthenticationOptions {
316369
/** The environment the ambient mechanisms are found in. */
317370
readonly ambient?: Readonly<Record<string, string | undefined>>;
@@ -343,6 +396,7 @@ export function denoCredentialBroker(
343396
ambient: Readonly<Record<string, string | undefined>> = process.env,
344397
observe: GitAuthenticationObserver = {},
345398
assembly?: HelperAssembly,
399+
operations: CredentialOperations = denoCredentialOperations(),
346400
): CredentialBroker {
347401
return {
348402
lease(request: CredentialRequest): Operation<CredentialLease> {
@@ -356,7 +410,7 @@ export function denoCredentialBroker(
356410
closed = true;
357411
held = undefined;
358412
observe.step?.("released");
359-
yield* until(rm(directory, { recursive: true, force: true }));
413+
yield* operations.removeWorkingDirectory(directory);
360414
observe.step?.("removed");
361415
observe.released?.(directory);
362416
});
@@ -395,11 +449,15 @@ export function denoCredentialBroker(
395449
return;
396450
}
397451

452+
// Installing the launcher is the one thing acquisition does that can
453+
// fail after an identity was proved, and a failure of it is this host
454+
// being unable to give Git a helper — never a credential it lacks.
398455
const launcher = join(directory, launcherName(assembly));
399-
yield* until(writeFile(launcher, launcherProgram(assembly), { mode: 0o700 }));
400-
if (assembly.platform !== "windows") {
401-
yield* until(chmod(launcher, 0o700));
402-
}
456+
yield* operations.writeLauncher(
457+
launcher,
458+
launcherProgram(assembly),
459+
assembly.platform !== "windows",
460+
);
403461
const marker = join(directory, "rejected");
404462

405463
yield* provide({
@@ -413,11 +471,7 @@ export function denoCredentialBroker(
413471
if (closed) {
414472
return false;
415473
}
416-
return yield* until(
417-
stat(marker)
418-
.then(() => true)
419-
.catch(() => false),
420-
);
474+
return yield* operations.markerPresent(marker);
421475
},
422476
attachment: () =>
423477
closed || held === undefined

‎packages/workflow/tests/credential-helper.test.ts‎

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ import {
3131
launcherProgram,
3232
type HelperAssembly,
3333
} from "../src/deno/composition/credential-helper.ts";
34-
import { denoCredentialBroker } from "../src/deno/composition/authentication.ts";
35-
import type { CredentialRequest } from "../src/deno/composition/authentication.ts";
34+
import {
35+
denoCredentialBroker,
36+
denoCredentialOperations,
37+
} from "../src/deno/composition/authentication.ts";
38+
import type {
39+
CredentialOperations,
40+
CredentialRequest,
41+
} from "../src/deno/composition/authentication.ts";
3642
import { TEST_HELPER } from "./support/composition.ts";
3743
import { useInvokingHome } from "./support/credential-home.ts";
3844

@@ -275,7 +281,11 @@ describe("workflow credential acquisition refusals", () => {
275281
[
276282
"#!/bin/sh",
277283
'if [ "$1" != "get" ]; then exit 0; fi',
278-
`cat > ${JSON.stringify(record)}`,
284+
// Reduced to labels here, so what a failure prints is a setting's
285+
// name and a verdict rather than anything the helper was given.
286+
`{ cat; echo "prompt=\${GIT_TERMINAL_PROMPT:-unset}"; ` +
287+
`echo "askpass=\${GIT_ASKPASS-unset}"; ` +
288+
`echo "sshaskpass=\${SSH_ASKPASS-unset}"; } > ${JSON.stringify(record)}`,
279289
`echo username=${HELD.username}`,
280290
`echo password=${HELD.password}`,
281291
"",
@@ -307,6 +317,15 @@ describe("workflow credential acquisition refusals", () => {
307317
expect(asked).toContain("protocol=https");
308318
expect(asked).toContain(`host=${HOST}`);
309319
expect(asked).toContain(`path=${PATH}`);
320+
321+
// And the helper ran with no way to stop the run on a question: terminal
322+
// prompting off, and both askpass hooks present-but-empty rather than
323+
// naming a program somebody's environment chose.
324+
expect(asked).toContain("prompt=0");
325+
expect(asked).toContain("askpass=");
326+
expect(asked).not.toContain("askpass=unset");
327+
expect(asked).toContain("sshaskpass=");
328+
expect(asked).not.toContain("sshaskpass=unset");
310329
});
311330
});
312331

@@ -380,3 +399,95 @@ describe("workflow credential infrastructure failures", () => {
380399
});
381400
});
382401
});
402+
403+
describe("workflow credential injected infrastructure faults", () => {
404+
/** The operations this host performs, each replaceable and each able to fail. */
405+
function operations(broken: string): CredentialOperations {
406+
const real = denoCredentialOperations();
407+
const fault = (named: string) =>
408+
function* (): Operation<never> {
409+
throw new Error(`injected ${named} failure`);
410+
};
411+
return {
412+
writeLauncher: broken === "install" ? fault("install") : real.writeLauncher,
413+
markerPresent: broken === "marker-read" ? fault("marker-read") : real.markerPresent,
414+
removeWorkingDirectory:
415+
broken === "marker-remove" ? fault("marker-remove") : real.removeWorkingDirectory,
416+
};
417+
}
418+
419+
/**
420+
* Each frozen boundary, and what breaking it must produce.
421+
*
422+
* Injected rather than arranged with permissions: a privileged runner can
423+
* write where it should not be able to, and Windows does not express the same
424+
* modes, so a filesystem-arranged fault would be the environment's rather than
425+
* this test's.
426+
*/
427+
const BOUNDARIES = ["install", "marker-read", "marker-remove"] as const;
428+
429+
it("raises rather than reporting a credential this host does not lack", function* () {
430+
const home = yield* useInvokingHome([{ host: HOST, path: PATH, ...HELD }]);
431+
432+
for (const boundary of BOUNDARIES) {
433+
// The try encloses the scope rather than sitting inside it: removal
434+
// happens as the invocation ends, so a failure of it arrives out of
435+
// teardown rather than out of the body.
436+
let raised: unknown;
437+
try {
438+
yield* scoped(function* () {
439+
const lease = yield* denoCredentialBroker(
440+
home.ambient,
441+
{},
442+
TEST_HELPER,
443+
operations(boundary),
444+
).lease(EXACT);
445+
// The marker read fails when it is reached rather than when the
446+
// lease is opened.
447+
if (boundary === "marker-read") {
448+
yield* lease.rejected();
449+
}
450+
});
451+
} catch (error) {
452+
raised = error;
453+
}
454+
455+
expect(raised).toBeDefined();
456+
// Infrastructure, not unavailability: this host failed to provide a
457+
// mechanism, and saying "no credential" would make a broken machine look
458+
// like a clean refusal.
459+
expect(String(raised)).toContain("injected");
460+
// And nothing about a credential travels with it.
461+
expect(String(raised).includes(HELD.password)).toBe(false);
462+
expect(String(raised).includes(HELD.username)).toBe(false);
463+
}
464+
});
465+
466+
it("reads an absent marker as absent, and nothing else as absent", function* () {
467+
const home = yield* useInvokingHome([{ host: HOST, path: PATH, ...HELD }]);
468+
469+
// The shipped operations, which is the code under test here.
470+
yield* scoped(function* () {
471+
const lease = yield* denoCredentialBroker(home.ambient, {}, TEST_HELPER).lease(EXACT);
472+
expect(yield* lease.rejected()).toBe(false);
473+
});
474+
475+
// A read that fails for any other reason is raised. Only absence is an
476+
// answer, because only absence means nothing was refused.
477+
const raised = yield* scoped(function* () {
478+
try {
479+
const lease = yield* denoCredentialBroker(
480+
home.ambient,
481+
{},
482+
TEST_HELPER,
483+
operations("marker-read"),
484+
).lease(EXACT);
485+
yield* lease.rejected();
486+
return undefined;
487+
} catch (error) {
488+
return error;
489+
}
490+
});
491+
expect(String(raised)).toContain("injected marker-read failure");
492+
});
493+
});

0 commit comments

Comments
 (0)