feat: import prepared Catalog bindings through authenticated services
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
import { createLocalAuthFixture, localPassword } from "./auth-test-fixtures.js";
|
||||
import { afterEach, expect, test, vi } from "vitest";
|
||||
import { buildApp } from "../src/app.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
import { MemoryCatalogRepository } from "../src/catalog/memory-repository.js";
|
||||
import { WorkspaceSecretStore } from "../src/workspaces/secret-store.js";
|
||||
import type { WorkspaceRegistry } from "../src/workspaces/registry.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(() => roots.splice(0).forEach(root => rmSync(root, { recursive: true, force: true })));
|
||||
const commit = "a".repeat(40);
|
||||
const configuration = { workspaceId: "chinook", engine: "postgres", databaseName: "chinook", schema: "public",
|
||||
binding: { transport: "postgres_direct", host: "database", port: 5432, username: "reader" } };
|
||||
function setup(environment: Record<string, string> = {}) {
|
||||
const root = mkdtempSync(join(tmpdir(), "installation-binding-")); roots.push(root);
|
||||
const secrets = new WorkspaceSecretStore({ root, installationId: "installation" });
|
||||
const repository = new MemoryCatalogRepository();
|
||||
const registry = {
|
||||
read: vi.fn(async () => ({ workspace: { workspace: { schema_version: 4, id: "chinook", name: "Chinook", language: "en" } },
|
||||
revision: { id: "chinook", commit, blob: "b".repeat(40) } })),
|
||||
} as unknown as WorkspaceRegistry;
|
||||
const app = buildApp(loadConfig({ NODE_ENV: "test", THT_HARNESS_DIR: "/missing", ...environment }), {
|
||||
workspaceRegistry: registry, workspaceSecretStore: secrets, catalogRepository: repository,
|
||||
catalogPostgresAccess: { connect: async () => ({ query: async () => ({ rows: [{ database: "chinook", schema: "public" }] }), end: async () => {} }) } as never,
|
||||
});
|
||||
const apply = (values = { password: "private-test-password" }) => app.inject({ method: "POST", url: "/installation/bindings",
|
||||
payload: { schemaVersion: 1, revision: commit, configuration, secrets: values, evidenceSecrets: {} } });
|
||||
return { app, apply, repository, secrets, registry };
|
||||
}
|
||||
|
||||
test("prepared binding is imported once and equivalent rerun preserves credentials and version", async () => {
|
||||
const { app, apply, repository, secrets } = setup();
|
||||
try {
|
||||
const first = await apply(); expect(first.statusCode).toBe(200);
|
||||
expect(first.json()).toMatchObject({ status: "applied", connectionStatus: "reachable" });
|
||||
const generation = secrets.generation("chinook");
|
||||
const before = await repository.getByWorkspace("chinook");
|
||||
const second = await apply(); expect(second.statusCode).toBe(200);
|
||||
expect(second.json()).toMatchObject({ status: "unchanged", databaseId: before!.id, version: before!.version });
|
||||
expect(secrets.generation("chinook")).toBe(generation);
|
||||
expect(await repository.list()).toHaveLength(1);
|
||||
expect(second.body).not.toContain("private-test-password");
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
|
||||
test("released pipe client uses real local login, CSRF and logout without exposing credentials", async () => {
|
||||
const original = setup(); await original.app.close();
|
||||
const registry = original.registry;
|
||||
registry.prepareInstallation = async () => ({ revision: commit });
|
||||
const fixture = await createLocalAuthFixture({ workspaceRegistry: registry,
|
||||
workspaceSecretStore: original.secrets, catalogRepository: original.repository,
|
||||
catalogPostgresAccess: { connect: async () => ({ query: async () => ({ rows: [{ database: "chinook", schema: "public" }] }), end: async () => {} }) } as never,
|
||||
});
|
||||
try {
|
||||
const address = await fixture.app.listen({ host: "127.0.0.1", port: 0 });
|
||||
const payload = { username: "Admin", password: localPassword, origin: fixture.publicUrl,
|
||||
source: { remote: "https://example.invalid/workspaces.git", branch: "main", revision: commit,
|
||||
files: { "thoth-workspaces.yaml": "a".repeat(64) } },
|
||||
databases: [{ schemaVersion: 1, configuration, secrets: { password: "private-test-password" }, evidenceSecrets: {} }] };
|
||||
const run = () => new Promise<{ code: number | null; output: string }>((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--input-type=module", "-e",
|
||||
`import { importPreparedBindings } from '../tools/tht/internal/installapply/import-bindings.mjs'; await importPreparedBindings(${JSON.stringify(address)});`], {
|
||||
env: process.env, stdio: ["pipe", "pipe", "pipe"],
|
||||
});
|
||||
let output = ""; child.stdout.on("data", data => { output += data; }); child.stderr.on("data", data => { output += data; });
|
||||
child.once("error", reject); child.once("close", code => resolve({ code, output }));
|
||||
child.stdin.end(JSON.stringify(payload));
|
||||
});
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const result = await run();
|
||||
expect(result).toEqual({ code: 0, output: JSON.stringify({ ok: true, code: "bindings_ready", completed: 1 }) });
|
||||
}
|
||||
expect(await original.repository.list()).toHaveLength(1);
|
||||
} finally { await fixture.close(); }
|
||||
});
|
||||
|
||||
test("administrative binding and credential changes require reconciliation without overwriting", async () => {
|
||||
const { app, apply, repository, secrets } = setup();
|
||||
try {
|
||||
expect((await apply()).statusCode).toBe(200);
|
||||
const current = (await repository.getByWorkspace("chinook"))!;
|
||||
secrets.put("chinook", "catalog.dwh.password", "changed-by-administrator");
|
||||
expect((await apply()).statusCode).toBe(409);
|
||||
expect(secrets.matches("chinook", "catalog.dwh.password", "changed-by-administrator")).toBe(true);
|
||||
const modified = { ...current, binding: { ...current.binding, host: "different-database" } };
|
||||
await repository.update(current.id, current.version, modified);
|
||||
expect((await apply()).statusCode).toBe(409);
|
||||
expect((await repository.get(current.id))!.binding.host).toBe("different-database");
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
|
||||
test("a manually configured binding is never silently adopted", async () => {
|
||||
const { app, apply, repository } = setup();
|
||||
try {
|
||||
const created = await app.inject({ method: "POST", url: "/catalog/databases", payload: configuration });
|
||||
expect(created.statusCode).toBe(201);
|
||||
expect((await apply()).statusCode).toBe(409);
|
||||
expect(await repository.list()).toHaveLength(1);
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
|
||||
test("administrative deletion remains a reconciliation stop rather than recreating a binding", async () => {
|
||||
const { app, apply, repository, secrets } = setup();
|
||||
try {
|
||||
expect((await apply()).statusCode).toBe(200);
|
||||
const database = (await repository.getByWorkspace("chinook"))!;
|
||||
const deleted = await app.inject({ method: "DELETE", url: `/catalog/databases/${database.id}?version=${database.version}` });
|
||||
expect(deleted.statusCode).toBe(204);
|
||||
expect((await apply()).statusCode).toBe(409);
|
||||
expect(await repository.list()).toHaveLength(0);
|
||||
expect(secrets.has("chinook", "catalog.dwh.password")).toBe(false);
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
|
||||
test("interruption after Catalog commit resumes the same identity without a host journal", async () => {
|
||||
const { app, apply, repository } = setup();
|
||||
const create = repository.create.bind(repository);
|
||||
vi.spyOn(repository, "create").mockImplementationOnce(async (...args) => {
|
||||
await create(...args); throw new Error("simulated connection loss after commit");
|
||||
});
|
||||
try {
|
||||
expect((await apply()).statusCode).toBe(503);
|
||||
const current = (await repository.getByWorkspace("chinook"))!;
|
||||
expect((await apply()).json()).toMatchObject({ databaseId: current.id, connectionStatus: "reachable" });
|
||||
expect(await repository.list()).toHaveLength(1);
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
|
||||
test("concurrent imports cannot duplicate or replace credentials", async () => {
|
||||
const { app, apply, repository, secrets } = setup();
|
||||
let entered!: () => void;
|
||||
let release!: () => void;
|
||||
const started = new Promise<void>(resolve => { entered = resolve; });
|
||||
const continueCreation = new Promise<void>(resolve => { release = resolve; });
|
||||
const create = repository.create.bind(repository);
|
||||
vi.spyOn(repository, "create").mockImplementationOnce(async (...args) => {
|
||||
entered(); await continueCreation; return create(...args);
|
||||
});
|
||||
try {
|
||||
const first = apply();
|
||||
await started;
|
||||
const second = await apply();
|
||||
release();
|
||||
const results = [await first, second];
|
||||
expect(results.map(result => result.statusCode).sort()).toEqual([200, 409]);
|
||||
expect(await repository.list()).toHaveLength(1);
|
||||
expect(secrets.matches("chinook", "catalog.dwh.password", "private-test-password")).toBe(true);
|
||||
} finally { release(); await app.close(); }
|
||||
});
|
||||
|
||||
test("imports and source activation require an authenticated administrator", async () => {
|
||||
const { app, repository } = setup({ AUTH_MODE: "upstream" });
|
||||
try {
|
||||
for (const url of ["/installation/bindings", "/installation/workspace-source"]) {
|
||||
expect((await app.inject({ method: "POST", url, payload: {} })).statusCode).toBe(401);
|
||||
expect((await app.inject({ method: "POST", url, payload: {}, headers: {
|
||||
"x-thoth-principal-issuer": "portal", "x-thoth-principal-subject": "reader", "x-thoth-is-admin": "0",
|
||||
} })).statusCode).toBe(403);
|
||||
}
|
||||
expect(await repository.list()).toHaveLength(0);
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
Reference in New Issue
Block a user