Add Access Graph view with filters, details panel and revoke
CI / integration (push) Successful in 12s
CI / check (push) Successful in 15s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-10-20 11:34:11 +01:00
1 parent 5180e1bb74
commit 265c618884
11 files changed
+731 -54

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import type { AccessGraph } from './graph'
export interface GraphFilter {
/** Case-insensitive substring of an identity name. */
identity: string
/** Case-insensitive substring of a path. */
path: string
/** Policy names to hide (and the paths only they reach). */
hidePolicies: string[]
}
export const noFilter: GraphFilter = { identity: '', path: '', hidePolicies: [] }
const matches = (label: string, q: string) => label.toLowerCase().includes(q.trim().toLowerCase())
function reach(graph: AccessGraph, seeds: Set<string>, dir: 'down' | 'up'): Set<string> {
const seen = new Set(seeds)
const queue = [...seeds]
while (queue.length) {
const id = queue.pop()!
for (const e of graph.edges) {
const [from, to] = dir === 'down' ? [e.source, e.target] : [e.target, e.source]
if (from === id && !seen.has(to)) {
seen.add(to)
queue.push(to)
}
}
}
return seen
}
/**
* Identity filter keeps matching identities and everything they reach; path filter keeps matching
* paths and everything that reaches them. With both, only what satisfies both remains.
*/
export function filterGraph(graph: AccessGraph, f: GraphFilter): AccessGraph {
let nodes = graph.nodes.filter((n) => !(n.type === 'policy' && f.hidePolicies.includes(n.label)))
let edges = graph.edges.filter((e) => nodes.some((n) => n.id === e.source) && nodes.some((n) => n.id === e.target))
let g: AccessGraph = { nodes, edges }
// Hiding a policy can leave paths nothing reaches any more.
const reached = new Set(g.edges.map((e) => e.target))
g = { ...g, nodes: g.nodes.filter((n) => n.type !== 'path' || reached.has(n.id)) }
const sets: Set<string>[] = []
if (f.identity.trim()) {
const seeds = new Set(g.nodes.filter((n) => n.type === 'identity' && matches(n.label, f.identity)).map((n) => n.id))
sets.push(reach(g, seeds, 'down'))
}
if (f.path.trim()) {
const seeds = new Set(g.nodes.filter((n) => n.type === 'path' && matches(n.label, f.path)).map((n) => n.id))
sets.push(reach(g, seeds, 'up'))
}
if (sets.length) {
const keep = sets.reduce((a, b) => new Set([...a].filter((id) => b.has(id))))
nodes = g.nodes.filter((n) => keep.has(n.id))
edges = g.edges.filter((e) => keep.has(e.source) && keep.has(e.target))
g = { nodes, edges }
}
return g
}
+76
View File
@@ -0,0 +1,76 @@
import { describe, expect, test } from 'vitest'
import { buildAccessGraph, type Identity } from './graph'
import { filterGraph, noFilter } from './graphFilter'
import { COLUMN_X, layoutColumns } from './layout'
const identities: Identity[] = [
{ kind: 'user', name: 'alice', policies: ['default', 'app'] },
{ kind: 'user', name: 'bob', policies: ['default', 'db'] },
{ kind: 'group', name: 'ops', policies: ['db'] },
]
const policies = [
{ name: 'default', hcl: 'path "auth/token/lookup-self" { capabilities = ["read"] }' },
{ name: 'app', hcl: 'path "secret/data/app/*" { capabilities = ["read"] }' },
{ name: 'db', hcl: 'path "secret/data/db" { capabilities = ["read", "update"] }\npath "secret/data/app/*" { capabilities = ["list"] }' },
{ name: 'orphan', hcl: 'path "secret/data/lonely" { capabilities = ["read"] }' },
]
const graph = buildAccessGraph(identities, policies)
const labels = (g: typeof graph, type: string) => g.nodes.filter((n) => n.type === type).map((n) => n.label).sort()
describe('layoutColumns', () => {
const pos = layoutColumns(graph)
test('places every node in its type column, including unattached policies', () => {
for (const n of graph.nodes) expect(pos.get(n.id)!.x).toBe(COLUMN_X[n.type])
expect(pos.get('policy:orphan')!.x).toBe(COLUMN_X.policy)
})
test('gives nodes in a column distinct rows', () => {
const ys = graph.nodes.filter((n) => n.type === 'policy').map((n) => pos.get(n.id)!.y)
expect(new Set(ys).size).toBe(ys.length)
})
test('handles an empty graph', () => expect(layoutColumns({ nodes: [], edges: [] }).size).toBe(0))
})
describe('filterGraph', () => {
test('no filter returns everything', () => expect(filterGraph(graph, noFilter)).toEqual(graph))
test('identity filter keeps what the identity reaches', () => {
const g = filterGraph(graph, { ...noFilter, identity: 'ALI' })
expect(labels(g, 'identity')).toEqual(['alice'])
expect(labels(g, 'policy')).toEqual(['app', 'default'])
expect(labels(g, 'path')).toEqual(['auth/token/lookup-self', 'secret/data/app/*'])
})
test('path filter keeps everything that reaches the path', () => {
const g = filterGraph(graph, { ...noFilter, path: 'secret/data/db' })
expect(labels(g, 'identity')).toEqual(['bob', 'ops'])
expect(labels(g, 'policy')).toEqual(['db'])
// the policy's other rule is not part of what we asked about only if unreachable; here it is the same policy
expect(labels(g, 'path')).toContain('secret/data/db')
})
test('both filters narrow to the intersection', () => {
const g = filterGraph(graph, { ...noFilter, identity: 'bob', path: 'app' })
expect(labels(g, 'identity')).toEqual(['bob'])
expect(labels(g, 'path')).toEqual(['secret/data/app/*'])
expect(labels(g, 'policy')).toEqual(['db'])
})
test('no match yields an empty graph', () => {
expect(filterGraph(graph, { ...noFilter, identity: 'nobody' }).nodes).toEqual([])
})
test('hiding a policy drops paths only it reached', () => {
const g = filterGraph(graph, { ...noFilter, hidePolicies: ['default'] })
expect(labels(g, 'policy')).not.toContain('default')
expect(labels(g, 'path')).not.toContain('auth/token/lookup-self')
expect(g.edges.every((e) => g.nodes.some((n) => n.id === e.source) && g.nodes.some((n) => n.id === e.target))).toBe(true)
})
test('hiding a policy keeps paths other policies still reach', () => {
const g = filterGraph(graph, { ...noFilter, hidePolicies: ['app'] })
expect(labels(g, 'path')).toContain('secret/data/app/*') // still reached by db
})
})
+54
View File
@@ -0,0 +1,54 @@
import type { AccessGraph, GraphNode } from './graph'
export const COLUMN_X = { identity: 0, policy: 380, path: 780 } as const
export const ROW_HEIGHT = 72
export interface Position {
x: number
y: number
}
/**
* Strict three-column layout (identities | policies | paths). Rows within each column are
* ordered by repeated barycenter sweeps to keep edge crossings low.
*/
export function layoutColumns(graph: AccessGraph): Map<string, Position> {
const cols: Record<GraphNode['type'], GraphNode[]> = { identity: [], policy: [], path: [] }
for (const n of graph.nodes) cols[n.type].push(n)
for (const list of Object.values(cols)) list.sort((a, b) => a.label.localeCompare(b.label))
const neighbours = new Map<string, string[]>()
const link = (a: string, b: string) => neighbours.set(a, [...(neighbours.get(a) ?? []), b])
for (const e of graph.edges) {
link(e.source, e.target)
link(e.target, e.source)
}
const index = new Map<string, number>()
const reindex = () => Object.values(cols).forEach((list) => list.forEach((n, i) => index.set(n.id, i)))
reindex()
const sweep = (list: GraphNode[]) => {
const key = new Map(
list.map((n, i) => {
const ns = neighbours.get(n.id) ?? []
return [n.id, ns.length ? ns.reduce((sum, id) => sum + (index.get(id) ?? 0), 0) / ns.length : i] as const
}),
)
// stable: ties keep the alphabetical order from before
list.sort((a, b) => key.get(a.id)! - key.get(b.id)!)
}
for (let i = 0; i < 4; i++) {
sweep(cols.policy)
reindex()
sweep(cols.identity)
sweep(cols.path)
reindex()
}
const pos = new Map<string, Position>()
for (const type of ['identity', 'policy', 'path'] as const) {
cols[type].forEach((n, i) => pos.set(n.id, { x: COLUMN_X[type], y: i * ROW_HEIGHT }))
}
return pos
}