import { useMutation, useQueryClient } from '@tanstack/react-query' import type { Ref } from 'react' 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 ref?: Ref } 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 ( <> {label(node)} {node?.type === 'identity' && {node.kind}} ) } function Warnings({ items }: { items: string[] | undefined }) { return items?.map((w) => (

{w}

)) } 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 ( <>

{node.label}

{node.kind}

Policies

{out.length ? ( ) : (

No policies attached.

)} ) } 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 ( <>

{node.label}

{status}.

Used by

{into.length ? ( ) : (

Not attached to anything.

)}

Grants

{out.length ? ( ) : (

No grants.

)} {hcl && (
Policy HCL
{hcl}
)} ) } // path: who reaches it, via which policy return ( <>

{node.label}

Reached through

) } function EdgeDetails({ client, edge, full, policies, onRevoked }: { client: BaoClient; edge: GraphEdge; full: AccessGraph } & Pick) { 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 ( <>

{label(to)}

{edge.capabilities && ( <>

Capabilities

)} {isGrant && ( <> {managed?.editable && ownerMatch ? ( ) : (

This grant is in a policy EnvBao does not manage (or that was edited outside EnvBao), so it can’t be revoked here.

)} {revoke.error &&

{revoke.error.message}

} )} ) } export function Details({ client, full, policies, selection, onRevoked, ref }: Props) { let body =

Select a node or a line to see what it grants.

if (selection?.kind === 'node') { const node = nodeById(full, selection.id) if (node) body = } else if (selection?.kind === 'edge') { const edge = full.edges.find((e) => e.id === selection.id) if (edge) body = } return ( ) }