Access: grant and inspect access to dynamic engine roles
dynamicGrantRules expands a grant on a role into the credential paths its requests need (generate) and the role CRUD paths (manage); unticked access comes back empty so writing it revokes. Roles get an Access panel showing who can generate credentials or manage them, and the identity Access editor can pick dynamic mounts and roles. Checked against capabilities-self and a real token in the integration tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
48ee757e8d
commit
85a318c351
14 files changed
+642
-22
No files matched your search
@@ -4,11 +4,18 @@ import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
|
||||
import { BaoClient } from '../../api/bao'
|
||||
import * as grants from '../../api/grants'
|
||||
import * as roles from '../../api/engines/roles'
|
||||
import * as secrets from '../../api/secrets'
|
||||
import { AccessEditor } from './AccessEditor'
|
||||
|
||||
vi.mock('../../api/grants')
|
||||
vi.mock('../../api/secrets')
|
||||
vi.mock('../../api/engines/roles')
|
||||
vi.mock('../../api/secrets', async (orig) => ({
|
||||
...(await orig<typeof secrets>()),
|
||||
listMounts: vi.fn(),
|
||||
listSecrets: vi.fn(),
|
||||
readPolicyHcl: vi.fn(),
|
||||
}))
|
||||
|
||||
const client = new BaoClient('http://b', 't')
|
||||
const target = { kind: 'user' as const, name: 'alice' }
|
||||
@@ -29,7 +36,7 @@ const state = (over: Partial<grants.GrantState> = {}): grants.GrantState => ({
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(grants.getGrantState).mockResolvedValue(state())
|
||||
vi.mocked(secrets.listKvMounts).mockResolvedValue([{ path: 'secret/' }])
|
||||
vi.mocked(secrets.listMounts).mockResolvedValue([{ path: 'secret/', type: 'kv', version: '2' }])
|
||||
vi.mocked(secrets.listSecrets).mockImplementation(async (_c, _m, prefix) =>
|
||||
prefix === ''
|
||||
? [
|
||||
@@ -140,3 +147,32 @@ test('shows load errors', async () => {
|
||||
setup()
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('permission denied')
|
||||
})
|
||||
|
||||
test('grants generate on a dynamic-engine role from the identity side', async () => {
|
||||
vi.mocked(secrets.listMounts).mockResolvedValue([
|
||||
{ path: 'secret/', type: 'kv', version: '2' },
|
||||
{ path: 'database/', type: 'database' },
|
||||
])
|
||||
vi.mocked(roles.listRoles).mockResolvedValue(['readonly', 'readwrite'])
|
||||
vi.mocked(grants.grantAccess).mockResolvedValue()
|
||||
const user = setup()
|
||||
await user.selectOptions(await screen.findByLabelText('Secrets engine'), 'database/')
|
||||
await user.click(await screen.findByLabelText('readwrite'))
|
||||
await user.click(screen.getByLabelText(/Manage role/))
|
||||
await user.click(screen.getByRole('button', { name: 'Grant' }))
|
||||
expect(grants.grantAccess).toHaveBeenCalledWith(client, target, [
|
||||
{ path: 'database/creds/readwrite', capabilities: ['read'] },
|
||||
{ path: 'database/roles/readwrite', capabilities: ['create', 'read', 'update', 'delete'] },
|
||||
{ path: 'database/roles', capabilities: ['list'] },
|
||||
])
|
||||
})
|
||||
|
||||
test('a dynamic grant needs a role chosen', async () => {
|
||||
vi.mocked(secrets.listMounts).mockResolvedValue([{ path: 'database/', type: 'database' }])
|
||||
vi.mocked(roles.listRoles).mockResolvedValue(['readonly'])
|
||||
const user = setup()
|
||||
await screen.findByLabelText('readonly')
|
||||
await user.click(screen.getByRole('button', { name: 'Grant' }))
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('Choose a role.')
|
||||
expect(grants.grantAccess).not.toHaveBeenCalled()
|
||||
})
|
||||
@@ -2,13 +2,17 @@ import { useMutation, useQuery } from '@tanstack/react-query'
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import type { BaoClient } from '../../api/bao'
|
||||
import { getGrantState, grantAccess, revokeAccess, type Target } from '../../api/grants'
|
||||
import { listKvMounts, readPolicyHcl } from '../../api/secrets'
|
||||
import { kvGrantRules, validateGrantPath, type KvCapability } from '../../model/managed'
|
||||
import { engineFor, mountKind } from '../../api/engines/registry'
|
||||
import { readPolicyHcl } from '../../api/secrets'
|
||||
import { dynamicGrantRules, kvGrantRules, validateGrantPath, type DynamicAccess, type KvCapability } from '../../model/managed'
|
||||
import { groupGrants } from '../../model/grantRows'
|
||||
import { CapabilityChips } from '../shared/CapabilityChips'
|
||||
import { CapabilityPicker } from '../shared/CapabilityPicker'
|
||||
import { DynamicAccessPicker } from '../shared/DynamicAccessPicker'
|
||||
import { useMounts } from '../shared/useMounts'
|
||||
import { useRefreshAccess } from '../shared/useRefreshAccess'
|
||||
import { PathPicker } from './PathPicker'
|
||||
import { RolePicker, type RoleSelection } from './RolePicker'
|
||||
import { useConfirm } from '../shared/useConfirm'
|
||||
|
||||
interface Props {
|
||||
@@ -35,21 +39,29 @@ export function AccessEditor({ client, target, onClose }: Props) {
|
||||
const confirm = useConfirm()
|
||||
const stateKey = ['grant-state', target.kind, target.name]
|
||||
const state = useQuery({ queryKey: stateKey, queryFn: () => getGrantState(client, target) })
|
||||
const mounts = useQuery({ queryKey: ['kv-mounts'], queryFn: () => listKvMounts(client) })
|
||||
const mounts = useMounts(client)
|
||||
|
||||
const [mountChoice, setMount] = useState<string | null>(null)
|
||||
const [secretPath, setSecretPath] = useState('')
|
||||
const [caps, setCaps] = useState<KvCapability[]>(['read'])
|
||||
const [roleSel, setRoleSel] = useState<RoleSelection>({ kindId: '', role: '' })
|
||||
const [access, setAccess] = useState<DynamicAccess[]>(['generate'])
|
||||
const [showRaw, setShowRaw] = useState(false)
|
||||
const [formError, setFormError] = useState<string | null>(null)
|
||||
const mount = mountChoice ?? mounts.data?.[0]?.path ?? ''
|
||||
const current = mounts.data?.find((m) => m.path === mountChoice) ?? mounts.data?.[0]
|
||||
const mount = current?.path ?? ''
|
||||
const dynamic = current !== undefined && mountKind(current) === 'dynamic'
|
||||
const engine = dynamic ? engineFor(current.type) : null
|
||||
const roleKind = engine ? (engine.roleKinds.find((k) => k.id === roleSel.kindId) ?? engine.roleKinds[0]) : null
|
||||
|
||||
const done = useRefreshAccess()
|
||||
|
||||
const grant = useMutation({
|
||||
mutationFn: () => grantAccess(client, target, kvGrantRules(mount, secretPath, caps)),
|
||||
mutationFn: () =>
|
||||
grantAccess(client, target, roleKind ? dynamicGrantRules(roleKind, mount, roleSel.role, access) : kvGrantRules(mount, secretPath, caps)),
|
||||
onSuccess: () => {
|
||||
setSecretPath('')
|
||||
setRoleSel({ kindId: roleSel.kindId, role: '' })
|
||||
return done()
|
||||
},
|
||||
})
|
||||
@@ -57,7 +69,10 @@ export function AccessEditor({ client, target, onClose }: Props) {
|
||||
|
||||
function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
const err = validateGrantPath(secretPath) ?? (caps.length === 0 ? 'Select at least one capability.' : null)
|
||||
const err = roleKind
|
||||
? (roleSel.role ? validateGrantPath(`${mount}${roleKind.path}/${roleSel.role}`) : 'Choose a role.') ??
|
||||
(access.length === 0 ? 'Select at least one kind of access.' : null)
|
||||
: (validateGrantPath(secretPath) ?? (caps.length === 0 ? 'Select at least one capability.' : null))
|
||||
setFormError(err)
|
||||
if (!err) grant.mutate()
|
||||
}
|
||||
@@ -163,6 +178,7 @@ export function AccessEditor({ client, target, onClose }: Props) {
|
||||
onChange={(e) => {
|
||||
setMount(e.target.value)
|
||||
setSecretPath('')
|
||||
setRoleSel({ kindId: '', role: '' })
|
||||
}}
|
||||
>
|
||||
{mounts.data?.map((m) => (
|
||||
@@ -170,10 +186,13 @@ export function AccessEditor({ client, target, onClose }: Props) {
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{mount && <PathPicker key={mount} client={client} mount={mount} value={secretPath} onChange={setSecretPath} />}
|
||||
<CapabilityPicker value={caps} onChange={setCaps} />
|
||||
{mount && engine && <RolePicker key={mount} client={client} mount={mount} engine={engine} value={roleSel} onChange={setRoleSel} />}
|
||||
{mount && !engine && <PathPicker key={mount} client={client} mount={mount} value={secretPath} onChange={setSecretPath} />}
|
||||
{engine ? <DynamicAccessPicker value={access} onChange={setAccess} /> : <CapabilityPicker value={caps} onChange={setCaps} />}
|
||||
<p className="hint">
|
||||
Granting a path that already has a grant replaces its capabilities. “list” applies to the secret’s metadata path.
|
||||
{engine
|
||||
? 'Granting a role that already has a grant replaces its access. Unticked access is removed.'
|
||||
: 'Granting a path that already has a grant replaces its capabilities. “list” applies to the secret’s metadata path.'}
|
||||
</p>
|
||||
{(formError || grant.error) && <p role="alert">{formError ?? grant.error?.message}</p>}
|
||||
<button type="submit" disabled={grant.isPending || !mount}>
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useId } from 'react'
|
||||
import type { BaoClient } from '../../api/bao'
|
||||
import type { EngineDef } from '../../api/engines/registry'
|
||||
import { listRoles } from '../../api/engines/roles'
|
||||
|
||||
export interface RoleSelection {
|
||||
kindId: string
|
||||
/** A role name, "*" for every role of the kind, or "" for none chosen yet. */
|
||||
role: string
|
||||
}
|
||||
|
||||
/** Pick a role of a dynamic engine (or all of them) to grant access to. Lists role names only. */
|
||||
export function RolePicker({
|
||||
client,
|
||||
mount,
|
||||
engine,
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
client: BaoClient
|
||||
mount: string
|
||||
engine: EngineDef
|
||||
value: RoleSelection
|
||||
onChange: (v: RoleSelection) => void
|
||||
}) {
|
||||
const id = useId()
|
||||
const kind = engine.roleKinds.find((k) => k.id === value.kindId) ?? engine.roleKinds[0]
|
||||
const roles = useQuery({ queryKey: ['engine-roles', mount, kind.id], queryFn: () => listRoles(client, mount, kind) })
|
||||
const all = value.role === '*'
|
||||
|
||||
return (
|
||||
<fieldset className="path-picker">
|
||||
<legend>Role</legend>
|
||||
{engine.roleKinds.length > 1 && (
|
||||
<label>
|
||||
Role type
|
||||
<select value={kind.id} onChange={(e) => onChange({ kindId: e.target.value, role: '' })}>
|
||||
{engine.roleKinds.map((k) => (
|
||||
<option key={k.id} value={k.id}>
|
||||
{k.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
{roles.isPending && <p className="hint">Loading…</p>}
|
||||
{roles.error && <p role="alert">Can’t list roles here: {roles.error.message}</p>}
|
||||
{roles.data?.length === 0 && <p className="hint">No roles in {mount}{kind.path} yet.</p>}
|
||||
{!!roles.data?.length && (
|
||||
<ul className="entries" aria-label={`Roles in ${mount}`}>
|
||||
{roles.data.map((name) => (
|
||||
<li key={name}>
|
||||
<label className="entry-secret">
|
||||
<input
|
||||
type="radio"
|
||||
name={`${id}-role`}
|
||||
checked={value.role === name}
|
||||
disabled={all}
|
||||
onChange={() => onChange({ kindId: kind.id, role: name })}
|
||||
/>
|
||||
{name}
|
||||
</label>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<label className="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={all}
|
||||
onChange={(e) => onChange({ kindId: kind.id, role: e.target.checked ? '*' : '' })}
|
||||
/>
|
||||
<span>
|
||||
Every role in <code>{mount}{kind.path}</code>, including ones added later
|
||||
</span>
|
||||
</label>
|
||||
</fieldset>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user