Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion INFISICAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Machine-identity connections verify read access before saving.  Declared machin

An unreadable connection is reported separately so healthy project uses remain manageable.  Replace it by entering all credentials, or disable it to suppress its environment fallback; blank fields cannot reuse credentials from unreadable saved data.  Replacements retain the original saved-row revision so a concurrent edit or disable wins.  Project token counting tolerates malformed routes when the project and token inventory remain readable, but an unknown project or encrypted inventory still blocks service-token saves until that source is replaced or disabled; machine-identity saves add no service tokens.

The value-blind secrets helper accepts only `INFISICAL_UM_WRITE_TOKEN` with verified service-token write metadata for token-based writes; generic `INFISICAL_TOKEN` remains a read alias.  Opaque bearer tokens cannot attest write permissions.  The helper also retains its explicit UM machine-identity login path, whose real operations are authorized by Infisical without claiming a service-token permission check.
The value-blind secrets helper requires `INFISICAL_UM_READ_TOKEN` for reads or `INFISICAL_UM_WRITE_TOKEN` for writes, verified against the exact project/prod/folder permission; `INFISICAL_TOKEN` remains a service-token read alias.  Opaque bearers and machine-identity login are refused.  Copy operations read only explicitly named Infisical-synced environment values; platform sync uses its fixed map and never copies retired machine credentials.  The manual Local keys bundle reads only its provider catalog's synced environment names, with an explicit fixture-file option retained for tests.  Actual token custody, finite expiry and producer delivery remain separately qualified.

Legacy raw responses can omit project/environment/folder fields.  Only a token whose own verified metadata contains one exact folder makes those omissions unambiguous; any returned contradictory identity is rejected.  A multi-folder token must receive the exact identity fields in each response, or the operation fails closed.

Expand Down
12 changes: 7 additions & 5 deletions docs/rollouts/2026-11-08-collector-keychain-tokens.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Per-Consumer Collector Tokens In The Mac Login Keychain

Each collector that reads a provider secret reads it with its own scoped Infisical service token, and that token now lives in the owner's login keychain instead of a plaintext file on disk.  The nine per-consumer binding files that say so are in this repository now, tracked, with the read implemented once and the per-consumer differences held as data.
Eight collector bindings read through their own scoped Infisical service tokens in the owner's login keychain instead of plaintext files.  The ninth, Codex, accepts only Infisical-synced values injected for its own consumer.  The nine binding files are tracked, with shared resolution and the per-consumer differences held in thin bindings.

## Why The Files Were Missing

Expand All @@ -26,26 +26,28 @@ The two Infisical responses are external, so neither is read field by field befo

The resolver honours COLLECTOR_STRICT_INJECTED_SECRETS=1 as environment-only, the same way the shared resolver does, so a host that asked for environment-only resolution never reaches the login keychain and never makes an Infisical request.  The lookup is built once per resolver and shared, so the keychain read, the metadata request and the per-folder fetches happen once per run rather than once per name the collector asks for.

`scripts/lib/automation-collector-token.mjs` holds the Codex collector's existing mechanism, unchanged.  The Codex collector reads through the System keychain automation identity, which the owner permitted, and that identity is shared rather than a per-consumer grant, so it is neither one of the scoped tokens nor something the owner's login keychain should hold.
`scripts/lib/codex-run-session-token-collector.mjs` forces the shared resolver's injected-secrets mode on every call.  It accepts `CODEX_INGEST_TOKEN` before `USAGE_INGEST_TOKEN` from its supplied environment and returns no token when both are absent.  A caller cannot enable runtime lookups by supplying a lookup function or disabling strict mode.  The former System keychain automation reader is removed; Codex performs no keychain read, universal-auth login, CLI lookup or default-folder discovery.

The nine per-consumer files under `scripts/lib/` are now thin.  Each one re-exports the shared library, states its own binding, and overrides the resolver with the scoped one.  The binding is data: the token's account name, the human label, the pinned grant id, the project and the folders.  All eight scoped consumers share one project, the root folder and the global API keys folder, and read permission only.  Because the binding is data and the read is one implementation, a change to the read can no longer drift between consumers, which is exactly what the nine forks had been doing.
The nine per-consumer files under `scripts/lib/` are thin and re-export the shared library.  Eight override the resolver with the scoped reader; their binding data is the token's account name, the human label, the pinned grant id, the project and the folders.  All eight scoped consumers share one project, the root folder and the global API keys folder, and read permission only.  Codex overrides the shared resolver only to require injected values.  The shared implementations keep reads from drifting between consumers.

## The Entry Points

Each of the nine collectors now imports its own binding instead of the shared library, and awaits the resolver.  The eight other collectors, which have no per-consumer grant, keep the shared library and are unchanged.

The Codex collector's resolver stays synchronous, because the automation identity read is synchronous and has always been.  Awaiting it changes nothing.
The Codex collector's resolver stays synchronous and preserves the shared library's public exports and option shape.  It copies the supplied environment before forcing injected mode, leaving caller options and environment unchanged.  Awaiting its result remains supported.

## The Install Order

The plaintext files are deleted only after the merged collectors are deployed and proven to read the keychain.  The order is the same one the Mac resource watcher used: install the keychain items, deploy the merged collectors, prove the read, then overwrite the plaintext files with zeros and remove them.  A collector that cannot find its keychain item says so and stops; it never falls back to a file.

## The Test

`scripts/collector-keychain.node-test.mjs` runs under Node's own test runner as part of the collector runtime gate.  Every keychain read goes through an injected stand-in for `security`, so the test never touches the real keychain.  It covers the item lookup and its arguments, the three refusals and how they are told apart, the refusal to read an invalid account name, the grant id check, the malformed-token refusal, the environment taking precedence over the keychain, the metadata binding refusal, the refusal of a malformed Infisical body, the strict mode that reaches neither the keychain nor Infisical, the shared lookup that reads the keychain once, and the rule that a failed read never carries the value.  It also pins each of the nine bindings to its own account, label, grant id, project and folders, checks that each entry point imports its own binding and awaits the resolver, checks that the other collectors still import the shared library, and checks that no collector reads a plaintext token file any more.
`scripts/collector-keychain.node-test.mjs` runs under Node's own test runner as part of the collector runtime gate.  Every keychain read goes through an injected stand-in for `security`, so the test never touches the real keychain.  It covers the item lookup and its arguments, the three refusals and how they are told apart, the refusal to read an invalid account name, the grant id check, the malformed-token refusal, the environment taking precedence over the keychain, the metadata binding refusal, the refusal of a malformed Infisical body, the strict mode that reaches neither the keychain nor Infisical, the shared lookup that reads the keychain once, and the rule that a failed read never carries the value.  It pins the eight scoped bindings to their own accounts, labels, grant ids, project and folders.  Codex fixtures exercise the actual wrapper and shared resolver in bounded isolated child processes, guard commands with inert authentication traps, and reject provider calls.  They verify injected name priority, changed values between calls, absent and partial bootstrap, immutable caller options and the inability to bypass injected mode.  The suite also checks each entry point's binding import and awaited resolver, the other collectors' shared imports, and the absence of plaintext token readers.

## What Is Not Covered Here

The Codex source change does not qualify an actual grant or installed injector.  Its own service-token custody, exact project/prod/folder/read metadata, finite expiry, producer injection and installed collector acceptance remain separate gates.  No account, grant id or folder is inferred, and the generic CLI resolver's compatibility lookups do not establish that qualification.

The ninth plaintext file on the Mac, `fleet-zulip-client-read`, is not a collector token and has no consumer that a search of the Mac's applications, launch agents, secrets directory and configuration found.  It is moved to the keychain on its own footing, not by this change.

The nine collector grants have no row in the Fleet-OPS registry.  The registry names each service token and where its value lives, and a reader holding the registry row is how a keychain item is found, so the rows belong with this change and are filed separately.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"test:apple-projects": "bash scripts/verify-apple-projects.sh",
"test:antigravity-collector": "node scripts/test-antigravity-collector.mjs",
"test:session-token-collectors": "node scripts/test-session-token-collectors.mjs",
"test:collector-runtime": "node scripts/test-local-collector-runtime.mjs && node --test scripts/__tests__/opencode-profiles.node-test.mjs scripts/sentry-partial-ack.node-test.mjs scripts/idle-collector-sentry.node-test.mjs scripts/collector-keychain.node-test.mjs && node scripts/__tests__/run-session-token-collector.fixture.mjs && node scripts/__tests__/retirement-sentry.fixture.mjs && vitest run --config scripts/vitest.collectors.config.mjs",
"test:collector-runtime": "python3 -m unittest scripts.test_ops_synced_credentials && node scripts/test-local-collector-runtime.mjs && node --test scripts/__tests__/opencode-profiles.node-test.mjs scripts/sentry-partial-ack.node-test.mjs scripts/idle-collector-sentry.node-test.mjs scripts/collector-keychain.node-test.mjs && node scripts/__tests__/run-session-token-collector.fixture.mjs && node scripts/__tests__/retirement-sentry.fixture.mjs && vitest run --config scripts/vitest.collectors.config.mjs",
"test:cf-token-map": "bash scripts/test-cf-token-map.sh",
"test:replica-status-probe": "bash scripts/test-replica-status-probe.sh",
"test:quota-wrapper": "bash scripts/test-subscription-quota-wrapper.sh",
Expand Down
128 changes: 9 additions & 119 deletions scripts/__tests__/resolve-collector-token.test.mjs
Original file line number Diff line number Diff line change
@@ -1,144 +1,34 @@
import { describe, expect, it, vi } from "vitest";

import { fleetSecretsGet, fleetSecretsGetAsync, fleetSecretsNames, resetFleetSecretsCache } from "../lib/fleet-secrets.mjs";
import { resolveCollectorToken } from "../lib/run-session-token-collector.mjs";

function fakeFleet(values) {
function fakeLookup(values) {
return vi.fn((name) => values[name] ?? null);
}

describe("resolveCollectorToken", () => {
it("prefers a scoped name from fleet-secrets over an unscoped name in the environment", () => {
describe("resolveCollectorToken injected lookup compatibility", () => {
it("prefers a scoped name from the supplied lookup over an unscoped environment value", () => {
const token = resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], {
env: { USAGE_INGEST_TOKEN: "unscoped-env" },
fleetSecretsGet: fakeFleet({ CODEX_INGEST_TOKEN: "scoped-fleet", USAGE_INGEST_TOKEN: "unscoped-fleet" }),
fleetSecretsGet: fakeLookup({ CODEX_INGEST_TOKEN: "scoped-lookup", USAGE_INGEST_TOKEN: "unscoped-lookup" }),
});
expect(token).toBe("scoped-fleet");
expect(token).toBe("scoped-lookup");
});

it("prefers the environment for the same name and never calls fleet-secrets", () => {
const lookup = fakeFleet({ CODEX_INGEST_TOKEN: "scoped-fleet" });
it("prefers the environment for the same name without invoking the supplied lookup", () => {
const lookup = fakeLookup({ CODEX_INGEST_TOKEN: "scoped-lookup" });
expect(
resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: { CODEX_INGEST_TOKEN: "scoped-env" }, fleetSecretsGet: lookup }),
).toBe("scoped-env");
expect(lookup).not.toHaveBeenCalled();
});

it("falls back in order and returns null when nothing matches", () => {
const lookup = fakeFleet({ USAGE_INGEST_TOKEN: "unscoped-fleet" });
const lookup = fakeLookup({ USAGE_INGEST_TOKEN: "unscoped-lookup" });
expect(resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], { env: {}, fleetSecretsGet: lookup })).toBe(
"unscoped-fleet",
"unscoped-lookup",
);
expect(resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: {}, fleetSecretsGet: lookup })).toBeNull();
expect(resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: {}, fleetSecretsGet: () => null })).toBeNull();
});
});

describe("fleetSecretsGet", () => {
const options = (exec) => ({ exec, binary: "/fake/fleet-secrets", useCache: false });

it("returns the trimmed stdout of the root lookup", () => {
const exec = vi.fn(() => "value-from-root\n");
expect(fleetSecretsGet("USAGE_INGEST_TOKEN", options(exec))).toBe("value-from-root");
expect(exec).toHaveBeenCalledTimes(1);
expect(exec.mock.calls[0][0]).toBe("/fake/fleet-secrets");
expect(exec.mock.calls[0][1]).toEqual(["get", "USAGE_INGEST_TOKEN", "--path", "/"]);
});

it("retries /global-api-keys when the key is absent at the root", () => {
const exec = vi.fn((_bin, args) => {
if (args.at(-1) === "/") throw Object.assign(new Error("absent"), { status: 3 });
return "value-from-folder";
});
expect(fleetSecretsGet("USAGE_INGEST_TOKEN", options(exec))).toBe("value-from-folder");
expect(exec.mock.calls.map((call) => call[1].at(-1))).toEqual(["/", "/global-api-keys"]);
});

it("returns null when the binary is missing or every lookup fails", () => {
const exec = vi.fn(() => {
throw Object.assign(new Error("spawn ENOENT"), { code: "ENOENT" });
});
expect(fleetSecretsGet("USAGE_INGEST_TOKEN", options(exec))).toBeNull();
});

it("refuses a name that is not a plain env var name", () => {
const exec = vi.fn(() => "x");
expect(fleetSecretsGet("--path", options(exec))).toBeNull();
expect(exec).not.toHaveBeenCalled();
});

it("caches per process", () => {
resetFleetSecretsCache();
const exec = vi.fn(() => "cached-value");
const opts = { exec, binary: "/fake/fleet-secrets" };
expect(fleetSecretsGet("CACHE_TEST_NAME", opts)).toBe("cached-value");
expect(fleetSecretsGet("CACHE_TEST_NAME", opts)).toBe("cached-value");
expect(exec).toHaveBeenCalledTimes(1);
resetFleetSecretsCache();
});

it("still returns null by default when every lookup fails transiently", () => {
const exec = vi.fn(() => {
throw Object.assign(new Error("fetch failed"), { status: 4 });
});
expect(fleetSecretsGet("USAGE_INGEST_TOKEN", options(exec))).toBeNull();
});

it("throws with a cause in strict mode instead of reporting a failure as absent", () => {
const failure = Object.assign(new Error("fetch failed"), { status: 4 });
const exec = vi.fn(() => {
throw failure;
});
let error = null;
try {
fleetSecretsGet("USAGE_INGEST_TOKEN", { ...options(exec), strict: true });
} catch (caught) {
error = caught;
}
expect(error).not.toBeNull();
expect(error.message).toContain("USAGE_INGEST_TOKEN");
expect(error.message).not.toContain("secret-value");
expect(error.secretName).toBe("USAGE_INGEST_TOKEN");
expect(error.cause).toBe(failure);
});

it("strict mode still returns null for an absent key", () => {
const exec = vi.fn(() => {
throw Object.assign(new Error("absent"), { status: 3 });
});
expect(fleetSecretsGet("USAGE_INGEST_TOKEN", { ...options(exec), strict: true })).toBeNull();
});

it("the async twin throws with a cause in strict mode on a transient failure", async () => {
const failure = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" });
const exec = vi.fn(async () => {
throw failure;
});
let error = null;
try {
await fleetSecretsGetAsync("USAGE_INGEST_TOKEN", { exec, binary: "/fake/fleet-secrets", strict: true });
} catch (caught) {
error = caught;
}
expect(error).not.toBeNull();
expect(error.message).toContain("USAGE_INGEST_TOKEN");
expect(error.cause).toBe(failure);
});
});

describe("fleetSecretsNames", () => {
it("merges names from root and the folder and ignores junk lines", () => {
const exec = vi.fn((_bin, args) =>
args.at(-1) === "/" ? "ALPHA\nBETA\n\n" : "BETA\nGAMMA\nnot a name\n",
);
const names = fleetSecretsNames({ exec, binary: "/fake/fleet-secrets" });
expect([...names].sort()).toEqual(["ALPHA", "BETA", "GAMMA"]);
});

it("returns an empty set when the binary fails", () => {
const exec = vi.fn(() => {
throw new Error("boom");
});
expect(fleetSecretsNames({ exec, binary: "/fake/fleet-secrets" }).size).toBe(0);
});
});
2 changes: 1 addition & 1 deletion scripts/antigravity-usage-collector.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ async function main() {
]);
if (!ingestToken && !DRY_RUN) {
fail(
"Missing ANTIGRAVITY_INGEST_TOKEN or USAGE_INGEST_TOKEN. Ensure the token is set in the environment or available through `fleet-secrets get`."
"Missing ANTIGRAVITY_INGEST_TOKEN or USAGE_INGEST_TOKEN.\u00A0 Check this consumer's Infisical-synced value or qualified scoped binding."
);
}

Expand Down
Loading
Loading