From b55259c492a90581474119f1197570e6e96a0ff7 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Tue, 20 Oct 2026 12:52:13 +0100 Subject: [PATCH] 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 --- src/api/tokens.test.ts | 22 ++++++++++++++++++++++ src/api/tokens.ts | 5 ++++- src/features/identities/TokensTab.test.tsx | 16 +++++++++++++++- src/features/identities/TokensTab.tsx | 10 +++------- 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/api/tokens.test.ts diff --git a/src/api/tokens.test.ts b/src/api/tokens.test.ts new file mode 100644 index 0000000..c9caded --- /dev/null +++ b/src/api/tokens.test.ts @@ -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'] }) +}) diff --git a/src/api/tokens.ts b/src/api/tokens.ts index 6a6e514..cb68345 100644 --- a/src/api/tokens.ts +++ b/src/api/tokens.ts @@ -39,13 +39,16 @@ export async function listTokens(c: BaoClient): Promise { 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 { 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, diff --git a/src/features/identities/TokensTab.test.tsx b/src/features/identities/TokensTab.test.tsx index 4ad2b8d..2a02f73 100644 --- a/src/features/identities/TokensTab.test.tsx +++ b/src/features/identities/TokensTab.test.tsx @@ -47,6 +47,20 @@ test('lists tokens with a human TTL', async () => { expect(within(row).getByText('2h')).toBeInTheDocument() }) +test('a new token starts with only the default policy ticked', async () => { + setup() + await userEvent.setup().click(await screen.findByRole('button', { name: 'New token' })) + expect(await screen.findByLabelText('default')).toBeChecked() + expect(screen.getByLabelText('dev')).not.toBeChecked() +}) + +test('explains what an empty policy selection means', async () => { + const user = setup() + await user.click(await screen.findByRole('button', { name: 'New token' })) + await user.click(await screen.findByLabelText('default')) + expect(screen.getByText(/gets only the default policy/)).toBeInTheDocument() +}) + test('creates a token and shows its value once', async () => { vi.mocked(api.createToken).mockResolvedValue({ token: 'hvs.SECRET', accessor: 'x', policies: ['dev'] }) const user = setup() @@ -58,7 +72,7 @@ test('creates a token and shows its value once', async () => { expect(api.createToken).toHaveBeenCalledWith(client, { displayName: 'ci', - policies: ['dev'], + policies: ['default', 'dev'], ttl: '24h', renewable: true, orphan: false, diff --git a/src/features/identities/TokensTab.tsx b/src/features/identities/TokensTab.tsx index ea8ce04..f0b5170 100644 --- a/src/features/identities/TokensTab.tsx +++ b/src/features/identities/TokensTab.tsx @@ -16,7 +16,8 @@ interface Draft { orphan: boolean } -const empty: Draft = { displayName: '', policies: [], ttl: '', renewable: true, orphan: false } +// Least privilege by default: a new token starts with the default policy only. +const empty: Draft = { displayName: '', policies: ['default'], ttl: '', renewable: true, orphan: false } function formatTtl(seconds: number): string { if (!seconds) return 'no expiry' @@ -161,12 +162,7 @@ export function TokensTab({ client }: { client: BaoClient }) { setDraft({ ...draft, policies })} /> - {draft.policies.length === 0 && ( -

- No policies selected: OpenBao gives the new token the same policies as yours - {tokenInfo?.policies.length ? ` (${tokenInfo.policies.join(', ')})` : ''}. -

- )} + {draft.policies.length === 0 &&

No policies selected: the token gets only the default policy.

} {create.error &&

{create.error.message}

}