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 <noreply@anthropic.com>
This commit is contained in:
1 parent
44e4eb75c9
commit
fd20caa525
4 files changed
+116
-9
No files matched your search
@@ -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')
|
||||
})
|
||||
@@ -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 (
|
||||
<tr>
|
||||
<td>{name}</td>
|
||||
<td data-label="Connection URL">
|
||||
{cfg.isPending ? (
|
||||
<span className="hint">Loading…</span>
|
||||
) : cfg.error ? (
|
||||
<span role="alert">{cfg.error.message}</span>
|
||||
) : typeof url === 'string' && url ? (
|
||||
<code>{maskConnectionUrl(url)}</code>
|
||||
) : (
|
||||
<span className="hint">None</span>
|
||||
)}
|
||||
</td>
|
||||
<td data-label="Allowed roles">{cfg.data ? <NameChips names={roles} /> : null}</td>
|
||||
<td>
|
||||
<button type="button" aria-label={`Edit ${name}`} onClick={onEdit}>
|
||||
Edit
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Connection URL</th>
|
||||
<th>Allowed roles</th>
|
||||
<th />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{names.data.map((n) => (
|
||||
<tr key={n}>
|
||||
<td>{n}</td>
|
||||
<td>
|
||||
<button type="button" aria-label={`Edit ${n}`} onClick={() => open.mutate(n)}>
|
||||
Edit
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<ConnectionRow key={n} client={client} mount={mount} kind={kind} name={n} onEdit={() => open.mutate(n)} />
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -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('')
|
||||
})
|
||||
@@ -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}`))
|
||||
}
|
||||
Reference in new issue
Block a user