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)
|
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> {
|
export async function createToken(c: BaoClient, input: CreateTokenInput): Promise<CreatedToken> {
|
||||||
const res = await c.request<{ auth: { client_token: string; accessor: string; policies: string[] } }>(
|
const res = await c.request<{ auth: { client_token: string; accessor: string; policies: string[] } }>(
|
||||||
'POST',
|
'POST',
|
||||||
'auth/token/create',
|
'auth/token/create',
|
||||||
{
|
{
|
||||||
display_name: input.displayName || undefined,
|
display_name: input.displayName || undefined,
|
||||||
policies: input.policies,
|
policies: input.policies.length ? input.policies : DEFAULT_TOKEN_POLICIES,
|
||||||
ttl: input.ttl || undefined,
|
ttl: input.ttl || undefined,
|
||||||
renewable: input.renewable,
|
renewable: input.renewable,
|
||||||
no_parent: input.orphan || undefined,
|
no_parent: input.orphan || undefined,
|
||||||
|
|||||||
@@ -47,6 +47,20 @@ test('lists tokens with a human TTL', async () => {
|
|||||||
expect(within(row).getByText('2h')).toBeInTheDocument()
|
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 () => {
|
test('creates a token and shows its value once', async () => {
|
||||||
vi.mocked(api.createToken).mockResolvedValue({ token: 'hvs.SECRET', accessor: 'x', policies: ['dev'] })
|
vi.mocked(api.createToken).mockResolvedValue({ token: 'hvs.SECRET', accessor: 'x', policies: ['dev'] })
|
||||||
const user = setup()
|
const user = setup()
|
||||||
@@ -58,7 +72,7 @@ test('creates a token and shows its value once', async () => {
|
|||||||
|
|
||||||
expect(api.createToken).toHaveBeenCalledWith(client, {
|
expect(api.createToken).toHaveBeenCalledWith(client, {
|
||||||
displayName: 'ci',
|
displayName: 'ci',
|
||||||
policies: ['dev'],
|
policies: ['default', 'dev'],
|
||||||
ttl: '24h',
|
ttl: '24h',
|
||||||
renewable: true,
|
renewable: true,
|
||||||
orphan: false,
|
orphan: false,
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ interface Draft {
|
|||||||
orphan: boolean
|
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 {
|
function formatTtl(seconds: number): string {
|
||||||
if (!seconds) return 'no expiry'
|
if (!seconds) return 'no expiry'
|
||||||
@@ -161,12 +162,7 @@ export function TokensTab({ client }: { client: BaoClient }) {
|
|||||||
</span>
|
</span>
|
||||||
</label>
|
</label>
|
||||||
<PolicyPicker client={client} value={draft.policies} onChange={(policies) => setDraft({ ...draft, policies })} />
|
<PolicyPicker client={client} value={draft.policies} onChange={(policies) => setDraft({ ...draft, policies })} />
|
||||||
{draft.policies.length === 0 && (
|
{draft.policies.length === 0 && <p className="hint">No policies selected: the token gets only the default policy.</p>}
|
||||||
<p className="note-warn">
|
|
||||||
No policies selected: OpenBao gives the new token the same policies as yours
|
|
||||||
{tokenInfo?.policies.length ? ` (${tokenInfo.policies.join(', ')})` : ''}.
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
{create.error && <p role="alert">{create.error.message}</p>}
|
{create.error && <p role="alert">{create.error.message}</p>}
|
||||||
<div className="form-actions">
|
<div className="form-actions">
|
||||||
<button type="submit" disabled={create.isPending}>
|
<button type="submit" disabled={create.isPending}>
|
||||||
|
|||||||
Reference in new issue
Block a user