import { useMutation, useQuery } from '@tanstack/react-query' import { useState, type FormEvent } from 'react' import type { BaoClient } from '../../api/bao' import { getGrantState, grantAccess, revokeAccess, type Target } from '../../api/grants' import { engineFor, mountKind } from '../../api/engines/registry' import { readPolicyHcl } from '../../api/secrets' import { dynamicGrantRules, kvGrantRules, validateGrantPath, type DynamicAccess, type KvCapability } from '../../model/managed' import { groupGrants } from '../../model/grantRows' import { CapabilityChips } from '../shared/CapabilityChips' import { CapabilityPicker } from '../shared/CapabilityPicker' import { DynamicAccessPicker } from '../shared/DynamicAccessPicker' import { useMounts } from '../shared/useMounts' import { useRefreshAccess } from '../shared/useRefreshAccess' import { PathPicker } from './PathPicker' import { RolePicker, type RoleSelection } from './RolePicker' import { useConfirm } from '../shared/useConfirm' interface Props { client: BaoClient target: Target onClose: () => void } function PolicyView({ client, name }: { client: BaoClient; name: string }) { const [open, setOpen] = useState(false) const hcl = useQuery({ queryKey: ['policy-hcl', name], queryFn: () => readPolicyHcl(client, name), enabled: open }) return (
  • {name}{' '} {open &&
    {hcl.isPending ? 'Loading…' : (hcl.data ?? 'Could not read this policy.')}
    }
  • ) } export function AccessEditor({ client, target, onClose }: Props) { const confirm = useConfirm() const stateKey = ['grant-state', target.kind, target.name] const state = useQuery({ queryKey: stateKey, queryFn: () => getGrantState(client, target) }) const mounts = useMounts(client) const [mountChoice, setMount] = useState(null) const [secretPath, setSecretPath] = useState('') const [caps, setCaps] = useState(['read']) const [roleSel, setRoleSel] = useState({ kindId: '', role: '' }) const [access, setAccess] = useState(['generate']) const [showRaw, setShowRaw] = useState(false) const [formError, setFormError] = useState(null) const current = mounts.data?.find((m) => m.path === mountChoice) ?? mounts.data?.[0] const mount = current?.path ?? '' const dynamic = current !== undefined && mountKind(current) === 'dynamic' const engine = dynamic ? engineFor(current.type) : null const roleKind = engine ? (engine.roleKinds.find((k) => k.id === roleSel.kindId) ?? engine.roleKinds[0]) : null const done = useRefreshAccess() const grant = useMutation({ mutationFn: () => grantAccess(client, target, roleKind ? dynamicGrantRules(roleKind, mount, roleSel.role, access) : kvGrantRules(mount, secretPath, caps)), onSuccess: () => { setSecretPath('') setRoleSel({ kindId: roleSel.kindId, role: '' }) return done() }, }) const revoke = useMutation({ mutationFn: (paths: string[]) => revokeAccess(client, target, paths), onSuccess: done }) function submit(e: FormEvent) { e.preventDefault() const err = roleKind ? (roleSel.role ? validateGrantPath(`${mount}${roleKind.path}/${roleSel.role}`) : 'Choose a role.') ?? (access.length === 0 ? 'Select at least one kind of access.' : null) : (validateGrantPath(secretPath) ?? (caps.length === 0 ? 'Select at least one capability.' : null)) setFormError(err) if (!err) grant.mutate() } const s = state.data const rows = groupGrants(s?.managed?.grants ?? [], mounts.data?.map((m) => m.path) ?? []) const editable = !s?.managed || s.managed.editable return (

    Access for {target.name} {target.kind}

    {state.error &&

    {state.error.message}

    } {s && (

    Granted by EnvBao

    {s.hcl && ( )}
    {s.managed && !s.managed.editable && (

    This policy was changed outside EnvBao and is read-only here: {s.managed.reasons.join('; ')}

    )} {showRaw &&
    {s.hcl}
    } {rows.length ? ( {rows.map((row) => ( ))}
    Secret Can
    {row.display} {row.wildcard && everything inside}
    ) : (

    No access granted through EnvBao yet.

    )} {revoke.error &&

    {revoke.error.message}

    }

    Other policies

    Set outside EnvBao, so they can only be viewed here.

    {s.otherPolicies.length ? (
      {s.otherPolicies.map((p) => ( ))}
    ) : (

    None.

    )}
    {editable && (

    Grant access

    {mount && engine && } {mount && !engine && } {engine ? : }

    {engine ? 'Granting a role that already has a grant replaces its access. Unticked access is removed.' : 'Granting a path that already has a grant replaces its capabilities. “list” applies to the secret’s metadata path.'}

    {(formError || grant.error) &&

    {formError ?? grant.error?.message}

    } )}
    )}
    ) }