Skip to content

Commit 84537c4

Browse files
yordismaria-rcks
andauthored
feat(server): read source control host tokens from a secret provider (#27756)
Signed-off-by: Yordis Prieto <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9bcf753 commit 84537c4

12 files changed

Lines changed: 256 additions & 58 deletions

File tree

‎apps/server/src/provider/ProviderInstanceRegistry.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
type SecretReference,
1010
secretReferencesInSettings,
1111
type ServerProvider,
12+
type SettingsSecretSlot,
13+
withSettingsSecrets,
1214
} from "@t3tools/contracts";
1315
import * as Context from "effect/Context";
1416
import * as Effect from "effect/Effect";
@@ -45,7 +47,7 @@ export class ProviderInstanceRegistry extends Context.Service<
4547

4648
interface InstanceSecrets {
4749
readonly environment: ResolvedProviderInstanceEnvironment;
48-
readonly config: Readonly<Record<string, string>>;
50+
readonly config: ReadonlyArray<SettingsSecretSlot & { readonly value: string }>;
4951
}
5052

5153
interface LiveEntry {
@@ -110,12 +112,12 @@ const readInstanceSecrets = Effect.fn("ProviderInstanceRegistry.readInstanceSecr
110112
}
111113
return [{ ...variable, value: secret, sensitive: true }];
112114
});
113-
const config: Record<string, string> = {};
114-
for (const { field } of configFields) {
115+
const config = configFields.flatMap(({ field, key }) => {
115116
const secret = values[index++];
116-
if (secret === undefined) unreadable.push(field);
117-
else config[field] = secret;
118-
}
117+
if (secret !== undefined) return [{ field, key, value: secret }];
118+
unreadable.push(field);
119+
return [];
120+
});
119121
const secrets: InstanceSecrets = { environment, config };
120122
return { secrets, unreadable };
121123
});
@@ -224,7 +226,7 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* <R
224226
const { secrets, unreadable } = enabled
225227
? yield* readInstanceSecrets(driver, entry)
226228
: {
227-
secrets: { environment: withoutSecretReferences(entry.environment ?? []), config: {} },
229+
secrets: { environment: withoutSecretReferences(entry.environment ?? []), config: [] },
228230
unreadable: [],
229231
};
230232
// Starting with a blank credential would fail later with a less useful error, or fall back to
@@ -252,9 +254,9 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* <R
252254
environment: secrets.environment,
253255
enabled,
254256
config:
255-
Object.keys(secrets.config).length === 0
257+
secrets.config.length === 0
256258
? typedConfig
257-
: { ...typedConfig, ...secrets.config },
259+
: withSettingsSecrets(typedConfig, secrets.config),
258260
})
259261
.pipe(Effect.provideService(Scope.Scope, childScope), Effect.result);
260262
if (createResult._tag === "Failure") {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { secretReferencesInSettings } from "@t3tools/contracts";
1+
import { secretReferencesInSettings, withSettingsSecrets } from "@t3tools/contracts";
22
import type * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
33
import * as Effect from "effect/Effect";
44
import type * as Schema from "effect/Schema";
55

66
/**
7-
* Replaces each reference in the fields `schema` declares as `SettingsSecretValue` with the secret
7+
* Replaces each reference in the slots `schema` declares as `SettingsSecretValue` with the secret
88
* it names, read in one batch. An unreadable secret comes back as `""`, so it behaves as unset.
99
*/
1010
export const resolveSettingsSecrets = Effect.fn("SettingsSecrets.resolve")(function* <
@@ -13,8 +13,8 @@ export const resolveSettingsSecrets = Effect.fn("SettingsSecrets.resolve")(funct
1313
const pending = secretReferencesInSettings(schema, settings);
1414
if (pending.length === 0) return settings;
1515
const values = yield* resolver.read(pending.map(({ reference }) => reference));
16-
const resolved = Object.fromEntries(
17-
pending.map(({ field }, index) => [field, values[index] ?? ""] as const),
16+
return withSettingsSecrets(
17+
settings,
18+
pending.map((slot, index) => ({ ...slot, value: values[index] ?? "" })),
1819
);
19-
return { ...settings, ...resolved };
2020
});

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,20 +1547,34 @@ it.layer(NodeServices.layer)("server settings", (it) => {
15471547
provider: SecretProviderId.make("personal"),
15481548
reference: "op://Private/Bitbucket/token",
15491549
};
1550+
const gitHubReference = {
1551+
provider: SecretProviderId.make("personal"),
1552+
reference: "op://Private/GitHub/token",
1553+
};
15501554
yield* serverSettings.updateSettings({
1551-
sourceControlHosts: { bitbucket: { accessToken: "bb-access" } },
1555+
sourceControlHosts: {
1556+
bitbucket: { accessToken: "bb-access" },
1557+
github: { tokens: { "github.com": "ghp_dotcom", "ghe.acme.test": "ghp_ghe" } },
1558+
},
15521559
});
15531560
assert.isTrue(Option.isSome(yield* stored("bitbucket", "accessToken", null)));
1561+
assert.isTrue(Option.isSome(yield* stored("github", "tokens", "github.com")));
15541562
const referenced = yield* serverSettings.updateSettings({
1555-
sourceControlHosts: { bitbucket: { accessToken: reference } },
1563+
sourceControlHosts: {
1564+
bitbucket: { accessToken: reference },
1565+
github: { tokens: { "github.com": gitHubReference, "ghe.acme.test": "ghp_ghe" } },
1566+
},
15561567
});
15571568
assert.deepEqual(referenced.sourceControlHosts.bitbucket?.accessToken, reference);
15581569
assert.isTrue(Option.isNone(yield* stored("bitbucket", "accessToken", null)));
1559-
assert.deepEqual(
1560-
ServerSettingsModule.redactServerSettingsForClient(referenced).sourceControlHosts
1561-
.bitbucket?.accessToken,
1562-
reference,
1563-
);
1570+
assert.isTrue(Option.isNone(yield* stored("github", "tokens", "github.com")));
1571+
assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));
1572+
const redacted =
1573+
ServerSettingsModule.redactServerSettingsForClient(referenced).sourceControlHosts;
1574+
assert.deepEqual(redacted.bitbucket?.accessToken, reference);
1575+
const tokens = redacted.github?.tokens as Record<string, unknown>;
1576+
assert.deepEqual(tokens["github.com"], gitHubReference);
1577+
assert.notEqual(tokens["ghe.acme.test"], "ghp_ghe");
15641578
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
15651579
);
15661580

‎apps/server/src/sourceControl/sourceControlHostSecrets.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const source = (reference: string) => ({
1414
reference,
1515
});
1616

17-
const secretsByReference = new Map([["bitbucket#token", "bb_from_provider"]]);
17+
const secretsByReference = new Map([
18+
["bitbucket#token", "bb_from_provider"],
19+
["github#token", "ghp_from_provider"],
20+
]);
21+
22+
const perHostHosts = {
23+
github: { tokens: { "github.com": source("github#token"), "ghe.acme.test": "ghp_literal" } },
24+
};
1825

1926
const hosts = {
2027
bitbucket: { email: "[email protected]", accessToken: source("bitbucket#token") },
@@ -69,11 +76,25 @@ it.effect("leaves the resolver unused for a kind that holds no reference", () =>
6976
}),
7077
);
7178

79+
it.effect("replaces a per-host token reference and keeps the literal beside it", () =>
80+
Effect.gen(function* () {
81+
const { reads, resolver } = countingResolver();
82+
const resolved = yield* resolveSourceControlHostSecrets(resolver, perHostHosts, "github");
83+
assert.deepEqual(resolved, {
84+
github: { tokens: { "github.com": "ghp_from_provider", "ghe.acme.test": "ghp_literal" } },
85+
});
86+
assert.deepEqual(reads, ["github#token"]);
87+
}),
88+
);
89+
7290
it("collects the references held in credential fields and skips literal values", () => {
7391
assert.deepEqual(collectSourceControlHostSecretReferences(hosts), [
7492
source("missing#token"),
7593
source("bitbucket#token"),
7694
]);
95+
assert.deepEqual(collectSourceControlHostSecretReferences(perHostHosts), [
96+
source("github#token"),
97+
]);
7798
assert.deepEqual(
7899
collectSourceControlHostSecretReferences({
79100
bitbucket: { accessToken: "literal" },

‎apps/server/src/sourceControl/sourceControlHostSecrets.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ export function writeSecretFieldValues(
5353
): unknown {
5454
if (typeof original === "string") return values[0]?.[1] ?? "";
5555
if (isSecretReference(original)) return original;
56-
return Object.fromEntries(values.map(([serverHost, value]) => [serverHost ?? "", value]));
56+
const sources =
57+
original !== null && typeof original === "object" && !Array.isArray(original)
58+
? Object.entries(original).flatMap(([serverHost, entry]) =>
59+
isSecretReference(entry) ? [[serverHost.trim().toLowerCase(), entry] as const] : [],
60+
)
61+
: [];
62+
return Object.fromEntries([
63+
...sources,
64+
...values.map(([serverHost, value]) => [serverHost ?? "", value] as const),
65+
]);
5766
}
5867

5968
type SourceControlHostsSettings = Readonly<Record<string, Readonly<Record<string, unknown>>>>;

‎apps/web/src/components/settings/SourceControlAppSignInSettings.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ export function SourceControlAppSignInSettings({
101101
readHostCredentialSettings(settings, definition.kind),
102102
);
103103
const signedIn = (signIns[host] ?? "").length > 0;
104-
const tokenOverrides = (tokens[host] ?? "").length > 0;
104+
const token = tokens[host];
105+
// A token read from a secret provider overrides the sign-in like a saved one.
106+
const tokenOverrides = typeof token === "string" ? token.length > 0 : token !== undefined;
105107
const canSignIn = useAtomValue(
106108
sourceControlEnvironment.startSignIn.permissionAtom(environmentId),
107109
);

‎apps/web/src/components/settings/SourceControlTokenSettings.tsx‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { EnvironmentId } from "@t3tools/contracts";
1+
import {
2+
isSecretReference,
3+
type EnvironmentId,
4+
type SecretReference,
5+
type SettingsSecretValue,
6+
} from "@t3tools/contracts";
27
import { ExternalLinkIcon } from "lucide-react";
38
import { useState } from "react";
49

@@ -12,8 +17,11 @@ import { serverEnvironment } from "../../state/server";
1217
import { useAtomCommand } from "../../state/use-atom-command";
1318
import { Button, InlineButton } from "../ui/button";
1419
import { Input } from "../ui/input";
15-
import { SecretInput } from "./SecretInput";
1620
import { Label } from "../ui/label";
21+
import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
22+
23+
const hasToken = (token: SettingsSecretValue | undefined) =>
24+
typeof token === "string" ? token.length > 0 : token !== undefined;
1725

1826
export function SourceControlTokenSettings({
1927
environmentId,
@@ -34,15 +42,24 @@ export function SourceControlTokenSettings({
3442
});
3543
const defaultHost = definition.publicHost ?? "";
3644
const [host, setHost] = useState(defaultHost);
37-
const [draft, setDraft] = useState("");
45+
const secretSources = useSecretSources(environmentId);
46+
const [pending, setPending] = useState<{
47+
readonly key: string;
48+
readonly draft: string | SecretReference;
49+
} | null>(null);
3850
const [saving, setSaving] = useState(false);
3951
const normalizedHost = host.trim().toLowerCase();
40-
const isSaved = (tokens[normalizedHost] ?? "").length > 0;
52+
const savedToken = tokens[normalizedHost];
53+
const isSaved = hasToken(savedToken);
4154
const savedHosts = Object.entries(tokens)
42-
.filter(([, value]) => value.length > 0)
55+
.filter(([, value]) => hasToken(value))
4356
.map(([saved]) => saved);
57+
const sourceKey = JSON.stringify([normalizedHost, savedToken ?? ""]);
58+
const draft =
59+
pending?.key === sourceKey ? pending.draft : isSecretReference(savedToken) ? savedToken : "";
60+
const submission = typeof draft === "string" ? draft.trim() : draft;
4461

45-
const save = async (target: string, token: string) => {
62+
const save = async (target: string, token: SettingsSecretValue) => {
4663
setSaving(true);
4764
const result = await updateSettings({
4865
environmentId,
@@ -55,7 +72,7 @@ export function SourceControlTokenSettings({
5572
});
5673
setSaving(false);
5774
if (result._tag === "Success") {
58-
setDraft("");
75+
setPending(null);
5976
onSaved();
6077
}
6178
};
@@ -65,7 +82,7 @@ export function SourceControlTokenSettings({
6582
className="grid gap-3"
6683
onSubmit={(event) => {
6784
event.preventDefault();
68-
if (normalizedHost && draft.trim()) void save(normalizedHost, draft.trim());
85+
if (normalizedHost && submission !== "") void save(normalizedHost, submission);
6986
}}
7087
>
7188
{/* Locked while saving: a successful save clears the draft, which would drop edits made mid-request. */}
@@ -106,12 +123,15 @@ export function SourceControlTokenSettings({
106123
</div>
107124
<div className="grid gap-1.5">
108125
<Label htmlFor={`${definition.kind}-token-${environmentId}`}>Token</Label>
109-
<SecretInput
126+
<SecretSourceInput
110127
id={`${definition.kind}-token-${environmentId}`}
111128
size="sm"
112-
configured={isSaved}
129+
aria-label={`${definition.label} token`}
130+
configured={isSaved && !isSecretReference(savedToken)}
113131
value={draft}
114-
onChange={(event) => setDraft(event.target.value)}
132+
sources={secretSources}
133+
onChange={(event) => setPending({ key: sourceKey, draft: event.target.value })}
134+
onSecretChange={(secret) => setPending({ key: sourceKey, draft: secret ?? "" })}
115135
/>
116136
</div>
117137
</div>
@@ -132,7 +152,11 @@ export function SourceControlTokenSettings({
132152
Remove
133153
</Button>
134154
) : null}
135-
<Button type="submit" size="xs" disabled={!normalizedHost || !draft.trim() || saving}>
155+
<Button
156+
type="submit"
157+
size="xs"
158+
disabled={!normalizedHost || submission === "" || saving}
159+
>
136160
Save
137161
</Button>
138162
</div>

‎packages/contracts/src/settingsSecretValue.ts‎

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,68 @@ export function acceptsSecretReference(schema: Schema.Top): boolean {
2626
return Schema.resolveAnnotationsKey(schema)?.settingsSecretReference === true;
2727
}
2828

29-
/** Fields of a settings struct typed `SettingsSecretValue`; any other schema has none. */
30-
function secretReferenceSettingsFields(schema: Schema.Top): ReadonlyArray<string> {
29+
const declaresSecretReference = (ast: SchemaAST.AST) =>
30+
ast.context?.annotations?.settingsSecretReference === true;
31+
32+
interface SecretReferenceField {
33+
readonly field: string;
34+
readonly keyed: boolean;
35+
}
36+
37+
/**
38+
* Fields of a settings struct typed `SettingsSecretValue`, or a record of it such as one value
39+
* per host; any other schema has none.
40+
*/
41+
function secretReferenceSettingsFields(schema: Schema.Top): ReadonlyArray<SecretReferenceField> {
3142
if (!SchemaAST.isObjects(schema.ast)) return [];
32-
return schema.ast.propertySignatures.flatMap((property) =>
33-
property.type.context?.annotations?.settingsSecretReference === true &&
34-
typeof property.name === "string"
35-
? [property.name]
36-
: [],
37-
);
43+
return schema.ast.propertySignatures.flatMap<SecretReferenceField>(({ name, type }) => {
44+
if (typeof name !== "string") return [];
45+
if (declaresSecretReference(type)) return [{ field: name, keyed: false }];
46+
return SchemaAST.isObjects(type) &&
47+
type.indexSignatures.some((signature) => declaresSecretReference(signature.type))
48+
? [{ field: name, keyed: true }]
49+
: [];
50+
});
3851
}
3952

40-
/** Each declared reference field of `settings` that holds a reference, in field order. */
53+
/** Where a reference sits: a field, or one key of a record field. */
54+
export interface SettingsSecretSlot {
55+
readonly field: string;
56+
readonly key: string | undefined;
57+
}
58+
59+
interface SettingsSecretReference extends SettingsSecretSlot {
60+
readonly reference: SecretReference;
61+
}
62+
63+
/** Each declared reference slot of `settings` that holds a reference, in field order. */
4164
export function secretReferencesInSettings(
4265
schema: Schema.Top,
4366
settings: unknown,
44-
): ReadonlyArray<{ readonly field: string; readonly reference: SecretReference }> {
67+
): ReadonlyArray<SettingsSecretReference> {
4568
if (settings === null || typeof settings !== "object") return [];
4669
const values = settings as Readonly<Record<string, unknown>>;
47-
return secretReferenceSettingsFields(schema).flatMap((field) => {
48-
const value = values[field];
49-
return isSecretReference(value) ? [{ field, reference: value }] : [];
50-
});
70+
return secretReferenceSettingsFields(schema).flatMap<SettingsSecretReference>(
71+
({ field, keyed }) => {
72+
const value = values[field];
73+
if (!keyed)
74+
return isSecretReference(value) ? [{ field, key: undefined, reference: value }] : [];
75+
if (value === null || typeof value !== "object") return [];
76+
return Object.entries(value).flatMap(([key, entry]) =>
77+
isSecretReference(entry) ? [{ field, key, reference: entry }] : [],
78+
);
79+
},
80+
);
81+
}
82+
83+
/** `settings` with each slot set to its value, keeping the other keys of a record field. */
84+
export function withSettingsSecrets<Settings extends Readonly<Record<string, unknown>>>(
85+
settings: Settings,
86+
slots: ReadonlyArray<SettingsSecretSlot & { readonly value: string }>,
87+
): Settings {
88+
const next: Record<string, unknown> = { ...settings };
89+
for (const { field, key, value } of slots) {
90+
next[field] = key === undefined ? value : { ...(next[field] as object), [key]: value };
91+
}
92+
return next as Settings;
5193
}

‎packages/source-control-core/src/shared/definition.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type PullRequestReviewVerdict,
44
type RepositoryIdentity,
55
secretSetting,
6+
SettingsSecretValue,
67
SourceControlProviderKind,
78
type SourceControlProviderInfo,
89
TrimmedNonEmptyString,
@@ -214,7 +215,7 @@ export const hostCredentialSettingsFields = {
214215
Schema.withDecodingDefault(Effect.succeed({})),
215216
Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
216217
),
217-
tokens: Schema.Record(SourceControlHostName, TrimmedString).pipe(
218+
tokens: Schema.Record(SourceControlHostName, SettingsSecretValue).pipe(
218219
Schema.withDecodingDefault(Effect.succeed({})),
219220
secretSetting,
220221
Schema.annotateKey({ providerSettingsForm: { hidden: true } }),

0 commit comments

Comments
 (0)