From fd20caa525f273f953f466c549daea072c8bfd84 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 9 Nov 2026 14:09:42 +0000 Subject: [PATCH] Config: show each database connection's URL and allowed roles The connections table reads each connection and lists its URL and allowed roles. Literal passwords in a URL are masked; templated {{password}} URLs are shown as-is. Co-Authored-By: Claude Sonnet 5.5 --- src/features/secrets/ConfigPanel.test.tsx | 40 ++++++++++++++++++- src/features/secrets/ConfigPanel.tsx | 48 +++++++++++++++++++---- src/model/connectionUrl.test.ts | 24 ++++++++++++ src/model/connectionUrl.ts | 13 ++++++ 4 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 src/model/connectionUrl.test.ts create mode 100644 src/model/connectionUrl.ts diff --git a/src/features/secrets/ConfigPanel.test.tsx b/src/features/secrets/ConfigPanel.test.tsx index a98086a..0d1f9cb 100644 --- a/src/features/secrets/ConfigPanel.test.tsx +++ b/src/features/secrets/ConfigPanel.test.tsx @@ -1,5 +1,5 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { render, screen } from '@testing-library/react' +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' @@ -95,3 +95,41 @@ test('shows the load error', async () => { renderPanel('database') expect(await screen.findByRole('alert')).toHaveTextContent('permission denied') }) + +test('the connections table shows each connection\'s URL and allowed roles, hiding literal passwords', async () => { + vi.mocked(config.listConfigs).mockResolvedValue(['main', 'legacy', 'bare']) + vi.mocked(config.readConfig).mockImplementation(async (_c, _m, _k, name) => + name === 'main' + ? { connection_url: 'postgresql://{{username}}:{{password}}@db:5432/app', allowed_roles: ['readonly', 'readwrite'] } + : name === 'legacy' + ? { connection_url: 'postgresql://bao:hunter2@old:5432/app', allowed_roles: ['*'] } + : { allowed_roles: [] }, + ) + renderPanel('database') + const table = await screen.findByRole('table', { name: 'Connections' }) + expect(within(table).getAllByRole('columnheader').map((h) => h.textContent)).toEqual(['Name', 'Connection URL', 'Allowed roles', '']) + + const main = (await within(table).findByText('main')).closest('tr')! + expect(await within(main).findByText('postgresql://{{username}}:{{password}}@db:5432/app')).toBeInTheDocument() + expect(within(main).getByText('readonly')).toBeInTheDocument() + expect(within(main).getByText('readwrite')).toBeInTheDocument() + + const legacy = within(table).getByText('legacy').closest('tr')! + expect(await within(legacy).findByText('postgresql://bao:••••@old:5432/app')).toBeInTheDocument() + expect(table).not.toHaveTextContent('hunter2') + expect(within(legacy).getByText('*')).toBeInTheDocument() + + const bare = within(table).getByText('bare').closest('tr')! + expect((await within(bare).findAllByText('None')).length).toBe(2) +}) + +test('a connection that cannot be read says why, without hiding the others', async () => { + vi.mocked(config.listConfigs).mockResolvedValue(['ok', 'broken']) + vi.mocked(config.readConfig).mockImplementation(async (_c, _m, _k, name) => { + if (name === 'broken') throw new Error('permission denied') + return { connection_url: 'postgresql://db/app', allowed_roles: ['ro'] } + }) + renderPanel('database') + expect(await screen.findByText('postgresql://db/app')).toBeInTheDocument() + expect(await screen.findByRole('alert')).toHaveTextContent('permission denied') +}) diff --git a/src/features/secrets/ConfigPanel.tsx b/src/features/secrets/ConfigPanel.tsx index ddb44e9..dc7ddc6 100644 --- a/src/features/secrets/ConfigPanel.tsx +++ b/src/features/secrets/ConfigPanel.tsx @@ -4,7 +4,9 @@ import type { BaoClient } from '../../api/bao' import { deleteConfig, listConfigs, readConfig, runAction, writeConfig } from '../../api/engines/config' import type { ConfigKind, EngineDef } from '../../api/engines/registry' import { draftFromRole, emptyDraft, payloadFromDraft, type RoleData, type RoleDraft } from '../../api/engines/roles' +import { maskConnectionUrl } from '../../model/connectionUrl' import { ListState } from '../shared/ListState' +import { NameChips } from '../shared/PolicyChips' import { useConfirm } from '../shared/useConfirm' import { RoleEditor } from './RoleEditor' @@ -122,6 +124,41 @@ function SingleConfig({ client, mount, kind }: { client: BaoClient; mount: strin ) } +/** One connection in the list: its URL and the roles allowed to use it, read from the connection itself. */ +function ConnectionRow({ client, mount, kind, name, onEdit }: { + client: BaoClient + mount: string + kind: ConfigKind + name: string + onEdit: () => void +}) { + const cfg = useQuery({ queryKey: ['engine-config', mount, kind.id, name], queryFn: () => readConfig(client, mount, kind, name) }) + const url = cfg.data?.connection_url + const roles = Array.isArray(cfg.data?.allowed_roles) ? (cfg.data.allowed_roles as string[]) : [] + return ( + + {name} + + {cfg.isPending ? ( + Loading… + ) : cfg.error ? ( + {cfg.error.message} + ) : typeof url === 'string' && url ? ( + {maskConnectionUrl(url)} + ) : ( + None + )} + + {cfg.data ? : null} + + + + + ) +} + function NamedConfigs({ client, mount, kind }: { client: BaoClient; mount: string; kind: ConfigKind }) { const [editing, setEditing] = useState<{ name?: string; initial: RoleData | null } | null>(null) const names = useQuery({ queryKey: ['engine-config', mount, kind.id], queryFn: () => listConfigs(client, mount, kind) }) @@ -137,19 +174,14 @@ function NamedConfigs({ client, mount, kind }: { client: BaoClient; mount: strin Name + Connection URL + Allowed roles {names.data.map((n) => ( - - {n} - - - - + open.mutate(n)} /> ))} diff --git a/src/model/connectionUrl.test.ts b/src/model/connectionUrl.test.ts new file mode 100644 index 0000000..c617f8c --- /dev/null +++ b/src/model/connectionUrl.test.ts @@ -0,0 +1,24 @@ +import { expect, test } from 'vitest' +import { maskConnectionUrl } from './connectionUrl' + +test('keeps templated credentials', () => { + const u = 'postgresql://{{username}}:{{password}}@db:5432/app?sslmode=disable' + expect(maskConnectionUrl(u)).toBe(u) +}) + +test('hides a literal password in the userinfo', () => { + expect(maskConnectionUrl('postgresql://bao:hunter2@db:5432/app')).toBe('postgresql://bao:••••@db:5432/app') + expect(maskConnectionUrl('mongodb+srv://u:p%40ss@cluster.example.test/x')).toBe('mongodb+srv://u:••••@cluster.example.test/x') +}) + +test('hides a literal password parameter, in either style', () => { + expect(maskConnectionUrl('postgresql://db/app?user=bao&password=hunter2&sslmode=disable')).toBe( + 'postgresql://db/app?user=bao&password=••••&sslmode=disable', + ) + expect(maskConnectionUrl('host=db user=bao password=hunter2 dbname=app')).toBe('host=db user=bao password=•••• dbname=app') +}) + +test('leaves URLs without credentials alone', () => { + expect(maskConnectionUrl('postgresql://db:5432/app')).toBe('postgresql://db:5432/app') + expect(maskConnectionUrl('')).toBe('') +}) diff --git a/src/model/connectionUrl.ts b/src/model/connectionUrl.ts new file mode 100644 index 0000000..3444006 --- /dev/null +++ b/src/model/connectionUrl.ts @@ -0,0 +1,13 @@ +const MASK = '••••' + +/** + * A connection URL safe to put on screen. Templated passwords ("{{password}}", which OpenBao fills in itself) are + * kept, but a literal one typed into the URL ("user:secret@host", "?password=secret") is hidden. + */ +export function maskConnectionUrl(url: string): string { + return url + .replace(/^([a-z][a-z0-9+.-]*:\/\/[^:@/\s]*:)([^@/\s]*)(@)/i, (m, head: string, pw: string, at: string) => + pw.includes('{{') ? m : `${head}${MASK}${at}`, + ) + .replace(/([?&;\s]password=)([^&;\s]*)/gi, (m, head: string, pw: string) => (pw.includes('{{') ? m : `${head}${MASK}`)) +}