@mago-foundation/mago-ski 0.0.0-bootstrap.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +122 -1
- package/action.yml +58 -0
- package/dist/hosts/github/run.d.ts +21 -0
- package/dist/hosts/github/run.js +119 -0
- package/dist/hosts/pi/index.d.ts +11 -0
- package/dist/hosts/pi/index.js +388 -0
- package/dist/hosts/pi/paths.d.ts +9 -0
- package/dist/hosts/pi/paths.js +55 -0
- package/dist/src/admin.d.ts +70 -0
- package/dist/src/admin.js +115 -0
- package/dist/src/canonical-json.d.ts +12 -0
- package/dist/src/canonical-json.js +104 -0
- package/dist/src/certificate.d.ts +34 -0
- package/dist/src/certificate.js +104 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +419 -0
- package/dist/src/diff.d.ts +30 -0
- package/dist/src/diff.js +148 -0
- package/dist/src/dsse.d.ts +23 -0
- package/dist/src/dsse.js +55 -0
- package/dist/src/experimental/sigstore.d.ts +26 -0
- package/dist/src/experimental/sigstore.js +153 -0
- package/dist/src/fs-safe.d.ts +11 -0
- package/dist/src/fs-safe.js +96 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.js +14 -0
- package/dist/src/keys.d.ts +21 -0
- package/dist/src/keys.js +108 -0
- package/dist/src/main-module.d.ts +5 -0
- package/dist/src/main-module.js +19 -0
- package/dist/src/names.d.ts +7 -0
- package/dist/src/names.js +40 -0
- package/dist/src/policy.d.ts +59 -0
- package/dist/src/policy.js +237 -0
- package/dist/src/revocations.d.ts +28 -0
- package/dist/src/revocations.js +91 -0
- package/dist/src/time.d.ts +9 -0
- package/dist/src/time.js +32 -0
- package/dist/src/tree-digest.d.ts +43 -0
- package/dist/src/tree-digest.js +243 -0
- package/dist/src/trust-root.d.ts +33 -0
- package/dist/src/trust-root.js +115 -0
- package/dist/src/verify.d.ts +73 -0
- package/dist/src/verify.js +134 -0
- package/docs/hosts/adapter-interface.md +33 -0
- package/docs/hosts/github-action.md +45 -0
- package/docs/hosts/pi.md +36 -0
- package/docs/spec/skill-certificate-v1.md +60 -0
- package/docs/spec/skill-tree-v2.md +59 -0
- package/docs/spec/trust-documents-v1.md +103 -0
- package/docs/spec/verification.md +46 -0
- package/docs/threat-model.md +44 -0
- package/package.json +76 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Canonical JSON: sorted object keys, no insignificant whitespace, one trailing LF.
|
|
2
|
+
// Parsing rejects any byte sequence that is not exactly the canonical form, so
|
|
3
|
+
// duplicate keys, alternate escapes and reordered fields cannot change meaning.
|
|
4
|
+
export const MAX_CANONICAL_JSON_BYTES = 4 * 1024 * 1024;
|
|
5
|
+
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
|
|
6
|
+
export function isRecord(value) {
|
|
7
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function canonicalValue(value, depth, state) {
|
|
10
|
+
state.nodes += 1;
|
|
11
|
+
if (state.nodes > 200_000 || depth > 64)
|
|
12
|
+
throw new Error('Canonical JSON exceeds structural limits');
|
|
13
|
+
if (value === null)
|
|
14
|
+
return 'null';
|
|
15
|
+
if (typeof value === 'string')
|
|
16
|
+
return JSON.stringify(value);
|
|
17
|
+
if (typeof value === 'boolean')
|
|
18
|
+
return value ? 'true' : 'false';
|
|
19
|
+
if (typeof value === 'number') {
|
|
20
|
+
if (!Number.isSafeInteger(value))
|
|
21
|
+
throw new Error('Canonical JSON numbers must be safe integers');
|
|
22
|
+
return JSON.stringify(value);
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
return `[${value.map((item) => canonicalValue(item, depth + 1, state)).join(',')}]`;
|
|
26
|
+
}
|
|
27
|
+
if (isRecord(value)) {
|
|
28
|
+
const entries = Object.keys(value).sort().map((key) => {
|
|
29
|
+
const item = value[key];
|
|
30
|
+
if (item === undefined)
|
|
31
|
+
throw new Error(`Canonical JSON field ${JSON.stringify(key)} is undefined`);
|
|
32
|
+
return `${JSON.stringify(key)}:${canonicalValue(item, depth + 1, state)}`;
|
|
33
|
+
});
|
|
34
|
+
return `{${entries.join(',')}}`;
|
|
35
|
+
}
|
|
36
|
+
throw new Error('Canonical JSON contains an unsupported value');
|
|
37
|
+
}
|
|
38
|
+
export function serializeCanonicalJson(value) {
|
|
39
|
+
return `${canonicalValue(value, 0, { nodes: 0 })}\n`;
|
|
40
|
+
}
|
|
41
|
+
export function canonicalBytes(value) {
|
|
42
|
+
return Buffer.from(serializeCanonicalJson(value), 'utf8');
|
|
43
|
+
}
|
|
44
|
+
export function parseCanonicalJsonBytes(input, label = 'JSON document') {
|
|
45
|
+
const bytes = Buffer.from(input);
|
|
46
|
+
if (bytes.length === 0 || bytes.length > MAX_CANONICAL_JSON_BYTES) {
|
|
47
|
+
throw new Error(`${label} must be between 1 byte and ${MAX_CANONICAL_JSON_BYTES} bytes`);
|
|
48
|
+
}
|
|
49
|
+
let text;
|
|
50
|
+
try {
|
|
51
|
+
text = utf8Decoder.decode(bytes);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new Error(`${label} is not valid UTF-8`);
|
|
55
|
+
}
|
|
56
|
+
if (text.charCodeAt(0) === 0xfeff || !text.endsWith('\n')) {
|
|
57
|
+
throw new Error(`${label} must be canonical UTF-8 JSON with one trailing LF`);
|
|
58
|
+
}
|
|
59
|
+
let value;
|
|
60
|
+
try {
|
|
61
|
+
value = JSON.parse(text.slice(0, -1));
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new Error(`${label} is not valid JSON`);
|
|
65
|
+
}
|
|
66
|
+
let canonical;
|
|
67
|
+
try {
|
|
68
|
+
canonical = serializeCanonicalJson(value);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new Error(`${label} is structurally invalid: ${error.message}`);
|
|
72
|
+
}
|
|
73
|
+
if (!bytes.equals(Buffer.from(canonical, 'utf8'))) {
|
|
74
|
+
throw new Error(`${label} is not canonical JSON (duplicate keys and alternate encodings are rejected)`);
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
export function exactKeys(value, keys, label) {
|
|
79
|
+
if (!isRecord(value))
|
|
80
|
+
throw new Error(`${label} must be a JSON object`);
|
|
81
|
+
const actual = Object.keys(value).sort();
|
|
82
|
+
const expected = [...keys].sort();
|
|
83
|
+
if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
|
|
84
|
+
throw new Error(`${label} has missing or unsupported fields`);
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
export function requireString(value, label, maxLength = 1024) {
|
|
89
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > maxLength || /[\u0000-\u001f\u007f]/u.test(value)) {
|
|
90
|
+
throw new Error(`${label} must be a non-empty string of at most ${maxLength} characters without control characters`);
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
export function requireArray(value, label, maxLength) {
|
|
95
|
+
if (!Array.isArray(value) || value.length > maxLength)
|
|
96
|
+
throw new Error(`${label} must be an array of at most ${maxLength} items`);
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
export function requirePositiveInteger(value, label) {
|
|
100
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 1) {
|
|
101
|
+
throw new Error(`${label} must be a positive integer`);
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { parseEnvelope } from './dsse.ts';
|
|
2
|
+
import type { PrivateKeyInfo } from './keys.ts';
|
|
3
|
+
import { type TreeFile } from './tree-digest.ts';
|
|
4
|
+
export declare const STATEMENT_TYPE = "https://in-toto.io/Statement/v1";
|
|
5
|
+
export declare const STATEMENT_PAYLOAD_TYPE = "application/vnd.in-toto+json";
|
|
6
|
+
export declare const CERTIFICATE_PREDICATE_TYPE = "https://github.com/mago-foundation/mago-ski/blob/main/docs/spec/skill-certificate-v1.md";
|
|
7
|
+
/** in-toto DigestSet algorithm name for the mago.skill-tree/v2 digest. */
|
|
8
|
+
export declare const SUBJECT_DIGEST_ALGORITHM = "magoSkillTreeV2";
|
|
9
|
+
export interface Certificate {
|
|
10
|
+
/** sha256 of the certificate file bytes; used in logs. */
|
|
11
|
+
id: string;
|
|
12
|
+
skillName: string;
|
|
13
|
+
digest: string;
|
|
14
|
+
files: TreeFile[];
|
|
15
|
+
approverKeyId: string;
|
|
16
|
+
issuedAt: Date;
|
|
17
|
+
expiresAt: Date;
|
|
18
|
+
reason: string;
|
|
19
|
+
previousDigest: string | null;
|
|
20
|
+
envelope: ReturnType<typeof parseEnvelope>;
|
|
21
|
+
}
|
|
22
|
+
export interface IssueOptions {
|
|
23
|
+
skillName: string;
|
|
24
|
+
digest: string;
|
|
25
|
+
files: TreeFile[];
|
|
26
|
+
approver: PrivateKeyInfo;
|
|
27
|
+
issuedAt: Date;
|
|
28
|
+
expiresAt: Date;
|
|
29
|
+
reason: string;
|
|
30
|
+
previousDigest?: string | null;
|
|
31
|
+
}
|
|
32
|
+
export declare function issueCertificate(options: IssueOptions): Buffer;
|
|
33
|
+
/** Parses a certificate's structure. Signature and trust are checked by the verifier. */
|
|
34
|
+
export declare function parseCertificate(bytes: Uint8Array): Certificate;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { canonicalBytes, exactKeys, isRecord, parseCanonicalJsonBytes, requireArray, requireString } from "./canonical-json.js";
|
|
2
|
+
import { envelopeBytes, parseEnvelope, signEnvelope } from "./dsse.js";
|
|
3
|
+
import { validateDigest, validateSkillName } from "./names.js";
|
|
4
|
+
import { formatTimestamp, parseTimestamp } from "./time.js";
|
|
5
|
+
import { digestFileList, MAX_RELATIVE_PATH_BYTES, MAX_TREE_FILES, sha256Hex, SKILL_FILENAME, TREE_PROFILE } from "./tree-digest.js";
|
|
6
|
+
export const STATEMENT_TYPE = 'https://in-toto.io/Statement/v1';
|
|
7
|
+
export const STATEMENT_PAYLOAD_TYPE = 'application/vnd.in-toto+json';
|
|
8
|
+
export const CERTIFICATE_PREDICATE_TYPE = 'https://github.com/mago-foundation/mago-ski/blob/main/docs/spec/skill-certificate-v1.md';
|
|
9
|
+
/** in-toto DigestSet algorithm name for the mago.skill-tree/v2 digest. */
|
|
10
|
+
export const SUBJECT_DIGEST_ALGORITHM = 'magoSkillTreeV2';
|
|
11
|
+
export function issueCertificate(options) {
|
|
12
|
+
validateSkillName(options.skillName);
|
|
13
|
+
if (!options.files.some((file) => file.path === SKILL_FILENAME))
|
|
14
|
+
throw new Error(`Certificate file list must include ${SKILL_FILENAME}`);
|
|
15
|
+
if (digestFileList(options.files) !== options.digest)
|
|
16
|
+
throw new Error('File list does not match the tree digest');
|
|
17
|
+
if (options.expiresAt <= options.issuedAt)
|
|
18
|
+
throw new Error('Certificate must expire after it is issued');
|
|
19
|
+
const statement = {
|
|
20
|
+
_type: STATEMENT_TYPE,
|
|
21
|
+
subject: [{ name: options.skillName, digest: { [SUBJECT_DIGEST_ALGORITHM]: options.digest.slice('sha256:'.length) } }],
|
|
22
|
+
predicateType: CERTIFICATE_PREDICATE_TYPE,
|
|
23
|
+
predicate: {
|
|
24
|
+
tree_profile: TREE_PROFILE,
|
|
25
|
+
files: options.files.map((file) => ({ path: file.path, size: file.size, sha256: file.sha256, exec: file.exec })),
|
|
26
|
+
approval: {
|
|
27
|
+
approver_key_id: options.approver.keyId,
|
|
28
|
+
issued_at: formatTimestamp(options.issuedAt),
|
|
29
|
+
expires_at: formatTimestamp(options.expiresAt),
|
|
30
|
+
reason: requireString(options.reason, 'reason', 1024),
|
|
31
|
+
},
|
|
32
|
+
previous_digest: options.previousDigest == null ? null : validateDigest(options.previousDigest, 'previous digest'),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const payload = canonicalBytes(statement);
|
|
36
|
+
return envelopeBytes(signEnvelope(STATEMENT_PAYLOAD_TYPE, payload, options.approver.keyId, options.approver.privateKey));
|
|
37
|
+
}
|
|
38
|
+
function parseFiles(value, label) {
|
|
39
|
+
return requireArray(value, label, MAX_TREE_FILES).map((item, index) => {
|
|
40
|
+
const entry = exactKeys(item, ['path', 'size', 'sha256', 'exec'], `${label}[${index}]`);
|
|
41
|
+
const filePath = entry.path;
|
|
42
|
+
if (typeof filePath !== 'string' || filePath.length === 0 || Buffer.byteLength(filePath) > MAX_RELATIVE_PATH_BYTES ||
|
|
43
|
+
filePath.startsWith('/') || filePath.includes('\\') || filePath.split('/').some((part) => part === '' || part === '.' || part === '..')) {
|
|
44
|
+
throw new Error(`${label}[${index}] path is not a safe relative path`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof entry.size !== 'number' || !Number.isSafeInteger(entry.size) || entry.size < 0)
|
|
47
|
+
throw new Error(`${label}[${index}] size is invalid`);
|
|
48
|
+
if (typeof entry.sha256 !== 'string' || !/^[0-9a-f]{64}$/u.test(entry.sha256))
|
|
49
|
+
throw new Error(`${label}[${index}] sha256 is invalid`);
|
|
50
|
+
if (typeof entry.exec !== 'boolean')
|
|
51
|
+
throw new Error(`${label}[${index}] exec must be a boolean`);
|
|
52
|
+
return { path: filePath, size: entry.size, sha256: entry.sha256, exec: entry.exec };
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** Parses a certificate's structure. Signature and trust are checked by the verifier. */
|
|
56
|
+
export function parseCertificate(bytes) {
|
|
57
|
+
const label = 'certificate';
|
|
58
|
+
const envelope = parseEnvelope(bytes, label);
|
|
59
|
+
if (envelope.payloadType !== STATEMENT_PAYLOAD_TYPE)
|
|
60
|
+
throw new Error(`${label} has the wrong payload type`);
|
|
61
|
+
const statement = exactKeys(parseCanonicalJsonBytes(envelope.payloadBytes, `${label} statement`), ['_type', 'subject', 'predicateType', 'predicate'], `${label} statement`);
|
|
62
|
+
if (statement._type !== STATEMENT_TYPE)
|
|
63
|
+
throw new Error(`${label} is not an in-toto v1 statement`);
|
|
64
|
+
if (statement.predicateType !== CERTIFICATE_PREDICATE_TYPE)
|
|
65
|
+
throw new Error(`${label} has an unsupported predicateType`);
|
|
66
|
+
const subjects = requireArray(statement.subject, `${label} subject`, 1);
|
|
67
|
+
if (subjects.length !== 1)
|
|
68
|
+
throw new Error(`${label} must have exactly one subject`);
|
|
69
|
+
const subject = exactKeys(subjects[0], ['name', 'digest'], `${label} subject`);
|
|
70
|
+
const skillName = validateSkillName(subject.name, `${label} subject name`);
|
|
71
|
+
const digestSet = exactKeys(subject.digest, [SUBJECT_DIGEST_ALGORITHM], `${label} subject digest`);
|
|
72
|
+
const digest = validateDigest(`sha256:${String(digestSet[SUBJECT_DIGEST_ALGORITHM])}`, `${label} subject digest`);
|
|
73
|
+
const predicate = exactKeys(statement.predicate, ['tree_profile', 'files', 'approval', 'previous_digest'], `${label} predicate`);
|
|
74
|
+
if (predicate.tree_profile !== TREE_PROFILE)
|
|
75
|
+
throw new Error(`${label} uses unsupported tree profile ${String(predicate.tree_profile)}`);
|
|
76
|
+
const files = parseFiles(predicate.files, `${label} files`);
|
|
77
|
+
if (!files.some((file) => file.path === SKILL_FILENAME))
|
|
78
|
+
throw new Error(`${label} file list does not include ${SKILL_FILENAME}`);
|
|
79
|
+
if (digestFileList(files) !== digest)
|
|
80
|
+
throw new Error(`${label} file list does not match its subject digest`);
|
|
81
|
+
const approval = exactKeys(predicate.approval, ['approver_key_id', 'issued_at', 'expires_at', 'reason'], `${label} approval`);
|
|
82
|
+
const approverKeyId = String(approval.approver_key_id);
|
|
83
|
+
if (approverKeyId !== envelope.signatures[0].keyid)
|
|
84
|
+
throw new Error(`${label} approver_key_id does not match the signing key`);
|
|
85
|
+
const issuedAt = parseTimestamp(approval.issued_at, `${label} issued_at`);
|
|
86
|
+
const expiresAt = parseTimestamp(approval.expires_at, `${label} expires_at`);
|
|
87
|
+
if (expiresAt <= issuedAt)
|
|
88
|
+
throw new Error(`${label} expires before it is issued`);
|
|
89
|
+
const previous = predicate.previous_digest;
|
|
90
|
+
if (previous !== null && isRecord(previous))
|
|
91
|
+
throw new Error(`${label} previous_digest is invalid`);
|
|
92
|
+
return {
|
|
93
|
+
id: `sha256:${sha256Hex(bytes)}`,
|
|
94
|
+
skillName,
|
|
95
|
+
digest,
|
|
96
|
+
files,
|
|
97
|
+
approverKeyId,
|
|
98
|
+
issuedAt,
|
|
99
|
+
expiresAt,
|
|
100
|
+
reason: requireString(approval.reason, `${label} reason`, 1024),
|
|
101
|
+
previousDigest: previous === null ? null : validateDigest(previous, `${label} previous_digest`),
|
|
102
|
+
envelope,
|
|
103
|
+
};
|
|
104
|
+
}
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { addApprover, approveSkill, initRoot, removeApprover, updateRevocations } from "./admin.js";
|
|
6
|
+
import { parseCertificate } from "./certificate.js";
|
|
7
|
+
import { diffSkillTrees } from "./diff.js";
|
|
8
|
+
import { readRegularFile } from "./fs-safe.js";
|
|
9
|
+
import { generateKeyPair, validateKeyId } from "./keys.js";
|
|
10
|
+
import { appendDecision, decisionRecord, DEFAULT_POLICY_PATH, findSkillDirs, loadPolicy, loadPolicyTrust, tooDeepResult, verifyWithPolicy, } from "./policy.js";
|
|
11
|
+
import { isMainModule } from "./main-module.js";
|
|
12
|
+
import { formatTimestamp } from "./time.js";
|
|
13
|
+
import { collectSkillTree, TREE_PROFILE } from "./tree-digest.js";
|
|
14
|
+
import { verifyTrustRoot } from "./trust-root.js";
|
|
15
|
+
import { unverifiable } from "./verify.js";
|
|
16
|
+
const usage = `mago-ski: organization-approved Skill certificates, enforced on the agent host.
|
|
17
|
+
|
|
18
|
+
Keys and trust (run by the organization's root key holder):
|
|
19
|
+
mago-ski keygen --private-key FILE --public-key FILE
|
|
20
|
+
mago-ski root init --root-key FILE --trust-root OUT --revocations OUT [--trust-root-expires 365d] [--revocations-expires 30d]
|
|
21
|
+
mago-ski root add-approver --root-key FILE --trust-root FILE --public-key FILE --name LABEL --scope PATTERN [--scope ...] --expires 180d [--out FILE] [--trust-root-expires 365d]
|
|
22
|
+
mago-ski root remove-approver --root-key FILE --trust-root FILE --key-id ID [--out FILE]
|
|
23
|
+
mago-ski root show --trust-root FILE --root-fingerprint ID
|
|
24
|
+
mago-ski revoke key --root-key FILE --revocations FILE --key-id ID --reason TEXT [--expires 30d] [--out FILE]
|
|
25
|
+
mago-ski revoke digest --root-key FILE --revocations FILE --digest sha256:... --reason TEXT [--expires 30d] [--out FILE]
|
|
26
|
+
mago-ski revoke refresh --root-key FILE --revocations FILE [--expires 30d] [--out FILE]
|
|
27
|
+
|
|
28
|
+
Approving Skills (run by a reviewer with an approver key):
|
|
29
|
+
mago-ski approve SKILL_DIR --key FILE --expires 90d --reason TEXT [--name NAME] [--previous CERT|DIGEST] [--out FILE]
|
|
30
|
+
|
|
31
|
+
Checking Skills (uses .mago-ski/policy.json unless --policy is given):
|
|
32
|
+
mago-ski verify SKILL_DIR [--policy FILE] [--name NAME] [--certificate FILE] [--json]
|
|
33
|
+
mago-ski verify-all [--policy FILE] [--json]
|
|
34
|
+
mago-ski digest SKILL_DIR [--explain]
|
|
35
|
+
mago-ski diff --base DIR --candidate DIR
|
|
36
|
+
mago-ski inspect CERTIFICATE
|
|
37
|
+
|
|
38
|
+
Verdicts: VERIFIED, UNAPPROVED_CHANGE, NO_CERTIFICATE, UNTRUSTED_SIGNER, REVOKED, EXPIRED, UNVERIFIABLE.
|
|
39
|
+
Exit codes: 0 success or VERIFIED; 1 any other verdict or a required re-certification; 2 usage or input error.
|
|
40
|
+
Run "mago-ski <command> --help" for a command's options, and "mago-ski --version" for the version.
|
|
41
|
+
|
|
42
|
+
A certificate records who approved an exact Skill version. It does not certify that the Skill is safe.`;
|
|
43
|
+
const DURATION_NOTE = 'Durations are a number plus d, h or m, for example 90d, 12h or 30m.';
|
|
44
|
+
/** Per-command help: `mago-ski <command> --help`. Keys are "command" or "command subcommand". */
|
|
45
|
+
const COMMAND_HELP = {
|
|
46
|
+
keygen: `Usage: mago-ski keygen --private-key FILE --public-key FILE
|
|
47
|
+
|
|
48
|
+
Creates an Ed25519 key pair for a root key or an approver key.
|
|
49
|
+
|
|
50
|
+
--private-key FILE where to write the private key (created with mode 600; must not exist yet)
|
|
51
|
+
--public-key FILE where to write the public key
|
|
52
|
+
|
|
53
|
+
Keep private keys secret and outside every Skill directory; keygen refuses to write inside one.
|
|
54
|
+
Prints the key id (sha256:...), which identifies the key in trust roots and revocations.`,
|
|
55
|
+
root: `Usage: mago-ski root <init | add-approver | remove-approver | show> [options]
|
|
56
|
+
|
|
57
|
+
Manages the organization's signed trust root (who may approve which Skills).
|
|
58
|
+
Run "mago-ski root <subcommand> --help" for details.`,
|
|
59
|
+
'root init': `Usage: mago-ski root init --root-key FILE --trust-root OUT --revocations OUT [options]
|
|
60
|
+
|
|
61
|
+
Creates version 1 of the signed trust root (no approvers yet) and an empty signed revocation list.
|
|
62
|
+
|
|
63
|
+
--root-key FILE the root private key (keep it offline)
|
|
64
|
+
--trust-root OUT where to write trust-root.json (must not exist yet)
|
|
65
|
+
--revocations OUT where to write revocations.json (must not exist yet)
|
|
66
|
+
--trust-root-expires DUR trust root lifetime (default 365d)
|
|
67
|
+
--revocations-expires DUR revocation list lifetime (default 30d; republish before it expires)
|
|
68
|
+
|
|
69
|
+
Prints root_fingerprint: put it in every host policy as "root_fingerprint".
|
|
70
|
+
${DURATION_NOTE}`,
|
|
71
|
+
'root add-approver': `Usage: mago-ski root add-approver --root-key FILE --trust-root FILE --public-key FILE --name LABEL --scope PATTERN [--scope ...] --expires DUR [options]
|
|
72
|
+
|
|
73
|
+
Adds (or replaces) an approver and re-signs the trust root with the next version.
|
|
74
|
+
|
|
75
|
+
--root-key FILE the root private key
|
|
76
|
+
--trust-root FILE the current trust root (updated in place unless --out is given)
|
|
77
|
+
--public-key FILE the approver's public key
|
|
78
|
+
--name LABEL a readable name, shown in verdicts
|
|
79
|
+
--scope PATTERN Skill names this approver may approve; repeat for several.
|
|
80
|
+
"*" = every Skill, "docs-*" = names starting with docs-, otherwise an exact name
|
|
81
|
+
--expires DUR how long the approver key stays valid
|
|
82
|
+
--out FILE write the new trust root here instead
|
|
83
|
+
--trust-root-expires DUR also set a new trust root lifetime
|
|
84
|
+
${DURATION_NOTE}`,
|
|
85
|
+
'root remove-approver': `Usage: mago-ski root remove-approver --root-key FILE --trust-root FILE --key-id ID [--out FILE] [--trust-root-expires DUR]
|
|
86
|
+
|
|
87
|
+
Removes an approver and re-signs the trust root. Certificates from that approver stop verifying once hosts read
|
|
88
|
+
the new trust root. If the key may be compromised, also revoke it with "mago-ski revoke key".`,
|
|
89
|
+
'root show': `Usage: mago-ski root show --trust-root FILE --root-fingerprint ID
|
|
90
|
+
|
|
91
|
+
Verifies a trust root against the pinned root fingerprint and prints its version, expiry and approvers.`,
|
|
92
|
+
revoke: `Usage: mago-ski revoke <key | digest | refresh> [options]
|
|
93
|
+
|
|
94
|
+
Updates the organization's signed revocation list. Run "mago-ski revoke <subcommand> --help" for details.`,
|
|
95
|
+
'revoke key': `Usage: mago-ski revoke key --root-key FILE --revocations FILE --key-id ID --reason TEXT [--expires DUR] [--out FILE]
|
|
96
|
+
|
|
97
|
+
Revokes an approver key: every certificate it signed stops verifying once hosts read the new list.
|
|
98
|
+
|
|
99
|
+
--key-id ID the approver's key id (sha256:...)
|
|
100
|
+
--reason TEXT shown in verdicts
|
|
101
|
+
--expires DUR lifetime of the new list (default 30d)
|
|
102
|
+
${DURATION_NOTE}`,
|
|
103
|
+
'revoke digest': `Usage: mago-ski revoke digest --root-key FILE --revocations FILE --digest sha256:... --reason TEXT [--expires DUR] [--out FILE]
|
|
104
|
+
|
|
105
|
+
Revokes one approved Skill version (its tree digest), whoever signed it.
|
|
106
|
+
${DURATION_NOTE}`,
|
|
107
|
+
'revoke refresh': `Usage: mago-ski revoke refresh --root-key FILE --revocations FILE [--expires DUR] [--out FILE]
|
|
108
|
+
|
|
109
|
+
Re-signs the revocation list with the next version and a new expiry, without changing its entries.
|
|
110
|
+
Hosts refuse an expired list, so refresh it before it expires (default lifetime 30d).`,
|
|
111
|
+
approve: `Usage: mago-ski approve SKILL_DIR --key FILE --expires DUR --reason TEXT [options]
|
|
112
|
+
|
|
113
|
+
Issues a certificate for the exact current bytes of a Skill, signed with your approver key.
|
|
114
|
+
|
|
115
|
+
--key FILE your approver private key (must be outside the Skill directory)
|
|
116
|
+
--expires DUR how long the approval is valid
|
|
117
|
+
--reason TEXT what you reviewed; stored in the certificate
|
|
118
|
+
--name NAME Skill name (default: "name" in SKILL.md, else the directory name)
|
|
119
|
+
--previous CERT|DIG the previously approved certificate or digest, for the update history
|
|
120
|
+
--out FILE where to write the certificate (default: SKILL_DIR/.mago-ski-cert.json)
|
|
121
|
+
|
|
122
|
+
Refuses a Skill that contains a private key. A certificate records who approved which exact version;
|
|
123
|
+
it does not certify that the Skill is safe.
|
|
124
|
+
${DURATION_NOTE}`,
|
|
125
|
+
verify: `Usage: mago-ski verify SKILL_DIR [--policy FILE] [--name NAME] [--certificate FILE] [--json]
|
|
126
|
+
|
|
127
|
+
Checks one Skill against the policy's trust root and revocation list, and prints its verdict.
|
|
128
|
+
|
|
129
|
+
--policy FILE policy file (default: .mago-ski/policy.json)
|
|
130
|
+
--name NAME Skill name to check (default: "name" in SKILL.md, else the directory name)
|
|
131
|
+
--certificate FILE also try this certificate
|
|
132
|
+
--json print the result as JSON
|
|
133
|
+
|
|
134
|
+
Exit code 0 when VERIFIED, 1 for any other verdict, 2 for usage or input errors.`,
|
|
135
|
+
'verify-all': `Usage: mago-ski verify-all [--policy FILE] [--json]
|
|
136
|
+
|
|
137
|
+
Checks every Skill found under the policy's skill_dirs and prints one line per Skill and a summary.
|
|
138
|
+
Exits 1 if any Skill is not VERIFIED, in shadow mode too (shadow mode affects hosts, not this exit code).`,
|
|
139
|
+
digest: `Usage: mago-ski digest SKILL_DIR [--explain]
|
|
140
|
+
|
|
141
|
+
Prints the Skill's tree digest (sha256:...). --explain also lists every covered file, its hash and
|
|
142
|
+
executable bit, and the paths excluded from the digest.`,
|
|
143
|
+
diff: `Usage: mago-ski diff --base DIR --candidate DIR
|
|
144
|
+
|
|
145
|
+
Compares two versions of a Skill: changed files and Markdown sections, executable-bit changes, and added
|
|
146
|
+
or removed network origins. Exits 1 when the digest changed, since that always needs a new certificate.`,
|
|
147
|
+
inspect: `Usage: mago-ski inspect CERTIFICATE
|
|
148
|
+
|
|
149
|
+
Prints a certificate's Skill, digest, approver, dates and reason. Checks structure only; use
|
|
150
|
+
"mago-ski verify" to check its signature against your trust root.`,
|
|
151
|
+
};
|
|
152
|
+
function readVersion() {
|
|
153
|
+
for (const candidate of ['../package.json', '../../package.json']) {
|
|
154
|
+
try {
|
|
155
|
+
const pkg = JSON.parse(readFileSync(fileURLToPath(new URL(candidate, import.meta.url)), 'utf8'));
|
|
156
|
+
if ((pkg.name === '@mago-foundation/mago-ski' || pkg.name === 'mago-ski') && pkg.version)
|
|
157
|
+
return pkg.version;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// try the next location (source tree vs. built dist/)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return 'unknown';
|
|
164
|
+
}
|
|
165
|
+
class UsageError extends Error {
|
|
166
|
+
}
|
|
167
|
+
const BOOLEAN_FLAGS = new Set(['--json', '--explain', '--help']);
|
|
168
|
+
function parseArgs(args) {
|
|
169
|
+
const parsed = { positional: [], flags: new Map(), booleans: new Set() };
|
|
170
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
171
|
+
const arg = args[index];
|
|
172
|
+
if (BOOLEAN_FLAGS.has(arg)) {
|
|
173
|
+
parsed.booleans.add(arg);
|
|
174
|
+
}
|
|
175
|
+
else if (arg.startsWith('--')) {
|
|
176
|
+
const value = args[index + 1];
|
|
177
|
+
if (value === undefined || value.startsWith('--') || value === '')
|
|
178
|
+
throw new UsageError(`Missing value for ${arg}`);
|
|
179
|
+
parsed.flags.set(arg, [...(parsed.flags.get(arg) ?? []), value]);
|
|
180
|
+
index += 1;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
parsed.positional.push(arg);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return parsed;
|
|
187
|
+
}
|
|
188
|
+
function allow(parsed, allowed, positional, repeatable = []) {
|
|
189
|
+
for (const [flag, values] of parsed.flags) {
|
|
190
|
+
if (!allowed.includes(flag))
|
|
191
|
+
throw new UsageError(`Unsupported option ${flag}`);
|
|
192
|
+
if (values.length > 1 && !repeatable.includes(flag))
|
|
193
|
+
throw new UsageError(`${flag} may be given only once`);
|
|
194
|
+
}
|
|
195
|
+
for (const flag of parsed.booleans)
|
|
196
|
+
if (flag !== '--json' && flag !== '--explain' && !allowed.includes(flag))
|
|
197
|
+
throw new UsageError(`Unsupported option ${flag}`);
|
|
198
|
+
if (parsed.positional.length !== positional)
|
|
199
|
+
throw new UsageError(`Expected ${positional} positional argument(s), got ${parsed.positional.length}`);
|
|
200
|
+
}
|
|
201
|
+
function one(parsed, flag) {
|
|
202
|
+
const value = parsed.flags.get(flag)?.[0];
|
|
203
|
+
if (!value)
|
|
204
|
+
throw new UsageError(`${flag} is required`);
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
function optional(parsed, flag) {
|
|
208
|
+
return parsed.flags.get(flag)?.[0];
|
|
209
|
+
}
|
|
210
|
+
function print(value) {
|
|
211
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
212
|
+
}
|
|
213
|
+
function printResult(result, json) {
|
|
214
|
+
if (json) {
|
|
215
|
+
const { files: _files, ...rest } = result;
|
|
216
|
+
process.stdout.write(`${JSON.stringify(rest)}\n`);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const mark = result.verdict === 'VERIFIED' ? 'ok' : 'FAIL';
|
|
220
|
+
process.stdout.write(`${mark.padEnd(4)} ${result.verdict.padEnd(17)} ${result.skillName} ${result.reason}\n`);
|
|
221
|
+
}
|
|
222
|
+
async function runRoot(sub, parsed, now) {
|
|
223
|
+
if (sub === 'init') {
|
|
224
|
+
allow(parsed, ['--root-key', '--trust-root', '--revocations', '--trust-root-expires', '--revocations-expires'], 0);
|
|
225
|
+
const result = await initRoot({
|
|
226
|
+
rootKeyPath: one(parsed, '--root-key'), trustRootOut: one(parsed, '--trust-root'), revocationsOut: one(parsed, '--revocations'), now,
|
|
227
|
+
trustRootExpires: optional(parsed, '--trust-root-expires'), revocationsExpires: optional(parsed, '--revocations-expires'),
|
|
228
|
+
});
|
|
229
|
+
print({ status: 'ROOT_INITIALIZED', root_fingerprint: result.rootKeyId, trust_root: result.trustRoot, revocations: result.revocations,
|
|
230
|
+
next: 'Store the root private key offline. Put root_fingerprint in each host policy.' });
|
|
231
|
+
return 0;
|
|
232
|
+
}
|
|
233
|
+
if (sub === 'add-approver') {
|
|
234
|
+
allow(parsed, ['--root-key', '--trust-root', '--out', '--public-key', '--name', '--scope', '--expires', '--trust-root-expires'], 0, ['--scope']);
|
|
235
|
+
const result = await addApprover({
|
|
236
|
+
rootKeyPath: one(parsed, '--root-key'), trustRootPath: one(parsed, '--trust-root'), out: optional(parsed, '--out'), now,
|
|
237
|
+
trustRootExpires: optional(parsed, '--trust-root-expires'), publicKeyPath: one(parsed, '--public-key'),
|
|
238
|
+
name: one(parsed, '--name'), scopes: parsed.flags.get('--scope') ?? [], expires: one(parsed, '--expires'),
|
|
239
|
+
});
|
|
240
|
+
print({ status: 'APPROVER_ADDED', key_id: result.keyId, trust_root_version: result.version, trust_root: result.path });
|
|
241
|
+
return 0;
|
|
242
|
+
}
|
|
243
|
+
if (sub === 'remove-approver') {
|
|
244
|
+
allow(parsed, ['--root-key', '--trust-root', '--out', '--key-id', '--trust-root-expires'], 0);
|
|
245
|
+
const result = await removeApprover({
|
|
246
|
+
rootKeyPath: one(parsed, '--root-key'), trustRootPath: one(parsed, '--trust-root'), out: optional(parsed, '--out'), now,
|
|
247
|
+
trustRootExpires: optional(parsed, '--trust-root-expires'), keyId: validateKeyId(one(parsed, '--key-id'), '--key-id'),
|
|
248
|
+
});
|
|
249
|
+
print({ status: 'APPROVER_REMOVED', trust_root_version: result.version, trust_root: result.path });
|
|
250
|
+
return 0;
|
|
251
|
+
}
|
|
252
|
+
if (sub === 'show') {
|
|
253
|
+
allow(parsed, ['--trust-root', '--root-fingerprint'], 0);
|
|
254
|
+
const trustRoot = verifyTrustRoot(await readRegularFile(one(parsed, '--trust-root'), 'Trust root', 4 * 1024 * 1024), validateKeyId(one(parsed, '--root-fingerprint'), '--root-fingerprint'), now);
|
|
255
|
+
print({
|
|
256
|
+
version: trustRoot.version, issued_at: formatTimestamp(trustRoot.issuedAt), expires_at: formatTimestamp(trustRoot.expiresAt),
|
|
257
|
+
root_fingerprint: trustRoot.root.keyId,
|
|
258
|
+
approvers: trustRoot.approvers.map((entry) => ({ name: entry.name, key_id: entry.keyId, scopes: entry.scopes, expires_at: formatTimestamp(entry.expiresAt) })),
|
|
259
|
+
});
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
throw new UsageError('Unknown root command; use init, add-approver, remove-approver or show');
|
|
263
|
+
}
|
|
264
|
+
async function runRevoke(sub, parsed, now) {
|
|
265
|
+
const common = ['--root-key', '--revocations', '--out', '--expires'];
|
|
266
|
+
const base = { rootKeyPath: '', revocationsPath: '', now };
|
|
267
|
+
if (sub !== 'key' && sub !== 'digest' && sub !== 'refresh')
|
|
268
|
+
throw new UsageError('Unknown revoke command; use key, digest or refresh');
|
|
269
|
+
if (sub === 'key')
|
|
270
|
+
allow(parsed, [...common, '--key-id', '--reason'], 0);
|
|
271
|
+
if (sub === 'digest')
|
|
272
|
+
allow(parsed, [...common, '--digest', '--reason'], 0);
|
|
273
|
+
if (sub === 'refresh')
|
|
274
|
+
allow(parsed, common, 0);
|
|
275
|
+
const result = await updateRevocations({
|
|
276
|
+
...base,
|
|
277
|
+
rootKeyPath: one(parsed, '--root-key'),
|
|
278
|
+
revocationsPath: one(parsed, '--revocations'),
|
|
279
|
+
...(optional(parsed, '--out') ? { out: one(parsed, '--out') } : {}),
|
|
280
|
+
...(optional(parsed, '--expires') ? { expires: one(parsed, '--expires') } : {}),
|
|
281
|
+
...(sub === 'key' ? { revokeKey: validateKeyId(one(parsed, '--key-id'), '--key-id'), reason: one(parsed, '--reason') } : {}),
|
|
282
|
+
...(sub === 'digest' ? { revokeDigest: one(parsed, '--digest'), reason: one(parsed, '--reason') } : {}),
|
|
283
|
+
});
|
|
284
|
+
print({ status: sub === 'refresh' ? 'REVOCATIONS_REFRESHED' : 'REVOKED', revocations_version: result.version, expires_at: formatTimestamp(result.expiresAt), revocations: result.path });
|
|
285
|
+
return 0;
|
|
286
|
+
}
|
|
287
|
+
async function runVerify(parsed, now) {
|
|
288
|
+
allow(parsed, ['--policy', '--name', '--certificate'], 1);
|
|
289
|
+
const policy = await loadPolicy(optional(parsed, '--policy') ?? DEFAULT_POLICY_PATH);
|
|
290
|
+
const skillDir = parsed.positional[0];
|
|
291
|
+
const result = await verifyWithPolicy(policy, skillDir, {
|
|
292
|
+
now, ...(optional(parsed, '--name') ? { skillName: one(parsed, '--name') } : {}),
|
|
293
|
+
...(optional(parsed, '--certificate') ? { certificate: one(parsed, '--certificate') } : {}),
|
|
294
|
+
});
|
|
295
|
+
await appendDecision(policy.decisionLogPath, decisionRecord(result, policy.mode, 'cli-verify'));
|
|
296
|
+
printResult(result, parsed.booleans.has('--json'));
|
|
297
|
+
return result.verdict === 'VERIFIED' ? 0 : 1;
|
|
298
|
+
}
|
|
299
|
+
async function runVerifyAll(parsed, now) {
|
|
300
|
+
allow(parsed, ['--policy'], 0);
|
|
301
|
+
const policy = await loadPolicy(optional(parsed, '--policy') ?? DEFAULT_POLICY_PATH);
|
|
302
|
+
if (policy.skillDirs.length === 0)
|
|
303
|
+
throw new UsageError('Policy has no skill_dirs to scan');
|
|
304
|
+
const { skillDirs, tooDeep } = await findSkillDirs(policy.skillDirs);
|
|
305
|
+
const trust = await loadPolicyTrust(policy, now);
|
|
306
|
+
let failures = 0;
|
|
307
|
+
const results = [];
|
|
308
|
+
for (const skillDir of skillDirs) {
|
|
309
|
+
results.push(trust.ok
|
|
310
|
+
? await verifyWithPolicy(policy, skillDir, { now, context: trust.context })
|
|
311
|
+
: unverifiable(skillDir, path.basename(skillDir), trust.reason, now, trust.verdict));
|
|
312
|
+
}
|
|
313
|
+
for (const directory of tooDeep)
|
|
314
|
+
results.push(tooDeepResult(directory, now));
|
|
315
|
+
for (const result of results) {
|
|
316
|
+
if (result.verdict !== 'VERIFIED')
|
|
317
|
+
failures += 1;
|
|
318
|
+
await appendDecision(policy.decisionLogPath, decisionRecord(result, policy.mode, 'cli-verify-all'));
|
|
319
|
+
printResult(result, parsed.booleans.has('--json'));
|
|
320
|
+
}
|
|
321
|
+
if (!parsed.booleans.has('--json')) {
|
|
322
|
+
process.stdout.write(`${results.length - failures}/${results.length} Skills verified (mode: ${policy.mode})\n`);
|
|
323
|
+
}
|
|
324
|
+
return failures === 0 ? 0 : 1;
|
|
325
|
+
}
|
|
326
|
+
export async function runCli(argv = process.argv.slice(2), now = new Date()) {
|
|
327
|
+
if (argv[0] === '--version' || argv[0] === '-V' || argv[0] === 'version') {
|
|
328
|
+
process.stdout.write(`mago-ski ${readVersion()}\n`);
|
|
329
|
+
return 0;
|
|
330
|
+
}
|
|
331
|
+
// "mago-ski help verify" is the same as "mago-ski verify --help".
|
|
332
|
+
if (argv[0] === 'help' && argv.length > 1)
|
|
333
|
+
argv = [...argv.slice(1), '--help'];
|
|
334
|
+
if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h' || argv[0] === 'help') {
|
|
335
|
+
process.stdout.write(`${usage}\n`);
|
|
336
|
+
return 0;
|
|
337
|
+
}
|
|
338
|
+
const [command, maybeSub] = argv;
|
|
339
|
+
if (argv.slice(1).some((arg) => arg === '--help' || arg === '-h')) {
|
|
340
|
+
const sub = maybeSub && !maybeSub.startsWith('-') ? `${command} ${maybeSub}` : undefined;
|
|
341
|
+
const text = (sub && COMMAND_HELP[sub]) ?? COMMAND_HELP[command];
|
|
342
|
+
if (!text)
|
|
343
|
+
throw new UsageError(`Unknown command: ${command}`);
|
|
344
|
+
process.stdout.write(`${text}\n`);
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
|
347
|
+
const hasSub = command === 'root' || command === 'revoke';
|
|
348
|
+
const parsed = parseArgs(argv.slice(hasSub ? 2 : 1));
|
|
349
|
+
switch (command) {
|
|
350
|
+
case 'keygen': {
|
|
351
|
+
allow(parsed, ['--private-key', '--public-key'], 0);
|
|
352
|
+
const pair = await generateKeyPair(one(parsed, '--private-key'), one(parsed, '--public-key'));
|
|
353
|
+
print({ status: 'KEY_CREATED', key_id: pair.keyId, private_key: pair.privatePath, public_key: pair.publicPath });
|
|
354
|
+
process.stderr.write('Keep the private key secret and outside any Skill directory.\n');
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
case 'root':
|
|
358
|
+
return await runRoot(maybeSub, parsed, now);
|
|
359
|
+
case 'revoke':
|
|
360
|
+
return await runRevoke(maybeSub, parsed, now);
|
|
361
|
+
case 'approve': {
|
|
362
|
+
allow(parsed, ['--key', '--expires', '--reason', '--name', '--previous', '--out'], 1);
|
|
363
|
+
const result = await approveSkill({
|
|
364
|
+
skillDir: parsed.positional[0], approverKeyPath: one(parsed, '--key'), now, expires: one(parsed, '--expires'),
|
|
365
|
+
reason: one(parsed, '--reason'),
|
|
366
|
+
...(optional(parsed, '--name') ? { name: one(parsed, '--name') } : {}),
|
|
367
|
+
...(optional(parsed, '--previous') ? { previous: one(parsed, '--previous') } : {}),
|
|
368
|
+
...(optional(parsed, '--out') ? { out: one(parsed, '--out') } : {}),
|
|
369
|
+
});
|
|
370
|
+
print({ status: 'CERTIFICATE_ISSUED', skill: result.skillName, digest: result.digest, certificate_id: result.certificateId,
|
|
371
|
+
expires_at: formatTimestamp(result.expiresAt), certificate: result.path });
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
case 'verify':
|
|
375
|
+
return await runVerify(parsed, now);
|
|
376
|
+
case 'verify-all':
|
|
377
|
+
return await runVerifyAll(parsed, now);
|
|
378
|
+
case 'digest': {
|
|
379
|
+
allow(parsed, [], 1);
|
|
380
|
+
const tree = await collectSkillTree(parsed.positional[0]);
|
|
381
|
+
if (parsed.booleans.has('--explain')) {
|
|
382
|
+
print({ profile: TREE_PROFILE, digest: tree.digest, root: tree.root, total_bytes: tree.totalBytes, files: tree.files, excluded: tree.excluded });
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
process.stdout.write(`${tree.digest}\n`);
|
|
386
|
+
}
|
|
387
|
+
return 0;
|
|
388
|
+
}
|
|
389
|
+
case 'diff': {
|
|
390
|
+
allow(parsed, ['--base', '--candidate'], 0);
|
|
391
|
+
const result = await diffSkillTrees(one(parsed, '--base'), one(parsed, '--candidate'));
|
|
392
|
+
print(result);
|
|
393
|
+
return result.recertification_required ? 1 : 0;
|
|
394
|
+
}
|
|
395
|
+
case 'inspect': {
|
|
396
|
+
allow(parsed, [], 1);
|
|
397
|
+
const certificate = parseCertificate(await readRegularFile(parsed.positional[0], 'Certificate', 8 * 1024 * 1024));
|
|
398
|
+
print({
|
|
399
|
+
certificate_id: certificate.id, skill: certificate.skillName, digest: certificate.digest, approver_key_id: certificate.approverKeyId,
|
|
400
|
+
issued_at: formatTimestamp(certificate.issuedAt), expires_at: formatTimestamp(certificate.expiresAt), reason: certificate.reason,
|
|
401
|
+
previous_digest: certificate.previousDigest, file_count: certificate.files.length,
|
|
402
|
+
note: 'Structure only. Run mago-ski verify to check the signature against your trust root.',
|
|
403
|
+
});
|
|
404
|
+
return 0;
|
|
405
|
+
}
|
|
406
|
+
default:
|
|
407
|
+
throw new UsageError(`Unknown command: ${command}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (isMainModule(import.meta.url)) {
|
|
411
|
+
runCli().then((code) => {
|
|
412
|
+
process.exitCode = code;
|
|
413
|
+
}, (error) => {
|
|
414
|
+
process.stderr.write(`mago-ski: ${error?.message ?? 'operation failed'}\n`);
|
|
415
|
+
if (error instanceof UsageError)
|
|
416
|
+
process.stderr.write('Run "mago-ski --help" for usage.\n');
|
|
417
|
+
process.exitCode = 2;
|
|
418
|
+
});
|
|
419
|
+
}
|