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:
mattandClaude Sonnet 5.5 committed 2026-11-09 13:51:28 +00:00
1 parent 48ee757e8d
commit 85a318c351
14 files changed
+642 -22

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import { expect, test } from 'vitest'
import { ENGINES } from '../api/engines/registry'
import { accessForPath } from './graph'
import { dynamicGrantRules, parseManagedPolicy, renderManagedPolicy } from './managed'
import { pathMatches } from './policy'
const dbRoles = ENGINES.database.roleKinds[0]
const dbStatic = ENGINES.database.roleKinds[1]
const aws = ENGINES.aws.roleKinds[0]
const ssh = ENGINES.ssh.roleKinds[0]
test('database: generate is read on creds, and is removed when not granted', () => {
expect(dynamicGrantRules(dbRoles, 'database/', 'ro', ['generate'])).toEqual([
{ path: 'database/creds/ro', capabilities: ['read'] },
{ path: 'database/roles/ro', capabilities: [] },
])
expect(dynamicGrantRules(dbStatic, 'database/', 'svc', ['generate'])[0]).toEqual({ path: 'database/static-creds/svc', capabilities: ['read'] })
})
test('manage grants role CRUD plus listing, and generate is revoked when unticked', () => {
expect(dynamicGrantRules(dbRoles, 'database', 'ro', ['manage'])).toEqual([
{ path: 'database/creds/ro', capabilities: [] },
{ path: 'database/roles/ro', capabilities: ['create', 'read', 'update', 'delete'] },
{ path: 'database/roles', capabilities: ['list'] },
])
})
test('aws and ssh use the capabilities their requests need', () => {
expect(dynamicGrantRules(aws, 'aws/', 'ci', ['generate'])[0]).toEqual({ path: 'aws/creds/ci', capabilities: ['read', 'update'] })
const s = dynamicGrantRules(ssh, 'ssh/', 'ops', ['generate'])
expect(s.slice(0, 2)).toEqual([
{ path: 'ssh/sign/ops', capabilities: ['update'] },
{ path: 'ssh/creds/ops', capabilities: ['update'] },
])
})
test('"*" grants every role of the kind', () => {
expect(dynamicGrantRules(dbRoles, 'database/', '*', ['generate', 'manage'])).toEqual([
{ path: 'database/creds/*', capabilities: ['read'] },
{ path: 'database/roles/*', capabilities: ['create', 'read', 'update', 'delete'] },
{ path: 'database/roles', capabilities: ['list'] },
])
})
test('rules survive a managed-policy round trip and resolve in the access model', () => {
const grants = dynamicGrantRules(dbRoles, 'database/', 'ro', ['generate']).filter((g) => g.capabilities.length)
const hcl = renderManagedPolicy('user', 'alice', grants)
const parsed = parseManagedPolicy(hcl)
expect(parsed.editable).toBe(true)
expect(parsed.grants).toEqual(grants)
const entries = accessForPath(
[{ kind: 'user', name: 'alice', policies: ['envbao-user-alice'] }],
[{ name: 'envbao-user-alice', hcl }],
'database/creds/ro',
pathMatches,
)
expect(entries).toHaveLength(1)
expect(entries[0].capabilities).toEqual(['read'])
expect(accessForPath([{ kind: 'user', name: 'alice', policies: ['envbao-user-alice'] }], [{ name: 'envbao-user-alice', hcl }], 'database/creds/other', pathMatches)).toEqual([])
})
+26
View File
@@ -1,3 +1,4 @@
import type { RoleKind } from '../api/engines/registry'
import { MANAGED_PREFIX } from './graph'
import { CAPABILITIES, parsePolicy, type Capability } from './policy'
@@ -94,3 +95,28 @@ export function kvGrantRules(mount: string, secretPath: string, caps: KvCapabili
{ path: `${m}metadata/${secretPath}`, capabilities: caps.includes('list') ? ['list'] : [] },
]
}
/** What a grant on a dynamic-engine role can allow. */
export type DynamicAccess = 'generate' | 'manage'
export const DYNAMIC_ACCESS: { id: DynamicAccess; label: string; help: string }[] = [
{ id: 'generate', label: 'Generate credentials', help: 'Issue database logins, AWS keys, SSH certificates or OTPs from the role.' },
{ id: 'manage', label: 'Manage role', help: 'View, change and delete the role, and list the roles of this kind.' },
]
/**
* Expand a grant on a dynamic-engine role (`role` may be "*" for every role of that kind) into API-path rules.
* Like kvGrantRules, a path that is not granted comes back with no capabilities so that writing the rules removes it;
* the shared "list roles" path is only emitted when managing, so revoking one role never hides the others.
*/
export function dynamicGrantRules(roleKind: RoleKind, mount: string, role: string, access: DynamicAccess[]): Grant[] {
const m = mount.endsWith('/') ? mount : `${mount}/`
const rules: Grant[] = roleKind.creds.map((c) => ({
path: m + c.path(role),
capabilities: access.includes('generate') ? c.capabilities : [],
}))
const manage = access.includes('manage')
rules.push({ path: `${m}${roleKind.path}/${role}`, capabilities: manage ? ['create', 'read', 'update', 'delete'] : [] })
if (manage) rules.push({ path: `${m}${roleKind.path}`, capabilities: ['list'] })
return rules
}