From 58b6230f9b9b562d5bf4523dba1b44dac18c02d3 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 9 Nov 2026 14:27:31 +0000 Subject: [PATCH] Docs: admin policy and tests for managing secrets engine mounts Adds sys/mounts/* and secret/config to the least-privilege policy and runs create, tune and delete under only that policy. Mounting, tuning and deleting need no sudo; KV settings are only covered on secret/, so settings on another KV mount fail after the mount is created, which the README and the UI's partial-create message now explain. Integration tests also confirm that deleting a mount revokes the leases it issued and that a blank TTL resets to the system default. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 16 +++- docs/envbao-admin.hcl | 6 ++ src/api/adminPolicy.integration.test.ts | 45 +++++++++++ src/api/mounts.integration.test.ts | 99 +++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 src/api/mounts.integration.test.ts diff --git a/README.md b/README.md index 8d8ce07..12952bc 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The UI talks directly to the OpenBao HTTP API from the browser. There is no back - **Connect** with a token or a userpass login. Refuses sealed or uninitialised servers, and explains CORS and connection problems. - **Identities**: create, edit and delete userpass users, identity entities (with userpass aliases) and groups; create, list and revoke tokens. A new token's value is shown once. - **Secrets**: browse KV v2 mounts and paths (names only; KV secret values are never read) and see which identities can reach a secret, through which policy, with which capabilities. +- **Secrets engines**: create (KV v2, database, AWS, SSH), change the settings of, and delete secrets engine mounts, including KV v2 settings such as max versions. Deleting asks you to type the mount path, and says what will be lost and which policies still refer to it. - **Dynamic secrets**: for database (Postgres, ...), AWS and SSH engines, and a raw-JSON editor for any other engine type: create, edit and delete roles; configure the engine (database connections with rotate and reset, AWS root and lease, the SSH signing CA); generate credentials, sign SSH keys or issue OTPs; and browse, renew and revoke leases. Issued credentials are shown once and are never stored or cached by the UI. Passwords and private keys you type into config forms are write-only: they are never shown again. - **Access**: from an identity's **Access** button, grant it capabilities on a secret path (globs allowed) or access to a dynamic-engine role (generate credentials, manage the role), or revoke them, without writing policy HCL. A role's **Access** button shows who can already use it. - **Graph**: Identity → Policy → Path graph, filterable by identity and path. Click a policy→path edge to revoke that grant. @@ -92,7 +93,7 @@ bao auth enable userpass bao secrets enable -path=secret -version=2 kv # skip if already mounted ``` -Dynamic engines are optional and appear under **Secrets** once mounted: +Dynamic engines are optional and appear under **Secrets** once mounted. You can mount them from the UI (**Secrets → New secrets engine**) or with the CLI: ```sh bao secrets enable database @@ -135,6 +136,12 @@ path "auth/token/revoke-accessor" { capabilities = ["update"] } # Discovery: mounted auth methods and secrets engines, and KV v2 metadata for the secret browser path "sys/auth" { capabilities = ["read"] } path "sys/mounts" { capabilities = ["read"] } + +# Secrets engine mounts: create, change (tune) and delete. Deleting a mount destroys everything in it +# and revokes its leases, so this is a powerful rule; leave it out if mounts are managed elsewhere. +path "sys/mounts/*" { capabilities = ["create", "read", "update", "delete"] } +# KV v2 settings (max versions, check-and-set) of the KV mount +path "secret/config" { capabilities = ["read", "update"] } path "secret/metadata/*" { capabilities = ["list"] } # Dynamic secrets engines: roles, config, credentials and leases. The paths use the default mount @@ -182,7 +189,9 @@ Notes: - Listing token accessors needs `sudo` in OpenBao, so the Tokens tab needs it on `auth/token/accessors`. - Creating **orphan** tokens (the "Orphan" checkbox) needs `sudo` on `auth/token/create`. It is deliberately not granted above; add `"sudo"` there if you want it. -- If your KV v2 mount is not `secret/`, change `secret/metadata/*`. The dynamic-engine rules name the default mounts (`database/`, `aws/`, `ssh/`), so change those too, or repeat a block for each mount. They deliberately avoid a `+/roles/*` wildcard, which would also match system paths such as `sys/config/cors`. +- The `sys/mounts/*` rule lets this account create, change and **delete any secrets engine**, which destroys its data and revokes its leases. Remove it (and the New / Settings / Delete buttons will report permission denied) if mounts are managed elsewhere. Mounting, tuning and deleting do not need `sudo`. +- KV v2 settings (max versions, check-and-set) are stored by the engine at `/config`, so the policy only covers them on `secret/`. Creating another KV store with settings mounts it and then fails to save them; EnvBao tells you the store exists, and you can add `/config` to the policy and use **Settings**. +- If your KV v2 mount is not `secret/`, change `secret/metadata/*` and `secret/config`. The dynamic-engine rules name the default mounts (`database/`, `aws/`, `ssh/`), so change those too, or repeat a block for each mount. They deliberately avoid a `+/roles/*` wildcard, which would also match system paths such as `sys/config/cors`. - Listing leases and revoking by prefix need `sudo` in OpenBao (looking up, renewing and revoking a single lease do not). Without it the Leases tab shows a permission error. - The `creds`, `sign` and `static-creds` lines let this account issue live database logins, AWS keys and SSH certificates. Remove them if the account should manage roles without being able to use them. - The account can create policies and attach them to identities, which is effectively the power to grant access to anything. Treat it as an administrator. @@ -252,7 +261,7 @@ BAO_ADDR=http://127.0.0.1:8200 BAO_TOKEN=root npm run test:integration # again The integration tests need a dev server with userpass available (they enable it if missing) and a KV v2 mount at `secret/` (the dev-mode default). They create and delete their own uniquely named objects, but run them against a throwaway instance, not a real one. -The dynamic-engine tests mount their own `ssh` engine (built into OpenBao). The database tests are skipped unless `BAO_PG_URL` points at a Postgres whose user has `CREATEROLE`, as seen from OpenBao, for example `BAO_PG_URL='postgresql://postgres:secret@postgres:5432/app?sslmode=disable'`; CI starts one as a service, and `docker compose up -d postgres` gives you one on `127.0.0.1:5433`. The admin-policy tests use the default `ssh/` and `database/` mounts and skip those checks when the mount already exists, so they never overwrite a real CA or connection. The AWS engine tests skip when the server does not have it. +The dynamic-engine and mount-management tests mount and unmount their own uniquely named engines (`ssh` and `kv` are built into OpenBao). The database tests are skipped unless `BAO_PG_URL` points at a Postgres whose user has `CREATEROLE`, as seen from OpenBao, for example `BAO_PG_URL='postgresql://postgres:secret@postgres:5432/app?sslmode=disable'`; CI starts one as a service, and `docker compose up -d postgres` gives you one on `127.0.0.1:5433`. The admin-policy tests use the default `ssh/` and `database/` mounts and skip those checks when the mount already exists, so they never overwrite a real CA or connection. The AWS engine tests skip when the server does not have it. Some test files enable `userpass` at the same time, which can fail with `path is already in use at userpass/`. If that happens, add `-- --no-file-parallelism`. @@ -262,6 +271,7 @@ CI runs on Gitea Actions (`.gitea/workflows/ci.yml`): lint, test and build, plus - **Browser-direct**: CORS must be enabled on OpenBao, and the token lives in the browser's memory (never written to storage). There is no server-side audit trail beyond OpenBao's own audit log. - **Scope**: KV v2 and the database, AWS and SSH dynamic engines (other engine types get a raw JSON role editor only, with no credential generation), and only userpass auth, tokens and identity entities/groups are managed. KV v1 is not shown. +- **Mount management**: a mount's path and type can't be changed, and there is no rename (OpenBao's remount revokes leases and would leave grants pointing at the old path). Only KV v2, database, AWS and SSH can be created; KV v1 is not upgraded. Deleting a mount leaves policies that mention it in place. - **Credentials**: dynamic credentials appear once, in the browser, when generated; copy them then. Avoid generating them on a shared screen. Credentials without a lease (database static roles) cannot be revoked from the Leases tab. - **Managed policies**: access is granted by writing one policy per user, entity or group, named `envbao--` (for example `envbao-user-alice`). If a managed policy is edited outside EnvBao (for example to add parameter constraints) it becomes read-only in the UI rather than being overwritten. All other policies are shown read-only. Policy names are lower-cased by OpenBao, so identities whose names differ only by case or punctuation would collide; EnvBao detects this and refuses to overwrite. - **Tokens**: cannot be granted access after creation (OpenBao fixes a token's policies when it is created), so choose their policies when creating them. Tokens created by a token die with it unless created as orphans. diff --git a/docs/envbao-admin.hcl b/docs/envbao-admin.hcl index d255499..ea7c85c 100644 --- a/docs/envbao-admin.hcl +++ b/docs/envbao-admin.hcl @@ -26,6 +26,12 @@ path "auth/token/revoke-accessor" { capabilities = ["update"] } # Discovery: mounted auth methods and secrets engines, and KV v2 metadata for the secret browser path "sys/auth" { capabilities = ["read"] } path "sys/mounts" { capabilities = ["read"] } + +# Secrets engine mounts: create, change (tune) and delete. Deleting a mount destroys everything in it +# and revokes its leases, so this is a powerful rule; leave it out if mounts are managed elsewhere. +path "sys/mounts/*" { capabilities = ["create", "read", "update", "delete"] } +# KV v2 settings (max versions, check-and-set) of the KV mount +path "secret/config" { capabilities = ["read", "update"] } path "secret/metadata/*" { capabilities = ["list"] } # Dynamic secrets engines: roles, config, credentials and leases. The paths use the default mount diff --git a/src/api/adminPolicy.integration.test.ts b/src/api/adminPolicy.integration.test.ts index 1bef3ea..855241e 100644 --- a/src/api/adminPolicy.integration.test.ts +++ b/src/api/adminPolicy.integration.test.ts @@ -11,6 +11,8 @@ import { generateCreds } from './engines/creds' import { listLeases, lookupLease, renewLease, revokeLease, revokePrefix } from './engines/leases' import { ENGINES } from './engines/registry' import { pgTarget } from '../test/postgres' +import { createMount, deleteMount, mountImpact, PartialMountError, readKvConfig, tuneMount } from './mounts' +import { MOUNT_TYPES } from './engines/registry' import { deleteRole, emptyDraft, listRoles, payloadFromDraft, readRole, writeRole } from './engines/roles' // Everything the UI does, run with ONLY docs/envbao-admin.hcl (plus the default policy), so the @@ -203,3 +205,46 @@ describe('the admin policy covers the database engine (at the default mount)', ( await deleteConfig(admin, 'database/', conn, 'app') }) }) + +describe('the admin policy covers managing secrets engine mounts', () => { + const kv = MOUNT_TYPES.find((t) => t.type === 'kv')! + const sshType = MOUNT_TYPES.find((t) => t.type === 'ssh')! + const made: string[] = [] + afterAll(async () => { + for (const p of made) await root.request('DELETE', `sys/mounts/${p}`).catch(() => {}) + }) + const find = async (path: string) => (await secrets.listMounts(admin)).find((m) => m.path === path) + + test('create, tune, inspect and delete a dynamic engine', async () => { + const path = `it-pol-ssh-${sfx}` + made.push(path) + await createMount(admin, path, sshType, { description: 'hosts', default_lease_ttl: '1h' }) + expect(await find(`${path}/`)).toMatchObject({ type: 'ssh', description: 'hosts', defaultLeaseTtl: 3600 }) + await tuneMount(admin, path, sshType, { description: 'changed' }) + expect(await find(`${path}/`)).toMatchObject({ description: 'changed', defaultLeaseTtl: 0 }) + expect((await mountImpact(admin, `${path}/`, 'ssh')).kind).toBe('dynamic') + await deleteMount(admin, path) + expect(await find(`${path}/`)).toBeUndefined() + }) + + test('create and delete a KV v2 store without settings', async () => { + const path = `it-pol-kv-${sfx}` + made.push(path) + await createMount(admin, path, kv, { description: 'plain' }) + expect(await find(`${path}/`)).toMatchObject({ type: 'kv', version: '2' }) + await deleteMount(admin, path) + }) + + test('KV settings work on secret/ but not on other mounts, which the README says', async () => { + // secret/config is the one KV config path the policy names + const before = await readKvConfig(admin, 'secret/') + await tuneMount(admin, 'secret/', kv, { description: 'key/value secret storage', max_versions: String(before.max_versions), cas_required: before.cas_required === true }) + + const path = `it-pol-kvset-${sfx}` + made.push(path) + const err = await createMount(admin, path, kv, { max_versions: '3' }).catch((e: unknown) => e) + expect(err).toBeInstanceOf(PartialMountError) + expect((err as Error).message).toMatch(/was mounted, but saving its KV settings failed: .*permission denied/s) + expect(await find(`${path}/`)).toBeDefined() + }) +}) diff --git a/src/api/mounts.integration.test.ts b/src/api/mounts.integration.test.ts new file mode 100644 index 0000000..d1088a1 --- /dev/null +++ b/src/api/mounts.integration.test.ts @@ -0,0 +1,99 @@ +import { afterAll, describe, expect, test } from 'vitest' +import { BaoClient } from './bao' +import { generateCreds } from './engines/creds' +import { lookupLease } from './engines/leases' +import { MOUNT_TYPES } from './engines/registry' +import { emptyDraft, payloadFromDraft, writeRole } from './engines/roles' +import { createMount, deleteMount, mountImpact, PartialMountError, readKvConfig, tuneMount } from './mounts' +import { listMounts } from './secrets' +import { ENGINES } from './engines/registry' + +// Mounts and unmounts real engines, so run against a throwaway server (see README). +const client = new BaoClient(process.env.BAO_ADDR ?? 'http://127.0.0.1:8200', process.env.BAO_TOKEN ?? 'root') +const sfx = Math.random().toString(36).slice(2, 8) +const kv = MOUNT_TYPES.find((t) => t.type === 'kv')! +const ssh = MOUNT_TYPES.find((t) => t.type === 'ssh')! +const made: string[] = [] +const find = async (path: string) => (await listMounts(client)).find((m) => m.path === path) + +afterAll(async () => { + for (const p of made) await deleteMount(client, p).catch(() => {}) +}) + +describe('mount management', () => { + test('creates a KV v2 store with settings, and reads them back', async () => { + const path = `it-kv-${sfx}` + made.push(path) + await createMount(client, path, kv, { description: 'scratch store', max_versions: '4', cas_required: true, delete_version_after: '48h' }) + + const m = await find(`${path}/`) + expect(m).toMatchObject({ type: 'kv', version: '2', description: 'scratch store' }) + expect(await readKvConfig(client, path)).toMatchObject({ max_versions: 4, cas_required: true, delete_version_after: '48h0m0s' }) + }) + + test('tunes a mount: description, TTLs and KV settings', async () => { + const path = `it-kv-${sfx}` + await tuneMount(client, path, kv, { description: 'renamed', max_versions: '9', cas_required: false }) + expect(await find(`${path}/`)).toMatchObject({ description: 'renamed' }) + expect(await readKvConfig(client, path)).toMatchObject({ max_versions: 9, cas_required: false }) + }) + + test('lease TTLs are stored as set, and a blank TTL resets to the system default (0)', async () => { + const path = `it-ssh-${sfx}` + made.push(path) + await createMount(client, path, ssh, { description: 'hosts', default_lease_ttl: '1h', max_lease_ttl: '8h' }) + expect(await find(`${path}/`)).toMatchObject({ type: 'ssh', description: 'hosts', defaultLeaseTtl: 3600, maxLeaseTtl: 28800 }) + + // saving the form back unchanged must not pin anything else + await tuneMount(client, path, ssh, { description: 'hosts', default_lease_ttl: '3600s', max_lease_ttl: '28800s' }) + expect(await find(`${path}/`)).toMatchObject({ defaultLeaseTtl: 3600, maxLeaseTtl: 28800 }) + + await tuneMount(client, path, ssh, { description: 'hosts' }) + expect(await find(`${path}/`)).toMatchObject({ defaultLeaseTtl: 0, maxLeaseTtl: 0 }) + }) + + test('refuses a path that is already mounted, and creates nothing else', async () => { + await expect(createMount(client, `it-ssh-${sfx}`, ssh, {})).rejects.toThrow(/already in use|existing mount/i) + }) + + test('KV settings that fail after the mount exists are reported as a partial create', async () => { + // an unparseable duration is rejected by the KV engine itself, after sys/mounts has succeeded + const path = `it-partial-${sfx}` + made.push(path) + const err = await createMount(client, path, kv, { delete_version_after: 'banana' }).catch((e: unknown) => e) + expect(err).toBeInstanceOf(PartialMountError) + expect(await find(`${path}/`)).toBeDefined() + }) + + test('impact counts what is in a mount without reading values', async () => { + const path = `it-kv-${sfx}` + await client.request('POST', `${path}/data/a/b`, { data: { k: 'v' } }) + await client.request('POST', `${path}/data/top`, { data: { k: 'v' } }) + expect(await mountImpact(client, `${path}/`, 'kv')).toEqual({ kind: 'kv', topLevelEntries: 2, unknown: [] }) + }) + + test('deleting a mount removes it and revokes the leases it issued', async () => { + const path = `it-lease-${sfx}` + made.push(path) + await createMount(client, path, ssh, {}) + const role = ENGINES.ssh.roleKinds[0] + const draft = emptyDraft(role) + draft.values.key_type = 'otp' + draft.values.default_user = 'alice' + draft.values.cidr_list = '10.0.0.0/8' + await writeRole(client, `${path}/`, role, 'otp', payloadFromDraft(role, draft)) + const creds = await generateCreds(client, `${path}/`, role.creds[1], 'otp', { ip: '10.1.1.1' }) + expect(await mountImpact(client, `${path}/`, 'ssh')).toEqual({ kind: 'dynamic', roles: 1, hasLeases: true, unknown: [] }) + expect((await lookupLease(client, creds.leaseId)).id).toBe(creds.leaseId) + + await deleteMount(client, path) + expect(await find(`${path}/`)).toBeUndefined() + await expect(lookupLease(client, creds.leaseId)).rejects.toThrow() + }) + + test('deleting removes the KV store and its data', async () => { + const path = `it-kv-${sfx}` + await deleteMount(client, path) + expect(await find(`${path}/`)).toBeUndefined() + }) +})