feat: Add pluggable secret providers (1Password, Infisical, HashiCorp Vault) (#1360)

* Add pluggable secret provider foundation

Introduces a provider-agnostic secret backend abstraction so stacks can
pull deploy-time secrets from 1Password, Infisical or HashiCorp Vault.

Providers implement the SecretProvider contract in secretproviders/shared.ts
and advertise which of the two resolution modes they support:

  - reference interpolation: a variable's value is an inline reference in
    the backend's own syntax (e.g. 1Password's op://vault/item/field)
  - bulk pull: every secret under a provider-defined selector, fetched as
    a flat key/value map

Adding a backend means dropping a file in secretproviders/ and registering
it in the index; no caller changes.

Four providers ship here: 1Password service account (references + bulk),
1Password Connect (references), Infisical (bulk) and HashiCorp Vault KV v2
(bulk). Provider config is persisted as an encrypted JSON blob in the new
secret_providers table, mirroring the existing notification_settings
type+config pattern, and stack_sources gains a nullable secret_provider_id.

Decrypted tokens stay in the provider modules for the duration of a call
and are never written to disk or returned to the client.

Co-authored-by: GooRoo <sergey.olendarenko@gmail.com>

* Add secret provider API routes and permissions

CRUD plus two test-connection endpoints under /api/secret-providers. The
unsaved variant validates a config posted from the provider form; the
stored variant tests a persisted provider by id.

Responses only ever carry provider summaries (id, type, name, timestamps).
The decrypted config never leaves the server.

Adds a "secrets" permission resource with view/create/edit/delete actions,
wired through the permission type, the role editor and the seeded roles,
and a secret_provider audit entity type so provider changes are logged.
Config rotations are audited as a boolean rather than a value.

* Resolve provider secrets at stack deploy time

Stacks can bind a secret provider; when one is bound, its secrets are
resolved and merged into the vars handed to docker compose.

Bulk pull is triggered by a selector variable: OP_ENVIRONMENT_ID, kept for
compatibility with the original 1Password integration, or the generic
DOCKHAND_SECRET_SELECTOR for any bulk-capable provider. The selector is
consumed by the provider and stripped before the vars reach the stack.
Values the bound provider recognises as inline references are resolved in
place, and references found only in the stack's .env file are promoted into
the secret set so they are never written back as literals.

Resolution is applied on deploy, on up, on recreate and on service update.
Explicitly configured secrets always win over bulk-pulled ones. A missing
or unsupported provider warns and leaves values untouched rather than
failing the deploy; a provider that errors mid-resolution fails it.

* Add secret provider settings UI and stack binding

A Secrets tab under settings lists configured providers and drives the
create/edit modal, which switches its fields on the selected provider type
and can test a connection before saving.

Both stack modals gain a provider selector so a stack can be bound to a
provider or left unbound. When a provider is bound, the bulk selector
variables are no longer reported as unused compose variables, since they
are consumed by the provider rather than the compose file.

---------

Co-authored-by: GooRoo <sergey.olendarenko@gmail.com>
Co-authored-by: Jarek Krochmalski <jotka@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-08-13 10:08:02 +02:00
1 parent 905c4a004d
commit 1ad36a83fb
39 files changed
+3094 -31

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
// Test preload, referenced by `[test] preload` in bunfig.toml.
//
// The current suite is pure unit tests that mock their own transport, so there
// is no global setup to do yet. This file exists so the path bunfig.toml points
// at resolves; without it `bun test` fails before collecting any test.
export {};
+318
View File
@@ -0,0 +1,318 @@
/**
* Unit tests for the 1Password Connect secret provider.
*
* The Connect backend is a self-hosted REST server we have no Business account
* for, so these tests mock undici's `request` and exercise the provider's HTTP
* choreography directly: health check, op:// reference resolution (including the
* optional section segment and the per-call vault/item cache), the not-found
* skip+warn path, auth-failure propagation, and the unsupported bulk pull.
*/
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
/**
* A queued response for the mocked undici `request`. `json`/`text` drive the
* body helpers the provider calls; `status` is the HTTP status code.
*/
interface MockResponse {
status: number;
json?: unknown;
text?: string;
}
/** Records one outgoing request so tests can assert URL/headers/count. */
interface RecordedCall {
url: string;
method?: string;
headers?: Record<string, string>;
}
let responses: MockResponse[];
let calls: RecordedCall[];
const requestMock = mock(async (url: unknown, opts?: { method?: string; headers?: Record<string, string> }) => {
calls.push({ url: String(url), method: opts?.method, headers: opts?.headers });
const next = responses.shift();
if (!next) {
throw new Error(`Unexpected request to ${String(url)}: no mock response queued`);
}
return {
statusCode: next.status,
body: {
async json() {
return next.json;
},
async text() {
return next.text ?? (next.json !== undefined ? JSON.stringify(next.json) : '');
}
}
};
});
mock.module('undici', () => ({ request: requestMock }));
// Imported after the mock is registered so connect.ts binds the mocked request.
const { connectProvider } = await import('../src/lib/server/secretproviders/connect');
const { UnsupportedOperationError } = await import('../src/lib/server/secretproviders/shared');
const CONFIG = { host: 'https://connect.example.com/', token: 'test-token' };
/** Queue the responses a single-field resolution walks through. */
function queueSuccessfulResolution() {
responses = [
{ status: 200, json: [{ id: 'vault-1', name: 'Prod' }] },
{ status: 200, json: [{ id: 'item-1', title: 'Database' }] },
{
status: 200,
json: {
id: 'item-1',
title: 'Database',
fields: [{ id: 'f1', label: 'password', value: 's3cret' }]
}
}
];
}
beforeEach(() => {
responses = [];
calls = [];
requestMock.mockClear();
});
afterEach(() => {
// Fail loudly if a test queued responses it never consumed.
expect(responses.length).toBe(0);
});
describe('connectProvider capabilities', () => {
test('advertises the expected type, label, and capability flags', () => {
expect(connectProvider.type).toBe('op-connect');
expect(connectProvider.label).toBe('1Password Connect');
expect(connectProvider.supportsReferences).toBe(true);
expect(connectProvider.supportsBulk).toBe(false);
});
test('isReference matches op:// strings only', () => {
expect(connectProvider.isReference('op://Prod/Database/password')).toBe(true);
expect(connectProvider.isReference(' op://Prod/Database/password')).toBe(true);
expect(connectProvider.isReference('plain-value')).toBe(false);
expect(connectProvider.isReference(42)).toBe(false);
expect(connectProvider.isReference(undefined)).toBe(false);
});
});
describe('testConnection', () => {
test('rejects empty host or token without hitting the network', async () => {
expect(await connectProvider.testConnection({ host: '', token: 't' })).toEqual({
ok: false,
error: 'Host is empty'
});
expect(await connectProvider.testConnection({ host: 'https://x', token: ' ' })).toEqual({
ok: false,
error: 'Token is empty'
});
expect(requestMock).not.toHaveBeenCalled();
});
test('ok on a 2xx /v1/vaults response and sends bearer auth', async () => {
responses = [{ status: 200, json: [{ id: 'v1', name: 'Vault' }] }];
const result = await connectProvider.testConnection(CONFIG);
expect(result).toEqual({ ok: true });
expect(calls[0].url).toBe('https://connect.example.com/v1/vaults');
expect(calls[0].headers?.Authorization).toBe('Bearer test-token');
expect(calls[0].headers?.Accept).toBe('application/json');
});
test('ok:false with a clear message on auth failure, without throwing', async () => {
responses = [{ status: 401, text: 'invalid token' }];
const result = await connectProvider.testConnection(CONFIG);
expect(result.ok).toBe(false);
expect(result.error).toBe('Invalid 1Password Connect token');
});
test('ok:false when the request throws (transport error)', async () => {
responses = [];
requestMock.mockImplementationOnce(async () => {
throw new Error('ECONNREFUSED');
});
const result = await connectProvider.testConnection(CONFIG);
expect(result.ok).toBe(false);
expect(result.error).toContain('ECONNREFUSED');
});
});
describe('resolveSecretReferences', () => {
test('returns an empty map for no refs without any request', async () => {
const result = await connectProvider.resolveSecretReferences(CONFIG, []);
expect(result.size).toBe(0);
expect(requestMock).not.toHaveBeenCalled();
});
test('resolves a op://vault/item/field reference', async () => {
queueSuccessfulResolution();
const ref = 'op://Prod/Database/password';
const result = await connectProvider.resolveSecretReferences(CONFIG, [ref]);
expect(result.get(ref)).toBe('s3cret');
expect(result.size).toBe(1);
// vaults, items, item detail — three GETs against the v1 base.
expect(calls.map((c) => c.url)).toEqual([
'https://connect.example.com/v1/vaults',
'https://connect.example.com/v1/vaults/vault-1/items',
'https://connect.example.com/v1/vaults/vault-1/items/item-1'
]);
});
test('resolves a reference with a section segment', async () => {
responses = [
{ status: 200, json: [{ id: 'vault-1', name: 'Prod' }] },
{ status: 200, json: [{ id: 'item-1', title: 'Database' }] },
{
status: 200,
json: {
id: 'item-1',
title: 'Database',
fields: [
{ id: 'f1', label: 'password', value: 'wrong', section: { id: 's-other' } },
{
id: 'f2',
label: 'password',
value: 'right',
section: { id: 's-1', label: 'replica' }
}
]
}
}
];
const ref = 'op://Prod/Database/replica/password';
const result = await connectProvider.resolveSecretReferences(CONFIG, [ref]);
expect(result.get(ref)).toBe('right');
});
test('matches vault by id and item by id', async () => {
responses = [
{ status: 200, json: [{ id: 'vault-1', name: 'Prod' }] },
{ status: 200, json: [{ id: 'item-1', title: 'Database' }] },
{
status: 200,
json: { id: 'item-1', title: 'Database', fields: [{ id: 'notes', value: 'byid' }] }
}
];
const ref = 'op://vault-1/item-1/notes';
const result = await connectProvider.resolveSecretReferences(CONFIG, [ref]);
expect(result.get(ref)).toBe('byid');
});
test('caches vault and item lists across refs in one call', async () => {
responses = [
{ status: 200, json: [{ id: 'vault-1', name: 'Prod' }] },
{ status: 200, json: [{ id: 'item-1', title: 'Database' }] },
{
status: 200,
json: { id: 'item-1', title: 'Database', fields: [{ label: 'password', value: 'p1' }] }
},
// Second ref hits the same vault+item: only the item-detail GET repeats.
{
status: 200,
json: { id: 'item-1', title: 'Database', fields: [{ label: 'username', value: 'u1' }] }
}
];
const refs = ['op://Prod/Database/password', 'op://Prod/Database/username'];
const result = await connectProvider.resolveSecretReferences(CONFIG, refs);
expect(result.get(refs[0])).toBe('p1');
expect(result.get(refs[1])).toBe('u1');
// 1 vaults + 1 items + 2 item-details = 4 requests (lists were cached).
expect(calls.length).toBe(4);
expect(calls.filter((c) => c.url.endsWith('/v1/vaults')).length).toBe(1);
expect(calls.filter((c) => c.url.endsWith('/items')).length).toBe(1);
});
test('skips and warns a ref whose field is missing, keeping the others', async () => {
const warn = mock(() => {});
const original = console.warn;
console.warn = warn;
try {
responses = [
{ status: 200, json: [{ id: 'vault-1', name: 'Prod' }] },
{ status: 200, json: [{ id: 'item-1', title: 'Database' }] },
// missing field -> skip
{
status: 200,
json: { id: 'item-1', title: 'Database', fields: [{ label: 'password', value: 'p1' }] }
},
// second ref resolves fine
{
status: 200,
json: { id: 'item-1', title: 'Database', fields: [{ label: 'password', value: 'p1' }] }
}
];
const missing = 'op://Prod/Database/nope';
const present = 'op://Prod/Database/password';
const result = await connectProvider.resolveSecretReferences(CONFIG, [missing, present]);
expect(result.has(missing)).toBe(false);
expect(result.get(present)).toBe('p1');
expect(warn).toHaveBeenCalled();
} finally {
console.warn = original;
}
});
test('skips a ref whose vault 404s on the specific lookup', async () => {
const warn = mock(() => {});
const original = console.warn;
console.warn = warn;
try {
// Empty vault list -> vault not found -> skip+warn, no throw.
responses = [{ status: 200, json: [] }];
const ref = 'op://Ghost/Database/password';
const result = await connectProvider.resolveSecretReferences(CONFIG, [ref]);
expect(result.size).toBe(0);
expect(warn).toHaveBeenCalled();
} finally {
console.warn = original;
}
});
test('skips a malformed op:// reference', async () => {
const warn = mock(() => {});
const original = console.warn;
console.warn = warn;
try {
responses = [];
const bad = 'op://Prod/Database'; // too few segments
const result = await connectProvider.resolveSecretReferences(CONFIG, [bad]);
expect(result.size).toBe(0);
expect(requestMock).not.toHaveBeenCalled();
expect(warn).toHaveBeenCalled();
} finally {
console.warn = original;
}
});
test('throws on an auth failure (401) instead of skipping', async () => {
responses = [{ status: 401, text: 'unauthorized' }];
await expect(
connectProvider.resolveSecretReferences(CONFIG, ['op://Prod/Database/password'])
).rejects.toThrow(/401/);
});
test('throws on a server error (500)', async () => {
responses = [{ status: 500, text: 'boom' }];
await expect(
connectProvider.resolveSecretReferences(CONFIG, ['op://Prod/Database/password'])
).rejects.toThrow(/500/);
});
});
describe('resolveBulk', () => {
test('throws UnsupportedOperationError', async () => {
await expect(connectProvider.resolveBulk(CONFIG, 'any-selector')).rejects.toBeInstanceOf(
UnsupportedOperationError
);
});
test('the error message points at using a service account', async () => {
await expect(connectProvider.resolveBulk(CONFIG, 'any-selector')).rejects.toThrow(
/service account/
);
});
});
+180
View File
@@ -0,0 +1,180 @@
/**
* Unit tests for the Infisical secret provider.
*
* Infisical is open-source and self-hostable, so it is integration-testable
* against a real instance later. Here we exercise the provider in isolation by
* mocking undici's `request`, covering:
* - a successful bulk pull mapping { secretKey, secretValue } → Record
* - the selector / config.path / '/' precedence for the queried path
* - an auth failure (HTTP 401) surfacing as a thrown error
* - testConnection returning ok on 2xx and a helpful error otherwise
* - resolveSecretReferences throwing UnsupportedOperationError (bulk-only)
*/
import { describe, it, expect, mock, beforeEach } from 'bun:test';
/**
* A single mock for undici's `request`. Registered before the provider is
* imported so the provider's `import { request } from 'undici'` binds to it.
* Each test sets its behaviour via `requestMock.mockImplementation(...)`.
*/
const requestMock = mock();
mock.module('undici', () => ({ request: requestMock }));
const { infisicalProvider } = await import('../src/lib/server/secretproviders/infisical.ts');
const { UnsupportedOperationError } = await import('../src/lib/server/secretproviders/shared.ts');
/** Builds an undici-style response with json()/text() bodies. */
function response(statusCode: number, json: unknown, text = '') {
return {
statusCode,
body: {
json: async () => json,
text: async () => text
}
};
}
const baseConfig = {
host: 'https://app.infisical.com',
token: 'st.token.value',
projectId: 'proj-123',
environment: 'prod',
path: '/'
};
beforeEach(() => {
requestMock.mockReset();
});
describe('infisicalProvider capabilities', () => {
it('advertises bulk-only, no references', () => {
expect(infisicalProvider.type).toBe('infisical');
expect(infisicalProvider.label).toBe('Infisical');
expect(infisicalProvider.supportsBulk).toBe(true);
expect(infisicalProvider.supportsReferences).toBe(false);
});
it('isReference is always false', () => {
expect(infisicalProvider.isReference('op://vault/item/field')).toBe(false);
expect(infisicalProvider.isReference('anything')).toBe(false);
expect(infisicalProvider.isReference(42)).toBe(false);
});
});
describe('infisicalProvider.resolveBulk', () => {
it('maps secretKey/secretValue pairs into a flat record', async () => {
requestMock.mockImplementation(async () =>
response(200, {
secrets: [
{ secretKey: 'DB_PASSWORD', secretValue: 's3cr3t' },
{ secretKey: 'API_KEY', secretValue: 'abc123' }
]
})
);
const result = await infisicalProvider.resolveBulk(baseConfig, '');
expect(result).toEqual({ DB_PASSWORD: 's3cr3t', API_KEY: 'abc123' });
// Verify the request shape: raw secrets endpoint, bearer auth, and the
// workspace/environment/path query.
const [url, opts] = requestMock.mock.calls[0];
expect(url).toContain('https://app.infisical.com/api/v3/secrets/raw');
expect(url).toContain('workspaceId=proj-123');
expect(url).toContain('environment=prod');
expect(url).toContain('secretPath=%2F');
expect(opts.method).toBe('GET');
expect(opts.headers.authorization).toBe('Bearer st.token.value');
});
it('tolerates an absent secrets array', async () => {
requestMock.mockImplementation(async () => response(200, {}));
const result = await infisicalProvider.resolveBulk(baseConfig, '');
expect(result).toEqual({});
});
it('prefers the selector over config.path for secretPath', async () => {
requestMock.mockImplementation(async () => response(200, { secrets: [] }));
await infisicalProvider.resolveBulk({ ...baseConfig, path: '/config' }, '/services/web');
const [url] = requestMock.mock.calls[0];
expect(url).toContain('secretPath=%2Fservices%2Fweb');
});
it('falls back to config.path when the selector is empty', async () => {
requestMock.mockImplementation(async () => response(200, { secrets: [] }));
await infisicalProvider.resolveBulk({ ...baseConfig, path: '/config' }, '');
const [url] = requestMock.mock.calls[0];
expect(url).toContain('secretPath=%2Fconfig');
});
it('throws on an auth failure (HTTP 401)', async () => {
requestMock.mockImplementation(async () =>
response(401, {}, 'Unauthorized: invalid token')
);
await expect(infisicalProvider.resolveBulk(baseConfig, '')).rejects.toThrow(/401/);
});
it('throws a clear error when the environment is missing', async () => {
await expect(
infisicalProvider.resolveBulk({ ...baseConfig, environment: undefined }, '')
).rejects.toThrow(/Environment is required/);
// No HTTP call should have been attempted.
expect(requestMock).not.toHaveBeenCalled();
});
});
describe('infisicalProvider.resolveSecretReferences', () => {
it('throws UnsupportedOperationError (bulk-only backend)', async () => {
await expect(
infisicalProvider.resolveSecretReferences(baseConfig, ['ref'])
).rejects.toBeInstanceOf(UnsupportedOperationError);
await expect(
infisicalProvider.resolveSecretReferences(baseConfig, ['ref'])
).rejects.toThrow(/does not support inline references/);
expect(requestMock).not.toHaveBeenCalled();
});
});
describe('infisicalProvider.testConnection', () => {
it('returns ok on a 2xx response', async () => {
requestMock.mockImplementation(async () => response(200, { secrets: [] }));
const result = await infisicalProvider.testConnection(baseConfig);
expect(result.ok).toBe(true);
const [url] = requestMock.mock.calls[0];
expect(url).toContain('secretPath=%2F');
});
it('returns ok:false with the status on an auth failure', async () => {
requestMock.mockImplementation(async () => response(401, {}, 'Unauthorized'));
const result = await infisicalProvider.testConnection(baseConfig);
expect(result.ok).toBe(false);
expect(result.error).toContain('401');
});
it('does not throw when the transport fails, returns the message', async () => {
requestMock.mockImplementation(async () => {
throw new Error('ECONNREFUSED');
});
const result = await infisicalProvider.testConnection(baseConfig);
expect(result.ok).toBe(false);
expect(result.error).toContain('ECONNREFUSED');
});
it('returns a helpful error when projectId is empty (no HTTP call)', async () => {
const result = await infisicalProvider.testConnection({ ...baseConfig, projectId: '' });
expect(result.ok).toBe(false);
expect(result.error).toContain('Project ID');
expect(requestMock).not.toHaveBeenCalled();
});
it('returns a helpful error when environment is empty (no HTTP call)', async () => {
const result = await infisicalProvider.testConnection({
...baseConfig,
environment: undefined
});
expect(result.ok).toBe(false);
expect(result.error).toContain('Environment');
expect(requestMock).not.toHaveBeenCalled();
});
});
+199
View File
@@ -0,0 +1,199 @@
/**
* Unit tests for the HashiCorp Vault (KV v2) secret provider.
*
* undici's `request` is mocked so these run without a real Vault server. They
* cover: a successful KV v2 read (mapping `data.data`, coercing non-strings),
* the `X-Vault-Namespace` header being sent only when configured, the mount /
* URL shape, auth failure (403) on both bulk read and testConnection, a missing
* path (404) surfacing as an error, and the unsupported inline-reference mode.
*
* Vault OSS is separately integration-testable against `vault server -dev`.
*/
// `svelte-check` runs over `tests/**` (per .svelte-kit/tsconfig.json) but this
// repo ships no bun type declarations (`@types/bun`), so it cannot resolve the
// `bun:test` built-in. Harmless at runtime — bun provides the module. Drop this
// suppression once `@types/bun` is a dev dependency (see coordinator note).
// @ts-expect-error -- bun:test is a runtime built-in with no types installed
import { test, expect, describe, mock, beforeEach } from 'bun:test';
/** Per-test undici stub; each test overrides this before exercising the provider. */
let requestImpl: (url: string, opts: { method: string; headers: Record<string, string> }) => unknown;
mock.module('undici', () => ({
request: (url: string, opts: { method: string; headers: Record<string, string> }) =>
requestImpl(url, opts)
}));
const { vaultProvider } = await import('../src/lib/server/secretproviders/vault.ts');
const { UnsupportedOperationError } = await import('../src/lib/server/secretproviders/shared.ts');
/** Fake undici response with a body exposing json()/text()/dump(). */
function makeResponse(
statusCode: number,
opts: { json?: unknown; text?: string } = {}
): { statusCode: number; headers: Record<string, string>; body: unknown } {
return {
statusCode,
headers: {},
body: {
async json() {
if ('json' in opts) return opts.json;
return JSON.parse(opts.text ?? 'null');
},
async text() {
if (opts.text !== undefined) return opts.text;
return opts.json !== undefined ? JSON.stringify(opts.json) : '';
},
async dump() {
/* drain — no-op in the mock */
}
}
};
}
const baseConfig = { address: 'https://vault.example.com', token: 's.rootToken' };
beforeEach(() => {
requestImpl = () => {
throw new Error('unexpected undici.request call');
};
});
describe('vaultProvider capabilities', () => {
test('advertises bulk-only, no references', () => {
expect(vaultProvider.type).toBe('vault');
expect(vaultProvider.label).toBe('HashiCorp Vault');
expect(vaultProvider.supportsBulk).toBe(true);
expect(vaultProvider.supportsReferences).toBe(false);
});
test('isReference returns false for anything', () => {
expect(vaultProvider.isReference('vault://secret/foo#bar')).toBe(false);
expect(vaultProvider.isReference('op://a/b/c')).toBe(false);
expect(vaultProvider.isReference(null)).toBe(false);
});
});
describe('resolveBulk', () => {
test('reads KV v2 data.data and coerces non-string values', async () => {
let capturedUrl = '';
let capturedHeaders: Record<string, string> = {};
requestImpl = (url, opts) => {
capturedUrl = url;
capturedHeaders = opts.headers;
return makeResponse(200, {
json: {
data: {
data: { DB_PASSWORD: 'hunter2', PORT: 5432, ENABLED: true },
metadata: { version: 3 }
}
}
});
};
const out = await vaultProvider.resolveBulk(baseConfig, 'myapp/config');
expect(out).toEqual({ DB_PASSWORD: 'hunter2', PORT: '5432', ENABLED: 'true' });
// mount defaults to 'secret'; address keeps no trailing slash.
expect(capturedUrl).toBe('https://vault.example.com/v1/secret/data/myapp/config');
expect(capturedHeaders['X-Vault-Token']).toBe('s.rootToken');
expect(capturedHeaders['X-Vault-Namespace']).toBeUndefined();
});
test('honours a custom mount and strips trailing slash / leading path slash', async () => {
let capturedUrl = '';
requestImpl = (url) => {
capturedUrl = url;
return makeResponse(200, { json: { data: { data: { K: 'v' } } } });
};
await vaultProvider.resolveBulk(
{ address: 'https://vault.example.com/', token: 't', mount: 'kv' },
'/team/app'
);
expect(capturedUrl).toBe('https://vault.example.com/v1/kv/data/team/app');
});
test('sends X-Vault-Namespace when configured', async () => {
let capturedHeaders: Record<string, string> = {};
requestImpl = (_url, opts) => {
capturedHeaders = opts.headers;
return makeResponse(200, { json: { data: { data: { K: 'v' } } } });
};
await vaultProvider.resolveBulk({ ...baseConfig, namespace: 'admin/team' }, 'app');
expect(capturedHeaders['X-Vault-Namespace']).toBe('admin/team');
expect(capturedHeaders['X-Vault-Token']).toBe('s.rootToken');
});
test('returns empty map when data.data is absent', async () => {
requestImpl = () => makeResponse(200, { json: { data: { metadata: {} } } });
const out = await vaultProvider.resolveBulk(baseConfig, 'empty');
expect(out).toEqual({});
});
test('throws a clear "path not found" on 404', async () => {
requestImpl = () => makeResponse(404, { json: { errors: [] } });
await expect(vaultProvider.resolveBulk(baseConfig, 'missing/path')).rejects.toThrow(
/path not found: secret\/missing\/path/
);
});
test('throws on auth failure (403), including Vault error detail', async () => {
requestImpl = () => makeResponse(403, { json: { errors: ['permission denied'] } });
await expect(vaultProvider.resolveBulk(baseConfig, 'app')).rejects.toThrow(
/status 403.*permission denied/
);
});
});
describe('testConnection', () => {
test('ok:true on 2xx from lookup-self', async () => {
let capturedUrl = '';
requestImpl = (url) => {
capturedUrl = url;
return makeResponse(200, { json: { data: { display_name: 'token' } } });
};
const res = await vaultProvider.testConnection(baseConfig);
expect(res.ok).toBe(true);
expect(capturedUrl).toBe('https://vault.example.com/v1/auth/token/lookup-self');
});
test('ok:false (no throw) on auth failure (403)', async () => {
requestImpl = () => makeResponse(403, { json: { errors: ['permission denied'] } });
const res = await vaultProvider.testConnection(baseConfig);
expect(res.ok).toBe(false);
expect(res.error).toContain('403');
expect(res.error).toContain('permission denied');
});
test('ok:false on transport error without throwing', async () => {
requestImpl = () => {
throw new Error('ECONNREFUSED');
};
const res = await vaultProvider.testConnection(baseConfig);
expect(res.ok).toBe(false);
expect(res.error).toContain('ECONNREFUSED');
});
test('rejects empty address / token before any request', async () => {
expect((await vaultProvider.testConnection({ address: '', token: 't' })).ok).toBe(false);
expect((await vaultProvider.testConnection({ address: 'x', token: '' })).ok).toBe(false);
});
});
describe('resolveSecretReferences', () => {
test('throws UnsupportedOperationError (bulk-only backend)', async () => {
await expect(vaultProvider.resolveSecretReferences(baseConfig, ['anything'])).rejects.toThrow(
UnsupportedOperationError
);
await expect(vaultProvider.resolveSecretReferences(baseConfig, [])).rejects.toThrow(
/does not support inline references/
);
});
});