Add KV v2 secret browser with per-secret Access panel
Model follows OpenBao's most-specific-path rule rather than unioning matches, verified against sys/capabilities-self in integration tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f2ba697093
commit
cd1438987b
11 files changed
+604
-30
No files matched your search
@@ -0,0 +1,95 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { BaoClient } from '../../api/bao'
|
||||
import { listEntities, listGroups, listPolicyNames, listUsers } from '../../api/identities'
|
||||
import { dataPath, readPolicyHcl } from '../../api/secrets'
|
||||
import { listTokens } from '../../api/tokens'
|
||||
import { accessForPath } from '../../model/graph'
|
||||
import { resolveIdentities } from '../../model/identities'
|
||||
import { pathMatches } from '../../model/policy'
|
||||
|
||||
interface Props {
|
||||
client: BaoClient
|
||||
mount: string
|
||||
secret: string
|
||||
}
|
||||
|
||||
export function AccessPanel({ client, mount, secret }: Props) {
|
||||
const data = useQuery({
|
||||
queryKey: ['access-inputs'],
|
||||
queryFn: async () => {
|
||||
// Each source can be forbidden for a limited admin, so a failure degrades to "none" with a note.
|
||||
const soft = async <T,>(label: string, p: Promise<T>, fallback: T) => {
|
||||
try {
|
||||
return { value: await p, note: null }
|
||||
} catch (e) {
|
||||
return { value: fallback, note: `${label}: ${e instanceof Error ? e.message : String(e)}` }
|
||||
}
|
||||
}
|
||||
const [users, entities, groups, tokens, names] = await Promise.all([
|
||||
soft('users', listUsers(client), []),
|
||||
soft('entities', listEntities(client), []),
|
||||
soft('groups', listGroups(client), []),
|
||||
soft('tokens', listTokens(client), []),
|
||||
listPolicyNames(client),
|
||||
])
|
||||
const policies = await Promise.all(names.map(async (name) => ({ name, hcl: await readPolicyHcl(client, name) })))
|
||||
return {
|
||||
identities: resolveIdentities({
|
||||
users: users.value,
|
||||
entities: entities.value,
|
||||
groups: groups.value,
|
||||
tokens: tokens.value,
|
||||
}),
|
||||
policies,
|
||||
notes: [users, entities, groups, tokens].flatMap((r) => (r.note ? [r.note] : [])),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
if (data.isPending) return <p>Loading access…</p>
|
||||
if (data.error) return <p role="alert">Could not load access: {data.error.message}</p>
|
||||
|
||||
const path = dataPath(mount, secret)
|
||||
const entries = accessForPath(data.data.identities, data.data.policies, path, pathMatches)
|
||||
|
||||
return (
|
||||
<section aria-label="Access">
|
||||
<h2>Access to {secret}</h2>
|
||||
<p>
|
||||
Evaluated against <code>{path}</code>
|
||||
</p>
|
||||
{data.data.notes.map((n) => (
|
||||
<p key={n} role="alert">
|
||||
Incomplete: could not read {n}
|
||||
</p>
|
||||
))}
|
||||
{entries.length === 0 ? (
|
||||
<p>No identity can reach this secret.</p>
|
||||
) : (
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Identity</th>
|
||||
<th>Type</th>
|
||||
<th>Via policy</th>
|
||||
<th>Capabilities</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{entries.map((e) => (
|
||||
<tr key={`${e.identity.kind}:${e.identity.name}:${e.policy}`}>
|
||||
<td>{e.identity.name}</td>
|
||||
<td>{e.identity.kind}</td>
|
||||
<td>{e.policy}</td>
|
||||
<td>{e.capabilities.join(', ')}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
<p className="hint">
|
||||
Only the most specific matching policy path applies, as in OpenBao. Parameter constraints and templated paths are approximated.
|
||||
</p>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter } from 'react-router'
|
||||
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
|
||||
import { BaoClient } from '../../api/bao'
|
||||
import * as ids from '../../api/identities'
|
||||
import * as api from '../../api/secrets'
|
||||
import { SessionProvider } from '../../api/session'
|
||||
import { useSession } from '../../api/useSession'
|
||||
import * as tokens from '../../api/tokens'
|
||||
import { SecretsPage } from './SecretsPage'
|
||||
|
||||
vi.mock('../../api/identities')
|
||||
vi.mock('../../api/secrets', async (orig) => ({ ...(await orig<typeof api>()), listKvMounts: vi.fn(), listSecrets: vi.fn(), readPolicyHcl: vi.fn() }))
|
||||
vi.mock('../../api/tokens')
|
||||
|
||||
const client = new BaoClient('http://b', 't')
|
||||
|
||||
function Connected() {
|
||||
const { connect } = useSession()
|
||||
return (
|
||||
<button type="button" onClick={() => connect(client, { accessor: 'me', display_name: 'me', policies: [], ttl: 0, renewable: false })}>
|
||||
go
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(api.listKvMounts).mockResolvedValue([{ path: 'secret/' }])
|
||||
vi.mocked(api.listSecrets).mockImplementation(async (_c, _m, prefix) =>
|
||||
prefix === ''
|
||||
? [{ name: 'app/', path: 'app/', isFolder: true }]
|
||||
: [{ name: 'db', path: 'app/db', isFolder: false }],
|
||||
)
|
||||
vi.mocked(ids.listUsers).mockResolvedValue([
|
||||
{ name: 'alice', policies: ['app-rw'], ttl: 0 },
|
||||
{ name: 'bob', policies: ['other'], ttl: 0 },
|
||||
])
|
||||
vi.mocked(ids.listEntities).mockResolvedValue([])
|
||||
vi.mocked(ids.listGroups).mockResolvedValue([])
|
||||
vi.mocked(tokens.listTokens).mockResolvedValue([])
|
||||
vi.mocked(ids.listPolicyNames).mockResolvedValue(['app-rw', 'other'])
|
||||
vi.mocked(api.readPolicyHcl).mockImplementation(async (_c, n) =>
|
||||
n === 'app-rw' ? 'path "secret/data/app/*" { capabilities = ["read", "update"] }' : 'path "secret/data/zzz" { capabilities = ["read"] }',
|
||||
)
|
||||
})
|
||||
afterEach(() => vi.resetAllMocks())
|
||||
|
||||
test('navigates folders and shows who can reach a secret', async () => {
|
||||
render(
|
||||
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
|
||||
<SessionProvider>
|
||||
<MemoryRouter>
|
||||
<Connected />
|
||||
<SecretsPage />
|
||||
</MemoryRouter>
|
||||
</SessionProvider>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
const user = userEvent.setup()
|
||||
await user.click(screen.getByRole('button', { name: 'go' }))
|
||||
await user.click(await screen.findByRole('button', { name: /app\// }))
|
||||
await user.click(await screen.findByRole('button', { name: /db/ }))
|
||||
|
||||
const panel = await screen.findByRole('region', { name: 'Access' })
|
||||
const row = (await within(panel).findByText('alice')).closest('tr')!
|
||||
expect(within(row).getByText('app-rw')).toBeInTheDocument()
|
||||
expect(within(row).getByText('read, update')).toBeInTheDocument()
|
||||
expect(within(panel).queryByText('bob')).not.toBeInTheDocument()
|
||||
expect(within(panel).getByText('secret/data/app/db')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('asks to connect first', () => {
|
||||
render(
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
<SessionProvider>
|
||||
<MemoryRouter>
|
||||
<SecretsPage />
|
||||
</MemoryRouter>
|
||||
</SessionProvider>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
expect(screen.getByText(/Not connected/)).toBeInTheDocument()
|
||||
})
|
||||
@@ -1,3 +1,85 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import type { BaoClient } from '../../api/bao'
|
||||
import { listKvMounts, listSecrets } from '../../api/secrets'
|
||||
import { RequireConnection } from '../shared/RequireConnection'
|
||||
import { AccessPanel } from './AccessPanel'
|
||||
|
||||
function Browser({ client }: { client: BaoClient }) {
|
||||
const mounts = useQuery({ queryKey: ['kv-mounts'], queryFn: () => listKvMounts(client) })
|
||||
const [mountChoice, setMount] = useState<string | null>(null)
|
||||
const [prefix, setPrefix] = useState('')
|
||||
const [selected, setSelected] = useState<string | null>(null)
|
||||
|
||||
const mount = mountChoice ?? mounts.data?.[0]?.path ?? null
|
||||
const entries = useQuery({
|
||||
queryKey: ['kv-list', mount, prefix],
|
||||
queryFn: () => listSecrets(client, mount!, prefix),
|
||||
enabled: mount !== null,
|
||||
})
|
||||
|
||||
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>
|
||||
if (!mount) return <p>No KV v2 secrets engines are mounted.</p>
|
||||
|
||||
return (
|
||||
<>
|
||||
<label>
|
||||
Mount
|
||||
<select
|
||||
value={mount}
|
||||
onChange={(e) => {
|
||||
setMount(e.target.value)
|
||||
go('')
|
||||
}}
|
||||
>
|
||||
{mounts.data.map((m) => (
|
||||
<option key={m.path}>{m.path}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<nav aria-label="Path" className="crumbs">
|
||||
<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.error && <p role="alert">{entries.error.message}</p>}
|
||||
{entries.data?.length === 0 && <p>Nothing here.</p>}
|
||||
<ul className="entries">
|
||||
{entries.data?.map((e) => (
|
||||
<li key={e.path}>
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={selected === e.path}
|
||||
onClick={() => (e.isFolder ? go(e.path) : setSelected(e.path))}
|
||||
>
|
||||
{e.isFolder ? '📁 ' : '🔑 '}
|
||||
{e.name}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{selected && <AccessPanel client={client} mount={mount} secret={selected} />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function SecretsPage() {
|
||||
return <h1>Secrets</h1>
|
||||
return (
|
||||
<section>
|
||||
<h1>Secrets</h1>
|
||||
<RequireConnection>{(client) => <Browser client={client} />}</RequireConnection>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user