Add KV v2 secret browser with per-secret Access panel
CI / integration (push) Successful in 11s
CI / check (push) Successful in 13s

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:
mattandClaude Sonnet 5.5 committed 2026-10-20 11:26:33 +01:00
1 parent f2ba697093
commit cd1438987b
11 files changed
+604 -30

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { accessForPath } from '../model/graph'
import { resolveIdentities } from '../model/identities'
import { pathMatches } from '../model/policy'
import { BaoClient } from './bao'
import * as secrets from './secrets'
import * as tokens from './tokens'
const client = new BaoClient(process.env.BAO_ADDR ?? 'http://127.0.0.1:8200', process.env.BAO_TOKEN ?? 'root')
const sfx = Math.random().toString(36).slice(2, 8)
const P = { rw: `it-sec-rw-${sfx}`, ro: `it-sec-ro-${sfx}`, deny: `it-sec-deny-${sfx}` }
const created: tokens.CreatedToken[] = []
const policy = (name: string, hcl: string) => client.request('PUT', `sys/policies/acl/${name}`, { policy: hcl })
beforeAll(async () => {
await policy(P.rw, `path "secret/data/it-${sfx}/*" { capabilities = ["create", "read", "update"] }`)
await policy(P.ro, `path "secret/data/it-${sfx}/+/pub" { capabilities = ["read", "list"] }`)
await policy(P.deny, `path "secret/data/it-${sfx}/private" { capabilities = ["deny"] }`)
await client.request('POST', `secret/data/it-${sfx}/app/db`, { data: { k: 'v' } })
await client.request('POST', `secret/data/it-${sfx}/team/pub`, { data: { k: 'v' } })
for (const [name, policies] of [
['a', [P.rw, P.ro]],
['b', [P.rw, P.deny]],
['c', [P.ro]],
] as const) {
created.push(await tokens.createToken(client, { displayName: `it-${sfx}-${name}`, policies: [...policies], renewable: false, orphan: false }))
}
})
afterAll(async () => {
for (const t of created) await tokens.revokeToken(client, t.accessor).catch(() => {})
for (const p of Object.values(P)) await client.request('DELETE', `sys/policies/acl/${p}`).catch(() => {})
await client.request('DELETE', `secret/metadata/it-${sfx}/app/db`).catch(() => {})
await client.request('DELETE', `secret/metadata/it-${sfx}/team/pub`).catch(() => {})
})
describe('secret browser API', () => {
test('finds the KV v2 mount and lists folders before secrets', async () => {
expect((await secrets.listKvMounts(client)).map((m) => m.path)).toContain('secret/')
const top = await secrets.listSecrets(client, 'secret/', `it-${sfx}/`)
expect(top.map((e) => [e.name, e.isFolder])).toEqual([
['app/', true],
['team/', true],
])
const app = await secrets.listSecrets(client, 'secret/', `it-${sfx}/app/`)
expect(app).toEqual([{ name: 'db', path: `it-${sfx}/app/db`, isFolder: false }])
})
})
describe('access model vs sys/capabilities-self', () => {
test.each([
`it-${sfx}/app/db`,
`it-${sfx}/private`,
`it-${sfx}/team/pub`,
`it-${sfx}/team/x/y`,
`it-${sfx}/team`,
`other-${sfx}/z`,
])('agrees for %s', async (secret) => {
const names = await client.list('sys/policies/acl')
const policies = await Promise.all(names.map(async (name) => ({ name, hcl: await secrets.readPolicyHcl(client, name) })))
const identities = resolveIdentities({ users: [], entities: [], groups: [], tokens: await tokens.listTokens(client) })
const path = secrets.dataPath('secret/', secret)
const model = accessForPath(identities, policies, path, pathMatches)
for (const t of created) {
const info = await tokens.lookupAccessor(client, t.accessor)
const real = await secrets.capabilitiesSelf(client.withToken(t.token), path)
const predicted = [...new Set(model.filter((m) => m.identity.name === info.display_name).flatMap((m) => m.capabilities))]
expect(predicted.length ? predicted.sort() : ['deny'], `${info.display_name} on ${path}`).toEqual([...real].sort())
}
})
})
+52
View File
@@ -0,0 +1,52 @@
import type { BaoClient } from './bao'
const enc = (path: string) => path.split('/').map(encodeURIComponent).join('/')
export interface KvMount {
/** Mount path with trailing slash, e.g. "secret/". */
path: string
}
export interface SecretEntry {
name: string
/** Path relative to the mount, e.g. "app/db" (folders keep a trailing slash). */
path: string
isFolder: boolean
}
export async function listKvMounts(c: BaoClient): Promise<KvMount[]> {
const res = await c.request<{ data: Record<string, { type: string; options?: { version?: string } | null }> }>(
'GET',
'sys/mounts',
)
return Object.entries(res.data)
.filter(([, m]) => m.type === 'kv' && m.options?.version === '2')
.map(([path]) => ({ path }))
.sort((a, b) => a.path.localeCompare(b.path))
}
/** Lists metadata only; secret values are never read. `prefix` is "" or ends with "/". */
export async function listSecrets(c: BaoClient, mount: string, prefix: string): Promise<SecretEntry[]> {
const keys = await c.list(`${mount}metadata/${enc(prefix)}`)
return keys
.map((k) => ({ name: k, path: prefix + k, isFolder: k.endsWith('/') }))
.sort((a, b) => Number(b.isFolder) - Number(a.isFolder) || a.name.localeCompare(b.name))
}
/** API path of a secret's data, which is what ACL policies are written against. */
export const dataPath = (mount: string, secret: string) => `${mount}data/${secret}`
export async function readPolicyHcl(c: BaoClient, name: string): Promise<string | null> {
try {
const res = await c.request<{ data: { policy: string } }>('GET', `sys/policies/acl/${encodeURIComponent(name)}`)
return res.data.policy
} catch {
return null
}
}
/** Capabilities of the current token on a path (used to cross-check the access model). */
export async function capabilitiesSelf(c: BaoClient, path: string): Promise<string[]> {
const res = await c.request<{ capabilities: string[] }>('POST', 'sys/capabilities-self', { paths: [path] })
return res.capabilities
}