diff --git a/.bifrost/workflows.yaml b/.bifrost/workflows.yaml index 57e186f..463bbed 100644 --- a/.bifrost/workflows.yaml +++ b/.bifrost/workflows.yaml @@ -336,3 +336,15 @@ workflows: access_level: role_based role_names: ["Bifrost Docs Administrator"] timeout_seconds: 1800 + + 4cd9ecbe-462d-49fc-b785-19bfdf5d433b: + id: 4cd9ecbe-462d-49fc-b785-19bfdf5d433b + name: docs_list_expirations + path: functions/expirations.py + function_name: docs_list_expirations + type: workflow + description: Read one bounded page of explicitly flagged asset expiration dates for an authorized organization. + category: Bifrost Docs + access_level: role_based + role_names: ["Bifrost Docs Reader", "Bifrost Docs Editor", "Bifrost Docs Administrator"] + timeout_seconds: 60 diff --git a/README.md b/README.md index 016892d..8c5a18a 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,40 @@ Native documentation works without IT Glue. Installers configure only the roles the Bifrost platform resources needed for their organization. IT Glue is an optional one-way migration source for teams that have an existing IT Glue account. +## MTG feature development + +This fork develops Skra's operational documentation features on top of the +upstream Bifrost Solution. Its first slice adds expiration tracking. In Settings → +Asset types, enable **Track as an expiration date** on a Date field, then enter a +`YYYY-MM-DD` value on an asset. The dashboard shows overdue and upcoming dates for +the selected organization, with 7, 14, 30, and 90 day windows. Ordinary dates are +excluded unless explicitly flagged. Archived/disabled assets and inactive types +are excluded; the caller's existing table policies still govern restricted rows. + +The dashboard scans 100 assets per request. Use **Check next 100 assets** until the +scan is complete; dates are ordered across the assets checked so far, not across +unread pages. Refresh starts a new scan. Invalid nonempty flagged date values are +counted rather than treated as valid dates. Dates use the current UTC calendar day. +This slice does not send reminders or change schedules, secrets, or table policies. +Mapped IT Glue schemas retain their existing source-overwrite behavior; an upstream +schema change can replace locally added expiration flags. + +Next slices are deduplicated expiration notifications, attributed SOP checklists, +favorites and saved preferences, and field-level sync conflict review. Keep each +slice independently reviewable and preserve customer/provider isolation. + +Production releases should use reviewed release tags in this fork. Connecting an +install to Git changes its source owner; it does not deploy a feature branch. +Advance the configured release ref and sync only after release acceptance. Preserve +the upstream slug and manifest identities so the existing install retains its data. + ## Install Use Bifrost CLI 1.4.2 or later against the intended Bifrost instance: ```bash -git clone https://github.com/gobifrost/docs.git -cd docs +git clone https://github.com/MTG-Thomas/bifrost-docs-solution.git +cd bifrost-docs-solution bifrost solution deploy . --global ``` diff --git a/apps/bifrost-docs/src/components/AssetTypeEditor.test.ts b/apps/bifrost-docs/src/components/AssetTypeEditor.test.ts index fa4dbd6..9ad238b 100644 --- a/apps/bifrost-docs/src/components/AssetTypeEditor.test.ts +++ b/apps/bifrost-docs/src/components/AssetTypeEditor.test.ts @@ -2,6 +2,14 @@ import { describe, expect, it } from "vitest"; import { normalizeAssetTypeFields, safeAssetTypeFields, serializeAssetTypeFields } from "./AssetTypeEditor"; describe("asset type schema helpers", () => { + it("round-trips expiration flags and clears them when a date becomes text", () => { + const source = [{ attributes: { "name-key": "renewal", kind: "date", name: "Renewal", is_expiration: true } }]; + const fields = normalizeAssetTypeFields(source); + expect(fields[0].is_expiration).toBe(true); + expect(serializeAssetTypeFields(fields)).toEqual(source); + expect(serializeAssetTypeFields([{ ...fields[0], type: "text" }])[0]).toEqual({ attributes: expect.objectContaining({ kind: "text", is_expiration: false }) }); + expect(serializeAssetTypeFields([{ ...fields[0], is_expiration: false }])[0]).toEqual({ attributes: expect.objectContaining({ is_expiration: false }) }); + }); it("round-trips imported protected and unsupported definitions without converting them", () => { const source = [ { attributes: { name: "Host Name", kind: "text", required: true, options: null } }, diff --git a/apps/bifrost-docs/src/components/AssetTypeEditor.tsx b/apps/bifrost-docs/src/components/AssetTypeEditor.tsx index aea659c..04abf28 100644 --- a/apps/bifrost-docs/src/components/AssetTypeEditor.tsx +++ b/apps/bifrost-docs/src/components/AssetTypeEditor.tsx @@ -6,7 +6,7 @@ import { BfCheckbox } from "@/components/bifrost/BfSelection"; import { BfSelect, BfTextField, BfTextarea } from "@/components/bifrost/BfField"; export type AssetFieldType = "text" | "textbox" | "number" | "date" | "checkbox" | "select" | "header" | "url" | "tag" | "upload" | "password" | "totp" | "unknown"; -type SourceState = Pick; +type SourceState = Pick; export type AssetTypeField = { key: string; @@ -14,6 +14,7 @@ export type AssetTypeField = { type: AssetFieldType; required: boolean; show_in_list: boolean; + is_expiration?: boolean; default_value: string | null; options: string[] | null; /** Original source type, retained for diagnostics and exact serialization. */ @@ -85,6 +86,7 @@ function stateFor(field: AssetTypeField): SourceState { type: field.type, required: field.required, show_in_list: field.show_in_list, + is_expiration: field.is_expiration, default_value: field.default_value, options: field.options ? [...field.options] : null, }; @@ -108,6 +110,7 @@ function serializedField(field: AssetTypeField): Record { setAliased(attrs, ["type", "kind"], field.type, "type"); setAliased(attrs, ["required"], field.required, "required"); setAliased(attrs, ["show_in_list", "show-in-list"], field.show_in_list, "show_in_list"); + if (field.is_expiration || "is_expiration" in attrs) attrs.is_expiration = field.type === "date" && field.is_expiration === true; setAliased(attrs, ["default_value", "default-value"], field.default_value, "default_value"); setAliased(attrs, ["options"], field.type === "select" ? field.options?.filter(Boolean) ?? [] : null, "options"); return source; @@ -131,6 +134,7 @@ export function normalizeAssetTypeFields(raw: unknown): AssetTypeField[] { type: kind.type, required: Boolean(attrs.required), show_in_list: Boolean(attrs.show_in_list ?? attrs["show-in-list"]), + is_expiration: attrs.is_expiration === true, default_value: (attrs.default_value ?? attrs["default-value"]) == null ? null : String(attrs.default_value ?? attrs["default-value"]), options: optionsFrom(attrs.options), sourceType: kind.sourceType, @@ -215,6 +219,7 @@ export function AssetTypeEditor({ initial, onSave, onCancel, saving = false, sou {field.type === "select" && updateField(index, { options: event.target.value.split("\n").map((option) => option.trim()).filter(Boolean) })} />} {field.type !== "header" && field.type !== "upload" && updateField(index, { default_value: event.target.value || null })} />} {field.type === "upload" &&

Upload references are managed with attachments.

} + {field.type === "date" && updateField(index, { is_expiration })}>Track as an expiration date}
updateField(index, { required })}>Required updateField(index, { show_in_list })}>Show in list
}
} onClick={() => move(index, -1)}>Up} onClick={() => move(index, 1)}>Down} onClick={() => update({ fields: values.fields.filter((_, position) => position !== index) })}>Remove
; diff --git a/apps/bifrost-docs/src/components/UpcomingExpirations.test.tsx b/apps/bifrost-docs/src/components/UpcomingExpirations.test.tsx new file mode 100644 index 0000000..0031cee --- /dev/null +++ b/apps/bifrost-docs/src/components/UpcomingExpirations.test.tsx @@ -0,0 +1,68 @@ +import "@testing-library/jest-dom/vitest"; +import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { MemoryRouter } from "react-router-dom"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const mocks = vi.hoisted(() => ({ + scope: { organizationId: "a", viewerId: "user-a", viewerSettled: true, loading: false, error: "" }, + mutate: vi.fn(), +})); +vi.mock("bifrost", () => ({ useWorkflowMutation: () => ({ mutate: mocks.mutate }) })); +vi.mock("./layout/useDocsOrganizations", () => ({ useDocsOrganizations: () => mocks.scope })); +import { UpcomingExpirations } from "./UpcomingExpirations"; + +const item = { asset_id: "asset-a", asset_name: "Firewall", asset_type_id: "type-a", asset_type_name: "License", organization_id: "a", field_key: "renewal", field_name: "Renewal", expires_on: "2026-10-15", days_until: 1 }; +const page = { items: [item], scanned: 1, invalid_dates: 0, next_cursor: null, as_of: "2026-10-14", within_days: 30 }; +function mount() { return render(); } +beforeEach(() => { + mocks.scope = { organizationId: "a", viewerId: "user-a", viewerSettled: true, loading: false, error: "" }; + mocks.mutate.mockReset().mockResolvedValue(page); +}); +afterEach(cleanup); + +describe("UpcomingExpirations", () => { + it("loads scoped dates with an actionable asset link and window selection", async () => { + mount(); + expect(await screen.findByRole("link", { name: "Firewall" })).toHaveAttribute("href", "/org/a/assets/type-a/asset-a"); + expect(screen.getByText("Expires in 1 day")).toBeVisible(); + expect(mocks.mutate).toHaveBeenCalledWith({ organization_id: "a", within_days: 30, cursor: "" }); + fireEvent.change(screen.getByLabelText("Due within"), { target: { value: "7" } }); + await waitFor(() => expect(mocks.mutate).toHaveBeenLastCalledWith({ organization_id: "a", within_days: 7, cursor: "" })); + }); + it("does not execute with no selected organization or unsettled identity", () => { + mocks.scope.organizationId = ""; + const view = mount(); + expect(screen.getByText("Select an organization to view its expiration dates.")).toBeVisible(); + expect(mocks.mutate).not.toHaveBeenCalled(); + mocks.scope.organizationId = "a"; + mocks.scope.viewerSettled = false; + view.rerender(); + expect(mocks.mutate).not.toHaveBeenCalled(); + }); + it("does not label an incomplete empty page as no expirations", async () => { + mocks.mutate.mockResolvedValueOnce({ ...page, items: [], scanned: 100, next_cursor: "cursor-a" }).mockResolvedValueOnce(page); + mount(); + expect(await screen.findByText("No matching dates in the assets checked so far.")).toBeVisible(); + fireEvent.click(screen.getByRole("button", { name: "Check next 100 assets" })); + expect(await screen.findByRole("link", { name: "Firewall" })).toBeVisible(); + expect(screen.getByText(/Checked 101 assets/)).toBeVisible(); + expect(mocks.mutate).toHaveBeenLastCalledWith({ organization_id: "a", within_days: 30, cursor: "cursor-a" }); + }); + it("shows errors with retry and date validation warnings", async () => { + mocks.mutate.mockRejectedValueOnce(new Error("unavailable")).mockResolvedValueOnce({ ...page, invalid_dates: 2 }); + mount(); + fireEvent.click(await screen.findByRole("button", { name: "Retry" })); + expect(await screen.findByText("Some dates need attention")).toBeVisible(); + }); + it("hides previous account data immediately and ignores its delayed result", async () => { + let resolve!: (value: typeof page) => void; + mocks.mutate.mockReturnValueOnce(new Promise(result => { resolve = result; })); + const view = mount(); + mocks.scope = { ...mocks.scope, organizationId: "b", viewerId: "user-b" }; + mocks.mutate.mockResolvedValue({ ...page, items: [] }); + view.rerender(); + await act(async () => { resolve(page); }); + expect(screen.queryByRole("link", { name: "Firewall" })).not.toBeInTheDocument(); + expect(await screen.findByText(/No matching expiration dates/)).toBeVisible(); + }); +}); diff --git a/apps/bifrost-docs/src/components/UpcomingExpirations.tsx b/apps/bifrost-docs/src/components/UpcomingExpirations.tsx new file mode 100644 index 0000000..e88fe49 --- /dev/null +++ b/apps/bifrost-docs/src/components/UpcomingExpirations.tsx @@ -0,0 +1,83 @@ +import { useCallback, useEffect, useRef, useState } from "react"; +import { Link } from "react-router-dom"; +import { useWorkflowMutation } from "bifrost"; +import { useDocsOrganizations } from "./layout/useDocsOrganizations"; +import { BfAlert } from "./bifrost/BfAlert"; +import { BfButton } from "./bifrost/BfButton"; +import { BfSelect } from "./bifrost/BfField"; + +type Expiration = { + asset_id: string; asset_name: string; asset_type_id: string; asset_type_name: string; organization_id: string; + field_key: string; field_name: string; expires_on: string; days_until: number; +}; +type Page = { items: Expiration[]; scanned: number; invalid_dates: number; next_cursor: string | null; as_of: string; within_days: number }; +type Snapshot = { key: string; page: Page | null; loading: boolean; error: string; failedCursor: string }; +const WORKFLOW = "functions/expirations.py::docs_list_expirations"; + +function timing(days: number) { + return days < 0 ? `Expired ${-days} ${days === -1 ? "day" : "days"} ago` : days === 0 ? "Expires today" : `Expires in ${days} ${days === 1 ? "day" : "days"}`; +} + +export function UpcomingExpirations() { + const { organizationId, viewerId, viewerSettled, loading: scopeLoading, error: scopeError } = useDocsOrganizations(); + const [withinDays, setWithinDays] = useState(30); + const workflow = useWorkflowMutation(WORKFLOW); + const mutate = useRef(workflow.mutate); + mutate.current = workflow.mutate; + const key = `${viewerId ?? ""}:${organizationId}:${withinDays}:${viewerSettled}:${scopeLoading}:${scopeError}`; + const activeKey = useRef(key); + activeKey.current = key; + const request = useRef(0); + const [snapshot, setSnapshot] = useState({ key: "", page: null, loading: false, error: "", failedCursor: "" }); + const current = snapshot.key === key ? snapshot : null; + const ready = Boolean(organizationId && viewerId && viewerSettled && !scopeLoading && !scopeError); + + const load = useCallback(async (cursor = "") => { + const expectedKey = key; + const attempt = ++request.current; + setSnapshot(previous => ({ key, page: cursor && previous.key === key ? previous.page : null, loading: true, error: "", failedCursor: cursor })); + try { + const page = await mutate.current({ organization_id: organizationId, within_days: withinDays, cursor }); + if (activeKey.current !== expectedKey || request.current !== attempt) return; + if (page.next_cursor && page.next_cursor === cursor) throw new Error("The next asset page repeated. Refresh to restart."); + setSnapshot(previous => { + const prior = cursor && previous.key === key ? previous.page : null; + const items = new Map([...(prior?.items ?? []), ...page.items].map(item => [`${item.asset_id}:${item.field_key}`, item])); + return { key, loading: false, error: "", failedCursor: "", page: { + ...page, items: [...items.values()].sort((a, b) => a.expires_on.localeCompare(b.expires_on) || a.asset_id.localeCompare(b.asset_id) || a.field_key.localeCompare(b.field_key)), + scanned: (prior?.scanned ?? 0) + page.scanned, + invalid_dates: (prior?.invalid_dates ?? 0) + page.invalid_dates, + } }; + }); + } catch { + if (activeKey.current === expectedKey && request.current === attempt) { + setSnapshot(previous => ({ ...previous, loading: false, error: "Expiration dates could not load. Try again.", failedCursor: cursor })); + } + } + }, [key, organizationId, withinDays]); + + useEffect(() => { + if (ready) void load(); + return () => { request.current += 1; }; + }, [load, ready]); + + return
+
+

Upcoming expirations

Expired assets and flagged dates due soon in the selected organization.

+ {ready &&
({ value: String(days), label: `${days} days` }))} onChange={event => setWithinDays(Number(event.target.value))} /> void load()}>Refresh
} +
+ {!ready ?

{scopeError ? "Organization scope is unavailable. Retry using the organization selector." : scopeLoading || !viewerSettled ? "Loading organization scope…" : "Select an organization to view its expiration dates."}

: <> + {current?.error && {current.error} void load(current.failedCursor)}>Retry} + {(!current || current.loading) &&

Loading expiration dates…

} + {current?.page && <> + {current.page.items.length > 0 ?
    {current.page.items.map(item =>
  • +
    {item.asset_name}

    {item.asset_type_name} · {item.field_name}

    +
    {timing(item.days_until)}
    +
  • )}
:

{current.page.next_cursor ? "No matching dates in the assets checked so far." : "No matching expiration dates. In Settings → Asset types, mark a Date field as an expiration date and set its value on an asset."}

} +

Checked {current.page.scanned} assets as of {current.page.as_of} UTC.{current.page.next_cursor ? " Results are incomplete; check the next assets to find additional dates." : " Scan complete."}

+ {current.page.invalid_dates > 0 && {current.page.invalid_dates} flagged values are not valid YYYY-MM-DD dates and were excluded.} + {current.page.next_cursor && !current.error && void load(current.page!.next_cursor!)}>Check next 100 assets} + } + } +
; +} diff --git a/apps/bifrost-docs/src/pages/DashboardPage.test.tsx b/apps/bifrost-docs/src/pages/DashboardPage.test.tsx index 0dd042d..bdc7698 100644 --- a/apps/bifrost-docs/src/pages/DashboardPage.test.tsx +++ b/apps/bifrost-docs/src/pages/DashboardPage.test.tsx @@ -15,6 +15,7 @@ vi.mock("@/components/layout/useDocsOrganizations", () => ({ useDocsOrganizations: () => ({ organizations: mocks.organizations }), })); vi.mock("@/components/layout/recentRecords", () => ({ useRecentRecords: () => mocks.recent })); +vi.mock("@/components/UpcomingExpirations", () => ({ UpcomingExpirations: () =>
})); import { DashboardPage } from "./DashboardPage"; diff --git a/apps/bifrost-docs/src/pages/DashboardPage.tsx b/apps/bifrost-docs/src/pages/DashboardPage.tsx index f6930a3..ca1b1d8 100644 --- a/apps/bifrost-docs/src/pages/DashboardPage.tsx +++ b/apps/bifrost-docs/src/pages/DashboardPage.tsx @@ -3,6 +3,7 @@ import { Building2, FileText, KeyRound, Layers, MapPin, Server, type LucideIcon import { Card, CardContent } from "@/components/ui/card"; import { useDocsOrganizations } from "@/components/layout/useDocsOrganizations"; import { useRecentRecords, type RecentRecord } from "@/components/layout/recentRecords"; +import { UpcomingExpirations } from "@/components/UpcomingExpirations"; const RECORD_TYPES: Record = { documents: { icon: FileText, label: "Document", route: "documents" }, @@ -44,6 +45,7 @@ export function DashboardPage() { return (

Dashboard

Welcome to Bifrost Docs.

+ {recentOrganizations.length > 0 &&

Recent Organizations

{recentOrganizations.map((record) => )}
} {recentItems.length > 0 &&

Recent Items

{recentItems.map((record) => )}
}

Select an organization from the header to get started, or Browse organizations.

diff --git a/bifrost.solution.yaml b/bifrost.solution.yaml index 3794ed8..b08e2ca 100644 --- a/bifrost.solution.yaml +++ b/bifrost.solution.yaml @@ -1,5 +1,5 @@ slug: bifrost-docs name: Bifrost Docs -version: 0.1.3 +version: 0.1.3+mtg.1 allow_outbound_access: false allow_inbound_access: true diff --git a/docs/CUTOVER_RUNBOOK.md b/docs/CUTOVER_RUNBOOK.md index 9c66530..1f55522 100644 --- a/docs/CUTOVER_RUNBOOK.md +++ b/docs/CUTOVER_RUNBOOK.md @@ -48,3 +48,32 @@ Rollback changes routing and access; it does not delete migrated data. Integration credentials and mappings before relying on a restored environment. 5. Classify and repair the blocking condition, repeat the required non-production proof, and schedule a new go/no-go decision. + +## Attachments that require browser authentication + +IT Glue can return a tenant web link such as `/attachments/` in an +attachment's `download-url`, rather than an API-accessible or signed file URL. +A successful metadata request does not prove that this link can download the +file. Refreshing the parent or attachment metadata can return the same link. + +If the link redirects to sign-in, migration reports HTTP 401 with +`attachment download requires browser authentication`. It stops before following +the sign-in page or uploading file bytes. API keys remain restricted to the API +origin; do not forward them or browser cookies to arbitrary download hosts. +The HTML integrity check remains enabled for all other downloads. + +Keep these items failed and resumable. Do not mark them imported, skip them to +make proof green, or upload the sign-in HTML as the original attachment. +For CLI-only recovery, obtain an approved export or downloaded original file and +record the exact source organization, parent resource type/ID, attachment ID, +original filename, declared MIME type/size, and local file SHA-256 in a private +change record. Verify file type and size against fresh metadata and review any +difference before applying a recovery. Never commit customer files or signed +URLs. A filename match alone is insufficient source-identity evidence. + +The current API migration does not accept local-file overrides. Downloading a +file alone therefore does not repair the failed item: a reviewed recovery path +must preserve the attachment's existing deterministic identity, organization, +parent and restriction policy, complete managed-file registration, verify stored +bytes/hash, and retain an auditable migration result. Re-run reconciliation only +after that recovery is implemented and verified. diff --git a/functions/expirations.py b/functions/expirations.py new file mode 100644 index 0000000..6daefcc --- /dev/null +++ b/functions/expirations.py @@ -0,0 +1,79 @@ +"""Bounded, caller-authorized expiration reads for the Docs dashboard.""" + +from datetime import datetime, timezone +from typing import Any +from uuid import UUID + +from bifrost import UserError, context, tables, workflow + +from modules.expiration import project_expirations + +ASSETS = "docs-flexible-assets" +TYPES = "docs-flexible-asset-types" +PAGE_SIZE = 100 + + +@workflow( + name="docs_list_expirations", + description="Read one bounded page of explicitly flagged asset expiration dates for an authorized organization.", + category="Bifrost Docs", +) +async def docs_list_expirations( + organization_id: str, within_days: int = 30, cursor: str = "", +) -> dict[str, Any]: + """Scan 100 assets per call. A next_cursor means results are incomplete.""" + if not isinstance(organization_id, str) or not organization_id or len(organization_id) > 128: + raise UserError("Select an organization to view expirations") + caller_org = str(getattr(context, "org_id", None) or "") + provider = bool( + getattr(context, "is_platform_admin", False) + or getattr(getattr(context, "organization", None), "is_provider", False) + ) + if not provider and (not caller_org or organization_id != caller_org): + raise UserError("You can view expirations only for your own organization") + if type(within_days) is not int or not 1 <= within_days <= 365: + raise UserError("within_days must be an integer between 1 and 365") + if not isinstance(cursor, str): + raise UserError("Invalid expiration cursor") + if cursor: + try: + UUID(cursor) + except ValueError as exc: + raise UserError("Invalid expiration cursor") from exc + + page = await tables.query( + ASSETS, where={"organization_id": organization_id}, scope=organization_id, + limit=PAGE_SIZE, after_document_id=cursor, skip_count=True, + ) + rows = list(page.documents) + type_ids = sorted({ + row.data.get("flexible_asset_type_id") for row in rows + if isinstance(row.data, dict) + and row.data.get("organization_id") == organization_id + and isinstance(row.data.get("flexible_asset_type_id"), str) + and row.data.get("flexible_asset_type_id") + }) + types: dict[str, dict[str, Any]] = {} + if type_ids: + type_page = await tables.query( + TYPES, where={"organization_id": organization_id}, scope=organization_id, + document_ids=type_ids, limit=PAGE_SIZE, skip_count=True, + ) + types = {str(row.id): row.data for row in type_page.documents if isinstance(row.data, dict)} + today = datetime.now(timezone.utc).date() + items: list[dict[str, Any]] = [] + invalid_dates = 0 + for row in rows: + asset = row.data if isinstance(row.data, dict) else {} + projected, invalid = project_expirations( + str(row.id), asset, types.get(asset.get("flexible_asset_type_id"), {}), + organization_id=organization_id, today=today, within_days=within_days, + ) + items.extend(projected) + invalid_dates += invalid + items.sort(key=lambda item: (item["expires_on"], item["asset_id"], item["field_key"])) + return { + "items": items, "scanned": len(rows), "invalid_dates": invalid_dates, + "next_cursor": str(rows[-1].id) if len(rows) == PAGE_SIZE else None, + "as_of": today.isoformat(), "within_days": within_days, + } diff --git a/modules/expiration.py b/modules/expiration.py new file mode 100644 index 0000000..cfc8273 --- /dev/null +++ b/modules/expiration.py @@ -0,0 +1,64 @@ +"""Safe, date-only projections for explicitly flagged asset expiration fields.""" + +from datetime import date +from typing import Any + + +def project_expirations( + asset_id: str, asset: dict[str, Any], asset_type: dict[str, Any], + *, organization_id: str, today: date, within_days: int, +) -> tuple[list[dict[str, Any]], int]: + """Return selected date fields only; never return traits or raw source data.""" + if ( + asset.get("organization_id") != organization_id + or asset_type.get("organization_id") != organization_id + or asset.get("archived") is True + or asset.get("is_enabled") is False + or asset_type.get("active") is False + ): + return [], 0 + fields = asset_type.get("fields") + traits = asset.get("traits") + if not isinstance(fields, list) or not isinstance(traits, dict): + return [], 0 + items: list[dict[str, Any]] = [] + invalid = 0 + seen: set[str] = set() + for raw in fields: + if not isinstance(raw, dict): + continue + field = raw.get("attributes", raw) + if not isinstance(field, dict): + continue + if field.get("type", field.get("kind")) != "date" or field.get("is_expiration") is not True: + continue + key = field.get("key", field.get("name-key", field.get("name_key"))) + if not isinstance(key, str) or not key or key in seen: + continue + seen.add(key) + value = traits.get(key) + if value is None or value == "": + continue + try: + # Date fields are calendar dates, not instants in a browser timezone. + if not isinstance(value, str) or len(value) != 10: + raise ValueError("Expected an ISO calendar date") + expires = date.fromisoformat(value) + except ValueError: + invalid += 1 + continue + days = (expires - today).days + if days > within_days: + continue + items.append({ + "asset_id": asset_id, + "asset_name": str(asset.get("name") or "Unnamed asset"), + "asset_type_name": str(asset_type.get("name") or "Asset"), + "asset_type_id": str(asset.get("flexible_asset_type_id") or ""), + "organization_id": organization_id, + "field_key": key, + "field_name": str(field.get("name") or key), + "expires_on": expires.isoformat(), + "days_until": days, + }) + return items, invalid diff --git a/modules/itglue_api.py b/modules/itglue_api.py index c6458b3..71cbe11 100644 --- a/modules/itglue_api.py +++ b/modules/itglue_api.py @@ -352,6 +352,13 @@ async def _fetch_response( if not redirect: raise ITGlueError("IT Glue file download redirected without a location") resolved_source = urljoin(resolved_source, redirect) + if _is_browser_authentication_url(resolved_source): + raise ITGlueError( + "[xfer-v7] IT Glue attachment download requires browser authentication; " + "API-key access did not provide file bytes. " + "Supply an approved export or downloaded file for this attachment.", + status_code=401, + ) headers = ( {"x-api-key": self._api_key} if (urlparse(resolved_source).scheme, urlparse(resolved_source).netloc) @@ -466,6 +473,14 @@ async def chunks() -> AsyncIterator[bytes]: return transferred, digest.hexdigest() +def _is_browser_authentication_url(value: str) -> bool: + """Recognize sign-in redirects without logging URLs or following interactive auth.""" + path = urlparse(value).path.rstrip("/").casefold() + return any(path.endswith(suffix) for suffix in ( + "/login", "/sign_in", "/signin", "/oauth2/authorize", "/oauth2/v2.0/authorize", + )) + + def _as_int(value: Any) -> int | None: try: return int(value) if value is not None else None diff --git a/tests/test_expiration.py b/tests/test_expiration.py new file mode 100644 index 0000000..c516130 --- /dev/null +++ b/tests/test_expiration.py @@ -0,0 +1,44 @@ +from datetime import date + +from modules.expiration import project_expirations + +# Value of a non-date trait that must never appear in a projection. +NOT_PROJECTED = "projection-sentinel" + + +def project(*, asset_changes=None, fields=None, traits=None, type_changes=None): + return project_expirations( + "asset-a", {"organization_id": "a", "name": "Firewall", "traits": traits or {"renewal": "2026-10-15"}, **(asset_changes or {})}, + {"organization_id": "a", "name": "License", "fields": fields if fields is not None else [{"key": "renewal", "type": "date", "is_expiration": True}], **(type_changes or {})}, + organization_id="a", today=date(2026, 10, 14), within_days=30, + ) + + +def test_dates_are_opt_in_and_projection_does_not_include_secrets(): + items, invalid = project(traits={"renewal": "2026-10-15", "password": NOT_PROJECTED}) + assert items[0]["days_until"] == 1 + assert items[0]["expires_on"] == "2026-10-15" + assert NOT_PROJECTED not in str(items) + assert invalid == 0 + for field in [{"key": "renewal", "type": "date"}, {"key": "renewal", "type": "password", "is_expiration": True}]: + assert project(fields=[field]) == ([], 0) + + +def test_expired_today_and_window_boundaries(): + for value, expected in [("2026-10-13", -1), ("2026-10-14", 0), ("2026-11-13", 30)]: + assert project(traits={"renewal": value})[0][0]["days_until"] == expected + assert project(traits={"renewal": "2026-11-14"}) == ([], 0) + + +def test_invalid_and_missing_dates_and_source_field_aliases(): + for value in ["2026-02-30", "20261015", "2026-10-15T00:00:00Z", True, {}]: + assert project(traits={"renewal": value}) == ([], 1) + assert project(traits={"other": "2026-10-15"}) == ([], 0) + assert project(fields=[{"attributes": {"name-key": "renewal", "kind": "date", "is_expiration": True}}])[0] + + +def test_foreign_disabled_and_archived_assets_and_inactive_types_are_excluded(): + for change in [{"organization_id": "b"}, {"archived": True}, {"is_enabled": False}]: + assert project(asset_changes=change) == ([], 0) + for change in [{"organization_id": "b"}, {"active": False}]: + assert project(type_changes=change) == ([], 0) diff --git a/tests/test_expiration_workflow.py b/tests/test_expiration_workflow.py new file mode 100644 index 0000000..c2e57e3 --- /dev/null +++ b/tests/test_expiration_workflow.py @@ -0,0 +1,73 @@ +import asyncio +from datetime import datetime, timedelta, timezone +from pathlib import Path +from types import SimpleNamespace +from uuid import UUID + +import pytest +import yaml + +from functions import expirations + + +def setup(monkeypatch, *, provider=False, org="a", rows=None, type_org="a"): + calls = [] + due = (datetime.now(timezone.utc).date() + timedelta(days=2)).isoformat() + asset = SimpleNamespace(id=str(UUID(int=1)), data={"organization_id": "a", "name": "Firewall", "flexible_asset_type_id": "type-a", "traits": {"expiry": due, "secret": "not-returned"}}) + async def query(table, **kwargs): + calls.append((table, kwargs)) + if table == expirations.ASSETS: + return SimpleNamespace(documents=rows if rows is not None else [asset]) + return SimpleNamespace(documents=[SimpleNamespace(id="type-a", data={"organization_id": type_org, "fields": [{"key": "expiry", "type": "date", "is_expiration": True}]})]) + monkeypatch.setattr(expirations, "context", SimpleNamespace(org_id=org, is_platform_admin=False, organization=SimpleNamespace(is_provider=provider))) + monkeypatch.setattr(expirations, "tables", SimpleNamespace(query=query)) + return calls + + +def test_customer_scope_and_safe_projection(monkeypatch): + calls = setup(monkeypatch) + result = asyncio.run(expirations.docs_list_expirations("a")) + assert result["items"][0]["days_until"] == 2 + assert "not-returned" not in str(result) + assert result["next_cursor"] is None + assert result["scanned"] == 1 + assert calls[0] == (expirations.ASSETS, {"where": {"organization_id": "a"}, "scope": "a", "limit": 100, "after_document_id": "", "skip_count": True}) + assert calls[1][1]["document_ids"] == ["type-a"] + + +def test_foreign_customer_and_missing_identity_denied_before_reads(monkeypatch): + for org in ["b", None]: + calls = setup(monkeypatch, org=org) + with pytest.raises(expirations.UserError, match="own organization"): + asyncio.run(expirations.docs_list_expirations("a")) + assert not calls + + +def test_provider_may_choose_target_but_foreign_schema_is_not_projected(monkeypatch): + setup(monkeypatch, provider=True, org="provider", type_org="b") + assert asyncio.run(expirations.docs_list_expirations("a"))["items"] == [] + + +@pytest.mark.parametrize("params", [{"within_days": 0}, {"within_days": 366}, {"within_days": True}, {"cursor": "not-an-id"}, {"cursor": 5}, {"organization_id": ""}]) +def test_invalid_inputs_rejected_before_reads(monkeypatch, params): + calls = setup(monkeypatch) + with pytest.raises(expirations.UserError): + asyncio.run(expirations.docs_list_expirations(**{"organization_id": "a", **params})) + assert not calls + + +def test_cursor_is_last_asset_not_last_match(monkeypatch): + rows = [SimpleNamespace(id=str(UUID(int=i)), data={"organization_id": "a"}) for i in range(1, 101)] + calls = setup(monkeypatch, rows=rows) + result = asyncio.run(expirations.docs_list_expirations("a", cursor=str(UUID(int=1000)))) + assert result["items"] == [] + assert result["next_cursor"] == rows[-1].id + assert len(calls) == 1 + assert calls[0][1]["after_document_id"] == str(UUID(int=1000)) + + +def test_manifest_retains_reader_access_without_migration_privileges(): + entries = yaml.safe_load((Path(__file__).parents[1] / ".bifrost/workflows.yaml").read_text())["workflows"] + entry = next(e for e in entries.values() if e["function_name"] == "docs_list_expirations") + assert entry["access_level"] == "role_based" + assert entry["role_names"] == ["Bifrost Docs Reader", "Bifrost Docs Editor", "Bifrost Docs Administrator"] diff --git a/tests/test_itglue_api.py b/tests/test_itglue_api.py index c47d27e..6711020 100644 --- a/tests/test_itglue_api.py +++ b/tests/test_itglue_api.py @@ -705,3 +705,49 @@ async def copy() -> None: assert source_requests == 2 assert len(created) == 3 assert all(client.closed for client in created) + + +@pytest.mark.parametrize("login_url", [ + "https://tenant.itglue.com/login?return_to=private", + "https://tenant.itglue.com/users/sign_in", + "https://login.microsoftonline.com/tenant/oauth2/authorize?state=private", +]) +def test_file_copy_reports_browser_authentication_without_following_login(monkeypatch: Any, login_url: str) -> None: + real_client = httpx.AsyncClient + requests: list[httpx.Request] = [] + clients: list[Any] = [] + + def handler(request: httpx.Request) -> httpx.Response: + requests.append(request) + if len(requests) == 1: + return httpx.Response(302, headers={"Location": login_url}) + return httpx.Response(200, headers={"Content-Type": "text/html"}, content=b"") + + class TransferClient: + def __init__(self, **kwargs: Any) -> None: + self.source = real_client(transport=httpx.MockTransport(handler)) + clients.append(self.source) + + def build_request(self, *args: Any, **kwargs: Any) -> httpx.Request: + return self.source.build_request(*args, **kwargs) + + async def send(self, *args: Any, **kwargs: Any) -> httpx.Response: + return await self.source.send(*args, **kwargs) + + async def aclose(self) -> None: + await self.source.aclose() + + async def copy() -> None: + async with real_client(transport=httpx.MockTransport(handler)) as raw: + client = ITGlueClient("private-api-key", base_url="https://api.itglue.test", client=raw) + await client.stream_to_signed_url("https://tenant.itglue.com/attachments/123", "https://storage.test/private", content_type="application/pdf") + + monkeypatch.setattr("modules.itglue_api.httpx.AsyncClient", TransferClient) + with pytest.raises(ITGlueError, match="requires browser authentication") as caught: + asyncio.run(copy()) + assert caught.value.status_code == 401 + assert len(requests) == 1 + assert "x-api-key" not in requests[0].headers + assert all(client.is_closed for client in clients) + assert "private" not in str(caught.value) + assert "https://" not in str(caught.value)