Files
envbao-admin/src/features/graph/Details.tsx
T
mattandClaude Sonnet 5.5 0a2054eb41
CI / integration (push) Successful in 12s
CI / check (push) Successful in 15s
Polish pass: identity forms, confirm dialogs, policy chips, merged grants, narrow screens
- Tokens: bigger show-once token with primary Copy; warn when no policies are
  selected (OpenBao then inherits the creator's policies).
- Identity forms get headings, wider cards and two-column policy lists.
- Replace window.confirm with an in-app confirm dialog (falls back to
  window.confirm without a provider).
- Show policies, aliases and members as chips in the identity tables.
- Access panel: one row per secret instead of separate data/metadata rules.
- Stack graph details below 60rem; phone-width rules for tables, nav, toolbar.
- New favicon and page title; label the root "*" path as "everything".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 12:48:13 +01:00

228 lines
7.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useMutation, useQueryClient } from '@tanstack/react-query'
import type { BaoClient } from '../../api/bao'
import { revokeAccess } from '../../api/grants'
import type { AccessGraph, GraphEdge, GraphNode, PolicyInput } from '../../model/graph'
import { parseManagedPolicy, type GrantTarget } from '../../model/managed'
import { CapabilityChips } from '../shared/CapabilityChips'
import { useConfirm } from '../shared/useConfirm'
export type Selection = { kind: 'node'; id: string } | { kind: 'edge'; id: string }
interface Props {
client: BaoClient
full: AccessGraph
policies: PolicyInput[]
selection: Selection | null
onRevoked: () => void
}
const nodeById = (g: AccessGraph, id: string) => g.nodes.find((n) => n.id === id)
const label = (n: GraphNode | undefined) => n?.label ?? '?'
/** An identity's name with its kind, so "alice" the user and "alice" the entity read differently. */
function Who({ node }: { node: GraphNode | undefined }) {
return (
<>
<span>{label(node)}</span>
{node?.type === 'identity' && <span className="tag">{node.kind}</span>}
</>
)
}
function Warnings({ items }: { items: string[] | undefined }) {
return items?.map((w) => (
<p key={w} className="note-warn">
{w}
</p>
))
}
function NodeDetails({ node, full, policies }: { node: GraphNode; full: AccessGraph; policies: PolicyInput[] }) {
const out = full.edges.filter((e) => e.source === node.id)
const into = full.edges.filter((e) => e.target === node.id)
if (node.type === 'identity') {
return (
<>
<header className="detail-head">
<h2>{node.label}</h2>
<span className="tag">{node.kind}</span>
</header>
<h3>Policies</h3>
{out.length ? (
<ul className="rows">
{out.map((e) => (
<li key={e.id}>
<code>{label(nodeById(full, e.target))}</code>
</li>
))}
</ul>
) : (
<p className="hint">No policies attached.</p>
)}
</>
)
}
if (node.type === 'policy') {
const hcl = policies.find((p) => p.name === node.label)?.hcl
const status = node.missing ? 'Attached but does not exist' : node.managed ? 'Managed by EnvBao' : 'Not managed by EnvBao (read-only)'
return (
<>
<header className="detail-head">
<h2>{node.label}</h2>
</header>
<p className="hint">{status}.</p>
<Warnings items={node.warnings} />
<h3>Used by</h3>
{into.length ? (
<ul className="rows">
{into.map((e) => (
<li key={e.id}>
<Who node={nodeById(full, e.source)} />
</li>
))}
</ul>
) : (
<p className="hint">Not attached to anything.</p>
)}
<h3>Grants</h3>
{out.length ? (
<ul className="rows">
{out.map((e) => (
<li key={e.id}>
<code>{label(nodeById(full, e.target))}</code>
{e.capabilities && <CapabilityChips capabilities={e.capabilities} />}
</li>
))}
</ul>
) : (
<p className="hint">No grants.</p>
)}
{hcl && (
<details>
<summary>Policy HCL</summary>
<pre>{hcl}</pre>
</details>
)}
</>
)
}
// path: who reaches it, via which policy
return (
<>
<header className="detail-head">
<h2>
<code>{node.label}</code>
</h2>
</header>
<h3>Reached through</h3>
<ul className="rows">
{into.map((e) => {
const users = full.edges.filter((x) => x.target === e.source).map((x) => nodeById(full, x.source))
return (
<li key={e.id} className="stacked">
<div className="row-main">
<code>{label(nodeById(full, e.source))}</code>
{e.capabilities && <CapabilityChips capabilities={e.capabilities} />}
</div>
{users.length ? (
<ul className="people">
{users.map((u, i) => (
<li key={i}>
<Who node={u} />
</li>
))}
</ul>
) : (
<p className="hint">No identity uses this policy.</p>
)}
</li>
)
})}
</ul>
</>
)
}
function EdgeDetails({ client, edge, full, policies, onRevoked }: { client: BaoClient; edge: GraphEdge; full: AccessGraph } & Pick<Props, 'policies' | 'onRevoked'>) {
const qc = useQueryClient()
const confirm = useConfirm()
const from = nodeById(full, edge.source)
const to = nodeById(full, edge.target)
const isGrant = from?.type === 'policy' && to?.type === 'path'
const hcl = isGrant ? policies.find((p) => p.name === from.label)?.hcl : null
const managed = hcl ? parseManagedPolicy(hcl) : null
const ownerMatch = managed?.owner ? /^(user|entity|group):(.+)$/.exec(managed.owner) : null
const revoke = useMutation({
mutationFn: () => revokeAccess(client, { kind: ownerMatch![1] as GrantTarget, name: ownerMatch![2] }, [to!.label]),
onSuccess: async () => {
await Promise.all(['access-inputs', 'grant-state', 'users', 'entities', 'groups'].map((k) => qc.invalidateQueries({ queryKey: [k] })))
onRevoked()
},
})
return (
<>
<header className="detail-head">
<h2>
<Who node={from} />
</h2>
</header>
<p className="edge-to">
<span aria-hidden="true">→</span> <code>{label(to)}</code>
</p>
{edge.capabilities && (
<>
<h3>Capabilities</h3>
<CapabilityChips capabilities={edge.capabilities} />
</>
)}
<Warnings items={edge.warnings} />
{isGrant && (
<>
{managed?.editable && ownerMatch ? (
<button
type="button"
className="danger"
onClick={async () => {
const ok = await confirm({
title: `Revoke access to ${to!.label}?`,
message: `Removes it from ${ownerMatch[1]} “${ownerMatch[2]}”. Other policies are unaffected.`,
confirmLabel: 'Revoke',
})
if (ok) revoke.mutate()
}}
disabled={revoke.isPending}
>
Revoke
</button>
) : (
<p className="hint">This grant is in a policy EnvBao does not manage (or that was edited outside EnvBao), so it can’t be revoked here.</p>
)}
{revoke.error && <p role="alert">{revoke.error.message}</p>}
</>
)}
</>
)
}
export function Details({ client, full, policies, selection, onRevoked }: Props) {
let body = <p className="detail-empty">Select a node or a line to see what it grants.</p>
if (selection?.kind === 'node') {
const node = nodeById(full, selection.id)
if (node) body = <NodeDetails node={node} full={full} policies={policies} />
} else if (selection?.kind === 'edge') {
const edge = full.edges.find((e) => e.id === selection.id)
if (edge) body = <EdgeDetails client={client} edge={edge} full={full} policies={policies} onRevoked={onRevoked} />
}
return (
<aside className="graph-details" aria-label="Details">
{body}
</aside>
)
}