import { useState } from "react"; import { garage, type Config, type KeyInfo, type KeyListItem } from "./api"; import { fmtDate } from "./format"; import { EyeIcon, KeyIcon, PlusIcon, RefreshIcon, TrashIcon } from "./icons"; import { Confirm, CopyButton, Loading, Modal, Mono, PageHead, Status, useAct, useLoad } from "./ui"; // --- Liste ------------------------------------------------------------------------- export function Keys({ cfg }: { cfg: Config }) { const [creating, setCreating] = useState<"new" | "import" | null>(null); const [created, setCreated] = useState(null); const { data, error, loading, reload } = useLoad(() => garage.get("ListKeys")); return (
{data && (
{data.map((k) => ( (location.hash = `#/keys/${k.id}`)}> ))} {data.length === 0 && ( )}
Nom Identifiant Créée Expiration
e.stopPropagation()}> {k.name || "(sans nom)"} {fmtDate(k.created)} {k.expired ? expirée : k.expiration ? fmtDate(k.expiration) : jamais}
Aucune clé.
)} {creating && ( setCreating(null)} onDone={(k) => { setCreating(null); setCreated(k); reload(); }} /> )} {created && ( setCreated(null)}>

Le secret reste consultable plus tard depuis la page de la clé.

)}
); } function KeyForm({ mode, onClose, onDone }: { mode: "new" | "import"; onClose: () => void; onDone: (k: KeyInfo) => void }) { const act = useAct(); const [name, setName] = useState(""); const [id, setId] = useState(""); const [secret, setSecret] = useState(""); const [exp, setExp] = useState(""); const [createBucket, setCreateBucket] = useState(false); const [busy, setBusy] = useState(false); const valid = mode === "new" || (/^GK[0-9a-f]{24}$/.test(id) && /^[0-9a-f]{64}$/.test(secret)); return (
{ e.preventDefault(); setBusy(true); let k: KeyInfo | undefined; if (await act(async () => { if (mode === "new") { k = await garage.post("CreateKey", undefined, { name: name || null, allow: { createBucket }, ...(exp ? { expiration: new Date(exp).toISOString() } : { neverExpires: true }), }); } else { k = await garage.post("ImportKey", undefined, { name: name || null, accessKeyId: id, secretAccessKey: secret }); } })) onDone(k!); setBusy(false); }} > {mode === "import" && ( <> )} {mode === "new" && ( <> )}
); } /** Identifiant, secret et exemples de configuration client. */ function Secrets({ k, cfg, reveal }: { k: KeyInfo; cfg: Config; reveal?: boolean }) { const [show, setShow] = useState(!!reveal); const secret = k.secretAccessKey || ""; const shown = show ? secret : "•".repeat(32); const endpoint = cfg.s3Endpoint || "https://s3.example.org"; const rclone = `[garage]\ntype = s3\nprovider = Other\naccess_key_id = ${k.accessKeyId}\nsecret_access_key = ${secret}\nendpoint = ${endpoint}\nregion = ${cfg.s3Region}`; const aws = `aws configure set aws_access_key_id ${k.accessKeyId}\naws configure set aws_secret_access_key ${secret}\naws configure set region ${cfg.s3Region}\naws --endpoint-url ${endpoint} s3 ls`; return (
{show && secret && ( <>
rclone
{rclone}
AWS CLI
{aws}
)}
); } // --- Détail ------------------------------------------------------------------------ export function KeyPage({ id, cfg }: { id: string; cfg: Config }) { const act = useAct(); const [reveal, setReveal] = useState(false); const [confirmDelete, setConfirmDelete] = useState(false); const { data: k, error, loading, reload } = useLoad(() => garage.get("GetKeyInfo", { id, showSecretKey: reveal }), [id, reveal]); const [name, setName] = useState(null); if (!k) return (
); const run = (fn: () => Promise, ok?: string) => act(fn, ok).then((r) => r && reload()); return (

Identifiants {k.expired && expirée}

{reveal || k.secretAccessKey ? ( ) : ( <>

Le secret n'est récupéré auprès de Garage que sur demande.

)}

Réglages

{ e.preventDefault(); if (name !== null) run(() => garage.post("UpdateKey", { id: k.accessKeyId }, { name }), "Nom enregistré").then(() => setName(null)); }} >

Créée le {fmtDate(k.created)} · expiration : {k.expiration ? fmtDate(k.expiration) : "jamais"}

Buckets accessibles

{k.buckets.map((b) => ( {(["read", "write", "owner"] as const).map((p) => ( ))} ))} {k.buckets.length === 0 && ( )}
Bucket Lecture Écriture Propriétaire
{b.globalAliases[0] ?? b.localAliases[0] ?? b.id.slice(0, 12)} run(() => garage.post(e.target.checked ? "AllowBucketKey" : "DenyBucketKey", undefined, { bucketId: b.id, accessKeyId: k.accessKeyId, permissions: { [p]: true } })) } aria-label={p} />
Cette clé n'a accès à aucun bucket.
{confirmDelete && ( La clé {k.name || k.accessKeyId} sera supprimée et ses accès aux buckets révoqués. Les applications qui l'utilisent cesseront de fonctionner.

} confirmLabel="Supprimer" onClose={() => setConfirmDelete(false)} onConfirm={async () => { if (await act(() => garage.post("DeleteKey", { id: k.accessKeyId }), "Clé supprimée")) location.hash = "#/keys"; else setConfirmDelete(false); }} /> )}
); }