Skip to content

Commit 7a4f5b6

Browse files
yordisjuliusmarmingeclaude
authored
feat(settings): add and list 1Password secret providers behind T3CODE_FEATURE_SECRETS (#25709)
Signed-off-by: Yordis Prieto <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 960d576 commit 7a4f5b6

19 files changed

Lines changed: 782 additions & 5 deletions

‎apps/server/src/auth/RpcAuthorization.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AuthSettingsWriteScope,
77
DEFAULT_SERVER_SETTINGS,
88
ProviderInstanceId,
9+
SecretProviderId,
910
ThreadId,
1011
AuthOrchestrationOperateScope,
1112
AuthOrchestrationReadScope,
@@ -173,7 +174,7 @@ describe("settings mutation authorization", () => {
173174
}).pipe(Effect.scoped),
174175
);
175176

176-
it.effect("does not let a settings grant create or remove providers", () =>
177+
it.effect("does not let a settings grant create or remove providers or secret providers", () =>
177178
Effect.gen(function* () {
178179
let handled = false;
179180
const client = yield* RpcTest.makeClient(group).pipe(
@@ -195,6 +196,11 @@ describe("settings mutation authorization", () => {
195196
providerInstanceMutation,
196197
}).pipe(Effect.flip),
197198
).toMatchObject({ requiredPermission: AuthProvidersManageScope });
199+
expect(
200+
yield* client[WS_METHODS.serverUpdateSettings]({
201+
patch: { secretProviders: { [SecretProviderId.make("personal")]: null } },
202+
}).pipe(Effect.flip),
203+
).toMatchObject({ requiredPermission: AuthProvidersManageScope });
198204
expect(handled).toBe(false);
199205
}).pipe(Effect.scoped),
200206
);

‎apps/web/src/components/Icons.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,12 @@ export const MacAccessibilityIcon: Icon = (props) => {
505505
</svg>
506506
);
507507
};
508+
509+
export const OnePasswordIcon: Icon = (props) => (
510+
<svg {...props} viewBox="0 0 24 24" fill="none">
511+
<path
512+
d="M12 0c6.627 0 12 5.373 12 12 0 6.628-5.373 12-12 12S0 18.628 0 12C0 5.373 5.373 0 12 0m-.893 4.86c-.485 0-.727.001-.913.095a.87.87 0 0 0-.378.379c-.094.185-.095.428-.095.912v2.747c0 .12 0 .182.016.238q.02.075.065.138a1 1 0 0 0 .175.162l.695.564c.113.092.17.139.19.194a.22.22 0 0 1 0 .15c-.02.056-.077.102-.19.194l-.695.564a1 1 0 0 0-.175.162.4.4 0 0 0-.065.138 1 1 0 0 0-.016.238v6.019c0 .485 0 .728.095.913a.87.87 0 0 0 .378.378c.186.094.428.094.913.094h1.786c.485 0 .727 0 .913-.094a.87.87 0 0 0 .378-.378c.095-.185.095-.428.095-.913v-2.747c0-.12 0-.182-.016-.238a.4.4 0 0 0-.065-.138 1 1 0 0 0-.175-.162l-.695-.564c-.113-.092-.17-.138-.191-.193a.22.22 0 0 1 0-.152c.02-.055.078-.1.19-.193l.696-.564a1 1 0 0 0 .175-.162.4.4 0 0 0 .065-.138 1 1 0 0 0 .016-.238V6.246c0-.484 0-.727-.095-.912a.87.87 0 0 0-.378-.379c-.186-.094-.428-.094-.913-.094Z"
513+
fill="currentColor"
514+
/>
515+
</svg>
516+
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { SecretProviderId } from "@t3tools/contracts";
2+
import * as Result from "effect/Result";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import { secretProviderFromDraft } from "./SecretProviderDialog.logic";
6+
7+
describe("secretProviderFromDraft", () => {
8+
it("adds a provider keyed by its name and reports every field that blocks it", () => {
9+
expect(
10+
secretProviderFromDraft({ name: " 1Password! ", account: "my.1password.com" }, new Set(), {
11+
mode: "add",
12+
}),
13+
).toEqual(
14+
Result.succeed({
15+
id: "1password",
16+
config: {
17+
kind: "one_password",
18+
label: "1Password!",
19+
config: { account: "my.1password.com" },
20+
},
21+
}),
22+
);
23+
24+
const blocked = secretProviderFromDraft(
25+
{ name: "1Password", account: "--help" },
26+
new Set(["1password"]),
27+
{ mode: "add" },
28+
);
29+
expect(Result.isFailure(blocked) && Object.keys(blocked.failure).toSorted()).toEqual([
30+
"account",
31+
"name",
32+
]);
33+
});
34+
35+
it("keeps the id when an edit renames the provider", () => {
36+
const id = SecretProviderId.make("personal");
37+
const result = secretProviderFromDraft(
38+
{ name: "Personal (team)", account: "team.1password.com" },
39+
new Set(["personal"]),
40+
{ mode: "edit", id },
41+
);
42+
expect(Result.isSuccess(result) && result.success).toEqual({
43+
id,
44+
config: {
45+
kind: "one_password",
46+
label: "Personal (team)",
47+
config: { account: "team.1password.com" },
48+
},
49+
});
50+
});
51+
});
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import {
2+
OnePasswordSecretProviderConfig,
3+
SecretProviderId,
4+
SecretProviderKind,
5+
SecretProviderLabel,
6+
type OnePasswordAccount,
7+
type SecretProviderConfig,
8+
} from "@t3tools/contracts";
9+
import * as Option from "effect/Option";
10+
import * as Result from "effect/Result";
11+
import * as Schema from "effect/Schema";
12+
13+
const decodeSecretProviderId = Schema.decodeOption(SecretProviderId);
14+
const decodeOnePasswordConfig = Schema.decodeUnknownOption(OnePasswordSecretProviderConfig);
15+
16+
export type ClassifiedSecretProvider =
17+
| { readonly kind: "one_password"; readonly account: OnePasswordAccount }
18+
| { readonly kind: "unsupported" };
19+
20+
/** Unknown kinds and configs that no longer decode are shown read-only; removal stays available. */
21+
export function classifySecretProvider(provider: SecretProviderConfig): ClassifiedSecretProvider {
22+
if (provider.kind !== "one_password") return { kind: "unsupported" };
23+
const decoded = decodeOnePasswordConfig(provider.config);
24+
return decoded._tag === "Some"
25+
? { kind: "one_password", account: decoded.value.account }
26+
: { kind: "unsupported" };
27+
}
28+
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 {
31+
const slug = name
32+
.toLowerCase()
33+
.replace(/[^a-z0-9]+/g, "-")
34+
.replace(/^-|-$/g, "")
35+
.slice(0, 64)
36+
.replace(/-$/, "");
37+
return Option.getOrNull(decodeSecretProviderId(slug));
38+
}
39+
40+
export interface SecretProviderDraft {
41+
readonly name: string;
42+
readonly account: string;
43+
}
44+
45+
export function draftFromSecretProvider(provider: SecretProviderConfig): SecretProviderDraft {
46+
const classified = classifySecretProvider(provider);
47+
return {
48+
name: provider.label,
49+
account: classified.kind === "one_password" ? classified.account : "",
50+
};
51+
}
52+
53+
export type SecretProviderTarget =
54+
| { readonly mode: "add" }
55+
| { readonly mode: "edit"; readonly id: SecretProviderId };
56+
57+
export type SecretProviderDraftIssues = {
58+
readonly [Field in keyof SecretProviderDraft]?: string;
59+
};
60+
61+
function decodeField<S extends Schema.ConstraintDecoder<unknown>>(
62+
schema: S,
63+
value: string,
64+
emptyMessage: string,
65+
): Result.Result<S["Type"], string> {
66+
const trimmed = value.trim();
67+
if (trimmed.length === 0) return Result.fail(emptyMessage);
68+
return Result.mapError(Schema.decodeResult(schema)(trimmed), (error) => error.message);
69+
}
70+
71+
/** The settings entry a draft adds or replaces, or every field that blocks it, one message each. */
72+
export function secretProviderFromDraft(
73+
draft: SecretProviderDraft,
74+
existingIds: ReadonlySet<string>,
75+
target: SecretProviderTarget,
76+
): Result.Result<
77+
{ readonly id: SecretProviderId; readonly config: SecretProviderConfig },
78+
SecretProviderDraftIssues
79+
> {
80+
const label = decodeField(SecretProviderLabel, draft.name, "Enter a name.");
81+
const account = decodeField(
82+
OnePasswordSecretProviderConfig.fields.account,
83+
draft.account,
84+
"Enter the 1Password account.",
85+
);
86+
87+
let id: SecretProviderId | null = null;
88+
let nameIssue: string | undefined;
89+
if (Result.isFailure(label)) {
90+
nameIssue = label.failure;
91+
} else {
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.";
96+
}
97+
98+
if (
99+
id === null ||
100+
nameIssue !== undefined ||
101+
Result.isFailure(label) ||
102+
Result.isFailure(account)
103+
) {
104+
return Result.fail({
105+
...(nameIssue === undefined ? {} : { name: nameIssue }),
106+
...(Result.isFailure(account) ? { account: account.failure } : {}),
107+
});
108+
}
109+
return Result.succeed({
110+
id,
111+
config: {
112+
kind: SecretProviderKind.make("one_password"),
113+
label: label.success,
114+
config: { account: account.success },
115+
},
116+
});
117+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import type { EnvironmentId, SecretProviderConfig, SecretProviderId } from "@t3tools/contracts";
2+
import * as Result from "effect/Result";
3+
import { useState } from "react";
4+
5+
import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
6+
import { Button } from "../ui/button";
7+
import {
8+
Dialog,
9+
DialogDescription,
10+
DialogFooter,
11+
DialogHeader,
12+
DialogPanel,
13+
DialogPopup,
14+
DialogTitle,
15+
} from "../ui/dialog";
16+
import { Input } from "../ui/input";
17+
import { Label } from "../ui/label";
18+
import {
19+
draftFromSecretProvider,
20+
secretProviderFromDraft,
21+
type SecretProviderDraft,
22+
} from "./SecretProviderDialog.logic";
23+
24+
/** Mounted per open, so each open starts from a fresh draft. */
25+
export function SecretProviderDialog({
26+
onClose,
27+
environmentId,
28+
environmentLabel,
29+
existingIds,
30+
existing,
31+
}: {
32+
readonly onClose: () => void;
33+
readonly environmentId: EnvironmentId;
34+
readonly environmentLabel: string;
35+
readonly existingIds: ReadonlySet<string>;
36+
readonly existing?: { readonly id: SecretProviderId; readonly config: SecretProviderConfig };
37+
}) {
38+
const updateSettings = useUpdateEnvironmentSettings(environmentId);
39+
const [draft, setDraft] = useState<SecretProviderDraft>(
40+
existing ? draftFromSecretProvider(existing.config) : { name: "", account: "" },
41+
);
42+
const [submitted, setSubmitted] = useState(false);
43+
const result = secretProviderFromDraft(
44+
draft,
45+
existingIds,
46+
existing ? { mode: "edit", id: existing.id } : { mode: "add" },
47+
);
48+
const issues = Result.isFailure(result) ? result.failure : {};
49+
const nameError = submitted ? issues.name : undefined;
50+
const accountError = submitted ? issues.account : undefined;
51+
52+
const update = (patch: Partial<SecretProviderDraft>) => setDraft({ ...draft, ...patch });
53+
const save = () => {
54+
setSubmitted(true);
55+
if (Result.isFailure(result)) return;
56+
updateSettings({ secretProviders: { [result.success.id]: result.success.config } });
57+
onClose();
58+
};
59+
60+
return (
61+
<Dialog
62+
open
63+
onOpenChange={(next) => {
64+
if (!next) onClose();
65+
}}
66+
>
67+
<DialogPopup className="max-w-md">
68+
<DialogHeader>
69+
<DialogTitle>
70+
{existing ? `Edit ${existing.config.label}` : "Add a 1Password provider"}
71+
</DialogTitle>
72+
<DialogDescription>
73+
Provider variables on {environmentLabel} can read values from it by name. Values are
74+
read with the op CLI on that server when a provider starts.
75+
</DialogDescription>
76+
</DialogHeader>
77+
<DialogPanel>
78+
<form
79+
id="secret-provider-form"
80+
className="grid gap-4"
81+
onSubmit={(event) => {
82+
event.preventDefault();
83+
save();
84+
}}
85+
>
86+
<div className="grid gap-1.5">
87+
<Label htmlFor="secret-provider-name">Name</Label>
88+
<Input
89+
id="secret-provider-name"
90+
placeholder="Personal"
91+
value={draft.name}
92+
onChange={(event) => update({ name: event.target.value })}
93+
maxLength={64}
94+
aria-invalid={nameError !== undefined}
95+
aria-describedby={
96+
nameError !== undefined ? "secret-provider-name-error" : undefined
97+
}
98+
autoFocus
99+
/>
100+
{nameError !== undefined ? (
101+
<p
102+
id="secret-provider-name-error"
103+
role="alert"
104+
className="text-xs text-destructive"
105+
>
106+
{nameError}
107+
</p>
108+
) : null}
109+
</div>
110+
<div className="grid gap-1.5">
111+
<Label htmlFor="secret-provider-account">Account</Label>
112+
<Input
113+
id="secret-provider-account"
114+
placeholder="my.1password.com"
115+
value={draft.account}
116+
onChange={(event) => update({ account: event.target.value })}
117+
maxLength={253}
118+
spellCheck={false}
119+
aria-invalid={accountError !== undefined}
120+
aria-describedby={
121+
accountError !== undefined ? "secret-provider-account-error" : undefined
122+
}
123+
/>
124+
{accountError !== undefined ? (
125+
<p
126+
id="secret-provider-account-error"
127+
role="alert"
128+
className="text-xs text-destructive"
129+
>
130+
{accountError}
131+
</p>
132+
) : null}
133+
<p className="text-xs text-muted-foreground">
134+
The account shorthand, sign-in address without https://, or ID that op account list
135+
shows.
136+
</p>
137+
</div>
138+
</form>
139+
</DialogPanel>
140+
<DialogFooter variant="bare">
141+
<Button variant="outline" onClick={onClose}>
142+
Cancel
143+
</Button>
144+
<Button
145+
type="submit"
146+
form="secret-provider-form"
147+
disabled={submitted && Result.isFailure(result)}
148+
>
149+
{existing ? "Save changes" : "Add provider"}
150+
</Button>
151+
</DialogFooter>
152+
</DialogPopup>
153+
</Dialog>
154+
);
155+
}

0 commit comments

Comments
 (0)