Add identity management: userpass users, entities with aliases, groups
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a13d2cb04a
commit
4706014dc2
10 files changed
+733
-1
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { beforeAll, describe, expect, test } from 'vitest'
|
||||
import { BaoClient } from './bao'
|
||||
import * as api from './identities'
|
||||
|
||||
const client = new BaoClient(process.env.BAO_ADDR ?? 'http://127.0.0.1:8200', process.env.BAO_TOKEN ?? 'root')
|
||||
const suffix = Math.random().toString(36).slice(2, 8)
|
||||
|
||||
beforeAll(async () => {
|
||||
const mounts = await client.request<{ data: Record<string, { type: string }> }>('GET', 'sys/auth')
|
||||
if (!Object.values(mounts.data).some((m) => m.type === 'userpass')) {
|
||||
await client.request('POST', 'sys/auth/userpass', { type: 'userpass' })
|
||||
}
|
||||
})
|
||||
|
||||
describe('identity API against a dev OpenBao', () => {
|
||||
test('userpass user CRUD', async () => {
|
||||
const name = `it-user-${suffix}`
|
||||
await api.saveUser(client, { name, password: 'pw-123456', policies: ['default'], ttl: '1h' })
|
||||
expect((await api.listUsers(client)).find((u) => u.name === name)).toMatchObject({
|
||||
policies: ['default'],
|
||||
ttl: 3600,
|
||||
})
|
||||
|
||||
// edit without a password keeps the old one and updates policies
|
||||
await api.saveUser(client, { name, policies: ['default', 'it-extra'] })
|
||||
expect((await api.readUser(client, name)).policies).toEqual(['default', 'it-extra'])
|
||||
const login = await client.loginUserpass(name, 'pw-123456')
|
||||
expect(login.token).toBeTruthy()
|
||||
|
||||
await api.deleteUser(client, name)
|
||||
expect((await api.listUsers(client)).some((u) => u.name === name)).toBe(false)
|
||||
})
|
||||
|
||||
test('entity with alias, and group membership', async () => {
|
||||
const user = `it-alias-${suffix}`
|
||||
const ename = `it-entity-${suffix}`
|
||||
const gname = `it-group-${suffix}`
|
||||
await api.saveUser(client, { name: user, password: 'pw-123456', policies: [] })
|
||||
|
||||
await api.saveEntity(client, { name: ename, policies: ['default'] })
|
||||
const entity = await api.readEntity(client, ename)
|
||||
const accessor = await api.userpassAccessor(client)
|
||||
expect(accessor).toBeTruthy()
|
||||
await api.addAlias(client, { name: user, entityId: entity.id, mountAccessor: accessor! })
|
||||
const withAlias = await api.readEntity(client, ename)
|
||||
expect(withAlias.aliases).toHaveLength(1)
|
||||
expect(withAlias.aliases[0].name).toBe(user)
|
||||
|
||||
await api.saveGroup(client, { name: gname, policies: ['default'], memberEntityIds: [entity.id] })
|
||||
expect((await api.listGroups(client)).find((g) => g.name === gname)?.member_entity_ids).toEqual([entity.id])
|
||||
|
||||
await api.deleteGroup(client, gname)
|
||||
await api.deleteAlias(client, withAlias.aliases[0].id)
|
||||
expect((await api.readEntity(client, ename)).aliases).toHaveLength(0)
|
||||
await api.deleteEntity(client, ename)
|
||||
await api.deleteUser(client, user)
|
||||
expect((await api.listEntities(client)).some((e) => e.name === ename)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,139 @@
|
||||
import type { BaoClient } from './bao'
|
||||
|
||||
const enc = encodeURIComponent
|
||||
|
||||
export interface UserpassUser {
|
||||
name: string
|
||||
policies: string[]
|
||||
ttl: number
|
||||
}
|
||||
|
||||
export interface EntityAlias {
|
||||
id: string
|
||||
name: string
|
||||
mount_accessor: string
|
||||
mount_path: string
|
||||
}
|
||||
|
||||
export interface Entity {
|
||||
id: string
|
||||
name: string
|
||||
policies: string[]
|
||||
disabled: boolean
|
||||
aliases: EntityAlias[]
|
||||
}
|
||||
|
||||
export interface Group {
|
||||
id: string
|
||||
name: string
|
||||
policies: string[]
|
||||
member_entity_ids: string[]
|
||||
}
|
||||
|
||||
export async function listPolicyNames(c: BaoClient): Promise<string[]> {
|
||||
return c.list('sys/policies/acl')
|
||||
}
|
||||
|
||||
// ---- userpass users ----
|
||||
|
||||
export async function listUsers(c: BaoClient): Promise<UserpassUser[]> {
|
||||
const names = await c.list('auth/userpass/users')
|
||||
return Promise.all(names.map((n) => readUser(c, n)))
|
||||
}
|
||||
|
||||
export async function readUser(c: BaoClient, name: string): Promise<UserpassUser> {
|
||||
const res = await c.request<{ data: { token_policies?: string[]; token_ttl?: number } }>(
|
||||
'GET',
|
||||
`auth/userpass/users/${enc(name)}`,
|
||||
)
|
||||
return { name, policies: res.data.token_policies ?? [], ttl: res.data.token_ttl ?? 0 }
|
||||
}
|
||||
|
||||
/** `password` is omitted on edit to keep the existing one. */
|
||||
export async function saveUser(
|
||||
c: BaoClient,
|
||||
user: { name: string; password?: string; policies: string[]; ttl?: string },
|
||||
): Promise<void> {
|
||||
await c.request('POST', `auth/userpass/users/${enc(user.name)}`, {
|
||||
...(user.password ? { password: user.password } : {}),
|
||||
token_policies: user.policies,
|
||||
...(user.ttl ? { token_ttl: user.ttl } : {}),
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteUser(c: BaoClient, name: string): Promise<void> {
|
||||
await c.request('DELETE', `auth/userpass/users/${enc(name)}`)
|
||||
}
|
||||
|
||||
/** Mount accessor of the userpass auth method, needed to create entity aliases. */
|
||||
export async function userpassAccessor(c: BaoClient): Promise<string | null> {
|
||||
const res = await c.request<{ data: Record<string, { accessor: string; type: string }> }>('GET', 'sys/auth')
|
||||
return Object.values(res.data).find((m) => m.type === 'userpass')?.accessor ?? null
|
||||
}
|
||||
|
||||
// ---- entities ----
|
||||
|
||||
export async function listEntities(c: BaoClient): Promise<Entity[]> {
|
||||
const names = await c.list('identity/entity/name')
|
||||
return Promise.all(names.map((n) => readEntity(c, n)))
|
||||
}
|
||||
|
||||
export async function readEntity(c: BaoClient, name: string): Promise<Entity> {
|
||||
const res = await c.request<{ data: Partial<Entity> & { id: string } }>('GET', `identity/entity/name/${enc(name)}`)
|
||||
const d = res.data
|
||||
return { id: d.id, name, policies: d.policies ?? [], disabled: d.disabled ?? false, aliases: d.aliases ?? [] }
|
||||
}
|
||||
|
||||
export async function saveEntity(
|
||||
c: BaoClient,
|
||||
e: { name: string; policies: string[]; disabled?: boolean },
|
||||
): Promise<void> {
|
||||
await c.request('POST', `identity/entity/name/${enc(e.name)}`, { policies: e.policies, disabled: e.disabled ?? false })
|
||||
}
|
||||
|
||||
export async function deleteEntity(c: BaoClient, name: string): Promise<void> {
|
||||
await c.request('DELETE', `identity/entity/name/${enc(name)}`)
|
||||
}
|
||||
|
||||
export async function addAlias(
|
||||
c: BaoClient,
|
||||
a: { name: string; entityId: string; mountAccessor: string },
|
||||
): Promise<void> {
|
||||
await c.request('POST', 'identity/entity-alias', {
|
||||
name: a.name,
|
||||
canonical_id: a.entityId,
|
||||
mount_accessor: a.mountAccessor,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteAlias(c: BaoClient, aliasId: string): Promise<void> {
|
||||
await c.request('DELETE', `identity/entity-alias/id/${enc(aliasId)}`)
|
||||
}
|
||||
|
||||
// ---- groups ----
|
||||
|
||||
export async function listGroups(c: BaoClient): Promise<Group[]> {
|
||||
const names = await c.list('identity/group/name')
|
||||
return Promise.all(names.map((n) => readGroup(c, n)))
|
||||
}
|
||||
|
||||
export async function readGroup(c: BaoClient, name: string): Promise<Group> {
|
||||
const res = await c.request<{ data: Partial<Group> & { id: string } }>('GET', `identity/group/name/${enc(name)}`)
|
||||
const d = res.data
|
||||
return { id: d.id, name, policies: d.policies ?? [], member_entity_ids: d.member_entity_ids ?? [] }
|
||||
}
|
||||
|
||||
export async function saveGroup(
|
||||
c: BaoClient,
|
||||
g: { name: string; policies: string[]; memberEntityIds: string[] },
|
||||
): Promise<void> {
|
||||
await c.request('POST', `identity/group/name/${enc(g.name)}`, {
|
||||
type: 'internal',
|
||||
policies: g.policies,
|
||||
member_entity_ids: g.memberEntityIds,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteGroup(c: BaoClient, name: string): Promise<void> {
|
||||
await c.request('DELETE', `identity/group/name/${enc(name)}`)
|
||||
}
|
||||
Reference in new issue
Block a user