Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+68
-7
@@ -1,14 +1,16 @@
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { spawn, type SpawnOptions } from "node:child_process";
|
||||
import { mkdir, mkdtemp, readFile, readdir, rm } from "node:fs/promises";
|
||||
import { lstat, mkdir, mkdtemp, readFile, readdir, realpath, rm, stat } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import { extract as extractTar } from "tar";
|
||||
import { S3Client } from "@aws-sdk/client-s3";
|
||||
import { downloadObjectToFile } from "./s3-upload.js";
|
||||
import { decryptFile } from "./encryption.js";
|
||||
import { deserializeManifest, type BackupManifest } from "./manifest.js";
|
||||
import { deserializeManifest, type BackupManifest, type ComponentEntry } from "./manifest.js";
|
||||
import { resolveBackupConfig, type BackupEnv } from "./env.js";
|
||||
|
||||
export interface RunRestoreOptions {
|
||||
@@ -57,29 +59,32 @@ export async function runRestoreBackup(opts: RunRestoreOptions): Promise<RunRest
|
||||
if (entries.length !== 1) {
|
||||
throw new Error(`Unexpected archive layout; got ${entries.length} top-level entries`);
|
||||
}
|
||||
const stageDir = join(extractDir, entries[0]);
|
||||
const stageDir = await resolveExtractedStageDirectory(extractDir, entries[0]);
|
||||
|
||||
const manifestRaw = await readFile(join(stageDir, "manifest.json"), "utf8");
|
||||
const manifestPath = await resolveContainedRegularFile(stageDir, "manifest.json", "backup manifest");
|
||||
const manifestRaw = await readFile(manifestPath, "utf8");
|
||||
const manifest = deserializeManifest(manifestRaw);
|
||||
|
||||
const dbUrl = opts.targetDatabaseUrl ?? config.databaseUrl;
|
||||
if (manifest.components.postgres) {
|
||||
const dumpPath = await verifyBackupComponent(stageDir, manifest.components.postgres, "Postgres dump");
|
||||
consola.info("Restoring Postgres dump");
|
||||
await pgRestore({
|
||||
connectionString: dbUrl,
|
||||
dumpPath: join(stageDir, manifest.components.postgres.path),
|
||||
dumpPath,
|
||||
binary: opts.pgRestoreBinary,
|
||||
spawnFn: opts.spawnFn,
|
||||
});
|
||||
}
|
||||
|
||||
if (manifest.components.storage && !opts.skipStorage) {
|
||||
const storagePath = await verifyBackupComponent(stageDir, manifest.components.storage, "storage archive", { verifySize: false });
|
||||
// Storage restore is operator-driven via re-upload from the staged tar.
|
||||
// Implementation lives in storage.ts in a follow-up if/when staging
|
||||
// verifies the create flow against a real bucket. For now we surface
|
||||
// a clear hint so the operator can manually re-import.
|
||||
consola.warn(
|
||||
`Storage tar at ${join(stageDir, manifest.components.storage.path)} not auto-uploaded; re-upload via aws s3 sync or use --target-bucket in a future revision.`,
|
||||
`Storage tar at ${storagePath} not auto-uploaded; re-upload via aws s3 sync or use --target-bucket in a future revision.`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -89,6 +94,62 @@ export async function runRestoreBackup(opts: RunRestoreOptions): Promise<RunRest
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveExtractedStageDirectory(extractDir: string, entry: string): Promise<string> {
|
||||
const extractRoot = await realpath(extractDir);
|
||||
const candidate = resolve(extractRoot, entry);
|
||||
assertContainedPath(extractRoot, candidate, "backup stage directory");
|
||||
const candidateStat = await lstat(candidate);
|
||||
if (candidateStat.isSymbolicLink() || !candidateStat.isDirectory()) {
|
||||
throw new Error("Backup stage entry must be a real directory");
|
||||
}
|
||||
const resolvedCandidate = await realpath(candidate);
|
||||
assertContainedPath(extractRoot, resolvedCandidate, "backup stage directory");
|
||||
return resolvedCandidate;
|
||||
}
|
||||
|
||||
async function resolveContainedRegularFile(rootDir: string, relativePath: string, label: string): Promise<string> {
|
||||
const root = await realpath(rootDir);
|
||||
const candidate = resolve(root, relativePath);
|
||||
assertContainedPath(root, candidate, label);
|
||||
const resolvedCandidate = await realpath(candidate);
|
||||
assertContainedPath(root, resolvedCandidate, label);
|
||||
const candidateStat = await stat(resolvedCandidate);
|
||||
if (!candidateStat.isFile()) {
|
||||
throw new Error(`${label} must be a regular file`);
|
||||
}
|
||||
return resolvedCandidate;
|
||||
}
|
||||
|
||||
function assertContainedPath(root: string, candidate: string, label: string): void {
|
||||
const relativePath = relative(root, candidate);
|
||||
if (relativePath === ".." || relativePath.startsWith(`..${sep}`) || isAbsolute(relativePath)) {
|
||||
throw new Error(`${label} escapes the extracted backup directory`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyBackupComponent(
|
||||
stageDir: string,
|
||||
component: ComponentEntry,
|
||||
label: string,
|
||||
options: { verifySize?: boolean } = {},
|
||||
): Promise<string> {
|
||||
const componentPath = await resolveContainedRegularFile(stageDir, component.path, label);
|
||||
const componentStat = await stat(componentPath);
|
||||
if (options.verifySize !== false && componentStat.size !== component.sizeBytes) {
|
||||
throw new Error(`${label} size mismatch: expected ${component.sizeBytes} bytes, got ${componentStat.size}`);
|
||||
}
|
||||
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(componentPath)) {
|
||||
hash.update(chunk as Buffer);
|
||||
}
|
||||
const actualChecksum = hash.digest("hex");
|
||||
if (actualChecksum !== component.checksum.value) {
|
||||
throw new Error(`${label} checksum mismatch`);
|
||||
}
|
||||
return componentPath;
|
||||
}
|
||||
|
||||
async function rename(from: string, to: string): Promise<void> {
|
||||
const fs = await import("node:fs/promises");
|
||||
await fs.rename(from, to);
|
||||
|
||||
Reference in new issue
Block a user