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
94 changes: 93 additions & 1 deletion apps/api/src/routes/internal/audit-trail.http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AuditTrailApiGroup,
type AuditTrailEntry,
AuditTrailListResponse,
AuditTrailSourcesResponse,
CurrentTenant,
OrgId,
RoleName,
Expand All @@ -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")
Expand All @@ -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[] = []
Expand Down Expand Up @@ -77,6 +83,7 @@ const makeHarness = (
page: AuditTrailPage,
directoryDown = false,
testDb: TestDb = createTestDb(trackedDbs),
inventoryDown = false,
) => {
const asked: Array<AuditTrailListFilters> = []
let directoryReads = 0
Expand All @@ -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),
Expand Down Expand Up @@ -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}`, {
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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()
}
})
})
34 changes: 33 additions & 1 deletion apps/api/src/routes/internal/audit-trail.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/** `<occurredAt ms>:<event id>`; the query schema has already checked the shape. */
const parseCursor = (cursor: string) => {
const [occurredAtMs, id = ""] = Str.split(":")(cursor)
Expand Down Expand Up @@ -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 }) =>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/runtime/service-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -250,6 +251,7 @@ export const HttpServicesLive = Layer.mergeAll(
AuditLogService.layer,
AuditTrailService.layer,
AuditDetectionsService.layer,
IntegrationInventoryService.layer,
WarehouseQueryService.layer,
QueryEngineService.layer,
RuntimeReleaseCatalog.layer,
Expand Down
57 changes: 56 additions & 1 deletion apps/web/src/lab/security-audit-log-fixture.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
},
],
}
16 changes: 13 additions & 3 deletions apps/web/src/lab/security-lab/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<DetectionsPage.Model, DetectionsPage.Message>
const DT = DetectionsPage.Message

Expand Down Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
Loading
Loading