Skip to content

Commit 4f63275

Browse files
feat(source-control): host settings live on each host's definition, with a GitCafe token (#17901)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2a4ee33 commit 4f63275

34 files changed

Lines changed: 979 additions & 634 deletions

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

Lines changed: 96 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as Stream from "effect/Stream";
2626
import * as TestClock from "effect/testing/TestClock";
2727
import * as SqlClient from "effect/sql/SqlClient";
2828
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
29+
import { sourceControlHostSecretName } from "./sourceControl/sourceControlHostSecrets.ts";
2930
import * as ServerConfig from "./config.ts";
3031
import * as SqlitePersistence from "./persistence/Sqlite.ts";
3132
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
@@ -1418,7 +1419,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
14181419
);
14191420

14201421
it.effect(
1421-
"keeps Bitbucket tokens in the secret store and tells clients only that one is set",
1422+
"keeps source control host secrets in the secret store and tells clients only that one is set",
14221423
() =>
14231424
Effect.gen(function* () {
14241425
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
@@ -1427,164 +1428,153 @@ it.layer(NodeServices.layer)("server settings", (it) => {
14271428
const fileSystem = yield* FileSystem.FileSystem;
14281429

14291430
const saved = yield* serverSettings.updateSettings({
1430-
bitbucket: { email: "[email protected]", accessToken: "bb-access", apiToken: "bb-api" },
1431+
sourceControlHosts: {
1432+
bitbucket: { email: "[email protected]", accessToken: "bb-access" },
1433+
github: { tokens: { "github.com": "ghp_dotcom", "ghe.acme.test": "ghp_ghe" } },
1434+
},
14311435
});
1432-
assert.deepEqual(saved.bitbucket, {
1436+
assert.deepEqual(saved.sourceControlHosts.bitbucket, {
14331437
14341438
accessToken: "bb-access",
1435-
apiToken: "bb-api",
14361439
});
1437-
14381440
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1439-
assert.notInclude(raw, "bb-access");
1440-
assert.notInclude(raw, "bb-api");
1441+
for (const secret of ["bb-access", "ghp_dotcom", "ghp_ghe"]) assert.notInclude(raw, secret);
14411442
assert.include(raw, "[email protected]");
14421443

1443-
const forClient = ServerSettingsModule.redactServerSettingsForClient(saved).bitbucket;
1444-
assert.equal(forClient.email, "[email protected]");
1445-
assert.notInclude(forClient.accessToken, "bb-access");
1446-
assert.notInclude(forClient.apiToken, "bb-api");
1447-
assert.isAbove(forClient.accessToken.length, 0);
1448-
assert.isAbove(forClient.apiToken.length, 0);
1449-
1450-
// A client echoing the redacted values back, or omitting them, keeps the saved tokens.
1451-
yield* serverSettings.updateSettings({ bitbucket: forClient });
1452-
yield* serverSettings.updateSettings({ bitbucket: { email: "[email protected]" } });
1453-
assert.deepEqual((yield* serverSettings.getSettings).bitbucket, {
1454-
1455-
accessToken: "bb-access",
1456-
apiToken: "bb-api",
1457-
});
1458-
1459-
const cleared = yield* serverSettings.updateSettings({ bitbucket: { accessToken: "" } });
1460-
assert.equal(cleared.bitbucket.accessToken, "");
1461-
assert.equal(cleared.bitbucket.apiToken, "bb-api");
1462-
assert.isTrue(Option.isNone(yield* secrets.get("bitbucket-access-token")));
1463-
assert.equal(
1464-
ServerSettingsModule.redactServerSettingsForClient(cleared).bitbucket.accessToken,
1465-
"",
1466-
);
1467-
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
1468-
);
1469-
1470-
it.effect(
1471-
"keeps GitHub tokens per host in the secret store and tells clients only that one is set",
1472-
() =>
1473-
Effect.gen(function* () {
1474-
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1475-
const secrets = yield* ServerSecretStore.ServerSecretStore;
1476-
const serverConfig = yield* ServerConfig.ServerConfig;
1477-
const fileSystem = yield* FileSystem.FileSystem;
1444+
const forClient =
1445+
ServerSettingsModule.redactServerSettingsForClient(saved).sourceControlHosts;
1446+
assert.equal(forClient.bitbucket?.email, "[email protected]");
1447+
assert.notInclude(String(forClient.bitbucket?.accessToken), "bb-access");
1448+
assert.isAbove(String(forClient.bitbucket?.accessToken).length, 0);
14781449

1479-
const saved = yield* serverSettings.updateSettings({
1480-
github: { tokens: { "GitHub.com": "ghp_dotcom", "ghe.acme.test": "ghp_ghe" } },
1481-
});
1482-
assert.deepEqual(saved.github.tokens, {
1483-
"github.com": "ghp_dotcom",
1484-
"ghe.acme.test": "ghp_ghe",
1485-
});
1486-
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1487-
assert.notInclude(raw, "ghp_dotcom");
1488-
assert.notInclude(raw, "ghp_ghe");
1489-
1490-
const forClient = ServerSettingsModule.redactServerSettingsForClient(saved).github;
1491-
assert.notInclude(forClient.tokens["github.com"]!, "ghp_dotcom");
1492-
assert.isAbove(forClient.tokens["github.com"]!.length, 0);
1493-
1494-
// Echoing the redacted values back keeps them; host and account changes leave tokens alone.
1495-
yield* serverSettings.updateSettings({ github: { tokens: forClient.tokens } });
1450+
// Echoing the redacted values back keeps them; other fields change on their own.
1451+
yield* serverSettings.updateSettings({ sourceControlHosts: forClient });
14961452
yield* serverSettings.updateSettings({
1497-
github: { hosts: { "github.com": { enabled: true, account: "work" } } },
1453+
sourceControlHosts: { bitbucket: { email: "other@example.com" } },
14981454
});
1499-
assert.deepEqual((yield* serverSettings.getSettings).github.tokens, {
1455+
const kept = (yield* serverSettings.getSettings).sourceControlHosts;
1456+
assert.deepEqual(kept.bitbucket, { email: "[email protected]", accessToken: "bb-access" });
1457+
assert.deepEqual(kept.github?.tokens, {
15001458
"github.com": "ghp_dotcom",
15011459
"ghe.acme.test": "ghp_ghe",
15021460
});
15031461

1504-
// An empty token removes that host's token and nothing else.
1462+
// An empty value removes that secret, and one per server host leaves the others.
15051463
const cleared = yield* serverSettings.updateSettings({
1506-
github: { tokens: { "github.com": "" } },
1464+
sourceControlHosts: {
1465+
bitbucket: { accessToken: "" },
1466+
github: {
1467+
tokens: {
1468+
"github.com": "",
1469+
"ghe.acme.test": (forClient.github!.tokens as Record<string, string>)[
1470+
"ghe.acme.test"
1471+
],
1472+
},
1473+
},
1474+
},
15071475
});
1508-
assert.equal(cleared.github.tokens["github.com"] ?? "", "");
1509-
assert.equal(cleared.github.tokens["ghe.acme.test"], "ghp_ghe");
1510-
const remaining = yield* Effect.forEach(["github.com", "ghe.acme.test"], (host) =>
1511-
secrets.get(`github-token-${Buffer.from(host, "utf8").toString("base64url")}`),
1512-
);
1513-
assert.isTrue(Option.isNone(remaining[0]!));
1514-
assert.isTrue(Option.isSome(remaining[1]!));
1476+
assert.equal(cleared.sourceControlHosts.bitbucket?.accessToken, "");
1477+
const stored = (kind: string, field: string, serverHost: string | null) =>
1478+
secrets.get(sourceControlHostSecretName({ kind, field, serverHost }));
1479+
assert.isTrue(Option.isNone(yield* stored("bitbucket", "accessToken", null)));
1480+
assert.isTrue(Option.isNone(yield* stored("github", "tokens", "github.com")));
1481+
assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));
15151482
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
15161483
);
15171484

1518-
it.effect("removes a Bitbucket secret once its token is cleared by hand in settings.json", () =>
1519-
Effect.gen(function* () {
1485+
it.effect("keeps the retired keys for another load when a token cannot be copied", () => {
1486+
const cause = new ServerSecretStore.SecretStoreReadError({
1487+
resource: "bitbucket-access-token",
1488+
cause: PlatformError.systemError({
1489+
_tag: "PermissionDenied",
1490+
module: "FileSystem",
1491+
method: "readFile",
1492+
pathOrDescriptor: "bitbucket-access-token",
1493+
description: "Secret backend unavailable.",
1494+
}),
1495+
});
1496+
const layerSettings = ServerSettingsModule.layer.pipe(
1497+
Layer.provide(layerFailingSecretStore(cause)),
1498+
Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
1499+
Layer.provideMerge(
1500+
Layer.fresh(
1501+
ServerConfig.layerTest(process.cwd(), {
1502+
prefix: "t3code-server-settings-legacy-failure-test-",
1503+
}),
1504+
),
1505+
),
1506+
);
1507+
return Effect.gen(function* () {
15201508
const serverConfig = yield* ServerConfig.ServerConfig;
15211509
const fileSystem = yield* FileSystem.FileSystem;
1522-
const secrets = yield* ServerSecretStore.ServerSecretStore;
15231510
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1524-
// A token was saved, then the user deleted it from settings.json directly.
1525-
yield* secrets.set("bitbucket-access-token", new TextEncoder().encode("stale-token"));
1526-
yield* fileSystem.writeFileString(serverConfig.settingsPath, "{}");
1511+
const legacy = JSON.stringify({
1512+
bitbucket: { email: "[email protected]", accessToken: "\u2022\u2022\u2022\u2022\u2022\u2022" },
1513+
});
1514+
yield* fileSystem.writeFileString(serverConfig.settingsPath, legacy);
15271515

1528-
yield* serverSettings.updateSettings({ cursorKeychainUsageEnabled: true });
1516+
yield* Effect.exit(serverSettings.getSettings);
15291517

1530-
assert.isTrue(Option.isNone(yield* secrets.get("bitbucket-access-token")));
1531-
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
1532-
);
1518+
assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), legacy);
1519+
}).pipe(Effect.provide(layerSettings));
1520+
});
15331521

1534-
it.effect("moves a hand-edited Bitbucket token into the secret store when settings load", () =>
1522+
it.effect("moves the retired bitbucket and github keys and their secrets on load", () =>
15351523
Effect.gen(function* () {
15361524
const serverConfig = yield* ServerConfig.ServerConfig;
15371525
const fileSystem = yield* FileSystem.FileSystem;
15381526
const secrets = yield* ServerSecretStore.ServerSecretStore;
15391527
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1528+
const marker = "\u2022\u2022\u2022\u2022\u2022\u2022";
1529+
yield* secrets.set("bitbucket-access-token", new TextEncoder().encode("bb-saved"));
1530+
yield* secrets.set(
1531+
`github-token-${Buffer.from("github.com", "utf8").toString("base64url")}`,
1532+
new TextEncoder().encode("ghp_saved"),
1533+
);
15401534
yield* fileSystem.writeFileString(
15411535
serverConfig.settingsPath,
1542-
'{"bitbucket":{"accessToken":"hand-edited-token"}}',
1536+
JSON.stringify({
1537+
bitbucket: { email: "[email protected]", accessToken: marker },
1538+
github: {
1539+
hosts: { "github.com": { account: "work" } },
1540+
tokens: { "github.com": marker },
1541+
},
1542+
}),
15431543
);
15441544

1545-
// Loading alone moves it: no settings update is needed.
1546-
const loaded = yield* serverSettings.getSettings;
1545+
const loaded = (yield* serverSettings.getSettings).sourceControlHosts;
15471546

1548-
assert.equal(loaded.bitbucket.accessToken, "hand-edited-token");
1549-
assert.notInclude(
1550-
yield* fileSystem.readFileString(serverConfig.settingsPath),
1551-
"hand-edited-token",
1552-
);
1553-
const stored = yield* secrets.get("bitbucket-access-token");
1554-
assert.equal(
1555-
Option.isSome(stored) ? new TextDecoder().decode(stored.value) : null,
1556-
"hand-edited-token",
1557-
);
1547+
assert.deepEqual(loaded.bitbucket, { email: "[email protected]", accessToken: "bb-saved" });
1548+
assert.deepEqual(loaded.github?.tokens, { "github.com": "ghp_saved" });
1549+
assert.deepEqual(loaded.github?.hosts, { "github.com": { account: "work" } });
1550+
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1551+
assert.notInclude(raw, '"bitbucket":{"email"');
1552+
assert.include(raw, "sourceControlHosts");
1553+
assert.isTrue(Option.isNone(yield* secrets.get("bitbucket-access-token")));
15581554
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
15591555
);
15601556

15611557
it.effect(
1562-
"moves a hand-edited Bitbucket token into the secret store when a client echoes the marker",
1558+
"moves a hand-edited source control token into the secret store when settings load",
15631559
() =>
15641560
Effect.gen(function* () {
15651561
const serverConfig = yield* ServerConfig.ServerConfig;
15661562
const fileSystem = yield* FileSystem.FileSystem;
15671563
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
15681564
yield* fileSystem.writeFileString(
15691565
serverConfig.settingsPath,
1570-
'{"bitbucket":{"email":"[email protected]","apiToken":"hand-edited-token"}}',
1566+
'{"sourceControlHosts":{"bitbucket":{"accessToken":"hand-edited-token"}}}',
15711567
);
15721568

1573-
// The form resends the redacted token when only the email changes.
1574-
const forClient = ServerSettingsModule.redactServerSettingsForClient(
1575-
yield* serverSettings.getSettings,
1576-
).bitbucket;
1577-
const updated = yield* serverSettings.updateSettings({
1578-
bitbucket: { email: "[email protected]", apiToken: forClient.apiToken },
1579-
});
1569+
// Loading alone moves it: no settings update is needed.
1570+
const loaded = yield* serverSettings.getSettings;
15801571

1581-
assert.equal(updated.bitbucket.apiToken, "hand-edited-token");
1582-
assert.equal((yield* serverSettings.getSettings).bitbucket.apiToken, "hand-edited-token");
1572+
assert.equal(loaded.sourceControlHosts.bitbucket?.accessToken, "hand-edited-token");
15831573
assert.notInclude(
15841574
yield* fileSystem.readFileString(serverConfig.settingsPath),
15851575
"hand-edited-token",
15861576
);
1587-
}).pipe(Effect.provide(layerServerSettings())),
1577+
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
15881578
);
15891579

15901580
it.effect("materializes provider secrets for terminal environment resolution", () =>

0 commit comments

Comments
 (0)