From 97aa1d4081a40b1864e3c244d72ab9e61581f16a Mon Sep 17 00:00:00 2001 From: Makisuo Date: Mon, 16 Nov 2026 18:49:11 +0100 Subject: [PATCH] feat(security): show which platforms feed the audit log and how to connect more A Sources panel above the audit log lists Maple, Vercel and Google Cloud with their state (built in, receiving, no events yet, feed off, failing, not connected), 30 day event counts and the action to connect or turn each on. Events now show each platform's mark. Backed by GET /internal/audit-trail/sources, which reads per-platform counts against the integration inventory. --- .../routes/internal/audit-trail.http.test.ts | 94 ++++++++- .../src/routes/internal/audit-trail.http.ts | 34 +++- apps/api/src/runtime/service-graph.ts | 2 + .../web/src/lab/security-audit-log-fixture.ts | 57 +++++- apps/web/src/lab/security-lab/scenarios.ts | 16 +- .../audit-log/audit-log.scene.test.ts | 93 ++++++++- .../src/page/security/audit-log/command.ts | 9 + apps/web/src/page/security/audit-log/model.ts | 8 +- .../web/src/page/security/audit-log/update.ts | 35 +++- .../page/security/audit-log/view-sources.ts | 187 ++++++++++++++++++ apps/web/src/page/security/audit-log/view.ts | 9 +- .../web/src/page/security/kit/audit-brands.ts | 50 +++++ .../services/audit-trail/AuditTrailService.ts | 29 ++- .../audit-trail/audit-trail-sources.test.ts | 105 ++++++++++ .../audit-trail/audit-trail-sources.ts | 101 ++++++++++ packages/domain/src/http/audit-trail.ts | 46 +++++ .../src/__sql_baseline__/catalog.sql | 11 ++ .../query-engine/src/benchmark/builders.ts | 7 + .../src/ch/queries/audit-trail.ts | 24 +++ 19 files changed, 893 insertions(+), 24 deletions(-) create mode 100644 apps/web/src/page/security/audit-log/view-sources.ts create mode 100644 apps/web/src/page/security/kit/audit-brands.ts create mode 100644 packages/backend/src/services/audit-trail/audit-trail-sources.test.ts create mode 100644 packages/backend/src/services/audit-trail/audit-trail-sources.ts diff --git a/apps/api/src/routes/internal/audit-trail.http.test.ts b/apps/api/src/routes/internal/audit-trail.http.test.ts index d9f3942468..5da0dee59f 100644 --- a/apps/api/src/routes/internal/audit-trail.http.test.ts +++ b/apps/api/src/routes/internal/audit-trail.http.test.ts @@ -8,6 +8,7 @@ import { AuditTrailApiGroup, type AuditTrailEntry, AuditTrailListResponse, + AuditTrailSourcesResponse, CurrentTenant, OrgId, RoleName, @@ -27,6 +28,10 @@ import { AuditTrailService, } from "@maple/backend/services/audit-trail/AuditTrailService" import { OrgMembersService } from "@maple/backend/services/org/OrgMembersService" +import { + IntegrationInventoryService, + IntegrationInventoryUnavailableError, +} from "@maple/backend/services/integrations/IntegrationInventoryService" import { HttpAuditTrailLive } from "./audit-trail.http" class AuditTrailOnlyApi extends HttpApi.make("MapleInternalApi") @@ -42,6 +47,7 @@ const asRole = Schema.decodeSync(RoleName) const asPage = Schema.decodeUnknownSync(AuditTrailListResponse) const asDetection = Schema.decodeUnknownSync(AuditDetection) const asDetections = Schema.decodeUnknownSync(AuditDetectionListResponse) +const asSources = Schema.decodeUnknownSync(AuditTrailSourcesResponse) const asFailure = Schema.decodeUnknownSync(Schema.Struct({ _tag: Schema.String })) const trackedDbs: TestDb[] = [] @@ -77,6 +83,7 @@ const makeHarness = ( page: AuditTrailPage, directoryDown = false, testDb: TestDb = createTestDb(trackedDbs), + inventoryDown = false, ) => { const asked: Array = [] let directoryReads = 0 @@ -86,6 +93,39 @@ const makeHarness = ( write: () => unused, list: (_orgId, filters) => Effect.sync(() => (asked.push(filters), page)), count: () => unused, + providerStats: () => + Effect.succeed([ + { provider: "maple", events: 12, lastEventAt: 1_700_000_000_000 }, + { provider: "vercel", events: 3, lastEventAt: 1_699_000_000_000 }, + ]), + }), + Layer.succeed(IntegrationInventoryService, { + list: () => + inventoryDown + ? Effect.fail(new IntegrationInventoryUnavailableError({ message: "db down" })) + : Effect.succeed({ + integrations: [ + { + provider: "vercel", + status: "healthy", + account: "Acme (2 projects)", + connectedAt: 1, + problem: null, + feeds: [], + resources: [ + { + kind: "drain", + name: "audit log", + status: "healthy", + lastSuccessAt: null, + lastError: null, + note: null, + }, + ], + }, + ], + available: [], + }), }), Layer.effect(AuditDetectionsService, AuditDetectionsService.make).pipe(Layer.provide(testDb.layer)), Layer.succeed(AuditLogService, auditLog), @@ -135,6 +175,13 @@ const makeHarness = ( return { status: response.status, body: await response.json() } } + const sources = () => + send( + new Request("http://maple.test/internal/audit-trail/sources", { + headers: { authorization: "Bearer test-token" }, + }), + ) + const list = (search = "") => send( new Request(`http://maple.test/internal/audit-trail/events${search}`, { @@ -163,7 +210,7 @@ const makeHarness = ( changedFields: entry.changedFields, })) - return { list, detections, audited, asked, directoryReads: () => directoryReads, dispose } + return { list, sources, detections, audited, asked, directoryReads: () => directoryReads, dispose } } const ADMIN = ["org:admin"] @@ -491,3 +538,48 @@ describe("/internal/audit-trail/detections", () => { } }) }) + +describe("GET /internal/audit-trail/sources", () => { + it("refuses an engineer: what feeds the log says what the log holds", async () => { + const harness = makeHarness(["org:engineer"], { events: [], next: null }) + try { + expect((await harness.sources()).status).toBe(403) + } finally { + await harness.dispose() + } + }) + + it("tells an admin which platforms send events and which are not connected", async () => { + const harness = makeHarness(ADMIN, { events: [], next: null }) + try { + const response = await harness.sources() + expect(response.status).toBe(200) + const body = asSources(response.body) + expect(Arr.map(body.sources, (source) => [source.provider, source.state, source.events])).toEqual( + [ + ["maple", "built_in", 12], + ["vercel", "receiving", 3], + ["gcp", "not_connected", 0], + ], + ) + expect(body.sources[1]?.account).toBe("Acme (2 projects)") + } finally { + await harness.dispose() + } + }) + + it("still answers with what arrived when the integration state cannot be read", async () => { + const harness = makeHarness(ADMIN, { events: [], next: null }, false, undefined, true) + try { + const response = await harness.sources() + expect(response.status).toBe(200) + expect(Arr.map(asSources(response.body).sources, (source) => source.state)).toEqual([ + "built_in", + "receiving", + "waiting", + ]) + } finally { + await harness.dispose() + } + }) +}) diff --git a/apps/api/src/routes/internal/audit-trail.http.ts b/apps/api/src/routes/internal/audit-trail.http.ts index ddfec81108..4dc0220a5c 100644 --- a/apps/api/src/routes/internal/audit-trail.http.ts +++ b/apps/api/src/routes/internal/audit-trail.http.ts @@ -10,14 +10,19 @@ import * as Policy from "@maple/domain/policy" import { recordHttpAudit } from "@maple/backend/services/audit/AuditLogService" import { AuditDetectionsService } from "@maple/backend/services/audit-trail/AuditDetectionsService" import { AuditTrailService } from "@maple/backend/services/audit-trail/AuditTrailService" +import { auditTrailSources } from "@maple/backend/services/audit-trail/audit-trail-sources" +import { IntegrationInventoryService } from "@maple/backend/services/integrations/IntegrationInventoryService" import type { OrgMember } from "@maple/backend/services/org/OrgMembersService" import { OrgMembersService } from "@maple/backend/services/org/OrgMembersService" -import { Effect, Array as Arr, Option, String as Str } from "effect" +import { Clock, Effect, Array as Arr, Option, String as Str } from "effect" import { orgMemberDirectory, orgMemberDirectoryOrFail } from "@/routes/org-member-directory" import { auditDiff } from "@/routes/v2/audit-changes" const DEFAULT_PAGE_LIMIT = 50 +/** How far back the source list counts each platform's events. */ +const SOURCES_WINDOW_DAYS = 30 + /** `:`; the query schema has already checked the shape. */ const parseCursor = (cursor: string) => { const [occurredAtMs, id = ""] = Str.split(":")(cursor) @@ -76,6 +81,7 @@ export const HttpAuditTrailLive = HttpApiBuilder.group(MapleInternalApi, "auditT const trail = yield* AuditTrailService const detections = yield* AuditDetectionsService const members = yield* OrgMembersService + const inventory = yield* IntegrationInventoryService return handlers .handle("list", ({ query }) => @@ -133,6 +139,32 @@ export const HttpAuditTrailLive = HttpApiBuilder.group(MapleInternalApi, "auditT } }), ) + .handle("sources", () => + Effect.gen(function* () { + const tenant = yield* requireAuditLogRead + const now = yield* Clock.currentTimeMillis + const stats = yield* trail.providerStats( + tenant.orgId, + now - SOURCES_WINDOW_DAYS * 24 * 60 * 60 * 1000, + ) + // Connection state is context: without it the list still says what arrived. + const integrations = yield* inventory.list(tenant.orgId).pipe( + Effect.asSome, + Effect.catchTag( + "@maple/backend/services/IntegrationInventoryUnavailableError", + (error) => + Effect.logWarning("Audit log sources read without integration state").pipe( + Effect.annotateLogs({ cause: error.message }), + Effect.as(Option.none()), + ), + ), + ) + return { + sources: auditTrailSources(stats, Option.getOrNull(integrations)), + windowDays: SOURCES_WINDOW_DAYS, + } + }), + ) .handle("listDetections", () => Effect.gen(function* () { const tenant = yield* requireAuditLogRead diff --git a/apps/api/src/runtime/service-graph.ts b/apps/api/src/runtime/service-graph.ts index a8b42d4591..9a2dc2d40e 100644 --- a/apps/api/src/runtime/service-graph.ts +++ b/apps/api/src/runtime/service-graph.ts @@ -136,6 +136,7 @@ import { ProductEventsService } from "@maple/backend/services/product-events/Pro import { AuditLogService } from "@maple/backend/services/audit/AuditLogService" import { AuditTrailService } from "@maple/backend/services/audit-trail/AuditTrailService" import { AuditDetectionsService } from "@maple/backend/services/audit-trail/AuditDetectionsService" +import { IntegrationInventoryService } from "@maple/backend/services/integrations/IntegrationInventoryService" import { WarehouseQueryService } from "@maple/backend/services/warehouse/WarehouseQueryService" import { QueryEngineService } from "@maple/backend/services/warehouse/QueryEngineService" import { TelemetryReadService } from "@maple/backend/services/warehouse/TelemetryReadService" @@ -250,6 +251,7 @@ export const HttpServicesLive = Layer.mergeAll( AuditLogService.layer, AuditTrailService.layer, AuditDetectionsService.layer, + IntegrationInventoryService.layer, WarehouseQueryService.layer, QueryEngineService.layer, RuntimeReleaseCatalog.layer, diff --git a/apps/web/src/lab/security-audit-log-fixture.ts b/apps/web/src/lab/security-audit-log-fixture.ts index c8ddb6b941..622f2ab609 100644 --- a/apps/web/src/lab/security-audit-log-fixture.ts +++ b/apps/web/src/lab/security-audit-log-fixture.ts @@ -1,4 +1,4 @@ -import type { AuditTrailEntry, AuditTrailListResponse } from "@maple/domain/http" +import type { AuditTrailEntry, AuditTrailListResponse, AuditTrailSourcesResponse } from "@maple/domain/http" import { Array as Arr, String as Str } from "effect" // Audit events for the Security hub's audit log lab: Maple's own changes and refusals over the @@ -189,3 +189,58 @@ export const auditEventsOf = (nowMs: number, hasMore = true): AuditTrailListResp } export const noAuditEvents: AuditTrailListResponse = { events: [], nextCursor: null } + +/** Maple and a Vercel team sending, Google Cloud connected with its log forwarding off. */ +export const auditSourcesOf = (nowMs: number): AuditTrailSourcesResponse => ({ + windowDays: 30, + sources: [ + { + provider: "maple", + state: "built_in", + account: null, + problem: null, + events: 214, + lastEventAt: nowMs - 4 * MINUTE, + }, + { + provider: "vercel", + state: "receiving", + account: "Acme (6 projects)", + problem: null, + events: 37, + lastEventAt: nowMs - 52 * MINUTE, + }, + { + provider: "gcp", + state: "off", + account: "project acme-prod", + problem: null, + events: 0, + lastEventAt: null, + }, + ], +}) + +/** A new org: only Maple's own feed, nothing connected. */ +export const noAuditSources: AuditTrailSourcesResponse = { + windowDays: 30, + sources: [ + { provider: "maple", state: "built_in", account: null, problem: null, events: 0, lastEventAt: null }, + { + provider: "vercel", + state: "not_connected", + account: null, + problem: null, + events: 0, + lastEventAt: null, + }, + { + provider: "gcp", + state: "not_connected", + account: null, + problem: null, + events: 0, + lastEventAt: null, + }, + ], +} diff --git a/apps/web/src/lab/security-lab/scenarios.ts b/apps/web/src/lab/security-lab/scenarios.ts index d0edde44a0..c19b345726 100644 --- a/apps/web/src/lab/security-lab/scenarios.ts +++ b/apps/web/src/lab/security-lab/scenarios.ts @@ -68,7 +68,7 @@ import { repositoriesOf, } from "../security-dependencies-fixture" import { emptyExposureOf, exposureOf, runtimesOf } from "../security-telemetry-fixture" -import { auditEventsOf, noAuditEvents } from "../security-audit-log-fixture" +import { auditEventsOf, auditSourcesOf, noAuditEvents, noAuditSources } from "../security-audit-log-fixture" import { detectionDestinationsOf, detectionsOf, @@ -392,6 +392,9 @@ const auditLog = ( const auditLogPage = (page: AuditLogPage.EventsPage) => (model: AuditLogPage.Model) => A.CompletedFetchFirstPage({ key: model.firstPage.key ?? "", result: Result.succeed(page) }) +const auditLogSources = (sources: AuditLogPage.Sources) => (model: AuditLogPage.Model) => + A.CompletedFetchSources({ key: model.sources.key ?? "", result: Result.succeed(sources) }) + type DetectionsStep = Step const DT = DetectionsPage.Message @@ -647,7 +650,9 @@ export const build = (scenario: Scenario, nowMs: number): Built => { case "audit-log": return { _tag: "AuditLog", - step: auditLog(nowMs, { answers: [auditLogPage(auditEventsOf(nowMs))] }), + step: auditLog(nowMs, { + answers: [auditLogPage(auditEventsOf(nowMs)), auditLogSources(auditSourcesOf(nowMs))], + }), } case "audit-log-detail": return { @@ -667,7 +672,12 @@ export const build = (scenario: Scenario, nowMs: number): Built => { }), } case "audit-log-empty": - return { _tag: "AuditLog", step: auditLog(nowMs, { answers: [auditLogPage(noAuditEvents)] }) } + return { + _tag: "AuditLog", + step: auditLog(nowMs, { + answers: [auditLogPage(noAuditEvents), auditLogSources(noAuditSources)], + }), + } case "audit-log-filtered-empty": return { _tag: "AuditLog", diff --git a/apps/web/src/page/security/audit-log/audit-log.scene.test.ts b/apps/web/src/page/security/audit-log/audit-log.scene.test.ts index 678b181073..d602c270b8 100644 --- a/apps/web/src/page/security/audit-log/audit-log.scene.test.ts +++ b/apps/web/src/page/security/audit-log/audit-log.scene.test.ts @@ -10,7 +10,7 @@ import { AppRoute, hrefOf } from "../../../route" import type { PageContext } from "../../contract" import { auditEntryHref } from "../../kit/audit-entry-link" import { eventsOf, nextCursorOf } from "./derive" -import { type EventsPage, Message, type Model, type Search } from "./model" +import { type EventsPage, Message, type Model, type Search, type Sources } from "./model" import { enter, routeChanged, update } from "./update" import { view as pageView } from "./view" @@ -26,6 +26,7 @@ const names = (commands: ReadonlyArray<{ readonly name: string }> | undefined) = Arr.map(commands ?? [], ({ name }) => name) const FIRST_PAGE = "SecurityApiAuditLogFirstPage" const NEXT_PAGE = "SecurityApiAuditLogNextPage" +const SOURCES = "SecurityApiAuditLogSources" const routeOf = (search: Search = {}) => AppRoute.SecurityAuditLog({ search }) /** The hosted product: the session, not the build, says what the viewer may read. */ @@ -104,14 +105,14 @@ describe("security audit log", () => { test("an admin's session answer asks for the first page", () => { const answered = pageUpdate(init(routeOf(), context).model, grantsRead(ADMIN)()) - expect(names(answered.commands)).toEqual([FIRST_PAGE]) + expect(names(answered.commands)).toEqual([FIRST_PAGE, SOURCES]) }) // A failed session read answers with no grants at all. That is "unknown", not "member": an // admin must not be locked out of the log by it. test("a session that could not be read asks the API, and the API's refusal is what locks the page", () => { const asked = pageUpdate(init(routeOf(), context).model, grantsRead([])()) - expect(names(asked.commands)).toEqual([FIRST_PAGE]) + expect(names(asked.commands)).toEqual([FIRST_PAGE, SOURCES]) scene( { update: pageUpdate, view }, @@ -423,4 +424,90 @@ describe("security audit log", () => { expectView(role("button", { name: "Clear filters" })).toExist(), ) }) + + describe("sources", () => { + const SOURCES_ANSWER: Sources = { + windowDays: 30, + sources: [ + { + provider: "maple", + state: "built_in", + account: null, + problem: null, + events: 41, + lastEventAt: NOW - 60_000, + }, + { + provider: "vercel", + state: "receiving", + account: "Acme (3 projects)", + problem: null, + events: 7, + lastEventAt: NOW - 3_600_000, + }, + { + provider: "gcp", + state: "not_connected", + account: null, + problem: null, + events: 0, + lastEventAt: null, + }, + ], + } + const withSources = (model: Model, answer: Sources = SOURCES_ANSWER) => + fold(model, [ + (current) => + Message.CompletedFetchSources({ + key: current.sources.key ?? "", + result: Result.succeed(answer), + }), + ]) + + test("says which platforms send events and how to connect the ones that do not", () => { + scene( + { update: pageUpdate, view }, + given(withSources(loaded())), + expectView(role("list", { name: "Audit log sources" })).toExist(), + expectView(text("2 of 3 sending · last 30 days")).toExist(), + expectView(text("Built in")).toExist(), + expectView(text("Not connected")).toExist(), + expectView(role("link", { name: "Connect Google Cloud" })).toHaveAttr( + "href", + hrefOf(AppRoute.SourcesNew({ search: { integration: "gcp" } })), + ), + ) + }) + + test("a connected platform with its audit feed off links to where it is turned on", () => { + const off: Sources = { + ...SOURCES_ANSWER, + sources: Arr.map(SOURCES_ANSWER.sources, (source) => + source.provider === "vercel" + ? { ...source, state: "off" as const, events: 0, lastEventAt: null } + : source, + ), + } + scene( + { update: pageUpdate, view }, + given(withSources(loaded(), off)), + expectView(text("Audit feed off")).toExist(), + expectView(role("link", { name: "Turn on" })).toHaveAttr( + "href", + hrefOf(AppRoute.SourcesNew({ search: { integration: "vercel" } })), + ), + ) + }) + + test("View events narrows the list to that platform", () => { + const model = withSources(loaded()) + const clicked = pageUpdate(model, Message.ChangedFilter({ filter: "provider", value: "vercel" })) + expect(clicked.outMessage).toMatchObject({ url: hrefOf(routeOf({ provider: "vercel" })) }) + scene( + { update: pageUpdate, view }, + given(model), + expectView(role("button", { name: "Show only Vercel events" })).toExist(), + ) + }) + }) }) diff --git a/apps/web/src/page/security/audit-log/command.ts b/apps/web/src/page/security/audit-log/command.ts index 040819ae77..224a1fdb46 100644 --- a/apps/web/src/page/security/audit-log/command.ts +++ b/apps/web/src/page/security/audit-log/command.ts @@ -55,3 +55,12 @@ export const FetchNextPage = Command.define("SecurityApiAuditLogNextPage", { settleTo((result) => Message.CompletedFetchNextPage({ key, generation, cursor, result })), ), }) + +export const FetchSources = Command.define("SecurityApiAuditLogSources", { + args: { key: Schema.String }, + messages: [Message.CompletedFetchSources], + execute: ({ key }) => + Effect.flatMap(MapleInternalApiClient, (api) => api.auditTrail.sources()).pipe( + settleTo((result) => Message.CompletedFetchSources({ key, result })), + ), +}) diff --git a/apps/web/src/page/security/audit-log/model.ts b/apps/web/src/page/security/audit-log/model.ts index 56bc60e3ee..f9f11fcb09 100644 --- a/apps/web/src/page/security/audit-log/model.ts +++ b/apps/web/src/page/security/audit-log/model.ts @@ -1,4 +1,4 @@ -import { AuditTrailListResponse } from "@maple/domain/http" +import { AuditTrailListResponse, AuditTrailSourcesResponse } from "@maple/domain/http" import * as Policy from "@maple/domain/policy" import { Schema } from "effect" import { defineMessageUnion } from "foldkit/message" @@ -17,6 +17,10 @@ export type Search = SecurityAuditLogSearch export const EventsPage = AuditTrailListResponse export type EventsPage = typeof EventsPage.Type +/** Every platform that can feed the log, connected or not, and what each sent lately. */ +export const Sources = AuditTrailSourcesResponse +export type Sources = typeof Sources.Type + /** The pages after the first, for the current first-page key. */ export const LaterPages = Schema.Struct({ key: Schema.NullOr(Schema.String), @@ -35,6 +39,7 @@ export const Model = Schema.Struct({ /** False until the session answers: the page says nothing about access before it knows. */ isGrantsRead: Schema.Boolean, firstPage: KeyedSchema(EventsPage, LoadFailure), + sources: KeyedSchema(Sources, LoadFailure), laterPages: LaterPages, /** The events whose detail is open, by id. */ expanded: Schema.Array(Schema.String), @@ -49,6 +54,7 @@ export const Message = defineMessageUnion({ ReadClock: { nowMs: Schema.Finite }, CompletedReadGrants: SessionGrants, CompletedFetchFirstPage: { key: Schema.String, result: Schema.Result(EventsPage, LoadFailure) }, + CompletedFetchSources: { key: Schema.String, result: Schema.Result(Sources, LoadFailure) }, CompletedFetchNextPage: { key: Schema.String, generation: Schema.Number, diff --git a/apps/web/src/page/security/audit-log/update.ts b/apps/web/src/page/security/audit-log/update.ts index 93036f396c..77ded0f108 100644 --- a/apps/web/src/page/security/audit-log/update.ts +++ b/apps/web/src/page/security/audit-log/update.ts @@ -8,7 +8,7 @@ import { type PageContext, PageOutMessage, type PageReturn } from "../../contrac import { appendedPage, fetchingNext, freshLaterPages } from "../../kit/infinite-pages" import { chain, slotStep } from "../../kit/keyed-step" import { acceptGrants } from "../../kit/session-grants" -import { FetchFirstPage, FetchNextPage, ReadAuditLogClock, ReadAuditLogGrants } from "./command" +import { FetchFirstPage, FetchNextPage, FetchSources, ReadAuditLogClock, ReadAuditLogGrants } from "./command" import { nextCursorOf } from "./derive" import { accessOf, type LaterPages, Message, type Model, type Search } from "./model" @@ -44,6 +44,23 @@ const readFirstPage = (model: Model, context: PageContext, mode: Keyed.ReadMode) ) } +/** Which platforms feed the log: read beside the first page, behind the same permission. */ +const readSources = (model: Model, context: PageContext, mode: Keyed.ReadMode): Return => + accessOf(model) !== "ask" + ? { model } + : slotStep( + model, + model.sources, + (sources) => ({ sources }), + requestKey(context.orgId, "auditTrail.sources", {}), + mode, + (next) => FetchSources({ key: next }), + ) + +/** The list and the sources together, as the page needs them on entry, on grants and on retry. */ +const readAll = (model: Model, context: PageContext, mode: Keyed.ReadMode): Return => + chain(readFirstPage(model, context, mode), (next) => readSources(next, context, mode)) + const fetchNext = (model: Model): Return => { const cursor = nextCursorOf(model) const { laterPages } = model @@ -97,6 +114,7 @@ export const enter = ( grants: sessionDecides ? [] : Policy.allPermissions, isGrantsRead: !sessionDecides, firstPage: Keyed.idle(), + sources: Keyed.idle(), laterPages: freshPages(null, 0), expanded: [], actorText: route.search.actor ?? "", @@ -108,7 +126,7 @@ export const enter = ( ...(sessionDecides ? [ReadAuditLogGrants({ orgId: context.orgId })] : []), ], } - return chain(entered, (next) => readFirstPage(next, context, "Load")) + return chain(entered, (next) => readAll(next, context, "Load")) } export const init = (route: SecurityAuditLogRoute, context: PageContext): Return => @@ -136,14 +154,13 @@ export const update = (model: Model, message: Message, context: PageContext): Re Message.match(message, { ReadClock: ({ nowMs }) => ({ model: { ...model, nowMs } }), CompletedReadGrants: (answer) => - readFirstPage( - acceptGrants(model, context.orgId, answer, { isGrantsRead: true }), - context, - "Load", - ), + readAll(acceptGrants(model, context.orgId, answer, { isGrantsRead: true }), context, "Load"), CompletedFetchFirstPage: ({ key, result }) => ({ model: { ...model, firstPage: Keyed.accept(model.firstPage, key, result) }, }), + CompletedFetchSources: ({ key, result }) => ({ + model: { ...model, sources: Keyed.accept(model.sources, key, result) }, + }), CompletedFetchNextPage: ({ key, generation, cursor, result }) => key !== model.laterPages.key || generation !== model.laterPages.generation || @@ -152,9 +169,7 @@ export const update = (model: Model, message: Message, context: PageContext): Re ? { model } : { model: { ...model, laterPages: appendedPage(model.laterPages, result) } }, ClickedRetry: () => - chain({ model, commands: [ReadAuditLogClock()] }, (next) => - readFirstPage(next, context, "Refresh"), - ), + chain({ model, commands: [ReadAuditLogClock()] }, (next) => readAll(next, context, "Refresh")), ClickedLoadMore: () => fetchNext(model), ChangedFilter: ({ filter, value }) => navigate(model, withFilter(model.search, filter, value)), ChangedActorText: ({ value }) => ({ model: { ...model, actorText: value } }), diff --git a/apps/web/src/page/security/audit-log/view-sources.ts b/apps/web/src/page/security/audit-log/view-sources.ts new file mode 100644 index 0000000000..9fe8a1ed91 --- /dev/null +++ b/apps/web/src/page/security/audit-log/view-sources.ts @@ -0,0 +1,187 @@ +import type { AuditTrailSource, AuditTrailSourceState } from "@maple/domain/http" +import { AuditTrailProvider } from "@maple/domain/audit-trail" +import { Array as Arr, Schema } from "effect" +import { AsyncData } from "foldkit" +import type { Html, HtmlBuilder } from "foldkit/html" +import { button, linkButton } from "@maple/views/ui/button" +import { listRow } from "@maple/views/ui/list-row" +import { panel, panelHeader } from "@maple/views/ui/panel" +import { hstack, vstack } from "@maple/views/ui/stack" +import { statusDot } from "@maple/views/ui/status-dot" +import { typography } from "@maple/views/ui/typography" +import { AppRoute, hrefOf } from "../../../route" +import { type IntegrationBrand, integrationIconPlate } from "../../kit/integration-empty" +import { relativeTime } from "../../kit/relative-time" +import { providerBrand } from "../kit/audit-brands" +import { providerLabel } from "../kit/audit-labels" +import { Message, type Model } from "./model" + +// The audit log's sources: every platform that can feed it, whether it does, and the way to turn +// on the ones that do not. Answers "what is in here, and what else could be". + +type H = HtmlBuilder +type Clock = Readonly<{ nowMs: number; timeZone: string }> + +type SourceCopy = Readonly<{ + brand: IntegrationBrand + /** What the platform's events are about. */ + sends: string + /** What to do when the platform is connected but its audit feed is off. */ + turnOn: string + /** What to do when the platform is not connected. */ + connect: string +}> + +/** The platforms this build knows how to set up; Maple's own feed has nothing to set up. */ +const COPY = { + maple: { + brand: providerBrand("maple"), + sends: "Changes made in Maple, and requests it refused", + turnOn: "", + connect: "", + }, + vercel: { + brand: providerBrand("vercel"), + sends: "Team actions: env vars, domains, members, tokens", + turnOn: "Turn on the Audit log drain in the Vercel integration.", + connect: "Connect a Vercel team and turn on its Audit log drain.", + }, + gcp: { + brand: providerBrand("gcp"), + sends: "Admin Activity and Policy Denied audit logs", + turnOn: "Turn on log forwarding: admin activity arrives through the log sink.", + connect: "Connect a project or organization with log forwarding on.", + }, +} satisfies Record + +/** A platform a newer writer added: its events still show, with nothing to set up. */ +const isProvider = Schema.is(AuditTrailProvider) + +const copyOf = (provider: string): SourceCopy => + isProvider(provider) + ? COPY[provider] + : { brand: providerBrand(provider), sends: "Audit events", turnOn: "", connect: "" } + +const STATE = { + built_in: { label: "Built in", tone: "ok" }, + receiving: { label: "Receiving", tone: "ok" }, + waiting: { label: "No events yet", tone: "info" }, + off: { label: "Audit feed off", tone: "neutral" }, + failing: { label: "Failing", tone: "crit" }, + not_connected: { label: "Not connected", tone: "neutral" }, +} as const satisfies Record + +const setupHref = (provider: string) => hrefOf(AppRoute.SourcesNew({ search: { integration: provider } })) + +const eventCount = (events: number) => `${events.toLocaleString("en-US")} event${events === 1 ? "" : "s"}` + +/** The line under the platform's name: what it sends, then how it is doing or what to do. */ +const sourceMeta = (h: H, source: AuditTrailSource, windowDays: number, clock: Clock): Html => { + const copy = copyOf(source.provider) + const status: ReadonlyArray = + source.state === "failing" + ? [source.problem ?? "The feed reports an error. Open the integration to see why."] + : source.state === "off" + ? [copy.turnOn] + : source.state === "not_connected" + ? [copy.connect] + : source.lastEventAt === null + ? source.state === "waiting" + ? ["On. Events show up the next time someone acts there."] + : [`Nothing in the last ${windowDays} days`] + : [ + `${eventCount(source.events)} in ${windowDays} days, last `, + relativeTime(h, { value: source.lastEventAt, ...clock }), + ] + return vstack(h, { gap: "0.5" }, [ + typography(h, { size: "xs", tone: "muted" }, [ + source.account === null ? copy.sends : `${copy.sends} · ${source.account}`, + ]), + typography(h, { size: "xs", tone: source.state === "failing" ? "destructive" : "muted" }, status), + ]) +} + +const stateMark = (h: H, state: AuditTrailSourceState): Html => + hstack(h, { gap: "1.5", align: "center", className: "whitespace-nowrap" }, [ + statusDot(h, { tone: STATE[state].tone }), + // A phone has no room beside the description: there the dot shows and the label is read out. + typography(h, { size: "xs", tone: "muted", className: "sr-only sm:not-sr-only" }, [ + STATE[state].label, + ]), + ]) + +/** Filters the list to the platform when it has events; otherwise opens its setup. */ +const sourceAction = (h: H, source: AuditTrailSource, model: Model): Html | undefined => { + const name = providerLabel(source.provider) + if (source.state === "not_connected") + return linkButton(h, { variant: "outline", size: "sm", href: setupHref(source.provider) }, [ + `Connect ${name}`, + ]) + if (source.state === "off" || source.state === "failing") + return linkButton(h, { variant: "outline", size: "sm", href: setupHref(source.provider) }, [ + source.state === "off" ? "Turn on" : "Open integration", + ]) + if (source.events === 0 || model.search.provider === source.provider) return undefined + return button( + h, + { + variant: "ghost", + size: "sm", + attributes: [ + h.AriaLabel(`Show only ${name} events`), + h.OnClick(Message.ChangedFilter({ filter: "provider", value: source.provider })), + ], + }, + ["View events"], + ) +} + +const sourceRow = (h: H, source: AuditTrailSource, windowDays: number, model: Model, clock: Clock): Html => { + const action = sourceAction(h, source, model) + return listRow( + h, + { + as: "li", + divided: true, + wrapMeta: true, + leading: integrationIconPlate(h, copyOf(source.provider).brand, { + size: 16, + plateClassName: "size-8 rounded-md", + }), + title: providerLabel(source.provider), + meta: sourceMeta(h, source, windowDays, clock), + trailing: [stateMark(h, source.state), ...(action === undefined ? [] : [action])], + }, + [h.Key(source.provider)], + ) +} + +/** How many platforms send events, for the header: "2 of 3 sending". */ +const sendingCount = (sources: ReadonlyArray) => + `${Arr.filter(sources, (source) => source.events > 0).length} of ${sources.length} sending` + +/** + * The sources panel above the list. Nothing while it loads, so the list does not jump twice; a + * failed read says so in one line, since the list below still works without it. + */ +export const sourcesPanel = (h: H, model: Model, clock: Clock): Html | null => + AsyncData.matchData(model.sources.data, { + onEmpty: () => null, + onFailure: () => + typography(h, { size: "xs", tone: "muted", as: "p" }, [ + "Couldn't load which platforms feed the audit log. Try again from the list below.", + ]), + onData: ({ sources, windowDays }) => + panel(h, { className: "overflow-hidden" }, [ + panelHeader(h, { + title: "Sources", + action: typography(h, { size: "xs", tone: "muted" }, [ + `${sendingCount(sources)} · last ${windowDays} days`, + ]), + }), + h.ul( + [h.AriaLabel("Audit log sources")], + Arr.map(sources, (source) => sourceRow(h, source, windowDays, model, clock)), + ), + ]), + }) diff --git a/apps/web/src/page/security/audit-log/view.ts b/apps/web/src/page/security/audit-log/view.ts index 39e266d0b8..5f491078c4 100644 --- a/apps/web/src/page/security/audit-log/view.ts +++ b/apps/web/src/page/security/audit-log/view.ts @@ -48,6 +48,7 @@ import { expandChevron } from "../../kit/expand-chevron" import { jsonBlock, prettyJson } from "../../kit/json-block" import { refusedBadge } from "../../kit/refused-badge" import { relativeTime } from "../../kit/relative-time" +import { providerName } from "../kit/audit-brands" import { ROOMY_COLUMN, securityLayout, WIDE_COLUMN } from "../kit/layout" import { placeholderRows } from "../kit/parts" import { @@ -73,6 +74,7 @@ import { targetText, } from "./derive" import { accessOf, Message, type Model, type SelectFilter } from "./model" +import { sourcesPanel } from "./view-sources" type H = HtmlBuilder type Clock = Readonly<{ nowMs: number; timeZone: string }> @@ -222,7 +224,7 @@ const eventDetail = (h: H, event: AuditTrailEntry, clock: Clock): Html => { ...detailField(h, "IP address", event.originIp, true), ...detailField(h, "Country", event.originCountry), ...detailField(h, "User agent", event.userAgent, true), - keyValue(h, { label: "Platform", wrap: true }, [providerLabel(event.provider)]), + keyValue(h, { label: "Platform", wrap: true }, [providerName(h, event.provider)]), ...detailField(h, "Account", event.account, true), keyValue(h, { label: "Occurred", wrap: true }, [at(event.occurredAt)]), keyValue(h, { label: "Recorded", wrap: true }, [at(event.ingestedAt)]), @@ -271,7 +273,7 @@ const eventRows = (h: H, model: Model, event: AuditTrailEntry, clock: Clock): Re ], "p-0", ), - tableCell(h, [providerLabel(event.provider)], WIDE_COLUMN), + tableCell(h, [providerName(h, event.provider)], WIDE_COLUMN), tableCell( h, [ @@ -427,7 +429,7 @@ const noEvents = (h: H): Html => emptyMedia(h, { variant: "icon" }, [icon(h, "HistoryIcon")]), emptyTitle(h, ["No audit events yet"]), emptyDescription(h, [ - "Changes made in Maple, and requests it refused, appear here as they happen. So do audit events from Vercel and Google Cloud once their audit feeds are on.", + "Changes made in Maple, and requests it refused, appear here as they happen. Platforms you connect above add their own audit events.", ]), ]), ], @@ -511,6 +513,7 @@ export const view = Submodel.defineView((model, // In the header, beside the title: the toolbar is full at laptop width and would wrap it. { source: "auditLog", actions: alertControl(h, model) }, [ + ...Arr.filter([sourcesPanel(h, model, clock)], (part): part is Html => part !== null), panel(h, { className: "overflow-hidden" }, [toolbar(h, model), listBody(h, model, clock)]), footnote(h), ], diff --git a/apps/web/src/page/security/kit/audit-brands.ts b/apps/web/src/page/security/kit/audit-brands.ts new file mode 100644 index 0000000000..7c1414326c --- /dev/null +++ b/apps/web/src/page/security/kit/audit-brands.ts @@ -0,0 +1,50 @@ +import { AuditTrailProvider } from "@maple/domain/audit-trail" +import { cn } from "@maple/ui/lib/utils" +import { Schema } from "effect" +import type { Html, HtmlBuilder } from "foldkit/html" +import { icon } from "@maple/views/ui/icon" +import { hstack } from "@maple/views/ui/stack" +import { typography } from "@maple/views/ui/typography" +import type { IntegrationBrand } from "../../kit/integration-empty" +import { PROVIDERS } from "../../kit/providers" +import { providerLabel } from "./audit-labels" + +// Each audit platform's mark, shared by the sources panel and the event list. + +const BRANDS = { + maple: { icon: "MapleMark", accent: "var(--color-brand-maple)" }, + vercel: PROVIDERS.vercel, + gcp: PROVIDERS.gcp, +} satisfies Record + +/** A platform a newer writer added gets a neutral history mark. */ +const UNKNOWN_BRAND: IntegrationBrand = { + icon: "HistoryIcon", + accent: "var(--primary)", + iconClassName: "text-muted-foreground", +} + +const isProvider = Schema.is(AuditTrailProvider) + +export const providerBrand = (provider: string): IntegrationBrand => + isProvider(provider) ? BRANDS[provider] : UNKNOWN_BRAND + +/** The platform's bare mark, tinted like its plate. */ +export const providerMark = (h: HtmlBuilder, provider: string, size = 14): Html => { + const brand = providerBrand(provider) + return h.span( + [ + h.AriaHidden(true), + h.Class(cn("inline-flex shrink-0", brand.iconClassName)), + ...(brand.iconClassName === undefined ? [h.Style({ color: brand.accent })] : []), + ], + [brand.glyph === undefined ? icon(h, brand.icon, { size }) : brand.glyph(size)], + ) +} + +/** The platform's mark beside its name. */ +export const providerName = (h: HtmlBuilder, provider: string): Html => + hstack(h, { gap: "2", align: "center", className: "min-w-0" }, [ + providerMark(h, provider), + typography(h, { truncate: true }, [providerLabel(provider)]), + ]) diff --git a/packages/backend/src/services/audit-trail/AuditTrailService.ts b/packages/backend/src/services/audit-trail/AuditTrailService.ts index 4bf7c5e274..963eefb138 100644 --- a/packages/backend/src/services/audit-trail/AuditTrailService.ts +++ b/packages/backend/src/services/audit-trail/AuditTrailService.ts @@ -7,6 +7,7 @@ import { Clock, Context, DateTime, Effect, Layer, Option, Array as Arr } from "e import { ALERTING_SYSTEM_USER_ID, systemTenant } from "@maple/backend/services/auth/system-tenant" import { WarehouseQueryService } from "@maple/backend/services/warehouse/WarehouseQueryService" import { auditTrailEventToRow, storedRowToEntry } from "./audit-trail-row" +import type { AuditTrailProviderStats } from "./audit-trail-sources" const AUDIT_TRAIL_DATASOURCE = "audit_trail_events" @@ -54,6 +55,11 @@ export interface AuditTrailServiceApi { filters: AuditDetectionFilters, window: { readonly startMs: number; readonly endMs: number }, ) => Effect.Effect + /** Per platform: distinct events that happened since `sinceMs` and the newest one's time. */ + readonly providerStats: ( + orgId: OrgId, + sinceMs: number, + ) => Effect.Effect, AuditTrailPersistenceError> } const persistenceError = (message: string) => (error: { readonly _tag: string }) => @@ -179,7 +185,28 @@ export class AuditTrailService extends Context.Service ({ + provider: row.provider, + events: row.events, + lastEventAt: row.events > 0 ? row.lastOccurredAt * 1000 : null, + })) + }) + + return { write, list, count, providerStats } }), }, ) { diff --git a/packages/backend/src/services/audit-trail/audit-trail-sources.test.ts b/packages/backend/src/services/audit-trail/audit-trail-sources.test.ts new file mode 100644 index 0000000000..f05b070a50 --- /dev/null +++ b/packages/backend/src/services/audit-trail/audit-trail-sources.test.ts @@ -0,0 +1,105 @@ +import { describe, expect, it } from "vitest" +import type { IntegrationEntry, IntegrationResource } from "@maple/domain/integration-inventory" +import { Array as Arr, Option } from "effect" +import { auditTrailSources } from "./audit-trail-sources" + +const resource = (overrides: Partial): IntegrationResource => ({ + kind: "drain", + name: "logs", + status: "healthy", + lastSuccessAt: null, + lastError: null, + note: null, + ...overrides, +}) + +const entry = (overrides: Partial): IntegrationEntry => ({ + provider: "vercel", + status: "healthy", + account: "Acme", + connectedAt: 1, + problem: null, + feeds: [], + resources: [], + ...overrides, +}) + +const sourceOf = (sources: ReturnType, provider: string) => + Option.getOrUndefined(Arr.findFirst(sources, (source) => source.provider === provider)) + +const stateOf = (sources: ReturnType, provider: string) => + sourceOf(sources, provider)?.state + +describe("auditTrailSources", () => { + it("lists Maple as built in even before it has recorded anything", () => { + const sources = auditTrailSources([], { integrations: [], available: [] }) + expect(sources[0]).toEqual({ + provider: "maple", + state: "built_in", + account: null, + problem: null, + events: 0, + lastEventAt: null, + }) + }) + + it("reads a connected Vercel team without the audit drain as off", () => { + const sources = auditTrailSources([], { + integrations: [entry({ resources: [resource({ name: "logs" })] })], + available: [], + }) + expect(stateOf(sources, "vercel")).toBe("off") + }) + + it("reads an audit drain that is on but has sent nothing as waiting, and one with events as receiving", () => { + const inventory = { + integrations: [entry({ resources: [resource({ name: "audit log" })] })], + available: [], + } + expect(stateOf(auditTrailSources([], inventory), "vercel")).toBe("waiting") + expect( + stateOf( + auditTrailSources([{ provider: "vercel", events: 4, lastEventAt: 5 }], inventory), + "vercel", + ), + ).toBe("receiving") + }) + + it("says why a broken feed fails", () => { + const sources = auditTrailSources([], { + integrations: [ + entry({ + provider: "gcp", + account: "project acme-prod", + resources: [ + resource({ + kind: "logs", + name: "project acme-prod", + status: "failing", + lastError: "sink missing", + }), + ], + }), + ], + available: [], + }) + const gcp = sourceOf(sources, "gcp") + expect(gcp?.state).toBe("failing") + expect(gcp?.problem).toBe("sink missing") + expect(gcp?.account).toBe("project acme-prod") + }) + + it("reads Google Cloud with log forwarding off as off: audit events ride the log sink", () => { + const sources = auditTrailSources([], { + integrations: [entry({ provider: "gcp", resources: [resource({ kind: "metrics" })] })], + available: [], + }) + expect(stateOf(sources, "gcp")).toBe("off") + }) + + it("keeps a platform a newer writer added, so its events are not unaccounted for", () => { + const sources = auditTrailSources([{ provider: "github", events: 2, lastEventAt: 9 }], null) + expect(Arr.map(sources, (source) => source.provider)).toEqual(["maple", "vercel", "gcp", "github"]) + expect(stateOf(sources, "github")).toBe("receiving") + }) +}) diff --git a/packages/backend/src/services/audit-trail/audit-trail-sources.ts b/packages/backend/src/services/audit-trail/audit-trail-sources.ts new file mode 100644 index 0000000000..866063d654 --- /dev/null +++ b/packages/backend/src/services/audit-trail/audit-trail-sources.ts @@ -0,0 +1,101 @@ +import { AuditTrailProvider } from "@maple/domain/audit-trail" +import type { AuditTrailSource, AuditTrailSourceState } from "@maple/domain/http" +import type { + IntegrationEntry, + IntegrationInventory, + IntegrationResource, +} from "@maple/domain/integration-inventory" +import { Array as Arr, Option } from "effect" + +// Which platforms feed the audit log and how each is doing: the events the table holds per +// platform, read against each integration's switches and health. Pure, like the inventory itself. + +/** What the table holds for one platform in the window. */ +export interface AuditTrailProviderStats { + readonly provider: string + readonly events: number + readonly lastEventAt: number | null +} + +/** Where each connectable platform's audit feed lives in its integration. */ +const FEED_OF = { + // The "Maple audit log" drain, one of the drains the Vercel integration installs. + vercel: (resource: IntegrationResource) => resource.kind === "drain" && resource.name === "audit log", + // Admin Activity and Policy Denied ride the log sink the setup script creates. + gcp: (resource: IntegrationResource) => resource.kind === "logs", +} as const satisfies Record, (resource: IntegrationResource) => boolean> + +type ConnectableProvider = keyof typeof FEED_OF + +const isBroken = (status: IntegrationResource["status"]) => status === "failing" || status === "disconnected" + +const connectableState = ( + provider: ConnectableProvider, + inventory: IntegrationInventory | null, + stats: AuditTrailProviderStats | undefined, +): { state: AuditTrailSourceState; account: string | null; problem: string | null } => { + const received = (stats?.events ?? 0) > 0 + // Without the inventory the page still says what arrived; a quiet feed reads as waiting. + if (inventory === null) return { state: received ? "receiving" : "waiting", account: null, problem: null } + const entry: IntegrationEntry | undefined = Option.getOrUndefined( + Arr.findFirst(inventory.integrations, (candidate) => candidate.provider === provider), + ) + if (entry === undefined) + // Events from a connection removed since still show; the platform reads as not connected. + return { state: "not_connected", account: null, problem: null } + const feeds = Arr.filter(entry.resources, FEED_OF[provider]) + if (entry.status === "disconnected") + return { state: "failing", account: entry.account, problem: entry.problem } + if (feeds.length === 0) return { state: "off", account: entry.account, problem: null } + const broken = Arr.findFirst(feeds, (feed) => isBroken(feed.status)) + if (Option.isSome(broken)) + return { + state: "failing", + account: entry.account, + problem: broken.value.lastError ?? broken.value.note ?? entry.problem, + } + return { state: received ? "receiving" : "waiting", account: entry.account, problem: null } +} + +/** + * Every platform the audit log can hold, Maple first, then the connectable ones, then any + * platform a newer writer added that this build has no setup for. `inventory` is null when the + * integration state could not be read. + */ +export const auditTrailSources = ( + stats: ReadonlyArray, + inventory: IntegrationInventory | null, +): ReadonlyArray => { + const statsOf = (provider: string) => + Option.getOrUndefined(Arr.findFirst(stats, (row) => row.provider === provider)) + const counts = (provider: string) => { + const row = statsOf(provider) + return { events: row?.events ?? 0, lastEventAt: row?.lastEventAt ?? null } + } + const known = AuditTrailProvider.literals + return [ + { provider: "maple", state: "built_in", account: null, problem: null, ...counts("maple") }, + ...Arr.map( + Arr.filter(known, (provider): provider is ConnectableProvider => provider !== "maple"), + (provider): AuditTrailSource => ({ + provider, + ...connectableState(provider, inventory, statsOf(provider)), + ...counts(provider), + }), + ), + ...Arr.map( + Arr.filter( + stats, + (row) => row.events > 0 && !Arr.some(known, (provider) => provider === row.provider), + ), + (row): AuditTrailSource => ({ + provider: row.provider, + state: "receiving", + account: null, + problem: null, + events: row.events, + lastEventAt: row.lastEventAt, + }), + ), + ] +} diff --git a/packages/domain/src/http/audit-trail.ts b/packages/domain/src/http/audit-trail.ts index 1a9b2c0c28..72ec7ff588 100644 --- a/packages/domain/src/http/audit-trail.ts +++ b/packages/domain/src/http/audit-trail.ts @@ -89,6 +89,46 @@ export const AuditTrailListResponse = Schema.Struct({ }) export type AuditTrailListResponse = typeof AuditTrailListResponse.Type +/** + * Where a platform's audit events stand. `built_in`: Maple's own, always on. `receiving`: events + * arrived in the window. `waiting`: the feed is on but nothing arrived yet. `off`: the platform is + * connected with its audit feed switched off. `failing`: the feed reports an error. + * `not_connected`: the platform is not connected at all. + */ +export const AuditTrailSourceState = Schema.Literals([ + "built_in", + "receiving", + "waiting", + "off", + "failing", + "not_connected", +]) +export type AuditTrailSourceState = typeof AuditTrailSourceState.Type + +/** One platform that can feed the audit log, and what it has sent. */ +export const AuditTrailSource = Schema.Struct({ + /** A plain string for the same reason as an entry's: a newer adapter must not fail the page. */ + provider: Schema.String, + state: AuditTrailSourceState, + /** The connected account, team or project scope, when the platform is connected. */ + account: Schema.NullOr(Schema.String), + /** Why a feed is failing, in the integration's words; null otherwise. */ + problem: Schema.NullOr(Schema.String), + /** Distinct events that happened in the last `windowDays`. */ + events: Schema.Finite, + /** Epoch ms of the newest event in the window, or null. */ + lastEventAt: Schema.NullOr(Schema.Finite), +}) +export type AuditTrailSource = typeof AuditTrailSource.Type + +/** Every platform that can feed the audit log, connected or not. */ +export const AuditTrailSourcesResponse = Schema.Struct({ + sources: Schema.Array(AuditTrailSource), + /** How far back `events` and `lastEventAt` look. */ + windowDays: Schema.Finite, +}) +export type AuditTrailSourcesResponse = typeof AuditTrailSourcesResponse.Type + // A detection is an alert rule that counts audit events as they arrive. Its incidents and // notifications carry the count, never an event. @@ -220,6 +260,12 @@ export class AuditTrailApiGroup extends HttpApiGroup.make("auditTrail") error: [AuditTrailPersistenceError, AuditTrailForbiddenError], }), ) + .add( + HttpApiEndpoint.get("sources", "/sources", { + success: AuditTrailSourcesResponse, + error: [AuditTrailPersistenceError, AuditTrailForbiddenError], + }), + ) .add( HttpApiEndpoint.get("listDetections", "/detections", { success: AuditDetectionListResponse, diff --git a/packages/query-engine/src/__sql_baseline__/catalog.sql b/packages/query-engine/src/__sql_baseline__/catalog.sql index 1b19fd1c40..65ca1bcfb4 100644 --- a/packages/query-engine/src/__sql_baseline__/catalog.sql +++ b/packages/query-engine/src/__sql_baseline__/catalog.sql @@ -454,6 +454,17 @@ SELECT LIMIT 51 FORMAT JSON +-- builder:audit-trail:auditTrailSourcesQuery:default [c248e070] +SELECT + audit_trail_events.Provider AS provider, + uniqExact(audit_trail_events.Id) AS events, + toUnixTimestamp(max(audit_trail_events.OccurredAt)) AS lastOccurredAt + FROM audit_trail_events + WHERE audit_trail_events.OrgId = 'org_sql_catalog' + AND audit_trail_events.OccurredAt >= '2026-01-01 10:30:00' + GROUP BY provider + FORMAT JSON + -- builder:aws-ingest:awsIngestQuery:default [c5891e34] SELECT metrics_sum.Attributes['aws.namespace'] AS namespace, diff --git a/packages/query-engine/src/benchmark/builders.ts b/packages/query-engine/src/benchmark/builders.ts index eabc06d1da..99176ba70c 100644 --- a/packages/query-engine/src/benchmark/builders.ts +++ b/packages/query-engine/src/benchmark/builders.ts @@ -1524,6 +1524,13 @@ export const builderFixtures: ReadonlyArray = [ }, ), }, + // Audit log sources (AuditTrailService.ts `providerStats`): events per platform in the window. + { + module: "audit-trail", + name: "auditTrailSourcesQuery", + label: "default", + compile: () => CH.compileUnsafe(CH.auditTrailSourcesQuery(), { orgId: ORG_ID, since: START_TIME }), + }, // Cancellation evidence (CancellationTelemetryEvidence.read). { module: "cancellation-evidence", diff --git a/packages/query-engine/src/ch/queries/audit-trail.ts b/packages/query-engine/src/ch/queries/audit-trail.ts index b1a22ad807..3ef23905b9 100644 --- a/packages/query-engine/src/ch/queries/audit-trail.ts +++ b/packages/query-engine/src/ch/queries/audit-trail.ts @@ -126,3 +126,27 @@ export function auditTrailCountQuery(opts: AuditTrailCountOpts = {}) { .format("JSON") .route("ingest") } + +export const auditTrailSourcesRowSchema = Schema.Struct({ + provider: Schema.String, + events: CHNumber, + /** Unix seconds of the newest event. */ + lastOccurredAt: CHNumber, +}) + +/** + * Per platform: how many of one org's events happened since `since`, and when the newest did. + * What the audit log's source list says each feed has sent. Params: orgId, since. + */ +export function auditTrailSourcesQuery() { + return from(Datasources.auditTrailEvents) + .select(($) => ({ + provider: $.Provider, + events: CH.uniqExact($.Id), + lastOccurredAt: CH.toUnixTimestamp(CH.max_($.OccurredAt)), + })) + .where(($) => [$.OrgId.eq(orgIdParam), $.OccurredAt.gte(param.dateTime("since"))]) + .groupBy("provider") + .format("JSON") + .route("ingest") +}