Add token management: create, list, revoke with show-once secret
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0e1e8988bd
commit
f2ba697093
6 files changed
+373
-1
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { BaoClient } from './bao'
|
||||
import * as api from './tokens'
|
||||
|
||||
const client = new BaoClient(process.env.BAO_ADDR ?? 'http://127.0.0.1:8200', process.env.BAO_TOKEN ?? 'root')
|
||||
|
||||
describe('token API against a dev OpenBao', () => {
|
||||
test('create, authenticate, list, revoke', async () => {
|
||||
const created = await api.createToken(client, {
|
||||
displayName: 'it-token',
|
||||
policies: ['default', 'it-extra'],
|
||||
ttl: '1h',
|
||||
renewable: false,
|
||||
orphan: false,
|
||||
})
|
||||
expect(created.token).toBeTruthy()
|
||||
|
||||
// the new token authenticates with the chosen policies
|
||||
const self = await client.withToken(created.token).lookupSelf()
|
||||
expect(self.policies.sort()).toEqual(['default', 'it-extra'])
|
||||
expect(self.display_name).toBe('token-it-token')
|
||||
expect(self.renewable).toBe(false)
|
||||
|
||||
const listed = await api.listTokens(client)
|
||||
expect(listed.find((t) => t.accessor === created.accessor)).toMatchObject({ display_name: 'token-it-token' })
|
||||
|
||||
await api.revokeToken(client, created.accessor)
|
||||
await expect(client.withToken(created.token).lookupSelf()).rejects.toMatchObject({ kind: 'forbidden' })
|
||||
expect((await api.listTokens(client)).some((t) => t.accessor === created.accessor)).toBe(false)
|
||||
})
|
||||
|
||||
test('orphan tokens survive revocation of their parent', async () => {
|
||||
const parent = await api.createToken(client, {
|
||||
displayName: 'it-parent',
|
||||
policies: ['root'],
|
||||
renewable: true,
|
||||
orphan: false,
|
||||
})
|
||||
const parentClient = client.withToken(parent.token)
|
||||
const child = await api.createToken(parentClient, {
|
||||
displayName: 'it-orphan',
|
||||
policies: ['default'],
|
||||
renewable: false,
|
||||
orphan: true,
|
||||
})
|
||||
const plain = await api.createToken(parentClient, {
|
||||
displayName: 'it-child',
|
||||
policies: ['default'],
|
||||
renewable: false,
|
||||
orphan: false,
|
||||
})
|
||||
await api.revokeToken(client, parent.accessor)
|
||||
expect((await client.withToken(child.token).lookupSelf()).policies).toContain('default')
|
||||
await expect(client.withToken(plain.token).lookupSelf()).rejects.toMatchObject({ kind: 'forbidden' })
|
||||
await api.revokeToken(client, child.accessor)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { BaoClient } from './bao'
|
||||
|
||||
export interface TokenSummary {
|
||||
accessor: string
|
||||
display_name: string
|
||||
policies: string[]
|
||||
/** Remaining TTL in seconds; 0 means no expiry (e.g. root). */
|
||||
ttl: number
|
||||
renewable: boolean
|
||||
orphan: boolean
|
||||
creation_time: string
|
||||
expire_time: string | null
|
||||
}
|
||||
|
||||
export interface CreateTokenInput {
|
||||
displayName: string
|
||||
policies: string[]
|
||||
ttl?: string
|
||||
renewable: boolean
|
||||
/** Detach from the creating token so it outlives it. Needs sudo. */
|
||||
orphan: boolean
|
||||
}
|
||||
|
||||
export interface CreatedToken {
|
||||
token: string
|
||||
accessor: string
|
||||
policies: string[]
|
||||
}
|
||||
|
||||
export async function lookupAccessor(c: BaoClient, accessor: string): Promise<TokenSummary> {
|
||||
const res = await c.request<{ data: TokenSummary }>('POST', 'auth/token/lookup-accessor', { accessor })
|
||||
return res.data
|
||||
}
|
||||
|
||||
/** A token can expire between listing and lookup, so individual failures are skipped. */
|
||||
export async function listTokens(c: BaoClient): Promise<TokenSummary[]> {
|
||||
const accessors = await c.list('auth/token/accessors')
|
||||
const found = await Promise.all(accessors.map((a) => lookupAccessor(c, a).catch(() => null)))
|
||||
return found.filter((t): t is TokenSummary => t !== null)
|
||||
}
|
||||
|
||||
export async function createToken(c: BaoClient, input: CreateTokenInput): Promise<CreatedToken> {
|
||||
const res = await c.request<{ auth: { client_token: string; accessor: string; policies: string[] } }>(
|
||||
'POST',
|
||||
'auth/token/create',
|
||||
{
|
||||
display_name: input.displayName || undefined,
|
||||
policies: input.policies,
|
||||
ttl: input.ttl || undefined,
|
||||
renewable: input.renewable,
|
||||
no_parent: input.orphan || undefined,
|
||||
},
|
||||
)
|
||||
return { token: res.auth.client_token, accessor: res.auth.accessor, policies: res.auth.policies }
|
||||
}
|
||||
|
||||
export async function revokeToken(c: BaoClient, accessor: string): Promise<void> {
|
||||
await c.request('POST', 'auth/token/revoke-accessor', { accessor })
|
||||
}
|
||||
Reference in new issue
Block a user