Files
envbao-admin/src/features/secrets/SecretsPage.tsx
T
mattandClaude Sonnet 5.5 1b3f6e4de5 Secrets: delete a secrets engine with a typed confirmation
The delete panel counts what will be lost (KV entries, or roles and
leases for dynamic engines), lists the policies that still grant access
under the mount, and keeps Delete disabled until the mount path is
typed. Anything it can't count is said so rather than hidden. Switching
mounts closes the panel, and the deleted mount's cached data is dropped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 14:25:21 +00:00

205 lines
6.4 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 { useQuery } from '@tanstack/react-query'
import { useState } from 'react'
import type { BaoClient } from '../../api/bao'
import { engineFor, mountKind, mountTypeFor } from '../../api/engines/registry'
import { listSecrets } from '../../api/secrets'
import { RequireConnection } from '../shared/RequireConnection'
import { useMounts } from '../shared/useMounts'
import { AccessPanel } from './AccessPanel'
import { DeleteMountDialog } from './DeleteMountDialog'
import { EngineView } from './EngineView'
import { MountDialog } from './MountDialog'
function Browser({ client }: { client: BaoClient }) {
const mounts = useMounts(client)
const [mountChoice, setMount] = useState<string | null>(null)
const [prefix, setPrefix] = useState('')
const [selected, setSelected] = useState<string | null>(null)
const [dialog, setDialog] = useState<'create' | 'edit' | 'delete' | null>(null)
const current = mounts.data?.find((m) => m.path === mountChoice) ?? mounts.data?.[0] ?? null
const mount = current?.path ?? null
const isKv = current !== null && mountKind(current) === 'kv'
const entries = useQuery({
queryKey: ['kv-list', mount, prefix],
queryFn: () => listSecrets(client, mount!, prefix),
enabled: isKv,
})
const crumbs = prefix.split('/').filter(Boolean)
const go = (p: string) => {
setPrefix(p)
setSelected(null)
}
if (mounts.error) return <p role="alert">{mounts.error.message}</p>
if (mounts.isPending) return <p>Loading…</p>
const existing = mounts.data.map((m) => m.path)
const closeDialog = () => setDialog(null)
const manage = (
<div className="mount-actions">
<button type="button" onClick={() => setDialog(dialog === 'create' ? null : 'create')} aria-expanded={dialog === 'create'}>
New secrets engine
</button>
{current && mountTypeFor(current) && (
<button type="button" onClick={() => setDialog(dialog === 'edit' ? null : 'edit')} aria-expanded={dialog === 'edit'}>
Settings
</button>
)}
{current && (
<button type="button" onClick={() => setDialog(dialog === 'delete' ? null : 'delete')} aria-expanded={dialog === 'delete'}>
Delete
</button>
)}
</div>
)
const dialogs = dialog === 'delete' && current ? (
<DeleteMountDialog
key={current.path}
client={client}
mount={current}
onClose={closeDialog}
onDeleted={() => {
closeDialog()
// Fall back to the first remaining mount.
setMount(null)
go('')
}}
/>
) : dialog && dialog !== 'delete' && (
<MountDialog
// Remount per mount so one mount's settings are never shown (or saved) for another.
key={dialog === 'edit' ? current?.path : 'create'}
client={client}
existing={existing}
mount={dialog === 'edit' && current ? current : undefined}
onClose={closeDialog}
onDone={(path) => {
closeDialog()
// A new mount starts at its root; saving settings keeps your place.
if (path !== mount) {
setMount(path)
go('')
}
}}
/>
)
if (!current || !mount) {
return (
<>
<p>No secrets engines are mounted.</p>
{manage}
{dialogs}
</>
)
}
const where = prefix ? `${mount}${prefix}` : mount
const kv = mounts.data.filter((m) => mountKind(m) === 'kv')
const dynamic = mounts.data.filter((m) => mountKind(m) === 'dynamic')
const mountSelect = mounts.data.length > 1 && (
<select
aria-label="Mount"
value={mount}
onChange={(e) => {
setMount(e.target.value)
go('')
// A settings or delete panel belongs to the mount it was opened for.
setDialog(null)
}}
>
{kv.length > 0 && (
<optgroup label="KV">
{kv.map((m) => (
<option key={m.path}>{m.path}</option>
))}
</optgroup>
)}
{[...new Set(dynamic.map((m) => m.type))].map((type) => (
<optgroup key={type} label={engineFor(type).label}>
{dynamic
.filter((m) => m.type === type)
.map((m) => (
<option key={m.path}>{m.path}</option>
))}
</optgroup>
))}
</select>
)
if (!isKv) {
return (
<>
{manage}
{dialogs}
<div className="secrets-layout">
<div className="secrets-browser">
<nav aria-label="Path" className="crumbs">
{mountSelect}
</nav>
</div>
<EngineView client={client} mount={current} />
</div>
</>
)
}
return (
<>
{manage}
{dialogs}
<div className="secrets-layout">
<div className="secrets-browser">
<nav aria-label="Path" className="crumbs">
{mountSelect}
<button type="button" onClick={() => go('')}>
{mount}
</button>
{crumbs.map((c, i) => (
<button key={i} type="button" onClick={() => go(crumbs.slice(0, i + 1).join('/') + '/')}>
{c}/
</button>
))}
</nav>
{entries.isPending && <p className="hint">Loading…</p>}
{entries.error && <p role="alert">{entries.error.message}</p>}
{entries.data?.length === 0 && <p className="hint">No secrets in {where} yet.</p>}
{!!entries.data?.length && (
<ul className="entries" aria-label={`Contents of ${where}`}>
{entries.data.map((e) => (
<li key={e.path}>
<button
type="button"
className={e.isFolder ? 'entry-folder' : undefined}
aria-pressed={e.isFolder ? undefined : selected === e.path}
onClick={() => (e.isFolder ? go(e.path) : setSelected(e.path))}
>
{e.name}
{e.isFolder && <span aria-hidden="true">›</span>}
</button>
</li>
))}
</ul>
)}
</div>
{selected ? (
<AccessPanel key={`${mount}${selected}`} client={client} mount={mount} secret={selected} />
) : (
<p className="secret-placeholder">Select a secret to see who can reach it.</p>
)}
</div>
</>
)
}
export function SecretsPage() {
return (
<section>
<h1>Secrets</h1>
<RequireConnection>{(client) => <Browser client={client} />}</RequireConnection>
</section>
)
}