@coffre/ui 0.5.1 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/dist/chunks/{affordances-Bqdevf7w.js → affordances-Dtyha68j.js} +11 -3
  2. package/dist/chunks/affordances-Dtyha68j.js.map +1 -0
  3. package/dist/chunks/{audit-sentence-Cm5nby5k.js → audit-sentence-CUUMshIp.js} +22 -6
  4. package/dist/chunks/audit-sentence-CUUMshIp.js.map +1 -0
  5. package/dist/chunks/{connected-apps-DTNS7YmY.js → connected-apps-DTpatjge.js} +5 -5
  6. package/dist/chunks/{connected-apps-DTNS7YmY.js.map → connected-apps-DTpatjge.js.map} +1 -1
  7. package/dist/chunks/{delete-place-CIaOq0mt.js → delete-place-SOOYVt6A.js} +5 -5
  8. package/dist/chunks/{delete-place-CIaOq0mt.js.map → delete-place-SOOYVt6A.js.map} +1 -1
  9. package/dist/chunks/{directory-DSVt5Tya.js → directory-DrtdKny2.js} +60 -37
  10. package/dist/chunks/directory-DrtdKny2.js.map +1 -0
  11. package/dist/chunks/{folders-CoGK9oqg.js → folders-hR2Lnk0k.js} +3 -3
  12. package/dist/chunks/{folders-CoGK9oqg.js.map → folders-hR2Lnk0k.js.map} +1 -1
  13. package/dist/chunks/{grants-CRU7yZhL.js → grants-DDBui9YD.js} +6 -6
  14. package/dist/chunks/{grants-CRU7yZhL.js.map → grants-DDBui9YD.js.map} +1 -1
  15. package/dist/chunks/{offboarding-DVBSiJRZ.js → offboarding-C7ECW5o0.js} +5 -5
  16. package/dist/chunks/{offboarding-DVBSiJRZ.js.map → offboarding-C7ECW5o0.js.map} +1 -1
  17. package/dist/chunks/{page-DvsgZ2Hd.js → page-JTORvh-N.js} +7 -3
  18. package/dist/chunks/page-JTORvh-N.js.map +1 -0
  19. package/dist/chunks/{principal-BIN-xwZw.js → principal-DFBswTBr.js} +2 -2
  20. package/dist/chunks/{principal-BIN-xwZw.js.map → principal-DFBswTBr.js.map} +1 -1
  21. package/dist/chunks/{principal-page-BEBr_zW7.js → principal-page-CB6dAABC.js} +17 -19
  22. package/dist/chunks/principal-page-CB6dAABC.js.map +1 -0
  23. package/dist/chunks/{project-page-B3TZ5_ig.js → project-page-BHNUvr2L.js} +3 -3
  24. package/dist/chunks/{project-page-B3TZ5_ig.js.map → project-page-BHNUvr2L.js.map} +1 -1
  25. package/dist/chunks/{queries-DE5nK2y4.js → queries-DgQo3mC-.js} +27 -12
  26. package/dist/chunks/queries-DgQo3mC-.js.map +1 -0
  27. package/dist/chunks/{references-uv4Mli9z.js → references-BQc35Q9g.js} +5 -5
  28. package/dist/chunks/{references-uv4Mli9z.js.map → references-BQc35Q9g.js.map} +1 -1
  29. package/dist/chunks/{role-field-DWhkRY6n.js → role-field-BfWRCqzl.js} +14 -7
  30. package/dist/chunks/role-field-BfWRCqzl.js.map +1 -0
  31. package/dist/chunks/{row-state-DOPUsjQK.js → row-state-Zf3kZVhd.js} +13 -5
  32. package/dist/chunks/row-state-Zf3kZVhd.js.map +1 -0
  33. package/dist/chunks/{signin-errors--ykMFuqN.js → signin-errors-TlQmQe0t.js} +2 -2
  34. package/dist/chunks/signin-errors-TlQmQe0t.js.map +1 -0
  35. package/dist/chunks/{theme-DJuz-xLM.js → theme-B6-V9JvY.js} +3 -3
  36. package/dist/chunks/{theme-DJuz-xLM.js.map → theme-B6-V9JvY.js.map} +1 -1
  37. package/dist/chunks/{ui-DJje3gST.js → ui-Br-dNUTc.js} +2 -2
  38. package/dist/chunks/{ui-DJje3gST.js.map → ui-Br-dNUTc.js.map} +1 -1
  39. package/dist/chunks/{use-action-BRgvbUWt.js → use-action-DthL5hL8.js} +3 -3
  40. package/dist/chunks/{use-action-BRgvbUWt.js.map → use-action-DthL5hL8.js.map} +1 -1
  41. package/dist/chunks/{use-change-BJ3dCFHb.js → use-change-By4cG8bT.js} +31 -19
  42. package/dist/chunks/use-change-By4cG8bT.js.map +1 -0
  43. package/dist/chunks/{use-shell-BO4ZTr1y.js → use-shell-D6kjSPls.js} +3 -3
  44. package/dist/chunks/{use-shell-BO4ZTr1y.js.map → use-shell-D6kjSPls.js.map} +1 -1
  45. package/dist/index.d.ts +6 -7
  46. package/dist/index.js +8 -9
  47. package/dist/index.js.map +1 -1
  48. package/dist/pages/account.js +12 -13
  49. package/dist/pages/account.js.map +1 -1
  50. package/dist/pages/approval.js +2 -2
  51. package/dist/pages/audit.js +5 -5
  52. package/dist/pages/audit.js.map +1 -1
  53. package/dist/pages/device-login.js +2 -2
  54. package/dist/pages/environment.js +837 -155
  55. package/dist/pages/environment.js.map +1 -1
  56. package/dist/pages/login.js +4 -4
  57. package/dist/pages/oauth-authorize.js +2 -2
  58. package/dist/pages/project-access.js +160 -80
  59. package/dist/pages/project-access.js.map +1 -1
  60. package/dist/pages/project-environments.js +10 -10
  61. package/dist/pages/project-settings.js +8 -8
  62. package/dist/pages/project.js +3 -3
  63. package/dist/pages/projects.js +10 -10
  64. package/dist/pages/projects.js.map +1 -1
  65. package/dist/pages/service-account.js +537 -109
  66. package/dist/pages/service-account.js.map +1 -1
  67. package/dist/pages/service-accounts.js +5 -5
  68. package/dist/pages/settings.js +8 -18
  69. package/dist/pages/settings.js.map +1 -1
  70. package/dist/pages/unregistered.js +3 -3
  71. package/dist/pages/unregistered.js.map +1 -1
  72. package/dist/pages/user.js +6 -6
  73. package/dist/pages/users.js +5 -5
  74. package/package.json +3 -3
  75. package/src/components/affordances.ts +13 -1
  76. package/src/components/directory.tsx +50 -36
  77. package/src/components/generate-value.tsx +362 -0
  78. package/src/components/github-picker.tsx +83 -0
  79. package/src/components/icons.tsx +4 -0
  80. package/src/components/masked.tsx +60 -0
  81. package/src/components/missing.tsx +3 -3
  82. package/src/components/multi-combobox.tsx +94 -0
  83. package/src/components/principal-page.tsx +8 -12
  84. package/src/components/principal-picker.tsx +68 -18
  85. package/src/components/role-field.tsx +7 -5
  86. package/src/components/row-state.tsx +7 -3
  87. package/src/components/shell.tsx +0 -1
  88. package/src/components/trusted-workloads.tsx +157 -62
  89. package/src/lib/audit-sentences.ts +25 -3
  90. package/src/lib/bindings.ts +125 -14
  91. package/src/lib/capabilities.ts +15 -9
  92. package/src/lib/changes.ts +15 -9
  93. package/src/lib/coffre.ts +2 -0
  94. package/src/lib/generate.ts +160 -0
  95. package/src/lib/queries.ts +19 -5
  96. package/src/lib/secret-edit-batch.ts +38 -5
  97. package/src/lib/secret-order.ts +23 -0
  98. package/src/lib/shortcuts.ts +9 -0
  99. package/src/lib/signin-errors.ts +1 -1
  100. package/src/lib/use-github-lookup.ts +70 -0
  101. package/src/pages/account.tsx +1 -2
  102. package/src/pages/audit.tsx +1 -1
  103. package/src/pages/environment.tsx +241 -119
  104. package/src/pages/project-access.tsx +19 -8
  105. package/src/pages/projects.tsx +3 -3
  106. package/src/pages/settings.tsx +0 -10
  107. package/src/pages/unregistered.tsx +1 -1
  108. package/src/parity.ts +3 -1
  109. package/src/shared/models.ts +2 -2
  110. package/src/styles/globals.css +303 -19
  111. package/dist/chunks/affordances-Bqdevf7w.js.map +0 -1
  112. package/dist/chunks/audit-sentence-Cm5nby5k.js.map +0 -1
  113. package/dist/chunks/directory-DSVt5Tya.js.map +0 -1
  114. package/dist/chunks/page-DvsgZ2Hd.js.map +0 -1
  115. package/dist/chunks/principal-page-BEBr_zW7.js.map +0 -1
  116. package/dist/chunks/queries-DE5nK2y4.js.map +0 -1
  117. package/dist/chunks/role-field-DWhkRY6n.js.map +0 -1
  118. package/dist/chunks/row-state-DOPUsjQK.js.map +0 -1
  119. package/dist/chunks/signin-errors--ykMFuqN.js.map +0 -1
  120. package/dist/chunks/use-change-BJ3dCFHb.js.map +0 -1
@@ -1,12 +1,6 @@
1
- import { byFolder, CoffreError, foldersOf, planImport, type CoffreClient, type ListedReference } from '@coffre/client';
1
+ import { ageInDays, byFolder, CoffreError, foldersOf, olderThan, planImport, shownMember, type CoffreClient, type ListedReference } from '@coffre/client';
2
2
  import { parseDotenv } from '@coffre/core/dotenv';
3
- import {
4
- useEffect,
5
- useRef,
6
- useState,
7
- type InputHTMLAttributes,
8
- type TextareaHTMLAttributes,
9
- } from 'react';
3
+ import { useEffect, useRef, useState, type ReactNode } from 'react';
10
4
  import { useQuery, useSuspenseQuery } from '@tanstack/react-query';
11
5
  import { Link } from '@tanstack/react-router';
12
6
  import { useShell } from '../lib/use-shell';
@@ -28,12 +22,16 @@ import type {
28
22
  } from '../shared/models';
29
23
  import { canRevealSecrets } from '../lib/capabilities';
30
24
  import {
25
+ companionsOf,
31
26
  hasSecretEditConflict,
32
27
  secretChangeFor,
28
+ type Companion,
33
29
  type SecretChange,
34
30
  type SecretDraft,
35
31
  } from '../lib/secret-edit-batch';
36
32
  import { revealIsCurrent, type Reveal } from '../lib/reveal';
33
+ import { sortKeys, type Sort } from '../lib/secret-order';
34
+ import { inDialog } from '../lib/shortcuts';
37
35
  import { secretKeyProblem } from '../lib/validation';
38
36
  import {
39
37
  breakAfterUnderscores,
@@ -51,6 +49,8 @@ import {
51
49
  } from '../components/ui';
52
50
  import { Card, ClosedDoor, PageHeader } from '../components/page';
53
51
  import { SecretReadOnly } from '../components/affordances';
52
+ import { GenerateValue, type GeneratedUse } from '../components/generate-value';
53
+ import { MaskedInput, MaskedTextarea } from '../components/masked';
54
54
  import { FolderMenu, FolderRow, MoveToFolder } from '../components/folders';
55
55
  import { MissingKeys } from '../components/missing';
56
56
  import { ArchiveBlocked, archiveBlockers, LentReferences, MakeReference, ReferenceValue } from '../components/references';
@@ -62,8 +62,11 @@ import {
62
62
  Clock,
63
63
  Eye,
64
64
  ArrowRight,
65
+ ChevronDown,
66
+ ChevronsUpDown,
65
67
  EyeOff,
66
68
  Folder,
69
+ Generate,
67
70
  Hash,
68
71
  History,
69
72
  Key,
@@ -127,6 +130,7 @@ export function EnvironmentPage() {
127
130
  environment={environment}
128
131
  permissions={result.permissions}
129
132
  keys={result.keys}
133
+ rotateAfterDays={result.rotateAfterDays}
130
134
  />
131
135
  );
132
136
  }
@@ -138,11 +142,14 @@ function EnvironmentLedger({
138
142
  environment,
139
143
  permissions,
140
144
  keys,
145
+ rotateAfterDays,
141
146
  }: {
142
147
  project: string;
143
148
  environment: string;
144
149
  permissions: Permission[];
145
150
  keys: SecretKey[];
151
+ /** Days a value may go unchanged before it is flagged for rotation; null for no flag. */
152
+ rotateAfterDays: number | null;
146
153
  }) {
147
154
  const coffre = useCoffre();
148
155
  const { principal } = useShell();
@@ -169,6 +176,9 @@ function EnvironmentLedger({
169
176
  const [editing, setEditing] = useState<ReadonlySet<string>>(() => new Set());
170
177
  const [saveError, setSaveError] = useState<string | null>(null);
171
178
  const [query, setQuery] = useState(Route.useSearch().filter ?? '');
179
+ const [sort, setSort] = useState<Sort>({ by: 'key', descending: false });
180
+ // A column's first click sorts it ascending: names A to Z, the oldest values first.
181
+ const sortBy = (by: Sort['by']) => setSort((current) => ({ by, descending: current.by === by && !current.descending }));
172
182
  const nextDraftId = useRef(0);
173
183
 
174
184
  const canWrite = permissions.includes('secret.write');
@@ -202,6 +212,7 @@ function EnvironmentLedger({
202
212
  );
203
213
  const archived = keys.filter((entry) => entry.archived);
204
214
  const existing = new Set(keys.map((entry) => entry.key));
215
+ const versionOf = (key: string) => keys.find((entry) => entry.key === key.trim())?.version ?? null;
205
216
 
206
217
  const changedEntries = active.filter(
207
218
  (entry) => secretChangeFor(changes, entry.key) !== undefined,
@@ -215,6 +226,7 @@ function EnvironmentLedger({
215
226
  const change = secretChangeFor(changes, entry.key);
216
227
  return change === undefined || change.archived ? [] : [change.key.trim()];
217
228
  }),
229
+ ...companionsOf(active, drafts, changes).map((companion) => companion.key.trim()),
218
230
  ];
219
231
  const unnamed = pendingNames.some((name) => name === '');
220
232
  const invalid = pendingNames.some((name) => name !== '' && secretKeyProblem(name) !== null);
@@ -259,6 +271,7 @@ function EnvironmentLedger({
259
271
  const names = [
260
272
  ...drafts.map((row) => row.key.trim()),
261
273
  ...Object.values(changes).flatMap((change) => (change.archived ? [] : [change.key.trim()])),
274
+ ...companionsOf(active, drafts, changes).map((companion) => companion.key.trim()),
262
275
  ];
263
276
  return (
264
277
  (drafts.length > 0 || Object.keys(changes).length > 0) &&
@@ -292,9 +305,9 @@ function EnvironmentLedger({
292
305
  });
293
306
  }
294
307
 
295
- function addDraft() {
308
+ function addDraft(key = '') {
296
309
  // New rows go last, by the button that made them, in the order they were added.
297
- setDrafts((rows) => [...rows, { id: nextDraftId.current++, key: '', value: '' }]);
310
+ setDrafts((rows) => [...rows, { id: nextDraftId.current++, key, value: '' }]);
298
311
  }
299
312
 
300
313
  function discardAll() {
@@ -351,13 +364,14 @@ function EnvironmentLedger({
351
364
  );
352
365
  }
353
366
 
354
- // Cmd/Ctrl+Enter saves from anywhere on the page while edits are pending.
367
+ // Cmd/Ctrl+Enter saves from anywhere on the page while edits are pending,
368
+ // but for an open dialog, such as Generate's.
355
369
  const saveRef = useRef(saveChanges);
356
370
  saveRef.current = saveChanges;
357
371
  useEffect(() => {
358
372
  if (pendingCount === 0) return;
359
373
  function onKeyDown(event: KeyboardEvent) {
360
- if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
374
+ if (event.key === 'Enter' && (event.metaKey || event.ctrlKey) && !inDialog(event.target)) {
361
375
  event.preventDefault();
362
376
  saveRef.current();
363
377
  }
@@ -366,20 +380,22 @@ function EnvironmentLedger({
366
380
  return () => document.removeEventListener('keydown', onKeyDown);
367
381
  }, [pendingCount]);
368
382
 
383
+ const withCompanion = (what: string, companion: Companion | undefined) =>
384
+ companion === undefined ? what : `${what}, with ${companion.key.trim()}`;
369
385
  const pending: Pending[] = [
370
386
  ...drafts.map((draft) => ({
371
387
  key: draft.key.trim() || 'unnamed',
372
- what: existing.has(draft.key.trim()) ? 'new version' : 'added',
388
+ what: withCompanion(existing.has(draft.key.trim()) ? 'new version' : 'added', draft.companion),
373
389
  })),
374
390
  ...changedEntries.map((entry) => {
375
391
  const change = secretChangeFor(changes, entry.key)!;
376
392
  if (change.archived) return { key: entry.key, what: 'archived' };
377
393
  const renamed = change.key.trim() !== entry.key;
378
394
  if (renamed && change.value !== null) {
379
- return { key: entry.key, what: `→ ${change.key.trim()}, new value` };
395
+ return { key: entry.key, what: withCompanion(`→ ${change.key.trim()}, new value`, change.companion) };
380
396
  }
381
397
  if (renamed) return { key: entry.key, what: `→ ${change.key.trim()}` };
382
- return { key: entry.key, what: 'new value' };
398
+ return { key: entry.key, what: withCompanion('new value', change.companion) };
383
399
  }),
384
400
  ];
385
401
 
@@ -396,8 +412,10 @@ function EnvironmentLedger({
396
412
  // Filtering narrows what is listed, never what is saved: a pending edit on
397
413
  // a row the filter hides still counts, and still shows in the save bar.
398
414
  const needle = query.trim().toLowerCase();
399
- const listed =
400
- needle === '' ? active : active.filter((entry) => entry.key.toLowerCase().includes(needle));
415
+ const listed = sortKeys(
416
+ needle === '' ? active : active.filter((entry) => entry.key.toLowerCase().includes(needle)),
417
+ sort,
418
+ );
401
419
  const columns = 6;
402
420
 
403
421
  return (
@@ -442,7 +460,7 @@ function EnvironmentLedger({
442
460
  project={project}
443
461
  environment={environment}
444
462
  canWrite={canWrite}
445
- onAdd={(key) => setDrafts((rows) => [...rows, { id: nextDraftId.current++, key, value: '' }])}
463
+ onAdd={addDraft}
446
464
  />
447
465
 
448
466
  {refile.error !== null && <ErrorLine error={refile.error} />}
@@ -455,16 +473,14 @@ function EnvironmentLedger({
455
473
  </EmptyState>
456
474
  ) : (
457
475
  <div className="dt-wrap">
458
- <table className="dt secrets stacks">
476
+ <table className="dt secrets stacks sortable">
459
477
  <thead>
460
478
  <tr>
461
479
  <th className="n">#</th>
462
- <th className="col-key">
463
- <span className="th">
464
- <Key size={14} />
465
- Key
466
- </span>
467
- </th>
480
+ <SortHeader className="col-key" by="key" sort={sort} onSort={sortBy}>
481
+ <Key size={14} />
482
+ Key
483
+ </SortHeader>
468
484
  <th>
469
485
  <span className="th">
470
486
  <Lock size={14} />
@@ -478,12 +494,10 @@ function EnvironmentLedger({
478
494
  Version
479
495
  </span>
480
496
  </th>
481
- <th className="col-written col-hide-narrow">
482
- <span className="th">
483
- <Clock size={14} />
484
- Last written
485
- </span>
486
- </th>
497
+ <SortHeader className="col-written" by="written" sort={sort} onSort={sortBy}>
498
+ <Clock size={14} />
499
+ Last written
500
+ </SortHeader>
487
501
  <th className="col-actions">
488
502
  <span className="visually-hidden">Actions</span>
489
503
  </th>
@@ -502,6 +516,7 @@ function EnvironmentLedger({
502
516
  project={project}
503
517
  environment={environment}
504
518
  entry={entry}
519
+ rotateAfterDays={rotateAfterDays}
505
520
  change={change ?? { key: entry.key, value: null, archived: false }}
506
521
  editing={
507
522
  change?.archived !== true &&
@@ -522,6 +537,7 @@ function EnvironmentLedger({
522
537
  onPatch={(patch) => patchChange(entry, patch)}
523
538
  onUndo={() => dropChange(entry.key)}
524
539
  onSave={() => saveRow(entry.key)}
540
+ versionOf={versionOf}
525
541
  saveReady={
526
542
  change === undefined || readyAlone([], { [entry.key]: change })
527
543
  }
@@ -553,9 +569,7 @@ function EnvironmentLedger({
553
569
  <DraftRow
554
570
  key={draft.id}
555
571
  draft={draft}
556
- existingVersion={
557
- keys.find((entry) => entry.key === draft.key.trim())?.version ?? null
558
- }
572
+ existingVersion={versionOf(draft.key)}
559
573
  disabled={false}
560
574
  onChange={(patch) =>
561
575
  setDrafts((rows) =>
@@ -564,6 +578,7 @@ function EnvironmentLedger({
564
578
  }
565
579
  onRemove={() => setDrafts((rows) => rows.filter((row) => row.id !== draft.id))}
566
580
  onSave={() => saveDraft(draft)}
581
+ versionOf={versionOf}
567
582
  saveReady={readyAlone([draft], {})}
568
583
  />
569
584
  ))}
@@ -575,7 +590,7 @@ function EnvironmentLedger({
575
590
 
576
591
  {canWrite && (
577
592
  <div className="table-actions">
578
- <button className="btn btn-primary" onClick={addDraft}>
593
+ <button className="btn btn-primary" onClick={() => addDraft()}>
579
594
  <Plus size={14} />
580
595
  New secret
581
596
  </button>
@@ -695,65 +710,6 @@ function EnvironmentName({ project, environment }: { project: string; environmen
695
710
  /* Rows */
696
711
  /* -------------------------------------------------------------------------- */
697
712
 
698
- /**
699
- * A text field whose contents are masked until asked, for secret values.
700
- *
701
- * Where the browser supports it, the mask is CSS on a text input rather than
702
- * `type="password"`: password fields invite password managers to offer to
703
- * save or fill them, and a secret being typed into coffre is neither.
704
- */
705
- function MaskedInput({
706
- masked,
707
- className = '',
708
- ...props
709
- }: Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> & { masked: boolean }) {
710
- const cssMask = useCssMask();
711
- return (
712
- <input
713
- {...props}
714
- type={masked && !cssMask ? 'password' : 'text'}
715
- className={`${className}${masked && cssMask ? ' is-masked' : ''}`}
716
- autoComplete="off"
717
- spellCheck={false}
718
- data-1p-ignore
719
- data-lpignore="true"
720
- data-bwignore
721
- />
722
- );
723
- }
724
-
725
- function useCssMask(): boolean {
726
- const [cssMask, setCssMask] = useState(false);
727
- useEffect(() => {
728
- setCssMask(typeof CSS !== 'undefined' && CSS.supports('-webkit-text-security', 'disc'));
729
- }, []);
730
- return cssMask;
731
- }
732
-
733
- /**
734
- * MaskedInput for a value with line breaks, which a text input would drop.
735
- * Without the CSS mask there is no password textarea, so masked text is
736
- * drawn transparent instead.
737
- */
738
- function MaskedTextarea({
739
- masked,
740
- className = '',
741
- ...props
742
- }: TextareaHTMLAttributes<HTMLTextAreaElement> & { masked: boolean }) {
743
- const cssMask = useCssMask();
744
- return (
745
- <textarea
746
- {...props}
747
- className={`${className}${masked ? (cssMask ? ' is-masked' : ' is-masked-fallback') : ''}`}
748
- autoComplete="off"
749
- spellCheck={false}
750
- data-1p-ignore
751
- data-lpignore="true"
752
- data-bwignore
753
- />
754
- );
755
- }
756
-
757
713
  const lineCount = (text: string) => text.split('\n').length;
758
714
 
759
715
  function ValueField({
@@ -765,6 +721,7 @@ function ValueField({
765
721
  disabled,
766
722
  autoFocus,
767
723
  onEscape,
724
+ generate,
768
725
  }: {
769
726
  label: string;
770
727
  value: string;
@@ -775,8 +732,31 @@ function ValueField({
775
732
  disabled: boolean;
776
733
  autoFocus?: boolean;
777
734
  onEscape?: () => void;
735
+ /** The row Generate fills: its name, whether that can change, and what Use does. */
736
+ generate: {
737
+ name: string;
738
+ renamable: boolean;
739
+ /** The public key a pair generated here before writes with the row. */
740
+ companion: Companion | undefined;
741
+ onUse: (use: GeneratedUse) => void;
742
+ };
778
743
  }) {
779
744
  const [shown, setShown] = useState(false);
745
+ const [generating, setGenerating] = useState(false);
746
+ // A value with a line break is edited in a textarea, and stays in one to
747
+ // the end of the edit: taking its last line break out moves no focus.
748
+ const [lines, setLines] = useState(() => start.includes('\n') || value.includes('\n'));
749
+ if (!lines && value.includes('\n')) setLines(true);
750
+ // Where the caret goes in the textarea a paste or a typed line break opened.
751
+ const caret = useRef<number | null>(null);
752
+ // Text goes into the value of the single-line field in place of its
753
+ // selection, and the field turns into a textarea, focused, its caret after it.
754
+ const insert = (text: string, field: HTMLInputElement) => {
755
+ const from = field.selectionStart ?? value.length;
756
+ const to = field.selectionEnd ?? from;
757
+ caret.current = from + text.length;
758
+ onChange(value.slice(0, from) + text + value.slice(to));
759
+ };
780
760
  const field = {
781
761
  className: 'input input-mono',
782
762
  masked: !shown,
@@ -791,18 +771,56 @@ function ValueField({
791
771
  },
792
772
  };
793
773
  return (
794
- <div className="input-group">
774
+ <div className="input-group has-generate">
795
775
  {/* A value with line breaks opens one line tall, as tall as the row it
796
- replaces, and grows only by the lines typed into it. */}
797
- {start.includes('\n') ? (
776
+ replaces, and grows only by the lines typed or pasted into it. */}
777
+ {lines ? (
798
778
  <MaskedTextarea
799
779
  {...field}
780
+ ref={(node) => {
781
+ if (node === null || caret.current === null) return;
782
+ node.focus();
783
+ node.setSelectionRange(caret.current, caret.current);
784
+ caret.current = null;
785
+ }}
800
786
  className="input input-mono value-lines"
801
787
  rows={Math.min(8, 1 + Math.max(0, lineCount(value) - lineCount(start)))}
802
788
  />
803
789
  ) : (
804
- <MaskedInput {...field} />
790
+ <MaskedInput
791
+ {...field}
792
+ // A text input drops a pasted value's line breaks, a PEM key's
793
+ // included. Such a paste goes into the value here instead, and the
794
+ // field turns into a textarea, focused, its caret after the paste.
795
+ // Line breaks are \n, as a textarea keeps them: \r\n is one \n.
796
+ onPaste={(event) => {
797
+ const pasted = event.clipboardData.getData('text/plain');
798
+ if (!/[\r\n]/.test(pasted)) return;
799
+ event.preventDefault();
800
+ insert(pasted.replace(/\r\n?/g, '\n'), event.currentTarget);
801
+ }}
802
+ // Shift+Enter types a line break, the way a textarea's Enter does.
803
+ // Enter alone stays as it was, and so does Cmd/Ctrl+Enter, which saves,
804
+ // and so does Shift+Enter that confirms text an input method is composing.
805
+ onKeyDown={(event) => {
806
+ if (event.key === 'Enter' && event.shiftKey && !event.metaKey && !event.ctrlKey && !event.nativeEvent.isComposing) {
807
+ event.preventDefault();
808
+ insert('\n', event.currentTarget);
809
+ } else field.onKeyDown(event);
810
+ }}
811
+ />
805
812
  )}
813
+ <Tip label="Generate a value">
814
+ <button
815
+ type="button"
816
+ className="btn btn-quiet btn-sm btn-icon input-addon input-addon-generate"
817
+ aria-label={`Generate a value for ${generate.name.trim() === '' ? 'the new secret' : generate.name.trim()}`}
818
+ disabled={disabled}
819
+ onClick={() => setGenerating(true)}
820
+ >
821
+ <Generate size={14} />
822
+ </button>
823
+ </Tip>
806
824
  <button
807
825
  type="button"
808
826
  className="btn btn-quiet btn-sm btn-icon input-addon"
@@ -812,10 +830,38 @@ function ValueField({
812
830
  >
813
831
  {shown ? <EyeOff size={14} /> : <Eye size={14} />}
814
832
  </button>
833
+ <GenerateValue
834
+ open={generating}
835
+ onOpenChange={setGenerating}
836
+ name={generate.name}
837
+ renamable={generate.renamable}
838
+ companion={generate.companion}
839
+ onUse={generate.onUse}
840
+ />
815
841
  </div>
816
842
  );
817
843
  }
818
844
 
845
+ /** Under a row's value: the public key that saves with it, and the version it makes. */
846
+ function CompanionNote({
847
+ companion,
848
+ versionOf,
849
+ }: {
850
+ companion: Companion | undefined;
851
+ versionOf: (key: string) => number | null;
852
+ }) {
853
+ if (companion === undefined) return null;
854
+ const version = versionOf(companion.key);
855
+ return (
856
+ <span className="edit-note">
857
+ <span>
858
+ Saves its public key as <span className="mono">{companion.key.trim()}</span>
859
+ {version === null ? ', a new secret' : `, v${version} → v${version + 1}`}
860
+ </span>
861
+ </span>
862
+ );
863
+ }
864
+
819
865
  function useSecondsLeft(reveal: Reveal | null): number {
820
866
  const [now, setNow] = useState(() => Date.now());
821
867
  useEffect(() => {
@@ -833,6 +879,7 @@ function SecretRow({
833
879
  project,
834
880
  environment,
835
881
  entry,
882
+ rotateAfterDays,
836
883
  change,
837
884
  editing,
838
885
  canWrite,
@@ -849,6 +896,7 @@ function SecretRow({
849
896
  onPatch,
850
897
  onUndo,
851
898
  onSave,
899
+ versionOf,
852
900
  saveReady,
853
901
  onMarkArchive,
854
902
  }: {
@@ -856,6 +904,7 @@ function SecretRow({
856
904
  project: string;
857
905
  environment: string;
858
906
  entry: SecretKey;
907
+ rotateAfterDays: number | null;
859
908
  change: SecretChange;
860
909
  editing: boolean;
861
910
  canWrite: boolean;
@@ -877,6 +926,8 @@ function SecretRow({
877
926
  onUndo: () => void;
878
927
  /** This row's change alone, saved now. */
879
928
  onSave: () => void;
929
+ /** The current version of a key in this environment, for a companion's note. */
930
+ versionOf: (key: string) => number | null;
880
931
  saveReady: boolean;
881
932
  onMarkArchive: () => void;
882
933
  }) {
@@ -1045,11 +1096,20 @@ function SecretRow({
1045
1096
  placeholder={canReveal ? 'Value' : 'New value; the current one is hidden from you'}
1046
1097
  disabled={disabled}
1047
1098
  autoFocus={!focusName}
1099
+ // A value typed over a generated private key no longer matches its public key.
1048
1100
  onChange={(value) =>
1049
- onPatch({ value: value === (editBase?.value ?? '') ? null : value })
1101
+ onPatch({ value: value === (editBase?.value ?? '') ? null : value, companion: undefined })
1050
1102
  }
1051
1103
  onEscape={onUndo}
1104
+ generate={{
1105
+ // A pending rename is the name it saves under.
1106
+ name: change.key,
1107
+ renamable: false,
1108
+ companion: change.companion,
1109
+ onUse: ({ value, companion }) => onPatch({ value, companion }),
1110
+ }}
1052
1111
  />
1112
+ <CompanionNote companion={change.companion} versionOf={versionOf} />
1053
1113
  {reference !== null && reference.state === 'live' && (
1054
1114
  <span className="edit-note">
1055
1115
  Saving a value stops following <span className="mono">{reference.source}</span>
@@ -1155,8 +1215,8 @@ function SecretRow({
1155
1215
  )}
1156
1216
  </td>
1157
1217
 
1158
- <td className="col-written col-hide-narrow" data-label="Last written">
1159
- <Written entry={reference === null ? entry : { ...entry, updatedBy: reference.createdBy.replace(/^user:/, ''), updatedAt: reference.createdAt }} />
1218
+ <td className="col-written" data-label="Last written">
1219
+ <Written entry={entry} rotateAfterDays={rotateAfterDays} />
1160
1220
  </td>
1161
1221
 
1162
1222
  <td className="col-actions">
@@ -1323,29 +1383,80 @@ function SecretRow({
1323
1383
  }
1324
1384
 
1325
1385
  /**
1326
- * Who wrote the current version and how long ago, on one line: the local part
1327
- * of the email is enough to recognise a colleague, and the full address and
1328
- * the exact time are a hover away.
1386
+ * A column header that orders the table: its first click ascending, the
1387
+ * next descending. Below the width where rows stack, the sortable headers
1388
+ * stay, a row of their own above the keys.
1389
+ */
1390
+ function SortHeader({
1391
+ className,
1392
+ by,
1393
+ sort,
1394
+ onSort,
1395
+ children,
1396
+ }: {
1397
+ className: string;
1398
+ by: Sort['by'];
1399
+ sort: Sort;
1400
+ onSort: (by: Sort['by']) => void;
1401
+ children: ReactNode;
1402
+ }) {
1403
+ const sorted = sort.by === by;
1404
+ return (
1405
+ <th className={`${className} th-sortable`} aria-sort={sorted ? (sort.descending ? 'descending' : 'ascending') : undefined}>
1406
+ <button type="button" className="th th-sort" onClick={() => onSort(by)}>
1407
+ {children}
1408
+ {sorted ? (
1409
+ <ChevronDown size={13} className={sort.descending ? 'sort-mark' : 'sort-mark is-ascending'} />
1410
+ ) : (
1411
+ <ChevronsUpDown size={13} className="sort-mark is-idle" />
1412
+ )}
1413
+ </button>
1414
+ </th>
1415
+ );
1416
+ }
1417
+
1418
+ /**
1419
+ * Who wrote the value a key reads and how long ago, on one line: the local
1420
+ * part of the email is enough to recognise a colleague, and the full address
1421
+ * and the exact time are a hover away. A reference's value is its source's,
1422
+ * as old as the version it reads, and who made the reference is a hover
1423
+ * away too. A value unchanged past the deployment's `rotateAfterDays` gets a
1424
+ * quiet tag, its age in days a hover away: it is due for rotation.
1329
1425
  */
1330
- function Written({ entry }: { entry: SecretKey }) {
1331
- if (entry.updatedBy === null && entry.updatedAt === null) {
1332
- return <span className="cell-muted">—</span>;
1426
+ function Written({ entry, rotateAfterDays }: { entry: SecretKey; rotateAfterDays: number | null }) {
1427
+ const { reference } = entry;
1428
+ const made = reference === null
1429
+ ? undefined
1430
+ : `Reference made by ${shownMember(reference.createdBy).replace(/^user:/, '')} on ${reference.createdAt.slice(0, 10)}`;
1431
+ if (entry.updatedAt === null) {
1432
+ return <span className="cell-muted" title={made}>—</span>;
1333
1433
  }
1334
1434
  const by = entry.updatedBy ?? 'unknown';
1435
+ const due = rotateAfterDays !== null && olderThan(entry, rotateAfterDays);
1436
+ const age = ageInDays(entry);
1335
1437
  return (
1336
1438
  <span className="written">
1337
- <span className="written-by" title={by}>
1338
- {by.split('@')[0]}
1439
+ {reference === null ? (
1440
+ <span className="written-by" title={by}>
1441
+ {by.split('@')[0]}
1442
+ </span>
1443
+ ) : (
1444
+ <span className="written-by cell-muted" title={`Its source's current version. ${made}`}>
1445
+ source
1446
+ </span>
1447
+ )}
1448
+ <span className="cell-muted" aria-hidden>
1449
+ ·
1339
1450
  </span>
1340
- {entry.updatedAt !== null && (
1341
- <>
1342
- <span className="cell-muted" aria-hidden>
1343
- ·
1344
- </span>
1345
- <span className="cell-muted">
1346
- <Timestamp iso={entry.updatedAt} display="relative" />
1451
+ <span className="cell-muted">
1452
+ <Timestamp iso={entry.updatedAt} display="relative" />
1453
+ </span>
1454
+ {due && (
1455
+ <Tip label={`Unchanged for ${age} days, past the ${rotateAfterDays} this deployment allows: rotate it`}>
1456
+ <span className="tag tag-amber written-flag" tabIndex={0}>
1457
+ Rotate
1347
1458
  </span>
1348
- </>
1459
+ </Tip>
1349
1460
  )}
1350
1461
  </span>
1351
1462
  );
@@ -1365,6 +1476,7 @@ function DraftRow({
1365
1476
  onChange,
1366
1477
  onRemove,
1367
1478
  onSave,
1479
+ versionOf,
1368
1480
  saveReady,
1369
1481
  }: {
1370
1482
  draft: SecretDraft;
@@ -1374,6 +1486,8 @@ function DraftRow({
1374
1486
  onRemove: () => void;
1375
1487
  /** This new secret alone, saved now. */
1376
1488
  onSave: () => void;
1489
+ /** The current version of a key in this environment, for a companion's note. */
1490
+ versionOf: (key: string) => number | null;
1377
1491
  saveReady: boolean;
1378
1492
  }) {
1379
1493
  const [touched, setTouched] = useState(false);
@@ -1423,9 +1537,17 @@ function DraftRow({
1423
1537
  value={draft.value}
1424
1538
  placeholder="Value"
1425
1539
  disabled={disabled}
1426
- onChange={(value) => onChange({ value })}
1540
+ // A value typed over a generated private key no longer matches its public key.
1541
+ onChange={(value) => onChange({ value, companion: undefined })}
1427
1542
  onEscape={onRemove}
1543
+ generate={{
1544
+ name: draft.key,
1545
+ renamable: true,
1546
+ companion: draft.companion,
1547
+ onUse: ({ value, name, companion }) => onChange({ key: name, value, companion }),
1548
+ }}
1428
1549
  />
1550
+ <CompanionNote companion={draft.companion} versionOf={versionOf} />
1429
1551
  </div>
1430
1552
  </div>
1431
1553
  <div className="acts value-acts">
@@ -1505,7 +1627,7 @@ function ArchivedRow({
1505
1627
  <span className="version-shift">{entry.version === null ? '—' : `v${entry.version}`}</span>
1506
1628
  </td>
1507
1629
  <td className="col-written col-hide-narrow" data-label="Last written">
1508
- <Written entry={entry} />
1630
+ <Written entry={entry} rotateAfterDays={null} />
1509
1631
  </td>
1510
1632
  <td className="col-actions">
1511
1633
  {state.state === 'pending' ? (