Files
rust-discovery/web/src/script-catalog.ts
T

81 lines
2.9 KiB
TypeScript

import { objectId, page, record, request, tenantPath, text, type RecordValue } from './api';
export type Interpreter = 'posix_sh' | 'bash' | 'powershell';
export type Identity = 'agent_service' | 'root' | 'system';
export interface Script {
name: string;
interpreter: Interpreter;
execution_identity: Identity;
script_utf8: string;
}
export interface Revision extends Script {
revision_id: string;
version: number;
script_sha256: string;
}
export const interpreterNames: Record<Interpreter, string> = {
posix_sh: 'Linux · Shell',
bash: 'Linux · Bash',
powershell: 'Windows · PowerShell'
};
export const identityNames: Record<Identity, string> = {
agent_service: 'Agent-Dienstkonto',
root: 'root',
system: 'SYSTEM'
};
export function parseRevision(value: unknown): Revision {
const row = record(value);
if (
(row.interpreter !== 'posix_sh' && row.interpreter !== 'bash' && row.interpreter !== 'powershell') ||
(row.execution_identity !== 'agent_service' &&
row.execution_identity !== 'root' &&
row.execution_identity !== 'system') ||
typeof row.version !== 'number' ||
!Number.isSafeInteger(row.version) ||
row.version < 1
) {
throw Error('Der Server meldet eine ungültige Skriptversion.');
}
return {
revision_id: objectId(row, 'revision'),
name: text(row.name, 'Skriptname'),
version: row.version,
interpreter: row.interpreter,
execution_identity: row.execution_identity,
script_utf8: text(row.script_utf8, 'Skriptinhalt'),
script_sha256: text(row.script_sha256, 'Prüfsumme')
};
}
export async function loadRevisions(tenantId: string, signal: AbortSignal): Promise<Revision[]> {
const revisions: Revision[] = [];
const cursors = new Set<string>();
let cursor: string | null = null;
do {
const params = new URLSearchParams({ limit: '100' });
if (cursor) params.set('cursor', cursor);
const batch = page(await request(tenantPath(tenantId, '/script-revisions?' + params), { signal }));
revisions.push(...batch.items.map(parseRevision));
cursor = batch.next_cursor;
if (cursor && cursors.has(cursor)) throw Error('Die nächste Bibliotheksseite konnte nicht geladen werden.');
if (cursor) cursors.add(cursor);
} while (cursor);
return revisions.sort((a, b) => a.name.localeCompare(b.name, 'de') || b.version - a.version);
}
export function latestRevisions(revisions: Revision[]) {
const heads = new Map<string, Revision>();
for (const item of revisions) {
const previous = heads.get(item.name);
if (!previous || previous.version < item.version) heads.set(item.name, item);
}
return [...heads.values()].sort((a, b) => a.name.localeCompare(b.name, 'de'));
}
export function devicePlatform(row: RecordValue) {
if (row.source_id === 'windows.base') return 'windows';
if (row.source_id === 'linux.base') return 'linux';
return null;
}
export function scriptPlatform(interpreter: Interpreter) {
return interpreter === 'powershell' ? 'windows' : 'linux';
}