Skip to content

Commit ba55117

Browse files
committed
feat(settings): add and list 1Password secret providers
Signed-off-by: Yordis Prieto <[email protected]>
1 parent ee144de commit ba55117

15 files changed

Lines changed: 698 additions & 10 deletions

‎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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { OnePasswordAccount, SecretProviderId } from "@t3tools/contracts";
2+
import * as Result from "effect/Result";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import {
6+
EMPTY_SECRET_PROVIDER_DRAFT,
7+
secretProviderDraftFromConfig,
8+
secretProviderFromDraft,
9+
secretProviderIdFromName,
10+
type SecretProviderTarget,
11+
} from "./SecretProviderDialog.logic";
12+
13+
const NO_IDS: ReadonlySet<string> = new Set();
14+
const ADD: SecretProviderTarget = { mode: "add" };
15+
16+
describe("secretProviderIdFromName", () => {
17+
it("slugs the name so the id stays readable in settings.json", () => {
18+
expect(secretProviderIdFromName(" Work 1Password! ")).toBe("work-1password");
19+
});
20+
21+
it("has no id for a name without letters or digits", () => {
22+
expect(secretProviderIdFromName(" -- ")).toBeNull();
23+
});
24+
});
25+
26+
describe("secretProviderFromDraft", () => {
27+
it("builds a 1Password provider", () => {
28+
const result = secretProviderFromDraft(
29+
{ name: "Personal", account: "my.1password.com" },
30+
NO_IDS,
31+
ADD,
32+
);
33+
expect(result).toEqual(
34+
Result.succeed({
35+
id: "personal",
36+
config: { kind: "1password", label: "Personal", account: "my.1password.com" },
37+
}),
38+
);
39+
});
40+
41+
it("rejects an empty name and a name that is already taken", () => {
42+
expect(
43+
secretProviderFromDraft({ ...EMPTY_SECRET_PROVIDER_DRAFT, name: " " }, NO_IDS, ADD),
44+
).toEqual(Result.fail({ name: "Enter a name.", account: "Enter the 1Password account." }));
45+
expect(
46+
secretProviderFromDraft({ name: "Work Vault", account: "my" }, new Set(["work-vault"]), ADD),
47+
).toEqual(Result.fail({ name: "A secret provider named Work Vault exists." }));
48+
});
49+
50+
it("rejects a name without letters or digits", () => {
51+
expect(secretProviderFromDraft({ name: "--", account: "my" }, NO_IDS, ADD)).toEqual(
52+
Result.fail({ name: "The name needs at least one letter or digit." }),
53+
);
54+
});
55+
56+
it("requires a 1Password account", () => {
57+
expect(
58+
secretProviderFromDraft({ ...EMPTY_SECRET_PROVIDER_DRAFT, name: "Ops" }, NO_IDS, ADD),
59+
).toEqual(Result.fail({ account: "Enter the 1Password account." }));
60+
});
61+
62+
it("rejects an account the op CLI could read as a flag", () => {
63+
const result = secretProviderFromDraft({ name: "Ops", account: "--help" }, NO_IDS, ADD);
64+
expect(Result.isFailure(result) && result.failure.account).toBeTruthy();
65+
});
66+
67+
it("keeps the id and accepts its own name when editing", () => {
68+
const id = SecretProviderId.make("personal");
69+
const result = secretProviderFromDraft(
70+
{ name: "Personal", account: "team.1password.com" },
71+
new Set(["personal"]),
72+
{ mode: "edit", id },
73+
);
74+
expect(Result.isSuccess(result) && result.success.id).toBe(id);
75+
});
76+
});
77+
78+
describe("secretProviderDraftFromConfig", () => {
79+
it("fills a 1Password draft", () => {
80+
expect(
81+
secretProviderDraftFromConfig({
82+
kind: "1password",
83+
label: "Personal",
84+
account: OnePasswordAccount.make("my.1password.com"),
85+
}),
86+
).toEqual({ name: "Personal", account: "my.1password.com" });
87+
});
88+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
OnePasswordAccount,
3+
SecretProviderId,
4+
SecretProviderLabel,
5+
type SecretProviderConfig,
6+
} from "@t3tools/contracts";
7+
import * as Result from "effect/Result";
8+
import * as Schema from "effect/Schema";
9+
10+
/** One line naming the kind and where it reads from, for rows and pickers. */
11+
export function secretProviderSummary(config: SecretProviderConfig): string {
12+
return `1Password · ${config.account}`;
13+
}
14+
15+
/** Stable and readable in settings.json, so re-adding a provider under the same name restores its variables. */
16+
export function secretProviderIdFromName(name: string): SecretProviderId | null {
17+
const slug = name
18+
.trim()
19+
.toLowerCase()
20+
.replace(/[^a-z0-9]+/g, "-")
21+
.replace(/^-+|-+$/g, "");
22+
return slug.length === 0 ? null : SecretProviderId.make(slug);
23+
}
24+
25+
export interface SecretProviderDraft {
26+
readonly name: string;
27+
readonly account: string;
28+
}
29+
30+
export const EMPTY_SECRET_PROVIDER_DRAFT: SecretProviderDraft = { name: "", account: "" };
31+
32+
export function secretProviderDraftFromConfig(config: SecretProviderConfig): SecretProviderDraft {
33+
return { name: config.label, account: config.account };
34+
}
35+
36+
export type SecretProviderTarget =
37+
| { readonly mode: "add" }
38+
| { readonly mode: "edit"; readonly id: SecretProviderId };
39+
40+
function decodeField<S extends Schema.ConstraintDecoder<unknown>>(
41+
schema: S,
42+
value: string,
43+
emptyMessage: string,
44+
): Result.Result<S["Type"], string> {
45+
if (value.trim().length === 0) return Result.fail(emptyMessage);
46+
const decoded = Schema.decodeResult(schema)(value.trim());
47+
return Result.isFailure(decoded)
48+
? Result.fail(decoded.failure.message)
49+
: Result.succeed(decoded.success);
50+
}
51+
52+
export type SecretProviderDraftField = "name" | "account";
53+
54+
export type SecretProviderDraftIssues = Partial<Readonly<Record<SecretProviderDraftField, string>>>;
55+
56+
/** The settings entry a draft adds or replaces, or every field that blocks it, one message each. */
57+
export function secretProviderFromDraft(
58+
draft: SecretProviderDraft,
59+
existingIds: ReadonlySet<string>,
60+
target: SecretProviderTarget,
61+
): Result.Result<
62+
{ readonly id: SecretProviderId; readonly config: SecretProviderConfig },
63+
SecretProviderDraftIssues
64+
> {
65+
const issues: { -readonly [Field in SecretProviderDraftField]?: string } = {};
66+
67+
const label = decodeField(SecretProviderLabel, draft.name, "Enter a name.");
68+
const id =
69+
target.mode === "edit"
70+
? target.id
71+
: Result.isSuccess(label)
72+
? secretProviderIdFromName(label.success)
73+
: null;
74+
if (Result.isFailure(label)) {
75+
issues.name = label.failure;
76+
} else if (id === null) {
77+
issues.name = "The name needs at least one letter or digit.";
78+
} else if (target.mode === "add" && existingIds.has(id)) {
79+
issues.name = `A secret provider named ${label.success} exists.`;
80+
}
81+
82+
const account = decodeField(OnePasswordAccount, draft.account, "Enter the 1Password account.");
83+
if (Result.isFailure(account)) issues.account = account.failure;
84+
85+
if (
86+
issues.name !== undefined ||
87+
Result.isFailure(label) ||
88+
id === null ||
89+
Result.isFailure(account)
90+
) {
91+
return Result.fail(issues);
92+
}
93+
return Result.succeed({
94+
id,
95+
config: { kind: "1password", label: label.success, account: account.success },
96+
});
97+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
EMPTY_SECRET_PROVIDER_DRAFT,
20+
secretProviderDraftFromConfig,
21+
secretProviderFromDraft,
22+
type SecretProviderDraft,
23+
type SecretProviderDraftField,
24+
} from "./SecretProviderDialog.logic";
25+
26+
/**
27+
* Adds or edits a named secret provider on one environment. Provider variables on that
28+
* environment pick it by name; values are read on the server when a provider starts.
29+
*/
30+
export function SecretProviderDialog({
31+
open,
32+
onOpenChange,
33+
environmentId,
34+
environmentLabel,
35+
existingIds,
36+
existing,
37+
}: {
38+
readonly open: boolean;
39+
readonly onOpenChange: (open: boolean) => void;
40+
readonly environmentId: EnvironmentId;
41+
readonly environmentLabel: string;
42+
readonly existingIds: ReadonlySet<string>;
43+
readonly existing?: { readonly id: SecretProviderId; readonly config: SecretProviderConfig };
44+
}) {
45+
const updateSettings = useUpdateEnvironmentSettings(environmentId);
46+
const [draft, setDraft] = useState<SecretProviderDraft>(
47+
existing ? secretProviderDraftFromConfig(existing.config) : EMPTY_SECRET_PROVIDER_DRAFT,
48+
);
49+
const [submitted, setSubmitted] = useState(false);
50+
const result = secretProviderFromDraft(
51+
draft,
52+
existingIds,
53+
existing ? { mode: "edit", id: existing.id } : { mode: "add" },
54+
);
55+
const issues = Result.isFailure(result) ? result.failure : {};
56+
const fieldError = (field: SecretProviderDraftField) => (submitted ? issues[field] : undefined);
57+
const nameError = fieldError("name");
58+
const accountError = fieldError("account");
59+
60+
const update = (patch: Partial<SecretProviderDraft>) => setDraft({ ...draft, ...patch });
61+
const close = () => {
62+
setDraft(EMPTY_SECRET_PROVIDER_DRAFT);
63+
setSubmitted(false);
64+
onOpenChange(false);
65+
};
66+
const save = () => {
67+
setSubmitted(true);
68+
if (Result.isFailure(result)) return;
69+
updateSettings({ secretProviders: { [result.success.id]: result.success.config } });
70+
close();
71+
};
72+
73+
return (
74+
<Dialog
75+
open={open}
76+
onOpenChange={(next) => {
77+
if (!next) close();
78+
else onOpenChange(next);
79+
}}
80+
>
81+
<DialogPopup className="max-w-md">
82+
<DialogHeader>
83+
<DialogTitle>
84+
{existing ? `Edit ${existing.config.label}` : "Add a 1Password provider"}
85+
</DialogTitle>
86+
<DialogDescription>
87+
Provider variables on {environmentLabel} can read values from it by name. Values are
88+
read with the op CLI on that server when a provider starts.
89+
</DialogDescription>
90+
</DialogHeader>
91+
<DialogPanel>
92+
<form
93+
className="grid gap-4"
94+
onSubmit={(event) => {
95+
event.preventDefault();
96+
save();
97+
}}
98+
>
99+
<div className="grid gap-1.5">
100+
<Label htmlFor="secret-provider-name">Name</Label>
101+
<Input
102+
id="secret-provider-name"
103+
placeholder="Personal"
104+
value={draft.name}
105+
onChange={(event) => update({ name: event.target.value })}
106+
readOnly={existing !== undefined}
107+
aria-invalid={nameError !== undefined}
108+
aria-describedby={
109+
nameError !== undefined ? "secret-provider-name-error" : undefined
110+
}
111+
autoFocus
112+
/>
113+
{nameError !== undefined ? (
114+
<p
115+
id="secret-provider-name-error"
116+
role="alert"
117+
className="text-xs text-destructive"
118+
>
119+
{nameError}
120+
</p>
121+
) : null}
122+
{existing ? (
123+
<p className="text-xs text-muted-foreground">
124+
Variables pick this provider by name, so it cannot change.
125+
</p>
126+
) : null}
127+
</div>
128+
<div className="grid gap-1.5">
129+
<Label htmlFor="secret-provider-account">Account</Label>
130+
<Input
131+
id="secret-provider-account"
132+
placeholder="my.1password.com"
133+
value={draft.account}
134+
onChange={(event) => update({ account: event.target.value })}
135+
spellCheck={false}
136+
aria-invalid={accountError !== undefined}
137+
aria-describedby={
138+
accountError !== undefined ? "secret-provider-account-error" : undefined
139+
}
140+
/>
141+
{accountError !== undefined ? (
142+
<p
143+
id="secret-provider-account-error"
144+
role="alert"
145+
className="text-xs text-destructive"
146+
>
147+
{accountError}
148+
</p>
149+
) : null}
150+
<p className="text-xs text-muted-foreground">
151+
The account shorthand, sign-in address, or ID that op account list shows.
152+
</p>
153+
</div>
154+
</form>
155+
</DialogPanel>
156+
<DialogFooter variant="bare">
157+
<Button variant="outline" onClick={close}>
158+
Cancel
159+
</Button>
160+
<Button onClick={save} disabled={submitted && Result.isFailure(result)}>
161+
{existing ? "Save changes" : "Add provider"}
162+
</Button>
163+
</DialogFooter>
164+
</DialogPopup>
165+
</Dialog>
166+
);
167+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { SecretProviderKind } from "@t3tools/contracts";
2+
import type { SVGProps } from "react";
3+
4+
import { OnePasswordIcon } from "../Icons";
5+
6+
export function SecretProviderKindIcon({
7+
kind,
8+
...props
9+
}: SVGProps<SVGSVGElement> & { readonly kind: SecretProviderKind }) {
10+
switch (kind) {
11+
case "1password":
12+
return <OnePasswordIcon {...props} />;
13+
}
14+
}

0 commit comments

Comments
 (0)