@coffre/ui 0.9.0 → 0.10.0

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 (92) hide show
  1. package/dist/chunks/{audit-sentence-JrQIUq6j.js → audit-sentence-Bjhg00cn.js} +73 -6
  2. package/dist/chunks/audit-sentence-Bjhg00cn.js.map +1 -0
  3. package/dist/chunks/{connected-apps-Dr8UgjK7.js → connected-apps-CZI7xxUq.js} +2 -2
  4. package/dist/chunks/{connected-apps-Dr8UgjK7.js.map → connected-apps-CZI7xxUq.js.map} +1 -1
  5. package/dist/chunks/{delete-place-CQCDHkne.js → delete-place-D3UQGl3b.js} +3 -3
  6. package/dist/chunks/{delete-place-CQCDHkne.js.map → delete-place-D3UQGl3b.js.map} +1 -1
  7. package/dist/chunks/{directory-XcSbDRJv.js → directory-CyHmxYP-.js} +9 -7
  8. package/dist/chunks/directory-CyHmxYP-.js.map +1 -0
  9. package/dist/chunks/{grants-CmdZjKYU.js → grants-CEIdNqJU.js} +4 -4
  10. package/dist/chunks/{grants-CmdZjKYU.js.map → grants-CEIdNqJU.js.map} +1 -1
  11. package/dist/chunks/{offboarding-CrgwaetA.js → offboarding-CvGgTmHi.js} +2 -2
  12. package/dist/chunks/{offboarding-CrgwaetA.js.map → offboarding-CvGgTmHi.js.map} +1 -1
  13. package/dist/chunks/{personal-tokens-Dvl1FktE.js → personal-tokens-BX97oX4a.js} +2 -2
  14. package/dist/chunks/{personal-tokens-Dvl1FktE.js.map → personal-tokens-BX97oX4a.js.map} +1 -1
  15. package/dist/chunks/{principal-page-bOaEYWJB.js → principal-page-Bp2O8kIW.js} +92 -76
  16. package/dist/chunks/principal-page-Bp2O8kIW.js.map +1 -0
  17. package/dist/chunks/{project-page-BI--vjt3.js → project-page-CUXV4FDR.js} +2 -2
  18. package/dist/chunks/{project-page-BI--vjt3.js.map → project-page-CUXV4FDR.js.map} +1 -1
  19. package/dist/chunks/protected-YYW3U-Sq.js +22 -0
  20. package/dist/chunks/protected-YYW3U-Sq.js.map +1 -0
  21. package/dist/chunks/{queries-epz4MIuW.js → queries-DEOPOpsT.js} +27 -3
  22. package/dist/chunks/queries-DEOPOpsT.js.map +1 -0
  23. package/dist/chunks/{references-DImfw0cz.js → references-DsXkt064.js} +3 -3
  24. package/dist/chunks/{references-DImfw0cz.js.map → references-DsXkt064.js.map} +1 -1
  25. package/dist/chunks/{role-field-C1KhNaIR.js → role-field-CCUeZd1a.js} +8 -5
  26. package/dist/chunks/role-field-CCUeZd1a.js.map +1 -0
  27. package/dist/chunks/{use-action-BB-RcHo1.js → use-action-COW-XFw8.js} +2 -2
  28. package/dist/chunks/{use-action-BB-RcHo1.js.map → use-action-COW-XFw8.js.map} +1 -1
  29. package/dist/chunks/{use-change-BY6KPmnJ.js → use-change-hfqE7baE.js} +217 -100
  30. package/dist/chunks/use-change-hfqE7baE.js.map +1 -0
  31. package/dist/chunks/use-roles-C_Tj5wBH.js +224 -0
  32. package/dist/chunks/use-roles-C_Tj5wBH.js.map +1 -0
  33. package/dist/chunks/{use-shell-C_NmvCt_.js → use-shell-DBLG80kk.js} +2 -2
  34. package/dist/chunks/{use-shell-C_NmvCt_.js.map → use-shell-DBLG80kk.js.map} +1 -1
  35. package/dist/index.d.ts +8 -3
  36. package/dist/index.js +25 -11
  37. package/dist/index.js.map +1 -1
  38. package/dist/pages/account.js +6 -6
  39. package/dist/pages/audit.js +2 -2
  40. package/dist/pages/environment.js +251 -111
  41. package/dist/pages/environment.js.map +1 -1
  42. package/dist/pages/login.js +1 -1
  43. package/dist/pages/project-access.js +78 -45
  44. package/dist/pages/project-access.js.map +1 -1
  45. package/dist/pages/project-environments.js +77 -28
  46. package/dist/pages/project-environments.js.map +1 -1
  47. package/dist/pages/project-settings.js +6 -6
  48. package/dist/pages/project.js +1 -1
  49. package/dist/pages/projects.js +23 -11
  50. package/dist/pages/projects.js.map +1 -1
  51. package/dist/pages/service-account.js +19 -12
  52. package/dist/pages/service-account.js.map +1 -1
  53. package/dist/pages/service-accounts.js +4 -4
  54. package/dist/pages/settings.js +580 -8
  55. package/dist/pages/settings.js.map +1 -1
  56. package/dist/pages/unregistered.js +1 -1
  57. package/dist/pages/user.js +5 -5
  58. package/dist/pages/users.js +4 -4
  59. package/package.json +3 -3
  60. package/src/components/directory.tsx +5 -2
  61. package/src/components/grants.tsx +1 -1
  62. package/src/components/principal-page.tsx +38 -23
  63. package/src/components/protected.tsx +17 -0
  64. package/src/components/role-editor.tsx +582 -0
  65. package/src/components/role-field.tsx +7 -3
  66. package/src/components/scope-picker.tsx +19 -9
  67. package/src/components/shell.tsx +7 -1
  68. package/src/lib/access-plan.ts +35 -36
  69. package/src/lib/audit-sentences.ts +75 -9
  70. package/src/lib/changes.ts +46 -8
  71. package/src/lib/project-access.ts +4 -78
  72. package/src/lib/queries.ts +16 -0
  73. package/src/lib/roles.ts +201 -0
  74. package/src/lib/row-reads.ts +12 -0
  75. package/src/lib/secret-edit-batch.ts +147 -56
  76. package/src/lib/sensitivity.ts +50 -0
  77. package/src/lib/service-setup.ts +20 -8
  78. package/src/lib/use-roles.ts +17 -0
  79. package/src/pages/environment.tsx +264 -101
  80. package/src/pages/project-access.tsx +49 -19
  81. package/src/pages/project-environments.tsx +62 -3
  82. package/src/pages/projects.tsx +3 -1
  83. package/src/pages/settings.tsx +2 -0
  84. package/src/parity.ts +41 -4
  85. package/src/shared/models.ts +2 -9
  86. package/src/styles/globals.css +322 -25
  87. package/dist/chunks/audit-sentence-JrQIUq6j.js.map +0 -1
  88. package/dist/chunks/directory-XcSbDRJv.js.map +0 -1
  89. package/dist/chunks/principal-page-bOaEYWJB.js.map +0 -1
  90. package/dist/chunks/queries-epz4MIuW.js.map +0 -1
  91. package/dist/chunks/role-field-C1KhNaIR.js.map +0 -1
  92. package/dist/chunks/use-change-BY6KPmnJ.js.map +0 -1
@@ -27,6 +27,7 @@ import {
27
27
  GitHub,
28
28
  Key,
29
29
  Ledger,
30
+ Lock,
30
31
  Mark,
31
32
  Menu as MenuIcon,
32
33
  PanelLeft,
@@ -506,6 +507,9 @@ function EnvironmentCrumb({
506
507
  slug: string;
507
508
  environments: ProjectSummary['environments'];
508
509
  }) {
510
+ // Protected, it says so where you are and in the list.
511
+ const guarded = environments.some((environment) => environment.slug === slug && environment.protected);
512
+ const lock = <Lock size={12} className="crumb-lock" label="protected" />;
509
513
  if (environments.length === 0) {
510
514
  return (
511
515
  <span className="crumb-link" aria-current="page">
@@ -517,10 +521,11 @@ function EnvironmentCrumb({
517
521
  <Menu.Root>
518
522
  <Menu.Trigger
519
523
  className="crumb-link crumb-switch"
520
- aria-label={`${slug}, switch environment`}
524
+ aria-label={`${slug}${guarded ? ', protected' : ''}, switch environment`}
521
525
  aria-current="page"
522
526
  >
523
527
  <span className="crumb-name">{slug}</span>
528
+ {guarded && <Lock size={12} className="crumb-lock" />}
524
529
  <ChevronsUpDown size={14} className="crumb-chevron" />
525
530
  </Menu.Trigger>
526
531
  <MenuPopup align="start">
@@ -541,6 +546,7 @@ function EnvironmentCrumb({
541
546
  }
542
547
  >
543
548
  <span className="mono">{environment.slug}</span>
549
+ {environment.protected && lock}
544
550
  <span className="menu-hint">
545
551
  {environment.details?.secretCount ?? 0} secret
546
552
  {environment.details?.secretCount === 1 ? '' : 's'}
@@ -1,23 +1,26 @@
1
1
  import type { AccessValue } from '@coffre/client';
2
- import type { Role } from '@coffre/core/access';
2
+ import { assignableToEnvironment, type Role } from '@coffre/core/access';
3
3
 
4
4
  import type { GrantRow } from '../shared/models';
5
5
 
6
- /** Read or write on one environment, and when it ends: an ISO instant, or null for never. */
7
- export type EnvironmentAccess = { role: Extract<Role, 'viewer' | 'developer'>; expiresAt: string | null };
6
+ /** A role on one environment, and when it ends: an ISO instant, or null for never. */
7
+ export type EnvironmentAccess = { role: Role; expiresAt: string | null };
8
8
 
9
9
  /**
10
- * What one project is set to in a principal's access editor: nothing, owner,
11
- * read or write everywhere, or read or write per environment. Every grant the
10
+ * What one project is set to in a principal's access editor: nothing, one
11
+ * role on the whole project, or a role per environment. Every grant the
12
12
  * plan asks for carries its own expiry.
13
13
  *
14
- * `custom` is for grants that do not fit that shape -- an auditor grant, or
15
- * read everywhere plus write on one environment. The editor shows them and
16
- * leaves them alone unless another setting is picked.
14
+ * `custom` is for grants that do not fit that shape -- read everywhere plus
15
+ * write on one environment, or a grant on an environment the editor does not
16
+ * list. The editor shows them and leaves them alone unless another setting
17
+ * is picked.
17
18
  */
18
19
  export type AccessPlan = {
19
- level: 'none' | Extract<Role, 'owner' | 'viewer' | 'developer'> | 'env' | 'custom';
20
- /** When the project-wide level ends. */
20
+ level: 'none' | 'project' | 'env' | 'custom';
21
+ /** The role on the whole project, at level `project`. */
22
+ role: Role | null;
23
+ /** When the project-wide role ends. */
21
24
  expiresAt: string | null;
22
25
  /** Environments without an entry get no access. */
23
26
  environments: Record<string, EnvironmentAccess>;
@@ -38,6 +41,8 @@ export function environmentAccess(plan: AccessPlan, slug: string): EnvironmentAc
38
41
  return Object.hasOwn(plan.environments, slug) ? plan.environments[slug] : null;
39
42
  }
40
43
 
44
+ const NONE: AccessPlan = { level: 'none', role: null, expiresAt: null, environments: {} };
45
+
41
46
  /**
42
47
  * Read what a principal holds on a project back into the editor's terms.
43
48
  * `environments` are the ones the editor can set: live, and not archived.
@@ -46,56 +51,52 @@ export function planFromGrants(
46
51
  grants: readonly HeldGrant[],
47
52
  environments: readonly string[],
48
53
  ): AccessPlan {
49
- if (grants.length === 0) return { level: 'none', expiresAt: null, environments: {} };
54
+ if (grants.length === 0) return NONE;
50
55
 
51
56
  const [only] = grants;
52
- if (
53
- grants.length === 1 &&
54
- only.environmentSlug === null &&
55
- (only.role === 'owner' || only.role === 'viewer' || only.role === 'developer')
56
- ) {
57
- return { level: only.role, expiresAt: only.expiresAt, environments: {} };
57
+ if (grants.length === 1 && only.environmentSlug === null) {
58
+ return { level: 'project', role: only.role, expiresAt: only.expiresAt, environments: {} };
58
59
  }
59
60
 
60
61
  const perEnvironment: Record<string, EnvironmentAccess> = {};
61
62
  for (const grant of grants) {
62
63
  const slug = grant.environmentSlug;
63
- if (
64
- slug === null ||
65
- !environments.includes(slug) ||
66
- Object.hasOwn(perEnvironment, slug) ||
67
- (grant.role !== 'viewer' && grant.role !== 'developer')
68
- ) {
69
- return { level: 'custom', expiresAt: null, environments: {} };
64
+ if (slug === null || !environments.includes(slug) || Object.hasOwn(perEnvironment, slug)) {
65
+ return { ...NONE, level: 'custom' };
70
66
  }
71
67
  perEnvironment[slug] = { role: grant.role, expiresAt: grant.expiresAt };
72
68
  }
73
- return { level: 'env', expiresAt: null, environments: perEnvironment };
69
+ return { level: 'env', role: null, expiresAt: null, environments: perEnvironment };
74
70
  }
75
71
 
76
72
  /**
77
- * Switch a plan's level. Going from read or write everywhere to per
78
- * environment starts every environment at that level and expiry, so the
79
- * switch alone keeps the same reach.
73
+ * Switch a plan's level, with its role for `project`. Going from one role
74
+ * on the whole project to per environment starts every environment at that
75
+ * role and expiry, where an environment takes it, so the switch alone keeps
76
+ * the same reach.
80
77
  */
81
78
  export function withLevel(
82
79
  plan: AccessPlan,
83
80
  level: AccessPlan['level'],
84
81
  environments: readonly string[],
82
+ { role = null, assignable = (each) => assignableToEnvironment(each) }: { role?: Role | null; assignable?: (role: Role) => boolean } = {},
85
83
  ): AccessPlan {
86
84
  if (
87
85
  level === 'env' &&
88
- (plan.level === 'viewer' || plan.level === 'developer') &&
86
+ plan.level === 'project' &&
87
+ plan.role !== null &&
88
+ assignable(plan.role) &&
89
89
  Object.keys(plan.environments).length === 0
90
90
  ) {
91
- const access = { role: plan.level, expiresAt: plan.expiresAt };
91
+ const access = { role: plan.role, expiresAt: plan.expiresAt };
92
92
  return {
93
93
  ...plan,
94
94
  level,
95
+ role: null,
95
96
  environments: Object.fromEntries(environments.map((slug) => [slug, access])),
96
97
  };
97
98
  }
98
- return { ...plan, level };
99
+ return { ...plan, level, role: level === 'project' ? role : null };
99
100
  }
100
101
 
101
102
  /** Set one environment's access, or take it away with `null`. */
@@ -117,10 +118,8 @@ function grantsFor(
117
118
  case 'none':
118
119
  case 'custom':
119
120
  return [];
120
- case 'owner':
121
- case 'viewer':
122
- case 'developer':
123
- return [{ role: plan.level, environmentSlug: null, expiresAt: plan.expiresAt }];
121
+ case 'project':
122
+ return plan.role === null ? [] : [{ role: plan.role, environmentSlug: null, expiresAt: plan.expiresAt }];
124
123
  case 'env':
125
124
  return Object.entries(plan.environments).map(([environmentSlug, access]) => ({
126
125
  environmentSlug,
@@ -177,7 +176,7 @@ export function accessPatch(project: string, changes: readonly AccessChange[]):
177
176
  const place = (environmentSlug: string | null) =>
178
177
  environmentSlug === null ? project : `${project}/${environmentSlug}`;
179
178
  const holding = (role: string, expiresAt: string | null): AccessValue =>
180
- expiresAt === null ? (role as Role) : { role: role as Role, until: expiresAt };
179
+ expiresAt === null ? role : { role, until: expiresAt };
181
180
 
182
181
  const patch: Record<string, AccessValue> = {};
183
182
  for (const change of changes) {
@@ -8,7 +8,7 @@
8
8
  * and places in it links. `plain` flattens it, for tests and titles.
9
9
  */
10
10
  import { shownMember, type AuditEntryView } from '@coffre/client';
11
- import { INSTANCE_ROLES, isInstanceRole, isScope, scopeInWords, type Scope } from '@coffre/core/access';
11
+ import { INSTANCE_ROLES, isBuiltinRole, isInstanceRole, isScope, ROLES, scopeInWords, type Scope } from '@coffre/core/access';
12
12
 
13
13
  /** What a sentence is made from: an entry of the log, as `GET /api/audit` gives it. */
14
14
  export type AuditEntry = Pick<
@@ -245,18 +245,22 @@ const TEMPLATES: Record<string, Template> = {
245
245
  what: secrets,
246
246
  },
247
247
  'secret.write': {
248
- // New secrets are added, others changed; a batch of both is written.
249
- did: ({ batch }) => {
248
+ // New secrets are added, others changed; a batch of both is written. One whose level changed is made that.
249
+ did: ({ entry, batch }) => {
250
+ if (batch.length === 1 && text(entry.metadata.was) !== null) return 'made';
250
251
  const firsts = batch.filter((entry) => entry.version === 1).length;
251
252
  return firsts === batch.length ? 'added' : firsts === 0 ? 'changed' : 'wrote';
252
253
  },
253
254
  tried: 'change',
254
255
  what: secrets,
255
- // A plain value is no secret: the sentence says it was written as one.
256
- then: ({ entry, batch }) => [
257
- ...(batch.length > 1 || entry.version === null || entry.version === 1 ? [] : [`, now version ${entry.version}`]),
258
- ...(batch.length === 1 && entry.metadata.plain === true ? [', as a plain value, no secret'] : []),
259
- ],
256
+ // Who sees the value, where it is not a secret's or it changed: a mark keeps the value, a write gives a new one.
257
+ then: ({ entry, batch }) => {
258
+ const version = batch.length > 1 || entry.version === null || entry.version === 1 ? [] : [`, now version ${entry.version}`];
259
+ if (batch.length > 1) return version;
260
+ const level = entry.metadata.sensitive === true ? 'sensitive' : entry.metadata.plain === true ? 'plain' : 'secret';
261
+ if (text(entry.metadata.was) !== null) return [` ${level}`, ...(number(entry.metadata.kept) === null ? [' with a new value'] : []), ...version];
262
+ return [...version, ...(level === 'plain' ? [', as a plain value, no secret'] : level === 'sensitive' ? [', as a sensitive value, shown to nobody'] : [])];
263
+ },
260
264
  },
261
265
  // The credential proxy's own entry beside the vault's read: which credential the keys went into, and where.
262
266
  'credential.use': {
@@ -396,6 +400,12 @@ const TEMPLATES: Record<string, Template> = {
396
400
  return setting(previous).map((words) => `, was ${words}`);
397
401
  },
398
402
  },
403
+ // The roles an Owner edits and which environments are protected (docs/design/roles.md), the vault's to change.
404
+ 'policy.change': {
405
+ did: ({ entry }) => POLICY_VERBS[policyOf(entry).verb][0],
406
+ tried: ({ entry }) => POLICY_VERBS[policyOf(entry).verb][1],
407
+ what: ({ entry }) => policyOf(entry).what,
408
+ },
399
409
  // A binding trusts CI runs to sign in as a service; removing one is its tombstone.
400
410
  'token.bind': {
401
411
  did: 'trusted CI runs to sign in as',
@@ -516,6 +526,57 @@ function roleName(role: string): string | null {
516
526
  return isInstanceRole(role) ? INSTANCE_ROLES[role].name : null;
517
527
  }
518
528
 
529
+ /** What a policy change did, and what it did it to, as `did` and `tried` say it. */
530
+ const POLICY_VERBS = {
531
+ protect: ['protected', 'protect'],
532
+ unprotect: ['unprotected', 'unprotect'],
533
+ edit: ['edited', 'edit'],
534
+ reset: ['reset', 'reset'],
535
+ make: ['made', 'make'],
536
+ delete: ['deleted', 'delete'],
537
+ } as const;
538
+
539
+ /**
540
+ * A `policy.change` entry, from what it names: its `change`, an
541
+ * environment's protection or a role's definition, and the role `before`
542
+ * it. A built-in role's definition dropped is a reset; one an Owner made, a
543
+ * deletion; one that was not there, made. A refusal the app wrote names the
544
+ * role alone.
545
+ *
546
+ * protected market/prod edited the Developer role
547
+ * made the Release manager role reset the Auditor instance role
548
+ */
549
+ function policyOf(entry: AuditEntry): { verb: keyof typeof POLICY_VERBS; what: Part[] } {
550
+ const change = record(entry.metadata.change);
551
+ if (change.kind === 'protect') {
552
+ const count = Array.isArray(change.environmentIds) ? change.environmentIds.length : 1;
553
+ // A fork's, protected before its row is written, names its project alone.
554
+ const what: Part[] = entry.environment !== null
555
+ ? place({ ...entry, key: null })
556
+ : entry.project === null
557
+ ? [count === 1 ? 'an environment' : plural(count, 'environment')]
558
+ : [count === 1 ? 'a new environment of ' : `${plural(count, 'environment')} of `, { place: entry.project }];
559
+ return { verb: change.protected === false ? 'unprotect' : 'protect', what };
560
+ }
561
+ const instance = (change.kind ?? entry.metadata.kind) === 'instance-role';
562
+ const id = text(change.role) ?? text(entry.metadata.role);
563
+ const shipped = id !== null && (instance ? isInstanceRole(id) : isBuiltinRole(id));
564
+ const definition = change.definition === null ? null : record(change.definition);
565
+ const before = record(entry.metadata.before);
566
+ const name = text(definition?.name) ?? text(record(before.definition).name)
567
+ ?? (id === null ? null : shipped ? (instance ? INSTANCE_ROLES[id as keyof typeof INSTANCE_ROLES] : ROLES[id as keyof typeof ROLES]).name : id);
568
+ // A reset or deletion: the vault's entry says so in its change, a refusal of the app's in its own metadata.
569
+ const verb = change.definition === null || entry.metadata.definition === null || entry.reason === 'role_granted'
570
+ ? (shipped ? 'reset' : 'delete')
571
+ : before.definition === null ? 'make' : 'edit';
572
+ return { verb, what: [name === null ? 'a role' : `the ${name} ${instance ? 'instance role' : 'role'}`] };
573
+ }
574
+
575
+ /** An object of the metadata's, or an empty one for anything else. */
576
+ function record(value: unknown): Record<string, unknown> {
577
+ return typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
578
+ }
579
+
519
580
  function article(name: string): string {
520
581
  return `${/^[AEIOU]/.test(name) ? 'an' : 'a'} ${name}`;
521
582
  }
@@ -597,7 +658,11 @@ const TAMPERING: Record<string, string> = {
597
658
  const REASONS: Record<string, string> = {
598
659
  no_grant: 'no grant',
599
660
  bulk_limit: 'bulk limit',
600
- stand_ins: 'people get stand-ins',
661
+ // 0.9's `people: 'stand-ins'`, in entries written before 0.10.
662
+ stand_ins: 'people get placeholders',
663
+ sensitive: 'the value is sensitive',
664
+ looser: 'making it visible takes a new value',
665
+ inherited: 'it is inherited',
601
666
  expired: 'grant expired',
602
667
  removed: 'removed from coffre',
603
668
  not_a_member: 'not a member',
@@ -634,6 +699,7 @@ const REASONS: Record<string, string> = {
634
699
  disconnected: 'the app was disconnected',
635
700
  personal_token: 'only its person can, signed in as themselves',
636
701
  beyond_holder: 'more than they hold',
702
+ role_granted: 'grants still name it',
637
703
  };
638
704
 
639
705
  /** What a missing permission meant, from the app's `missing_<permission>`. */
@@ -8,7 +8,7 @@ import type { Change, ListShape, Target, Words } from './optimistic.ts';
8
8
  import { affects, keys } from './queries.ts';
9
9
  import { regenerateWrite, type Generation } from '@coffre/core/recipe';
10
10
  import { regeneratedKeys, renamePairs } from './recipes.ts';
11
- import { applySecretEditBatch, companionsOf, generationsOf, plainValuesOf, type SecretChange, type SecretDraft, type SecretEditBatchResult } from './secret-edit-batch.ts';
11
+ import { applySecretEditBatch, companionsOf, generationsOf, levelsOf, type SecretChange, type SecretDraft, type SecretEditBatchResult } from './secret-edit-batch.ts';
12
12
  import type { DirectoryPrincipal, GrantRow, ProjectSummary } from '../shared/models.ts';
13
13
 
14
14
  /**
@@ -81,7 +81,7 @@ export function grantAccess(client: CoffreClient, project: string): Change<Grant
81
81
  const list = grantsList(project);
82
82
  return {
83
83
  list,
84
- label: (vars) => `${vars.roleName} for ${vars.principalId}`,
84
+ label: (vars) => `${projectAccessLabel(vars)} for ${vars.principalId}`,
85
85
  targets: (vars) => [saving(grantId(vars))],
86
86
  apply: (grants, vars) => {
87
87
  const row: GrantRow = {
@@ -106,7 +106,7 @@ export function grantAccess(client: CoffreClient, project: string): Change<Grant
106
106
  ],
107
107
  run: async (vars) => {
108
108
  const place = grantPlace(project, vars.environmentSlug);
109
- const role = vars.role as Role;
109
+ const role = vars.role;
110
110
  const member = memberRef(vars.principalType, vars.principalId);
111
111
  const access = { [place]: vars.expiresAt === null ? role : { role, until: vars.expiresAt } };
112
112
  const { changes } = vars.admit === true ? await client.members.add(member, { access }) : await client.access.set(member, access);
@@ -386,7 +386,16 @@ export function createEnvironment(client: CoffreClient, project: string): Change
386
386
  targets: (vars) => [saving(environmentId(project, vars.slug))],
387
387
  apply: (environments, vars) => [
388
388
  ...environments,
389
- { slug: vars.slug, name: vars.slug, accessible: false, parent: vars.inherit === true ? (vars.from ?? null) : null, personal: false, details: { archivedAt: null, secretCount: 0 } },
389
+ {
390
+ slug: vars.slug,
391
+ name: vars.slug,
392
+ accessible: false,
393
+ parent: vars.inherit === true ? (vars.from ?? null) : null,
394
+ // A fork of a protected environment, however made, is protected from its first moment.
395
+ protected: vars.from !== undefined && environments.some((environment) => environment.slug === vars.from && environment.protected),
396
+ personal: false,
397
+ details: { archivedAt: null, secretCount: 0 },
398
+ },
390
399
  ],
391
400
  affects: () => affects.places(),
392
401
  run: async (vars) => {
@@ -419,6 +428,25 @@ export function renameEnvironment(client: CoffreClient, project: string): Change
419
428
  };
420
429
  }
421
430
 
431
+ /**
432
+ * Protect an environment, or take its protection off (docs/design/roles.md):
433
+ * shown on its card at once, and the card says if the server refuses. Only
434
+ * an Owner of the whole instance unprotects one, which the page offers to
435
+ * them alone. What everyone holds there changes, so the roles' readers are
436
+ * refetched too.
437
+ */
438
+ export function protectEnvironment(client: CoffreClient, project: string): Change<Projects, Environment, { slug: string; protected: boolean }, unknown> {
439
+ return {
440
+ list: environmentsList(project),
441
+ label: (vars) => `environment ${vars.slug}`,
442
+ targets: (vars) => [saving(environmentId(project, vars.slug))],
443
+ apply: (environments, vars) =>
444
+ environments.map((environment) => (environment.slug === vars.slug ? { ...environment, protected: vars.protected } : environment)),
445
+ affects: () => [...affects.places(), ...affects.roles()],
446
+ run: (vars) => client.environments.protect(`${project}/${vars.slug}`, vars.protected),
447
+ };
448
+ }
449
+
422
450
  /**
423
451
  * Archive an environment or bring it back. Archiving ends every read of it,
424
452
  * so it waits for the server, as a removal does; restoring shows at once.
@@ -520,10 +548,11 @@ export function saveSecrets(
520
548
  const now = new Date().toISOString();
521
549
  // A value typed by hand has no recipe; a generated one has the one it was made by. A plain one shows as it is.
522
550
  const generations = generationsOf(vars.active, vars.drafts, vars.changes);
523
- const plains = plainValuesOf(vars.active, vars.drafts, vars.changes);
551
+ const levels = levelsOf(vars.active, vars.drafts, vars.changes);
524
552
  const bumped = (entry: SecretKey | undefined, key: string): SecretKey => {
525
553
  // In a fork, a value written over the parent's is an override, whose own version the list read back says.
526
554
  const inherited = entry?.parent != null && entry.parent.state !== 'overridden' && entry.key === key;
555
+ const level = levels.get(key);
527
556
  return {
528
557
  key,
529
558
  // A rename keeps its folder; a new secret is in none.
@@ -537,7 +566,11 @@ export function saveSecrets(
537
566
  updatedAt: now,
538
567
  updatedBy: author,
539
568
  generated: generations.get(key) ?? null,
540
- value: plains.get(key) ?? null,
569
+ value: level?.value ?? null,
570
+ sensitivity: level?.sensitivity ?? 'secret',
571
+ // Until the list is read back: a plain value as it is listed, a secret over one its writer read here, and nothing else.
572
+ readable: level?.value != null
573
+ || ((level?.sensitivity ?? 'secret') === 'secret' && !inherited && entry?.sensitivity === 'secret' && entry.readable),
541
574
  parent: entry?.parent == null || entry.key !== key ? null : { ...entry.parent, state: 'overridden', changed: false, reset: false },
542
575
  };
543
576
  };
@@ -547,7 +580,11 @@ export function saveSecrets(
547
580
  if (change === undefined || change.archived) return entry;
548
581
  const key = change.key.trim();
549
582
  if (key !== entry.key) renames.set(entry.key, key);
550
- return change.value === null ? { ...entry, key } : bumped(entry, key);
583
+ if (change.value !== null) return bumped(entry, key);
584
+ // A mark: the same value, as a new version, stricter, so a plain one no longer listed, nor read until the list says so.
585
+ return change.sensitivity === undefined
586
+ ? { ...entry, key }
587
+ : { ...entry, key, version: (entry.version ?? 0) + 1, versionId: null, updatedAt: now, updatedBy: author, value: null, sensitivity: change.sensitivity, readable: false };
551
588
  });
552
589
  // A rename moves no value: a pair it touches names the new key, in both halves.
553
590
  next = renamePairs(next, renames);
@@ -644,8 +681,9 @@ export function regenerateSecret(
644
681
  updatedAt: now,
645
682
  updatedBy: author,
646
683
  generated: vars.generated,
647
- // A generated value is a secret.
684
+ // A generated value is a secret, and sensitive where the key is.
648
685
  value: null,
686
+ sensitivity: entry.sensitivity === 'sensitive' ? 'sensitive' : 'secret',
649
687
  parent: entry.parent == null ? null : { ...entry.parent, state: 'overridden', changed: false, reset: false },
650
688
  };
651
689
  });
@@ -1,85 +1,11 @@
1
- import { ROLES, type Role } from '@coffre/core/access';
1
+ import type { GrantRow, ProjectSummary } from '../shared/models';
2
2
 
3
- import type { GrantRow, ProjectSummary, RoleRow } from '../shared/models';
4
-
5
- /** The roles the project's access picker offers, a product choice; others show by their names. */
6
- export type ProjectAccessRole = Extract<Role, 'owner' | 'viewer' | 'developer'>;
7
-
8
- export type ProjectAccessOption = {
9
- value: string;
10
- role: ProjectAccessRole;
11
- environmentSlug: string | null;
12
- label: string;
13
- };
14
-
15
- export function projectAccessOptions(
16
- environments: ProjectSummary['environments'],
17
- ): ProjectAccessOption[] {
18
- const options: ProjectAccessOption[] = [
19
- {
20
- value: 'owner:',
21
- role: 'owner',
22
- environmentSlug: null,
23
- label: ROLES.owner.name,
24
- },
25
- ];
26
-
27
- for (const role of ['viewer', 'developer'] as const) {
28
- options.push({
29
- value: `${role}:`,
30
- role,
31
- environmentSlug: null,
32
- label: `${ROLES[role].name}: all`,
33
- });
34
- }
35
-
36
- for (const environment of environments) {
37
- if (environment.details === null || environment.details.archivedAt !== null) continue;
38
- for (const role of ['viewer', 'developer'] as const) {
39
- options.push({
40
- value: `${role}:${environment.slug}`,
41
- role,
42
- environmentSlug: environment.slug,
43
- label: `${ROLES[role].name}: ${environment.slug}`,
44
- });
45
- }
46
- }
47
-
48
- return options;
49
- }
50
-
51
- export function parseProjectAccess(value: string): {
52
- role: ProjectAccessRole;
53
- environmentSlug: string | null;
54
- } {
55
- const [role, environmentSlug = ''] = value.split(':', 2);
56
- if (role !== 'owner' && role !== 'viewer' && role !== 'developer') {
57
- throw new Error('unknown project access level');
58
- }
59
- return { role, environmentSlug: environmentSlug || null };
60
- }
61
-
62
- /** "Owner", "Viewer: all" or "Developer: prod": a grant by its role's name, and where. */
3
+ /** "Owner", "Viewer: all" or "Developer: prod": a grant by its role's name, as the instance names it, and where. */
63
4
  export function projectAccessLabel(
64
5
  grant: Pick<GrantRow, 'role' | 'roleName' | 'environmentSlug'>,
65
6
  ): string {
66
- const name = Object.hasOwn(ROLES, grant.role) ? ROLES[grant.role as Role].name : grant.roleName;
67
- if (grant.role === 'owner') return name;
68
- return `${name}: ${grant.environmentSlug ?? 'all'}`;
69
- }
70
-
71
- export function projectAccessRoles(
72
- roles: RoleRow[],
73
- environmentSlug: string | null,
74
- currentRole?: string,
75
- ): RoleRow[] {
76
- const allowed = new Set(
77
- environmentSlug === null
78
- ? ['owner', 'viewer', 'developer']
79
- : ['viewer', 'developer'],
80
- );
81
- if (currentRole) allowed.add(currentRole);
82
- return roles.filter((role) => allowed.has(role.slug));
7
+ if (grant.role === 'owner') return grant.roleName;
8
+ return `${grant.roleName}: ${grant.environmentSlug ?? 'all'}`;
83
9
  }
84
10
 
85
11
  /** A project on someone's Access tab: their grants there, or why those could not be read. */
@@ -64,6 +64,7 @@ export const keys = {
64
64
  audit: ['audit'],
65
65
  auditKeys: ['audit', 'keys'],
66
66
  settings: ['settings'],
67
+ roles: ['roles'],
67
68
  } satisfies Record<string, QueryKey | ((...args: never[]) => QueryKey)>;
68
69
 
69
70
  /** Members' grants, as rows, and a row for each one whose instance role reaches the place listed. */
@@ -291,6 +292,14 @@ export const queries = {
291
292
  queryFn: () => (allowed ? uiResult(() => client.settings.get()) : Promise.resolve(null)),
292
293
  }),
293
294
 
295
+ /**
296
+ * The instance's roles and instance roles, as an Owner edited and made
297
+ * them (docs/design/roles.md), and whether the caller edits them. Any
298
+ * member reads them: they are what every grant picker offers.
299
+ */
300
+ roles: (client: CoffreClient) =>
301
+ queryOptions({ queryKey: keys.roles, queryFn: () => uiResult(() => client.roles.list()) }),
302
+
294
303
  /** A page of the log, as filtered: always read fresh. */
295
304
  auditEntries: (client: CoffreClient, search: AuditSearch) =>
296
305
  queryOptions({
@@ -494,6 +503,7 @@ export async function loadSettings(queryClient: QueryClient, client: CoffreClien
494
503
  loadDirectory(queryClient, client),
495
504
  queryClient.fetchQuery(queries.settings(client, shell.capabilities.runsInstance)),
496
505
  queryClient.fetchQuery(queries.auditKeys(client, shell.capabilities.runsInstance)),
506
+ queryClient.fetchQuery(queries.roles(client)),
497
507
  ]);
498
508
  }
499
509
 
@@ -674,6 +684,12 @@ export const affects = {
674
684
  * may do, which the directory and `/me` say.
675
685
  */
676
686
  settings: (): QueryKey[] => [keys.settings, keys.me, keys.directory],
687
+ /**
688
+ * A role edited, made, reset or deleted, or an environment protected:
689
+ * what everyone holds may change, so the roles, what you hold, and every
690
+ * grant and environment read with what it holds.
691
+ */
692
+ roles: (): QueryKey[] => [keys.roles, keys.me, keys.projects, ['grants'], ['member-access'], ['secrets']],
677
693
  credentials: (member: string): QueryKey[] => [keys.credentials(member)],
678
694
  bindings: (member: string): QueryKey[] => [keys.bindings(member)],
679
695
  /** Unlinking an account also ends the sessions it signed in. */