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,282 +1,282 @@
|
|
|
1
|
-
// Scope schema and matching logic
|
|
2
|
-
const { getAction } = require('./catalog');
|
|
3
|
-
|
|
4
|
-
// Convert glob pattern to RegExp (for migration, globs are marked with fromMigration:true)
|
|
5
|
-
function globToRegExp(glob) {
|
|
6
|
-
// Simple glob: * = .*, ? = .
|
|
7
|
-
// Escape all regex special chars except * and ?
|
|
8
|
-
const escaped = glob.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
9
|
-
const regexStr = escaped.replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
10
|
-
return new RegExp(`^${regexStr}$`);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function compileMatcher(m) {
|
|
14
|
-
if (m.op === 'exact') {
|
|
15
|
-
return new RegExp(`^${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}$`);
|
|
16
|
-
} else if (m.op === 'startsWith') {
|
|
17
|
-
return new RegExp(`^${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}`);
|
|
18
|
-
} else if (m.op === 'endsWith') {
|
|
19
|
-
return new RegExp(`${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}$`);
|
|
20
|
-
} else if (m.op === 'contains') {
|
|
21
|
-
return new RegExp(m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&'));
|
|
22
|
-
} else if (m.op === 'glob') {
|
|
23
|
-
return globToRegExp(m.value);
|
|
24
|
-
}
|
|
25
|
-
throw new Error(`Unknown matcher op: ${m.op}`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function serviceMatches(matchers, serviceName) {
|
|
29
|
-
if (!matchers || matchers.length === 0) {
|
|
30
|
-
return true; // No service narrowing = all services
|
|
31
|
-
}
|
|
32
|
-
if (!serviceName) {
|
|
33
|
-
return false; // Service narrowing but no service name = no match
|
|
34
|
-
}
|
|
35
|
-
return matchers.some((m) => compileMatcher(m).test(serviceName));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function validateRule({ action, scope }, { fromMigration = false } = {}) {
|
|
39
|
-
const actionDef = getAction(action);
|
|
40
|
-
if (!actionDef) {
|
|
41
|
-
return { error: `Unknown action: ${action}` };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Org actions must have null scope
|
|
45
|
-
if (actionDef.scopeKind === 'org') {
|
|
46
|
-
if (scope !== null) {
|
|
47
|
-
return { error: `Org action ${action} cannot have a scope` };
|
|
48
|
-
}
|
|
49
|
-
return { ok: true };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Other actions must have a scope
|
|
53
|
-
if (scope === null) {
|
|
54
|
-
return { error: `Action ${action} requires a scope` };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Validate server scope
|
|
58
|
-
if (!scope.servers) {
|
|
59
|
-
return { error: 'Scope must have a servers field' };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const { type } = scope.servers;
|
|
63
|
-
if (type === 'all') {
|
|
64
|
-
// OK
|
|
65
|
-
} else if (type === 'ids') {
|
|
66
|
-
if (!Array.isArray(scope.servers.ids) || scope.servers.ids.length === 0) {
|
|
67
|
-
return { error: 'servers.ids must be a non-empty array' };
|
|
68
|
-
}
|
|
69
|
-
} else if (type === 'tags') {
|
|
70
|
-
if (!Array.isArray(scope.servers.tags) || scope.servers.tags.length === 0) {
|
|
71
|
-
return { error: 'servers.tags must be a non-empty array' };
|
|
72
|
-
}
|
|
73
|
-
const match = scope.servers.match || 'all';
|
|
74
|
-
if (!['all', 'any'].includes(match)) {
|
|
75
|
-
return { error: 'servers.match must be "all" or "any"' };
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
return { error: `Unknown servers type: ${type}` };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Stricter shape checks for rules created/changed through the API (the UI applies the same
|
|
82
|
-
// ones in policyModel.ts). Stored rules load leniently so older data keeps working.
|
|
83
|
-
// - An empty list is rejected: leave the field out to mean "all". (Empty `services` used to
|
|
84
|
-
// silently mean "all services" while empty `envFiles` meant "no files".)
|
|
85
|
-
// - Narrowing only goes on actions that can use it; elsewhere it was silently ignored.
|
|
86
|
-
if (!fromMigration) {
|
|
87
|
-
const kind = actionDef.scopeKind;
|
|
88
|
-
if (scope.services) {
|
|
89
|
-
if (kind !== 'service' && kind !== 'envfile') {
|
|
90
|
-
return { error: `Action ${action} can't be limited to specific services` };
|
|
91
|
-
}
|
|
92
|
-
if (!Array.isArray(scope.services.matchers) || scope.services.matchers.length === 0) {
|
|
93
|
-
return { error: 'services.matchers must be a non-empty array (omit services for all)' };
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (scope.envFiles) {
|
|
97
|
-
if (kind !== 'envfile') {
|
|
98
|
-
return { error: `Action ${action} can't be limited to specific env files` };
|
|
99
|
-
}
|
|
100
|
-
if (!Array.isArray(scope.envFiles.ids) || scope.envFiles.ids.length === 0) {
|
|
101
|
-
return { error: 'envFiles.ids must be a non-empty array (omit envFiles for all)' };
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Validate service matchers (optional)
|
|
107
|
-
if (scope.services) {
|
|
108
|
-
if (!Array.isArray(scope.services.matchers)) {
|
|
109
|
-
return { error: 'services.matchers must be an array' };
|
|
110
|
-
}
|
|
111
|
-
for (const m of scope.services.matchers) {
|
|
112
|
-
if (!m.op || !m.value) {
|
|
113
|
-
return { error: 'Each service matcher must have op and value' };
|
|
114
|
-
}
|
|
115
|
-
const validOps = ['exact', 'startsWith', 'endsWith', 'contains', 'glob'];
|
|
116
|
-
if (!validOps.includes(m.op)) {
|
|
117
|
-
return { error: `Unknown service matcher op: ${m.op}` };
|
|
118
|
-
}
|
|
119
|
-
// Only allow glob if fromMigration=true
|
|
120
|
-
if (m.op === 'glob' && !fromMigration) {
|
|
121
|
-
return { error: 'Glob matchers only allowed in migration' };
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Validate env-file ids (optional)
|
|
127
|
-
if (scope.envFiles) {
|
|
128
|
-
if (!Array.isArray(scope.envFiles.ids)) {
|
|
129
|
-
return { error: 'envFiles.ids must be an array' };
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return { ok: true };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function serverMatches(serverScope, instance, authTagsOf) {
|
|
137
|
-
if (serverScope.type === 'all') {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
if (serverScope.type === 'ids') {
|
|
141
|
-
return serverScope.ids.includes(instance.id);
|
|
142
|
-
}
|
|
143
|
-
if (serverScope.type === 'tags') {
|
|
144
|
-
const instanceTags = authTagsOf(instance.id);
|
|
145
|
-
const match = serverScope.match || 'all';
|
|
146
|
-
if (match === 'all') {
|
|
147
|
-
return serverScope.tags.every((t) => instanceTags.includes(t));
|
|
148
|
-
} else {
|
|
149
|
-
return serverScope.tags.some((t) => instanceTags.includes(t));
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Check if scopeA is a subset of scopeB
|
|
156
|
-
function scopeSubset(scopeA, scopeB) {
|
|
157
|
-
if (!scopeA || !scopeB) {
|
|
158
|
-
return scopeA === scopeB;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Servers
|
|
162
|
-
const serverSubset = serverScopeSubset(scopeA.servers, scopeB.servers);
|
|
163
|
-
if (!serverSubset) return false;
|
|
164
|
-
|
|
165
|
-
// Services
|
|
166
|
-
const serviceSubset = serviceScopeSubset(scopeA.services, scopeB.services);
|
|
167
|
-
if (!serviceSubset) return false;
|
|
168
|
-
|
|
169
|
-
// Env files
|
|
170
|
-
const envSubset = envFileScopeSubset(scopeA.envFiles, scopeB.envFiles);
|
|
171
|
-
if (!envSubset) return false;
|
|
172
|
-
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function serverScopeSubset(scopeA, scopeB) {
|
|
177
|
-
if (scopeB.type === 'all') return true; // Anything is subset of 'all'
|
|
178
|
-
if (scopeA.type === 'all') return false; // 'all' is not subset of specific
|
|
179
|
-
|
|
180
|
-
if (scopeA.type === 'ids' && scopeB.type === 'ids') {
|
|
181
|
-
return scopeA.ids.every((id) => scopeB.ids.includes(id));
|
|
182
|
-
}
|
|
183
|
-
if (scopeA.type === 'ids' && scopeB.type === 'tags') {
|
|
184
|
-
// ids subset of tags only if we can prove all current ids match the tags (point-in-time)
|
|
185
|
-
// This is evaluated at delegation time with current tag state
|
|
186
|
-
return false; // Conservative: requires runtime check in delegation
|
|
187
|
-
}
|
|
188
|
-
if (scopeA.type === 'tags' && scopeB.type === 'ids') {
|
|
189
|
-
return false; // tags not subset of ids
|
|
190
|
-
}
|
|
191
|
-
if (scopeA.type === 'tags' && scopeB.type === 'tags') {
|
|
192
|
-
const matchA = scopeA.match || 'all';
|
|
193
|
-
const matchB = scopeB.match || 'all';
|
|
194
|
-
if (matchA === 'all' && matchB === 'all') {
|
|
195
|
-
// Requiring MORE tags is narrower: A ⊆ B iff every tag B requires, A requires too.
|
|
196
|
-
return scopeB.tags.every((t) => scopeA.tags.includes(t));
|
|
197
|
-
}
|
|
198
|
-
if (matchA === 'any' && matchB === 'any') {
|
|
199
|
-
return scopeA.tags.every((t) => scopeB.tags.includes(t)); // subset of available tags
|
|
200
|
-
}
|
|
201
|
-
if (matchA === 'all' && matchB === 'any') {
|
|
202
|
-
return scopeA.tags.some((t) => scopeB.tags.includes(t)); // some overlap
|
|
203
|
-
}
|
|
204
|
-
return false; // 'any' subset of 'all' is false
|
|
205
|
-
}
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function serviceScopeSubset(scopeA, scopeB) {
|
|
210
|
-
if (!scopeA && !scopeB) return true; // Both absent
|
|
211
|
-
if (!scopeB) return true; // B absent means no narrowing, A is subset
|
|
212
|
-
if (!scopeA) return false; // A absent but B present is not a subset
|
|
213
|
-
|
|
214
|
-
if (!scopeA.matchers || scopeA.matchers.length === 0) {
|
|
215
|
-
return !scopeB.matchers || scopeB.matchers.length === 0;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (!scopeB.matchers || scopeB.matchers.length === 0) {
|
|
219
|
-
return false; // A has matchers but B doesn't
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Each matcher in A must be covered by some matcher in B
|
|
223
|
-
return scopeA.matchers.every((mA) => scopeB.matchers.some((mB) => matcherSubset(mA, mB)));
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function matcherSubset(mA, mB) {
|
|
227
|
-
if (mB.op === 'exact' || mA.op === 'exact') {
|
|
228
|
-
if (mA.op === 'exact' && mB.op === 'exact') {
|
|
229
|
-
return mA.value === mB.value;
|
|
230
|
-
}
|
|
231
|
-
if (mA.op === 'exact' && mB.op !== 'exact') {
|
|
232
|
-
// exact(x) subset of pattern if pattern matches x
|
|
233
|
-
return compileMatcher(mB).test(mA.value);
|
|
234
|
-
}
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (mA.op === mB.op) {
|
|
239
|
-
// Same op: startsWith, endsWith, contains
|
|
240
|
-
// A is covered by B when everything A matches, B matches too: a LONGER prefix/suffix/
|
|
241
|
-
// substring is narrower (startsWith "api-v" ⊆ startsWith "api").
|
|
242
|
-
if (mA.op === 'startsWith') {
|
|
243
|
-
return mA.value.startsWith(mB.value);
|
|
244
|
-
}
|
|
245
|
-
if (mA.op === 'endsWith') {
|
|
246
|
-
return mA.value.endsWith(mB.value);
|
|
247
|
-
}
|
|
248
|
-
if (mA.op === 'contains') {
|
|
249
|
-
return mA.value.includes(mB.value);
|
|
250
|
-
}
|
|
251
|
-
if (mA.op === 'glob') {
|
|
252
|
-
return mA.value === mB.value; // Globs only equal to identical globs
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return false;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function envFileScopeSubset(scopeA, scopeB) {
|
|
260
|
-
if (!scopeA && !scopeB) return true;
|
|
261
|
-
if (!scopeB) return true; // B absent = no env narrowing, subset
|
|
262
|
-
if (!scopeA) return false;
|
|
263
|
-
|
|
264
|
-
if (scopeA.ids && scopeB.ids) {
|
|
265
|
-
return scopeA.ids.every((id) => scopeB.ids.includes(id));
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return scopeA.ids && !scopeB.ids ? true : false; // A specific, B not = subset
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
module.exports = {
|
|
272
|
-
compileMatcher,
|
|
273
|
-
serviceMatches,
|
|
274
|
-
serverMatches,
|
|
275
|
-
validateRule,
|
|
276
|
-
scopeSubset,
|
|
277
|
-
serverScopeSubset,
|
|
278
|
-
serviceScopeSubset,
|
|
279
|
-
matcherSubset,
|
|
280
|
-
envFileScopeSubset,
|
|
281
|
-
globToRegExp,
|
|
282
|
-
};
|
|
1
|
+
// Scope schema and matching logic
|
|
2
|
+
const { getAction } = require('./catalog');
|
|
3
|
+
|
|
4
|
+
// Convert glob pattern to RegExp (for migration, globs are marked with fromMigration:true)
|
|
5
|
+
function globToRegExp(glob) {
|
|
6
|
+
// Simple glob: * = .*, ? = .
|
|
7
|
+
// Escape all regex special chars except * and ?
|
|
8
|
+
const escaped = glob.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
9
|
+
const regexStr = escaped.replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
10
|
+
return new RegExp(`^${regexStr}$`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function compileMatcher(m) {
|
|
14
|
+
if (m.op === 'exact') {
|
|
15
|
+
return new RegExp(`^${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}$`);
|
|
16
|
+
} else if (m.op === 'startsWith') {
|
|
17
|
+
return new RegExp(`^${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}`);
|
|
18
|
+
} else if (m.op === 'endsWith') {
|
|
19
|
+
return new RegExp(`${m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&')}$`);
|
|
20
|
+
} else if (m.op === 'contains') {
|
|
21
|
+
return new RegExp(m.value.replace(/[.+^${}()|[\]\\]/g, '\\$&'));
|
|
22
|
+
} else if (m.op === 'glob') {
|
|
23
|
+
return globToRegExp(m.value);
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`Unknown matcher op: ${m.op}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function serviceMatches(matchers, serviceName) {
|
|
29
|
+
if (!matchers || matchers.length === 0) {
|
|
30
|
+
return true; // No service narrowing = all services
|
|
31
|
+
}
|
|
32
|
+
if (!serviceName) {
|
|
33
|
+
return false; // Service narrowing but no service name = no match
|
|
34
|
+
}
|
|
35
|
+
return matchers.some((m) => compileMatcher(m).test(serviceName));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateRule({ action, scope }, { fromMigration = false } = {}) {
|
|
39
|
+
const actionDef = getAction(action);
|
|
40
|
+
if (!actionDef) {
|
|
41
|
+
return { error: `Unknown action: ${action}` };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Org actions must have null scope
|
|
45
|
+
if (actionDef.scopeKind === 'org') {
|
|
46
|
+
if (scope !== null) {
|
|
47
|
+
return { error: `Org action ${action} cannot have a scope` };
|
|
48
|
+
}
|
|
49
|
+
return { ok: true };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Other actions must have a scope
|
|
53
|
+
if (scope === null) {
|
|
54
|
+
return { error: `Action ${action} requires a scope` };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Validate server scope
|
|
58
|
+
if (!scope.servers) {
|
|
59
|
+
return { error: 'Scope must have a servers field' };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { type } = scope.servers;
|
|
63
|
+
if (type === 'all') {
|
|
64
|
+
// OK
|
|
65
|
+
} else if (type === 'ids') {
|
|
66
|
+
if (!Array.isArray(scope.servers.ids) || scope.servers.ids.length === 0) {
|
|
67
|
+
return { error: 'servers.ids must be a non-empty array' };
|
|
68
|
+
}
|
|
69
|
+
} else if (type === 'tags') {
|
|
70
|
+
if (!Array.isArray(scope.servers.tags) || scope.servers.tags.length === 0) {
|
|
71
|
+
return { error: 'servers.tags must be a non-empty array' };
|
|
72
|
+
}
|
|
73
|
+
const match = scope.servers.match || 'all';
|
|
74
|
+
if (!['all', 'any'].includes(match)) {
|
|
75
|
+
return { error: 'servers.match must be "all" or "any"' };
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
return { error: `Unknown servers type: ${type}` };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Stricter shape checks for rules created/changed through the API (the UI applies the same
|
|
82
|
+
// ones in policyModel.ts). Stored rules load leniently so older data keeps working.
|
|
83
|
+
// - An empty list is rejected: leave the field out to mean "all". (Empty `services` used to
|
|
84
|
+
// silently mean "all services" while empty `envFiles` meant "no files".)
|
|
85
|
+
// - Narrowing only goes on actions that can use it; elsewhere it was silently ignored.
|
|
86
|
+
if (!fromMigration) {
|
|
87
|
+
const kind = actionDef.scopeKind;
|
|
88
|
+
if (scope.services) {
|
|
89
|
+
if (kind !== 'service' && kind !== 'envfile') {
|
|
90
|
+
return { error: `Action ${action} can't be limited to specific services` };
|
|
91
|
+
}
|
|
92
|
+
if (!Array.isArray(scope.services.matchers) || scope.services.matchers.length === 0) {
|
|
93
|
+
return { error: 'services.matchers must be a non-empty array (omit services for all)' };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (scope.envFiles) {
|
|
97
|
+
if (kind !== 'envfile') {
|
|
98
|
+
return { error: `Action ${action} can't be limited to specific env files` };
|
|
99
|
+
}
|
|
100
|
+
if (!Array.isArray(scope.envFiles.ids) || scope.envFiles.ids.length === 0) {
|
|
101
|
+
return { error: 'envFiles.ids must be a non-empty array (omit envFiles for all)' };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Validate service matchers (optional)
|
|
107
|
+
if (scope.services) {
|
|
108
|
+
if (!Array.isArray(scope.services.matchers)) {
|
|
109
|
+
return { error: 'services.matchers must be an array' };
|
|
110
|
+
}
|
|
111
|
+
for (const m of scope.services.matchers) {
|
|
112
|
+
if (!m.op || !m.value) {
|
|
113
|
+
return { error: 'Each service matcher must have op and value' };
|
|
114
|
+
}
|
|
115
|
+
const validOps = ['exact', 'startsWith', 'endsWith', 'contains', 'glob'];
|
|
116
|
+
if (!validOps.includes(m.op)) {
|
|
117
|
+
return { error: `Unknown service matcher op: ${m.op}` };
|
|
118
|
+
}
|
|
119
|
+
// Only allow glob if fromMigration=true
|
|
120
|
+
if (m.op === 'glob' && !fromMigration) {
|
|
121
|
+
return { error: 'Glob matchers only allowed in migration' };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Validate env-file ids (optional)
|
|
127
|
+
if (scope.envFiles) {
|
|
128
|
+
if (!Array.isArray(scope.envFiles.ids)) {
|
|
129
|
+
return { error: 'envFiles.ids must be an array' };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { ok: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function serverMatches(serverScope, instance, authTagsOf) {
|
|
137
|
+
if (serverScope.type === 'all') {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
if (serverScope.type === 'ids') {
|
|
141
|
+
return serverScope.ids.includes(instance.id);
|
|
142
|
+
}
|
|
143
|
+
if (serverScope.type === 'tags') {
|
|
144
|
+
const instanceTags = authTagsOf(instance.id);
|
|
145
|
+
const match = serverScope.match || 'all';
|
|
146
|
+
if (match === 'all') {
|
|
147
|
+
return serverScope.tags.every((t) => instanceTags.includes(t));
|
|
148
|
+
} else {
|
|
149
|
+
return serverScope.tags.some((t) => instanceTags.includes(t));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Check if scopeA is a subset of scopeB
|
|
156
|
+
function scopeSubset(scopeA, scopeB) {
|
|
157
|
+
if (!scopeA || !scopeB) {
|
|
158
|
+
return scopeA === scopeB;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Servers
|
|
162
|
+
const serverSubset = serverScopeSubset(scopeA.servers, scopeB.servers);
|
|
163
|
+
if (!serverSubset) return false;
|
|
164
|
+
|
|
165
|
+
// Services
|
|
166
|
+
const serviceSubset = serviceScopeSubset(scopeA.services, scopeB.services);
|
|
167
|
+
if (!serviceSubset) return false;
|
|
168
|
+
|
|
169
|
+
// Env files
|
|
170
|
+
const envSubset = envFileScopeSubset(scopeA.envFiles, scopeB.envFiles);
|
|
171
|
+
if (!envSubset) return false;
|
|
172
|
+
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function serverScopeSubset(scopeA, scopeB) {
|
|
177
|
+
if (scopeB.type === 'all') return true; // Anything is subset of 'all'
|
|
178
|
+
if (scopeA.type === 'all') return false; // 'all' is not subset of specific
|
|
179
|
+
|
|
180
|
+
if (scopeA.type === 'ids' && scopeB.type === 'ids') {
|
|
181
|
+
return scopeA.ids.every((id) => scopeB.ids.includes(id));
|
|
182
|
+
}
|
|
183
|
+
if (scopeA.type === 'ids' && scopeB.type === 'tags') {
|
|
184
|
+
// ids subset of tags only if we can prove all current ids match the tags (point-in-time)
|
|
185
|
+
// This is evaluated at delegation time with current tag state
|
|
186
|
+
return false; // Conservative: requires runtime check in delegation
|
|
187
|
+
}
|
|
188
|
+
if (scopeA.type === 'tags' && scopeB.type === 'ids') {
|
|
189
|
+
return false; // tags not subset of ids
|
|
190
|
+
}
|
|
191
|
+
if (scopeA.type === 'tags' && scopeB.type === 'tags') {
|
|
192
|
+
const matchA = scopeA.match || 'all';
|
|
193
|
+
const matchB = scopeB.match || 'all';
|
|
194
|
+
if (matchA === 'all' && matchB === 'all') {
|
|
195
|
+
// Requiring MORE tags is narrower: A ⊆ B iff every tag B requires, A requires too.
|
|
196
|
+
return scopeB.tags.every((t) => scopeA.tags.includes(t));
|
|
197
|
+
}
|
|
198
|
+
if (matchA === 'any' && matchB === 'any') {
|
|
199
|
+
return scopeA.tags.every((t) => scopeB.tags.includes(t)); // subset of available tags
|
|
200
|
+
}
|
|
201
|
+
if (matchA === 'all' && matchB === 'any') {
|
|
202
|
+
return scopeA.tags.some((t) => scopeB.tags.includes(t)); // some overlap
|
|
203
|
+
}
|
|
204
|
+
return false; // 'any' subset of 'all' is false
|
|
205
|
+
}
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function serviceScopeSubset(scopeA, scopeB) {
|
|
210
|
+
if (!scopeA && !scopeB) return true; // Both absent
|
|
211
|
+
if (!scopeB) return true; // B absent means no narrowing, A is subset
|
|
212
|
+
if (!scopeA) return false; // A absent but B present is not a subset
|
|
213
|
+
|
|
214
|
+
if (!scopeA.matchers || scopeA.matchers.length === 0) {
|
|
215
|
+
return !scopeB.matchers || scopeB.matchers.length === 0;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!scopeB.matchers || scopeB.matchers.length === 0) {
|
|
219
|
+
return false; // A has matchers but B doesn't
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Each matcher in A must be covered by some matcher in B
|
|
223
|
+
return scopeA.matchers.every((mA) => scopeB.matchers.some((mB) => matcherSubset(mA, mB)));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function matcherSubset(mA, mB) {
|
|
227
|
+
if (mB.op === 'exact' || mA.op === 'exact') {
|
|
228
|
+
if (mA.op === 'exact' && mB.op === 'exact') {
|
|
229
|
+
return mA.value === mB.value;
|
|
230
|
+
}
|
|
231
|
+
if (mA.op === 'exact' && mB.op !== 'exact') {
|
|
232
|
+
// exact(x) subset of pattern if pattern matches x
|
|
233
|
+
return compileMatcher(mB).test(mA.value);
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (mA.op === mB.op) {
|
|
239
|
+
// Same op: startsWith, endsWith, contains
|
|
240
|
+
// A is covered by B when everything A matches, B matches too: a LONGER prefix/suffix/
|
|
241
|
+
// substring is narrower (startsWith "api-v" ⊆ startsWith "api").
|
|
242
|
+
if (mA.op === 'startsWith') {
|
|
243
|
+
return mA.value.startsWith(mB.value);
|
|
244
|
+
}
|
|
245
|
+
if (mA.op === 'endsWith') {
|
|
246
|
+
return mA.value.endsWith(mB.value);
|
|
247
|
+
}
|
|
248
|
+
if (mA.op === 'contains') {
|
|
249
|
+
return mA.value.includes(mB.value);
|
|
250
|
+
}
|
|
251
|
+
if (mA.op === 'glob') {
|
|
252
|
+
return mA.value === mB.value; // Globs only equal to identical globs
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function envFileScopeSubset(scopeA, scopeB) {
|
|
260
|
+
if (!scopeA && !scopeB) return true;
|
|
261
|
+
if (!scopeB) return true; // B absent = no env narrowing, subset
|
|
262
|
+
if (!scopeA) return false;
|
|
263
|
+
|
|
264
|
+
if (scopeA.ids && scopeB.ids) {
|
|
265
|
+
return scopeA.ids.every((id) => scopeB.ids.includes(id));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return scopeA.ids && !scopeB.ids ? true : false; // A specific, B not = subset
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
module.exports = {
|
|
272
|
+
compileMatcher,
|
|
273
|
+
serviceMatches,
|
|
274
|
+
serverMatches,
|
|
275
|
+
validateRule,
|
|
276
|
+
scopeSubset,
|
|
277
|
+
serverScopeSubset,
|
|
278
|
+
serviceScopeSubset,
|
|
279
|
+
matcherSubset,
|
|
280
|
+
envFileScopeSubset,
|
|
281
|
+
globToRegExp,
|
|
282
|
+
};
|