feat(settings): point provider environment variables at a secret provider #27115
Mergedyordis(GitHub profile, opens in a new tab)merged 3 commits into
main fromrefs/pull/27115/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/secrets/SecretResolver.ts lines 8 to 8Resolvedby yordis(GitHub profile, opens in a new tab)@@ -0,0 +1,8 @@1+// Reads secrets that settings point at through a configured secret provider. Each read, including2+// a failed one, is held for the server's life so rebuilds never re-prompt an unlock; `invalidate`3+// (the Settings refresh) is the only way to read again.4+import {5+ OnePasswordSecretProviderConfig,6+ type OnePasswordAccount,7+ type SecretReference,8+ type ServerSettings,apps/server/src/ws.ts lines 2312 to 2313OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -2305,3 +2309,5 @@23052309 if (input.instanceId === undefined) {23062310 yield* usageLimitSources.refresh;23072311 }2312+ if (input.cwd === undefined) {2313+ yield* providerInstancesMutator.reloadSecrets(input.instanceId);apps/server/src/secrets/SecretResolver.ts lines 66 to 69OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -0,0 +49,21 @@49+const runOnePassword = Effect.fn("SecretResolver.runOnePassword")(function* (50+ args: ReadonlyArray<string>,51+) {52+ const spawnCommand = yield* resolveSpawnCommand(ONE_PASSWORD_BINARY, ["--cache=false", ...args]);53+ return yield* spawnAndCollect(54+ ONE_PASSWORD_BINARY,55+ ChildProcess.make(spawnCommand.command, spawnCommand.args, { shell: spawnCommand.shell }),56+ );57+});58+59+const nonEmpty = (value: string) => (value.trim().length > 0 ? value.trim() : undefined);60+61+const readOne = Effect.fn("SecretResolver.readOne")(function* (62+ account: OnePasswordAccount,63+ reference: string,64+) {65+ const result = yield* runOnePassword(["read", `--account=${account}`, "--no-newline", reference]);66+ if (result.code !== 0) {67+ // `op` never echoes a secret on stderr, so its reason is safe to log.68+ yield* Effect.logWarning("Could not read secret from 1Password", {69+ account,
- apps/server/src/secrets/SecretResolver.ts lines 59 to 59OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -0,0 +39,21 @@39+ invalidate: () => Effect.void,40+ }),41+ },42+) {}43+44+const ONE_PASSWORD_BINARY = "op";45+// Bounds a read long enough to reach for a fingerprint reader, short enough not to wedge a rebuild.46+const READ_TIMEOUT = Duration.seconds(45);47+48+// `--cache=false` skips the `op daemon`, which hangs on prompts a background server cannot show.49+const runOnePassword = Effect.fn("SecretResolver.runOnePassword")(function* (50+ args: ReadonlyArray<string>,51+) {52+ const spawnCommand = yield* resolveSpawnCommand(ONE_PASSWORD_BINARY, ["--cache=false", ...args]);53+ return yield* spawnAndCollect(54+ ONE_PASSWORD_BINARY,55+ ChildProcess.make(spawnCommand.command, spawnCommand.args, { shell: spawnCommand.shell }),56+ );57+});58+59+const nonEmpty = (value: string) => (value.trim().length > 0 ? value.trim() : undefined);
- apps/server/src/secrets/SecretResolver.ts lines 68 to 72OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +52,21 @@52+ const spawnCommand = yield* resolveSpawnCommand(ONE_PASSWORD_BINARY, ["--cache=false", ...args]);53+ return yield* spawnAndCollect(54+ ONE_PASSWORD_BINARY,55+ ChildProcess.make(spawnCommand.command, spawnCommand.args, { shell: spawnCommand.shell }),56+ );57+});58+59+const nonEmpty = (value: string) => (value.trim().length > 0 ? value.trim() : undefined);60+61+const readOne = Effect.fn("SecretResolver.readOne")(function* (62+ account: OnePasswordAccount,63+ reference: string,64+) {65+ const result = yield* runOnePassword(["read", `--account=${account}`, "--no-newline", reference]);66+ if (result.code !== 0) {67+ // `op` never echoes a secret on stderr, so its reason is safe to log.68+ yield* Effect.logWarning("Could not read secret from 1Password", {69+ account,70+ reference,71+ detail: result.stderr.trim(),72+ });apps/server/src/secrets/SecretResolver.ts lines 78 to 115OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +95,21 @@95+ yield* Effect.logWarning("Could not batch-read secrets from 1Password", {96+ account,97+ references: references.length,98+ detail: result.stderr.trim(),99+ });100+ return undefined;101+ }102+ return values.map(nonEmpty);103+}, Effect.scoped);104+105+const bounded = <A, E, R>(effect: Effect.Effect<A | undefined, E, R>) =>106+ effect.pipe(107+ Effect.timeoutOption(READ_TIMEOUT),108+ Effect.map(Option.getOrUndefined),109+ Effect.catch((error) =>110+ Effect.logWarning("Could not run 1Password", { detail: String(error) }).pipe(111+ Effect.as(undefined),112+ ),113+ ),114+ );115+apps/server/src/secrets/SecretResolver.ts lines 152 to 155OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +135,21 @@135+ // Only 1Password reads prompt, so only they are cached; a reference this build cannot read136+ // fails fast and is checked again on the next read.137+ const accountFor = (138+ providers: ServerSettings["secretProviders"],139+ reference: SecretReference,140+ ): OnePasswordAccount | undefined => {141+ const provider = providers[reference.provider];142+ if (provider?.kind !== "one_password" || !reference.reference.startsWith("op://")) {143+ return undefined;144+ }145+ return Option.getOrUndefined(decodeOnePasswordConfig(provider.config))?.account;146+ };147+148+ const readUncached = Effect.fn("SecretResolver.readUncached")(function* (149+ account: OnePasswordAccount,150+ references: ReadonlyArray<string>,151+ ) {152+ const together =153+ references.length > 1 ? yield* bounded(readTogether(account, references)) : undefined;154+ if (together !== undefined) return together;155+ return yield* Effect.forEach(references, (reference) => bounded(readOne(account, reference)));apps/web/src/components/settings/SecretSourceInput.tsx line 75OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +55,21 @@55+ const [pendingProvider, setPendingProvider] = useState<SecretProviderId | null>(null);56+ const provider = secret?.provider ?? pendingProvider;57+ if (sources.length === 0 && provider === null) return <SecretInput {...input} />;58+59+ const label = input["aria-label"] ?? "Secret";60+ const providerLabel = (id: SecretProviderId) =>61+ sources.find((source) => source.id === id)?.label ?? id;62+ return (63+ <div className="flex min-w-0 flex-1 items-center gap-1.5">64+ <div className="w-28 shrink-0">65+ <Select66+ value={provider ?? STORED_VALUE}67+ onValueChange={(value) => {68+ if (value === STORED_VALUE || value === null) {69+ setPendingProvider(null);70+ if (secret !== undefined) onSecretChange(undefined);71+ return;72+ }73+ const next = value as SecretProviderId;74+ if (secret === undefined) setPendingProvider(next);75+ else onSecretChange({ ...secret, provider: next });apps/web/src/components/settings/SecretSourceInput.tsx lines 104 to 105OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +85,21 @@85+ {source.label}86+ </SelectItem>87+ ))}88+ {provider !== null && !sources.some((source) => source.id === provider) ? (89+ <SelectItem value={provider}>{provider}</SelectItem>90+ ) : null}91+ </SelectPopup>92+ </Select>93+ </div>94+ {provider === null ? (95+ <SecretInput {...input} />96+ ) : (97+ <DraftInput98+ {...(input.id !== undefined ? { id: input.id } : {})}99+ size="sm"100+ font="mono"101+ className="min-w-0 flex-1"102+ value={secret?.reference ?? ""}103+ onCommit={(reference) => {104+ const decoded = decodeSecretReference({ provider, reference });105+ if (Option.isNone(decoded)) return;
- apps/web/src/components/settings/SecretSourceInput.tsx lines 109 to 112OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +92,21 @@92+ ))}93+ {provider !== null && !sources.some((source) => source.id === provider) ? (94+ <SelectItem value={provider}>{provider}</SelectItem>95+ ) : null}96+ </SelectPopup>97+ </Select>98+ </div>99+ {provider === null ? (100+ <SecretInput {...input} />101+ ) : (102+ <div className="flex min-w-0 flex-1 flex-col gap-1">103+ <DraftInput104+ {...(input.id !== undefined ? { id: input.id } : {})}105+ size="sm"106+ font="mono"107+ value={rejectedReference ?? reference}108+ onCommit={(next) => {109+ if (next.trim().length === 0) {110+ setRejectedReference(null);111+ return;112+ }
- apps/server/src/provider/ProviderInstanceRegistry.ts line 75OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -50,3 +57,19 @@5057 const entryEqual = (a: ProviderInstanceConfig, b: ProviderInstanceConfig): boolean =>5158 Equal.equals(a, b);525960+const secretReferencesIn = (environment: ProviderInstanceEnvironment): Array<SecretReference> =>61+ environment.flatMap((variable) => (typeof variable.value === "string" ? [] : [variable.value]));62+63+const secretReferencesOf = (entry: ProviderInstanceConfig): ReadonlyArray<SecretReference> =>64+ secretReferencesIn(entry.environment ?? []);65+66+const withoutSecretReferences = (67+ environment: ProviderInstanceEnvironment,68+): ResolvedProviderInstanceEnvironment =>69+ environment.flatMap((variable) =>70+ typeof variable.value === "string" ? [{ ...variable, value: variable.value }] : [],71+ );72+73+// Matches the flags `resolveEntryEnabled` reads before decoding, so a disabled instance never prompts.74+const mayBeEnabled = (entry: ProviderInstanceConfig) =>75+ entry.enabled !== false && providerInstanceConfigEnabledFlag(entry.config) !== false;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this97e35bc
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed