Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 49 additions & 1 deletion dashboard/e2e/infra-security.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test, type Page, type Route } from "@playwright/test";
import { shoot } from "./infra-shots";
import { ACCOUNT_MISSING, INFRA_FEEDS, INFRA_INSIGHTS, INFRA_RESOURCES, infraFinding, infraOverview } from "./infra-data";
import { ACCOUNT_MISSING, INFRA_FEEDS, INFRA_INSIGHTS, INFRA_RESOURCES, infraFinding, infraOverview, iso } from "./infra-data";

// Infrastructure vulnerabilities: Security › Infrastructure grouped by fix
// (pull requests, runbook cards, AWS-managed rows collapsed at the bottom),
Expand Down Expand Up @@ -139,6 +139,54 @@ test.describe("Infrastructure vulnerabilities", () => {
expect(call(db, "POST /api/security/infrastructure/check")).toHaveLength(1);
});

test("a pull request a person closed shows who closed it and when, and the fix is offered again", async ({ page }) => {
const closed = { number: 412, url: "https://github.com/acme/infra/pull/412", state: "closed", closed_at: iso(-3), closed_by: "octo", closed_by_person: true };
const ng = infraFinding({ id: "f_ng", name: "prod-ng", title: "EKS node group prod-ng runs v20261015 (fixed in v20261031)", tier: "P2", status: "open", pr: closed });
const db = await mockApi(page, { overview: infraOverview({ findings: [ng] }) });
await page.goto("/security/infrastructure");
const row = page.getByRole("list", { name: "Infrastructure findings" }).getByTestId("infra-row");
await expect(row).toHaveCount(1);
await expect(row.getByTestId("infra-pr")).toHaveAttribute("href", "https://github.com/acme/infra/pull/412");
await expect(row.getByTestId("infra-pr")).toHaveText(/PR #412\s*closed by octo on \w+ \d+/);
await expect(row.getByTestId("infra-pr-closed")).toHaveText(/PR #412 was closed by octo on \w+ \d+ without merging\. Open a new one when you want the fix/);
// The fix is offered again, and asking starts it.
await expect(row.getByTestId("infra-fix")).toHaveText("Open fix PR");
await row.getByTestId("infra-fix").click();
await expect(row).toContainText("Fix run started.");
await expect(row.getByTestId("infra-pr-closed")).toHaveCount(0);
expect(call(db, "POST /api/security/infrastructure/findings/f_ng/fix")).toHaveLength(1);

// An older server sends no closer: the page says closed, and still offers the fix.
db.overview = infraOverview({ findings: [{ ...ng, pr: { number: 412, url: closed.url, state: "closed" } }] });
await page.reload();
await expect(row.getByTestId("infra-pr-closed")).toHaveText(/^PR #412 was closed without merging\./);
await expect(row.getByTestId("infra-fix")).toBeVisible();

// Still open, merged, or held as a draft by the replacement check: the link, no button.
for (const state of ["open", "merged", "draft"]) {
db.overview = infraOverview({ findings: [{ ...ng, status: state === "draft" ? "open" : "pr_open", pr: { number: 412, url: closed.url, state }, fix: { ...ng.fix, kind: state === "draft" ? "runbook" : "pr" } }] });
await page.reload();
await expect(page.getByTestId("infra-row")).toHaveCount(1);
await expect(page.getByTestId("infra-fix")).toHaveCount(0);
await expect(page.getByTestId("infra-pr-closed")).toHaveCount(0);
}

// A closed draft on a finding that fell back to its runbook stays a runbook: no button.
db.overview = infraOverview({ findings: [{ ...ng, pr: closed, fix: { ...ng.fix, kind: "runbook" } }] });
await page.reload();
await expect(page.getByTestId("infra-row")).toHaveCount(1);
await expect(page.getByTestId("infra-fix")).toHaveCount(0);
await expect(page.getByTestId("infra-pr-closed")).toHaveCount(0);
});

test("a member sees who closed the pull request and no button", async ({ page }) => {
const closed = { number: 412, url: "https://github.com/acme/infra/pull/412", state: "closed", closed_at: iso(-3), closed_by: "octo", closed_by_person: true };
await mockApi(page, { role: "member", overview: infraOverview({ findings: [infraFinding({ id: "f_ng", name: "prod-ng", status: "open", pr: closed })] }) });
await page.goto("/security/infrastructure");
await expect(page.getByTestId("infra-pr-closed")).toContainText("An admin can open a new one.");
await expect(page.getByTestId("infra-fix")).toHaveCount(0);
});

test("an account switched on without the reads warns, naming the feature, with the one-click update", async ({ page }) => {
await mockApi(page, { overview: infraOverview({ accounts: [...infraOverview().accounts, ACCOUNT_MISSING] }) });
await page.goto("/security/infrastructure");
Expand Down
16 changes: 13 additions & 3 deletions dashboard/src/app/security/infrastructure/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Disclosure } from "@/components/disclosure";
import { AwsIcon } from "@/components/icons";
import { Chip, List, ListRow, ListSkeleton, StatusToggles, Time, type Tone } from "@/components/list";
import { linkButton, primaryButton, smallSecondaryButton } from "@/components/ui";
import { SECURITY_ADMIN, TIERS, type Tier } from "@/lib/security";
import { SECURITY_ADMIN, TIERS, closedWords, type Tier } from "@/lib/security";
import {
INFRA_STATUS_LOOK,
countWords,
Expand Down Expand Up @@ -284,7 +284,11 @@ function FindingRow({ f, admin, dryRun, defaultOpen }: { f: InfraFinding; admin:
}
setAsking(false);
};
const prWords = f.pr ? (f.pr.state === "merged" ? "merged, waiting for the new version to run" : f.pr.state) : null;
const closed = f.pr?.state === "closed";
const prWords = f.pr ? (f.pr.state === "merged" ? "merged, waiting for the new version to run" : closed ? closedWords(f.pr) : f.pr.state) : null;
// A pull request closed without merging is history, not a fix on its way: the fix is offered again.
// (A finding the replacement check moved to its runbook is no longer a "pr" fix, so it gets no button.)
const canAsk = admin && f.fix.kind === "pr" && f.status === "open" && (!f.pr || closed) && !asked;
return (
<ListRow
testId="infra-row"
Expand Down Expand Up @@ -320,7 +324,7 @@ function FindingRow({ f, admin, dryRun, defaultOpen }: { f: InfraFinding; admin:
</>
}
actions={
admin && f.fix.kind === "pr" && !f.pr && f.status === "open" && !asked ? (
canAsk ? (
<button type="button" className={smallSecondaryButton} onClick={askFix} disabled={asking} data-testid="infra-fix" title={dryRun ? "Opens one now, even in a dry run" : undefined}>
{asking ? "Starting…" : "Open fix PR"}
</button>
Expand All @@ -329,6 +333,12 @@ function FindingRow({ f, admin, dryRun, defaultOpen }: { f: InfraFinding; admin:
details={<FindingDetails f={f} />}
detailsFor={`details of ${f.name}`}
>
{closed && f.pr && f.status === "open" && f.fix.kind === "pr" && !asked && (
<p className="mt-1 text-xs text-zinc-300" data-testid="infra-pr-closed">
PR #{f.pr.number} was {closedWords(f.pr)} without merging.{" "}
{admin ? "Open a new one when you want the fix; Coderhelm does not open one by itself for a week after a pull request is closed." : "An admin can open a new one."}
</p>
)}
{asked && <p className="mt-1 text-xs text-sky-300">{asked}</p>}
</ListRow>
);
Expand Down
11 changes: 10 additions & 1 deletion dashboard/src/lib/infra-security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ export interface InfraFinding {
tier: Tier | null;
fix: InfraFix;
status: InfraStatus;
pr: { number: number; url: string; state: "open" | "merged" | "closed" | "draft" } | null;
pr: {
number: number;
url: string;
state: "open" | "merged" | "closed" | "draft";
/** Closed without merging: when, and who closed it (a GitHub login, or the person who closed its run). Absent while open, and on older servers. */
closed_at?: string;
closed_by?: string;
/** False when Coderhelm closed it itself. */
closed_by_person?: boolean;
} | null;
inspector?: { exploit_available: boolean; reachable: boolean | null; findings?: number } | null;
first_seen_at: string;
updated_at: string;
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/lib/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ export function runsHref(ticket: string): string {
}

/** "closed by octo on Nov 13": a pull request that closed without merging, with who and when as far as the server knows. */
export function closedWords(pr: NonNullable<Finding["pr"]>): string {
export function closedWords(pr: { closed_at?: string; closed_by?: string; closed_by_person?: boolean }): string {
const who = pr.closed_by_person === false ? "Coderhelm" : pr.closed_by;
const t = pr.closed_at ? new Date(pr.closed_at).getTime() : NaN;
const on = Number.isNaN(t) ? "" : ` on ${new Date(t).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`;
Expand Down
Loading