Skip to content

Commit 39c7f20

Browse files
committed
fix(settings): keep stored secret providers readable as their limits change
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 11c50ff commit 39c7f20

7 files changed

Lines changed: 57 additions & 66 deletions

File tree

‎apps/web/src/components/settings/SecretProviderDialog.logic.test.ts‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@ import { secretProviderFromDraft } from "./SecretProviderDialog.logic";
77
describe("secretProviderFromDraft", () => {
88
it("adds a provider keyed by its name and reports every field that blocks it", () => {
99
expect(
10-
secretProviderFromDraft(
11-
{ name: " Work 1Password! ", account: "my.1password.com" },
12-
new Set(),
13-
{ mode: "add" },
14-
),
10+
secretProviderFromDraft({ name: " 1Password! ", account: "my.1password.com" }, new Set(), {
11+
mode: "add",
12+
}),
1513
).toEqual(
1614
Result.succeed({
17-
id: "work-1password",
15+
id: "1password",
1816
config: {
19-
kind: "1password",
20-
label: "Work 1Password!",
17+
kind: "one_password",
18+
label: "1Password!",
2119
config: { account: "my.1password.com" },
2220
},
2321
}),
2422
);
2523

2624
const blocked = secretProviderFromDraft(
27-
{ name: "Work 1Password", account: "--help" },
28-
new Set(["work-1password"]),
25+
{ name: "1Password", account: "--help" },
26+
new Set(["1password"]),
2927
{ mode: "add" },
3028
);
3129
expect(Result.isFailure(blocked) && Object.keys(blocked.failure).toSorted()).toEqual([
@@ -34,17 +32,6 @@ describe("secretProviderFromDraft", () => {
3432
]);
3533
});
3634

37-
it("rejects a name that cannot start a slug", () => {
38-
const result = secretProviderFromDraft(
39-
{ name: "1Password", account: "my.1password.com" },
40-
new Set(),
41-
{ mode: "add" },
42-
);
43-
expect(Result.isFailure(result) && result.failure).toEqual({
44-
name: "The name needs to start with a letter.",
45-
});
46-
});
47-
4835
it("keeps the id when an edit renames the provider", () => {
4936
const id = SecretProviderId.make("personal");
5037
const result = secretProviderFromDraft(
@@ -55,7 +42,7 @@ describe("secretProviderFromDraft", () => {
5542
expect(Result.isSuccess(result) && result.success).toEqual({
5643
id,
5744
config: {
58-
kind: "1password",
45+
kind: "one_password",
5946
label: "Personal (team)",
6047
config: { account: "team.1password.com" },
6148
},

‎apps/web/src/components/settings/SecretProviderDialog.logic.ts‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,35 @@ import {
66
type OnePasswordAccount,
77
type SecretProviderConfig,
88
} from "@t3tools/contracts";
9+
import * as Option from "effect/Option";
910
import * as Result from "effect/Result";
1011
import * as Schema from "effect/Schema";
1112

12-
const decodeSecretProviderId = Schema.decodeResult(SecretProviderId);
13+
const decodeSecretProviderId = Schema.decodeOption(SecretProviderId);
1314
const decodeOnePasswordConfig = Schema.decodeUnknownOption(OnePasswordSecretProviderConfig);
1415

1516
export type ClassifiedSecretProvider =
16-
| { readonly kind: "1password"; readonly account: OnePasswordAccount }
17+
| { readonly kind: "one_password"; readonly account: OnePasswordAccount }
1718
| { readonly kind: "unsupported" };
1819

1920
/** Unknown kinds and configs that no longer decode are shown read-only; removal stays available. */
2021
export function classifySecretProvider(provider: SecretProviderConfig): ClassifiedSecretProvider {
21-
if (provider.kind !== "1password") return { kind: "unsupported" };
22+
if (provider.kind !== "one_password") return { kind: "unsupported" };
2223
const decoded = decodeOnePasswordConfig(provider.config);
2324
return decoded._tag === "Some"
24-
? { kind: "1password", account: decoded.value.account }
25+
? { kind: "one_password", account: decoded.value.account }
2526
: { kind: "unsupported" };
2627
}
2728

28-
/** Stable and readable in settings.json; the id must also be a valid slug, which starts with a letter. */
29-
function secretProviderIdFromName(name: string): Result.Result<SecretProviderId, string> {
29+
/** Stable and readable in settings.json, so re-adding a provider under the same name restores its variables. */
30+
function secretProviderIdFromName(name: string): SecretProviderId | null {
3031
const slug = name
3132
.toLowerCase()
3233
.replace(/[^a-z0-9]+/g, "-")
3334
.replace(/^-|-$/g, "")
3435
.slice(0, 64)
3536
.replace(/-$/, "");
36-
if (slug.length === 0) return Result.fail("The name needs at least one letter or digit.");
37-
return Result.mapError(
38-
decodeSecretProviderId(slug),
39-
() => "The name needs to start with a letter.",
40-
);
37+
return Option.getOrNull(decodeSecretProviderId(slug));
4138
}
4239

4340
export interface SecretProviderDraft {
@@ -49,7 +46,7 @@ export function draftFromSecretProvider(provider: SecretProviderConfig): SecretP
4946
const classified = classifySecretProvider(provider);
5047
return {
5148
name: provider.label,
52-
account: classified.kind === "1password" ? classified.account : "",
49+
account: classified.kind === "one_password" ? classified.account : "",
5350
};
5451
}
5552

@@ -92,16 +89,10 @@ export function secretProviderFromDraft(
9289
if (Result.isFailure(label)) {
9390
nameIssue = label.failure;
9491
} else {
95-
if (target.mode === "edit") {
96-
id = target.id;
97-
} else {
98-
const generated = secretProviderIdFromName(label.success);
99-
if (Result.isFailure(generated)) nameIssue = generated.failure;
100-
else {
101-
id = generated.success;
102-
if (existingIds.has(id)) nameIssue = "A secret provider with this name exists.";
103-
}
104-
}
92+
id = target.mode === "edit" ? target.id : secretProviderIdFromName(label.success);
93+
if (id === null) nameIssue = "The name needs at least one letter or digit.";
94+
else if (target.mode === "add" && existingIds.has(id))
95+
nameIssue = "A secret provider with this name exists.";
10596
}
10697

10798
if (
@@ -118,7 +109,7 @@ export function secretProviderFromDraft(
118109
return Result.succeed({
119110
id,
120111
config: {
121-
kind: SecretProviderKind.make("1password"),
112+
kind: SecretProviderKind.make("one_password"),
122113
label: label.success,
123114
config: { account: account.success },
124115
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsL
3030
const SECRET_PROVIDER_KINDS: Readonly<
3131
Record<string, { readonly name: string; readonly icon: Icon }>
3232
> = {
33-
"1password": { name: "1Password", icon: OnePasswordIcon },
33+
one_password: { name: "1Password", icon: OnePasswordIcon },
3434
};
3535

3636
export function SecretsSettingsPanel() {
@@ -127,7 +127,7 @@ function EnvironmentSecretProviders({
127127
</span>
128128
}
129129
description={
130-
classified.kind === "1password" && kind ? (
130+
classified.kind === "one_password" && kind ? (
131131
<span className="flex min-w-0 items-center gap-1">
132132
<span className="shrink-0">{kind.name} ·</span>
133133
<RedactedSensitiveText

‎packages/contracts/src/providerInstance.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ const PROVIDER_SLUG_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
77
const ENVIRONMENT_VARIABLE_NAME_MAX_CHARS = 128;
88
const ENVIRONMENT_VARIABLE_NAME_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
99

10-
export const SlugSchema = TrimmedNonEmptyString.check(
10+
const slugSchema = TrimmedNonEmptyString.check(
1111
Schema.isMaxLength(PROVIDER_SLUG_MAX_CHARS),
1212
Schema.isPattern(PROVIDER_SLUG_PATTERN),
1313
);
1414

15-
export const ProviderDriverKind = SlugSchema.pipe(Schema.brand("ProviderDriverKind"));
15+
export const ProviderDriverKind = slugSchema.pipe(Schema.brand("ProviderDriverKind"));
1616
export type ProviderDriverKind = typeof ProviderDriverKind.Type;
1717

1818
export const isProviderDriverKind = Schema.is(ProviderDriverKind);
1919

20-
export const ProviderInstanceId = SlugSchema.pipe(Schema.brand("ProviderInstanceId"));
20+
export const ProviderInstanceId = slugSchema.pipe(Schema.brand("ProviderInstanceId"));
2121
export type ProviderInstanceId = typeof ProviderInstanceId.Type;
2222

2323
export const ProviderInstanceEnvironmentVariableName = TrimmedNonEmptyString.check(

‎packages/contracts/src/settings.test.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ describe("ServerSettings usage price overrides", () => {
119119

120120
describe("ServerSettings secret providers", () => {
121121
const onePassword = (account: string) => ({
122-
kind: "1password",
122+
kind: "one_password",
123123
label: "Personal",
124124
config: { account },
125125
});
126126

127-
it("keeps an unknown provider kind from invalidating the rest of the settings", () => {
128-
const openbao = { kind: "openbao", label: "Vault", config: { address: "https://x" } };
127+
it("keeps an unknown kind or a longer label from invalidating the rest of the settings", () => {
128+
const openbao = { kind: "openbao", label: "V".repeat(65), config: { address: "https://x" } };
129129
const decoded = decodeServerSettings({
130130
worktreesDirectory: "/work",
131131
secretProviders: { vault: openbao, personal: onePassword("my.1password.com") },
@@ -140,14 +140,19 @@ describe("ServerSettings secret providers", () => {
140140
it("accepts slug ids and null removals but no id that is not a slug", () => {
141141
expect(
142142
decodeServerSettingsPatch({
143-
secretProviders: { personal: onePassword("my.1password.com"), work: null },
143+
secretProviders: { "1password": onePassword("my.1password.com"), work: null },
144144
}).secretProviders,
145-
).toEqual({ personal: onePassword("my.1password.com"), work: null });
146-
for (const id of ["", "My Vault", "a/b", "1password"]) {
145+
).toEqual({ "1password": onePassword("my.1password.com"), work: null });
146+
for (const id of ["", "My Vault", "a/b", "-vault"]) {
147147
expect(() =>
148148
decodeServerSettingsPatch({ secretProviders: { [id]: onePassword("my.1password.com") } }),
149149
).toThrow();
150150
}
151+
expect(() =>
152+
decodeServerSettingsPatch({
153+
secretProviders: { work: { ...onePassword("my.1password.com"), label: "V".repeat(65) } },
154+
}),
155+
).toThrow();
151156
});
152157

153158
it("rejects a 1Password account op could misread", () => {

‎packages/contracts/src/settings.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
ProviderInstanceConfig,
4747
ProviderInstanceId,
4848
ProviderDriverKind,
49-
SlugSchema,
5049
} from "./providerInstance.ts";
5150
import { PullRequestMergeMethod } from "./pullRequest.ts";
5251

@@ -853,14 +852,17 @@ export const UsageLimitSourceConfig = Schema.Struct({
853852
});
854853
export type UsageLimitSourceConfig = typeof UsageLimitSourceConfig.Type;
855854

856-
export const SecretProviderId = SlugSchema.pipe(Schema.brand("SecretProviderId"));
857-
export type SecretProviderId = typeof SecretProviderId.Type;
858-
859-
// Same shape as a slug but may start with a digit, because the first kind is "1password".
860-
export const SecretProviderKind = TrimmedNonEmptyString.check(
855+
// Final once shipped: a key or field that fails here fails the whole settings decode on older builds.
856+
// Unlike provider slugs it may start with a digit, so a provider named "1Password" fits.
857+
const SecretProviderSlug = TrimmedNonEmptyString.check(
861858
Schema.isMaxLength(64),
862859
Schema.isPattern(/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/),
863-
).pipe(Schema.brand("SecretProviderKind"));
860+
);
861+
862+
export const SecretProviderId = SecretProviderSlug.pipe(Schema.brand("SecretProviderId"));
863+
export type SecretProviderId = typeof SecretProviderId.Type;
864+
865+
export const SecretProviderKind = SecretProviderSlug.pipe(Schema.brand("SecretProviderKind"));
864866
export type SecretProviderKind = typeof SecretProviderKind.Type;
865867

866868
const ONE_PASSWORD_ACCOUNT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
@@ -889,11 +891,17 @@ export type OnePasswordSecretProviderConfig = typeof OnePasswordSecretProviderCo
889891
/** Kinds decode their own config; unknown-kind envelopes round-trip verbatim across versions. */
890892
export const SecretProviderConfig = Schema.Struct({
891893
kind: SecretProviderKind,
892-
label: SecretProviderLabel,
894+
label: TrimmedNonEmptyString,
893895
config: Schema.optionalKey(Schema.Unknown),
894896
});
895897
export type SecretProviderConfig = typeof SecretProviderConfig.Type;
896898

899+
// Limits that may change later are enforced on writes only, never on the stored envelope.
900+
const SecretProviderConfigWrite = Schema.Struct({
901+
...SecretProviderConfig.fields,
902+
label: SecretProviderLabel,
903+
});
904+
897905
export const SourceControlHostSettings = Schema.Record(Schema.String, Schema.Unknown);
898906
export type SourceControlHostSettings = typeof SourceControlHostSettings.Type;
899907

@@ -1661,7 +1669,7 @@ export const ServerSettingsPatch = Schema.Struct({
16611669
Schema.Record(UsageLimitSourceId, Schema.NullOr(UsageLimitSourceConfig)),
16621670
),
16631671
secretProviders: Schema.optionalKey(
1664-
Schema.Record(SecretProviderId, Schema.NullOr(SecretProviderConfig)),
1672+
Schema.Record(SecretProviderId, Schema.NullOr(SecretProviderConfigWrite)),
16651673
),
16661674
cursorKeychainUsageEnabled: Schema.optionalKey(Schema.Boolean),
16671675

‎packages/shared/src/serverSettings.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ describe("serverSettings helpers", () => {
533533
const personal = SecretProviderId.make("personal");
534534
const work = SecretProviderId.make("work");
535535
const onePassword = (label: string, account: string) => ({
536-
kind: SecretProviderKind.make("1password"),
536+
kind: SecretProviderKind.make("one_password"),
537537
label,
538538
config: { account: OnePasswordAccount.make(account) },
539539
});

0 commit comments

Comments
 (0)