mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
09a6374afe
* refactor(artifacts): share audit log reader lifecycle * refactor(artifacts): keep fixed ceilings with their formats
211 lines
7.0 KiB
TypeScript
211 lines
7.0 KiB
TypeScript
import type { Readable } from "node:stream";
|
|
|
|
import type { ArtifactFormatMetadata } from "../domain/artifactFormatMetadata.js";
|
|
import type {
|
|
RegionDecodeFailure,
|
|
RegionDecoder,
|
|
} from "../domain/firmware/regionDecoder.js";
|
|
import type { Result } from "../domain/result.js";
|
|
import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
|
|
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
|
|
import {
|
|
ArtifactReaderFailure,
|
|
malformedArtifact,
|
|
type ArtifactEntry,
|
|
type ArtifactReader,
|
|
type ArtifactTraversalOptions,
|
|
ArtifactResourceScope,
|
|
assertArtifactReadActive,
|
|
ArtifactFileSource,
|
|
yieldArtifactRead,
|
|
} from "../resources/index.js";
|
|
import { ArtifactPreparation } from "./ArtifactPreparation.js";
|
|
|
|
type AuditLogFormat = "auditd-log" | "kubernetes-audit-log";
|
|
|
|
interface AuditLogInventory<Format extends AuditLogFormat> {
|
|
readonly metadata: { readonly kind: Format };
|
|
readonly limitations: readonly string[];
|
|
}
|
|
|
|
interface AuditLogDecoding<Format extends AuditLogFormat> {
|
|
readonly label: string;
|
|
fileSizeFailure(
|
|
message: string,
|
|
boundary: NonNullable<RegionDecodeFailure["boundary"]>,
|
|
): ArtifactReaderFailure;
|
|
readonly schema: {
|
|
parse(metadata: unknown): Extract<ArtifactFormatMetadata, { kind: Format }>;
|
|
};
|
|
createDecoder(
|
|
fileBytes: number,
|
|
): Result<RegionDecoder<AuditLogInventory<Format>>, RegionDecodeFailure>;
|
|
}
|
|
|
|
/** Audit records describe their source. Only the root occurrence has metadata. */
|
|
export abstract class AuditLogArtifactReader<
|
|
Format extends AuditLogFormat,
|
|
> implements ArtifactReader {
|
|
readonly format: Format;
|
|
readonly #scope = new ArtifactResourceScope();
|
|
readonly #source: ArtifactFileSource;
|
|
readonly #preparation = new ArtifactPreparation();
|
|
readonly #decoding: AuditLogDecoding<Format>;
|
|
#metadata: ArtifactFormatMetadata | undefined;
|
|
#limitations: readonly string[] = [];
|
|
#closing = false;
|
|
|
|
protected constructor(
|
|
format: Format,
|
|
path: string,
|
|
admitted: StableRegularFileDescriptor | undefined,
|
|
decoding: AuditLogDecoding<Format>,
|
|
) {
|
|
this.format = format;
|
|
this.#decoding = decoding;
|
|
this.#source = new ArtifactFileSource(path, decoding.label, admitted);
|
|
}
|
|
|
|
formatMetadata(): ArtifactFormatMetadata | undefined {
|
|
return this.#metadata === undefined
|
|
? undefined
|
|
: structuredClone(this.#metadata);
|
|
}
|
|
|
|
async prepareContainer(
|
|
expectedSha256: string,
|
|
signal?: AbortSignal,
|
|
): Promise<void> {
|
|
await this.#scope.run(async () => {
|
|
this.#assertOpen(signal);
|
|
await this.#source.bindSha256(expectedSha256, signal);
|
|
});
|
|
}
|
|
|
|
async *entries(
|
|
signal?: AbortSignal,
|
|
options?: ArtifactTraversalOptions,
|
|
): AsyncIterable<ArtifactEntry> {
|
|
await this.#scope.run(async () => {
|
|
this.#assertOpen(signal);
|
|
await this.#preparation.run(
|
|
signal,
|
|
() => this.#prepare(signal),
|
|
() => this.#assertOpen(signal),
|
|
);
|
|
this.#assertOpen(signal);
|
|
await this.#source.read(signal);
|
|
});
|
|
// The log declares no members, so the root occurrence is the whole
|
|
// inventory; the coverage callback reports what the parse did not establish.
|
|
const coverage =
|
|
this.#limitations.length === 0
|
|
? { status: "complete" as const, limitations: [] }
|
|
: { status: "unknown" as const, limitations: [...this.#limitations] };
|
|
options?.onCoverage?.(coverage);
|
|
// onCoverage may abort the supplied signal; cancellation queued during the
|
|
// await must resolve as cancelled rather than as a completed traversal.
|
|
await yieldArtifactRead(signal);
|
|
}
|
|
|
|
async open(entry: ArtifactEntry, _signal?: AbortSignal): Promise<Readable> {
|
|
return this.#scope.run(async () => {
|
|
throw new ArtifactReaderFailure(
|
|
"unavailable",
|
|
`${this.#decoding.label} declares no member to open for ${entry.path}`,
|
|
);
|
|
});
|
|
}
|
|
|
|
async close(): Promise<void> {
|
|
this.#closing = true;
|
|
await this.#scope.close();
|
|
await this.#source.close();
|
|
this.#metadata = undefined;
|
|
}
|
|
|
|
provenance(): readonly [] {
|
|
return [];
|
|
}
|
|
|
|
#assertOpen(signal?: AbortSignal): void {
|
|
assertArtifactReadActive(signal);
|
|
if (this.#closing)
|
|
throw new ArtifactReaderFailure(
|
|
"unavailable",
|
|
`${this.#decoding.label} reader is closed`,
|
|
);
|
|
}
|
|
|
|
async #prepare(signal?: AbortSignal): Promise<void> {
|
|
const image = await this.#decode(signal);
|
|
const metadata = this.#decoding.schema.parse(image.metadata);
|
|
// The final read re-verifies the source; publishing the inventory before it
|
|
// succeeds would leave `formatMetadata()` returning facts whose source
|
|
// identity was never confirmed.
|
|
await this.#source.read(signal);
|
|
this.#metadata = metadata;
|
|
this.#limitations = image.limitations;
|
|
}
|
|
|
|
async #decode(signal?: AbortSignal): Promise<AuditLogInventory<Format>> {
|
|
const { handle, initial } = await this.#source.read(signal);
|
|
const decoder = this.#decoding.createDecoder(initial.size);
|
|
if (!decoder.ok) throw this.#failure(decoder.error);
|
|
if (initial.size > 0)
|
|
for await (const chunk of readFileHandleChunks(handle, {
|
|
end: initial.size - 1,
|
|
signal,
|
|
cancellationFailure: (reason) =>
|
|
new ArtifactReaderFailure(
|
|
"cancelled",
|
|
`${this.#decoding.label} read cancelled`,
|
|
{
|
|
cause: reason,
|
|
},
|
|
),
|
|
}) as AsyncIterable<Buffer>) {
|
|
this.#assertOpen(signal);
|
|
// The scanner joins records line by line and keeps only aggregate
|
|
// facts, so each chunk's work is bounded and cancellation is never
|
|
// blocked on a whole-file buffer.
|
|
const written = decoder.value.write(chunk);
|
|
if (!written.ok) throw await this.#stableFailure(written.error, signal);
|
|
}
|
|
const image = decoder.value.finish();
|
|
if (!image.ok) throw await this.#stableFailure(image.error, signal);
|
|
return image.value;
|
|
}
|
|
|
|
/**
|
|
* A decoder failure blames the bytes only once the source is shown to be
|
|
* unchanged; a source that shrank or was replaced reports that instead, so an
|
|
* analyst does not read "malformed" for a file that changed mid-read.
|
|
*/
|
|
async #stableFailure(
|
|
failure: RegionDecodeFailure,
|
|
signal: AbortSignal | undefined,
|
|
): Promise<ArtifactReaderFailure> {
|
|
await this.#source.read(signal);
|
|
return this.#failure(failure);
|
|
}
|
|
|
|
#failure(failure: RegionDecodeFailure): ArtifactReaderFailure {
|
|
if (failure.kind === "limit")
|
|
return this.#decoding.fileSizeFailure(
|
|
`${this.#decoding.label} exceeds a fixed limit: ${failure.message}`,
|
|
failure.boundary ?? "capacity",
|
|
);
|
|
return failure.kind === "malformed"
|
|
? malformedArtifact(
|
|
this.format,
|
|
`${this.#decoding.label} is malformed: ${failure.message}`,
|
|
)
|
|
: new ArtifactReaderFailure(
|
|
"limit",
|
|
`${this.#decoding.label} exceeds a fixed limit: ${failure.message}`,
|
|
failure.boundary === undefined ? {} : { boundary: failure.boundary },
|
|
);
|
|
}
|
|
}
|