Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(federation): pin the signing-key version and refuse an unexpected identity

ruv8666c5d74a64
diff --git a/plugins/ruflo-chatgpt-federation/README.md b/plugins/ruflo-chatgpt-federation/README.md
index dcf8367749..22ba4656c8 100644
--- a/plugins/ruflo-chatgpt-federation/README.md
+++ b/plugins/ruflo-chatgpt-federation/README.md
@@ -75,11 +75,18 @@ Anything shaped like key material is scrubbed from errors and logs by `redact()`
gcloud run deploy chatgpt-federation \
--project=ruv-dev --region=us-central1 --source=. \
--service-account=chatgpt-federation-runtime@ruv-dev.iam.gserviceaccount.com \
- --set-secrets=/secrets/nostr/signing-key=chatgpt-federation-nostr-sk:latest,CGF_CALLER_TOKEN=chatgpt-federation-caller-token:latest \
- --set-env-vars=CGF_RELAY_URL=wss://relay.ruv.io \
+ --set-secrets=/secrets/nostr/signing-key=chatgpt-federation-nostr-sk:1,CGF_CALLER_TOKEN=chatgpt-federation-caller-token:1 \
+ --set-env-vars=CGF_RELAY_URL=wss://relay.ruv.io,CGF_EXPECTED_PUBKEY=a29fbf2f7299d13e1f1049f829e0d7036949133de4226d728b2f181458d56890 \
--allow-unauthenticated
```
+**Pin the secret version. Never mount `:latest`.** A `versions add` under a `:latest`
+mount silently becomes an identity change on the next cold start — the connector comes
+back as a pubkey the relay has not admitted, every publish fails, and readers tracking
+the old identity just see it go quiet. `CGF_EXPECTED_PUBKEY` is the backstop: the
+service refuses to start if the mounted key derives anything else, so a deploy that
+forgets to pin fails loudly instead of rolling the identity over.
+
`--allow-unauthenticated` is correct here: the service is reached by a ChatGPT
connector that cannot mint a Google ID token. Authority to publish comes from the
caller token, not from Cloud Run IAM.
@@ -91,29 +98,50 @@ reaches the container, so a token sent that way never arrives.
## Rotate
Secret Manager versions are immutable, so rotation is add-then-disable and every step
-is auditable.
+is auditable. Rotation mints a **new federation identity**, so the order matters: the
+relay must admit the new pubkey *before* any traffic reaches it, and the old version
+stays enabled until the new one is proven.
```bash
-# 1. new key → new version. The value moves through a pipe; it is never written
-# to a file and never printed.
-node -e "const{generateSecretKey}=require('nostr-tools/pure');process.stdout.write(Buffer.from(generateSecretKey()).toString('hex'))" \
- | gcloud secrets versions add chatgpt-federation-nostr-sk --project=ruv-dev --data-file=-
-
-# 2. restart onto it
-gcloud run services update chatgpt-federation --project=ruv-dev --region=us-central1 \
- --set-secrets=/secrets/nostr/signing-key=chatgpt-federation-nostr-sk:latest,CGF_CALLER_TOKEN=chatgpt-federation-caller-token:latest
-
-# 3. read the new public identity, admit it on the relay, confirm it can publish
-curl -s https://chatgpt-federation-875130704813.us-central1.run.app/ | jq -r .pubkey
-
-# 4. only after federation continuity is confirmed
-gcloud secrets versions disable <old> --secret=chatgpt-federation-nostr-sk --project=ruv-dev
+P=ruv-dev
+SVC=chatgpt-federation
+NEW_PK=<derived in step 1>
+
+# 1. Generate the replacement locally and derive its pubkey. The secret is written to
+# a 0600 file; only the public half is ever printed.
+umask 077; WORK=$(mktemp -d)
+node -e "const{generateSecretKey,getPublicKey}=require('nostr-tools/pure');
+const fs=require('fs');const sk=generateSecretKey();
+fs.writeFileSync(process.argv[1],Buffer.from(sk).toString('hex'),{mode:0o600});
+console.error(getPublicKey(sk));" "$WORK/sk.hex"
+
+# 2. Add the version. The MOUNTED version does not change — the running revision is
+# pinned, so nothing rolls over here.
+gcloud secrets versions add chatgpt-federation-nostr-sk --project=$P --data-file="$WORK/sk.hex"
+find "$WORK" -type f -exec shred -u {} \; && rmdir "$WORK"
+
+# 3. Admit the new pubkey on the relay, BEFORE it can publish.
+# (federation_admit on https://x.ruv.io/mcp, admin-gated)
+
+# 4. Deploy a revision pinned to the new version, with no traffic yet.
+gcloud run deploy $SVC --project=$P --region=us-central1 --source=. --no-traffic \
+ --set-secrets=/secrets/nostr/signing-key=chatgpt-federation-nostr-sk:<N>,CGF_CALLER_TOKEN=chatgpt-federation-caller-token:1 \
+ --set-env-vars=CGF_RELAY_URL=wss://relay.ruv.io,CGF_EXPECTED_PUBKEY=$NEW_PK
+
+# 5. Publish a probe against that revision's own URL and verify it independently —
+# a different client, a different key, reading the relay directly. Confirm the
+# event id binds to its content and the pubkey is $NEW_PK.
+
+# 6. Only then route traffic.
+gcloud run services update-traffic $SVC --project=$P --region=us-central1 --to-latest
+
+# 7. Disable the old version, and revoke the old relay identity when appropriate.
+gcloud secrets versions disable <old> --secret=chatgpt-federation-nostr-sk --project=$P
```
-Step 3 is not optional. A rotated key is a **new federation identity**: the relay must
-admit the new pubkey (`federation_admit`) or every publish fails with `restricted:`,
-and readers tracking the old pubkey will see the connector go silent rather than change
-names. Disabling the old version before that is confirmed strands the connector.
+Steps 3 and 5 are not optional. Admitting after traffic moves means every publish
+fails with `restricted:` in the gap; disabling the old version before step 5 passes
+strands the connector with no way back.
## Test
diff --git a/plugins/ruflo-chatgpt-federation/src/signing-key.mjs b/plugins/ruflo-chatgpt-federation/src/signing-key.mjs
index 7b6f22bc32..9472028ce6 100644
--- a/plugins/ruflo-chatgpt-federation/src/signing-key.mjs
+++ b/plugins/ruflo-chatgpt-federation/src/signing-key.mjs
@@ -49,6 +49,16 @@ export function loadSigner(keyPath = process.env.CGF_SIGNING_KEY_PATH || DEFAULT
}
const sk = Uint8Array.from(Buffer.from(raw, 'hex'));
const pubkey = getPublicKey(sk);
+ // Identity pinning. A rotated key is a NEW federation identity: readers tracking
+ // the old pubkey see this connector go silent, and the relay refuses its events
+ // until the new pubkey is admitted. Pinning the secret *version* on the mount is
+ // the primary guard; this is the backstop for the deploy that forgets to, where
+ // `:latest` would otherwise roll the identity over on the next cold start with no
+ // signal at all. Fail the deploy instead.
+ const expected = (process.env.CGF_EXPECTED_PUBKEY || '').trim().toLowerCase();
+ if (expected && expected !== pubkey) {
+ throw new Error(`mounted key derives ${pubkey}, expected ${expected} — refusing to start under an unexpected federation identity`);
+ }
// `sk` is reachable only from the closure below. Nothing returns it.
return {
pubkey,
diff --git a/plugins/ruflo-chatgpt-federation/test/publisher.test.mjs b/plugins/ruflo-chatgpt-federation/test/publisher.test.mjs
index 4d0c4ad624..8a9a2d3db1 100644
--- a/plugins/ruflo-chatgpt-federation/test/publisher.test.mjs
+++ b/plugins/ruflo-chatgpt-federation/test/publisher.test.mjs
@@ -40,6 +40,24 @@ test('the signer exposes a public key and a sign function, and nothing else', ()
assert.ok(!serialized.includes(skHex), 'secret must not survive serialization');
});
+test('an unexpected identity fails startup rather than rolling over silently', () => {
+ const prev = process.env.CGF_EXPECTED_PUBKEY;
+ const other = getPublicKey(generateSecretKey());
+ try {
+ process.env.CGF_EXPECTED_PUBKEY = other;
+ assert.throws(() => loadSigner(keyPath), /refusing to start under an unexpected federation identity/);
+ // and the matching case still boots
+ process.env.CGF_EXPECTED_PUBKEY = getPublicKey(Uint8Array.from(Buffer.from(skHex, 'hex')));
+ assert.equal(loadSigner(keyPath).pubkey, process.env.CGF_EXPECTED_PUBKEY);
+ } finally { if (prev === undefined) delete process.env.CGF_EXPECTED_PUBKEY; else process.env.CGF_EXPECTED_PUBKEY = prev; }
+});
+
+test('the pin is opt-in: unset means no assertion', () => {
+ const prev = process.env.CGF_EXPECTED_PUBKEY;
+ try { delete process.env.CGF_EXPECTED_PUBKEY; assert.ok(loadSigner(keyPath).pubkey); }
+ finally { if (prev !== undefined) process.env.CGF_EXPECTED_PUBKEY = prev; }
+});
+
test('redact scrubs anything shaped like key material', () => {
assert.equal(redact(`leaked ${skHex} here`), 'leaked [redacted] here');
assert.equal(redact(undefined), '');
@@ -54,7 +72,9 @@ test('no environment variable can supply the key value', async () => {
// exposure a secret volume exists to remove, so the loader must not read one.
const src = await import('node:fs').then((fs) => fs.readFileSync(new URL('../src/signing-key.mjs', import.meta.url), 'utf8'));
const envReads = [...src.matchAll(/process\.env\.([A-Z0-9_]+)/g)].map((m) => m[1]);
- assert.deepEqual(envReads, ['CGF_SIGNING_KEY_PATH']);
+ assert.deepEqual(envReads.sort(), ['CGF_EXPECTED_PUBKEY', 'CGF_SIGNING_KEY_PATH']);
+ // neither carries key material: one is a path, the other a PUBLIC key
+ assert.ok(!/process\.env\.[A-Z0-9_]*(SECRET|SK|PRIVATE|KEY_HEX)/.test(src));
});
// ---- tag compatibility with the gateway (ADR-386) ----