Read every secret input from a credential file named by NAME_FILE

The recipe declared Heimdall's secrets as plain environment, so its admit
contract made the operator binding the only place their values could live,
and the binding was printed into a transcript. The recipe now declares each
secret only as its _FILE twin (GC_ACCESS_DATABASE_URL_FILE required, the rest
optional). A binding binds those under [workload.secret_files] to root:root
0400 credential files, and Idunn's rule that a binding binds only declared
names leaves a plaintext secret nowhere to go. GC_ACCESS_APP_SHARED_SECRET is
deleted outright: nothing reads it.

One reader, readSecretInput, serves every secret: it reads NAME_FILE and strips
one trailing newline, refuses NAME and NAME_FILE set together, and its errors
name the variable and path, never a value or file contents. Storage-backend
selection keys off the resolved database URL. The published provider witness
and the provider_not_configured body name the _FILE variables, and custody's
errors name GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE.

The plaintext fallback (NAME read while NAME_FILE is absent) is a named shim
for the legacy EnvironmentFile unit only; secret-files Cut 5 deletes it.

Idunn secret-files cut, Cut 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 12:30:57 +02:00
commit ac86352972
5 changed files with 245 additions and 28 deletions

View file

@ -1,3 +1,4 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { appSlugs, providers, type AppSlug, type Provider } from "./contracts.js";
@ -120,11 +121,47 @@ function readIdunnCandidateBind(
return { host, port };
}
/**
* Read one secret input. Its value lives in a file named by `NAME_FILE`: under
* Idunn that is a systemd credential, so the value never enters a binding, a
* unit, a plan or the process environment. One trailing newline is stripped.
*
* LEGACY SHIM, deleted with the legacy EnvironmentFile unit (secret-files
* Cut 5): when `NAME_FILE` is absent, the plaintext `NAME` is read instead.
* Setting both is refused, so one input has one owner. Under Idunn the recipe
* declares only the `_FILE` names, so the plaintext form cannot be bound there.
*
* Errors name the variable and the path, never a value or a file's contents.
*/
export function readSecretInput(env: NodeJS.ProcessEnv, name: string): string | undefined {
const fileVariable = `${name}_FILE`;
const filePath = env[fileVariable];
const plaintext = env[name];
if (filePath !== undefined && plaintext !== undefined) {
throw new Error(`${name} and ${fileVariable} are both set; set only ${fileVariable}.`);
}
if (filePath === undefined) {
return plaintext;
}
let contents: string;
try {
contents = readFileSync(filePath, "utf8");
} catch (error) {
const code = (error as NodeJS.ErrnoException).code ?? "read error";
throw new Error(`${fileVariable} names ${filePath}, which could not be read (${code}).`);
}
return contents.replace(/\r?\n$/, "");
}
function readProviderConfig(env: NodeJS.ProcessEnv, provider: Provider): ProviderClientConfig {
const prefix = `GC_ACCESS_PROVIDER_${provider.toUpperCase()}`;
const config: ProviderClientConfig = {};
const clientId = env[`${prefix}_CLIENT_ID`];
const clientSecret = env[`${prefix}_CLIENT_SECRET`];
const clientSecret = readSecretInput(env, `${prefix}_CLIENT_SECRET`);
if (clientId) {
config.clientId = clientId;
@ -200,8 +237,9 @@ export function loadConfig(
const issuer = trimTrailingSlash(env.GC_ACCESS_ISSUER ?? publicBaseUrl);
const dataRoot =
stateRootArgument ?? env.GC_ACCESS_DATA_ROOT ?? path.join(workspaceRoot, ".heimdall-data");
const databaseUrl = readSecretInput(env, "GC_ACCESS_DATABASE_URL");
const storageBackend =
env.GC_ACCESS_STORAGE_BACKEND === "postgres" || env.GC_ACCESS_DATABASE_URL ? "postgres" : "memory";
env.GC_ACCESS_STORAGE_BACKEND === "postgres" || databaseUrl ? "postgres" : "memory";
const providersConfig = Object.fromEntries(
providers.map((provider) => [provider, readProviderConfig(env, provider)])
) as Record<Provider, ProviderClientConfig>;
@ -209,7 +247,7 @@ export function loadConfig(
appSlugs
.map((appSlug) => {
const envKey = `GC_ACCESS_APP_${appSlug.toUpperCase()}_SHARED_SECRET`;
return [appSlug, env[envKey]];
return [appSlug, readSecretInput(env, envKey)];
})
.filter(([, value]) => Boolean(value))
) as Partial<Record<AppSlug, string>>;
@ -285,20 +323,22 @@ export function loadConfig(
config.signingKeyId = env.GC_ACCESS_SIGNING_KEY_ID;
}
if (env.GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64) {
config.tokenEncryptionKeyBase64 = env.GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64;
const tokenEncryptionKeyBase64 = readSecretInput(env, "GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64");
if (tokenEncryptionKeyBase64) {
config.tokenEncryptionKeyBase64 = tokenEncryptionKeyBase64;
}
if (env.GC_ACCESS_BIFROST_PATRON_SUPPORT_ENDPOINT) {
config.bifrostPatronSupportEndpoint = env.GC_ACCESS_BIFROST_PATRON_SUPPORT_ENDPOINT;
}
if (env.GC_ACCESS_BIFROST_PATRON_SUPPORT_SECRET) {
config.bifrostPatronSupportSecret = env.GC_ACCESS_BIFROST_PATRON_SUPPORT_SECRET;
const bifrostPatronSupportSecret = readSecretInput(env, "GC_ACCESS_BIFROST_PATRON_SUPPORT_SECRET");
if (bifrostPatronSupportSecret) {
config.bifrostPatronSupportSecret = bifrostPatronSupportSecret;
}
if (env.GC_ACCESS_DATABASE_URL) {
config.storage.databaseUrl = env.GC_ACCESS_DATABASE_URL;
if (databaseUrl) {
config.storage.databaseUrl = databaseUrl;
}
return config;

View file

@ -14,13 +14,13 @@ export interface TokenCustody {
function decodeConfiguredKey(base64Value: string): Buffer {
const normalized = base64Value.trim();
if (!normalized) {
throw new Error("GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64 is set but empty.");
throw new Error("GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE is set but empty.");
}
const key = Buffer.from(normalized, "base64");
if (key.byteLength !== TOKEN_KEY_BYTES) {
throw new Error(
`GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64 must decode to exactly ${TOKEN_KEY_BYTES} bytes for AES-256-GCM.`
`GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE must decode to exactly ${TOKEN_KEY_BYTES} bytes for AES-256-GCM.`
);
}
@ -51,7 +51,7 @@ export function createTokenCustody(config: HeimdallConfig): TokenCustody {
: (() => {
if (config.storage.backend === "postgres") {
throw new Error(
"GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64 is required when Heimdall uses persistent postgres storage."
"GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE is required when Heimdall uses persistent postgres storage."
);
}

View file

@ -21,7 +21,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
defaultScopes: ["identify", "guilds.members.read"],
roles: ["identity", "entitlement"],
clientIdEnv: "GC_ACCESS_PROVIDER_DISCORD_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_DISCORD_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_DISCORD_CLIENT_SECRET_FILE",
},
patreon: {
key: "patreon",
@ -30,7 +30,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
defaultScopes: ["identity", "identity[email]"],
roles: ["identity", "entitlement"],
clientIdEnv: "GC_ACCESS_PROVIDER_PATREON_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_PATREON_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_PATREON_CLIENT_SECRET_FILE",
},
github: {
key: "github",
@ -39,7 +39,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
defaultScopes: ["read:user", "user:email"],
roles: ["identity", "managed_credential"],
clientIdEnv: "GC_ACCESS_PROVIDER_GITHUB_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_GITHUB_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_GITHUB_CLIENT_SECRET_FILE",
},
twitch: {
key: "twitch",
@ -48,7 +48,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
defaultScopes: ["user:read:email"],
roles: ["identity", "managed_credential"],
clientIdEnv: "GC_ACCESS_PROVIDER_TWITCH_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_TWITCH_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_TWITCH_CLIENT_SECRET_FILE",
},
youtube: {
key: "youtube",
@ -62,7 +62,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
],
roles: ["identity", "managed_credential"],
clientIdEnv: "GC_ACCESS_PROVIDER_YOUTUBE_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_YOUTUBE_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_YOUTUBE_CLIENT_SECRET_FILE",
additionalAuthorizationParams: {
access_type: "offline",
include_granted_scopes: "true",
@ -80,7 +80,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
],
roles: ["identity", "managed_credential"],
clientIdEnv: "GC_ACCESS_PROVIDER_SPOTIFY_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_SPOTIFY_CLIENT_SECRET",
clientSecretEnv: "GC_ACCESS_PROVIDER_SPOTIFY_CLIENT_SECRET_FILE",
},
};