iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / components / secrets-page.tsxmain at d68d7e1

// THE SECRETS PAGE OF A DASH: the mind's secrets as `itx.secrets.list()` shows them (each one's
// path, the origins it may be sent to, its refresh strategy's kind, when it was set and its public
// fields, never a value), with an update and a delete per row, and a sheet that sets one: a name
// (the path `/secrets/<name>` that `getSecret("/secrets/<name>")` spells), a value (a string, or
// a JSON object whose fields the placeholder's `{ field }` picks) and the origins the value may be
// sent to. Update is the same sheet on an existing row: the name locked, the pin filled in, the
// value pasted again. The current value is never shown, and a pin only ever enters together with
// the value it guards.
//
// It knows no router. The page gives it the list and reads it again when asked (`onChanged`), and
// holds the sheet's state (`state`, `onStateChange`), so that the sheet is a link:
// `{ new: 1 }` opens New secret, `{ update: "<name>" }` that row's Update, and
// `{ new: 1, name, urls }` opens New secret with the name and the origins filled, so a person types
// only the value. The value never comes from the state.
//
//   <SecretsPage itx={itx} secrets={secrets} state={state} onStateChange={setState} onChanged={reload} />
import { useRef, useState, type FormEvent, type RefObject } from "react";
import { Plus } from "lucide-react";
import { z } from "zod";
import type { AuthenticatedApp } from "iterate/app";
import type { SecretCatalogEntry } from "iterate/api";
import { SECRET_NAME, SECRETS_PREFIX, secretMaterialOf } from "../lib/secrets.ts";
import { Identifier } from "#/components/identifier.tsx";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "#/components/ui/alert-dialog.tsx";
import { Button } from "#/components/ui/button.tsx";
import { Field, FieldDescription, FieldGroup, FieldLabel } from "#/components/ui/field.tsx";
import { Input } from "#/components/ui/input.tsx";
import {
  Sheet,
  SheetClose,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetTitle,
} from "#/components/ui/sheet.tsx";
import { Spinner } from "#/components/ui/spinner.tsx";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "#/components/ui/table.tsx";
import { Textarea } from "#/components/ui/textarea.tsx";

/** The sheet's state, a URL's search in an app with a router: `name` and `urls` fill New secret,
 *  checked as the form checks them. A key with a wrong value is an absent key. */
export const SecretsPageState = z.object({
  new: z.literal(1).optional().catch(undefined),
  update: z.string().optional().catch(undefined),
  name: z.string().regex(SECRET_NAME).optional().catch(undefined),
  urls: z
    .string()
    .refine((urls) => {
      const origins = originsOf(urls);
      return origins.length > 0 && origins.every((origin) => URL.canParse(origin));
    })
    .optional()
    .catch(undefined),
});
export type SecretsPageState = z.infer<typeof SecretsPageState>;

/** The sheet's state from anything a URL can hold (a page with no router keeps it as JSON in one
 *  search key). */
export function secretsPageStateOf(value: unknown): SecretsPageState {
  return SecretsPageState.safeParse(value).data || {};
}

export function SecretsPage({
  itx,
  secrets,
  state,
  onStateChange,
  onChanged,
}: {
  /** The mind's root, as the signed-in member: the page calls `secrets.set` and `secrets.delete`. */
  itx: AuthenticatedApp["itx"];
  /** `itx.secrets.list()`, read by the page. */
  secrets: SecretCatalogEntry[];
  state: SecretsPageState;
  /** The whole new state. `replace`: the sheet closed, so Back does not open it again. */
  onStateChange(state: SecretsPageState, replace?: boolean): void;
  /** A secret was set or deleted: read the list again. The sheet closes when this settles. */
  onChanged(): Promise<unknown>;
}) {
  const [pending, setPending] = useState(false);
  // where the sheet lands focus: the value on an update or a filled name, the name otherwise
  const valueField = useRef<HTMLTextAreaElement>(null);
  const [deleting, setDeleting] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [status, setStatus] = useState<string | null>(null);
  const closeSheet = () => onStateChange({}, true);
  /** The row `?update=<name>` names — none when the name is not (or no longer) a secret. */
  const updating =
    (state.update && secrets.find((secret) => secret.path === SECRETS_PREFIX + state.update)) ||
    null;

  const deleteSecret = async (secretPath: string) => {
    setError(null);
    setStatus(null);
    setDeleting(secretPath);
    try {
      await itx.secrets.delete(secretPath);
      setStatus(`${secretPath} is deleted.`);
      await onChanged();
    } catch (caught) {
      setError(caught instanceof Error ? caught.message : String(caught));
    } finally {
      setDeleting(null);
    }
  };

  return (
    <div className="mx-auto flex w-full max-w-5xl flex-col gap-8 p-4 md:p-8">
      <div className="flex flex-col gap-1">
        <div className="flex items-center justify-between gap-4">
          <h1 className="text-2xl font-semibold tracking-tight">Secrets</h1>
          <Button onClick={() => onStateChange({ new: 1 })}>
            <Plus data-icon="inline-start" />
            New secret
          </Button>
        </div>
        <p className="text-sm text-muted-foreground">
          A secret is its path. In a worker or a script, put{" "}
          <code>getSecret("/secrets/&lt;name&gt;")</code> in an outbound request's URL or headers:
          the platform substitutes the value as the request leaves, and only towards the origins
          pinned here. A value never comes back out.
        </p>
      </div>
      {error && (
        <p role="alert" data-type="error" className="text-sm text-destructive">
          {error}
        </p>
      )}
      {status && (
        <p role="status" className="text-sm text-muted-foreground">
          {status}
        </p>
      )}
      {secrets.length === 0 ? (
        <p className="text-sm text-muted-foreground">No secrets yet.</p>
      ) : (
        <div className="overflow-x-auto rounded-lg border">
          <Table data-testid="secrets">
            <TableHeader>
              <TableRow>
                <TableHead>Path</TableHead>
                <TableHead>Sent to</TableHead>
                <TableHead>Refresh</TableHead>
                <TableHead>Set</TableHead>
                <TableHead>Actions</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {secrets.map((secret) => (
                <TableRow key={secret.path}>
                  <TableCell>
                    <Identifier value={secret.path} />
                    {secret.public ? (
                      <ul className="mt-1 flex flex-col gap-0.5 font-mono text-xs text-muted-foreground">
                        {Object.entries(secret.public).map(([name, value]) => (
                          <li key={name} className="wrap-anywhere">
                            {name}={value}
                          </li>
                        ))}
                      </ul>
                    ) : null}
                  </TableCell>
                  <TableCell>
                    <ul className="flex flex-col gap-0.5 font-mono text-xs">
                      {secret.urls.map((url) => (
                        <li key={url}>{url}</li>
                      ))}
                    </ul>
                  </TableCell>
                  <TableCell className="text-muted-foreground">
                    {secret.refresh === "worker" && secret.refreshSourceSha256
                      ? `refreshed by code (${secret.refreshSourceSha256.slice(0, 12)})`
                      : secret.refresh || "—"}
                  </TableCell>
                  <TableCell className="text-muted-foreground">
                    {/* a stack's vault lists whether a secret is set, and no time */}
                    {Number.isNaN(Date.parse(secret.createdAt))
                      ? `${"set" in secret && secret.set === false ? "not " : ""}set`
                      : new Date(secret.createdAt).toISOString()}
                  </TableCell>
                  <TableCell>
                    <div className="flex gap-2">
                      <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        aria-label={`Update ${secret.path}`}
                        onClick={() =>
                          onStateChange({ update: secret.path.slice(SECRETS_PREFIX.length) })
                        }
                      >
                        Update
                      </Button>
                      <AlertDialog>
                        <AlertDialogTrigger
                          render={<Button variant="outline" size="sm" />}
                          aria-label={`Delete ${secret.path}`}
                          disabled={deleting === secret.path}
                        >
                          {deleting === secret.path ? "Deleting…" : "Delete"}
                        </AlertDialogTrigger>
                        <AlertDialogContent>
                          <AlertDialogHeader>
                            <AlertDialogTitle>Delete {secret.path}?</AlertDialogTitle>
                            <AlertDialogDescription>
                              The value is forgotten and requests that spell this placeholder fail
                              until it is set again. There is no undo.
                            </AlertDialogDescription>
                          </AlertDialogHeader>
                          <AlertDialogFooter>
                            <AlertDialogCancel>Cancel</AlertDialogCancel>
                            <AlertDialogAction onClick={() => void deleteSecret(secret.path)}>
                              Delete
                            </AlertDialogAction>
                          </AlertDialogFooter>
                        </AlertDialogContent>
                      </AlertDialog>
                    </div>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      )}
      <Sheet
        open={state.new === 1 || Boolean(state.update)}
        onOpenChange={(open) => {
          if (open || pending) return;
          closeSheet();
        }}
      >
        <SheetContent
          side="right"
          showCloseButton={!pending}
          initialFocus={updating || state.name ? valueField : undefined}
          className="overflow-y-auto data-[side=right]:w-full data-[side=right]:sm:max-w-md"
        >
          <SecretForm
            // mounted with the sheet, keyed by what it opens on, so every opening starts from its row
            key={state.update || "new"}
            itx={itx}
            secrets={secrets}
            initialName={state.update || state.name || ""}
            initialUrls={updating ? updating.urls.join(" ") : state.urls || ""}
            updating={updating}
            valueField={valueField}
            pending={pending}
            setPending={setPending}
            onDone={async (message) => {
              setError(null);
              setStatus(message);
              await onChanged();
              closeSheet();
            }}
          />
        </SheetContent>
      </Sheet>
    </div>
  );
}

/** The sheet's body: the name, the value and the pin — one `secrets.set`. On an existing row
 *  (`updating`) the name is locked and the pin pre-filled; the value is always typed here. */
function SecretForm({
  itx,
  secrets,
  initialName,
  initialUrls,
  updating,
  valueField,
  pending,
  setPending,
  onDone,
}: {
  itx: AuthenticatedApp["itx"];
  secrets: SecretCatalogEntry[];
  initialName: string;
  initialUrls: string;
  updating: SecretCatalogEntry | null;
  valueField: RefObject<HTMLTextAreaElement | null>;
  pending: boolean;
  setPending: (pending: boolean) => void;
  /** The secret is written: the line the page shows for it. */
  onDone: (message: string) => Promise<void>;
}) {
  const [name, setName] = useState(initialName);
  const [value, setValue] = useState("");
  const [urls, setUrls] = useState(initialUrls);
  const [error, setError] = useState<string | null>(null);
  const origins = originsOf(urls);
  const path = `${SECRETS_PREFIX}${name.trim()}`;
  const existing = secrets.find((secret) => secret.path === path);

  const setSecret = async (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    setError(null);
    // the platform pins ORIGINS, so each entry must be a whole URL — said here, where it was typed
    const notAUrl = origins.find((origin) => !URL.canParse(origin));
    if (notAUrl) {
      setError(`"${notAUrl}" is not a URL — write the whole origin, like https://api.example.com`);
      return;
    }
    setPending(true);
    try {
      // a row with public fields keeps them public: the new value, a JSON object, carries each
      const publicNames = Object.keys(existing?.public || {});
      await itx.secrets.set(
        path,
        secretMaterialOf(value),
        publicNames.length ? { urls: origins, public: publicNames } : { urls: origins },
      );
      await onDone(
        `${path} is ${existing ? "updated" : "set"}. The value is stored encrypted and is not shown again.`,
      );
    } catch (caught) {
      setError(caught instanceof Error ? caught.message : String(caught));
    } finally {
      setPending(false);
    }
  };

  return (
    <form onSubmit={setSecret} className="flex h-full flex-col">
      <SheetHeader className="border-b">
        <SheetTitle>{updating ? `Update ${updating.path}` : "New secret"}</SheetTitle>
        <SheetDescription>
          {updating
            ? "Paste the value again — the current one is never shown — and check the origins: a pin only ever changes together with the value it guards."
            : "Stored encrypted and never shown again; the log records who set what and when, never the value."}
        </SheetDescription>
      </SheetHeader>
      <FieldGroup className="flex-1 p-4">
        <Field>
          <FieldLabel htmlFor="secret-name">Name</FieldLabel>
          <div className="flex items-center gap-1">
            <span className="font-mono text-sm text-muted-foreground">{SECRETS_PREFIX}</span>
            <Input
              id="secret-name"
              value={name}
              onChange={(event) => setName(event.target.value)}
              pattern={SECRET_NAME.source}
              title="Letters, digits, dots, underscores and dashes"
              placeholder="stripe"
              autoComplete="off"
              spellCheck={false}
              required
              readOnly={Boolean(updating)}
              className="font-mono read-only:bg-muted read-only:text-muted-foreground"
            />
          </div>
          <FieldDescription>
            {updating
              ? "A secret is its path; to move it, set a new name and delete this one."
              : "Letters, digits, dots, underscores and dashes — the path is what the placeholder spells."}
          </FieldDescription>
        </Field>
        <Field>
          <FieldLabel htmlFor="secret-value">{updating ? "New value" : "Value"}</FieldLabel>
          <Textarea
            id="secret-value"
            value={value}
            onChange={(event) => setValue(event.target.value)}
            placeholder="sk_live_…"
            autoComplete="off"
            spellCheck={false}
            required
            ref={valueField}
            rows={4}
            className="font-mono"
          />
          <FieldDescription>
            One string, or a JSON object — then{" "}
            <code>
              getSecret("/secrets/name", {"{"} field: "a.b" {"}"})
            </code>{" "}
            picks one of its fields.
          </FieldDescription>
        </Field>
        <Field>
          <FieldLabel htmlFor="secret-urls">Sent to</FieldLabel>
          <Input
            id="secret-urls"
            value={urls}
            onChange={(event) => setUrls(event.target.value)}
            placeholder="https://api.stripe.com"
            autoComplete="off"
            spellCheck={false}
            required
            className="font-mono"
          />
          <FieldDescription>
            The origins the value may be sent to, separated by spaces or commas. A request to any
            other host is refused, and the value is never sent there.
          </FieldDescription>
        </Field>
        {existing?.refresh && (
          <p role="note" className="text-sm text-muted-foreground">
            This secret refreshes itself ({existing.refresh}). A value set from here has no refresh
            strategy — to keep one, set it from code with <code>refresh</code>.
          </p>
        )}
        {existing?.public && (
          <p role="note" className="text-sm text-muted-foreground">
            This secret has public fields ({Object.keys(existing.public).join(", ")}), which the
            mind reads from its list of secrets. The new value is a JSON object that carries each of
            them, and they stay public.
          </p>
        )}
        {error && (
          <p role="alert" data-type="error" className="text-sm text-destructive">
            {error}
          </p>
        )}
      </FieldGroup>
      <SheetFooter className="border-t sm:flex-row sm:justify-end">
        <SheetClose disabled={pending} render={<Button type="button" variant="outline" />}>
          Cancel
        </SheetClose>
        <Button type="submit" disabled={pending || !name.trim() || !value || origins.length === 0}>
          {pending ? <Spinner data-icon="inline-start" /> : null}
          {existing ? "Update secret" : "Set secret"}
        </Button>
      </SheetFooter>
    </form>
  );
}

/** The origins field's text as its entries: separated by spaces or commas. */
function originsOf(urls: string) {
  return urls.split(/[\s,]+/).filter(Boolean);
}