From 13a53538685d552e519ac49fabad6108931c0570 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 9 Nov 2026 14:23:43 +0000 Subject: [PATCH] Secrets: create and change secrets engine mounts from the UI New secrets engine (KV v2, database, aws, ssh) and per-mount Settings dialogs on the Secrets page, including KV v2 settings. Settings prefill from the explicit lease TTLs on the mount rather than the effective ones, and the KV configuration is re-read on every open so a stale copy is never written back. A server with no mounts can still create its first one. Co-Authored-By: Claude Sonnet 5.5 --- src/features/secrets/MountDialog.test.tsx | 155 ++++++++++++++++++++ src/features/secrets/MountDialog.tsx | 169 ++++++++++++++++++++++ src/features/secrets/RoleEditor.tsx | 5 +- src/features/secrets/SecretsPage.test.tsx | 57 ++++++++ src/features/secrets/SecretsPage.tsx | 142 ++++++++++++------ src/index.css | 15 ++ 6 files changed, 497 insertions(+), 46 deletions(-) create mode 100644 src/features/secrets/MountDialog.test.tsx create mode 100644 src/features/secrets/MountDialog.tsx diff --git a/src/features/secrets/MountDialog.test.tsx b/src/features/secrets/MountDialog.test.tsx new file mode 100644 index 0000000..abccb44 --- /dev/null +++ b/src/features/secrets/MountDialog.test.tsx @@ -0,0 +1,155 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { afterEach, expect, test, vi } from 'vitest' +import { BaoClient } from '../../api/bao' +import { MOUNT_TYPES } from '../../api/engines/registry' +import * as mounts from '../../api/mounts' +import type { Mount } from '../../api/secrets' +import { MountDialog } from './MountDialog' + +vi.mock('../../api/mounts', async (orig) => ({ + ...(await orig()), + createMount: vi.fn(), + tuneMount: vi.fn(), + readKvConfig: vi.fn(), +})) + +const client = new BaoClient('http://b', 't') +const kv = MOUNT_TYPES[0] +const database = MOUNT_TYPES[1] +afterEach(() => vi.resetAllMocks()) + +function setup(props: { mount?: Mount; existing?: string[] } = {}) { + const onClose = vi.fn() + const onDone = vi.fn() + render( + + + , + ) + return { user: userEvent.setup(), onClose, onDone } +} + +test('creates a KV v2 store with its settings', async () => { + vi.mocked(mounts.createMount).mockResolvedValue() + const { user, onDone } = setup() + await user.type(screen.getByLabelText('Path'), 'scratch') + await user.type(screen.getByLabelText('Description'), 'throwaway') + await user.type(screen.getByLabelText(/^Max versions/), '5') + await user.click(screen.getByLabelText(/Require check-and-set/)) + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(mounts.createMount).toHaveBeenCalledWith(client, 'scratch', kv, { + description: 'throwaway', + max_versions: '5', + cas_required: true, + }) + expect(onDone).toHaveBeenCalledWith('scratch/') +}) + +test('each type offers its own settings, and keeps the path you typed', async () => { + const { user } = setup() + expect(screen.queryByLabelText(/Default lease TTL/)).not.toBeInTheDocument() + await user.type(screen.getByLabelText('Path'), 'dbs') + await user.selectOptions(screen.getByLabelText('Type'), 'database') + expect(screen.getByLabelText('Path')).toHaveValue('dbs') + expect(screen.getByLabelText(/Default lease TTL/)).toBeInTheDocument() + expect(screen.queryByLabelText(/Max versions/)).not.toBeInTheDocument() +}) + +test('creates a dynamic engine with lease TTLs', async () => { + vi.mocked(mounts.createMount).mockResolvedValue() + const { user, onDone } = setup() + await user.selectOptions(screen.getByLabelText('Type'), 'database') + await user.type(screen.getByLabelText('Path'), 'team/db') + await user.type(screen.getByLabelText(/Default lease TTL/), '1h') + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(mounts.createMount).toHaveBeenCalledWith(client, 'team/db', database, { default_lease_ttl: '1h' }) + expect(onDone).toHaveBeenCalledWith('team/db/') +}) + +test.each([ + ['secret', /already a secrets engine/], + ['sys', /reserved/], + ['Has Caps', /lowercase/], + ['secret/inner', /inside the existing mount/], +])('refuses the path "%s" without calling OpenBao', async (path, message) => { + const { user } = setup() + await user.type(screen.getByLabelText('Path'), path) + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(await screen.findByRole('alert')).toHaveTextContent(message) + expect(mounts.createMount).not.toHaveBeenCalled() +}) + +test('shows the server error and stays open', async () => { + vi.mocked(mounts.createMount).mockRejectedValue(new Error('plugin not found in the catalog: aws')) + const { user, onDone } = setup() + await user.selectOptions(screen.getByLabelText('Type'), 'aws') + expect(screen.getByText(/Not every OpenBao build includes the AWS engine/)).toBeInTheDocument() + await user.type(screen.getByLabelText('Path'), 'aws') + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(await screen.findByRole('alert')).toHaveTextContent('plugin not found in the catalog') + expect(onDone).not.toHaveBeenCalled() +}) + +test('a half-finished create says the mount exists and stays open', async () => { + vi.mocked(mounts.createMount).mockRejectedValue(new mounts.PartialMountError('scratch/ was mounted, but saving its KV settings failed: nope')) + const { user, onDone } = setup() + await user.type(screen.getByLabelText('Path'), 'scratch') + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(await screen.findByRole('alert')).toHaveTextContent('scratch/ was mounted, but saving its KV settings failed') + expect(onDone).not.toHaveBeenCalled() +}) + +test('settings of a dynamic mount are prefilled from what is set on it, and saved with a blank TTL reset', async () => { + vi.mocked(mounts.tuneMount).mockResolvedValue() + const mount: Mount = { path: 'database/', type: 'database', description: 'main db', defaultLeaseTtl: 3600, maxLeaseTtl: 0 } + const { user, onDone } = setup({ mount }) + expect(screen.getByLabelText('Description')).toHaveValue('main db') + expect(screen.getByLabelText(/Default lease TTL/)).toHaveValue('3600s') + expect(screen.getByLabelText(/Max lease TTL/)).toHaveValue('') // 0 means "system default", not a value to save back + expect(screen.queryByLabelText('Path')).not.toBeInTheDocument() + expect(screen.queryByLabelText('Type')).not.toBeInTheDocument() + + await user.clear(screen.getByLabelText('Description')) + await user.type(screen.getByLabelText('Description'), 'renamed') + await user.click(screen.getByRole('button', { name: 'Save' })) + expect(mounts.tuneMount).toHaveBeenCalledWith(client, 'database/', database, { description: 'renamed', default_lease_ttl: '3600s' }) + expect(onDone).toHaveBeenCalledWith('database/') +}) + +test('settings of a KV mount load the KV configuration first', async () => { + vi.mocked(mounts.readKvConfig).mockResolvedValue({ max_versions: 3, cas_required: true, delete_version_after: '0s' }) + vi.mocked(mounts.tuneMount).mockResolvedValue() + const { user } = setup({ mount: { path: 'secret/', type: 'kv', version: '2', description: 'kv' } }) + expect(await screen.findByLabelText(/^Max versions/)).toHaveValue('3') + expect(screen.getByLabelText(/Require check-and-set/)).toBeChecked() + await user.clear(screen.getByLabelText(/^Max versions/)) + await user.type(screen.getByLabelText(/^Max versions/), '10') + await user.click(screen.getByRole('button', { name: 'Save' })) + expect(mounts.tuneMount).toHaveBeenCalledWith(client, 'secret/', kv, { + description: 'kv', + max_versions: '10', + cas_required: true, + delete_version_after: '0s', + }) +}) + +test('says so when the KV configuration cannot be read, instead of showing a blank form', async () => { + vi.mocked(mounts.readKvConfig).mockRejectedValue(new Error('permission denied')) + setup({ mount: { path: 'secret/', type: 'kv', version: '2' } }) + expect(await screen.findByRole('alert')).toHaveTextContent('permission denied') + expect(screen.queryByRole('button', { name: 'Save' })).not.toBeInTheDocument() +}) + +test('engines the UI cannot manage have no settings form', () => { + setup({ mount: { path: 'pki/', type: 'pki' } }) + expect(screen.getByText(/can’t change pki engines/)).toBeInTheDocument() +}) + +test('cancel closes without saving', async () => { + const { user, onClose } = setup() + await user.click(screen.getByRole('button', { name: 'Cancel' })) + expect(onClose).toHaveBeenCalled() + expect(mounts.createMount).not.toHaveBeenCalled() +}) diff --git a/src/features/secrets/MountDialog.tsx b/src/features/secrets/MountDialog.tsx new file mode 100644 index 0000000..1c16641 --- /dev/null +++ b/src/features/secrets/MountDialog.tsx @@ -0,0 +1,169 @@ +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { useState } from 'react' +import type { BaoClient } from '../../api/bao' +import { MOUNT_TYPES, mountTypeFor, type MountType } from '../../api/engines/registry' +import { draftFromRole, emptyDraft, payloadFromDraft, type RoleDraft, type Schema } from '../../api/engines/roles' +import { createMount, PartialMountError, readKvConfig, tuneMount } from '../../api/mounts' +import type { Mount } from '../../api/secrets' +import { validateMountPath } from '../../model/mounts' +import { RoleEditor } from './RoleEditor' + +const schemaOf = (t: MountType): Schema => ({ fields: [...t.fields, ...t.configFields] }) + +function CreateMount({ client, existing, onClose, onDone }: { client: BaoClient; existing: string[]; onClose: () => void; onDone: (path: string) => void }) { + const qc = useQueryClient() + const [type, setType] = useState(MOUNT_TYPES[0]) + const [draft, setDraft] = useState(() => emptyDraft(schemaOf(MOUNT_TYPES[0]))) + const [formError, setFormError] = useState(null) + + const create = useMutation({ + mutationFn: async () => { + const path = draft.name.trim().replace(/\/+$/, '') + try { + await createMount(client, path, type, payloadFromDraft(schemaOf(type), draft)) + } finally { + // Even a half-finished create leaves a mount behind, so always refresh the list. + await qc.invalidateQueries({ queryKey: ['mounts'] }) + } + return `${path}/` + }, + onSuccess: (path) => onDone(path), + }) + + function submit() { + setFormError(null) + const pathError = validateMountPath(draft.name.trim(), existing) + if (pathError) return setFormError(pathError) + try { + payloadFromDraft(schemaOf(type), draft) + } catch (e) { + return setFormError((e as Error).message) + } + create.mutate() + } + + return ( +
+ + + {type.type === 'aws' && ( +

Not every OpenBao build includes the AWS engine. If it is missing, OpenBao says the plugin was not found in the catalog.

+ )} +
+ ) +} + +function EditForm({ client, mount, type, initial, onClose, onDone }: { + client: BaoClient + mount: Mount + type: MountType + initial: Record + onClose: () => void + onDone: (path: string) => void +}) { + const qc = useQueryClient() + const schema = schemaOf(type) + const [draft, setDraft] = useState(() => draftFromRole(schema, mount.path, initial)) + const [formError, setFormError] = useState(null) + const save = useMutation({ + mutationFn: async () => { + await tuneMount(client, mount.path, type, payloadFromDraft(schema, draft)) + await qc.invalidateQueries({ queryKey: ['mounts'] }) + }, + onSuccess: () => onDone(mount.path), + }) + return ( + { + setFormError(null) + try { + payloadFromDraft(schema, draft) + } catch (e) { + return setFormError((e as Error).message) + } + save.mutate() + }} + onCancel={onClose} + /> + ) +} + +function EditMount({ client, mount, onClose, onDone }: { client: BaoClient; mount: Mount; onClose: () => void; onDone: (path: string) => void }) { + const type = mountTypeFor(mount) + // KV v2 settings live on the engine, not in sys/mounts, so they are read separately. + const kvConfig = useQuery({ + queryKey: ['kv-config', mount.path], + queryFn: () => readKvConfig(client, mount.path), + enabled: type !== null && type.configFields.length > 0, + // Never reuse an earlier read: a stale value would be written back as if it were current. + gcTime: 0, + staleTime: 0, + }) + if (!type) return

The UI can’t change {mount.type} engines.

+ if (type.configFields.length > 0 && kvConfig.isPending) return

Loading…

+ if (kvConfig.error) return

{kvConfig.error.message}

+ return ( + + ) +} + +/** Create a secrets engine mount, or change the settings of an existing one. Path and type can't be changed. */ +export function MountDialog(props: { client: BaoClient; existing: string[]; mount?: Mount; onClose: () => void; onDone: (path: string) => void }) { + return props.mount ? ( +
+ +
+ ) : ( + + ) +} diff --git a/src/features/secrets/RoleEditor.tsx b/src/features/secrets/RoleEditor.tsx index 1ad3f10..6ef3358 100644 --- a/src/features/secrets/RoleEditor.tsx +++ b/src/features/secrets/RoleEditor.tsx @@ -97,6 +97,7 @@ export function RoleEditor({ kind, noun = 'role', showName = true, + nameLabel, submitLabel = 'Save', draft, pending, @@ -110,6 +111,8 @@ export function RoleEditor({ noun?: string /** False for single-endpoint configs that have no name. */ showName?: boolean + /** Label for the name field; defaults to " name". */ + nameLabel?: string submitLabel?: string draft: RoleDraft pending: boolean @@ -128,7 +131,7 @@ export function RoleEditor({

{title}

{showName && ( )} diff --git a/src/features/secrets/SecretsPage.test.tsx b/src/features/secrets/SecretsPage.test.tsx index c0afd28..bf31ec3 100644 --- a/src/features/secrets/SecretsPage.test.tsx +++ b/src/features/secrets/SecretsPage.test.tsx @@ -6,6 +6,7 @@ import { afterEach, beforeEach, expect, test, vi } from 'vitest' import { BaoClient } from '../../api/bao' import * as grants from '../../api/grants' import * as ids from '../../api/identities' +import * as mountsApi from '../../api/mounts' import * as api from '../../api/secrets' import { SessionProvider } from '../../api/session' import { useSession } from '../../api/useSession' @@ -16,6 +17,7 @@ vi.mock('../../api/grants') vi.mock('../../api/identities') vi.mock('../../api/secrets', async (orig) => ({ ...(await orig()), listMounts: vi.fn(), listSecrets: vi.fn(), readPolicyHcl: vi.fn() })) vi.mock('../../api/tokens') +vi.mock('../../api/mounts', async (orig) => ({ ...(await orig()), createMount: vi.fn(), tuneMount: vi.fn(), readKvConfig: vi.fn() })) const client = new BaoClient('http://b', 't') @@ -157,6 +159,61 @@ test('groups mounts by engine and shows the engine view for a dynamic mount', as expect(api.listSecrets).not.toHaveBeenCalled() }) +async function openPage() { + render( + + + + + + + + , + ) + const user = userEvent.setup() + await user.click(screen.getByRole('button', { name: 'go' })) + return user +} + +test('a server with no engines can still create its first one', async () => { + vi.mocked(api.listMounts).mockResolvedValue([]) + vi.mocked(mountsApi.createMount).mockResolvedValue() + const user = await openPage() + expect(await screen.findByText('No secrets engines are mounted.')).toBeInTheDocument() + await user.click(screen.getByRole('button', { name: 'New secrets engine' })) + await user.type(screen.getByRole('textbox', { name: 'Path' }), 'secret') + vi.mocked(api.listMounts).mockResolvedValue([{ path: 'secret/', type: 'kv', version: '2' }]) + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(mountsApi.createMount).toHaveBeenCalled() + expect(await screen.findByRole('navigation', { name: 'Path' })).toBeInTheDocument() + expect(screen.queryByRole('region', { name: 'New secrets engine' })).not.toBeInTheDocument() +}) + +test('creating an engine selects it', async () => { + vi.mocked(mountsApi.createMount).mockResolvedValue() + const user = await openPage() + await user.click(await screen.findByRole('button', { name: 'New secrets engine' })) + await user.selectOptions(screen.getByLabelText('Type'), 'ssh') + await user.type(screen.getByRole('textbox', { name: 'Path' }), 'hosts') + vi.mocked(api.listMounts).mockResolvedValue([ + { path: 'secret/', type: 'kv', version: '2' }, + { path: 'hosts/', type: 'ssh' }, + ]) + vi.spyOn(BaoClient.prototype, 'list').mockResolvedValue([]) + await user.click(screen.getByRole('button', { name: 'Create' })) + expect(await screen.findByRole('heading', { name: /SSH · hosts\// })).toBeInTheDocument() + expect(screen.getByLabelText('Mount')).toHaveValue('hosts/') +}) + +test('Settings opens the selected mount\'s settings, and toggles closed', async () => { + vi.mocked(mountsApi.readKvConfig).mockResolvedValue({ max_versions: 0, cas_required: false, delete_version_after: '0s' }) + const user = await openPage() + await user.click(await screen.findByRole('button', { name: 'Settings' })) + expect(await screen.findByRole('region', { name: 'Settings for secret/' })).toBeInTheDocument() + await user.click(screen.getByRole('button', { name: 'Settings' })) + expect(screen.queryByRole('region', { name: 'Settings for secret/' })).not.toBeInTheDocument() +}) + test('asks to connect first', () => { render( diff --git a/src/features/secrets/SecretsPage.tsx b/src/features/secrets/SecretsPage.tsx index 3b837b5..76799d3 100644 --- a/src/features/secrets/SecretsPage.tsx +++ b/src/features/secrets/SecretsPage.tsx @@ -1,18 +1,20 @@ import { useQuery } from '@tanstack/react-query' import { useState } from 'react' import type { BaoClient } from '../../api/bao' -import { engineFor, mountKind } from '../../api/engines/registry' +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 { EngineView } from './EngineView' +import { MountDialog } from './MountDialog' function Browser({ client }: { client: BaoClient }) { const mounts = useMounts(client) const [mountChoice, setMount] = useState(null) const [prefix, setPrefix] = useState('') const [selected, setSelected] = useState(null) + const [dialog, setDialog] = useState<'create' | 'edit' | null>(null) const current = mounts.data?.find((m) => m.path === mountChoice) ?? mounts.data?.[0] ?? null const mount = current?.path ?? null @@ -31,7 +33,49 @@ function Browser({ client }: { client: BaoClient }) { if (mounts.error) return

{mounts.error.message}

if (mounts.isPending) return

Loading…

- if (!current || !mount) return

No secrets engines are mounted.

+ + const existing = mounts.data.map((m) => m.path) + const closeDialog = () => setDialog(null) + const manage = ( +
+ + {current && mountTypeFor(current) && ( + + )} +
+ ) + const dialogs = dialog && ( + { + closeDialog() + // A new mount starts at its root; saving settings keeps your place. + if (path !== mount) { + setMount(path) + go('') + } + }} + /> + ) + + if (!current || !mount) { + return ( + <> +

No secrets engines are mounted.

+ {manage} + {dialogs} + + ) + } const where = prefix ? `${mount}${prefix}` : mount const kv = mounts.data.filter((m) => mountKind(m) === 'kv') @@ -66,58 +110,66 @@ function Browser({ client }: { client: BaoClient }) { if (!isKv) { return ( -
-
- + <> + {manage} + {dialogs} +
+
+ +
+
- -
+ ) } return ( -
-
- - {entries.isPending &&

Loading…

} - {entries.error &&

{entries.error.message}

} - {entries.data?.length === 0 &&

No secrets in {where} yet.

} - {!!entries.data?.length && ( -
    - {entries.data.map((e) => ( -
  • - -
  • + {crumbs.map((c, i) => ( + ))} -
+ + {entries.isPending &&

Loading…

} + {entries.error &&

{entries.error.message}

} + {entries.data?.length === 0 &&

No secrets in {where} yet.

} + {!!entries.data?.length && ( +
    + {entries.data.map((e) => ( +
  • + +
  • + ))} +
+ )} +
+ {selected ? ( + + ) : ( +

Select a secret to see who can reach it.

)}
- {selected ? ( - - ) : ( -

Select a secret to see who can reach it.

- )} -
+ ) } diff --git a/src/index.css b/src/index.css index 4530623..ad10f5f 100644 --- a/src/index.css +++ b/src/index.css @@ -1500,3 +1500,18 @@ button.danger-solid:hover:not(:disabled) { font-family: var(--mono); white-space: pre-wrap; } + +/* Create / settings / delete panel for a secrets engine mount. */ +.mount-actions { + display: flex; + gap: var(--s2); + margin-bottom: var(--s4); +} + +.mount-dialog { + margin: 0 0 var(--s4); + padding: var(--s4); + background: var(--surface-sunken); + border: 1px solid var(--line); + border-radius: var(--radius); +}