Skip to content

Commit 0aac81d

Browse files
committed
fix(settings): keep legacy credentials when migration fails
An unrelated settings migration could rewrite the file after copying legacy source control secrets failed, dropping the old references. Fail and retry the settings load before any rewrite if a secret cannot be read or copied. Cover the failure alongside a provider migration. Model: GPT-6 Astra (max) Harness: Codex
1 parent 9c245b3 commit 0aac81d

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
17051705
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
17061706
);
17071707

1708-
it.effect("keeps the retired keys for another load when a token cannot be copied", () => {
1708+
it.effect("keeps retired secrets when another settings migration would rewrite the file", () => {
17091709
const cause = new ServerSecretStore.SecretStoreReadError({
17101710
resource: "bitbucket-access-token",
17111711
cause: PlatformError.systemError({
@@ -1732,11 +1732,12 @@ it.layer(NodeServices.layer)("server settings", (it) => {
17321732
const fileSystem = yield* FileSystem.FileSystem;
17331733
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
17341734
const legacy = JSON.stringify({
1735+
providers: {},
17351736
bitbucket: { email: "me@example.com", accessToken: "\u2022\u2022\u2022\u2022\u2022\u2022" },
17361737
});
17371738
yield* fileSystem.writeFileString(serverConfig.settingsPath, legacy);
17381739

1739-
yield* Effect.exit(serverSettings.getSettings);
1740+
assert.equal((yield* Effect.exit(serverSettings.getSettings))._tag, "Failure");
17401741

17411742
assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), legacy);
17421743
}).pipe(Effect.provide(layerSettings));

‎apps/server/src/serverSettings.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -931,10 +931,10 @@ const make = Effect.gen(function* () {
931931
const folded = settingsFileTrusted
932932
? foldLegacyProjectSettings(loaded, legacyProjectRows)
933933
: loaded;
934-
// Copied, not moved: the old entry is removed only after the new file is written. A token
935-
// that cannot be read or copied leaves the retired keys in place, so the migration runs again
936-
// on the next load instead of dropping it.
937-
const legacySecretsCopied = yield* Effect.forEach(
934+
// Copied, not moved: remove the old entry only after the new file is written.
935+
// Fail the load if a copy fails so neither another migration nor a later
936+
// settings update can erase the retired keys. Failed loads are retried.
937+
yield* Effect.forEach(
938938
settingsFileTrusted
939939
? migrateLegacySourceControlSettings(folded, legacySourceControl).secretMoves
940940
: [],
@@ -943,17 +943,13 @@ const make = Effect.gen(function* () {
943943
Effect.flatMap((secret) =>
944944
Option.isNone(secret) ? Effect.void : secretStore.set(move.to, secret.value),
945945
),
946-
Effect.as(true),
947-
Effect.catch(() =>
948-
Effect.logWarning("failed to copy a source control secret to its new name", {
949-
secretName: move.to,
950-
}).pipe(Effect.as(false)),
946+
Effect.mapError(
947+
(cause) => new ServerSettingsError({ settingsPath, operation: "write-secret", cause }),
951948
),
952949
),
953-
).pipe(Effect.map((copied) => copied.every(Boolean)));
950+
);
954951
const hasLegacySourceControl =
955952
settingsFileTrusted &&
956-
legacySecretsCopied &&
957953
(legacySourceControl.bitbucket !== undefined || legacySourceControl.github !== undefined);
958954
const sourceControlMigration = hasLegacySourceControl
959955
? migrateLegacySourceControlSettings(folded, legacySourceControl)

0 commit comments

Comments
 (0)