mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
feat(server): api/admin/storage/test — per-target ephemeral probes, mirror targets probed individually
This commit is contained in:
@@ -5,7 +5,7 @@ import type { User } from '../../../../src/types';
|
||||
import { StorageAdminController } from '../../../../src/nest/storage/storage-admin.controller';
|
||||
import type { StorageAdminService } from '../../../../src/nest/storage/storage-admin.service';
|
||||
import type { AuditService } from '../../../../src/nest/audit/audit.service';
|
||||
import type { StorageConfigDto } from '../../../../src/nest/storage/storage-admin.dto';
|
||||
import type { StorageConfigDto, StorageTestRequestDto } from '../../../../src/nest/storage/storage-admin.dto';
|
||||
import { StorageModule } from '../../../../src/nest/storage/storage.module';
|
||||
import { StorageBackendError } from '../../../../src/nest/storage/storage.types';
|
||||
import { expectRegisteredController } from '../../../helpers/module-providers';
|
||||
@@ -87,4 +87,28 @@ describe('StorageAdminController', () => {
|
||||
it('STORCTL-004 the controller is registered in StorageModule', () => {
|
||||
expectRegisteredController(StorageModule, StorageAdminController);
|
||||
});
|
||||
|
||||
it('STORCTL-005 POST /test probes, audits names only, answers 200-shaped result', async () => {
|
||||
const testResult = { ok: true, targets: [{ name: 'cand', ok: true }] };
|
||||
const { controller, writeAudit } = makeController({ testBackend: vi.fn(async () => testResult) });
|
||||
const result = await controller.test(user, { backend: CONFIG.backends[0]! } as StorageTestRequestDto, req);
|
||||
expect(result).toBe(testResult);
|
||||
expect(writeAudit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
action: 'admin.storage_test',
|
||||
details: { backend: 'off-box', type: 's3', ok: true, targets: [{ name: 'cand', ok: true }] },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('STORCTL-006 POST /test maps thrown refusals to 400 verbatim', async () => {
|
||||
const { controller } = makeController({
|
||||
testBackend: vi.fn(async () => {
|
||||
throw new StorageBackendError("mirror 'm' references unknown backend 'nope'");
|
||||
}),
|
||||
});
|
||||
await expect(
|
||||
controller.test(user, { backend: CONFIG.backends[0]! } as StorageTestRequestDto, req),
|
||||
).rejects.toMatchObject({ status: 400 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -215,3 +215,97 @@ describe('StorageAdminService.applyConfig', () => {
|
||||
expect(reloadSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('StorageAdminService.testBackend', () => {
|
||||
it('STORADM-020 probes a healthy local candidate: ok with one green target', async () => {
|
||||
const { service } = makeService();
|
||||
const result = await service.testBackend({ name: 'cand', type: 'local', options: { root: makeTmpDir() } });
|
||||
expect(result).toEqual({ ok: true, targets: [{ name: 'cand', ok: true }] });
|
||||
});
|
||||
|
||||
it('STORADM-021 an unreachable s3 candidate fails fast with a per-target error (no registry impact)', async () => {
|
||||
const { service, registry } = makeService();
|
||||
const result = await service.testBackend({
|
||||
name: 'cand',
|
||||
type: 's3',
|
||||
options: {
|
||||
endpoint: 'http://127.0.0.1:1',
|
||||
bucket: 'trek',
|
||||
accessKeyId: 'ak',
|
||||
secretAccessKey: 'sk',
|
||||
region: 'us-east-1',
|
||||
keyPrefix: '',
|
||||
retries: 0,
|
||||
timeoutMs: 200,
|
||||
},
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.targets[0]!.ok).toBe(false);
|
||||
expect(result.targets[0]!.error).toBeTruthy();
|
||||
expect(registry.resolve('backups').backendName).toBe('backups-local'); // state untouched
|
||||
});
|
||||
|
||||
it('STORADM-022 a mirror is probed per target, replica failures reported individually', async () => {
|
||||
const goodRoot = makeTmpDir();
|
||||
// Not yet a file: registry.build() eagerly driver.init()s EVERY local
|
||||
// backend (used or not) during applyConfig's preview, so a root that's
|
||||
// already a file at that point would make applyConfig itself throw
|
||||
// (unrelated to testBackend). Register it as a fresh, empty dir, then
|
||||
// corrupt it afterward — testBackend's ephemeralDriverFor re-runs
|
||||
// init() fresh at probe time and hits the same EEXIST there instead.
|
||||
const badRoot = path.join(makeTmpDir(), 'a-file');
|
||||
const { service, uploadsRoot } = makeService();
|
||||
service.applyConfig(configWith(uploadsRoot, {
|
||||
backends: [
|
||||
{ name: 'good-local', type: 'local', options: { root: goodRoot } },
|
||||
{ name: 'bad-local', type: 'local', options: { root: badRoot } },
|
||||
],
|
||||
categories: {},
|
||||
}));
|
||||
fs.rmSync(badRoot, { recursive: true, force: true });
|
||||
fs.writeFileSync(badRoot, 'not a dir');
|
||||
const result = await service.testBackend({
|
||||
name: 'cand-mirror',
|
||||
type: 'mirror',
|
||||
options: { primary: 'good-local', replicas: ['bad-local'] },
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.targets).toEqual([
|
||||
{ name: 'good-local', ok: true },
|
||||
expect.objectContaining({ name: 'bad-local', ok: false }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('STORADM-023 a mirror referencing an unknown or mirror-typed backend throws (→ 400)', async () => {
|
||||
const { service } = makeService();
|
||||
await expect(
|
||||
service.testBackend({ name: 'm', type: 'mirror', options: { primary: 'nope', replicas: [] } }),
|
||||
).rejects.toThrow("mirror 'm' references unknown backend 'nope'");
|
||||
});
|
||||
|
||||
it('STORADM-024 unmasks a stored backend by name before probing (mask echo works on /test)', async () => {
|
||||
const { service, uploadsRoot } = makeService();
|
||||
const root = makeTmpDir();
|
||||
service.applyConfig(configWith(uploadsRoot, {
|
||||
backends: [{ name: 'nas', type: 'local', options: { root } }],
|
||||
categories: {},
|
||||
}));
|
||||
// local has no secrets — the unmask contract is pinned via an s3 mask with no counterpart:
|
||||
await expect(
|
||||
service.testBackend({
|
||||
name: 'ghost',
|
||||
type: 's3',
|
||||
options: {
|
||||
endpoint: 'http://127.0.0.1:1',
|
||||
bucket: 'trek',
|
||||
accessKeyId: 'ak',
|
||||
secretAccessKey: MASKED_SETTING_VALUE,
|
||||
region: 'us-east-1',
|
||||
keyPrefix: '',
|
||||
retries: 0,
|
||||
timeoutMs: 200,
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow("re-enter the secret 'secretAccessKey' for 'ghost'");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { LocalDriver } from '../../../../src/nest/storage/drivers/local.driver';
|
||||
import { S3Driver } from '../../../../src/nest/storage/drivers/s3.driver';
|
||||
import { ephemeralDriverFor, probeDriver } from '../../../../src/nest/storage/storage-probe';
|
||||
import type { StorageDriver } from '../../../../src/nest/storage/storage.types';
|
||||
|
||||
function makeTmpDir(): string {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'trek-probe-'));
|
||||
}
|
||||
|
||||
describe('probeDriver', () => {
|
||||
it('PROBE-001 round-trips put/stat/delete against a healthy local driver and leaves no object', async () => {
|
||||
const root = makeTmpDir();
|
||||
const driver = new LocalDriver({ id: 'probe-t', root });
|
||||
driver.init({ ensurePrefixes: [], cleanSpool: false });
|
||||
const result = await probeDriver('t', driver);
|
||||
expect(result).toEqual({ name: 't', ok: true });
|
||||
// The probe object itself is gone (an empty trek-probe/ dir may remain — inert).
|
||||
const leftovers: string[] = [];
|
||||
for await (const obj of driver.list('')) leftovers.push(obj.key);
|
||||
expect(leftovers).toEqual([]);
|
||||
});
|
||||
|
||||
it('PROBE-002 captures a failing driver as ok:false with the error message', async () => {
|
||||
const driver = {
|
||||
id: 'boom',
|
||||
put: vi.fn().mockRejectedValue(new Error('disk on fire')),
|
||||
stat: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
getStream: vi.fn(),
|
||||
list: vi.fn(),
|
||||
} as unknown as StorageDriver;
|
||||
const result = await probeDriver('boom', driver);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toContain('disk on fire');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ephemeralDriverFor', () => {
|
||||
it('PROBE-003 builds an initialized LocalDriver for local and an S3Driver for s3 — no registry involved', () => {
|
||||
const local = ephemeralDriverFor({ name: 'l', type: 'local', options: { root: makeTmpDir() } });
|
||||
expect(local).toBeInstanceOf(LocalDriver);
|
||||
const s3 = ephemeralDriverFor({
|
||||
name: 's',
|
||||
type: 's3',
|
||||
options: {
|
||||
endpoint: 'http://127.0.0.1:1',
|
||||
bucket: 'trek',
|
||||
accessKeyId: 'ak',
|
||||
secretAccessKey: 'sk',
|
||||
region: 'us-east-1',
|
||||
keyPrefix: '',
|
||||
retries: 0,
|
||||
timeoutMs: 200,
|
||||
},
|
||||
});
|
||||
expect(s3).toBeInstanceOf(S3Driver);
|
||||
});
|
||||
|
||||
it('PROBE-004 an uncreatable local root surfaces as a throw (the service turns it into a target error)', () => {
|
||||
const file = path.join(makeTmpDir(), 'a-file');
|
||||
fs.writeFileSync(file, 'not a dir');
|
||||
expect(() => ephemeralDriverFor({ name: 'l', type: 'local', options: { root: file } })).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from '../../../../src/nest/common/crypto/apiKeyCrypto';
|
||||
import {
|
||||
assertNoMaskSentinels,
|
||||
decryptBackendSecrets,
|
||||
encryptionGateError,
|
||||
encryptStorageSecrets,
|
||||
listPlaintextSecrets,
|
||||
@@ -109,6 +110,18 @@ describe('assertNoMaskSentinels', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('decryptBackendSecrets', () => {
|
||||
it('decrypts enc:v1: secrets, passes plaintext through, throws on tampered ciphertext', () => {
|
||||
const encrypted = s3Config(encrypt_api_key('sk-secret')).backends[0]!;
|
||||
expect((decryptBackendSecrets(encrypted).options as Record<string, unknown>).secretAccessKey).toBe('sk-secret');
|
||||
const plain = s3Config('sk-plain').backends[0]!;
|
||||
expect((decryptBackendSecrets(plain).options as Record<string, unknown>).secretAccessKey).toBe('sk-plain');
|
||||
expect(() => decryptBackendSecrets(s3Config('enc:v1:AAAA').backends[0]!)).toThrow(
|
||||
"could not decrypt 'secretAccessKey'",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('maskBackendOptions / redactStorageSecrets', () => {
|
||||
it('masks exactly the secret fields on GET-shaped options', () => {
|
||||
const masked = maskBackendOptions('s3', { ...S3_OPTIONS });
|
||||
|
||||
Reference in New Issue
Block a user