@tpsdev-ai/flair 0.62.0 → 0.63.1
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/README.md +14 -13
- package/SECURITY.md +73 -43
- package/config.yaml +11 -11
- package/dist/bridges/runtime/execute.js +1 -0
- package/dist/bridges/runtime/import-runner.js +4 -0
- package/dist/bridges/types.js +1 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +309 -205
- package/dist/commands/agent.js +325 -137
- package/dist/commands/attention.js +2 -0
- package/dist/commands/backup-approval.js +218 -0
- package/dist/commands/backup.js +37 -33
- package/dist/commands/bootstrap.js +5 -1
- package/dist/commands/bridge.js +10 -0
- package/dist/commands/deploy.js +37 -7
- package/dist/commands/doctor.js +206 -42
- package/dist/commands/export.js +144 -15
- package/dist/commands/federation.js +653 -68
- package/dist/commands/grant.js +5 -6
- package/dist/commands/help.js +25 -0
- package/dist/commands/idp.js +14 -19
- package/dist/commands/import.js +167 -34
- package/dist/commands/init.js +380 -164
- package/dist/commands/integrity.js +5 -6
- package/dist/commands/keys.js +12 -8
- package/dist/commands/mcp.js +157 -79
- package/dist/commands/memory.js +38 -17
- package/dist/commands/migrate-harness-memory.js +9 -3
- package/dist/commands/orgevent.js +5 -10
- package/dist/commands/principal.js +32 -39
- package/dist/commands/quality.js +26 -3
- package/dist/commands/reembed.js +468 -111
- package/dist/commands/rem.js +484 -65
- package/dist/commands/restore.js +352 -44
- package/dist/commands/search.js +2 -0
- package/dist/commands/service.js +109 -2
- package/dist/commands/soul.js +0 -6
- package/dist/commands/status.js +165 -124
- package/dist/commands/test.js +4 -1
- package/dist/commands/upgrade.js +53 -22
- package/dist/commands/workspace.js +7 -8
- package/dist/deploy.js +59 -3
- package/dist/doctor-client.js +9 -25
- package/dist/fabric-upgrade.js +12 -2
- package/dist/federation/presence-send.js +139 -0
- package/dist/federation/scheduler.js +148 -12
- package/dist/federation-verify.js +269 -177
- package/dist/install/clients.js +171 -35
- package/dist/keystore.js +77 -10
- package/dist/lib/agent-id-rule.js +44 -2
- package/dist/lib/agent-key-file.js +509 -0
- package/dist/lib/agent-rotation-lock.js +249 -0
- package/dist/lib/atomic-write.js +3 -3
- package/dist/lib/auth-resolve.js +1040 -64
- package/dist/lib/backup-approval-store.js +612 -0
- package/dist/lib/backup-artifact.js +343 -0
- package/dist/lib/config-critical-section.js +212 -25
- package/dist/lib/connect-phase.js +33 -0
- package/dist/lib/daemon-liveness.js +2 -1
- package/dist/lib/federation-pair-access.js +4 -0
- package/dist/lib/federation-retry-queue.js +283 -0
- package/dist/lib/federation-retry-row.js +101 -0
- package/dist/lib/instruction-event-contract.js +17 -0
- package/dist/lib/instruction-event-doctor.js +102 -0
- package/dist/lib/key-dir.js +203 -0
- package/dist/lib/launchd-management.js +21 -6
- package/dist/lib/local-instance-pair.js +8 -1
- package/dist/lib/mcp-enable.js +14 -15
- package/dist/lib/memory-attributes.js +2 -0
- package/dist/lib/memory-provider-timeout.js +33 -0
- package/dist/lib/mqtt-config.js +132 -0
- package/dist/lib/mqtt-listener-doctor.js +104 -0
- package/dist/lib/node-alias-path.js +50 -2
- package/dist/lib/open-live-row.js +20 -0
- package/dist/lib/ops-url.js +52 -0
- package/dist/lib/promotion-content-hash.js +20 -0
- package/dist/lib/remote-admin-pass.js +157 -0
- package/dist/lib/restore-archive.js +760 -0
- package/dist/lib/restore-verify.js +1 -1
- package/dist/lib/scheduler-platform.js +13 -0
- package/dist/lib/service-repoint.js +13 -4
- package/dist/lib/skill-assignment-preflight.js +159 -0
- package/dist/lib/skill-seed.js +125 -10
- package/dist/lib/skill-tombstone-doctor.js +133 -0
- package/dist/lib/soul-scope-fields.js +26 -0
- package/dist/lib/terminal-text.js +32 -0
- package/dist/lib/uninstall-purge.js +266 -11
- package/dist/lib/upgrade-prior-liveness.js +2 -1
- package/dist/mcp-client-assertion.js +7 -14
- package/dist/memory-id-checkpoint.js +28 -7
- package/dist/mqtt-off-deploy.js +550 -0
- package/dist/rem/promote-policy.js +3 -3
- package/dist/rem/restore.js +302 -26
- package/dist/rem/runner.js +220 -58
- package/dist/rem/scheduler.js +21 -4
- package/dist/resources/A2AAdapter.js +131 -178
- package/dist/resources/AdminMemory.js +2 -0
- package/dist/resources/AdminMemoryCandidates.js +116 -0
- package/dist/resources/Agent.js +155 -45
- package/dist/resources/AgentCard.js +4 -0
- package/dist/resources/AgentKeyRegister.js +56 -22
- package/dist/resources/AgentReadPosition.js +7 -0
- package/dist/resources/AgentSeed.js +232 -107
- package/dist/resources/AutoPromoteCandidates.js +413 -84
- package/dist/resources/Credential.js +6 -8
- package/dist/resources/Federation.js +219 -57
- package/dist/resources/FederationRetry.js +4 -4
- package/dist/resources/InstructionVersion.js +1 -1
- package/dist/resources/MCPClientMetadata.js +6 -3
- package/dist/resources/Memory.js +1348 -271
- package/dist/resources/MemoryArchive.js +149 -64
- package/dist/resources/MemoryBootstrap.js +12 -22
- package/dist/resources/MemoryCandidate.js +16 -2
- package/dist/resources/MemoryConsolidate.js +2 -2
- package/dist/resources/MemoryFeed.js +360 -147
- package/dist/resources/MemoryMaintenance.js +290 -72
- package/dist/resources/MemoryPurge.js +138 -22
- package/dist/resources/MemoryReflect.js +68 -14
- package/dist/resources/MemoryReindex.js +91 -13
- package/dist/resources/Message.js +11 -2
- package/dist/resources/OAuth.js +156 -39
- package/dist/resources/OAuthClient.js +1 -1
- package/dist/resources/OrgEvent.js +292 -29
- package/dist/resources/OrgEventCatchup.js +7 -0
- package/dist/resources/OrgEventMaintenance.js +48 -10
- package/dist/resources/PairingToken.js +1 -1
- package/dist/resources/Presence.js +31 -11
- package/dist/resources/PromoteMemoryCandidate.js +8 -2
- package/dist/resources/RecordUsage.js +2 -4
- package/dist/resources/Soul.js +2 -0
- package/dist/resources/SoulFeed.js +2 -1
- package/dist/resources/WorkspaceLatest.js +4 -0
- package/dist/resources/XAA.js +36 -10
- package/dist/resources/a2a-stream.js +313 -0
- package/dist/resources/a2a-url.js +4 -5
- package/dist/resources/agent-admin.js +10 -1
- package/dist/resources/agent-auth.js +98 -68
- package/dist/resources/agent-read-position-lib.js +4 -1
- package/dist/resources/agent-status-guard.js +9 -7
- package/dist/resources/auth-middleware.js +166 -42
- package/dist/resources/authority-field-guard.js +9 -11
- package/dist/resources/auto-promote-lib.js +8 -5
- package/dist/resources/basic-auth-header.js +21 -0
- package/dist/resources/bm25-index-service.js +4 -5
- package/dist/resources/dedup-cluster.js +6 -1
- package/dist/resources/embeddings-provider.js +25 -0
- package/dist/resources/federation-classify.js +89 -0
- package/dist/resources/federation-retry-lib.js +6 -81
- package/dist/resources/health.js +75 -19
- package/dist/resources/host-pointer/registry.js +29 -0
- package/dist/resources/host-pointer-adapter.js +1 -0
- package/dist/resources/in-process-api.js +74 -11
- package/dist/resources/in-process.js +24 -22
- package/dist/resources/instruction-version-record.js +251 -7
- package/dist/resources/internal-authority.js +13 -0
- package/dist/resources/mcp-client-metadata-fields.js +4 -0
- package/dist/resources/mcp-handler.js +146 -13
- package/dist/resources/mcp-oauth-flag.js +12 -14
- package/dist/resources/mcp-oauth.js +446 -53
- package/dist/resources/mcp-tool-scopes.js +69 -0
- package/dist/resources/mcp-tools.js +70 -22
- package/dist/resources/memory-declared-attributes.js +1 -0
- package/dist/resources/memory-embed-hash.js +56 -0
- package/dist/resources/memory-host-source.js +5 -2
- package/dist/resources/memory-reflect-lib.js +1 -1
- package/dist/resources/memory-write-lock.js +202 -23
- package/dist/resources/memory-write-response.js +1 -1
- package/dist/resources/memory-write-transaction.js +243 -0
- package/dist/resources/migration-boot.js +17 -12
- package/dist/resources/migrations/boot-progress.js +25 -0
- package/dist/resources/migrations/data-dir.js +16 -3
- package/dist/resources/migrations/dir-safety.js +60 -2
- package/dist/resources/migrations/embedding-stamp.js +315 -49
- package/dist/resources/migrations/graph-heal.js +9 -2
- package/dist/resources/migrations/ledger.js +5 -2
- package/dist/resources/migrations/lock.js +867 -80
- package/dist/resources/migrations/progress.js +71 -10
- package/dist/resources/migrations/runner.js +212 -18
- package/dist/resources/migrations/state.js +36 -1
- package/dist/resources/migrations/status.js +2 -13
- package/dist/resources/migrations/synthetic-test-migration.js +78 -12
- package/dist/resources/migrations/types.js +26 -1
- package/dist/resources/migrations/visibility-backfill.js +111 -70
- package/dist/resources/oauth-discovery.js +0 -5
- package/dist/resources/object-body-guard.js +12 -5
- package/dist/resources/org-event-reserved.js +92 -0
- package/dist/resources/org-event-write-fault.js +19 -0
- package/dist/resources/owner-field-guard.js +1 -1
- package/dist/resources/owner-scoped-write-policy.js +65 -21
- package/dist/resources/owner-scoped-writes.js +37 -6
- package/dist/resources/path-id-rewrite.js +76 -0
- package/dist/resources/presence-internal.js +5 -5
- package/dist/resources/promotion-binding.js +43 -0
- package/dist/resources/promotion-stamp.js +124 -33
- package/dist/resources/protocol-context.js +189 -0
- package/dist/resources/record-type-kit.js +2 -4
- package/dist/resources/request-target.js +28 -1
- package/dist/resources/request-transaction.js +11 -1
- package/dist/resources/seed-reservation.js +1 -2
- package/dist/resources/semantic-retrieval-core.js +7 -6
- package/dist/resources/server-row-validation.js +71 -0
- package/dist/resources/skill-manifest.js +4 -3
- package/dist/resources/skill-version-write.js +400 -27
- package/dist/resources/skill-write-fault.js +6 -3
- package/dist/resources/skill-write-policy.js +2 -1
- package/dist/resources/soul-write-policy.js +26 -7
- package/dist/resources/table-helpers.js +1 -1
- package/dist/resources/table-subscription-policy.js +518 -85
- package/dist/resources/table-subscriptions.js +20 -7
- package/dist/resources/txn-pause-point.js +122 -5
- package/dist/resources/usage-recording.js +54 -46
- package/dist/resources/write-back.js +176 -11
- package/dist/resources/write-journal-context.js +5 -0
- package/dist/resources/write-journal.js +289 -0
- package/dist/src/keystore.js +77 -10
- package/dist/src/lib/agent-id-rule.js +44 -2
- package/dist/src/lib/atomic-write.js +3 -3
- package/dist/src/lib/federation-retry-row.js +101 -0
- package/dist/src/lib/instruction-event-contract.js +17 -0
- package/dist/src/lib/key-dir.js +203 -0
- package/dist/src/lib/memory-attributes.js +2 -0
- package/dist/src/lib/open-live-row.js +20 -0
- package/dist/src/lib/scheduler-platform.js +13 -0
- package/dist/src/lib/soul-scope-fields.js +26 -0
- package/dist/src/rem/promote-policy.js +3 -3
- package/dist/src/rem/scheduler.js +21 -4
- package/docs/access-control.md +39 -21
- package/docs/api-reference.md +161 -52
- package/docs/auth.md +69 -38
- package/docs/claude-code.md +3 -3
- package/docs/deploying-on-fabric.md +118 -2
- package/docs/deployment.md +17 -0
- package/docs/embedding-in-a-harper-app.md +23 -21
- package/docs/entity-vocabulary.md +7 -11
- package/docs/federation.md +29 -2
- package/docs/hosted-on-fabric.md +1 -1
- package/docs/integrations.md +11 -11
- package/docs/mcp-clients.md +7 -5
- package/docs/notes/mcp-agent-auth-consumer.md +4 -9
- package/docs/notes/mcp-oauth-model2.md +6 -6
- package/docs/openclaw.md +1 -1
- package/docs/quickstart-fabric.md +2 -2
- package/docs/quickstart.md +3 -3
- package/docs/releasing.md +13 -13
- package/docs/rem.md +24 -3
- package/docs/secrets-and-keys.md +9 -14
- package/docs/standalone-local.md +29 -2
- package/docs/supply-chain-policy.md +28 -20
- package/docs/the-team.md +2 -2
- package/docs/upgrade.md +29 -5
- package/package.json +18 -11
- package/schemas/agent.graphql +3 -3
- package/schemas/federation.graphql +10 -7
- package/schemas/memory.graphql +7 -2
- package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
- package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
- package/dist/resources/soul-adk-guard.js +0 -68
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-key-file.ts — the shared agent private-key file helper for the five
|
|
3
|
+
* provisioning paths (flair#3077 slice A): `flair agent add`, local
|
|
4
|
+
* `flair init --agent/--agent-id`, remote `flair init --target ... --agent-id`,
|
|
5
|
+
* `flair principal add`, and `flair import`. `flair agent rotate-key` (slice B)
|
|
6
|
+
* stages and replaces through this helper; `flair mcp grant` creates its key
|
|
7
|
+
* files through it (slice C).
|
|
8
|
+
*
|
|
9
|
+
* POSIX policy:
|
|
10
|
+
* - the keys directory is requested with mode 0700 (the umask may make it
|
|
11
|
+
* narrower); an existing directory owned by the current user is tightened
|
|
12
|
+
* to 0700 when it has group or world bits, with one notice line; a symlink,
|
|
13
|
+
* a non-directory, or a directory owned by another user is refused.
|
|
14
|
+
* - provisioning writes a new key to a unique temp file in the keys directory
|
|
15
|
+
* (O_CREAT|O_EXCL|O_NOFOLLOW, mode 0600), fsynced, then published with a
|
|
16
|
+
* hard link at the final path, so an occupied path is never overwritten.
|
|
17
|
+
* - an existing key is checked on the OPENED DESCRIPTOR: a regular file,
|
|
18
|
+
* owned by the current user, with no group/world bits. It is read and left
|
|
19
|
+
* untouched; a file with group or world bits is refused before its bytes
|
|
20
|
+
* are read.
|
|
21
|
+
*
|
|
22
|
+
* win32: mode bits do not establish owner-only there; the create path sets the
|
|
23
|
+
* mode and the reuse path neither checks nor sets it.
|
|
24
|
+
*/
|
|
25
|
+
import { randomBytes } from "node:crypto";
|
|
26
|
+
import { chmodSync, closeSync, constants, fchmodSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, renameSync, unlinkSync, writeSync, } from "node:fs";
|
|
27
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
28
|
+
/** A refusal is operator-facing: the caller prints the message and exits 1. */
|
|
29
|
+
export class AgentKeyFileRefusal extends Error {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "AgentKeyFileRefusal";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
let testHooks;
|
|
36
|
+
export function setAgentKeyFileTestHooks(hooks) {
|
|
37
|
+
testHooks = hooks;
|
|
38
|
+
}
|
|
39
|
+
const WIN32 = process.platform === "win32";
|
|
40
|
+
const NOFOLLOW = WIN32 ? 0 : constants.O_NOFOLLOW;
|
|
41
|
+
const O_NONBLOCK = WIN32 ? 0 : constants.O_NONBLOCK;
|
|
42
|
+
function errnoCode(error) {
|
|
43
|
+
const code = error?.code;
|
|
44
|
+
return typeof code === "string" ? code : undefined;
|
|
45
|
+
}
|
|
46
|
+
function currentUid() {
|
|
47
|
+
return typeof process.getuid === "function" ? process.getuid() : undefined;
|
|
48
|
+
}
|
|
49
|
+
function modeText(mode) {
|
|
50
|
+
return (mode & 0o777).toString(8).padStart(3, "0");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Refuse a symlink, a non-directory or another owner; tighten an owned
|
|
54
|
+
* directory with group or world bits. Shared by an existing directory and one
|
|
55
|
+
* this call just created, so neither is trusted on the strength of its path.
|
|
56
|
+
*/
|
|
57
|
+
function checkAgentKeysDir(keysDir) {
|
|
58
|
+
const st = lstatSync(keysDir);
|
|
59
|
+
if (st.isSymbolicLink()) {
|
|
60
|
+
throw new AgentKeyFileRefusal(`keys directory ${keysDir} is a symlink; replace it with a real directory you own (mode 700)`);
|
|
61
|
+
}
|
|
62
|
+
if (!st.isDirectory()) {
|
|
63
|
+
throw new AgentKeyFileRefusal(`keys directory ${keysDir} is not a directory; move it aside and run again so flair can create a directory (mode 700)`);
|
|
64
|
+
}
|
|
65
|
+
const uid = currentUid();
|
|
66
|
+
if (uid !== undefined && st.uid !== uid) {
|
|
67
|
+
throw new AgentKeyFileRefusal(`keys directory ${keysDir} is not owned by the current user; move it aside and run again so flair can create a directory (mode 700)`);
|
|
68
|
+
}
|
|
69
|
+
if ((st.mode & 0o077) !== 0) {
|
|
70
|
+
chmodSync(keysDir, 0o700);
|
|
71
|
+
console.log(`Keys directory ${keysDir} was accessible to other users; permissions tightened to 0700.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* On POSIX, request mode 0700 for a missing `keysDir` (the umask may make it
|
|
76
|
+
* narrower), check the path after creation like an existing directory, sync
|
|
77
|
+
* the created directory's parent, and tighten an owned directory; a symlink, a
|
|
78
|
+
* non-directory or another owner is refused. A retry with `keysDir` already
|
|
79
|
+
* present repeats the parent sync. On Windows the directory is created and
|
|
80
|
+
* returned unchecked.
|
|
81
|
+
*/
|
|
82
|
+
export function ensureAgentKeysDir(keysDir) {
|
|
83
|
+
if (WIN32) {
|
|
84
|
+
mkdirSync(keysDir, { recursive: true });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
let missing = false;
|
|
88
|
+
try {
|
|
89
|
+
lstatSync(keysDir);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (errnoCode(error) !== "ENOENT")
|
|
93
|
+
throw error;
|
|
94
|
+
missing = true;
|
|
95
|
+
}
|
|
96
|
+
if (missing) {
|
|
97
|
+
// The directory is owner-only from creation: the umask can only narrow the
|
|
98
|
+
// mode, never widen it past 0700. No later chmod is involved.
|
|
99
|
+
testHooks?.beforeKeysDirCreate?.(keysDir);
|
|
100
|
+
const firstCreated = mkdirSync(keysDir, { recursive: true, mode: 0o700 });
|
|
101
|
+
testHooks?.afterKeysDirCreate?.(keysDir);
|
|
102
|
+
// A recursive mkdir also succeeds when another process created the path
|
|
103
|
+
// first, so the path is checked like any existing directory before use.
|
|
104
|
+
checkAgentKeysDir(keysDir);
|
|
105
|
+
if (firstCreated !== undefined) {
|
|
106
|
+
for (let created = keysDir;; created = dirname(created)) {
|
|
107
|
+
if ([".", ".."].includes(basename(created)))
|
|
108
|
+
continue;
|
|
109
|
+
syncKeysParent(created, `new keys directory ${keysDir}`);
|
|
110
|
+
if (resolve(created) === resolve(firstCreated))
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// Another process created the directory between the check and this
|
|
116
|
+
// mkdir; its entry in its parent still needs the same sync.
|
|
117
|
+
syncKeysParent(keysDir, `keys directory ${keysDir}`);
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
checkAgentKeysDir(keysDir);
|
|
122
|
+
// A retry repeats the sync of the keys directory's own entry in its parent.
|
|
123
|
+
// Upper levels a recursive first run created are synced only by that first run.
|
|
124
|
+
syncKeysParent(keysDir, `keys directory ${keysDir}`);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* On POSIX, sync the parent of a directory entry so the entry survives a
|
|
128
|
+
* crash; a failure refuses with `subject` named as unconfirmed durable.
|
|
129
|
+
*/
|
|
130
|
+
function syncKeysParent(created, subject) {
|
|
131
|
+
let parent = dirname(created);
|
|
132
|
+
try {
|
|
133
|
+
parent = realpathSync(parent);
|
|
134
|
+
syncAgentKeysDir(parent);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
throw new AgentKeyFileRefusal(`flair could not sync parent directory ${parent} (${error instanceof Error ? error.message : String(error)}); ` +
|
|
138
|
+
`${subject} is not confirmed durable; check the directory and filesystem, then retry`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Read an existing key, or return null when the path does not exist. On POSIX,
|
|
143
|
+
* check the opened descriptor and refuse symlinks. Windows reads by path.
|
|
144
|
+
*/
|
|
145
|
+
export function readExistingAgentKey(privateKeyPath) {
|
|
146
|
+
if (WIN32) {
|
|
147
|
+
try {
|
|
148
|
+
return new Uint8Array(readFileSync(privateKeyPath));
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
if (errnoCode(error) === "ENOENT")
|
|
152
|
+
return null;
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// O_NOFOLLOW covers the last component; lstat gives the clearer refusal for
|
|
157
|
+
// a link that was already there when the command started.
|
|
158
|
+
try {
|
|
159
|
+
if (lstatSync(privateKeyPath).isSymbolicLink()) {
|
|
160
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is a symlink; remove it and run again so flair can create a new owner-only key`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (error instanceof AgentKeyFileRefusal)
|
|
165
|
+
throw error;
|
|
166
|
+
if (errnoCode(error) !== "ENOENT")
|
|
167
|
+
throw error;
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
let fd;
|
|
171
|
+
try {
|
|
172
|
+
fd = openSync(privateKeyPath, constants.O_RDONLY | NOFOLLOW | O_NONBLOCK);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (errnoCode(error) === "ELOOP") {
|
|
176
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is a symlink; remove it and run again so flair can create a new owner-only key`);
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
const st = fstatSync(fd);
|
|
182
|
+
if (!st.isFile()) {
|
|
183
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is not a regular file; remove it and run again so flair can create a new owner-only key`);
|
|
184
|
+
}
|
|
185
|
+
const uid = currentUid();
|
|
186
|
+
if (uid !== undefined && st.uid !== uid) {
|
|
187
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is owned by another user; move it aside and run again so flair can create a new owner-only key`);
|
|
188
|
+
}
|
|
189
|
+
if ((st.mode & 0o077) !== 0) {
|
|
190
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is accessible to other users (mode ${modeText(st.mode)}); ` +
|
|
191
|
+
`run chmod 600 ${privateKeyPath} to make it owner-only`);
|
|
192
|
+
}
|
|
193
|
+
testHooks?.beforeKeyRead?.(fd);
|
|
194
|
+
return new Uint8Array(readFileSync(fd));
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
closeSync(fd);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* On POSIX, read through a no-follow descriptor and refuse symlinks,
|
|
202
|
+
* non-regular files and foreign owners; Windows reads by path.
|
|
203
|
+
* A wide POSIX mode is reported; successful rotation replaces the file.
|
|
204
|
+
*/
|
|
205
|
+
export function readAgentKeyForRotation(privateKeyPath) {
|
|
206
|
+
if (WIN32) {
|
|
207
|
+
try {
|
|
208
|
+
return { bytes: new Uint8Array(readFileSync(privateKeyPath)), widerThanOwnerOnly: false };
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (errnoCode(error) === "ENOENT")
|
|
212
|
+
return null;
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
let fd;
|
|
217
|
+
try {
|
|
218
|
+
fd = openSync(privateKeyPath, constants.O_RDONLY | NOFOLLOW | O_NONBLOCK);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (errnoCode(error) === "ENOENT")
|
|
222
|
+
return null;
|
|
223
|
+
if (errnoCode(error) === "ELOOP") {
|
|
224
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is a symlink; replace it with a real key file you own and run again`);
|
|
225
|
+
}
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const st = fstatSync(fd);
|
|
230
|
+
if (!st.isFile()) {
|
|
231
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is not a regular file; replace it with a real key file you own and run again`);
|
|
232
|
+
}
|
|
233
|
+
const uid = currentUid();
|
|
234
|
+
if (uid !== undefined && st.uid !== uid) {
|
|
235
|
+
throw new AgentKeyFileRefusal(`key file ${privateKeyPath} is owned by another user; move it aside and run again so flair can create a new owner-only key`);
|
|
236
|
+
}
|
|
237
|
+
testHooks?.beforeKeyRead?.(fd);
|
|
238
|
+
return { bytes: new Uint8Array(readFileSync(fd)), widerThanOwnerOnly: (st.mode & 0o077) !== 0 };
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
closeSync(fd);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Write `data` to a unique temp file in `privateKeyPath`'s directory, at 0600
|
|
246
|
+
* from creation and fsynced, and return the temp path. The temp file is this
|
|
247
|
+
* call's to hand on: the caller publishes or removes it.
|
|
248
|
+
*/
|
|
249
|
+
function writeAgentKeyTemp(privateKeyPath, bytes, onCreated) {
|
|
250
|
+
const tempPrefix = `.${basename(privateKeyPath)}.tmp-`;
|
|
251
|
+
const maxTempAttempts = 5;
|
|
252
|
+
let fd;
|
|
253
|
+
let tempPath = "";
|
|
254
|
+
let tempCreated = false;
|
|
255
|
+
let attempt = 0;
|
|
256
|
+
while (fd === undefined) {
|
|
257
|
+
attempt += 1;
|
|
258
|
+
const candidate = join(dirname(privateKeyPath), `${tempPrefix}${randomBytes(8).toString("hex")}`);
|
|
259
|
+
try {
|
|
260
|
+
fd = openSync(candidate, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | NOFOLLOW, 0o600);
|
|
261
|
+
tempPath = candidate;
|
|
262
|
+
tempCreated = true;
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
if (errnoCode(error) === "EEXIST" && attempt < maxTempAttempts)
|
|
266
|
+
continue;
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
const created = fstatSync(fd);
|
|
272
|
+
const identity = { dev: created.dev, ino: created.ino };
|
|
273
|
+
onCreated?.(tempPath, identity);
|
|
274
|
+
// The open mode is narrowed by the umask; set it exactly, still before the
|
|
275
|
+
// first byte, so the protection is the mode the file is created with.
|
|
276
|
+
fchmodSync(fd, 0o600);
|
|
277
|
+
testHooks?.beforeFirstWrite?.(fd);
|
|
278
|
+
const writer = testHooks?.writeSeedFile ?? ((target, value) => writeSync(target, value));
|
|
279
|
+
let offset = 0;
|
|
280
|
+
while (offset < bytes.length) {
|
|
281
|
+
const written = writer(fd, bytes.subarray(offset));
|
|
282
|
+
if (written <= 0) {
|
|
283
|
+
throw new Error(`short write to key file ${privateKeyPath}: wrote ${written} bytes with ${bytes.length - offset} left`);
|
|
284
|
+
}
|
|
285
|
+
offset += written;
|
|
286
|
+
}
|
|
287
|
+
fsyncSync(fd);
|
|
288
|
+
closeSync(fd);
|
|
289
|
+
fd = undefined;
|
|
290
|
+
return { path: tempPath, identity };
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (fd !== undefined) {
|
|
294
|
+
try {
|
|
295
|
+
closeSync(fd);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
/* already failing */
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (tempCreated) {
|
|
302
|
+
try {
|
|
303
|
+
unlinkSync(tempPath);
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
/* best effort: remove only the temp file this call created */
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Create a new key file at 0600 from the first byte, refusing to replace an
|
|
314
|
+
* existing path. The bytes go to a unique temp file in the same directory and
|
|
315
|
+
* are published with a hard link, so an occupied path is never overwritten.
|
|
316
|
+
* A temp name that is already taken is retried with a new random suffix; the
|
|
317
|
+
* occupying entry is left untouched. On failure the temp file this call
|
|
318
|
+
* created is removed, best effort.
|
|
319
|
+
*/
|
|
320
|
+
export function createAgentKeyFile(privateKeyPath, data, onCreated) {
|
|
321
|
+
const temp = writeAgentKeyTemp(privateKeyPath, Buffer.from(data), onCreated);
|
|
322
|
+
try {
|
|
323
|
+
testHooks?.beforePublish?.(temp.path, privateKeyPath);
|
|
324
|
+
// linkSync fails with EEXIST when the final path appears before publication.
|
|
325
|
+
linkSync(temp.path, privateKeyPath);
|
|
326
|
+
testHooks?.afterPublish?.(privateKeyPath);
|
|
327
|
+
// The hard link initially publishes the temp file's inode at the final
|
|
328
|
+
// path; the identity recorded from the temp descriptor stays that inode
|
|
329
|
+
// even if the path is replaced later. Never re-derive it from the path.
|
|
330
|
+
onCreated?.(privateKeyPath, temp.identity);
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
try {
|
|
334
|
+
unlinkSync(temp.path);
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
/* best effort: remove only the temp file this call created */
|
|
338
|
+
}
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
unlinkSync(temp.path);
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
/* the published key is complete; a leftover temp file is mode 0600 */
|
|
346
|
+
}
|
|
347
|
+
// On POSIX, sync the key's directory before returning so the entry is durable
|
|
348
|
+
// before the caller registers the key; a failure refuses.
|
|
349
|
+
try {
|
|
350
|
+
syncAgentKeysDir(dirname(privateKeyPath));
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
throw new AgentKeyFileRefusal(`flair could not sync keys directory ${dirname(privateKeyPath)} (${error instanceof Error ? error.message : String(error)}); ` +
|
|
354
|
+
`published key ${privateKeyPath} is not confirmed durable; check the directory and filesystem, then retry`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
export function createAgentKeyBackup(privateKeyPath, data) {
|
|
358
|
+
const bytes = Buffer.from(data);
|
|
359
|
+
// Exclusive creation preserves occupied backups without requiring hard links.
|
|
360
|
+
const fd = openSync(privateKeyPath, constants.O_RDWR | constants.O_CREAT | constants.O_EXCL | NOFOLLOW, 0o600);
|
|
361
|
+
try {
|
|
362
|
+
fchmodSync(fd, 0o600);
|
|
363
|
+
testHooks?.beforeFirstWrite?.(fd);
|
|
364
|
+
const writer = testHooks?.writeSeedFile ?? ((target, value) => writeSync(target, value));
|
|
365
|
+
let offset = 0;
|
|
366
|
+
while (offset < bytes.length) {
|
|
367
|
+
const written = writer(fd, bytes.subarray(offset));
|
|
368
|
+
if (written <= 0)
|
|
369
|
+
throw new Error(`short write to backup ${privateKeyPath}`);
|
|
370
|
+
offset += written;
|
|
371
|
+
}
|
|
372
|
+
fsyncSync(fd);
|
|
373
|
+
const reread = Buffer.alloc(bytes.length);
|
|
374
|
+
offset = 0;
|
|
375
|
+
while (offset < reread.length) {
|
|
376
|
+
const count = readSync(fd, reread, offset, reread.length - offset, offset);
|
|
377
|
+
if (count === 0)
|
|
378
|
+
throw new Error(`short read from backup ${privateKeyPath}`);
|
|
379
|
+
offset += count;
|
|
380
|
+
}
|
|
381
|
+
if (fstatSync(fd).size !== bytes.length || !reread.equals(bytes)) {
|
|
382
|
+
throw new Error(`backup ${privateKeyPath} does not match the current key`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
try {
|
|
387
|
+
unlinkSync(privateKeyPath);
|
|
388
|
+
}
|
|
389
|
+
catch { /* best effort */ }
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
finally {
|
|
393
|
+
closeSync(fd);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/** UTC filename stamp without colons or periods; sorts by time. */
|
|
397
|
+
export function rotationBackupStamp(date = new Date()) {
|
|
398
|
+
return date.toISOString().replace(/[:.]/g, "-");
|
|
399
|
+
}
|
|
400
|
+
export function isAgentKeyRotationBackup(name, privateKeyName) {
|
|
401
|
+
const prefix = `${privateKeyName}.bak.`;
|
|
402
|
+
if (!name.startsWith(prefix))
|
|
403
|
+
return false;
|
|
404
|
+
const stamp = name.slice(prefix.length);
|
|
405
|
+
const [date, time] = stamp.split("T");
|
|
406
|
+
if (!time)
|
|
407
|
+
return false;
|
|
408
|
+
const parsed = new Date(`${date}T${time.slice(0, 8).replace(/-/g, ":")}.${time.slice(9)}`);
|
|
409
|
+
return Number.isFinite(parsed.getTime()) && rotationBackupStamp(parsed) === stamp;
|
|
410
|
+
}
|
|
411
|
+
function syncAgentKeysDir(directory) {
|
|
412
|
+
// Windows cannot open directories for fsync.
|
|
413
|
+
if (WIN32)
|
|
414
|
+
return;
|
|
415
|
+
const fd = openSync(directory, constants.O_RDONLY | constants.O_DIRECTORY | NOFOLLOW);
|
|
416
|
+
try {
|
|
417
|
+
(testHooks?.syncKeysDir ?? fsyncSync)(fd);
|
|
418
|
+
}
|
|
419
|
+
finally {
|
|
420
|
+
closeSync(fd);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Write `data` to a unique 0600 temp file beside `privateKeyPath` and leave it
|
|
425
|
+
* staged. A failure sends nothing and attempts to remove only the temp file
|
|
426
|
+
* this call created (best effort).
|
|
427
|
+
*/
|
|
428
|
+
export function stageAgentKeyFile(privateKeyPath, data) {
|
|
429
|
+
const tempPath = writeAgentKeyTemp(privateKeyPath, Buffer.from(data)).path;
|
|
430
|
+
let directory = dirname(privateKeyPath);
|
|
431
|
+
let kind = "keys";
|
|
432
|
+
try {
|
|
433
|
+
// On POSIX, sync the staged entry and its directory's parent before Flair
|
|
434
|
+
// can accept the new public key, including retries after a failed parent sync.
|
|
435
|
+
syncAgentKeysDir(directory);
|
|
436
|
+
if (!WIN32) {
|
|
437
|
+
kind = "parent";
|
|
438
|
+
directory = dirname(directory);
|
|
439
|
+
directory = realpathSync(directory);
|
|
440
|
+
syncAgentKeysDir(directory);
|
|
441
|
+
}
|
|
442
|
+
return tempPath;
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
try {
|
|
446
|
+
unlinkSync(tempPath);
|
|
447
|
+
}
|
|
448
|
+
catch { /* best effort */ }
|
|
449
|
+
throw new AgentKeyFileRefusal(`flair could not sync ${kind} directory ${directory} (${error instanceof Error ? error.message : String(error)}); check the directory and filesystem, then retry rotation`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Rename a staged temp file over `privateKeyPath`. A failed rename leaves the
|
|
454
|
+
* staged file in place so the caller can report its path.
|
|
455
|
+
*/
|
|
456
|
+
export function replaceAgentKeyFile(privateKeyPath, stagedPath) {
|
|
457
|
+
testHooks?.beforePublish?.(stagedPath, privateKeyPath);
|
|
458
|
+
renameSync(stagedPath, privateKeyPath);
|
|
459
|
+
try {
|
|
460
|
+
syncAgentKeysDir(dirname(privateKeyPath));
|
|
461
|
+
}
|
|
462
|
+
catch (error) {
|
|
463
|
+
console.error(`Warning: flair could not sync keys directory ${dirname(privateKeyPath)} (${error instanceof Error ? error.message : String(error)}); the new private key is in place at ${privateKeyPath}.`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Reuse the key at `privateKeyPath` when it exists, otherwise write the bytes
|
|
468
|
+
* `create` returns. An occupied path wins over `create` (an export that
|
|
469
|
+
* carries another key does not replace one already on disk). On POSIX a
|
|
470
|
+
* reused key's directory is synced before the key is returned, so a retry
|
|
471
|
+
* after a failed sync confirms the entry durable; a failure refuses and
|
|
472
|
+
* leaves the key in place. Windows cannot sync a directory.
|
|
473
|
+
*/
|
|
474
|
+
export function readOrCreateAgentKey(privateKeyPath, create) {
|
|
475
|
+
const existing = readExistingAgentKey(privateKeyPath);
|
|
476
|
+
if (existing) {
|
|
477
|
+
syncReusedAgentKeyDir(privateKeyPath);
|
|
478
|
+
return { bytes: existing, created: false };
|
|
479
|
+
}
|
|
480
|
+
const data = create();
|
|
481
|
+
try {
|
|
482
|
+
createAgentKeyFile(privateKeyPath, data);
|
|
483
|
+
}
|
|
484
|
+
catch (error) {
|
|
485
|
+
if (errnoCode(error) === "EEXIST") {
|
|
486
|
+
const raced = readExistingAgentKey(privateKeyPath);
|
|
487
|
+
if (raced) {
|
|
488
|
+
syncReusedAgentKeyDir(privateKeyPath);
|
|
489
|
+
return { bytes: raced, created: false };
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
throw error;
|
|
493
|
+
}
|
|
494
|
+
return { bytes: new Uint8Array(Buffer.from(data)), created: true };
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* On POSIX, sync the directory of a key this call did not write, before it is
|
|
498
|
+
* returned to a caller that registers it; a failure refuses and leaves the key
|
|
499
|
+
* in place. Windows cannot sync a directory.
|
|
500
|
+
*/
|
|
501
|
+
function syncReusedAgentKeyDir(privateKeyPath) {
|
|
502
|
+
try {
|
|
503
|
+
syncAgentKeysDir(dirname(privateKeyPath));
|
|
504
|
+
}
|
|
505
|
+
catch (error) {
|
|
506
|
+
throw new AgentKeyFileRefusal(`flair could not sync keys directory ${dirname(privateKeyPath)} (${error instanceof Error ? error.message : String(error)}); ` +
|
|
507
|
+
`existing key ${privateKeyPath} is not confirmed durable; check the directory and filesystem, then retry`);
|
|
508
|
+
}
|
|
509
|
+
}
|