Secrets: browse, renew and revoke leases for dynamic engines

Adds a Leases tab that walks sys/leases/lookup under the mount, shows a
lease's timings on demand, renews renewable leases, and revokes one
lease or everything under a prefix after confirmation. Covered by unit
tests and an ssh OTP integration test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-11-09 13:45:14 +00:00
1 parent 48746e2150
commit 48ee757e8d
7 files changed
+362 -11

No files matched your search

+5 -2
View File
@@ -3,16 +3,18 @@ import type { BaoClient } from '../../api/bao'
import { engineFor } from '../../api/engines/registry'
import type { Mount } from '../../api/secrets'
import { ConfigPanel } from './ConfigPanel'
import { LeasesPanel } from './LeasesPanel'
import { RolesPanel } from './RolesPanel'
type Tab = 'roles' | 'config'
type Tab = 'roles' | 'config' | 'leases'
/** Roles and Config (and, in a later phase, Leases) for a dynamic secrets engine. */
/** Roles, Config and Leases for a dynamic secrets engine. */
export function EngineView({ client, mount }: { client: BaoClient; mount: Mount }) {
const engine = engineFor(mount.type)
const [tab, setTab] = useState<Tab>('roles')
const tabs: { id: Tab; label: string }[] = [{ id: 'roles', label: 'Roles' }]
if (engine.configs.length > 0) tabs.push({ id: 'config', label: 'Config' })
tabs.push({ id: 'leases', label: 'Leases' })
const current = tabs.some((t) => t.id === tab) ? tab : 'roles'
return (
@@ -30,6 +32,7 @@ export function EngineView({ client, mount }: { client: BaoClient; mount: Mount
</div>
)}
{current === 'roles' && <RolesPanel key={mount.path} client={client} mount={mount.path} engine={engine} />}
{current === 'leases' && <LeasesPanel key={mount.path} client={client} mount={mount.path} />}
{current === 'config' && <ConfigPanel key={mount.path} client={client} mount={mount.path} engine={engine} />}
</div>
)
+99
View File
@@ -0,0 +1,99 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import { BaoClient } from '../../api/bao'
import * as leases from '../../api/engines/leases'
import { LeasesPanel } from './LeasesPanel'
vi.mock('../../api/engines/leases')
const client = new BaoClient('http://b', 't')
function setup() {
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<LeasesPanel client={client} mount="database/" />
</QueryClientProvider>,
)
return userEvent.setup()
}
beforeEach(() => {
vi.mocked(leases.listLeases).mockImplementation(async (_c, prefix) =>
prefix === 'database/'
? [{ name: 'creds/', path: 'database/creds/', isFolder: true }]
: prefix === 'database/creds/'
? [{ name: 'ro/', path: 'database/creds/ro/', isFolder: true }]
: [{ name: 'abc', path: 'database/creds/ro/abc', isFolder: false }],
)
vi.mocked(leases.lookupLease).mockResolvedValue({
id: 'database/creds/ro/abc',
issueTime: '2026-01-01T00:00:00Z',
expireTime: '2026-01-01T01:00:00Z',
lastRenewal: null,
renewable: true,
ttl: 3000,
})
})
afterEach(() => vi.resetAllMocks())
async function openLease(user: ReturnType<typeof userEvent.setup>) {
await user.click(await screen.findByRole('button', { name: /creds\// }))
await user.click(await screen.findByRole('button', { name: /ro\// }))
}
test('browses folders down to a lease and shows its details', async () => {
const user = setup()
await openLease(user)
await user.click(await screen.findByRole('button', { name: 'Details abc' }))
const details = await screen.findByLabelText('Lease abc')
expect(within(details).getByText('3000s')).toBeInTheDocument()
expect(leases.lookupLease).toHaveBeenCalledWith(client, 'database/creds/ro/abc')
})
test('renews a renewable lease', async () => {
vi.mocked(leases.renewLease).mockResolvedValue(3600)
const user = setup()
await openLease(user)
await user.click(await screen.findByRole('button', { name: 'Details abc' }))
await user.click(await screen.findByRole('button', { name: 'Renew' }))
expect(leases.renewLease).toHaveBeenCalledWith(client, 'database/creds/ro/abc')
})
test('revokes one lease only after confirmation', async () => {
vi.mocked(leases.revokeLease).mockResolvedValue()
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
const user = setup()
await openLease(user)
await user.click(await screen.findByRole('button', { name: 'Revoke abc' }))
expect(leases.revokeLease).not.toHaveBeenCalled()
confirm.mockReturnValue(true)
await user.click(screen.getByRole('button', { name: 'Revoke abc' }))
expect(leases.revokeLease).toHaveBeenCalledWith(client, 'database/creds/ro/abc')
})
test('revoke-all is offered below the mount root and names the prefix', async () => {
vi.mocked(leases.revokePrefix).mockResolvedValue()
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(true)
const user = setup()
await screen.findByRole('button', { name: /creds\// })
expect(screen.queryByRole('button', { name: /Revoke all/ })).not.toBeInTheDocument()
await openLease(user)
await user.click(await screen.findByRole('button', { name: 'Revoke all under this prefix' }))
expect(confirm.mock.calls[0][0]).toContain('database/creds/ro/')
expect(leases.revokePrefix).toHaveBeenCalledWith(client, 'database/creds/ro/')
})
test('says so when there are no leases, and why listing failed', async () => {
vi.mocked(leases.listLeases).mockResolvedValueOnce([])
setup()
expect(await screen.findByText('No leases under database/.')).toBeInTheDocument()
})
test('shows the error when listing is not permitted', async () => {
vi.mocked(leases.listLeases).mockRejectedValue(new Error('permission denied'))
setup()
expect(await screen.findByRole('alert')).toHaveTextContent('permission denied')
})
+134
View File
@@ -0,0 +1,134 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useState } from 'react'
import type { BaoClient } from '../../api/bao'
import { listLeases, lookupLease, renewLease, revokeLease, revokePrefix } from '../../api/engines/leases'
import { ListState } from '../shared/ListState'
import { useConfirm } from '../shared/useConfirm'
const when = (iso: string | null) => (iso ? new Date(iso).toLocaleString() : 'never')
function LeaseRow({ client, id, name, onChanged }: { client: BaoClient; id: string; name: string; onChanged: () => void }) {
const confirm = useConfirm()
const [open, setOpen] = useState(false)
const info = useQuery({ queryKey: ['lease', id], queryFn: () => lookupLease(client, id), enabled: open, retry: false })
const renew = useMutation({
mutationFn: () => renewLease(client, id),
onSuccess: () => info.refetch(),
})
const revoke = useMutation({ mutationFn: () => revokeLease(client, id), onSuccess: onChanged })
return (
<li>
<span>{name}</span>
<button type="button" aria-expanded={open} aria-label={`Details ${name}`} onClick={() => setOpen(!open)}>
Details
</button>
<button
type="button"
aria-label={`Revoke ${name}`}
onClick={async () => {
if (await confirm({ title: 'Revoke this lease?', message: 'The credentials it issued stop working immediately.', confirmLabel: 'Revoke' }))
revoke.mutate()
}}
>
Revoke
</button>
{open && info.isPending && <p role="status">Loading…</p>}
{open && info.error && <p role="alert">{info.error.message}</p>}
{open && info.data && (
<dl aria-label={`Lease ${name}`}>
<dt>Issued</dt>
<dd>{when(info.data.issueTime)}</dd>
<dt>Expires</dt>
<dd>{when(info.data.expireTime)}</dd>
<dt>Remaining</dt>
<dd>{info.data.ttl}s</dd>
{info.data.lastRenewal && (
<>
<dt>Last renewed</dt>
<dd>{when(info.data.lastRenewal)}</dd>
</>
)}
{info.data.renewable && (
<dd>
<button type="button" onClick={() => renew.mutate()} disabled={renew.isPending}>
Renew
</button>
</dd>
)}
</dl>
)}
{renew.error && <p role="alert">{renew.error.message}</p>}
{revoke.error && <p role="alert">{revoke.error.message}</p>}
</li>
)
}
/** Browse and revoke the leases issued by a dynamic engine mount. */
export function LeasesPanel({ client, mount }: { client: BaoClient; mount: string }) {
const qc = useQueryClient()
const confirm = useConfirm()
const [prefix, setPrefix] = useState(mount)
const key = ['leases', mount, prefix]
const entries = useQuery({ queryKey: key, queryFn: () => listLeases(client, prefix), retry: false })
const refresh = () => qc.invalidateQueries({ queryKey: ['leases', mount] })
const revokeAll = useMutation({
mutationFn: () => revokePrefix(client, prefix),
onSuccess: refresh,
})
const crumbs = prefix.slice(mount.length).split('/').filter(Boolean)
const atRoot = prefix === mount
return (
<section aria-label="Leases">
<nav aria-label="Lease path" className="crumbs">
<button type="button" onClick={() => setPrefix(mount)}>
{mount}
</button>
{crumbs.map((c, i) => (
<button key={i} type="button" onClick={() => setPrefix(mount + crumbs.slice(0, i + 1).join('/') + '/')}>
{c}/
</button>
))}
</nav>
<ListState query={entries} empty={`No leases under ${prefix}.`} />
{!!entries.data?.length && (
<ul className="entries" aria-label={`Leases under ${prefix}`}>
{entries.data.map((e) =>
e.isFolder ? (
<li key={e.path}>
<button type="button" className="entry-folder" onClick={() => setPrefix(e.path)}>
{e.name}
<span aria-hidden="true">›</span>
</button>
</li>
) : (
<LeaseRow key={e.path} client={client} id={e.path} name={e.name} onChanged={refresh} />
),
)}
</ul>
)}
{!atRoot && (
<button
type="button"
onClick={async () => {
if (
await confirm({
title: `Revoke every lease under ${prefix}?`,
message: 'All credentials issued under this prefix stop working immediately. This cannot be undone.',
confirmLabel: 'Revoke all',
})
)
revokeAll.mutate()
}}
disabled={revokeAll.isPending}
>
Revoke all under this prefix
</button>
)}
{revokeAll.error && <p role="alert">{revokeAll.error.message}</p>}
</section>
)
}