Demo and CI: Postgres and SSH server for the dynamic engines

docker compose now runs a Postgres with sample tables and an SSH server
that trusts the CA in OpenBao's ssh/ engine. seed.sh configures the
database engine (dynamic and static roles), the ssh CA and a role, a few
live leases, and grants for alice and the platform group. CI gains a
Postgres service so the database engine and its admin-policy rules are
tested against a real database (skipped when BAO_PG_URL is unset).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-11-09 13:58:59 +00:00
1 parent 0eff098140
commit 11c9483a88
15 files changed
+399 -5

No files matched your search

+50 -2
View File
@@ -6,11 +6,12 @@ import * as ids from './identities'
import * as secrets from './secrets'
import * as tokens from './tokens'
import { kvGrantRules } from '../model/managed'
import { deleteConfig, readConfig, runAction } from './engines/config'
import { deleteConfig, listConfigs, readConfig, runAction, writeConfig } from './engines/config'
import { generateCreds } from './engines/creds'
import { listLeases, lookupLease, renewLease, revokeLease, revokePrefix } from './engines/leases'
import { ENGINES } from './engines/registry'
import { deleteRole, listRoles, readRole, writeRole } from './engines/roles'
import { pgTarget } from '../test/postgres'
import { deleteRole, emptyDraft, listRoles, payloadFromDraft, readRole, writeRole } from './engines/roles'
// Everything the UI does, run with ONLY docs/envbao-admin.hcl (plus the default policy), so the
// documented least-privilege policy can't silently drift from what the app needs.
@@ -155,3 +156,50 @@ describe('the admin policy covers dynamic engines (ssh at the default mount)', (
await deleteConfig(admin, 'ssh/', ca)
})
})
// Same idea for the database engine at database/, when BAO_PG_URL points at a Postgres (see src/test/postgres.ts).
describe('the admin policy covers the database engine (at the default mount)', () => {
const pg = pgTarget()
const db = ENGINES.database
let mounted = false
beforeAll(async () => {
const mounts = await root.request<{ data: Record<string, unknown> }>('GET', 'sys/mounts')
if (pg && !('database/' in mounts.data)) {
await root.request('POST', 'sys/mounts/database', { type: 'database' })
mounted = true
}
})
afterAll(async () => {
if (mounted) await root.request('DELETE', 'sys/mounts/database').catch(() => {})
})
test('connection, roles, credentials and leases', async (ctx) => {
if (!mounted) ctx.skip()
const conn = db.configs[0]
const kind = db.roleKinds[0]
const cfg = emptyDraft(conn)
Object.assign(cfg.values, pg!.values)
await writeConfig(admin, 'database/', conn, payloadFromDraft(conn, cfg), 'app')
expect(await listConfigs(admin, 'database/', conn)).toEqual(['app'])
expect(await readConfig(admin, 'database/', conn, 'app')).not.toBeNull()
await runAction(admin, 'database/', conn.actions![1], 'app')
const role = emptyDraft(kind)
role.values.db_name = 'app'
role.values.creation_statements = pg!.creationStatements
await writeRole(admin, 'database/', kind, 'ro', payloadFromDraft(kind, role))
expect(await listRoles(admin, 'database/', kind)).toEqual(['ro'])
expect((await readRole(admin, 'database/', kind, 'ro')).db_name).toBe('app')
const creds = await generateCreds(admin, 'database/', kind.creds[0], 'ro', {})
expect((await listLeases(admin, 'database/creds/ro/')).length).toBe(1)
expect((await lookupLease(admin, creds.leaseId)).id).toBe(creds.leaseId)
await revokeLease(admin, creds.leaseId)
await generateCreds(admin, 'database/', kind.creds[0], 'ro', {})
await revokePrefix(admin, 'database/creds/ro/')
await deleteRole(admin, 'database/', kind, 'ro')
await deleteConfig(admin, 'database/', conn, 'app')
})
})
@@ -0,0 +1,82 @@
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { pgTarget } from '../../test/postgres'
import { BaoClient } from '../bao'
import { deleteConfig, listConfigs, readConfig, runAction, writeConfig } from './config'
import { generateCreds } from './creds'
import { listLeases, lookupLease, revokeLease } from './leases'
import { ENGINES } from './registry'
import { deleteRole, draftFromRole, emptyDraft, listRoles, payloadFromDraft, readRole, writeRole } from './roles'
// Runs the UI's own form model against a real database engine and Postgres. Needs BAO_PG_URL (see src/test/postgres.ts).
const pg = pgTarget()
const client = new BaoClient(process.env.BAO_ADDR ?? 'http://127.0.0.1:8200', process.env.BAO_TOKEN ?? 'root')
const sfx = Math.random().toString(36).slice(2, 8)
const mount = `it-db-${sfx}/`
const conn = ENGINES.database.configs[0]
const kind = ENGINES.database.roleKinds[0]
describe.skipIf(!pg)('database engine against Postgres', () => {
beforeAll(async () => {
await client.request('POST', `sys/mounts/${mount.slice(0, -1)}`, { type: 'database' })
})
afterAll(async () => {
await client.request('DELETE', `sys/mounts/${mount.slice(0, -1)}`).catch(() => {})
})
test('creates a connection through the form model; the password is never read back', async () => {
const draft = emptyDraft(conn)
draft.name = 'app'
Object.assign(draft.values, pg!.values)
await writeConfig(client, mount, conn, payloadFromDraft(conn, draft), 'app')
expect(await listConfigs(client, mount, conn)).toEqual(['app'])
const read = await readConfig(client, mount, conn, 'app')
expect(read).toMatchObject({ plugin_name: 'postgresql-database-plugin', username: pg!.values.username })
expect(String(read!.connection_url)).toContain('{{username}}')
expect(JSON.stringify(read)).not.toContain(String(pg!.values.password))
// editing with the password left blank must keep the stored one, or the next credential request would fail
const edit = draftFromRole(conn, 'app', read!)
expect(edit.values.password).toBe('')
edit.values.allowed_roles = 'ro'
await writeConfig(client, mount, conn, payloadFromDraft(conn, edit), 'app')
expect((await readConfig(client, mount, conn, 'app'))!.allowed_roles).toEqual(['ro'])
await runAction(client, mount, conn.actions![1], 'app') // reset
})
test('creates a role with multi-statement SQL, generates credentials and revokes the lease', async () => {
const draft = emptyDraft(kind)
draft.name = 'ro'
draft.values.db_name = 'app'
draft.values.creation_statements = pg!.creationStatements
draft.values.default_ttl = '5m'
await writeRole(client, mount, kind, 'ro', payloadFromDraft(kind, draft))
expect(await listRoles(client, mount, kind)).toEqual(['ro'])
const back = draftFromRole(kind, 'ro', await readRole(client, mount, kind, 'ro'))
expect(back.values.default_ttl).toBe('300s')
expect(payloadFromDraft(kind, back).creation_statements).toHaveLength(2)
const creds = await generateCreds(client, mount, kind.creds[0], 'ro', {})
expect(String(creds.data.username)).toMatch(/^v-/)
expect(String(creds.data.password).length).toBeGreaterThan(8)
expect(creds.leaseId).toContain(`${mount}creds/ro/`)
expect((await lookupLease(client, creds.leaseId)).renewable).toBe(true)
expect((await listLeases(client, `${mount}creds/ro/`)).map((e) => e.path)).toContain(creds.leaseId)
await revokeLease(client, creds.leaseId)
expect(await listLeases(client, `${mount}creds/ro/`)).toEqual([])
})
test('a role whose SQL is wrong reports the database error', async () => {
const bad = emptyDraft(kind)
bad.values.db_name = 'app'
bad.values.creation_statements = 'CREATE NONSENSE "{{name}}";'
await writeRole(client, mount, kind, 'bad', payloadFromDraft(kind, bad))
await expect(generateCreds(client, mount, kind.creds[0], 'bad', {})).rejects.toThrow()
await deleteRole(client, mount, kind, 'bad')
await deleteRole(client, mount, kind, 'ro')
await deleteConfig(client, mount, conn, 'app')
expect(await listRoles(client, mount, kind)).toEqual([])
})
})