pulse-hub 3.5.0 → 3.6.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.
- package/CHANGELOG.md +6 -0
- package/backend/.env.example +52 -52
- package/backend/agent.js +372 -372
- package/backend/cli.js +23 -1
- package/backend/emails/env-backup.html +52 -52
- package/backend/emails/invite.html +53 -53
- package/backend/lib/agent-rpc.js +72 -72
- package/backend/lib/async-route.js +17 -17
- package/backend/lib/authz/bootstrap.js +35 -35
- package/backend/lib/authz/catalog.js +291 -291
- package/backend/lib/authz/delegation.js +104 -104
- package/backend/lib/authz/effective.js +104 -104
- package/backend/lib/authz/engine.js +251 -251
- package/backend/lib/authz/index.js +134 -134
- package/backend/lib/authz/migrate.js +871 -871
- package/backend/lib/authz/mutate.js +87 -87
- package/backend/lib/authz/preview.js +327 -327
- package/backend/lib/authz/scope.js +282 -282
- package/backend/lib/env-mask.js +131 -131
- package/backend/lib/invite-helpers.js +83 -83
- package/backend/lib/invites.js +160 -160
- package/backend/lib/logs.js +244 -244
- package/backend/lib/metrics.js +249 -249
- package/backend/lib/relay-state.js +564 -564
- package/backend/lib/session.js +48 -48
- package/backend/middleware/authz.js +65 -65
- package/backend/public/assets/favicon.svg +6 -6
- package/backend/public/assets/index.js +1 -1
- package/backend/public/index.html +23 -24
- package/backend/routes/access.js +123 -123
- package/backend/routes/audit.js +41 -41
- package/backend/routes/auth.js +207 -207
- package/backend/routes/invites-public.js +91 -91
- package/backend/routes/invites.js +121 -121
- package/backend/routes/logs.js +68 -68
- package/backend/routes/metrics.js +76 -76
- package/backend/routes/policies.js +240 -240
- package/backend/routes/teams.js +221 -221
- package/backend/routes/users.js +100 -100
- package/backend/seed-admins.js +68 -68
- package/backend/server.js +106 -106
- package/backend/ws/agent-relay.js +159 -159
- package/package.json +1 -1
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
// Impact preview for access changes — runs the REAL scope-matching code over an in-memory
|
|
2
|
-
// copy of the whole authorization state (authz-store.loadState()) before and after a
|
|
3
|
-
// hypothetical change, and diffs each person's effective permission cells.
|
|
4
|
-
//
|
|
5
|
-
// Cell = (action, instanceId, service signature, env-file signature). Rules are unioned, so
|
|
6
|
-
// a narrower cell is dropped whenever an unrestricted cell exists for the same
|
|
7
|
-
// (action, instance). Root users bypass every check and are never part of a diff.
|
|
8
|
-
const { db } = require('../db');
|
|
9
|
-
const store = require('../authz-store');
|
|
10
|
-
const { getAction, ACTIONS } = require('./catalog');
|
|
11
|
-
const { serverMatches } = require('./scope');
|
|
12
|
-
const { canDelegate } = require('./delegation');
|
|
13
|
-
|
|
14
|
-
const CHANGE_KINDS = [
|
|
15
|
-
'team.members.set',
|
|
16
|
-
'team.policies.set',
|
|
17
|
-
'policy.update',
|
|
18
|
-
'policy.delete',
|
|
19
|
-
'team.delete',
|
|
20
|
-
'access-tags.set',
|
|
21
|
-
'invite.create',
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
function closureOf(action) {
|
|
25
|
-
const out = new Set([action]);
|
|
26
|
-
const walk = (a) => {
|
|
27
|
-
const def = getAction(a);
|
|
28
|
-
if (def && def.implies) {
|
|
29
|
-
def.implies.forEach((i) => {
|
|
30
|
-
if (!out.has(i)) {
|
|
31
|
-
out.add(i);
|
|
32
|
-
walk(i);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
walk(action);
|
|
38
|
-
return out;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function sigOf(def, scope) {
|
|
42
|
-
if (def.scopeKind === 'org' || def.scopeKind === 'server') return '';
|
|
43
|
-
const svc =
|
|
44
|
-
scope && scope.services && scope.services.matchers.length > 0
|
|
45
|
-
? JSON.stringify(scope.services.matchers.map((m) => `${m.op}:${m.value}`).sort())
|
|
46
|
-
: '*';
|
|
47
|
-
if (def.scopeKind === 'service') return `svc=${svc}`;
|
|
48
|
-
const env = scope && scope.envFiles ? JSON.stringify([...scope.envFiles.ids].sort()) : '*';
|
|
49
|
-
return `svc=${svc};env=${env}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function rulesForUser(state, userId) {
|
|
53
|
-
const teamIds = state.teamMembers.filter((m) => m.user_id === userId).map((m) => m.team_id);
|
|
54
|
-
const policyIds = new Set(
|
|
55
|
-
state.teamPolicies.filter((tp) => teamIds.includes(tp.team_id)).map((tp) => tp.policy_id),
|
|
56
|
-
);
|
|
57
|
-
return state.policyRules.filter((r) => policyIds.has(r.policy_id));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function cellsFor(state, rules) {
|
|
61
|
-
const tagsOf = new Map();
|
|
62
|
-
for (const t of state.instanceAuthTags) {
|
|
63
|
-
if (!tagsOf.has(t.instance_id)) tagsOf.set(t.instance_id, []);
|
|
64
|
-
tagsOf.get(t.instance_id).push(t.tag);
|
|
65
|
-
}
|
|
66
|
-
const authTagsOf = (id) => tagsOf.get(id) || [];
|
|
67
|
-
const cells = new Map();
|
|
68
|
-
const add = (action, instanceId, sig) => {
|
|
69
|
-
const key = `${action}\u0000${instanceId || ''}\u0000${sig}`;
|
|
70
|
-
cells.set(key, { action, instanceId: instanceId || null, sig });
|
|
71
|
-
};
|
|
72
|
-
for (const rule of rules) {
|
|
73
|
-
const def = getAction(rule.action);
|
|
74
|
-
if (!def) continue;
|
|
75
|
-
for (const action of closureOf(rule.action)) {
|
|
76
|
-
const adef = getAction(action);
|
|
77
|
-
if (adef.scopeKind === 'org') {
|
|
78
|
-
add(action, null, '');
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
if (!rule.scope || !rule.scope.servers) continue;
|
|
82
|
-
for (const instanceId of state.instances) {
|
|
83
|
-
if (serverMatches(rule.scope.servers, { id: instanceId }, authTagsOf)) {
|
|
84
|
-
add(action, instanceId, sigOf(adef, rule.scope));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
// Union semantics: an unrestricted cell subsumes any narrower one for the same target.
|
|
90
|
-
const WIDE = ['svc=*', 'svc=*;env=*'];
|
|
91
|
-
for (const [key, c] of [...cells]) {
|
|
92
|
-
if (c.sig === '' || WIDE.includes(c.sig)) continue;
|
|
93
|
-
if (WIDE.some((s) => cells.has(`${c.action}\u0000${c.instanceId || ''}\u0000${s}`))) {
|
|
94
|
-
cells.delete(key);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return cells;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function effectiveMatrix(state, rootIds) {
|
|
101
|
-
const out = new Map();
|
|
102
|
-
for (const u of state.users) {
|
|
103
|
-
if (rootIds.has(u.id)) continue;
|
|
104
|
-
out.set(u.id, cellsFor(state, rulesForUser(state, u.id)));
|
|
105
|
-
}
|
|
106
|
-
return out;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function cloneState(state) {
|
|
110
|
-
return {
|
|
111
|
-
...state,
|
|
112
|
-
teams: [...state.teams],
|
|
113
|
-
teamMembers: [...state.teamMembers],
|
|
114
|
-
policies: [...state.policies],
|
|
115
|
-
policyRules: [...state.policyRules],
|
|
116
|
-
teamPolicies: [...state.teamPolicies],
|
|
117
|
-
instances: [...state.instances],
|
|
118
|
-
instanceAuthTags: [...state.instanceAuthTags],
|
|
119
|
-
users: [...state.users],
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Pure: returns a new state with the change applied (never touches the DB).
|
|
124
|
-
function applyChange(state, change) {
|
|
125
|
-
const s = cloneState(state);
|
|
126
|
-
switch (change.kind) {
|
|
127
|
-
case 'team.members.set':
|
|
128
|
-
s.teamMembers = s.teamMembers.filter((m) => m.team_id !== change.teamId);
|
|
129
|
-
for (const userId of change.userIds || [])
|
|
130
|
-
s.teamMembers.push({ team_id: change.teamId, user_id: userId });
|
|
131
|
-
break;
|
|
132
|
-
case 'team.policies.set':
|
|
133
|
-
s.teamPolicies = s.teamPolicies.filter((tp) => tp.team_id !== change.teamId);
|
|
134
|
-
for (const policyId of change.policyIds || [])
|
|
135
|
-
s.teamPolicies.push({ team_id: change.teamId, policy_id: policyId });
|
|
136
|
-
break;
|
|
137
|
-
case 'policy.update':
|
|
138
|
-
s.policyRules = s.policyRules.filter((r) => r.policy_id !== change.policyId);
|
|
139
|
-
(change.rules || []).forEach((r, i) =>
|
|
140
|
-
s.policyRules.push({
|
|
141
|
-
id: `preview-${i}`,
|
|
142
|
-
policy_id: change.policyId,
|
|
143
|
-
action: r.action,
|
|
144
|
-
scope: r.scope || null,
|
|
145
|
-
}),
|
|
146
|
-
);
|
|
147
|
-
break;
|
|
148
|
-
case 'policy.delete':
|
|
149
|
-
s.policyRules = s.policyRules.filter((r) => r.policy_id !== change.policyId);
|
|
150
|
-
s.teamPolicies = s.teamPolicies.filter((tp) => tp.policy_id !== change.policyId);
|
|
151
|
-
s.policies = s.policies.filter((p) => p.id !== change.policyId);
|
|
152
|
-
break;
|
|
153
|
-
case 'team.delete':
|
|
154
|
-
s.teamMembers = s.teamMembers.filter((m) => m.team_id !== change.teamId);
|
|
155
|
-
s.teamPolicies = s.teamPolicies.filter((tp) => tp.team_id !== change.teamId);
|
|
156
|
-
s.teams = s.teams.filter((t) => t.id !== change.teamId);
|
|
157
|
-
break;
|
|
158
|
-
case 'access-tags.set':
|
|
159
|
-
s.instanceAuthTags = s.instanceAuthTags.filter((t) => t.instance_id !== change.instanceId);
|
|
160
|
-
for (const tag of change.tags || [])
|
|
161
|
-
s.instanceAuthTags.push({ instance_id: change.instanceId, tag });
|
|
162
|
-
break;
|
|
163
|
-
case 'invite.create': {
|
|
164
|
-
const id = `invite:${change.email}`;
|
|
165
|
-
s.users.push({ id, email: change.email });
|
|
166
|
-
for (const teamId of change.teamIds || [])
|
|
167
|
-
s.teamMembers.push({ team_id: teamId, user_id: id });
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
default:
|
|
171
|
-
throw new Error(`Unknown change kind: ${change.kind}`);
|
|
172
|
-
}
|
|
173
|
-
return s;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function rulesOfPolicies(state, policyIds) {
|
|
177
|
-
const set = new Set(policyIds);
|
|
178
|
-
return state.policyRules
|
|
179
|
-
.filter((r) => set.has(r.policy_id))
|
|
180
|
-
.map((r) => ({ action: r.action, scope: r.scope }));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function ruleKey(r) {
|
|
184
|
-
return `${r.action}|${JSON.stringify(r.scope || null)}`;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// The rules a change would hand to people, which the actor must be able to delegate (a
|
|
188
|
-
// privilege-escalation guard — see delegation.js). Removing access is always allowed.
|
|
189
|
-
function rulesToDelegate(state, change) {
|
|
190
|
-
switch (change.kind) {
|
|
191
|
-
case 'team.members.set': {
|
|
192
|
-
const current = new Set(
|
|
193
|
-
state.teamMembers.filter((m) => m.team_id === change.teamId).map((m) => m.user_id),
|
|
194
|
-
);
|
|
195
|
-
const added = (change.userIds || []).some((id) => !current.has(id));
|
|
196
|
-
if (!added) return [];
|
|
197
|
-
const policyIds = state.teamPolicies
|
|
198
|
-
.filter((tp) => tp.team_id === change.teamId)
|
|
199
|
-
.map((tp) => tp.policy_id);
|
|
200
|
-
return rulesOfPolicies(state, policyIds);
|
|
201
|
-
}
|
|
202
|
-
case 'team.policies.set': {
|
|
203
|
-
const current = new Set(
|
|
204
|
-
state.teamPolicies.filter((tp) => tp.team_id === change.teamId).map((tp) => tp.policy_id),
|
|
205
|
-
);
|
|
206
|
-
return rulesOfPolicies(
|
|
207
|
-
state,
|
|
208
|
-
(change.policyIds || []).filter((id) => !current.has(id)),
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
case 'policy.update': {
|
|
212
|
-
const existing = new Set(
|
|
213
|
-
state.policyRules.filter((r) => r.policy_id === change.policyId).map(ruleKey),
|
|
214
|
-
);
|
|
215
|
-
return (change.rules || [])
|
|
216
|
-
.map((r) => ({ action: r.action, scope: r.scope || null }))
|
|
217
|
-
.filter((r) => !existing.has(ruleKey(r)));
|
|
218
|
-
}
|
|
219
|
-
case 'invite.create': {
|
|
220
|
-
const policyIds = state.teamPolicies
|
|
221
|
-
.filter((tp) => (change.teamIds || []).includes(tp.team_id))
|
|
222
|
-
.map((tp) => tp.policy_id);
|
|
223
|
-
return rulesOfPolicies(state, policyIds);
|
|
224
|
-
}
|
|
225
|
-
default:
|
|
226
|
-
return [];
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function delegationErrors(actorSubject, state, change) {
|
|
231
|
-
const rules = rulesToDelegate(state, change);
|
|
232
|
-
if (rules.length === 0) return { ok: true, denied: [] };
|
|
233
|
-
const r = canDelegate(actorSubject, rules);
|
|
234
|
-
return r.ok ? { ok: true, denied: [] } : { ok: false, denied: r.denied };
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function groupBy(cells) {
|
|
238
|
-
const byAction = new Map();
|
|
239
|
-
for (const c of cells) {
|
|
240
|
-
if (!byAction.has(c.action))
|
|
241
|
-
byAction.set(c.action, { instanceIds: new Set(), sigs: new Set() });
|
|
242
|
-
const g = byAction.get(c.action);
|
|
243
|
-
if (c.instanceId) g.instanceIds.add(c.instanceId);
|
|
244
|
-
if (c.sig) g.sigs.add(c.sig);
|
|
245
|
-
}
|
|
246
|
-
return [...byAction].map(([action, g]) => ({
|
|
247
|
-
action,
|
|
248
|
-
instanceIds: [...g.instanceIds],
|
|
249
|
-
detail: [...g.sigs].join(' · ') || null,
|
|
250
|
-
}));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function labelOf(state, userId) {
|
|
254
|
-
const u = state.users.find((x) => x.id === userId);
|
|
255
|
-
return u ? u.email || userId : userId;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// preview({change, actorSubject}) → PreviewResult
|
|
259
|
-
function preview(change, actorSubject) {
|
|
260
|
-
if (!CHANGE_KINDS.includes(change.kind)) throw new Error(`Unknown change kind: ${change.kind}`);
|
|
261
|
-
const before = store.loadState();
|
|
262
|
-
const rootIds = new Set(
|
|
263
|
-
db
|
|
264
|
-
.prepare('SELECT id FROM users WHERE is_root = 1')
|
|
265
|
-
.all()
|
|
266
|
-
.map((r) => r.id),
|
|
267
|
-
);
|
|
268
|
-
const after = applyChange(before, change);
|
|
269
|
-
const mBefore = effectiveMatrix(before, rootIds);
|
|
270
|
-
const mAfter = effectiveMatrix(after, rootIds);
|
|
271
|
-
|
|
272
|
-
const people = [];
|
|
273
|
-
let gaining = 0;
|
|
274
|
-
let losing = 0;
|
|
275
|
-
let powerful = false;
|
|
276
|
-
const sentences = [];
|
|
277
|
-
const label = (a) => (getAction(a) ? getAction(a).label : a);
|
|
278
|
-
|
|
279
|
-
for (const [userId, afterCells] of mAfter) {
|
|
280
|
-
const beforeCells = mBefore.get(userId) || new Map();
|
|
281
|
-
const gainCells = [...afterCells].filter(([k]) => !beforeCells.has(k)).map(([, c]) => c);
|
|
282
|
-
const lossCells = [...beforeCells].filter(([k]) => !afterCells.has(k)).map(([, c]) => c);
|
|
283
|
-
if (gainCells.length === 0 && lossCells.length === 0) continue;
|
|
284
|
-
const gains = groupBy(gainCells);
|
|
285
|
-
const losses = groupBy(lossCells);
|
|
286
|
-
if (gains.length) gaining++;
|
|
287
|
-
if (losses.length) losing++;
|
|
288
|
-
if (gains.some((g) => (getAction(g.action) || {}).powerful)) powerful = true;
|
|
289
|
-
const who = labelOf(after, userId);
|
|
290
|
-
people.push({ userId, label: who, gains, losses });
|
|
291
|
-
const part = (list) =>
|
|
292
|
-
list
|
|
293
|
-
.map(
|
|
294
|
-
(g) =>
|
|
295
|
-
`${label(g.action)}${g.instanceIds.length ? ` on ${g.instanceIds.length} server(s)` : ''}`,
|
|
296
|
-
)
|
|
297
|
-
.join(', ');
|
|
298
|
-
if (gains.length) sentences.push(`${who} would gain: ${part(gains)}.`);
|
|
299
|
-
if (losses.length) sentences.push(`${who} would lose: ${part(losses)}.`);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
const deleg = actorSubject ? delegationErrors(actorSubject, before, change) : { ok: true };
|
|
303
|
-
const result = {
|
|
304
|
-
revision: store.getAuthzRevision(),
|
|
305
|
-
counts: { affected: people.length, gaining, losing },
|
|
306
|
-
people,
|
|
307
|
-
sentences,
|
|
308
|
-
powerful,
|
|
309
|
-
};
|
|
310
|
-
if (!deleg.ok) {
|
|
311
|
-
result.errors = deleg.denied.map(
|
|
312
|
-
(d) =>
|
|
313
|
-
`You cannot grant "${label((d.rule || {}).action)}" with that scope — you don't hold it.`,
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
return result;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
module.exports = {
|
|
320
|
-
CHANGE_KINDS,
|
|
321
|
-
ACTIONS,
|
|
322
|
-
preview,
|
|
323
|
-
applyChange,
|
|
324
|
-
effectiveMatrix,
|
|
325
|
-
rulesToDelegate,
|
|
326
|
-
delegationErrors,
|
|
327
|
-
};
|
|
1
|
+
// Impact preview for access changes — runs the REAL scope-matching code over an in-memory
|
|
2
|
+
// copy of the whole authorization state (authz-store.loadState()) before and after a
|
|
3
|
+
// hypothetical change, and diffs each person's effective permission cells.
|
|
4
|
+
//
|
|
5
|
+
// Cell = (action, instanceId, service signature, env-file signature). Rules are unioned, so
|
|
6
|
+
// a narrower cell is dropped whenever an unrestricted cell exists for the same
|
|
7
|
+
// (action, instance). Root users bypass every check and are never part of a diff.
|
|
8
|
+
const { db } = require('../db');
|
|
9
|
+
const store = require('../authz-store');
|
|
10
|
+
const { getAction, ACTIONS } = require('./catalog');
|
|
11
|
+
const { serverMatches } = require('./scope');
|
|
12
|
+
const { canDelegate } = require('./delegation');
|
|
13
|
+
|
|
14
|
+
const CHANGE_KINDS = [
|
|
15
|
+
'team.members.set',
|
|
16
|
+
'team.policies.set',
|
|
17
|
+
'policy.update',
|
|
18
|
+
'policy.delete',
|
|
19
|
+
'team.delete',
|
|
20
|
+
'access-tags.set',
|
|
21
|
+
'invite.create',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function closureOf(action) {
|
|
25
|
+
const out = new Set([action]);
|
|
26
|
+
const walk = (a) => {
|
|
27
|
+
const def = getAction(a);
|
|
28
|
+
if (def && def.implies) {
|
|
29
|
+
def.implies.forEach((i) => {
|
|
30
|
+
if (!out.has(i)) {
|
|
31
|
+
out.add(i);
|
|
32
|
+
walk(i);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
walk(action);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function sigOf(def, scope) {
|
|
42
|
+
if (def.scopeKind === 'org' || def.scopeKind === 'server') return '';
|
|
43
|
+
const svc =
|
|
44
|
+
scope && scope.services && scope.services.matchers.length > 0
|
|
45
|
+
? JSON.stringify(scope.services.matchers.map((m) => `${m.op}:${m.value}`).sort())
|
|
46
|
+
: '*';
|
|
47
|
+
if (def.scopeKind === 'service') return `svc=${svc}`;
|
|
48
|
+
const env = scope && scope.envFiles ? JSON.stringify([...scope.envFiles.ids].sort()) : '*';
|
|
49
|
+
return `svc=${svc};env=${env}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function rulesForUser(state, userId) {
|
|
53
|
+
const teamIds = state.teamMembers.filter((m) => m.user_id === userId).map((m) => m.team_id);
|
|
54
|
+
const policyIds = new Set(
|
|
55
|
+
state.teamPolicies.filter((tp) => teamIds.includes(tp.team_id)).map((tp) => tp.policy_id),
|
|
56
|
+
);
|
|
57
|
+
return state.policyRules.filter((r) => policyIds.has(r.policy_id));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function cellsFor(state, rules) {
|
|
61
|
+
const tagsOf = new Map();
|
|
62
|
+
for (const t of state.instanceAuthTags) {
|
|
63
|
+
if (!tagsOf.has(t.instance_id)) tagsOf.set(t.instance_id, []);
|
|
64
|
+
tagsOf.get(t.instance_id).push(t.tag);
|
|
65
|
+
}
|
|
66
|
+
const authTagsOf = (id) => tagsOf.get(id) || [];
|
|
67
|
+
const cells = new Map();
|
|
68
|
+
const add = (action, instanceId, sig) => {
|
|
69
|
+
const key = `${action}\u0000${instanceId || ''}\u0000${sig}`;
|
|
70
|
+
cells.set(key, { action, instanceId: instanceId || null, sig });
|
|
71
|
+
};
|
|
72
|
+
for (const rule of rules) {
|
|
73
|
+
const def = getAction(rule.action);
|
|
74
|
+
if (!def) continue;
|
|
75
|
+
for (const action of closureOf(rule.action)) {
|
|
76
|
+
const adef = getAction(action);
|
|
77
|
+
if (adef.scopeKind === 'org') {
|
|
78
|
+
add(action, null, '');
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!rule.scope || !rule.scope.servers) continue;
|
|
82
|
+
for (const instanceId of state.instances) {
|
|
83
|
+
if (serverMatches(rule.scope.servers, { id: instanceId }, authTagsOf)) {
|
|
84
|
+
add(action, instanceId, sigOf(adef, rule.scope));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Union semantics: an unrestricted cell subsumes any narrower one for the same target.
|
|
90
|
+
const WIDE = ['svc=*', 'svc=*;env=*'];
|
|
91
|
+
for (const [key, c] of [...cells]) {
|
|
92
|
+
if (c.sig === '' || WIDE.includes(c.sig)) continue;
|
|
93
|
+
if (WIDE.some((s) => cells.has(`${c.action}\u0000${c.instanceId || ''}\u0000${s}`))) {
|
|
94
|
+
cells.delete(key);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return cells;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function effectiveMatrix(state, rootIds) {
|
|
101
|
+
const out = new Map();
|
|
102
|
+
for (const u of state.users) {
|
|
103
|
+
if (rootIds.has(u.id)) continue;
|
|
104
|
+
out.set(u.id, cellsFor(state, rulesForUser(state, u.id)));
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function cloneState(state) {
|
|
110
|
+
return {
|
|
111
|
+
...state,
|
|
112
|
+
teams: [...state.teams],
|
|
113
|
+
teamMembers: [...state.teamMembers],
|
|
114
|
+
policies: [...state.policies],
|
|
115
|
+
policyRules: [...state.policyRules],
|
|
116
|
+
teamPolicies: [...state.teamPolicies],
|
|
117
|
+
instances: [...state.instances],
|
|
118
|
+
instanceAuthTags: [...state.instanceAuthTags],
|
|
119
|
+
users: [...state.users],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Pure: returns a new state with the change applied (never touches the DB).
|
|
124
|
+
function applyChange(state, change) {
|
|
125
|
+
const s = cloneState(state);
|
|
126
|
+
switch (change.kind) {
|
|
127
|
+
case 'team.members.set':
|
|
128
|
+
s.teamMembers = s.teamMembers.filter((m) => m.team_id !== change.teamId);
|
|
129
|
+
for (const userId of change.userIds || [])
|
|
130
|
+
s.teamMembers.push({ team_id: change.teamId, user_id: userId });
|
|
131
|
+
break;
|
|
132
|
+
case 'team.policies.set':
|
|
133
|
+
s.teamPolicies = s.teamPolicies.filter((tp) => tp.team_id !== change.teamId);
|
|
134
|
+
for (const policyId of change.policyIds || [])
|
|
135
|
+
s.teamPolicies.push({ team_id: change.teamId, policy_id: policyId });
|
|
136
|
+
break;
|
|
137
|
+
case 'policy.update':
|
|
138
|
+
s.policyRules = s.policyRules.filter((r) => r.policy_id !== change.policyId);
|
|
139
|
+
(change.rules || []).forEach((r, i) =>
|
|
140
|
+
s.policyRules.push({
|
|
141
|
+
id: `preview-${i}`,
|
|
142
|
+
policy_id: change.policyId,
|
|
143
|
+
action: r.action,
|
|
144
|
+
scope: r.scope || null,
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
break;
|
|
148
|
+
case 'policy.delete':
|
|
149
|
+
s.policyRules = s.policyRules.filter((r) => r.policy_id !== change.policyId);
|
|
150
|
+
s.teamPolicies = s.teamPolicies.filter((tp) => tp.policy_id !== change.policyId);
|
|
151
|
+
s.policies = s.policies.filter((p) => p.id !== change.policyId);
|
|
152
|
+
break;
|
|
153
|
+
case 'team.delete':
|
|
154
|
+
s.teamMembers = s.teamMembers.filter((m) => m.team_id !== change.teamId);
|
|
155
|
+
s.teamPolicies = s.teamPolicies.filter((tp) => tp.team_id !== change.teamId);
|
|
156
|
+
s.teams = s.teams.filter((t) => t.id !== change.teamId);
|
|
157
|
+
break;
|
|
158
|
+
case 'access-tags.set':
|
|
159
|
+
s.instanceAuthTags = s.instanceAuthTags.filter((t) => t.instance_id !== change.instanceId);
|
|
160
|
+
for (const tag of change.tags || [])
|
|
161
|
+
s.instanceAuthTags.push({ instance_id: change.instanceId, tag });
|
|
162
|
+
break;
|
|
163
|
+
case 'invite.create': {
|
|
164
|
+
const id = `invite:${change.email}`;
|
|
165
|
+
s.users.push({ id, email: change.email });
|
|
166
|
+
for (const teamId of change.teamIds || [])
|
|
167
|
+
s.teamMembers.push({ team_id: teamId, user_id: id });
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
default:
|
|
171
|
+
throw new Error(`Unknown change kind: ${change.kind}`);
|
|
172
|
+
}
|
|
173
|
+
return s;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function rulesOfPolicies(state, policyIds) {
|
|
177
|
+
const set = new Set(policyIds);
|
|
178
|
+
return state.policyRules
|
|
179
|
+
.filter((r) => set.has(r.policy_id))
|
|
180
|
+
.map((r) => ({ action: r.action, scope: r.scope }));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function ruleKey(r) {
|
|
184
|
+
return `${r.action}|${JSON.stringify(r.scope || null)}`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// The rules a change would hand to people, which the actor must be able to delegate (a
|
|
188
|
+
// privilege-escalation guard — see delegation.js). Removing access is always allowed.
|
|
189
|
+
function rulesToDelegate(state, change) {
|
|
190
|
+
switch (change.kind) {
|
|
191
|
+
case 'team.members.set': {
|
|
192
|
+
const current = new Set(
|
|
193
|
+
state.teamMembers.filter((m) => m.team_id === change.teamId).map((m) => m.user_id),
|
|
194
|
+
);
|
|
195
|
+
const added = (change.userIds || []).some((id) => !current.has(id));
|
|
196
|
+
if (!added) return [];
|
|
197
|
+
const policyIds = state.teamPolicies
|
|
198
|
+
.filter((tp) => tp.team_id === change.teamId)
|
|
199
|
+
.map((tp) => tp.policy_id);
|
|
200
|
+
return rulesOfPolicies(state, policyIds);
|
|
201
|
+
}
|
|
202
|
+
case 'team.policies.set': {
|
|
203
|
+
const current = new Set(
|
|
204
|
+
state.teamPolicies.filter((tp) => tp.team_id === change.teamId).map((tp) => tp.policy_id),
|
|
205
|
+
);
|
|
206
|
+
return rulesOfPolicies(
|
|
207
|
+
state,
|
|
208
|
+
(change.policyIds || []).filter((id) => !current.has(id)),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
case 'policy.update': {
|
|
212
|
+
const existing = new Set(
|
|
213
|
+
state.policyRules.filter((r) => r.policy_id === change.policyId).map(ruleKey),
|
|
214
|
+
);
|
|
215
|
+
return (change.rules || [])
|
|
216
|
+
.map((r) => ({ action: r.action, scope: r.scope || null }))
|
|
217
|
+
.filter((r) => !existing.has(ruleKey(r)));
|
|
218
|
+
}
|
|
219
|
+
case 'invite.create': {
|
|
220
|
+
const policyIds = state.teamPolicies
|
|
221
|
+
.filter((tp) => (change.teamIds || []).includes(tp.team_id))
|
|
222
|
+
.map((tp) => tp.policy_id);
|
|
223
|
+
return rulesOfPolicies(state, policyIds);
|
|
224
|
+
}
|
|
225
|
+
default:
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function delegationErrors(actorSubject, state, change) {
|
|
231
|
+
const rules = rulesToDelegate(state, change);
|
|
232
|
+
if (rules.length === 0) return { ok: true, denied: [] };
|
|
233
|
+
const r = canDelegate(actorSubject, rules);
|
|
234
|
+
return r.ok ? { ok: true, denied: [] } : { ok: false, denied: r.denied };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function groupBy(cells) {
|
|
238
|
+
const byAction = new Map();
|
|
239
|
+
for (const c of cells) {
|
|
240
|
+
if (!byAction.has(c.action))
|
|
241
|
+
byAction.set(c.action, { instanceIds: new Set(), sigs: new Set() });
|
|
242
|
+
const g = byAction.get(c.action);
|
|
243
|
+
if (c.instanceId) g.instanceIds.add(c.instanceId);
|
|
244
|
+
if (c.sig) g.sigs.add(c.sig);
|
|
245
|
+
}
|
|
246
|
+
return [...byAction].map(([action, g]) => ({
|
|
247
|
+
action,
|
|
248
|
+
instanceIds: [...g.instanceIds],
|
|
249
|
+
detail: [...g.sigs].join(' · ') || null,
|
|
250
|
+
}));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function labelOf(state, userId) {
|
|
254
|
+
const u = state.users.find((x) => x.id === userId);
|
|
255
|
+
return u ? u.email || userId : userId;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// preview({change, actorSubject}) → PreviewResult
|
|
259
|
+
function preview(change, actorSubject) {
|
|
260
|
+
if (!CHANGE_KINDS.includes(change.kind)) throw new Error(`Unknown change kind: ${change.kind}`);
|
|
261
|
+
const before = store.loadState();
|
|
262
|
+
const rootIds = new Set(
|
|
263
|
+
db
|
|
264
|
+
.prepare('SELECT id FROM users WHERE is_root = 1')
|
|
265
|
+
.all()
|
|
266
|
+
.map((r) => r.id),
|
|
267
|
+
);
|
|
268
|
+
const after = applyChange(before, change);
|
|
269
|
+
const mBefore = effectiveMatrix(before, rootIds);
|
|
270
|
+
const mAfter = effectiveMatrix(after, rootIds);
|
|
271
|
+
|
|
272
|
+
const people = [];
|
|
273
|
+
let gaining = 0;
|
|
274
|
+
let losing = 0;
|
|
275
|
+
let powerful = false;
|
|
276
|
+
const sentences = [];
|
|
277
|
+
const label = (a) => (getAction(a) ? getAction(a).label : a);
|
|
278
|
+
|
|
279
|
+
for (const [userId, afterCells] of mAfter) {
|
|
280
|
+
const beforeCells = mBefore.get(userId) || new Map();
|
|
281
|
+
const gainCells = [...afterCells].filter(([k]) => !beforeCells.has(k)).map(([, c]) => c);
|
|
282
|
+
const lossCells = [...beforeCells].filter(([k]) => !afterCells.has(k)).map(([, c]) => c);
|
|
283
|
+
if (gainCells.length === 0 && lossCells.length === 0) continue;
|
|
284
|
+
const gains = groupBy(gainCells);
|
|
285
|
+
const losses = groupBy(lossCells);
|
|
286
|
+
if (gains.length) gaining++;
|
|
287
|
+
if (losses.length) losing++;
|
|
288
|
+
if (gains.some((g) => (getAction(g.action) || {}).powerful)) powerful = true;
|
|
289
|
+
const who = labelOf(after, userId);
|
|
290
|
+
people.push({ userId, label: who, gains, losses });
|
|
291
|
+
const part = (list) =>
|
|
292
|
+
list
|
|
293
|
+
.map(
|
|
294
|
+
(g) =>
|
|
295
|
+
`${label(g.action)}${g.instanceIds.length ? ` on ${g.instanceIds.length} server(s)` : ''}`,
|
|
296
|
+
)
|
|
297
|
+
.join(', ');
|
|
298
|
+
if (gains.length) sentences.push(`${who} would gain: ${part(gains)}.`);
|
|
299
|
+
if (losses.length) sentences.push(`${who} would lose: ${part(losses)}.`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const deleg = actorSubject ? delegationErrors(actorSubject, before, change) : { ok: true };
|
|
303
|
+
const result = {
|
|
304
|
+
revision: store.getAuthzRevision(),
|
|
305
|
+
counts: { affected: people.length, gaining, losing },
|
|
306
|
+
people,
|
|
307
|
+
sentences,
|
|
308
|
+
powerful,
|
|
309
|
+
};
|
|
310
|
+
if (!deleg.ok) {
|
|
311
|
+
result.errors = deleg.denied.map(
|
|
312
|
+
(d) =>
|
|
313
|
+
`You cannot grant "${label((d.rule || {}).action)}" with that scope — you don't hold it.`,
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
module.exports = {
|
|
320
|
+
CHANGE_KINDS,
|
|
321
|
+
ACTIONS,
|
|
322
|
+
preview,
|
|
323
|
+
applyChange,
|
|
324
|
+
effectiveMatrix,
|
|
325
|
+
rulesToDelegate,
|
|
326
|
+
delegationErrors,
|
|
327
|
+
};
|