perf(dotnet): reuse authenticated artifact snapshots
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
|
import { createReadStream } from "node:fs";
|
||||||
import { readFile, stat } from "node:fs/promises";
|
import { readFile, stat } from "node:fs/promises";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -85,6 +86,8 @@ export class ManagedStaticProvider implements AnalysisProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ManagedStaticClient implements AnalysisClient {
|
class ManagedStaticClient implements AnalysisClient {
|
||||||
|
#snapshotBytes: Buffer | undefined;
|
||||||
|
|
||||||
constructor(private readonly target: BinaryTarget) {}
|
constructor(private readonly target: BinaryTarget) {}
|
||||||
|
|
||||||
async execute(
|
async execute(
|
||||||
@@ -123,11 +126,12 @@ class ManagedStaticClient implements AnalysisClient {
|
|||||||
`Artifact size ${String(metadata.size)} exceeds max_file_bytes ${String(maxFileBytes)}.`,
|
`Artifact size ${String(metadata.size)} exceeds max_file_bytes ${String(maxFileBytes)}.`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
const bytes = await readFile(
|
const snapshot = this.#snapshotBytes;
|
||||||
this.target.path,
|
const observed =
|
||||||
options?.signal === undefined ? undefined : { signal: options.signal },
|
snapshot === undefined
|
||||||
);
|
? await readManagedSnapshot(this.target.path, options?.signal)
|
||||||
if (bytes.length > maxFileBytes)
|
: await hashManagedSource(this.target.path, options?.signal);
|
||||||
|
if (observed.byteLength > maxFileBytes)
|
||||||
return err(
|
return err(
|
||||||
new AnalysisCapabilityUnavailableError(
|
new AnalysisCapabilityUnavailableError(
|
||||||
IDENTITY.id,
|
IDENTITY.id,
|
||||||
@@ -135,13 +139,16 @@ class ManagedStaticClient implements AnalysisClient {
|
|||||||
`Artifact grew beyond max_file_bytes ${String(maxFileBytes)} while it was read.`,
|
`Artifact grew beyond max_file_bytes ${String(maxFileBytes)} while it was read.`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
const observedDigest = createHash("sha256").update(bytes).digest("hex");
|
if (observed.sha256 !== this.target.sha256)
|
||||||
if (observedDigest !== this.target.sha256)
|
|
||||||
return err(
|
return err(
|
||||||
new EvidenceIntegrityError(
|
new EvidenceIntegrityError(
|
||||||
`Managed artifact digest changed after open: expected ${this.target.sha256}, observed ${observedDigest} at ${this.target.path}`,
|
`Managed artifact digest changed after open: expected ${this.target.sha256}, observed ${observed.sha256} at ${this.target.path}`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
const bytes = snapshot ?? observed.bytes;
|
||||||
|
if (bytes === undefined)
|
||||||
|
throw new TypeError("Managed snapshot bytes are unavailable");
|
||||||
|
this.#snapshotBytes = bytes;
|
||||||
const result = inspectManagedOperation(
|
const result = inspectManagedOperation(
|
||||||
operation,
|
operation,
|
||||||
parameters,
|
parameters,
|
||||||
@@ -164,10 +171,51 @@ class ManagedStaticClient implements AnalysisClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
|
this.#snapshotBytes = undefined;
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ManagedSourceObservation {
|
||||||
|
readonly byteLength: number;
|
||||||
|
readonly sha256: string;
|
||||||
|
readonly bytes?: Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const readManagedSnapshot = async (
|
||||||
|
path: string,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<ManagedSourceObservation> => {
|
||||||
|
const bytes = await readFile(
|
||||||
|
path,
|
||||||
|
signal === undefined ? undefined : { signal },
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
byteLength: bytes.length,
|
||||||
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||||
|
bytes,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const hashManagedSource = async (
|
||||||
|
path: string,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<ManagedSourceObservation> => {
|
||||||
|
const digest = createHash("sha256");
|
||||||
|
let byteLength = 0;
|
||||||
|
const stream = createReadStream(
|
||||||
|
path,
|
||||||
|
signal === undefined ? undefined : { signal },
|
||||||
|
);
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
if (!Buffer.isBuffer(chunk))
|
||||||
|
throw new TypeError("Managed source stream returned non-buffer data");
|
||||||
|
byteLength += chunk.length;
|
||||||
|
digest.update(chunk);
|
||||||
|
}
|
||||||
|
return { byteLength, sha256: digest.digest("hex") };
|
||||||
|
};
|
||||||
|
|
||||||
const isManagedOperation = (
|
const isManagedOperation = (
|
||||||
operation: AnalysisOperation,
|
operation: AnalysisOperation,
|
||||||
): operation is ManagedToolName =>
|
): operation is ManagedToolName =>
|
||||||
|
|||||||
Reference in New Issue
Block a user