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,251 +1,251 @@
|
|
|
1
|
-
// Core authorization engine — can(), resolveInstanceIds(), etc.
|
|
2
|
-
//
|
|
3
|
-
// The model in one paragraph: rules only ever GRANT (there are no deny rules), so rules
|
|
4
|
-
// can't conflict — a person's access is simply the union of every rule they hold. Inside
|
|
5
|
-
// one rule, every part of its scope must match (servers AND services AND env files). The
|
|
6
|
-
// one place that decides "does this rule cover this resource" is ruleCovers(); every other
|
|
7
|
-
// function here builds on it (or, for resolveInstanceIds, mirrors its server check in SQL).
|
|
8
|
-
const { db } = require('../db');
|
|
9
|
-
const { ACTIONS, getAction } = require('./catalog');
|
|
10
|
-
const { serviceMatches, serverMatches, validateRule } = require('./scope');
|
|
11
|
-
|
|
12
|
-
// For each action, the set of rule actions that satisfy a check for it: the action itself
|
|
13
|
-
// plus every action that (transitively) IMPLIES it. A rule for env.edit satisfies a check
|
|
14
|
-
// for env.view (env.edit implies env.view) — never the reverse, or a view-only grant would
|
|
15
|
-
// pass an edit check. The catalog is static, so this is computed once at load.
|
|
16
|
-
const SATISFIED_BY = new Map(
|
|
17
|
-
ACTIONS.map((def) => {
|
|
18
|
-
const out = new Set([def.id]);
|
|
19
|
-
let grew = true;
|
|
20
|
-
while (grew) {
|
|
21
|
-
grew = false;
|
|
22
|
-
for (const other of ACTIONS) {
|
|
23
|
-
if (!out.has(other.id) && (other.implies || []).some((i) => out.has(i))) {
|
|
24
|
-
out.add(other.id);
|
|
25
|
-
grew = true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return [def.id, out];
|
|
30
|
-
}),
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
function authTagsOf(instanceId) {
|
|
34
|
-
return db
|
|
35
|
-
.prepare('SELECT tag FROM instance_auth_tags WHERE instance_id = ?')
|
|
36
|
-
.all(instanceId)
|
|
37
|
-
.map((t) => t.tag);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// The subject's rules that could grant `action` (ignoring scope). Unknown action = none.
|
|
41
|
-
function rulesFor(subject, action) {
|
|
42
|
-
const satisfying = SATISFIED_BY.get(action);
|
|
43
|
-
return satisfying ? subject.rules.filter((r) => satisfying.has(r.action)) : [];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// THE matching rule. Does this one rule cover `resource` for an action of this kind?
|
|
47
|
-
// Every narrowing present on the rule must match; a narrowing the rule doesn't have
|
|
48
|
-
// doesn't restrict it. Server-level actions only ever look at servers.
|
|
49
|
-
function ruleCovers(rule, actionDef, resource) {
|
|
50
|
-
const { servers, services, envFiles } = rule.scope || {};
|
|
51
|
-
if (!servers) return false;
|
|
52
|
-
if (!serverMatches(servers, { id: resource.instanceId }, authTagsOf)) return false;
|
|
53
|
-
if (actionDef.scopeKind === 'server') return true;
|
|
54
|
-
if (services && !serviceMatches(services.matchers, resource.serviceName)) return false;
|
|
55
|
-
// A rule limited to specific env files only covers those; a check that names no env
|
|
56
|
-
// file can't be proven to be one of them, so it fails closed.
|
|
57
|
-
if (envFiles && !envFiles.ids.includes(resource.envFileId)) return false;
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Older stored rules may carry `services: {matchers: []}`, which has always meant "no
|
|
62
|
-
// service narrowing". Normalize it to the absent form so nothing downstream has to know
|
|
63
|
-
// that an empty list means "all". (New rules can't be saved this way — see validateRule.)
|
|
64
|
-
function normalizeScope(scope) {
|
|
65
|
-
if (scope && scope.services && scope.services.matchers.length === 0) {
|
|
66
|
-
const { services, ...rest } = scope;
|
|
67
|
-
return rest;
|
|
68
|
-
}
|
|
69
|
-
return scope;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Build a subject (user + their rules) for the request
|
|
73
|
-
function buildSubject(user, store) {
|
|
74
|
-
if (!user) return null;
|
|
75
|
-
|
|
76
|
-
// Root users bypass all checks
|
|
77
|
-
if (user.is_root) {
|
|
78
|
-
return { user, isRoot: true, rules: [] };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const teams = store.getUserTeams(user.id);
|
|
82
|
-
const rules = [];
|
|
83
|
-
for (const team of teams) {
|
|
84
|
-
for (const policy of store.getPoliciesForTeam(team.id)) {
|
|
85
|
-
for (const rule of store.getPolicyRules(policy.id)) {
|
|
86
|
-
// The store returns scope as a JSON string; parse it before validating/matching
|
|
87
|
-
let scope = null;
|
|
88
|
-
try {
|
|
89
|
-
scope = rule.scope ? JSON.parse(rule.scope) : null;
|
|
90
|
-
} catch (e) {
|
|
91
|
-
console.warn(`Unparseable scope in policy ${policy.id}, rule ${rule.id}; skipping`);
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
// Validate rule; skip if invalid (fails closed — a skipped rule grants nothing).
|
|
95
|
-
// Lenient mode: rules stored before the stricter API checks must keep loading.
|
|
96
|
-
const validation = validateRule({ action: rule.action, scope }, { fromMigration: true });
|
|
97
|
-
if (!validation.ok) {
|
|
98
|
-
console.warn(`Invalid rule in policy ${policy.id}: ${validation.error}`);
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
rules.push({
|
|
103
|
-
id: rule.id,
|
|
104
|
-
action: rule.action,
|
|
105
|
-
scope: normalizeScope(scope),
|
|
106
|
-
teamId: team.id,
|
|
107
|
-
teamName: team.name,
|
|
108
|
-
policyId: policy.id,
|
|
109
|
-
policyName: policy.name,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return { user, isRoot: false, rules, teams };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Check if user can perform action on resource ({instanceId, serviceName?, envFileId?})
|
|
119
|
-
function can(subject, action, resource = null) {
|
|
120
|
-
if (!subject) return false;
|
|
121
|
-
if (subject.isRoot) return true;
|
|
122
|
-
|
|
123
|
-
const actionDef = getAction(action);
|
|
124
|
-
if (!actionDef) return false; // Unknown action
|
|
125
|
-
|
|
126
|
-
const rules = rulesFor(subject, action);
|
|
127
|
-
if (actionDef.scopeKind === 'org') return rules.length > 0;
|
|
128
|
-
if (!resource) return false;
|
|
129
|
-
return rules.some((rule) => ruleCovers(rule, actionDef, resource));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Get all instance IDs a user can access for a server-level action.
|
|
133
|
-
// null = unrestricted. Mirrors ruleCovers' server check, but resolves tags in one query.
|
|
134
|
-
function resolveInstanceIds(subject, action) {
|
|
135
|
-
if (!subject) return new Set();
|
|
136
|
-
if (subject.isRoot) return null;
|
|
137
|
-
|
|
138
|
-
const actionDef = getAction(action);
|
|
139
|
-
if (!actionDef || actionDef.scopeKind !== 'server') return new Set();
|
|
140
|
-
|
|
141
|
-
const allowedIds = new Set();
|
|
142
|
-
for (const rule of rulesFor(subject, action)) {
|
|
143
|
-
const servers = rule.scope && rule.scope.servers;
|
|
144
|
-
if (!servers) continue;
|
|
145
|
-
if (servers.type === 'all') return null;
|
|
146
|
-
|
|
147
|
-
if (servers.type === 'ids') {
|
|
148
|
-
servers.ids.forEach((id) => allowedIds.add(id));
|
|
149
|
-
} else if (servers.type === 'tags') {
|
|
150
|
-
const uniqueTags = [...new Set(servers.tags)];
|
|
151
|
-
const placeholders = uniqueTags.map(() => '?').join(',');
|
|
152
|
-
const having =
|
|
153
|
-
(servers.match || 'all') === 'all'
|
|
154
|
-
? `HAVING COUNT(DISTINCT tag) = ${uniqueTags.length}`
|
|
155
|
-
: '';
|
|
156
|
-
db.prepare(
|
|
157
|
-
`SELECT instance_id FROM instance_auth_tags WHERE tag IN (${placeholders})
|
|
158
|
-
GROUP BY instance_id ${having}`,
|
|
159
|
-
)
|
|
160
|
-
.all(...uniqueTags)
|
|
161
|
-
.forEach(({ instance_id }) => allowedIds.add(instance_id));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return allowedIds;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Which service names may the user see for `action` on one instance?
|
|
168
|
-
// null = all services; [] = none; otherwise the union of matchers to filter names with.
|
|
169
|
-
// Same matching as can(): a rule counts only if ruleCovers() would accept it for a service
|
|
170
|
-
// on this instance, so the two can never disagree.
|
|
171
|
-
function serviceMatchersFor(subject, action, instanceId) {
|
|
172
|
-
if (!subject) return [];
|
|
173
|
-
if (subject.isRoot) return null;
|
|
174
|
-
|
|
175
|
-
const actionDef = getAction(action);
|
|
176
|
-
if (!actionDef || (actionDef.scopeKind !== 'service' && actionDef.scopeKind !== 'envfile')) {
|
|
177
|
-
return [];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const matchers = [];
|
|
181
|
-
for (const rule of rulesFor(subject, action)) {
|
|
182
|
-
// Probe with no service name and no env file: passing means the rule has neither
|
|
183
|
-
// narrowing, so it covers every service on this instance.
|
|
184
|
-
if (ruleCovers(rule, actionDef, { instanceId })) return null;
|
|
185
|
-
// Otherwise it only helps if its sole narrowing is by service name. A rule limited to
|
|
186
|
-
// specific env files can't vouch for a whole service (can() needs an env file id too).
|
|
187
|
-
const { services, envFiles } = rule.scope;
|
|
188
|
-
if (envFiles || !services) continue;
|
|
189
|
-
if (!serverMatches(rule.scope.servers, { id: instanceId }, authTagsOf)) continue;
|
|
190
|
-
matchers.push(...services.matchers);
|
|
191
|
-
}
|
|
192
|
-
return matchers;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Build a WHERE clause for SQL queries filtering env files by action
|
|
196
|
-
function envFileFilter(subject, action) {
|
|
197
|
-
if (!subject) return { sql: '1=0', params: [] };
|
|
198
|
-
if (subject.isRoot) return { sql: '1=1', params: [] };
|
|
199
|
-
|
|
200
|
-
const actionDef = getAction(action);
|
|
201
|
-
if (!actionDef || actionDef.scopeKind !== 'envfile') {
|
|
202
|
-
return { sql: '1=0', params: [] };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Evaluate the real can() against every env file, so this filter can never disagree with
|
|
206
|
-
// the individual checks.
|
|
207
|
-
const allowed = db
|
|
208
|
-
.prepare('SELECT id, instance_id, service_name FROM env_files')
|
|
209
|
-
.all()
|
|
210
|
-
.filter((f) =>
|
|
211
|
-
can(subject, action, {
|
|
212
|
-
instanceId: f.instance_id,
|
|
213
|
-
serviceName: f.service_name || undefined,
|
|
214
|
-
envFileId: f.id,
|
|
215
|
-
}),
|
|
216
|
-
)
|
|
217
|
-
.map((f) => f.id);
|
|
218
|
-
if (allowed.length === 0) return { sql: '1=0', params: [] };
|
|
219
|
-
return { sql: `env_files.id IN (${allowed.map(() => '?').join(',')})`, params: allowed };
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Get effective grants for a user/action (for UI preview)
|
|
223
|
-
function getEffectiveGrants(subject, action) {
|
|
224
|
-
if (!subject) return [];
|
|
225
|
-
if (subject.isRoot) return [{ action, scope: null, teamName: 'Root', policyName: null }];
|
|
226
|
-
return rulesFor(subject, action).map((rule) => ({
|
|
227
|
-
action: rule.action,
|
|
228
|
-
scope: rule.scope,
|
|
229
|
-
teamName: rule.teamName,
|
|
230
|
-
policyName: rule.policyName,
|
|
231
|
-
teamId: rule.teamId,
|
|
232
|
-
policyId: rule.policyId,
|
|
233
|
-
}));
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// Check if user holds an action anywhere (for UI nav)
|
|
237
|
-
function anyScope(subject, action) {
|
|
238
|
-
if (!subject) return false;
|
|
239
|
-
if (subject.isRoot) return true;
|
|
240
|
-
return rulesFor(subject, action).length > 0;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
module.exports = {
|
|
244
|
-
buildSubject,
|
|
245
|
-
can,
|
|
246
|
-
resolveInstanceIds,
|
|
247
|
-
serviceMatchersFor,
|
|
248
|
-
envFileFilter,
|
|
249
|
-
getEffectiveGrants,
|
|
250
|
-
anyScope,
|
|
251
|
-
};
|
|
1
|
+
// Core authorization engine — can(), resolveInstanceIds(), etc.
|
|
2
|
+
//
|
|
3
|
+
// The model in one paragraph: rules only ever GRANT (there are no deny rules), so rules
|
|
4
|
+
// can't conflict — a person's access is simply the union of every rule they hold. Inside
|
|
5
|
+
// one rule, every part of its scope must match (servers AND services AND env files). The
|
|
6
|
+
// one place that decides "does this rule cover this resource" is ruleCovers(); every other
|
|
7
|
+
// function here builds on it (or, for resolveInstanceIds, mirrors its server check in SQL).
|
|
8
|
+
const { db } = require('../db');
|
|
9
|
+
const { ACTIONS, getAction } = require('./catalog');
|
|
10
|
+
const { serviceMatches, serverMatches, validateRule } = require('./scope');
|
|
11
|
+
|
|
12
|
+
// For each action, the set of rule actions that satisfy a check for it: the action itself
|
|
13
|
+
// plus every action that (transitively) IMPLIES it. A rule for env.edit satisfies a check
|
|
14
|
+
// for env.view (env.edit implies env.view) — never the reverse, or a view-only grant would
|
|
15
|
+
// pass an edit check. The catalog is static, so this is computed once at load.
|
|
16
|
+
const SATISFIED_BY = new Map(
|
|
17
|
+
ACTIONS.map((def) => {
|
|
18
|
+
const out = new Set([def.id]);
|
|
19
|
+
let grew = true;
|
|
20
|
+
while (grew) {
|
|
21
|
+
grew = false;
|
|
22
|
+
for (const other of ACTIONS) {
|
|
23
|
+
if (!out.has(other.id) && (other.implies || []).some((i) => out.has(i))) {
|
|
24
|
+
out.add(other.id);
|
|
25
|
+
grew = true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [def.id, out];
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
function authTagsOf(instanceId) {
|
|
34
|
+
return db
|
|
35
|
+
.prepare('SELECT tag FROM instance_auth_tags WHERE instance_id = ?')
|
|
36
|
+
.all(instanceId)
|
|
37
|
+
.map((t) => t.tag);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The subject's rules that could grant `action` (ignoring scope). Unknown action = none.
|
|
41
|
+
function rulesFor(subject, action) {
|
|
42
|
+
const satisfying = SATISFIED_BY.get(action);
|
|
43
|
+
return satisfying ? subject.rules.filter((r) => satisfying.has(r.action)) : [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// THE matching rule. Does this one rule cover `resource` for an action of this kind?
|
|
47
|
+
// Every narrowing present on the rule must match; a narrowing the rule doesn't have
|
|
48
|
+
// doesn't restrict it. Server-level actions only ever look at servers.
|
|
49
|
+
function ruleCovers(rule, actionDef, resource) {
|
|
50
|
+
const { servers, services, envFiles } = rule.scope || {};
|
|
51
|
+
if (!servers) return false;
|
|
52
|
+
if (!serverMatches(servers, { id: resource.instanceId }, authTagsOf)) return false;
|
|
53
|
+
if (actionDef.scopeKind === 'server') return true;
|
|
54
|
+
if (services && !serviceMatches(services.matchers, resource.serviceName)) return false;
|
|
55
|
+
// A rule limited to specific env files only covers those; a check that names no env
|
|
56
|
+
// file can't be proven to be one of them, so it fails closed.
|
|
57
|
+
if (envFiles && !envFiles.ids.includes(resource.envFileId)) return false;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Older stored rules may carry `services: {matchers: []}`, which has always meant "no
|
|
62
|
+
// service narrowing". Normalize it to the absent form so nothing downstream has to know
|
|
63
|
+
// that an empty list means "all". (New rules can't be saved this way — see validateRule.)
|
|
64
|
+
function normalizeScope(scope) {
|
|
65
|
+
if (scope && scope.services && scope.services.matchers.length === 0) {
|
|
66
|
+
const { services, ...rest } = scope;
|
|
67
|
+
return rest;
|
|
68
|
+
}
|
|
69
|
+
return scope;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Build a subject (user + their rules) for the request
|
|
73
|
+
function buildSubject(user, store) {
|
|
74
|
+
if (!user) return null;
|
|
75
|
+
|
|
76
|
+
// Root users bypass all checks
|
|
77
|
+
if (user.is_root) {
|
|
78
|
+
return { user, isRoot: true, rules: [] };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const teams = store.getUserTeams(user.id);
|
|
82
|
+
const rules = [];
|
|
83
|
+
for (const team of teams) {
|
|
84
|
+
for (const policy of store.getPoliciesForTeam(team.id)) {
|
|
85
|
+
for (const rule of store.getPolicyRules(policy.id)) {
|
|
86
|
+
// The store returns scope as a JSON string; parse it before validating/matching
|
|
87
|
+
let scope = null;
|
|
88
|
+
try {
|
|
89
|
+
scope = rule.scope ? JSON.parse(rule.scope) : null;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.warn(`Unparseable scope in policy ${policy.id}, rule ${rule.id}; skipping`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
// Validate rule; skip if invalid (fails closed — a skipped rule grants nothing).
|
|
95
|
+
// Lenient mode: rules stored before the stricter API checks must keep loading.
|
|
96
|
+
const validation = validateRule({ action: rule.action, scope }, { fromMigration: true });
|
|
97
|
+
if (!validation.ok) {
|
|
98
|
+
console.warn(`Invalid rule in policy ${policy.id}: ${validation.error}`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
rules.push({
|
|
103
|
+
id: rule.id,
|
|
104
|
+
action: rule.action,
|
|
105
|
+
scope: normalizeScope(scope),
|
|
106
|
+
teamId: team.id,
|
|
107
|
+
teamName: team.name,
|
|
108
|
+
policyId: policy.id,
|
|
109
|
+
policyName: policy.name,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { user, isRoot: false, rules, teams };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Check if user can perform action on resource ({instanceId, serviceName?, envFileId?})
|
|
119
|
+
function can(subject, action, resource = null) {
|
|
120
|
+
if (!subject) return false;
|
|
121
|
+
if (subject.isRoot) return true;
|
|
122
|
+
|
|
123
|
+
const actionDef = getAction(action);
|
|
124
|
+
if (!actionDef) return false; // Unknown action
|
|
125
|
+
|
|
126
|
+
const rules = rulesFor(subject, action);
|
|
127
|
+
if (actionDef.scopeKind === 'org') return rules.length > 0;
|
|
128
|
+
if (!resource) return false;
|
|
129
|
+
return rules.some((rule) => ruleCovers(rule, actionDef, resource));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Get all instance IDs a user can access for a server-level action.
|
|
133
|
+
// null = unrestricted. Mirrors ruleCovers' server check, but resolves tags in one query.
|
|
134
|
+
function resolveInstanceIds(subject, action) {
|
|
135
|
+
if (!subject) return new Set();
|
|
136
|
+
if (subject.isRoot) return null;
|
|
137
|
+
|
|
138
|
+
const actionDef = getAction(action);
|
|
139
|
+
if (!actionDef || actionDef.scopeKind !== 'server') return new Set();
|
|
140
|
+
|
|
141
|
+
const allowedIds = new Set();
|
|
142
|
+
for (const rule of rulesFor(subject, action)) {
|
|
143
|
+
const servers = rule.scope && rule.scope.servers;
|
|
144
|
+
if (!servers) continue;
|
|
145
|
+
if (servers.type === 'all') return null;
|
|
146
|
+
|
|
147
|
+
if (servers.type === 'ids') {
|
|
148
|
+
servers.ids.forEach((id) => allowedIds.add(id));
|
|
149
|
+
} else if (servers.type === 'tags') {
|
|
150
|
+
const uniqueTags = [...new Set(servers.tags)];
|
|
151
|
+
const placeholders = uniqueTags.map(() => '?').join(',');
|
|
152
|
+
const having =
|
|
153
|
+
(servers.match || 'all') === 'all'
|
|
154
|
+
? `HAVING COUNT(DISTINCT tag) = ${uniqueTags.length}`
|
|
155
|
+
: '';
|
|
156
|
+
db.prepare(
|
|
157
|
+
`SELECT instance_id FROM instance_auth_tags WHERE tag IN (${placeholders})
|
|
158
|
+
GROUP BY instance_id ${having}`,
|
|
159
|
+
)
|
|
160
|
+
.all(...uniqueTags)
|
|
161
|
+
.forEach(({ instance_id }) => allowedIds.add(instance_id));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return allowedIds;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Which service names may the user see for `action` on one instance?
|
|
168
|
+
// null = all services; [] = none; otherwise the union of matchers to filter names with.
|
|
169
|
+
// Same matching as can(): a rule counts only if ruleCovers() would accept it for a service
|
|
170
|
+
// on this instance, so the two can never disagree.
|
|
171
|
+
function serviceMatchersFor(subject, action, instanceId) {
|
|
172
|
+
if (!subject) return [];
|
|
173
|
+
if (subject.isRoot) return null;
|
|
174
|
+
|
|
175
|
+
const actionDef = getAction(action);
|
|
176
|
+
if (!actionDef || (actionDef.scopeKind !== 'service' && actionDef.scopeKind !== 'envfile')) {
|
|
177
|
+
return [];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const matchers = [];
|
|
181
|
+
for (const rule of rulesFor(subject, action)) {
|
|
182
|
+
// Probe with no service name and no env file: passing means the rule has neither
|
|
183
|
+
// narrowing, so it covers every service on this instance.
|
|
184
|
+
if (ruleCovers(rule, actionDef, { instanceId })) return null;
|
|
185
|
+
// Otherwise it only helps if its sole narrowing is by service name. A rule limited to
|
|
186
|
+
// specific env files can't vouch for a whole service (can() needs an env file id too).
|
|
187
|
+
const { services, envFiles } = rule.scope;
|
|
188
|
+
if (envFiles || !services) continue;
|
|
189
|
+
if (!serverMatches(rule.scope.servers, { id: instanceId }, authTagsOf)) continue;
|
|
190
|
+
matchers.push(...services.matchers);
|
|
191
|
+
}
|
|
192
|
+
return matchers;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Build a WHERE clause for SQL queries filtering env files by action
|
|
196
|
+
function envFileFilter(subject, action) {
|
|
197
|
+
if (!subject) return { sql: '1=0', params: [] };
|
|
198
|
+
if (subject.isRoot) return { sql: '1=1', params: [] };
|
|
199
|
+
|
|
200
|
+
const actionDef = getAction(action);
|
|
201
|
+
if (!actionDef || actionDef.scopeKind !== 'envfile') {
|
|
202
|
+
return { sql: '1=0', params: [] };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Evaluate the real can() against every env file, so this filter can never disagree with
|
|
206
|
+
// the individual checks.
|
|
207
|
+
const allowed = db
|
|
208
|
+
.prepare('SELECT id, instance_id, service_name FROM env_files')
|
|
209
|
+
.all()
|
|
210
|
+
.filter((f) =>
|
|
211
|
+
can(subject, action, {
|
|
212
|
+
instanceId: f.instance_id,
|
|
213
|
+
serviceName: f.service_name || undefined,
|
|
214
|
+
envFileId: f.id,
|
|
215
|
+
}),
|
|
216
|
+
)
|
|
217
|
+
.map((f) => f.id);
|
|
218
|
+
if (allowed.length === 0) return { sql: '1=0', params: [] };
|
|
219
|
+
return { sql: `env_files.id IN (${allowed.map(() => '?').join(',')})`, params: allowed };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Get effective grants for a user/action (for UI preview)
|
|
223
|
+
function getEffectiveGrants(subject, action) {
|
|
224
|
+
if (!subject) return [];
|
|
225
|
+
if (subject.isRoot) return [{ action, scope: null, teamName: 'Root', policyName: null }];
|
|
226
|
+
return rulesFor(subject, action).map((rule) => ({
|
|
227
|
+
action: rule.action,
|
|
228
|
+
scope: rule.scope,
|
|
229
|
+
teamName: rule.teamName,
|
|
230
|
+
policyName: rule.policyName,
|
|
231
|
+
teamId: rule.teamId,
|
|
232
|
+
policyId: rule.policyId,
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Check if user holds an action anywhere (for UI nav)
|
|
237
|
+
function anyScope(subject, action) {
|
|
238
|
+
if (!subject) return false;
|
|
239
|
+
if (subject.isRoot) return true;
|
|
240
|
+
return rulesFor(subject, action).length > 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
module.exports = {
|
|
244
|
+
buildSubject,
|
|
245
|
+
can,
|
|
246
|
+
resolveInstanceIds,
|
|
247
|
+
serviceMatchersFor,
|
|
248
|
+
envFileFilter,
|
|
249
|
+
getEffectiveGrants,
|
|
250
|
+
anyScope,
|
|
251
|
+
};
|