Default new tokens to the default policy only (ENB-21)
OpenBao copies the creator's policies when none are sent, so a token created with nothing ticked while signed in as root was itself root. The New token form now starts with `default` ticked, and createToken sends ['default'] for an empty selection. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a2054eb41
commit
b55259c492
4 files changed
+44
-9
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { expect, test, vi } from 'vitest'
|
||||
import type { BaoClient } from './bao'
|
||||
import { createToken } from './tokens'
|
||||
|
||||
function fakeClient() {
|
||||
const request = vi.fn().mockResolvedValue({ auth: { client_token: 's.x', accessor: 'a', policies: ['default'] } })
|
||||
return { client: { request } as unknown as BaoClient, request }
|
||||
}
|
||||
|
||||
const base = { displayName: 'ci', renewable: true, orphan: false }
|
||||
|
||||
test('a token with no policies gets only the default policy, not its creator’s', async () => {
|
||||
const { client, request } = fakeClient()
|
||||
await createToken(client, { ...base, policies: [] })
|
||||
expect(request.mock.calls[0][2]).toMatchObject({ policies: ['default'] })
|
||||
})
|
||||
|
||||
test('chosen policies are sent as they are', async () => {
|
||||
const { client, request } = fakeClient()
|
||||
await createToken(client, { ...base, policies: ['dev'] })
|
||||
expect(request.mock.calls[0][2]).toMatchObject({ policies: ['dev'] })
|
||||
})
|
||||
+4
-1
@@ -39,13 +39,16 @@ export async function listTokens(c: BaoClient): Promise<TokenSummary[]> {
|
||||
return found.filter((t): t is TokenSummary => t !== null)
|
||||
}
|
||||
|
||||
/** What a token gets when no policy is chosen. OpenBao would otherwise copy the creator's policies (root, for root). */
|
||||
export const DEFAULT_TOKEN_POLICIES = ['default']
|
||||
|
||||
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,
|
||||
policies: input.policies.length ? input.policies : DEFAULT_TOKEN_POLICIES,
|
||||
ttl: input.ttl || undefined,
|
||||
renewable: input.renewable,
|
||||
no_parent: input.orphan || undefined,
|
||||
|
||||
Reference in new issue
Block a user