mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-23 03:18:09 +08:00
feat(ql3): establish 3.0 incubation baseline
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"name": "@qinglong/local-owner-maintenance",
|
||||
"version": "3.0.0-alpha.0",
|
||||
"private": true,
|
||||
"description": "QingLong 3.0 short-lived Local Owner maintenance authorities and GC command adapter",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=24.18.0 <25"
|
||||
},
|
||||
"exports": {
|
||||
"./pepper-gc": {
|
||||
"types": "./dist/security-maintenance/pepperGc.d.ts",
|
||||
"require": "./dist/security-maintenance/pepperGc.js",
|
||||
"default": "./dist/security-maintenance/pepperGc.js"
|
||||
},
|
||||
"./acknowledgement-gc": {
|
||||
"types": "./dist/security-maintenance/acknowledgementGc.d.ts",
|
||||
"require": "./dist/security-maintenance/acknowledgementGc.js",
|
||||
"default": "./dist/security-maintenance/acknowledgementGc.js"
|
||||
},
|
||||
"./prompt-output-gc": {
|
||||
"types": "./dist/prompt-output-maintenance/promptOutputGc.d.ts",
|
||||
"require": "./dist/prompt-output-maintenance/promptOutputGc.js",
|
||||
"default": "./dist/prompt-output-maintenance/promptOutputGc.js"
|
||||
},
|
||||
"./prompt-output-key-retirement": {
|
||||
"types": "./dist/prompt-output-maintenance/promptOutputKeyRetirement.d.ts",
|
||||
"require": "./dist/prompt-output-maintenance/promptOutputKeyRetirement.js",
|
||||
"default": "./dist/prompt-output-maintenance/promptOutputKeyRetirement.js"
|
||||
},
|
||||
"./command": {
|
||||
"types": "./dist/application-command/localOwnerMaintenanceCommand.d.ts",
|
||||
"require": "./dist/application-command/localOwnerMaintenanceCommand.js",
|
||||
"default": "./dist/application-command/localOwnerMaintenanceCommand.js"
|
||||
}
|
||||
},
|
||||
"bin": {
|
||||
"ql3-owner-gc": "dist/cli.js"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"pepper-gc": [
|
||||
"dist/security-maintenance/pepperGc.d.ts"
|
||||
],
|
||||
"acknowledgement-gc": [
|
||||
"dist/security-maintenance/acknowledgementGc.d.ts"
|
||||
],
|
||||
"prompt-output-gc": [
|
||||
"dist/prompt-output-maintenance/promptOutputGc.d.ts"
|
||||
],
|
||||
"prompt-output-key-retirement": [
|
||||
"dist/prompt-output-maintenance/promptOutputKeyRetirement.d.ts"
|
||||
],
|
||||
"command": [
|
||||
"dist/application-command/localOwnerMaintenanceCommand.d.ts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist/**/*.js",
|
||||
"dist/**/*.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"check": "node ../../scripts/ql3-build-package-closure.cjs && tsc -p tsconfig.json --noEmit",
|
||||
"test": "node ../../scripts/ql3-build-package-closure.cjs && node --test test/*.test.cjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@qinglong/local-command-file": "workspace:*",
|
||||
"@qinglong/local-owner-console": "workspace:*",
|
||||
"@qinglong/local-sqlite": "workspace:*",
|
||||
"@qinglong/runtime-core": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@qinglong/ai": "workspace:*",
|
||||
"@types/node": "24.13.3",
|
||||
"typescript": "5.9.3"
|
||||
}
|
||||
}
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
import {
|
||||
PrivateLocalCommandFileError,
|
||||
readPrivateLocalCommandFile,
|
||||
} from '@qinglong/local-command-file';
|
||||
import {
|
||||
openLocalOwnerDeliveryAcknowledgementGc,
|
||||
type CompactLocalOwnerDeliveryAcknowledgementRequest,
|
||||
type OpenLocalOwnerDeliveryAcknowledgementGcOptions,
|
||||
} from '../security-maintenance/acknowledgementGc';
|
||||
import {
|
||||
openLocalOwnerPepperMaterialGc,
|
||||
type CollectLocalOwnerPepperMaterialRequest,
|
||||
type OpenLocalOwnerPepperMaterialGcOptions,
|
||||
} from '../security-maintenance/pepperGc';
|
||||
import type {
|
||||
LocalOwnerPromptOutputGcAuthority,
|
||||
OpenLocalOwnerPromptOutputGcOptions,
|
||||
} from '../prompt-output-maintenance/promptOutputGc';
|
||||
import type {
|
||||
LocalOwnerPromptOutputKeyRetirementAuthority,
|
||||
OpenLocalOwnerPromptOutputKeyRetirementOptions,
|
||||
RetireLocalOwnerPromptOutputKeyRequest,
|
||||
} from '../prompt-output-maintenance/promptOutputKeyRetirement';
|
||||
|
||||
export interface LocalOwnerAcknowledgementGcCommand {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.delivery-acknowledgement.compact';
|
||||
readonly options: OpenLocalOwnerDeliveryAcknowledgementGcOptions;
|
||||
readonly request: CompactLocalOwnerDeliveryAcknowledgementRequest;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPepperMaterialGcCommand {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.pepper-material.collect';
|
||||
readonly options: OpenLocalOwnerPepperMaterialGcOptions;
|
||||
readonly request: CollectLocalOwnerPepperMaterialRequest;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputGcCommand {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.prompt-output.collect';
|
||||
readonly options: OpenLocalOwnerPromptOutputGcOptions;
|
||||
readonly request: Readonly<Record<string, never>>;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputKeyRetirementCommand {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.prompt-output-key.retire';
|
||||
readonly options: OpenLocalOwnerPromptOutputKeyRetirementOptions;
|
||||
readonly request: RetireLocalOwnerPromptOutputKeyRequest;
|
||||
}
|
||||
|
||||
export type LocalOwnerGcCommand =
|
||||
| LocalOwnerAcknowledgementGcCommand
|
||||
| LocalOwnerPepperMaterialGcCommand
|
||||
| LocalOwnerPromptOutputGcCommand
|
||||
| LocalOwnerPromptOutputKeyRetirementCommand;
|
||||
|
||||
export interface LocalOwnerAcknowledgementGcCommandResult {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.delivery-acknowledgement.compact';
|
||||
readonly status: 'inserted' | 'existing';
|
||||
readonly gcMutationId: string;
|
||||
readonly acknowledgementMutationId: string;
|
||||
readonly acknowledgementKind: 'credential' | 'challenge';
|
||||
readonly retentionEligibleAtMs: number;
|
||||
readonly compactedAtMs: number;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPepperMaterialGcCommandResult {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.pepper-material.collect';
|
||||
readonly status: 'inserted' | 'existing';
|
||||
readonly prepareMutationId: string;
|
||||
readonly completeMutationId: string;
|
||||
readonly pepperKeyId: string;
|
||||
readonly state: 'completed';
|
||||
readonly completedAtMs: number;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputGcCommandResult {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.prompt-output.collect';
|
||||
readonly scanned: number;
|
||||
readonly tombstoned: number;
|
||||
readonly skipped: number;
|
||||
readonly hasMore: boolean;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputKeyRetirementCommandResult {
|
||||
readonly schemaVersion: 1;
|
||||
readonly operation: 'owner.prompt-output-key.retire';
|
||||
readonly status: 'completed' | 'existing';
|
||||
readonly keyId: string;
|
||||
readonly retirementId: string;
|
||||
readonly preparationDigest: string;
|
||||
readonly completionDigest: string;
|
||||
readonly completedAtMs: number;
|
||||
}
|
||||
|
||||
export type LocalOwnerGcCommandResult =
|
||||
| LocalOwnerAcknowledgementGcCommandResult
|
||||
| LocalOwnerPepperMaterialGcCommandResult
|
||||
| LocalOwnerPromptOutputGcCommandResult
|
||||
| LocalOwnerPromptOutputKeyRetirementCommandResult;
|
||||
|
||||
export interface LocalOwnerGcCommandRunnerDependencies {
|
||||
readonly openAcknowledgementGc: typeof openLocalOwnerDeliveryAcknowledgementGc;
|
||||
readonly openPepperMaterialGc: typeof openLocalOwnerPepperMaterialGc;
|
||||
readonly openPromptOutputGc: (
|
||||
options: OpenLocalOwnerPromptOutputGcOptions,
|
||||
) => Promise<LocalOwnerPromptOutputGcAuthority>;
|
||||
readonly openPromptOutputKeyRetirement: (
|
||||
options: OpenLocalOwnerPromptOutputKeyRetirementOptions,
|
||||
) => Promise<LocalOwnerPromptOutputKeyRetirementAuthority>;
|
||||
}
|
||||
|
||||
export interface LocalOwnerGcCommandRunner {
|
||||
run(commandFilePath: string): Promise<Readonly<LocalOwnerGcCommandResult>>;
|
||||
}
|
||||
|
||||
export class LocalOwnerGcCliConfigurationError extends TypeError {
|
||||
readonly code = 'LOCAL_OWNER_GC_CLI_CONFIGURATION_INVALID';
|
||||
|
||||
constructor(message: string, readonly cause?: unknown) {
|
||||
super(`Local Owner GC CLI configuration is invalid: ${message}`);
|
||||
this.name = 'LocalOwnerGcCliConfigurationError';
|
||||
}
|
||||
}
|
||||
|
||||
function exactKeys(value: object, expected: readonly string[]): boolean {
|
||||
const actual = Object.keys(value).sort();
|
||||
const canonical = [...expected].sort();
|
||||
return (
|
||||
actual.length === canonical.length &&
|
||||
actual.every((key, index) => key === canonical[index])
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeCommand(value: unknown): Readonly<LocalOwnerGcCommand> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, ['schemaVersion', 'operation', 'options', 'request'])
|
||||
) {
|
||||
throw new LocalOwnerGcCliConfigurationError('command shape is invalid');
|
||||
}
|
||||
const candidate = value as Record<string, unknown>;
|
||||
if (
|
||||
candidate.schemaVersion !== 1 ||
|
||||
(candidate.operation !== 'owner.delivery-acknowledgement.compact' &&
|
||||
candidate.operation !== 'owner.pepper-material.collect' &&
|
||||
candidate.operation !== 'owner.prompt-output.collect' &&
|
||||
candidate.operation !== 'owner.prompt-output-key.retire') ||
|
||||
!candidate.options ||
|
||||
typeof candidate.options !== 'object' ||
|
||||
Array.isArray(candidate.options) ||
|
||||
!candidate.request ||
|
||||
typeof candidate.request !== 'object' ||
|
||||
Array.isArray(candidate.request)
|
||||
) {
|
||||
throw new LocalOwnerGcCliConfigurationError('command value is invalid');
|
||||
}
|
||||
return Object.freeze(value as LocalOwnerGcCommand);
|
||||
}
|
||||
|
||||
function readCommandFile(candidatePath: string): Readonly<LocalOwnerGcCommand> {
|
||||
try {
|
||||
return normalizeCommand(readPrivateLocalCommandFile(candidatePath));
|
||||
} catch (error) {
|
||||
if (error instanceof LocalOwnerGcCliConfigurationError) throw error;
|
||||
if (error instanceof PrivateLocalCommandFileError) {
|
||||
throw new LocalOwnerGcCliConfigurationError(
|
||||
'command file cannot be read',
|
||||
error,
|
||||
);
|
||||
}
|
||||
throw new LocalOwnerGcCliConfigurationError(
|
||||
'command file cannot be read',
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function dependencies(
|
||||
value: LocalOwnerGcCommandRunnerDependencies,
|
||||
): Readonly<LocalOwnerGcCommandRunnerDependencies> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, [
|
||||
'openAcknowledgementGc',
|
||||
'openPepperMaterialGc',
|
||||
'openPromptOutputGc',
|
||||
'openPromptOutputKeyRetirement',
|
||||
]) ||
|
||||
typeof value.openAcknowledgementGc !== 'function' ||
|
||||
typeof value.openPepperMaterialGc !== 'function' ||
|
||||
typeof value.openPromptOutputGc !== 'function' ||
|
||||
typeof value.openPromptOutputKeyRetirement !== 'function'
|
||||
) {
|
||||
throw new LocalOwnerGcCliConfigurationError(
|
||||
'runner dependencies are invalid',
|
||||
);
|
||||
}
|
||||
return Object.freeze({ ...value });
|
||||
}
|
||||
|
||||
export function createLocalOwnerGcCommandRunner(
|
||||
candidateDependencies: LocalOwnerGcCommandRunnerDependencies = {
|
||||
openAcknowledgementGc: openLocalOwnerDeliveryAcknowledgementGc,
|
||||
openPepperMaterialGc: openLocalOwnerPepperMaterialGc,
|
||||
async openPromptOutputGc(options) {
|
||||
const { openLocalOwnerPromptOutputGc } = await import(
|
||||
'../prompt-output-maintenance/promptOutputGc.js'
|
||||
);
|
||||
return openLocalOwnerPromptOutputGc(options);
|
||||
},
|
||||
async openPromptOutputKeyRetirement(options) {
|
||||
const { openLocalOwnerPromptOutputKeyRetirement } = await import(
|
||||
'../prompt-output-maintenance/promptOutputKeyRetirement.js'
|
||||
);
|
||||
return openLocalOwnerPromptOutputKeyRetirement(options);
|
||||
},
|
||||
},
|
||||
): LocalOwnerGcCommandRunner {
|
||||
const adapters = dependencies(candidateDependencies);
|
||||
return Object.freeze({
|
||||
async run(commandFilePath: string) {
|
||||
const command = readCommandFile(commandFilePath);
|
||||
if (command.operation === 'owner.delivery-acknowledgement.compact') {
|
||||
const authority = await adapters.openAcknowledgementGc(command.options);
|
||||
try {
|
||||
const result = await authority.compact(command.request);
|
||||
return Object.freeze({
|
||||
schemaVersion: 1 as const,
|
||||
operation: command.operation,
|
||||
status: result.status,
|
||||
gcMutationId: result.record.mutationId,
|
||||
acknowledgementMutationId: result.record.acknowledgementMutationId,
|
||||
acknowledgementKind: result.record.acknowledgementKind,
|
||||
retentionEligibleAtMs: result.record.retentionEligibleAtMs,
|
||||
compactedAtMs: result.record.compactedAtMs,
|
||||
});
|
||||
} finally {
|
||||
await authority.close();
|
||||
}
|
||||
}
|
||||
if (command.operation === 'owner.prompt-output.collect') {
|
||||
if (Object.keys(command.request).length !== 0) {
|
||||
throw new LocalOwnerGcCliConfigurationError(
|
||||
'Prompt output collection request must be empty',
|
||||
);
|
||||
}
|
||||
const authority = await adapters.openPromptOutputGc(command.options);
|
||||
try {
|
||||
const result = await authority.collect();
|
||||
return Object.freeze({
|
||||
schemaVersion: 1 as const,
|
||||
operation: command.operation,
|
||||
...result,
|
||||
});
|
||||
} finally {
|
||||
await authority.close();
|
||||
}
|
||||
}
|
||||
if (command.operation === 'owner.prompt-output-key.retire') {
|
||||
const authority = await adapters.openPromptOutputKeyRetirement(
|
||||
command.options,
|
||||
);
|
||||
try {
|
||||
const result = await authority.retire(command.request);
|
||||
return Object.freeze({
|
||||
schemaVersion: 1 as const,
|
||||
operation: command.operation,
|
||||
...result,
|
||||
});
|
||||
} finally {
|
||||
await authority.close();
|
||||
}
|
||||
}
|
||||
const authority = await adapters.openPepperMaterialGc(command.options);
|
||||
try {
|
||||
const result = await authority.collect(command.request);
|
||||
if (
|
||||
result.record.state !== 'completed' ||
|
||||
!result.record.completeMutationId ||
|
||||
result.record.completedAtMs === undefined
|
||||
) {
|
||||
throw new LocalOwnerGcCliConfigurationError(
|
||||
'pepper material collection did not complete',
|
||||
);
|
||||
}
|
||||
return Object.freeze({
|
||||
schemaVersion: 1 as const,
|
||||
operation: command.operation,
|
||||
status: result.status,
|
||||
prepareMutationId: result.record.prepareMutationId,
|
||||
completeMutationId: result.record.completeMutationId,
|
||||
pepperKeyId: result.record.pepperKeyId,
|
||||
state: result.record.state,
|
||||
completedAtMs: result.record.completedAtMs,
|
||||
});
|
||||
} finally {
|
||||
await authority.close();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function runLocalOwnerGcCommandFile(
|
||||
commandFilePath: string,
|
||||
): Promise<Readonly<LocalOwnerGcCommandResult>> {
|
||||
return createLocalOwnerGcCommandRunner().run(commandFilePath);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { runLocalOwnerGcCommandFile } from './application-command/localOwnerMaintenanceCommand';
|
||||
|
||||
const USAGE =
|
||||
'Usage: ql3-owner-gc run --command-file /absolute/private-command.json';
|
||||
|
||||
async function main(argv: readonly string[]): Promise<void> {
|
||||
if (argv.length === 1 && (argv[0] === '--help' || argv[0] === '-h')) {
|
||||
process.stdout.write(`${USAGE}\n`);
|
||||
return;
|
||||
}
|
||||
if (argv.length !== 3 || argv[0] !== 'run' || argv[1] !== '--command-file') {
|
||||
process.stderr.write(
|
||||
`${JSON.stringify({
|
||||
code: 'LOCAL_OWNER_GC_CLI_USAGE_INVALID',
|
||||
message: USAGE,
|
||||
})}\n`,
|
||||
);
|
||||
process.exitCode = 64;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await runLocalOwnerGcCommandFile(argv[2]!);
|
||||
process.stdout.write(`${JSON.stringify(result)}\n`);
|
||||
} catch (error) {
|
||||
const candidate = error as {
|
||||
readonly code?: unknown;
|
||||
readonly name?: unknown;
|
||||
readonly message?: unknown;
|
||||
};
|
||||
process.stderr.write(
|
||||
`${JSON.stringify({
|
||||
code:
|
||||
typeof candidate.code === 'string'
|
||||
? candidate.code
|
||||
: 'LOCAL_OWNER_GC_CLI_FAILED',
|
||||
name: typeof candidate.name === 'string' ? candidate.name : 'Error',
|
||||
message:
|
||||
typeof candidate.message === 'string'
|
||||
? candidate.message
|
||||
: 'Local Owner GC command failed',
|
||||
})}\n`,
|
||||
);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void main(process.argv.slice(2));
|
||||
@@ -0,0 +1,69 @@
|
||||
import { assertLocalModelInvocationFeatureReady } from '@qinglong/ai/model-invocation-migration';
|
||||
import { LocalPluginPackagePromptOutputGarbageCollector } from '@qinglong/ai/local-plugin-package-prompt-output-retention-storage';
|
||||
import {
|
||||
createPluginPackagePromptOutputRetentionPolicyCatalogResolver,
|
||||
type PluginPackagePromptOutputRetentionPolicyCatalog,
|
||||
} from '@qinglong/ai/plugin-package-prompt-output-retention';
|
||||
import {
|
||||
openLocalSqliteOptionalFeatureRuntimeDatabase,
|
||||
type LocalSqliteDatabaseOptions,
|
||||
type LocalSqliteProfile,
|
||||
} from '@qinglong/local-sqlite/optional-feature-runtime';
|
||||
|
||||
export interface OpenLocalOwnerPromptOutputGcOptions
|
||||
extends LocalSqliteDatabaseOptions {
|
||||
readonly retentionPolicyCatalog: PluginPackagePromptOutputRetentionPolicyCatalog;
|
||||
readonly limit?: number;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputGcAuthority {
|
||||
readonly profile: LocalSqliteProfile;
|
||||
collect(): Promise<Readonly<{
|
||||
scanned: number;
|
||||
tombstoned: number;
|
||||
skipped: number;
|
||||
hasMore: boolean;
|
||||
}>>;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export async function openLocalOwnerPromptOutputGc(
|
||||
options: OpenLocalOwnerPromptOutputGcOptions,
|
||||
): Promise<LocalOwnerPromptOutputGcAuthority> {
|
||||
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
||||
throw new TypeError('Local Prompt output GC options are invalid');
|
||||
}
|
||||
const policies =
|
||||
createPluginPackagePromptOutputRetentionPolicyCatalogResolver(
|
||||
options.retentionPolicyCatalog,
|
||||
);
|
||||
const database = await openLocalSqliteOptionalFeatureRuntimeDatabase({
|
||||
databasePath: options.databasePath,
|
||||
profile: options.profile,
|
||||
...(options.busyTimeoutMs === undefined
|
||||
? {}
|
||||
: { busyTimeoutMs: options.busyTimeoutMs }),
|
||||
});
|
||||
try {
|
||||
assertLocalModelInvocationFeatureReady(database.authority.client);
|
||||
const collector = new LocalPluginPackagePromptOutputGarbageCollector({
|
||||
authority: database.authority,
|
||||
policies,
|
||||
...(options.limit === undefined ? {} : { limit: options.limit }),
|
||||
});
|
||||
let closePromise: Promise<void> | undefined;
|
||||
return Object.freeze({
|
||||
profile: database.profile,
|
||||
collect() {
|
||||
return collector.collect();
|
||||
},
|
||||
close() {
|
||||
closePromise ??= database.close();
|
||||
return closePromise;
|
||||
},
|
||||
});
|
||||
} catch (cause) {
|
||||
await database.close();
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
import { assertLocalModelInvocationFeatureReady } from '@qinglong/ai/model-invocation-migration';
|
||||
import { PluginPackagePromptOutputFileKeyring } from '@qinglong/ai/plugin-package-prompt-output-file-keyring';
|
||||
import { PluginPackagePromptOutputKeyRetirementCoordinator } from '@qinglong/ai/plugin-package-prompt-output-key-retirement';
|
||||
import { LocalPluginPackagePromptOutputKeyRetirementRepository } from '@qinglong/ai/local-plugin-package-prompt-output-key-retirement-storage';
|
||||
import {
|
||||
openLocalSqliteOptionalFeatureRuntimeDatabase,
|
||||
type LocalSqliteDatabaseOptions,
|
||||
type LocalSqliteProfile,
|
||||
} from '@qinglong/local-sqlite/optional-feature-runtime';
|
||||
|
||||
export interface OpenLocalOwnerPromptOutputKeyRetirementOptions
|
||||
extends LocalSqliteDatabaseOptions {
|
||||
readonly keyringPath: string;
|
||||
}
|
||||
|
||||
export interface RetireLocalOwnerPromptOutputKeyRequest {
|
||||
readonly keyId: string;
|
||||
readonly retirementId: string;
|
||||
readonly requestId: string;
|
||||
readonly mutationId: string;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPromptOutputKeyRetirementAuthority {
|
||||
readonly profile: LocalSqliteProfile;
|
||||
retire(request: RetireLocalOwnerPromptOutputKeyRequest): Promise<Readonly<{
|
||||
status: 'completed' | 'existing';
|
||||
keyId: string;
|
||||
retirementId: string;
|
||||
preparationDigest: string;
|
||||
completionDigest: string;
|
||||
completedAtMs: number;
|
||||
}>>;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export async function openLocalOwnerPromptOutputKeyRetirement(
|
||||
options: OpenLocalOwnerPromptOutputKeyRetirementOptions,
|
||||
): Promise<LocalOwnerPromptOutputKeyRetirementAuthority> {
|
||||
if (
|
||||
!options ||
|
||||
typeof options !== 'object' ||
|
||||
Array.isArray(options) ||
|
||||
typeof options.keyringPath !== 'string'
|
||||
) {
|
||||
throw new TypeError('Local Prompt output key retirement options are invalid');
|
||||
}
|
||||
const database = await openLocalSqliteOptionalFeatureRuntimeDatabase({
|
||||
databasePath: options.databasePath,
|
||||
profile: options.profile,
|
||||
...(options.busyTimeoutMs === undefined
|
||||
? {}
|
||||
: { busyTimeoutMs: options.busyTimeoutMs }),
|
||||
});
|
||||
try {
|
||||
assertLocalModelInvocationFeatureReady(database.authority.client);
|
||||
const keys = new PluginPackagePromptOutputFileKeyring(options.keyringPath);
|
||||
const repository =
|
||||
new LocalPluginPackagePromptOutputKeyRetirementRepository({
|
||||
authority: database.authority,
|
||||
});
|
||||
const coordinator = new PluginPackagePromptOutputKeyRetirementCoordinator({
|
||||
repository,
|
||||
materials: keys,
|
||||
});
|
||||
let closePromise: Promise<void> | undefined;
|
||||
return Object.freeze({
|
||||
profile: database.profile,
|
||||
async retire(request: RetireLocalOwnerPromptOutputKeyRequest) {
|
||||
const result = await coordinator.retire(request);
|
||||
return Object.freeze({
|
||||
status: result.status,
|
||||
keyId: result.preparation.keyId,
|
||||
retirementId: result.preparation.retirementId,
|
||||
preparationDigest: result.preparation.preparationDigest,
|
||||
completionDigest: result.completion.completionDigest,
|
||||
completedAtMs: result.completion.completedAtMs,
|
||||
});
|
||||
},
|
||||
close() {
|
||||
closePromise ??= database.close();
|
||||
return closePromise;
|
||||
},
|
||||
});
|
||||
} catch (cause) {
|
||||
await database.close();
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
import { FileLocalOwnerBootstrapSecretDelivery } from '@qinglong/local-owner-console/secret-delivery';
|
||||
import {
|
||||
openLocalSqliteAcknowledgementGcDatabase,
|
||||
type LocalSqliteAcknowledgementGcDatabase,
|
||||
type LocalSqliteDatabaseOptions,
|
||||
type LocalSqliteProfile,
|
||||
} from '@qinglong/local-sqlite/acknowledgement-gc';
|
||||
import {
|
||||
localOwnerDeliveryAcknowledgementGcRetentionPolicyDigest,
|
||||
type LocalOwnerDeliveryAcknowledgementGcRecord,
|
||||
type LocalOwnerDeliveryAcknowledgementGcRepository,
|
||||
type LocalOwnerDeliveryAcknowledgementGcRetentionPolicy,
|
||||
} from '@qinglong/runtime-core/local-owner-delivery-acknowledgement-gc';
|
||||
|
||||
const UUID_V4_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||
const REQUEST_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
||||
const DIGEST_PATTERN = /^[0-9a-f]{64}$/;
|
||||
|
||||
export interface CompactLocalOwnerDeliveryAcknowledgementRequest {
|
||||
readonly mutationId: string;
|
||||
readonly requestId: string;
|
||||
readonly acknowledgementMutationId: string;
|
||||
readonly expectedKind: 'credential' | 'challenge';
|
||||
readonly expectedDeliveryDigest: string;
|
||||
}
|
||||
|
||||
export interface CompactLocalOwnerDeliveryAcknowledgementResult {
|
||||
readonly status: 'inserted' | 'existing';
|
||||
readonly record: Readonly<LocalOwnerDeliveryAcknowledgementGcRecord>;
|
||||
}
|
||||
|
||||
export interface LocalOwnerDeliveryAcknowledgementGcService {
|
||||
compact(
|
||||
request: CompactLocalOwnerDeliveryAcknowledgementRequest,
|
||||
): Promise<Readonly<CompactLocalOwnerDeliveryAcknowledgementResult>>;
|
||||
}
|
||||
|
||||
export interface CreateLocalOwnerDeliveryAcknowledgementGcServiceOptions {
|
||||
readonly secretDeliveryDirectory: string;
|
||||
readonly retentionPolicy: LocalOwnerDeliveryAcknowledgementGcRetentionPolicy;
|
||||
}
|
||||
|
||||
export interface OpenLocalOwnerDeliveryAcknowledgementGcOptions
|
||||
extends LocalSqliteDatabaseOptions,
|
||||
CreateLocalOwnerDeliveryAcknowledgementGcServiceOptions {}
|
||||
|
||||
export interface LocalOwnerDeliveryAcknowledgementGcAuthority
|
||||
extends LocalOwnerDeliveryAcknowledgementGcService {
|
||||
readonly profile: LocalSqliteProfile;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export class LocalOwnerDeliveryAcknowledgementGcConfigurationError extends TypeError {
|
||||
readonly code =
|
||||
'LOCAL_OWNER_DELIVERY_ACKNOWLEDGEMENT_GC_CONFIGURATION_INVALID';
|
||||
|
||||
constructor(message: string, readonly cause?: unknown) {
|
||||
super(
|
||||
`Local Owner delivery acknowledgement GC configuration is invalid: ${message}`,
|
||||
);
|
||||
this.name = 'LocalOwnerDeliveryAcknowledgementGcConfigurationError';
|
||||
}
|
||||
}
|
||||
|
||||
function exactKeys(value: object, expected: readonly string[]): boolean {
|
||||
const actual = Object.keys(value).sort();
|
||||
const canonical = [...expected].sort();
|
||||
return (
|
||||
actual.length === canonical.length &&
|
||||
actual.every((key, index) => key === canonical[index])
|
||||
);
|
||||
}
|
||||
|
||||
function request(
|
||||
value: CompactLocalOwnerDeliveryAcknowledgementRequest,
|
||||
): Readonly<CompactLocalOwnerDeliveryAcknowledgementRequest> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, [
|
||||
'mutationId',
|
||||
'requestId',
|
||||
'acknowledgementMutationId',
|
||||
'expectedKind',
|
||||
'expectedDeliveryDigest',
|
||||
]) ||
|
||||
!UUID_V4_PATTERN.test(value.mutationId) ||
|
||||
!UUID_V4_PATTERN.test(value.acknowledgementMutationId) ||
|
||||
value.mutationId === value.acknowledgementMutationId ||
|
||||
!REQUEST_ID_PATTERN.test(value.requestId) ||
|
||||
(value.expectedKind !== 'credential' &&
|
||||
value.expectedKind !== 'challenge') ||
|
||||
!DIGEST_PATTERN.test(value.expectedDeliveryDigest)
|
||||
) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'request shape is invalid',
|
||||
);
|
||||
}
|
||||
return Object.freeze({ ...value });
|
||||
}
|
||||
|
||||
function options(
|
||||
value: CreateLocalOwnerDeliveryAcknowledgementGcServiceOptions,
|
||||
): Readonly<CreateLocalOwnerDeliveryAcknowledgementGcServiceOptions> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, ['secretDeliveryDirectory', 'retentionPolicy'])
|
||||
) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'options shape is invalid',
|
||||
);
|
||||
}
|
||||
try {
|
||||
localOwnerDeliveryAcknowledgementGcRetentionPolicyDigest(
|
||||
value.retentionPolicy,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'retentionPolicy is invalid',
|
||||
error,
|
||||
);
|
||||
}
|
||||
return Object.freeze({
|
||||
secretDeliveryDirectory: value.secretDeliveryDirectory,
|
||||
retentionPolicy: Object.freeze({ ...value.retentionPolicy }),
|
||||
});
|
||||
}
|
||||
|
||||
function sameExisting(
|
||||
record: Readonly<LocalOwnerDeliveryAcknowledgementGcRecord>,
|
||||
value: Readonly<CompactLocalOwnerDeliveryAcknowledgementRequest>,
|
||||
retentionPolicy: LocalOwnerDeliveryAcknowledgementGcRetentionPolicy,
|
||||
): boolean {
|
||||
return (
|
||||
record.mutationId === value.mutationId &&
|
||||
record.requestId === value.requestId &&
|
||||
record.acknowledgementMutationId === value.acknowledgementMutationId &&
|
||||
record.acknowledgementKind === value.expectedKind &&
|
||||
record.deliveryDigest === value.expectedDeliveryDigest &&
|
||||
record.retentionPolicyDigest ===
|
||||
localOwnerDeliveryAcknowledgementGcRetentionPolicyDigest(retentionPolicy)
|
||||
);
|
||||
}
|
||||
|
||||
function audit(eventId: string, requestId: string, occurredAtMs: number) {
|
||||
return Object.freeze({
|
||||
eventId,
|
||||
requestId,
|
||||
operationId: 'owner.delivery_acknowledgement.gc',
|
||||
projectId: null,
|
||||
subject: Object.freeze({
|
||||
type: 'system' as const,
|
||||
id: 'owner-acknowledgement-gc',
|
||||
}),
|
||||
authenticationId: 'local-owner-console',
|
||||
outcome: 'allowed' as const,
|
||||
reasons: Object.freeze(['delivery_acknowledgement_gc']),
|
||||
fence: null,
|
||||
occurredAtMs,
|
||||
});
|
||||
}
|
||||
|
||||
export function createLocalOwnerDeliveryAcknowledgementGcService(
|
||||
repository: LocalOwnerDeliveryAcknowledgementGcRepository,
|
||||
candidateOptions: CreateLocalOwnerDeliveryAcknowledgementGcServiceOptions,
|
||||
): LocalOwnerDeliveryAcknowledgementGcService {
|
||||
if (
|
||||
!repository ||
|
||||
typeof repository.resolveByAcknowledgement !== 'function' ||
|
||||
typeof repository.compact !== 'function'
|
||||
) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'repository boundary is invalid',
|
||||
);
|
||||
}
|
||||
const settings = options(candidateOptions);
|
||||
const delivery = new FileLocalOwnerBootstrapSecretDelivery(
|
||||
settings.secretDeliveryDirectory,
|
||||
);
|
||||
return Object.freeze({
|
||||
async compact(candidate: CompactLocalOwnerDeliveryAcknowledgementRequest) {
|
||||
const command = request(candidate);
|
||||
const existing = await repository.resolveByAcknowledgement(
|
||||
command.acknowledgementMutationId,
|
||||
);
|
||||
if (existing) {
|
||||
if (!sameExisting(existing, command, settings.retentionPolicy)) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'request conflicts with the durable GC record',
|
||||
);
|
||||
}
|
||||
return Object.freeze({ status: 'existing' as const, record: existing });
|
||||
}
|
||||
const evidence = delivery.inspectBridgeClear(
|
||||
command.expectedKind,
|
||||
command.acknowledgementMutationId,
|
||||
);
|
||||
return repository.compact({
|
||||
...command,
|
||||
bridgeClearEvidence: evidence,
|
||||
retentionPolicy: settings.retentionPolicy,
|
||||
compactedAtMs: evidence.inspectedAtMs,
|
||||
audit: audit(
|
||||
command.mutationId,
|
||||
command.requestId,
|
||||
evidence.inspectedAtMs,
|
||||
),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function openLocalOwnerDeliveryAcknowledgementGc(
|
||||
candidate: OpenLocalOwnerDeliveryAcknowledgementGcOptions,
|
||||
): Promise<LocalOwnerDeliveryAcknowledgementGcAuthority> {
|
||||
if (
|
||||
!candidate ||
|
||||
typeof candidate !== 'object' ||
|
||||
Array.isArray(candidate) ||
|
||||
!exactKeys(candidate, [
|
||||
'databasePath',
|
||||
'profile',
|
||||
'secretDeliveryDirectory',
|
||||
'retentionPolicy',
|
||||
...(candidate.busyTimeoutMs === undefined ? [] : ['busyTimeoutMs']),
|
||||
])
|
||||
) {
|
||||
throw new LocalOwnerDeliveryAcknowledgementGcConfigurationError(
|
||||
'open options are invalid',
|
||||
);
|
||||
}
|
||||
const settings = options({
|
||||
secretDeliveryDirectory: candidate.secretDeliveryDirectory,
|
||||
retentionPolicy: candidate.retentionPolicy,
|
||||
});
|
||||
const databaseOptions: LocalSqliteDatabaseOptions = {
|
||||
databasePath: candidate.databasePath,
|
||||
profile: candidate.profile,
|
||||
...(candidate.busyTimeoutMs === undefined
|
||||
? {}
|
||||
: { busyTimeoutMs: candidate.busyTimeoutMs }),
|
||||
};
|
||||
let database: LocalSqliteAcknowledgementGcDatabase | null = null;
|
||||
try {
|
||||
database = await openLocalSqliteAcknowledgementGcDatabase(databaseOptions);
|
||||
const service = createLocalOwnerDeliveryAcknowledgementGcService(
|
||||
database.acknowledgementGc,
|
||||
settings,
|
||||
);
|
||||
const owned = database;
|
||||
let closePromise: Promise<void> | undefined;
|
||||
return Object.freeze({
|
||||
profile: owned.profile,
|
||||
compact(request: CompactLocalOwnerDeliveryAcknowledgementRequest) {
|
||||
return service.compact(request);
|
||||
},
|
||||
close() {
|
||||
return (closePromise ??= owned.close());
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await database?.close().catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
LocalOwnerPepperKeyringFileProvider,
|
||||
type LocalOwnerPepperKeyMaterial,
|
||||
} from '@qinglong/local-owner-console/pepper-custody';
|
||||
import {
|
||||
destroyLocalOwnerPepperKey,
|
||||
type DestroyLocalOwnerPepperKeyResult,
|
||||
} from '@qinglong/local-owner-console/pepper-custody/destructive';
|
||||
import {
|
||||
openLocalSqlitePepperGcDatabase,
|
||||
type LocalSqliteDatabaseOptions,
|
||||
type LocalSqlitePepperGcDatabase,
|
||||
type LocalSqliteProfile,
|
||||
} from '@qinglong/local-sqlite/pepper-gc';
|
||||
import { assertApiCredentialPepperKeyId } from '@qinglong/runtime-core/api-credential';
|
||||
import {
|
||||
type LocalOwnerPepperActivationRecord,
|
||||
type LocalOwnerPepperKeyRecord,
|
||||
type LocalOwnerPepperReferenceRepository,
|
||||
} from '@qinglong/runtime-core/local-owner-pepper';
|
||||
import {
|
||||
localOwnerPepperMaterialGcRetentionPolicyDigest,
|
||||
type LocalOwnerPepperMaterialGcRecord,
|
||||
type LocalOwnerPepperMaterialGcRepository,
|
||||
type LocalOwnerPepperMaterialGcRetentionPolicy,
|
||||
} from '@qinglong/runtime-core/local-owner-pepper-material-gc';
|
||||
|
||||
const UUID_V4_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||
const REQUEST_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
||||
|
||||
type LocalOwnerPepperGcCatalog = Pick<
|
||||
LocalOwnerPepperReferenceRepository,
|
||||
'resolveKey' | 'resolveActive'
|
||||
>;
|
||||
|
||||
export interface CollectLocalOwnerPepperMaterialRequest {
|
||||
readonly prepareMutationId: string;
|
||||
readonly prepareRequestId: string;
|
||||
readonly completeMutationId: string;
|
||||
readonly completeRequestId: string;
|
||||
readonly pepperKeyId: string;
|
||||
}
|
||||
|
||||
export interface CollectLocalOwnerPepperMaterialResult {
|
||||
readonly status: 'inserted' | 'existing';
|
||||
readonly record: Readonly<LocalOwnerPepperMaterialGcRecord>;
|
||||
readonly runtimeMaterial: Readonly<DestroyLocalOwnerPepperKeyResult>;
|
||||
readonly backupMaterial: Readonly<DestroyLocalOwnerPepperKeyResult>;
|
||||
}
|
||||
|
||||
export interface LocalOwnerPepperMaterialGcService {
|
||||
collect(
|
||||
request: CollectLocalOwnerPepperMaterialRequest,
|
||||
): Promise<Readonly<CollectLocalOwnerPepperMaterialResult>>;
|
||||
}
|
||||
|
||||
export interface CreateLocalOwnerPepperMaterialGcServiceOptions {
|
||||
readonly keyringDirectory: string;
|
||||
readonly backupDirectory: string;
|
||||
readonly retentionPolicy: LocalOwnerPepperMaterialGcRetentionPolicy;
|
||||
}
|
||||
|
||||
export interface OpenLocalOwnerPepperMaterialGcOptions
|
||||
extends LocalSqliteDatabaseOptions,
|
||||
CreateLocalOwnerPepperMaterialGcServiceOptions {}
|
||||
|
||||
export interface LocalOwnerPepperMaterialGcAuthority
|
||||
extends LocalOwnerPepperMaterialGcService {
|
||||
readonly profile: LocalSqliteProfile;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export class LocalOwnerPepperMaterialGcConfigurationError extends TypeError {
|
||||
readonly code = 'LOCAL_OWNER_PEPPER_MATERIAL_GC_CONFIGURATION_INVALID';
|
||||
|
||||
constructor(message: string, readonly cause?: unknown) {
|
||||
super(
|
||||
`Local Owner pepper material GC configuration is invalid: ${message}`,
|
||||
);
|
||||
this.name = 'LocalOwnerPepperMaterialGcConfigurationError';
|
||||
}
|
||||
}
|
||||
|
||||
export class LocalOwnerPepperMaterialGcMaterialUnavailableError extends Error {
|
||||
readonly code = 'LOCAL_OWNER_PEPPER_MATERIAL_GC_MATERIAL_UNAVAILABLE';
|
||||
|
||||
constructor() {
|
||||
super(
|
||||
'Local Owner pepper material or its independent backup is unavailable',
|
||||
);
|
||||
this.name = 'LocalOwnerPepperMaterialGcMaterialUnavailableError';
|
||||
}
|
||||
}
|
||||
|
||||
function exactKeys(value: object, expected: readonly string[]): boolean {
|
||||
const actual = Object.keys(value).sort();
|
||||
const canonical = [...expected].sort();
|
||||
return (
|
||||
actual.length === canonical.length &&
|
||||
actual.every((key, index) => key === canonical[index])
|
||||
);
|
||||
}
|
||||
|
||||
function request(
|
||||
value: CollectLocalOwnerPepperMaterialRequest,
|
||||
): Readonly<CollectLocalOwnerPepperMaterialRequest> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, [
|
||||
'prepareMutationId',
|
||||
'prepareRequestId',
|
||||
'completeMutationId',
|
||||
'completeRequestId',
|
||||
'pepperKeyId',
|
||||
]) ||
|
||||
!UUID_V4_PATTERN.test(value.prepareMutationId) ||
|
||||
!UUID_V4_PATTERN.test(value.completeMutationId) ||
|
||||
value.prepareMutationId === value.completeMutationId ||
|
||||
!REQUEST_ID_PATTERN.test(value.prepareRequestId) ||
|
||||
!REQUEST_ID_PATTERN.test(value.completeRequestId)
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'request shape is invalid',
|
||||
);
|
||||
}
|
||||
try {
|
||||
assertApiCredentialPepperKeyId(value.pepperKeyId);
|
||||
} catch {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'pepperKeyId is invalid',
|
||||
);
|
||||
}
|
||||
return Object.freeze({ ...value });
|
||||
}
|
||||
|
||||
function currentTime(): number {
|
||||
const value = Date.now();
|
||||
if (!Number.isSafeInteger(value) || value < 0) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'trusted clock is invalid',
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function options(
|
||||
value: CreateLocalOwnerPepperMaterialGcServiceOptions,
|
||||
): Readonly<CreateLocalOwnerPepperMaterialGcServiceOptions> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
!exactKeys(value, [
|
||||
'keyringDirectory',
|
||||
'backupDirectory',
|
||||
'retentionPolicy',
|
||||
]) ||
|
||||
typeof value.keyringDirectory !== 'string' ||
|
||||
typeof value.backupDirectory !== 'string' ||
|
||||
!path.isAbsolute(value.keyringDirectory) ||
|
||||
!path.isAbsolute(value.backupDirectory) ||
|
||||
path.normalize(value.keyringDirectory) !== value.keyringDirectory ||
|
||||
path.normalize(value.backupDirectory) !== value.backupDirectory ||
|
||||
value.keyringDirectory === value.backupDirectory
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'options shape is invalid',
|
||||
);
|
||||
}
|
||||
try {
|
||||
localOwnerPepperMaterialGcRetentionPolicyDigest(value.retentionPolicy);
|
||||
} catch (error) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'retentionPolicy is invalid',
|
||||
error,
|
||||
);
|
||||
}
|
||||
return Object.freeze({
|
||||
keyringDirectory: value.keyringDirectory,
|
||||
backupDirectory: value.backupDirectory,
|
||||
retentionPolicy: Object.freeze({ ...value.retentionPolicy }),
|
||||
});
|
||||
}
|
||||
|
||||
function audit(
|
||||
eventId: string,
|
||||
requestId: string,
|
||||
operation: 'prepare' | 'complete',
|
||||
occurredAtMs: number,
|
||||
) {
|
||||
return Object.freeze({
|
||||
eventId,
|
||||
requestId,
|
||||
operationId: `owner.pepper.material_gc.${operation}`,
|
||||
projectId: null,
|
||||
subject: Object.freeze({ type: 'system' as const, id: 'owner-pepper-gc' }),
|
||||
authenticationId: 'local-owner-console',
|
||||
outcome: 'allowed' as const,
|
||||
reasons: Object.freeze(['pepper_material_gc']),
|
||||
fence: null,
|
||||
occurredAtMs,
|
||||
});
|
||||
}
|
||||
|
||||
function materialMatches(
|
||||
material: Readonly<LocalOwnerPepperKeyMaterial> | null,
|
||||
expectedDigest: string | undefined,
|
||||
): material is Readonly<LocalOwnerPepperKeyMaterial> {
|
||||
return (
|
||||
!!material && !!expectedDigest && material.summary.digest === expectedDigest
|
||||
);
|
||||
}
|
||||
|
||||
async function verifiedActive(
|
||||
catalog: LocalOwnerPepperGcCatalog,
|
||||
runtime: LocalOwnerPepperKeyringFileProvider,
|
||||
backup: LocalOwnerPepperKeyringFileProvider,
|
||||
): Promise<Readonly<LocalOwnerPepperActivationRecord>> {
|
||||
const active = await catalog.resolveActive();
|
||||
if (!active) {
|
||||
throw new LocalOwnerPepperMaterialGcMaterialUnavailableError();
|
||||
}
|
||||
const key = await catalog.resolveKey(active.activePepperKeyId);
|
||||
if (
|
||||
!key ||
|
||||
key.state !== 'active' ||
|
||||
key.materialDigest !== active.materialDigest ||
|
||||
key.backupDigest !== active.backupDigest ||
|
||||
!materialMatches(
|
||||
runtime.resolve(active.activePepperKeyId),
|
||||
active.materialDigest,
|
||||
) ||
|
||||
!materialMatches(
|
||||
backup.resolve(active.activePepperKeyId),
|
||||
active.backupDigest,
|
||||
)
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcMaterialUnavailableError();
|
||||
}
|
||||
return active;
|
||||
}
|
||||
|
||||
function verifiedRetired(
|
||||
key: Readonly<LocalOwnerPepperKeyRecord> | null,
|
||||
runtime: LocalOwnerPepperKeyringFileProvider,
|
||||
backup: LocalOwnerPepperKeyringFileProvider,
|
||||
pepperKeyId: string,
|
||||
): Readonly<LocalOwnerPepperKeyRecord> {
|
||||
if (
|
||||
!key ||
|
||||
key.state !== 'retired' ||
|
||||
!key.materialDigest ||
|
||||
!key.backupDigest ||
|
||||
!materialMatches(runtime.resolve(pepperKeyId), key.materialDigest) ||
|
||||
!materialMatches(backup.resolve(pepperKeyId), key.backupDigest)
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcMaterialUnavailableError();
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
function combinedProof(
|
||||
runtime: Readonly<DestroyLocalOwnerPepperKeyResult>,
|
||||
backup: Readonly<DestroyLocalOwnerPepperKeyResult>,
|
||||
): string {
|
||||
return createHash('sha256')
|
||||
.update('qinglong.local-owner-pepper-material-gc-completion.v1\0', 'utf8')
|
||||
.update(runtime.destructionProofDigest, 'utf8')
|
||||
.update('\0', 'utf8')
|
||||
.update(backup.destructionProofDigest, 'utf8')
|
||||
.digest('hex');
|
||||
}
|
||||
|
||||
export function createLocalOwnerPepperMaterialGcService(
|
||||
repository: LocalOwnerPepperMaterialGcRepository,
|
||||
catalog: LocalOwnerPepperGcCatalog,
|
||||
candidateOptions: CreateLocalOwnerPepperMaterialGcServiceOptions,
|
||||
): LocalOwnerPepperMaterialGcService {
|
||||
if (
|
||||
!repository ||
|
||||
typeof repository.resolve !== 'function' ||
|
||||
typeof repository.prepare !== 'function' ||
|
||||
typeof repository.complete !== 'function' ||
|
||||
!catalog ||
|
||||
typeof catalog.resolveKey !== 'function' ||
|
||||
typeof catalog.resolveActive !== 'function'
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'repository boundary is invalid',
|
||||
);
|
||||
}
|
||||
const settings = options(candidateOptions);
|
||||
return Object.freeze({
|
||||
async collect(candidate: CollectLocalOwnerPepperMaterialRequest) {
|
||||
const command = request(candidate);
|
||||
const runtimeProvider = new LocalOwnerPepperKeyringFileProvider(
|
||||
settings.keyringDirectory,
|
||||
);
|
||||
const backupProvider = new LocalOwnerPepperKeyringFileProvider(
|
||||
settings.backupDirectory,
|
||||
);
|
||||
let record = await repository.resolve(command.prepareMutationId);
|
||||
if (!record) {
|
||||
const target = verifiedRetired(
|
||||
await catalog.resolveKey(command.pepperKeyId),
|
||||
runtimeProvider,
|
||||
backupProvider,
|
||||
command.pepperKeyId,
|
||||
);
|
||||
const active = await verifiedActive(
|
||||
catalog,
|
||||
runtimeProvider,
|
||||
backupProvider,
|
||||
);
|
||||
const preparedAtMs = currentTime();
|
||||
record = (
|
||||
await repository.prepare({
|
||||
mutationId: command.prepareMutationId,
|
||||
requestId: command.prepareRequestId,
|
||||
pepperKeyId: command.pepperKeyId,
|
||||
expectedMaterialDigest: target.materialDigest!,
|
||||
expectedBackupMaterialDigest: target.backupDigest!,
|
||||
expectedActivePepperKeyId: active.activePepperKeyId,
|
||||
expectedActiveGeneration: active.generation,
|
||||
expectedActiveMaterialDigest: active.materialDigest,
|
||||
retentionPolicy: settings.retentionPolicy,
|
||||
preparedAtMs,
|
||||
audit: audit(
|
||||
command.prepareMutationId,
|
||||
command.prepareRequestId,
|
||||
'prepare',
|
||||
preparedAtMs,
|
||||
),
|
||||
})
|
||||
).record;
|
||||
}
|
||||
if (
|
||||
record.prepareMutationId !== command.prepareMutationId ||
|
||||
record.prepareRequestId !== command.prepareRequestId ||
|
||||
record.pepperKeyId !== command.pepperKeyId ||
|
||||
record.retentionPolicyDigest !==
|
||||
localOwnerPepperMaterialGcRetentionPolicyDigest(
|
||||
settings.retentionPolicy,
|
||||
)
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'request conflicts with the durable GC record',
|
||||
);
|
||||
}
|
||||
await verifiedActive(catalog, runtimeProvider, backupProvider);
|
||||
const runtimeMaterial = destroyLocalOwnerPepperKey({
|
||||
keyringDirectory: settings.keyringDirectory,
|
||||
pepperKeyId: record.pepperKeyId,
|
||||
materialRole: 'runtime',
|
||||
expectedMaterialDigest: record.materialDigest,
|
||||
prepareMutationId: record.prepareMutationId,
|
||||
});
|
||||
const backupMaterial = destroyLocalOwnerPepperKey({
|
||||
keyringDirectory: settings.backupDirectory,
|
||||
pepperKeyId: record.pepperKeyId,
|
||||
materialRole: 'backup',
|
||||
expectedMaterialDigest: record.backupMaterialDigest,
|
||||
prepareMutationId: record.prepareMutationId,
|
||||
});
|
||||
const completedAtMs =
|
||||
record.completedAtMs ?? Math.max(currentTime(), record.preparedAtMs);
|
||||
const completion = await repository.complete({
|
||||
prepareMutationId: record.prepareMutationId,
|
||||
mutationId: command.completeMutationId,
|
||||
requestId: command.completeRequestId,
|
||||
destructionProofDigest: combinedProof(runtimeMaterial, backupMaterial),
|
||||
completedAtMs,
|
||||
audit: audit(
|
||||
command.completeMutationId,
|
||||
command.completeRequestId,
|
||||
'complete',
|
||||
completedAtMs,
|
||||
),
|
||||
});
|
||||
return Object.freeze({
|
||||
status: completion.status,
|
||||
record: completion.record,
|
||||
runtimeMaterial,
|
||||
backupMaterial,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function openLocalOwnerPepperMaterialGc(
|
||||
candidate: OpenLocalOwnerPepperMaterialGcOptions,
|
||||
): Promise<LocalOwnerPepperMaterialGcAuthority> {
|
||||
if (
|
||||
!candidate ||
|
||||
typeof candidate !== 'object' ||
|
||||
Array.isArray(candidate) ||
|
||||
!exactKeys(candidate, [
|
||||
'databasePath',
|
||||
'profile',
|
||||
'keyringDirectory',
|
||||
'backupDirectory',
|
||||
'retentionPolicy',
|
||||
...(candidate.busyTimeoutMs === undefined ? [] : ['busyTimeoutMs']),
|
||||
])
|
||||
) {
|
||||
throw new LocalOwnerPepperMaterialGcConfigurationError(
|
||||
'open options are invalid',
|
||||
);
|
||||
}
|
||||
const settings = options({
|
||||
keyringDirectory: candidate.keyringDirectory,
|
||||
backupDirectory: candidate.backupDirectory,
|
||||
retentionPolicy: candidate.retentionPolicy,
|
||||
});
|
||||
const databaseOptions: LocalSqliteDatabaseOptions = {
|
||||
databasePath: candidate.databasePath,
|
||||
profile: candidate.profile,
|
||||
...(candidate.busyTimeoutMs === undefined
|
||||
? {}
|
||||
: { busyTimeoutMs: candidate.busyTimeoutMs }),
|
||||
};
|
||||
let database: LocalSqlitePepperGcDatabase | null = null;
|
||||
try {
|
||||
database = await openLocalSqlitePepperGcDatabase(databaseOptions);
|
||||
const service = createLocalOwnerPepperMaterialGcService(
|
||||
database.materialGc,
|
||||
database.ownerPepper,
|
||||
settings,
|
||||
);
|
||||
const owned = database;
|
||||
let closePromise: Promise<void> | undefined;
|
||||
return Object.freeze({
|
||||
profile: owned.profile,
|
||||
collect(request: CollectLocalOwnerPepperMaterialRequest) {
|
||||
return service.collect(request);
|
||||
},
|
||||
close() {
|
||||
return (closePromise ??= owned.close());
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await database?.close().catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { test } = require('node:test');
|
||||
const {
|
||||
openLocalSqliteBootstrapDatabase,
|
||||
} = require('@qinglong/local-sqlite/bootstrap');
|
||||
const { migrateLocalSqlitePath } = require('@qinglong/local-sqlite/migration');
|
||||
const {
|
||||
MIN_LOCAL_OWNER_ACKNOWLEDGEMENT_AUDIT_RETENTION_MS,
|
||||
MIN_LOCAL_OWNER_ACKNOWLEDGEMENT_REPLAY_RETENTION_MS,
|
||||
} = require('@qinglong/runtime-core/local-owner-delivery-acknowledgement-gc');
|
||||
const {
|
||||
LocalOwnerDeliveryAcknowledgementGcConfigurationError,
|
||||
openLocalOwnerDeliveryAcknowledgementGc,
|
||||
} = require('../dist/security-maintenance/acknowledgementGc');
|
||||
|
||||
const NOW = 1_760_000_000_000;
|
||||
const ACK_MUTATION_ID = '00000000-0000-4000-8000-000000000e01';
|
||||
const GC_MUTATION_ID = '00000000-0000-4000-8000-000000000e02';
|
||||
const DELIVERY_DIGEST = 'd'.repeat(64);
|
||||
const POLICY = Object.freeze({
|
||||
version: 1,
|
||||
replayRetentionMs: MIN_LOCAL_OWNER_ACKNOWLEDGEMENT_REPLAY_RETENTION_MS,
|
||||
auditRetentionMs: MIN_LOCAL_OWNER_ACKNOWLEDGEMENT_AUDIT_RETENTION_MS,
|
||||
});
|
||||
const COMPACTED_AT_MS =
|
||||
NOW + MIN_LOCAL_OWNER_ACKNOWLEDGEMENT_AUDIT_RETENTION_MS + 1_000;
|
||||
|
||||
function request(overrides = {}) {
|
||||
return {
|
||||
mutationId: GC_MUTATION_ID,
|
||||
requestId: 'ack-gc-e02',
|
||||
acknowledgementMutationId: ACK_MUTATION_ID,
|
||||
expectedKind: 'credential',
|
||||
expectedDeliveryDigest: DELIVERY_DIGEST,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function fixture(t) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql3-ack-gc-e2e-'));
|
||||
const secretDeliveryDirectory = path.join(root, 'secrets');
|
||||
const databasePath = path.join(root, 'qinglong3.sqlite');
|
||||
fs.mkdirSync(secretDeliveryDirectory, { mode: 0o700 });
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const databaseOptions = { databasePath, profile: 'edge' };
|
||||
await migrateLocalSqlitePath(databaseOptions);
|
||||
const database = await openLocalSqliteBootstrapDatabase(databaseOptions);
|
||||
await database.ownerPepper.register({
|
||||
mutationId: '00000000-0000-4000-8000-000000000e91',
|
||||
pepperKeyId: 'legacy-v1',
|
||||
materialDigest: 'a'.repeat(64),
|
||||
backupDigest: 'b'.repeat(64),
|
||||
registeredAtMs: NOW - 2_000,
|
||||
});
|
||||
await database.ownerPepper.activate({
|
||||
mutationId: '00000000-0000-4000-8000-000000000e92',
|
||||
pepperKeyId: 'legacy-v1',
|
||||
expectedGeneration: 0,
|
||||
activatedAtMs: NOW - 1_000,
|
||||
});
|
||||
const subjectId = `usr_${Buffer.alloc(16, 41).toString('base64url')}`;
|
||||
const credentialId = `own_${Buffer.alloc(16, 42).toString('base64url')}`;
|
||||
await database.ownerBootstrap.provision({
|
||||
mutationId: ACK_MUTATION_ID,
|
||||
requestId: 'provision-e01',
|
||||
identity: {
|
||||
subject: { type: 'user', id: subjectId },
|
||||
status: 'active',
|
||||
version: 1,
|
||||
createdAtMs: NOW,
|
||||
updatedAtMs: NOW,
|
||||
},
|
||||
credential: {
|
||||
credentialId,
|
||||
version: 1,
|
||||
pepperKeyId: 'legacy-v1',
|
||||
state: 'active',
|
||||
subject: { type: 'user', id: subjectId },
|
||||
subjectStatus: 'active',
|
||||
secretDigest: 'c'.repeat(64),
|
||||
createdAtMs: NOW,
|
||||
notBeforeAtMs: NOW,
|
||||
expiresAtMs: NOW + 600_000,
|
||||
},
|
||||
issuer: {
|
||||
subject: { type: 'system', id: 'owner-bootstrap' },
|
||||
authenticationId: 'local-console-test',
|
||||
authenticatedAtMs: NOW - 1_000,
|
||||
expiresAtMs: NOW + 60_000,
|
||||
assurance: 'local_console',
|
||||
},
|
||||
audit: {
|
||||
eventId: ACK_MUTATION_ID,
|
||||
requestId: 'provision-e01',
|
||||
operationId: 'identity.bootstrap_provision',
|
||||
projectId: null,
|
||||
subject: { type: 'system', id: 'owner-bootstrap' },
|
||||
authenticationId: 'local-console-test',
|
||||
outcome: 'allowed',
|
||||
reasons: ['local_console_provisioning'],
|
||||
fence: null,
|
||||
occurredAtMs: NOW,
|
||||
},
|
||||
createdAtMs: NOW,
|
||||
});
|
||||
await database.ownerBootstrap.recordDeliveryAcknowledgement({
|
||||
kind: 'credential',
|
||||
mutationId: ACK_MUTATION_ID,
|
||||
requestId: 'provision-e01',
|
||||
subjectId,
|
||||
credentialId,
|
||||
factDigest: 'c'.repeat(64),
|
||||
deliveryDigest: DELIVERY_DIGEST,
|
||||
ttlMs: 600_000,
|
||||
acknowledgedAtMs: NOW + 1,
|
||||
});
|
||||
await database.close();
|
||||
return {
|
||||
databasePath,
|
||||
profile: 'edge',
|
||||
secretDeliveryDirectory,
|
||||
retentionPolicy: POLICY,
|
||||
};
|
||||
}
|
||||
|
||||
test('derives trusted bridge evidence and replays one durable compaction', async (t) => {
|
||||
const options = await fixture(t);
|
||||
t.mock.method(Date, 'now', () => COMPACTED_AT_MS);
|
||||
const authority = await openLocalOwnerDeliveryAcknowledgementGc(options);
|
||||
t.after(() => authority.close());
|
||||
const inserted = await authority.compact(request());
|
||||
assert.equal(inserted.status, 'inserted');
|
||||
assert.equal(inserted.record.compactedAtMs, COMPACTED_AT_MS);
|
||||
assert.equal(inserted.record.bridgeClearEvidenceDigest.length, 64);
|
||||
const replay = await authority.compact(request());
|
||||
assert.equal(replay.status, 'existing');
|
||||
assert.deepEqual(replay.record, inserted.record);
|
||||
});
|
||||
|
||||
test('rejects a live file bridge and caller-controlled time', async (t) => {
|
||||
const options = await fixture(t);
|
||||
t.mock.method(Date, 'now', () => COMPACTED_AT_MS);
|
||||
fs.writeFileSync(
|
||||
path.join(
|
||||
options.secretDeliveryDirectory,
|
||||
`credential-${ACK_MUTATION_ID}.ready.json`,
|
||||
),
|
||||
'{}',
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
const authority = await openLocalOwnerDeliveryAcknowledgementGc(options);
|
||||
t.after(() => authority.close());
|
||||
await assert.rejects(authority.compact(request()), /bridge is not clear/);
|
||||
await assert.rejects(
|
||||
authority.compact({ ...request(), compactedAtMs: COMPACTED_AT_MS }),
|
||||
LocalOwnerDeliveryAcknowledgementGcConfigurationError,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,334 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
const { test } = require('node:test');
|
||||
const {
|
||||
LocalOwnerGcCliConfigurationError,
|
||||
createLocalOwnerGcCommandRunner,
|
||||
} = require('@qinglong/local-owner-maintenance/command');
|
||||
const {
|
||||
pluginPackagePromptOutputArtifactRetentionPolicyDigest,
|
||||
} = require('../../ql3-ai/dist/prompt-output/pluginPackagePromptOutputArtifact');
|
||||
|
||||
function privateCommand(t, value) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql3-owner-gc-cli-'));
|
||||
const filePath = path.join(root, 'command.json');
|
||||
fs.writeFileSync(filePath, `${JSON.stringify(value)}\n`, { mode: 0o600 });
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
return filePath;
|
||||
}
|
||||
|
||||
function acknowledgementCommand(overrides = {}) {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
operation: 'owner.delivery-acknowledgement.compact',
|
||||
options: {
|
||||
databasePath: '/private/ql3.sqlite',
|
||||
profile: 'edge',
|
||||
secretDeliveryDirectory: '/private/secrets',
|
||||
retentionPolicy: {
|
||||
version: 1,
|
||||
replayRetentionMs: 2_592_000_000,
|
||||
auditRetentionMs: 2_592_000_000,
|
||||
},
|
||||
},
|
||||
request: {
|
||||
mutationId: '00000000-0000-4000-8000-000000000a02',
|
||||
requestId: 'gc-a02',
|
||||
acknowledgementMutationId: '00000000-0000-4000-8000-000000000a01',
|
||||
expectedKind: 'credential',
|
||||
expectedDeliveryDigest: 'd'.repeat(64),
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function dependencies(state) {
|
||||
return {
|
||||
async openAcknowledgementGc(options) {
|
||||
state.ackOptions = options;
|
||||
return {
|
||||
profile: 'edge',
|
||||
async compact(request) {
|
||||
state.ackRequest = request;
|
||||
return {
|
||||
status: 'inserted',
|
||||
record: {
|
||||
mutationId: request.mutationId,
|
||||
requestId: request.requestId,
|
||||
acknowledgementMutationId: request.acknowledgementMutationId,
|
||||
acknowledgementKind: request.expectedKind,
|
||||
deliveryDigest: request.expectedDeliveryDigest,
|
||||
acknowledgedAtMs: 100,
|
||||
acknowledgementSemanticDigest: 'a'.repeat(64),
|
||||
bridgeClearEvidenceDigest: 'b'.repeat(64),
|
||||
retentionPolicy: options.retentionPolicy,
|
||||
retentionPolicyDigest: 'c'.repeat(64),
|
||||
retentionEligibleAtMs: 200,
|
||||
compactedAtMs: 300,
|
||||
},
|
||||
};
|
||||
},
|
||||
async close() {
|
||||
state.ackClosed = true;
|
||||
},
|
||||
};
|
||||
},
|
||||
async openPepperMaterialGc(options) {
|
||||
state.pepperOptions = options;
|
||||
return {
|
||||
profile: 'standalone',
|
||||
async collect(request) {
|
||||
state.pepperRequest = request;
|
||||
return {
|
||||
status: 'existing',
|
||||
record: {
|
||||
prepareMutationId: request.prepareMutationId,
|
||||
prepareRequestId: request.prepareRequestId,
|
||||
pepperKeyId: request.pepperKeyId,
|
||||
materialDigest: 'a'.repeat(64),
|
||||
backupMaterialDigest: 'b'.repeat(64),
|
||||
activePepperKeyId: 'active-v2',
|
||||
activeGeneration: 2,
|
||||
activeMaterialDigest: 'c'.repeat(64),
|
||||
retentionPolicy: options.retentionPolicy,
|
||||
retentionPolicyDigest: 'd'.repeat(64),
|
||||
referencesInspectedAtMs: 100,
|
||||
retentionEligibleAtMs: 200,
|
||||
preparedAtMs: 300,
|
||||
state: 'completed',
|
||||
completeMutationId: request.completeMutationId,
|
||||
completeRequestId: request.completeRequestId,
|
||||
destructionProofDigest: 'e'.repeat(64),
|
||||
completedAtMs: 400,
|
||||
},
|
||||
runtimeMaterial: {},
|
||||
backupMaterial: {},
|
||||
};
|
||||
},
|
||||
async close() {
|
||||
state.pepperClosed = true;
|
||||
},
|
||||
};
|
||||
},
|
||||
async openPromptOutputGc(options) {
|
||||
state.promptOptions = options;
|
||||
return {
|
||||
profile: options.profile,
|
||||
async collect() {
|
||||
state.promptCollected = true;
|
||||
return {
|
||||
scanned: 3,
|
||||
tombstoned: 2,
|
||||
skipped: 1,
|
||||
hasMore: false,
|
||||
};
|
||||
},
|
||||
async close() {
|
||||
state.promptClosed = true;
|
||||
},
|
||||
};
|
||||
},
|
||||
async openPromptOutputKeyRetirement(options) {
|
||||
state.promptKeyOptions = options;
|
||||
return {
|
||||
profile: options.profile,
|
||||
async retire(request) {
|
||||
state.promptKeyRequest = request;
|
||||
return {
|
||||
status: 'completed',
|
||||
keyId: request.keyId,
|
||||
retirementId: request.retirementId,
|
||||
preparationDigest: 'e'.repeat(64),
|
||||
completionDigest: 'f'.repeat(64),
|
||||
completedAtMs: 500,
|
||||
};
|
||||
},
|
||||
async close() {
|
||||
state.promptKeyClosed = true;
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('runs acknowledgement compaction from one private durable command file', async (t) => {
|
||||
const state = {};
|
||||
const command = acknowledgementCommand();
|
||||
const result = await createLocalOwnerGcCommandRunner(dependencies(state)).run(
|
||||
privateCommand(t, command),
|
||||
);
|
||||
assert.deepEqual(result, {
|
||||
schemaVersion: 1,
|
||||
operation: command.operation,
|
||||
status: 'inserted',
|
||||
gcMutationId: command.request.mutationId,
|
||||
acknowledgementMutationId: command.request.acknowledgementMutationId,
|
||||
acknowledgementKind: 'credential',
|
||||
retentionEligibleAtMs: 200,
|
||||
compactedAtMs: 300,
|
||||
});
|
||||
assert.deepEqual(state.ackOptions, command.options);
|
||||
assert.deepEqual(state.ackRequest, command.request);
|
||||
assert.equal(state.ackClosed, true);
|
||||
});
|
||||
|
||||
test('runs pepper material collection without exposing destruction digests', async (t) => {
|
||||
const state = {};
|
||||
const command = {
|
||||
schemaVersion: 1,
|
||||
operation: 'owner.pepper-material.collect',
|
||||
options: {
|
||||
databasePath: '/private/ql3.sqlite',
|
||||
profile: 'standalone',
|
||||
keyringDirectory: '/private/keyring',
|
||||
backupDirectory: '/backup/keyring',
|
||||
retentionPolicy: {
|
||||
version: 1,
|
||||
acknowledgementRetentionMs: 604_800_000,
|
||||
auditRetentionMs: 2_592_000_000,
|
||||
backupRetentionMs: 2_592_000_000,
|
||||
},
|
||||
},
|
||||
request: {
|
||||
prepareMutationId: '00000000-0000-4000-8000-000000000b01',
|
||||
prepareRequestId: 'gc-b01',
|
||||
completeMutationId: '00000000-0000-4000-8000-000000000b02',
|
||||
completeRequestId: 'gc-b02',
|
||||
pepperKeyId: 'retired-v1',
|
||||
},
|
||||
};
|
||||
const result = await createLocalOwnerGcCommandRunner(dependencies(state)).run(
|
||||
privateCommand(t, command),
|
||||
);
|
||||
assert.deepEqual(result, {
|
||||
schemaVersion: 1,
|
||||
operation: command.operation,
|
||||
status: 'existing',
|
||||
prepareMutationId: command.request.prepareMutationId,
|
||||
completeMutationId: command.request.completeMutationId,
|
||||
pepperKeyId: command.request.pepperKeyId,
|
||||
state: 'completed',
|
||||
completedAtMs: 400,
|
||||
});
|
||||
assert.equal('destructionProofDigest' in result, false);
|
||||
assert.equal(state.pepperClosed, true);
|
||||
});
|
||||
|
||||
test('runs one bounded Prompt output collection without returning policy data', async (t) => {
|
||||
const state = {};
|
||||
const policy = { revision: 'retention-v1', retentionMs: 3_600_000 };
|
||||
const command = {
|
||||
schemaVersion: 1,
|
||||
operation: 'owner.prompt-output.collect',
|
||||
options: {
|
||||
databasePath: '/private/ql3.sqlite',
|
||||
profile: 'edge',
|
||||
limit: 4,
|
||||
retentionPolicyCatalog: {
|
||||
schemaVersion: 1,
|
||||
policies: [
|
||||
{
|
||||
projectId: 'project-a',
|
||||
policy,
|
||||
policyDigest:
|
||||
pluginPackagePromptOutputArtifactRetentionPolicyDigest(policy),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
request: {},
|
||||
};
|
||||
const result = await createLocalOwnerGcCommandRunner(dependencies(state)).run(
|
||||
privateCommand(t, command),
|
||||
);
|
||||
assert.deepEqual(result, {
|
||||
schemaVersion: 1,
|
||||
operation: command.operation,
|
||||
scanned: 3,
|
||||
tombstoned: 2,
|
||||
skipped: 1,
|
||||
hasMore: false,
|
||||
});
|
||||
assert.deepEqual(state.promptOptions, command.options);
|
||||
assert.equal(state.promptCollected, true);
|
||||
assert.equal(state.promptClosed, true);
|
||||
assert.equal('retentionPolicyCatalog' in result, false);
|
||||
});
|
||||
|
||||
test('retires one Prompt output key without returning key material', async (t) => {
|
||||
const state = {};
|
||||
const command = {
|
||||
schemaVersion: 1,
|
||||
operation: 'owner.prompt-output-key.retire',
|
||||
options: {
|
||||
databasePath: '/private/ql3.sqlite',
|
||||
profile: 'edge',
|
||||
keyringPath: '/private/prompt-output-keyring.json',
|
||||
},
|
||||
request: {
|
||||
keyId: 'qlpo-retired',
|
||||
retirementId: 'retirement-a',
|
||||
requestId: 'request-a',
|
||||
mutationId: 'mutation-a',
|
||||
},
|
||||
};
|
||||
const result = await createLocalOwnerGcCommandRunner(dependencies(state)).run(
|
||||
privateCommand(t, command),
|
||||
);
|
||||
assert.deepEqual(result, {
|
||||
schemaVersion: 1,
|
||||
operation: command.operation,
|
||||
status: 'completed',
|
||||
keyId: command.request.keyId,
|
||||
retirementId: command.request.retirementId,
|
||||
preparationDigest: 'e'.repeat(64),
|
||||
completionDigest: 'f'.repeat(64),
|
||||
completedAtMs: 500,
|
||||
});
|
||||
assert.deepEqual(state.promptKeyOptions, command.options);
|
||||
assert.deepEqual(state.promptKeyRequest, command.request);
|
||||
assert.equal(state.promptKeyClosed, true);
|
||||
assert.equal(JSON.stringify(result).includes('material'), false);
|
||||
});
|
||||
|
||||
test('rejects widened or non-private command files before opening authority', async (t) => {
|
||||
const state = {};
|
||||
const runner = createLocalOwnerGcCommandRunner(dependencies(state));
|
||||
const widened = privateCommand(t, acknowledgementCommand({ now: 1 }));
|
||||
await assert.rejects(runner.run(widened), LocalOwnerGcCliConfigurationError);
|
||||
const broad = privateCommand(t, acknowledgementCommand());
|
||||
fs.chmodSync(broad, 0o644);
|
||||
await assert.rejects(runner.run(broad), LocalOwnerGcCliConfigurationError);
|
||||
assert.equal(state.ackOptions, undefined);
|
||||
assert.equal(state.pepperOptions, undefined);
|
||||
assert.equal(state.promptOptions, undefined);
|
||||
});
|
||||
|
||||
test('binary has a bounded command-file-only interface', () => {
|
||||
const help = spawnSync(
|
||||
process.execPath,
|
||||
[path.join(__dirname, '../dist/cli.js'), '--help'],
|
||||
{
|
||||
encoding: 'utf8',
|
||||
},
|
||||
);
|
||||
assert.equal(help.status, 0);
|
||||
assert.match(help.stdout, /^Usage: ql3-owner-gc run --command-file /);
|
||||
assert.equal(help.stderr, '');
|
||||
const invalid = spawnSync(
|
||||
process.execPath,
|
||||
[path.join(__dirname, '../dist/cli.js'), 'run'],
|
||||
{
|
||||
encoding: 'utf8',
|
||||
},
|
||||
);
|
||||
assert.equal(invalid.status, 64);
|
||||
assert.equal(invalid.stdout, '');
|
||||
assert.equal(
|
||||
JSON.parse(invalid.stderr).code,
|
||||
'LOCAL_OWNER_GC_CLI_USAGE_INVALID',
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,293 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const { test } = require('node:test');
|
||||
const {
|
||||
LocalOwnerPepperKeyringFileProvider,
|
||||
localOwnerPepperKeyPath,
|
||||
provisionLocalOwnerPepperKey,
|
||||
} = require('@qinglong/local-owner-console/pepper-custody');
|
||||
const {
|
||||
destroyLocalOwnerPepperKey,
|
||||
} = require('@qinglong/local-owner-console/pepper-custody/destructive');
|
||||
const {
|
||||
openLocalSqlitePepperGcDatabase,
|
||||
} = require('@qinglong/local-sqlite/pepper-gc');
|
||||
const { migrateLocalSqlitePath } = require('@qinglong/local-sqlite/migration');
|
||||
const {
|
||||
MIN_LOCAL_OWNER_PEPPER_ACK_RETENTION_MS,
|
||||
MIN_LOCAL_OWNER_PEPPER_AUDIT_RETENTION_MS,
|
||||
MIN_LOCAL_OWNER_PEPPER_BACKUP_RETENTION_MS,
|
||||
} = require('@qinglong/runtime-core/local-owner-pepper-material-gc');
|
||||
const {
|
||||
LocalOwnerPepperMaterialGcMaterialUnavailableError,
|
||||
openLocalOwnerPepperMaterialGc,
|
||||
} = require('../dist/security-maintenance/pepperGc');
|
||||
|
||||
const RETIRED_KEY_ID = 'owner-key-retired';
|
||||
const ACTIVE_KEY_ID = 'owner-key-active';
|
||||
const REQUESTED_AT_MS = 3_000_000_000;
|
||||
|
||||
function policy() {
|
||||
return {
|
||||
version: 1,
|
||||
acknowledgementRetentionMs: MIN_LOCAL_OWNER_PEPPER_ACK_RETENTION_MS,
|
||||
auditRetentionMs: MIN_LOCAL_OWNER_PEPPER_AUDIT_RETENTION_MS,
|
||||
backupRetentionMs: MIN_LOCAL_OWNER_PEPPER_BACKUP_RETENTION_MS,
|
||||
};
|
||||
}
|
||||
|
||||
function request() {
|
||||
return {
|
||||
prepareMutationId: '00000000-0000-4000-8000-000000000801',
|
||||
prepareRequestId: 'pepper-gc-prepare',
|
||||
completeMutationId: '00000000-0000-4000-8000-000000000802',
|
||||
completeRequestId: 'pepper-gc-complete',
|
||||
pepperKeyId: RETIRED_KEY_ID,
|
||||
};
|
||||
}
|
||||
|
||||
function audit(eventId, requestId, operation) {
|
||||
return {
|
||||
eventId,
|
||||
requestId,
|
||||
operationId: `owner.pepper.material_gc.${operation}`,
|
||||
projectId: null,
|
||||
subject: { type: 'system', id: 'owner-pepper-gc' },
|
||||
authenticationId: 'local-owner-console',
|
||||
outcome: 'allowed',
|
||||
reasons: ['pepper_material_gc'],
|
||||
fence: null,
|
||||
occurredAtMs: REQUESTED_AT_MS,
|
||||
};
|
||||
}
|
||||
|
||||
async function fixture(t) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql3-pepper-gc-e2e-'));
|
||||
const keyringDirectory = path.join(root, 'keyring');
|
||||
const backupDirectory = path.join(root, 'backup');
|
||||
const databasePath = path.join(root, 'qinglong3.sqlite');
|
||||
fs.mkdirSync(keyringDirectory, { mode: 0o700 });
|
||||
fs.mkdirSync(backupDirectory, { mode: 0o700 });
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const retiredRuntime = provisionLocalOwnerPepperKey({
|
||||
keyringDirectory,
|
||||
pepperKeyId: RETIRED_KEY_ID,
|
||||
randomBytes: () => Buffer.alloc(32, 41),
|
||||
});
|
||||
const retiredBackup = provisionLocalOwnerPepperKey({
|
||||
keyringDirectory: backupDirectory,
|
||||
pepperKeyId: RETIRED_KEY_ID,
|
||||
randomBytes: () => Buffer.alloc(32, 41),
|
||||
});
|
||||
const activeRuntime = provisionLocalOwnerPepperKey({
|
||||
keyringDirectory,
|
||||
pepperKeyId: ACTIVE_KEY_ID,
|
||||
randomBytes: () => Buffer.alloc(32, 43),
|
||||
});
|
||||
const activeBackup = provisionLocalOwnerPepperKey({
|
||||
keyringDirectory: backupDirectory,
|
||||
pepperKeyId: ACTIVE_KEY_ID,
|
||||
randomBytes: () => Buffer.alloc(32, 43),
|
||||
});
|
||||
await migrateLocalSqlitePath({ databasePath, profile: 'edge' });
|
||||
const client = new DatabaseSync(databasePath);
|
||||
client.exec('PRAGMA foreign_keys = ON');
|
||||
client
|
||||
.prepare(
|
||||
`INSERT INTO "QingLong3LocalOwnerPepperKeys" (
|
||||
pepper_key_id, material_digest, backup_digest, state, version,
|
||||
register_mutation_id, activate_mutation_id, retire_mutation_id,
|
||||
registered_at_ms, activated_at_ms, retired_at_ms
|
||||
) VALUES (?, ?, ?, 'retired', 3, ?, ?, ?, 10, 50, 100)`,
|
||||
)
|
||||
.run(
|
||||
RETIRED_KEY_ID,
|
||||
retiredRuntime.digest,
|
||||
retiredBackup.digest,
|
||||
'00000000-0000-4000-8000-000000000811',
|
||||
'00000000-0000-4000-8000-000000000812',
|
||||
'00000000-0000-4000-8000-000000000813',
|
||||
);
|
||||
client
|
||||
.prepare(
|
||||
`INSERT INTO "QingLong3LocalOwnerPepperKeys" (
|
||||
pepper_key_id, material_digest, backup_digest, state, version,
|
||||
register_mutation_id, activate_mutation_id, registered_at_ms,
|
||||
activated_at_ms
|
||||
) VALUES (?, ?, ?, 'active', 2, ?, ?, 60, 100)`,
|
||||
)
|
||||
.run(
|
||||
ACTIVE_KEY_ID,
|
||||
activeRuntime.digest,
|
||||
activeBackup.digest,
|
||||
'00000000-0000-4000-8000-000000000821',
|
||||
'00000000-0000-4000-8000-000000000822',
|
||||
);
|
||||
client
|
||||
.prepare(
|
||||
`INSERT INTO "QingLong3LocalOwnerPepperActivations" (
|
||||
generation, mutation_id, expected_generation,
|
||||
previous_pepper_key_id, active_pepper_key_id,
|
||||
material_digest, backup_digest, activated_at_ms
|
||||
) VALUES (1, ?, 0, NULL, ?, ?, ?, 50)`,
|
||||
)
|
||||
.run(
|
||||
'00000000-0000-4000-8000-000000000812',
|
||||
RETIRED_KEY_ID,
|
||||
retiredRuntime.digest,
|
||||
retiredBackup.digest,
|
||||
);
|
||||
client
|
||||
.prepare(
|
||||
`INSERT INTO "QingLong3LocalOwnerPepperActivations" (
|
||||
generation, mutation_id, expected_generation,
|
||||
previous_pepper_key_id, active_pepper_key_id,
|
||||
material_digest, backup_digest, activated_at_ms
|
||||
) VALUES (2, ?, 1, ?, ?, ?, ?, 100)`,
|
||||
)
|
||||
.run(
|
||||
'00000000-0000-4000-8000-000000000822',
|
||||
RETIRED_KEY_ID,
|
||||
ACTIVE_KEY_ID,
|
||||
activeRuntime.digest,
|
||||
activeBackup.digest,
|
||||
);
|
||||
client.close();
|
||||
return {
|
||||
databasePath,
|
||||
keyringDirectory,
|
||||
backupDirectory,
|
||||
retiredRuntime,
|
||||
retiredBackup,
|
||||
activeRuntime,
|
||||
};
|
||||
}
|
||||
|
||||
function openOptions(fixture) {
|
||||
return {
|
||||
databasePath: fixture.databasePath,
|
||||
profile: 'edge',
|
||||
keyringDirectory: fixture.keyringDirectory,
|
||||
backupDirectory: fixture.backupDirectory,
|
||||
retentionPolicy: policy(),
|
||||
};
|
||||
}
|
||||
|
||||
test('destroys runtime and backup material then replays from durable absence', async (t) => {
|
||||
const prepared = await fixture(t);
|
||||
const authority = await openLocalOwnerPepperMaterialGc(openOptions(prepared));
|
||||
t.after(() => authority.close());
|
||||
const first = await authority.collect(request());
|
||||
assert.equal(first.status, 'inserted');
|
||||
assert.equal(first.record.state, 'completed');
|
||||
assert.equal(first.runtimeMaterial.status, 'destroyed');
|
||||
assert.equal(first.backupMaterial.status, 'destroyed');
|
||||
assert.equal(
|
||||
fs.existsSync(
|
||||
localOwnerPepperKeyPath(prepared.keyringDirectory, RETIRED_KEY_ID),
|
||||
),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
fs.existsSync(
|
||||
localOwnerPepperKeyPath(prepared.backupDirectory, RETIRED_KEY_ID),
|
||||
),
|
||||
false,
|
||||
);
|
||||
assert.ok(
|
||||
new LocalOwnerPepperKeyringFileProvider(prepared.keyringDirectory).resolve(
|
||||
ACTIVE_KEY_ID,
|
||||
),
|
||||
);
|
||||
|
||||
const replay = await authority.collect(request());
|
||||
assert.equal(replay.status, 'existing');
|
||||
assert.equal(replay.runtimeMaterial.status, 'absent');
|
||||
assert.equal(replay.backupMaterial.status, 'absent');
|
||||
assert.equal(
|
||||
replay.record.destructionProofDigest,
|
||||
first.record.destructionProofDigest,
|
||||
);
|
||||
});
|
||||
|
||||
test('recovers after runtime deletion but before backup deletion and completion', async (t) => {
|
||||
const prepared = await fixture(t);
|
||||
const command = request();
|
||||
const database = await openLocalSqlitePepperGcDatabase({
|
||||
databasePath: prepared.databasePath,
|
||||
profile: 'edge',
|
||||
});
|
||||
await database.materialGc.prepare({
|
||||
mutationId: command.prepareMutationId,
|
||||
requestId: command.prepareRequestId,
|
||||
pepperKeyId: command.pepperKeyId,
|
||||
expectedMaterialDigest: prepared.retiredRuntime.digest,
|
||||
expectedBackupMaterialDigest: prepared.retiredBackup.digest,
|
||||
expectedActivePepperKeyId: ACTIVE_KEY_ID,
|
||||
expectedActiveGeneration: 2,
|
||||
expectedActiveMaterialDigest: prepared.activeRuntime.digest,
|
||||
retentionPolicy: policy(),
|
||||
preparedAtMs: REQUESTED_AT_MS,
|
||||
audit: audit(
|
||||
command.prepareMutationId,
|
||||
command.prepareRequestId,
|
||||
'prepare',
|
||||
),
|
||||
});
|
||||
await database.close();
|
||||
destroyLocalOwnerPepperKey({
|
||||
keyringDirectory: prepared.keyringDirectory,
|
||||
pepperKeyId: RETIRED_KEY_ID,
|
||||
materialRole: 'runtime',
|
||||
expectedMaterialDigest: prepared.retiredRuntime.digest,
|
||||
prepareMutationId: command.prepareMutationId,
|
||||
});
|
||||
|
||||
const authority = await openLocalOwnerPepperMaterialGc(openOptions(prepared));
|
||||
t.after(() => authority.close());
|
||||
const recovered = await authority.collect(command);
|
||||
assert.equal(recovered.record.state, 'completed');
|
||||
assert.equal(recovered.runtimeMaterial.status, 'absent');
|
||||
assert.equal(recovered.backupMaterial.status, 'destroyed');
|
||||
});
|
||||
|
||||
test('fails before prepare when the active independent backup is missing', async (t) => {
|
||||
const prepared = await fixture(t);
|
||||
fs.unlinkSync(
|
||||
localOwnerPepperKeyPath(prepared.backupDirectory, ACTIVE_KEY_ID),
|
||||
);
|
||||
const authority = await openLocalOwnerPepperMaterialGc(openOptions(prepared));
|
||||
t.after(() => authority.close());
|
||||
await assert.rejects(
|
||||
authority.collect(request()),
|
||||
LocalOwnerPepperMaterialGcMaterialUnavailableError,
|
||||
);
|
||||
assert.equal(
|
||||
fs.existsSync(
|
||||
localOwnerPepperKeyPath(prepared.keyringDirectory, RETIRED_KEY_ID),
|
||||
),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects caller-controlled time before opening a destructive operation', async (t) => {
|
||||
const prepared = await fixture(t);
|
||||
const authority = await openLocalOwnerPepperMaterialGc(openOptions(prepared));
|
||||
t.after(() => authority.close());
|
||||
await assert.rejects(
|
||||
authority.collect({
|
||||
...request(),
|
||||
requestedAtMs: Number.MAX_SAFE_INTEGER,
|
||||
}),
|
||||
/request shape is invalid/,
|
||||
);
|
||||
assert.equal(
|
||||
fs.existsSync(
|
||||
localOwnerPepperKeyPath(prepared.keyringDirectory, RETIRED_KEY_ID),
|
||||
),
|
||||
true,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const test = require('node:test');
|
||||
|
||||
const {
|
||||
PluginPackagePromptOutputFileKeyring,
|
||||
provisionPluginPackagePromptOutputFileKeyring,
|
||||
rotatePluginPackagePromptOutputFileKeyring,
|
||||
} = require('../../ql3-ai/dist/prompt-output/key-management/pluginPackagePromptOutputFileKeyring.js');
|
||||
const {
|
||||
setupScenario,
|
||||
} = require('../../ql3-ai/test/fixtures/pluginPackagePromptCrashFixture.cjs');
|
||||
const {
|
||||
openLocalOwnerPromptOutputKeyRetirement,
|
||||
} = require('../dist/prompt-output-maintenance/promptOutputKeyRetirement.js');
|
||||
|
||||
test('Local Owner retires one inactive Prompt output key and exactly replays', async (t) => {
|
||||
const directory = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'ql3-owner-output-key-retirement-'),
|
||||
);
|
||||
fs.chmodSync(directory, 0o700);
|
||||
t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
||||
const databasePath = path.join(directory, 'runtime.sqlite');
|
||||
const keyringPath = path.join(directory, 'prompt-output-keyring.json');
|
||||
await setupScenario({
|
||||
databasePath,
|
||||
statePath: path.join(directory, 'fixture-state.json'),
|
||||
profile: 'edge',
|
||||
operation: 'admission',
|
||||
});
|
||||
const provisioned =
|
||||
await provisionPluginPackagePromptOutputFileKeyring(keyringPath);
|
||||
await rotatePluginPackagePromptOutputFileKeyring({
|
||||
filePath: keyringPath,
|
||||
expectedActiveKeyId: provisioned.activeKeyId,
|
||||
expectedCatalogDigest: provisioned.catalogDigest,
|
||||
});
|
||||
|
||||
const authority = await openLocalOwnerPromptOutputKeyRetirement({
|
||||
databasePath,
|
||||
profile: 'edge',
|
||||
keyringPath,
|
||||
});
|
||||
t.after(() => authority.close());
|
||||
const request = {
|
||||
keyId: provisioned.activeKeyId,
|
||||
retirementId: 'owner-retirement-a',
|
||||
requestId: 'owner-retirement-request-a',
|
||||
mutationId: 'owner-retirement-mutation-a',
|
||||
};
|
||||
const retired = await authority.retire(request);
|
||||
assert.equal(retired.status, 'completed');
|
||||
assert.equal((await authority.retire(request)).status, 'existing');
|
||||
assert.equal(retired.keyId, provisioned.activeKeyId);
|
||||
assert.equal(
|
||||
(await new PluginPackagePromptOutputFileKeyring(keyringPath).inspect(
|
||||
provisioned.activeKeyId,
|
||||
)).state,
|
||||
'absent',
|
||||
);
|
||||
|
||||
await authority.close();
|
||||
const client = new DatabaseSync(databasePath, { readOnly: true });
|
||||
try {
|
||||
assert.equal(
|
||||
client
|
||||
.prepare(
|
||||
`SELECT count(*) AS count
|
||||
FROM "ModelInvocationPromptOutputKeyRetirementPreparations"`,
|
||||
)
|
||||
.get().count,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
client
|
||||
.prepare(
|
||||
`SELECT count(*) AS count
|
||||
FROM "ModelInvocationPromptOutputKeyRetirementCompletions"`,
|
||||
)
|
||||
.get().count,
|
||||
1,
|
||||
);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../ql3-runtime-core/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": true,
|
||||
"noEmit": false
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user