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
@@ -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