diff --git a/src/api/engines/registry.ts b/src/api/engines/registry.ts new file mode 100644 index 0000000..5a663c5 --- /dev/null +++ b/src/api/engines/registry.ts @@ -0,0 +1,34 @@ +import type { Mount } from '../secrets' +import { isKvV2 } from '../secrets' + +/** Describes a dynamic secrets engine the UI can manage. Grows in later phases (role fields, creds, config). */ +export interface EngineDef { + /** Engine type as reported by sys/mounts. */ + type: string + label: string + /** Path, relative to the mount, that roles are listed/written under. */ + rolesPath: string +} + +export const ENGINES: Record = { + database: { type: 'database', label: 'Database', rolesPath: 'roles' }, + aws: { type: 'aws', label: 'AWS', rolesPath: 'roles' }, + ssh: { type: 'ssh', label: 'SSH', rolesPath: 'roles' }, +} + +/** Engine types that are managed as plain secret stores or by the system, not as dynamic engines. */ +const NOT_DYNAMIC = new Set(['kv', 'generic', 'cubbyhole', 'system', 'identity']) + +/** Known engine definition, or a generic fallback (raw JSON roles) for any other dynamic engine type. */ +export function engineFor(type: string): EngineDef { + return ENGINES[type] ?? { type, label: type, rolesPath: 'roles' } +} + +export type MountKind = 'kv' | 'dynamic' + +/** KV v2 mounts use the path-tree browser; every other non-system engine is treated as dynamic. */ +export function mountKind(m: Mount): MountKind | null { + if (isKvV2(m)) return 'kv' + if (NOT_DYNAMIC.has(m.type)) return null + return 'dynamic' +} diff --git a/src/api/secrets.ts b/src/api/secrets.ts index 62b3c0c..e592385 100644 --- a/src/api/secrets.ts +++ b/src/api/secrets.ts @@ -14,17 +14,32 @@ export interface SecretEntry { isFolder: boolean } -export async function listKvMounts(c: BaoClient): Promise { +export interface Mount { + /** Mount path with trailing slash, e.g. "database/". */ + path: string + /** Engine type as reported by sys/mounts, e.g. "kv", "database", "aws", "ssh". */ + type: string + /** KV version ("1" | "2") when the engine is KV. */ + version?: string +} + +/** Every secrets engine mount visible to the token, sorted by path. */ +export async function listMounts(c: BaoClient): Promise { const res = await c.request<{ data: Record }>( 'GET', 'sys/mounts', ) return Object.entries(res.data) - .filter(([, m]) => m.type === 'kv' && m.options?.version === '2') - .map(([path]) => ({ path })) + .map(([path, m]) => ({ path, type: m.type, version: m.options?.version })) .sort((a, b) => a.path.localeCompare(b.path)) } +export const isKvV2 = (m: Mount) => m.type === 'kv' && m.version === '2' + +export async function listKvMounts(c: BaoClient): Promise { + return (await listMounts(c)).filter(isKvV2).map(({ path }) => ({ 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 { const keys = await c.list(`${mount}metadata/${enc(prefix)}`) diff --git a/src/features/secrets/EngineView.tsx b/src/features/secrets/EngineView.tsx new file mode 100644 index 0000000..18c3e55 --- /dev/null +++ b/src/features/secrets/EngineView.tsx @@ -0,0 +1,12 @@ +import { engineFor } from '../../api/engines/registry' +import type { Mount } from '../../api/secrets' + +/** Roles / Config / Leases for a dynamic secrets engine. Filled in by later phases. */ +export function EngineView({ mount }: { mount: Mount }) { + const engine = engineFor(mount.type) + return ( +

+ {engine.label} engine at {mount.path}: role management is coming soon. +

+ ) +} diff --git a/src/features/secrets/SecretsPage.test.tsx b/src/features/secrets/SecretsPage.test.tsx index d7d1bdf..6780b45 100644 --- a/src/features/secrets/SecretsPage.test.tsx +++ b/src/features/secrets/SecretsPage.test.tsx @@ -14,7 +14,7 @@ import { SecretsPage } from './SecretsPage' vi.mock('../../api/grants') vi.mock('../../api/identities') -vi.mock('../../api/secrets', async (orig) => ({ ...(await orig()), listKvMounts: vi.fn(), listSecrets: vi.fn(), readPolicyHcl: vi.fn() })) +vi.mock('../../api/secrets', async (orig) => ({ ...(await orig()), listMounts: vi.fn(), listSecrets: vi.fn(), readPolicyHcl: vi.fn() })) vi.mock('../../api/tokens') const client = new BaoClient('http://b', 't') @@ -29,7 +29,7 @@ function Connected() { } beforeEach(() => { - vi.mocked(api.listKvMounts).mockResolvedValue([{ path: 'secret/' }]) + vi.mocked(api.listMounts).mockResolvedValue([{ path: 'secret/', type: 'kv', version: '2' }]) vi.mocked(api.listSecrets).mockImplementation(async (_c, _m, prefix) => prefix === '' ? [{ name: 'app/', path: 'app/', isFolder: true }] @@ -127,6 +127,34 @@ test('shows why a grant failed and keeps the form open', async () => { expect(screen.getByRole('form', { name: 'Grant access to this secret' })).toBeInTheDocument() }) +test('groups mounts by engine and shows the engine view for a dynamic mount', async () => { + vi.mocked(api.listMounts).mockResolvedValue([ + { path: 'database/', type: 'database' }, + { path: 'secret/', type: 'kv', version: '2' }, + { path: 'cubbyhole/', type: 'cubbyhole' }, + ]) + render( + + + + + + + + , + ) + const user = userEvent.setup() + await user.click(screen.getByRole('button', { name: 'go' })) + const select = await screen.findByLabelText('Mount') + expect(within(select).getByRole('group', { name: 'KV' })).toBeInTheDocument() + expect(within(select).getByRole('group', { name: 'Database' })).toBeInTheDocument() + expect(within(select).queryByText('cubbyhole/')).not.toBeInTheDocument() + + await user.selectOptions(select, 'database/') + expect(await screen.findByText(/Database engine at database\//)).toBeInTheDocument() + expect(api.listSecrets).not.toHaveBeenCalled() +}) + test('asks to connect first', () => { render( diff --git a/src/features/secrets/SecretsPage.tsx b/src/features/secrets/SecretsPage.tsx index 2b2e698..a2c8be3 100644 --- a/src/features/secrets/SecretsPage.tsx +++ b/src/features/secrets/SecretsPage.tsx @@ -1,21 +1,28 @@ import { useQuery } from '@tanstack/react-query' import { useState } from 'react' import type { BaoClient } from '../../api/bao' -import { listKvMounts, listSecrets } from '../../api/secrets' +import { engineFor, mountKind } from '../../api/engines/registry' +import { listMounts, listSecrets } from '../../api/secrets' import { RequireConnection } from '../shared/RequireConnection' import { AccessPanel } from './AccessPanel' +import { EngineView } from './EngineView' function Browser({ client }: { client: BaoClient }) { - const mounts = useQuery({ queryKey: ['kv-mounts'], queryFn: () => listKvMounts(client) }) + const mounts = useQuery({ + queryKey: ['mounts'], + queryFn: async () => (await listMounts(client)).filter((m) => mountKind(m) !== null), + }) const [mountChoice, setMount] = useState(null) const [prefix, setPrefix] = useState('') const [selected, setSelected] = useState(null) - const mount = mountChoice ?? mounts.data?.[0]?.path ?? 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: mount !== null, + enabled: isKv, }) const crumbs = prefix.split('/').filter(Boolean) @@ -26,28 +33,57 @@ function Browser({ client }: { client: BaoClient }) { if (mounts.error) return

{mounts.error.message}

if (mounts.isPending) return

Loading…

- if (!mount) return

No KV v2 secrets engines are mounted.

+ if (!current || !mount) return

No secrets engines are mounted.

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 && ( + + ) + + if (!isKv) { + return ( +
+
+ +
+ +
+ ) + } return (