Secrets: browse, renew and revoke leases for dynamic engines

Adds a Leases tab that walks sys/leases/lookup under the mount, shows a
lease's timings on demand, renews renewable leases, and revokes one
lease or everything under a prefix after confirmation. Covered by unit
tests and an ssh OTP integration test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-11-09 13:45:14 +00:00
1 parent 48746e2150
commit 48ee757e8d
7 files changed
+362 -11

No files matched your search

-8
View File
@@ -2,7 +2,6 @@ import { expect, test, vi } from 'vitest'
import { BaoClient, BaoError } from '../bao'
import { deleteConfig, listConfigs, readConfig, runAction, writeConfig } from './config'
import { generateCreds } from './creds'
import { revokeLease } from './leases'
import { ENGINES } from './registry'
import { draftFromRole, emptyDraft, payloadFromDraft } from './roles'
@@ -74,10 +73,3 @@ test('credential requests: POST sends inputs, result is normalised', async () =>
await generateCreds(c, 'database/', dbCreds, 'ro', { ignored: true })
expect(req).toHaveBeenLastCalledWith('GET', 'database/creds/ro', undefined)
})
test('revoking a lease', async () => {
const c = new BaoClient('http://b', 't')
const req = vi.spyOn(c, 'request').mockResolvedValue(undefined)
await revokeLease(c, 'database/creds/ro/xyz')
expect(req).toHaveBeenCalledWith('PUT', 'sys/leases/revoke', { lease_id: 'database/creds/ro/xyz' })
})
+50
View File
@@ -0,0 +1,50 @@
import { expect, test, vi } from 'vitest'
import { BaoClient } from '../bao'
import { listLeases, lookupLease, renewLease, revokeLease, revokePrefix } from './leases'
const c = () => new BaoClient('http://b', 't')
test('lists folders before lease ids, joining them onto the prefix', async () => {
const client = c()
const list = vi.spyOn(client, 'list').mockResolvedValue(['zz9', 'ro/', 'aa1'])
expect(await listLeases(client, 'database/creds/')).toEqual([
{ name: 'ro/', path: 'database/creds/ro/', isFolder: true },
{ name: 'aa1', path: 'database/creds/aa1', isFolder: false },
{ name: 'zz9', path: 'database/creds/zz9', isFolder: false },
])
expect(list).toHaveBeenCalledWith('sys/leases/lookup/database/creds/')
})
test('looks up a lease and maps its fields', async () => {
const client = c()
const req = vi.spyOn(client, 'request').mockResolvedValue({
data: { id: 'database/creds/ro/abc', issue_time: '2026-01-01T00:00:00Z', expire_time: null, last_renewal: null, renewable: true, ttl: 120 },
})
expect(await lookupLease(client, 'database/creds/ro/abc')).toEqual({
id: 'database/creds/ro/abc',
issueTime: '2026-01-01T00:00:00Z',
expireTime: null,
lastRenewal: null,
renewable: true,
ttl: 120,
})
expect(req).toHaveBeenCalledWith('PUT', 'sys/leases/lookup', { lease_id: 'database/creds/ro/abc' })
})
test('renew sends an increment only when given and returns the granted duration', async () => {
const client = c()
const req = vi.spyOn(client, 'request').mockResolvedValue({ lease_duration: 300 })
expect(await renewLease(client, 'a/b')).toBe(300)
expect(req).toHaveBeenLastCalledWith('PUT', 'sys/leases/renew', { lease_id: 'a/b' })
await renewLease(client, 'a/b', '1h')
expect(req).toHaveBeenLastCalledWith('PUT', 'sys/leases/renew', { lease_id: 'a/b', increment: '1h' })
})
test('revoke and revoke-prefix', async () => {
const client = c()
const req = vi.spyOn(client, 'request').mockResolvedValue(undefined)
await revokeLease(client, 'database/creds/ro/xyz')
expect(req).toHaveBeenLastCalledWith('PUT', 'sys/leases/revoke', { lease_id: 'database/creds/ro/xyz' })
await revokePrefix(client, 'database/creds/ro/')
expect(req).toHaveBeenLastCalledWith('PUT', 'sys/leases/revoke-prefix/database/creds/ro', {})
})
+53
View File
@@ -1,6 +1,59 @@
import type { BaoClient } from '../bao'
const enc = (path: string) => path.split('/').map(encodeURIComponent).join('/')
export interface LeaseEntry {
name: string
/** Full lease id, or for folders the prefix (trailing slash) to browse next. */
path: string
isFolder: boolean
}
export interface LeaseInfo {
id: string
/** ISO timestamps; expireTime is null for leases that never expire. */
issueTime: string
expireTime: string | null
lastRenewal: string | null
renewable: boolean
/** Seconds remaining. */
ttl: number
}
/**
* Lists leases under a prefix such as "database/creds/" (the mount plus a path). Folders end with "/" and the rest are
* lease ids. Needs sudo on sys/leases/lookup, which most tokens lack.
*/
export async function listLeases(c: BaoClient, prefix: string): Promise<LeaseEntry[]> {
const keys = await c.list(`sys/leases/lookup/${enc(prefix)}`)
return keys
.map((k) => ({ name: k, path: prefix + k, isFolder: k.endsWith('/') }))
.sort((a, b) => Number(b.isFolder) - Number(a.isFolder) || a.name.localeCompare(b.name))
}
export async function lookupLease(c: BaoClient, leaseId: string): Promise<LeaseInfo> {
const res = await c.request<{
data: { id: string; issue_time: string; expire_time: string | null; last_renewal: string | null; renewable: boolean; ttl: number }
}>('PUT', 'sys/leases/lookup', { lease_id: leaseId })
const d = res.data
return { id: d.id, issueTime: d.issue_time, expireTime: d.expire_time, lastRenewal: d.last_renewal, renewable: d.renewable, ttl: d.ttl }
}
/** Extends a lease by `increment` (e.g. "1h"); OpenBao may grant less. Returns the new lease duration in seconds. */
export async function renewLease(c: BaoClient, leaseId: string, increment?: string): Promise<number> {
const res = await c.request<{ lease_duration: number }>('PUT', 'sys/leases/renew', {
lease_id: leaseId,
...(increment ? { increment } : {}),
})
return res.lease_duration
}
/** Revokes one lease, which also deletes the credentials it issued (database user, AWS key, ...). */
export async function revokeLease(c: BaoClient, leaseId: string): Promise<void> {
await c.request('PUT', 'sys/leases/revoke', { lease_id: leaseId })
}
/** Revokes every lease under a prefix, e.g. all credentials issued from one role. Needs sudo. */
export async function revokePrefix(c: BaoClient, prefix: string): Promise<void> {
await c.request('PUT', `sys/leases/revoke-prefix/${enc(prefix.replace(/\/+$/, ''))}`, {})
}
+21 -1
View File
@@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { BaoClient } from '../bao'
import { readConfig, runAction, writeConfig, deleteConfig } from './config'
import { generateCreds } from './creds'
import { revokeLease } from './leases'
import { listLeases, lookupLease, renewLease, revokeLease, revokePrefix } from './leases'
import { listMounts } from '../secrets'
import { ENGINES } from './registry'
import { deleteRole, draftFromRole, emptyDraft, listRoles, payloadFromDraft, readRole, writeRole } from './roles'
@@ -99,6 +99,26 @@ describe('dynamic engine config and credentials', () => {
await revokeLease(client, out.leaseId)
})
test('leases can be browsed, inspected, renewed and revoked by prefix', async () => {
const a = await generateCreds(client, mount, kind.creds[1], 'otp', { ip: '10.1.2.3' })
const b = await generateCreds(client, mount, kind.creds[1], 'otp', { ip: '10.1.2.4' })
// folders first: mount -> creds/ -> otp/ -> lease ids
expect(await listLeases(client, mount)).toContainEqual({ name: 'creds/', path: `${mount}creds/`, isFolder: true })
const ids = (await listLeases(client, `${mount}creds/otp/`)).map((e) => e.path)
expect(ids).toEqual(expect.arrayContaining([a.leaseId, b.leaseId]))
const info = await lookupLease(client, a.leaseId)
expect(info.id).toBe(a.leaseId)
expect(info.ttl).toBeGreaterThan(0)
expect(info.renewable).toBe(false)
// OTP leases are not renewable, and the UI only offers Renew when renewable; OpenBao says so itself
await expect(renewLease(client, a.leaseId)).rejects.toThrow()
await revokePrefix(client, `${mount}creds/otp/`)
expect(await listLeases(client, `${mount}creds/otp/`)).toEqual([])
})
test('single-endpoint config round-trips (aws lease)', async (ctx) => {
if (!awsAvailable) ctx.skip()
const lease = ENGINES.aws.configs[1]