pulse-hub 0.0.0-stage → 3.5.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 +22 -0
- package/LICENSE +5 -0
- package/README.md +86 -2
- package/backend/.env.example +52 -0
- package/backend/agent-package.json +9 -0
- package/backend/agent.config.example.json +6 -0
- package/backend/agent.js +372 -0
- package/backend/authz-migrate.js +76 -0
- package/backend/cli.js +999 -0
- package/backend/config.js +120 -0
- package/backend/emails/env-backup.html +52 -0
- package/backend/emails/invite.html +53 -0
- package/backend/install-agent.sh +181 -0
- package/backend/lib/agent-rpc.js +72 -0
- package/backend/lib/async-route.js +17 -0
- package/backend/lib/audit.js +134 -0
- package/backend/lib/auth-google.js +78 -0
- package/backend/lib/authz/bootstrap.js +35 -0
- package/backend/lib/authz/catalog.js +291 -0
- package/backend/lib/authz/delegation.js +104 -0
- package/backend/lib/authz/effective.js +104 -0
- package/backend/lib/authz/engine.js +251 -0
- package/backend/lib/authz/index.js +134 -0
- package/backend/lib/authz/legacy-oracle.js +195 -0
- package/backend/lib/authz/migrate.js +871 -0
- package/backend/lib/authz/mutate.js +87 -0
- package/backend/lib/authz/preview.js +327 -0
- package/backend/lib/authz/scope.js +282 -0
- package/backend/lib/authz/templates.js +103 -0
- package/backend/lib/authz-store.js +535 -0
- package/backend/lib/bundle-version.js +71 -0
- package/backend/lib/config-check.js +100 -0
- package/backend/lib/crypto-helpers.js +15 -0
- package/backend/lib/db.js +984 -0
- package/backend/lib/env-backup.js +306 -0
- package/backend/lib/env-crypto.js +52 -0
- package/backend/lib/env-files.js +235 -0
- package/backend/lib/env-mask.js +131 -0
- package/backend/lib/gmail-send.js +234 -0
- package/backend/lib/http-helpers.js +48 -0
- package/backend/lib/instance-registry.js +34 -0
- package/backend/lib/invite-helpers.js +83 -0
- package/backend/lib/invites.js +160 -0
- package/backend/lib/jwt.js +59 -0
- package/backend/lib/logs.js +244 -0
- package/backend/lib/metrics.js +249 -0
- package/backend/lib/migrate-legacy.js +106 -0
- package/backend/lib/paths.js +17 -0
- package/backend/lib/relay-state.js +564 -0
- package/backend/lib/self-update.js +68 -0
- package/backend/lib/session.js +48 -0
- package/backend/lib/sqlite.js +197 -0
- package/backend/lib/static-files.js +86 -0
- package/backend/middleware/authz.js +65 -0
- package/backend/middleware/session-gate.js +38 -0
- package/backend/public/assets/favicon.ico +0 -0
- package/backend/public/assets/favicon.svg +6 -0
- package/backend/public/assets/index.css +1 -0
- package/backend/public/assets/index.js +111 -0
- package/backend/public/assets/inter-cyrillic-ext-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-cyrillic-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-greek-ext-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-greek-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-latin-ext-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-latin-wght-normal.woff2 +0 -0
- package/backend/public/assets/inter-vietnamese-wght-normal.woff2 +0 -0
- package/backend/public/index.html +26 -0
- package/backend/routes/access.js +123 -0
- package/backend/routes/audit.js +41 -0
- package/backend/routes/auth.js +207 -0
- package/backend/routes/env-files.js +943 -0
- package/backend/routes/instances.js +555 -0
- package/backend/routes/invites-public.js +91 -0
- package/backend/routes/invites.js +121 -0
- package/backend/routes/logs.js +68 -0
- package/backend/routes/metrics.js +76 -0
- package/backend/routes/policies.js +240 -0
- package/backend/routes/static.js +100 -0
- package/backend/routes/teams.js +221 -0
- package/backend/routes/users.js +100 -0
- package/backend/seed-admins.js +68 -0
- package/backend/server.js +106 -0
- package/backend/ws/agent-relay.js +159 -0
- package/package.json +95 -4
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Google OAuth 2.0 login — see .claude/plans/sqlite-oauth-invites.md, "Phase 2". This is
|
|
2
|
+
// now the ONLY way to sign in — password login was removed (see CLAUDE.md's "Auth
|
|
3
|
+
// model"), so a hub with these three env vars unset has no working login at all; that's
|
|
4
|
+
// on the operator to configure, not something this module falls back around. Uses
|
|
5
|
+
// Node's built-in global fetch, no SDK dependency, per
|
|
6
|
+
// .claude/rules/npm-dependencies-allowed.md's "small, well-vetted deps only" preference
|
|
7
|
+
// — this whole module is a couple of plain HTTPS calls.
|
|
8
|
+
//
|
|
9
|
+
// The redirect_uri must be an exact, absolute, HTTPS, pre-registered URL in Google Cloud
|
|
10
|
+
// Console. This hub can be reverse-proxied under an arbitrary subpath (see
|
|
11
|
+
// .claude/rules/relative-paths-under-subpath.md) that it has zero awareness of — nginx's
|
|
12
|
+
// proxy_redirect only rewrites same-origin Location headers, which is meaningless for a
|
|
13
|
+
// cross-origin provider URL, and inferring the origin from the request Host header would
|
|
14
|
+
// be spoofable. So PULSE_GOOGLE_REDIRECT_URI is the literal full value, configured
|
|
15
|
+
// manually (see CLAUDE.md's "Deploy workflows" section for the systemd drop-in this
|
|
16
|
+
// lands in), not derived at request time.
|
|
17
|
+
|
|
18
|
+
const CLIENT_ID = process.env.PULSE_GOOGLE_CLIENT_ID;
|
|
19
|
+
const CLIENT_SECRET = process.env.PULSE_GOOGLE_CLIENT_SECRET;
|
|
20
|
+
const REDIRECT_URI = process.env.PULSE_GOOGLE_REDIRECT_URI;
|
|
21
|
+
|
|
22
|
+
function isEnabled() {
|
|
23
|
+
return !!(CLIENT_ID && CLIENT_SECRET && REDIRECT_URI);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// scope: space-separated Google OAuth scopes.
|
|
27
|
+
function buildAuthorizationUrl(state, scope) {
|
|
28
|
+
const params = new URLSearchParams({
|
|
29
|
+
client_id: CLIENT_ID,
|
|
30
|
+
redirect_uri: REDIRECT_URI,
|
|
31
|
+
response_type: 'code',
|
|
32
|
+
scope,
|
|
33
|
+
state,
|
|
34
|
+
});
|
|
35
|
+
return `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function exchangeCodeForTokens(code) {
|
|
39
|
+
const res = await fetch('https://oauth2.googleapis.com/token', {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
42
|
+
body: new URLSearchParams({
|
|
43
|
+
code,
|
|
44
|
+
client_id: CLIENT_ID,
|
|
45
|
+
client_secret: CLIENT_SECRET,
|
|
46
|
+
redirect_uri: REDIRECT_URI,
|
|
47
|
+
grant_type: 'authorization_code',
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
50
|
+
if (!res.ok) throw new Error(`Google token exchange failed: HTTP ${res.status}`);
|
|
51
|
+
return res.json(); // { access_token, refresh_token?, id_token?, expires_in, ... }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Verifies an id_token via Google's tokeninfo endpoint rather than a local JWKS/JWT
|
|
55
|
+
// verification — one extra round trip, but no dependency, and this is a low-frequency
|
|
56
|
+
// human-login path where that cost doesn't matter. Requires email_verified so a plain
|
|
57
|
+
// Google login always corresponds to an address Google itself has confirmed ownership
|
|
58
|
+
// of (relevant later for the invite-email-must-match check).
|
|
59
|
+
async function verifyIdToken(idToken) {
|
|
60
|
+
const res = await fetch(
|
|
61
|
+
`https://oauth2.googleapis.com/tokeninfo?id_token=${encodeURIComponent(idToken)}`,
|
|
62
|
+
);
|
|
63
|
+
if (!res.ok) throw new Error(`id_token verification failed: HTTP ${res.status}`);
|
|
64
|
+
const payload = await res.json();
|
|
65
|
+
if (payload.aud !== CLIENT_ID) throw new Error('id_token was not issued for this client');
|
|
66
|
+
if (payload.email_verified !== 'true' && payload.email_verified !== true)
|
|
67
|
+
throw new Error('Google account email is not verified');
|
|
68
|
+
// payload.picture is only present because the authorization request included the
|
|
69
|
+
// 'profile' scope (see routes/auth.js's /auth/google) — a Google-hosted avatar URL.
|
|
70
|
+
return {
|
|
71
|
+
googleId: payload.sub,
|
|
72
|
+
email: payload.email,
|
|
73
|
+
name: payload.name || null,
|
|
74
|
+
picture: payload.picture || null,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = { isEnabled, buildAuthorizationUrl, exchangeCodeForTokens, verifyIdToken };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Startup hook for the permissions-v2 data migration (see authz/migrate.js). Required by
|
|
2
|
+
// authz/index.js, so it runs the first time anything loads the authorization module — i.e.
|
|
3
|
+
// before the hub serves a request. Kept separate from migrate.js so the migration itself
|
|
4
|
+
// never calls process.exit (tests and the CLI call runMigration directly and catch the
|
|
5
|
+
// typed MigrationParityError); only this wrapper turns a parity failure into exit(1).
|
|
6
|
+
|
|
7
|
+
const { runMigration, formatReport, formatDiffs, MigrationParityError } = require('./migrate');
|
|
8
|
+
|
|
9
|
+
function runStartupMigration() {
|
|
10
|
+
try {
|
|
11
|
+
const report = runMigration();
|
|
12
|
+
if (report.status === 'migrated') {
|
|
13
|
+
console.log('[authz] permissions migration applied');
|
|
14
|
+
console.log(
|
|
15
|
+
formatReport(report)
|
|
16
|
+
.split('\n')
|
|
17
|
+
.map((l) => `[authz] ${l}`)
|
|
18
|
+
.join('\n'),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
return report;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
if (err instanceof MigrationParityError) {
|
|
24
|
+
console.error('[authz] FATAL: permissions migration parity check failed.');
|
|
25
|
+
console.error(formatDiffs(err.diffs));
|
|
26
|
+
console.error(
|
|
27
|
+
'[authz] The migration was rolled back and the database is unchanged; a pre-migration backup was kept. Startup aborted — redeploy the previous version or run `npm run authz-migrate -- --dry-run`.',
|
|
28
|
+
);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { runStartupMigration };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// Permission catalog — single source of truth for all actions
|
|
2
|
+
const ACTIONS = [
|
|
3
|
+
// Servers
|
|
4
|
+
{
|
|
5
|
+
id: 'servers.view',
|
|
6
|
+
feature: 'Servers',
|
|
7
|
+
label: 'View servers',
|
|
8
|
+
description: 'View server information',
|
|
9
|
+
powerful: false,
|
|
10
|
+
scopeKind: 'server',
|
|
11
|
+
implies: [],
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: 'servers.create',
|
|
15
|
+
feature: 'Servers',
|
|
16
|
+
label: 'Register servers',
|
|
17
|
+
description: 'Register a new server',
|
|
18
|
+
powerful: true,
|
|
19
|
+
scopeKind: 'server',
|
|
20
|
+
implies: [],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'servers.delete',
|
|
24
|
+
feature: 'Servers',
|
|
25
|
+
label: 'Delete servers',
|
|
26
|
+
description: 'Remove a registered server',
|
|
27
|
+
powerful: true,
|
|
28
|
+
scopeKind: 'server',
|
|
29
|
+
implies: [],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: 'servers.update',
|
|
33
|
+
feature: 'Servers',
|
|
34
|
+
label: 'Update servers',
|
|
35
|
+
description: 'Modify server configuration and metadata',
|
|
36
|
+
powerful: true,
|
|
37
|
+
scopeKind: 'server',
|
|
38
|
+
implies: [],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'servers.tags.manage',
|
|
42
|
+
feature: 'Servers',
|
|
43
|
+
label: 'Manage access tags',
|
|
44
|
+
description: 'Modify server access tags',
|
|
45
|
+
powerful: true,
|
|
46
|
+
scopeKind: 'server',
|
|
47
|
+
implies: [],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'servers.agent.update',
|
|
51
|
+
feature: 'Servers',
|
|
52
|
+
label: 'Update agent',
|
|
53
|
+
description: 'Update or restart the server agent',
|
|
54
|
+
powerful: true,
|
|
55
|
+
scopeKind: 'server',
|
|
56
|
+
implies: [],
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
// Metrics
|
|
60
|
+
{
|
|
61
|
+
id: 'metrics.view',
|
|
62
|
+
feature: 'Metrics',
|
|
63
|
+
label: 'View metrics',
|
|
64
|
+
description: 'View server metrics and resource usage',
|
|
65
|
+
powerful: false,
|
|
66
|
+
scopeKind: 'service',
|
|
67
|
+
implies: [],
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
// Logs
|
|
71
|
+
{
|
|
72
|
+
id: 'logs.view',
|
|
73
|
+
feature: 'Logs',
|
|
74
|
+
label: 'View logs',
|
|
75
|
+
description: 'View service and server logs',
|
|
76
|
+
powerful: false,
|
|
77
|
+
scopeKind: 'service',
|
|
78
|
+
implies: [],
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
// Environment files
|
|
82
|
+
{
|
|
83
|
+
id: 'env.view',
|
|
84
|
+
feature: 'Environment files',
|
|
85
|
+
label: 'View env files',
|
|
86
|
+
description: 'View env files with sensitive values masked',
|
|
87
|
+
powerful: false,
|
|
88
|
+
scopeKind: 'envfile',
|
|
89
|
+
implies: [],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: 'env.reveal',
|
|
93
|
+
feature: 'Environment files',
|
|
94
|
+
label: 'Reveal env values',
|
|
95
|
+
description: 'Reveal plaintext env values',
|
|
96
|
+
powerful: true,
|
|
97
|
+
scopeKind: 'envfile',
|
|
98
|
+
implies: [],
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: 'env.edit',
|
|
102
|
+
feature: 'Environment files',
|
|
103
|
+
label: 'Edit env files',
|
|
104
|
+
description: 'Modify env variables',
|
|
105
|
+
powerful: true,
|
|
106
|
+
scopeKind: 'envfile',
|
|
107
|
+
implies: ['env.view'],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'env.setup',
|
|
111
|
+
feature: 'Environment files',
|
|
112
|
+
label: 'Register env files',
|
|
113
|
+
description: 'Register or remove env-file entries',
|
|
114
|
+
powerful: true,
|
|
115
|
+
scopeKind: 'envfile',
|
|
116
|
+
implies: [],
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
id: 'env.restore',
|
|
120
|
+
feature: 'Environment files',
|
|
121
|
+
label: 'Restore env files',
|
|
122
|
+
description: 'Restore a previous env-file configuration',
|
|
123
|
+
powerful: true,
|
|
124
|
+
scopeKind: 'envfile',
|
|
125
|
+
implies: ['env.view'],
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
// Services
|
|
129
|
+
{
|
|
130
|
+
id: 'service.restart',
|
|
131
|
+
feature: 'Services',
|
|
132
|
+
label: 'Restart services',
|
|
133
|
+
description: 'Restart an authorized service',
|
|
134
|
+
powerful: true,
|
|
135
|
+
scopeKind: 'service',
|
|
136
|
+
implies: [],
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
// People
|
|
140
|
+
{
|
|
141
|
+
id: 'people.view',
|
|
142
|
+
feature: 'People',
|
|
143
|
+
label: 'View people',
|
|
144
|
+
description: 'View people and their team memberships',
|
|
145
|
+
powerful: false,
|
|
146
|
+
scopeKind: 'org',
|
|
147
|
+
implies: [],
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
id: 'people.invite',
|
|
151
|
+
feature: 'People',
|
|
152
|
+
label: 'Invite people',
|
|
153
|
+
description: 'Invite new users',
|
|
154
|
+
powerful: true,
|
|
155
|
+
scopeKind: 'org',
|
|
156
|
+
implies: [],
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
id: 'people.remove',
|
|
160
|
+
feature: 'People',
|
|
161
|
+
label: 'Remove people',
|
|
162
|
+
description: 'Remove users from the system',
|
|
163
|
+
powerful: true,
|
|
164
|
+
scopeKind: 'org',
|
|
165
|
+
implies: [],
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
// Teams
|
|
169
|
+
{
|
|
170
|
+
id: 'teams.view',
|
|
171
|
+
feature: 'Teams',
|
|
172
|
+
label: 'View teams',
|
|
173
|
+
description: 'View team structure',
|
|
174
|
+
powerful: false,
|
|
175
|
+
scopeKind: 'org',
|
|
176
|
+
implies: [],
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
id: 'teams.create',
|
|
180
|
+
feature: 'Teams',
|
|
181
|
+
label: 'Create teams',
|
|
182
|
+
description: 'Create a new team',
|
|
183
|
+
powerful: true,
|
|
184
|
+
scopeKind: 'org',
|
|
185
|
+
implies: [],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
id: 'teams.update',
|
|
189
|
+
feature: 'Teams',
|
|
190
|
+
label: 'Update teams',
|
|
191
|
+
description: 'Modify team properties',
|
|
192
|
+
powerful: true,
|
|
193
|
+
scopeKind: 'org',
|
|
194
|
+
implies: [],
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: 'teams.delete',
|
|
198
|
+
feature: 'Teams',
|
|
199
|
+
label: 'Delete teams',
|
|
200
|
+
description: 'Delete a team',
|
|
201
|
+
powerful: true,
|
|
202
|
+
scopeKind: 'org',
|
|
203
|
+
implies: [],
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
id: 'teams.members.manage',
|
|
207
|
+
feature: 'Teams',
|
|
208
|
+
label: 'Manage team members',
|
|
209
|
+
description: 'Add or remove team members',
|
|
210
|
+
powerful: true,
|
|
211
|
+
scopeKind: 'org',
|
|
212
|
+
implies: [],
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
id: 'teams.policies.manage',
|
|
216
|
+
feature: 'Teams',
|
|
217
|
+
label: 'Manage team policies',
|
|
218
|
+
description: 'Attach or detach policies from a team',
|
|
219
|
+
powerful: true,
|
|
220
|
+
scopeKind: 'org',
|
|
221
|
+
implies: [],
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
// Policies
|
|
225
|
+
{
|
|
226
|
+
id: 'policies.view',
|
|
227
|
+
feature: 'Policies',
|
|
228
|
+
label: 'View policies',
|
|
229
|
+
description: 'View available policies',
|
|
230
|
+
powerful: false,
|
|
231
|
+
scopeKind: 'org',
|
|
232
|
+
implies: [],
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: 'policies.create',
|
|
236
|
+
feature: 'Policies',
|
|
237
|
+
label: 'Create policies',
|
|
238
|
+
description: 'Create a new policy',
|
|
239
|
+
powerful: true,
|
|
240
|
+
scopeKind: 'org',
|
|
241
|
+
implies: [],
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
id: 'policies.update',
|
|
245
|
+
feature: 'Policies',
|
|
246
|
+
label: 'Update policies',
|
|
247
|
+
description: 'Modify a policy',
|
|
248
|
+
powerful: true,
|
|
249
|
+
scopeKind: 'org',
|
|
250
|
+
implies: [],
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
id: 'policies.delete',
|
|
254
|
+
feature: 'Policies',
|
|
255
|
+
label: 'Delete policies',
|
|
256
|
+
description: 'Delete a policy',
|
|
257
|
+
powerful: true,
|
|
258
|
+
scopeKind: 'org',
|
|
259
|
+
implies: [],
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
// Audit
|
|
263
|
+
{
|
|
264
|
+
id: 'audit.view',
|
|
265
|
+
feature: 'Audit',
|
|
266
|
+
label: 'View audit log',
|
|
267
|
+
description: 'View the audit log',
|
|
268
|
+
powerful: false,
|
|
269
|
+
scopeKind: 'org',
|
|
270
|
+
implies: [],
|
|
271
|
+
},
|
|
272
|
+
];
|
|
273
|
+
|
|
274
|
+
// Validate that all implied actions exist
|
|
275
|
+
const actionIds = new Set(ACTIONS.map((a) => a.id));
|
|
276
|
+
for (const action of ACTIONS) {
|
|
277
|
+
for (const implied of action.implies) {
|
|
278
|
+
if (!actionIds.has(implied)) {
|
|
279
|
+
throw new Error(`Action ${action.id} implies non-existent action ${implied}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function getAction(id) {
|
|
285
|
+
return ACTIONS.find((a) => a.id === id);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
module.exports = {
|
|
289
|
+
ACTIONS,
|
|
290
|
+
getAction,
|
|
291
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Delegation and privilege escalation guard
|
|
2
|
+
const { db } = require('../db');
|
|
3
|
+
const { serverScopeSubset, serviceScopeSubset, envFileScopeSubset } = require('./scope');
|
|
4
|
+
const { getAction } = require('./catalog');
|
|
5
|
+
|
|
6
|
+
// Check if actor can delegate the given rules
|
|
7
|
+
// Returns {ok: true} or {denied: [{rule, reason}]}
|
|
8
|
+
function canDelegate(actorSubject, rules) {
|
|
9
|
+
if (!actorSubject) return { denied: [{ reason: 'No subject' }] };
|
|
10
|
+
if (actorSubject.isRoot) return { ok: true };
|
|
11
|
+
|
|
12
|
+
const denied = [];
|
|
13
|
+
|
|
14
|
+
for (const rule of rules) {
|
|
15
|
+
if (!canDelegateRule(actorSubject, rule)) {
|
|
16
|
+
denied.push({ rule, reason: 'Rule exceeds delegation scope' });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return denied.length === 0 ? { ok: true } : { denied };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function canDelegateRule(actorSubject, ruleToDelegate) {
|
|
24
|
+
if (actorSubject.isRoot) return true;
|
|
25
|
+
|
|
26
|
+
// Actor must hold the action they're delegating
|
|
27
|
+
// A held rule covers an action it implies too (env.edit covers env.view).
|
|
28
|
+
const covers = (held, wanted) => {
|
|
29
|
+
if (held === wanted) return true;
|
|
30
|
+
const def = getAction(held);
|
|
31
|
+
return !!def && def.implies.some((i) => covers(i, wanted));
|
|
32
|
+
};
|
|
33
|
+
const actorRules = actorSubject.rules.filter((r) => covers(r.action, ruleToDelegate.action));
|
|
34
|
+
if (actorRules.length === 0) return false;
|
|
35
|
+
|
|
36
|
+
// Check if any actor rule can cover this rule's scope
|
|
37
|
+
return actorRules.some((actorRule) => ruleScopeSubset(ruleToDelegate, actorRule));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check if ruleA's scope is a subset of ruleB's scope
|
|
41
|
+
function ruleScopeSubset(ruleA, ruleB) {
|
|
42
|
+
// org actions have no scope
|
|
43
|
+
if (!ruleA.scope && !ruleB.scope) return true;
|
|
44
|
+
if (!ruleA.scope || !ruleB.scope) return false;
|
|
45
|
+
|
|
46
|
+
// Check servers. ids ⊆ tags is only provable point-in-time against current access tags.
|
|
47
|
+
const a = ruleA.scope.servers;
|
|
48
|
+
const b = ruleB.scope.servers;
|
|
49
|
+
if (a.type === 'ids' && b.type === 'tags') {
|
|
50
|
+
if (!instanceIdsSatisfyTags(a.ids, b)) return false;
|
|
51
|
+
} else if (!serverScopeSubset(a, b)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Check services
|
|
56
|
+
if (!serviceScopeSubset(ruleA.scope.services, ruleB.scope.services)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Check env files
|
|
61
|
+
if (!envFileScopeSubset(ruleA.scope.envFiles, ruleB.scope.envFiles)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// For tag scopes: check if instanceIds currently satisfy the tag condition
|
|
69
|
+
// Used at delegation time to determine if ids A are a subset of tags B
|
|
70
|
+
function instanceIdsSatisfyTags(instanceIds, tagScope) {
|
|
71
|
+
if (tagScope.type !== 'tags') return false;
|
|
72
|
+
|
|
73
|
+
const match = tagScope.match || 'all';
|
|
74
|
+
|
|
75
|
+
for (const instanceId of instanceIds) {
|
|
76
|
+
const tags = db
|
|
77
|
+
.prepare(
|
|
78
|
+
`
|
|
79
|
+
SELECT tag FROM instance_auth_tags WHERE instance_id = ?
|
|
80
|
+
`,
|
|
81
|
+
)
|
|
82
|
+
.all(instanceId);
|
|
83
|
+
const instanceTags = tags.map((t) => t.tag);
|
|
84
|
+
|
|
85
|
+
if (match === 'all') {
|
|
86
|
+
if (!tagScope.tags.every((t) => instanceTags.includes(t))) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
if (!tagScope.tags.some((t) => instanceTags.includes(t))) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = {
|
|
100
|
+
canDelegate,
|
|
101
|
+
canDelegateRule,
|
|
102
|
+
ruleScopeSubset,
|
|
103
|
+
instanceIdsSatisfyTags,
|
|
104
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// "What can this person access?" — a per-person, per-server breakdown of the actions their
|
|
2
|
+
// teams' policies grant, with provenance (which team + policy granted each). Powers
|
|
3
|
+
// GET /api/users/:id/access and GET /api/me/access.
|
|
4
|
+
const db = require('../db');
|
|
5
|
+
const store = require('../authz-store');
|
|
6
|
+
const engine = require('./engine');
|
|
7
|
+
const { ACTIONS, getAction } = require('./catalog');
|
|
8
|
+
const { serverMatches } = require('./scope');
|
|
9
|
+
|
|
10
|
+
function grants(rule, action) {
|
|
11
|
+
if (rule.action === action) return true;
|
|
12
|
+
const def = getAction(rule.action);
|
|
13
|
+
return !!def && def.implies.some((i) => grants({ action: i }, action));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function describeMatcher(m) {
|
|
17
|
+
const v = `"${m.value}"`;
|
|
18
|
+
switch (m.op) {
|
|
19
|
+
case 'startsWith':
|
|
20
|
+
return `starting with ${v}`;
|
|
21
|
+
case 'endsWith':
|
|
22
|
+
return `ending with ${v}`;
|
|
23
|
+
case 'contains':
|
|
24
|
+
return `containing ${v}`;
|
|
25
|
+
case 'exact':
|
|
26
|
+
return `named ${v}`;
|
|
27
|
+
default:
|
|
28
|
+
return `matching custom pattern ${v}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function describeScope(scope, def) {
|
|
33
|
+
if (!scope) return '';
|
|
34
|
+
const parts = [];
|
|
35
|
+
if (def.scopeKind !== 'org' && def.scopeKind !== 'server' && scope.services) {
|
|
36
|
+
if (scope.services.matchers.length > 0)
|
|
37
|
+
parts.push(`services ${scope.services.matchers.map(describeMatcher).join(' or ')}`);
|
|
38
|
+
}
|
|
39
|
+
if (def.scopeKind === 'envfile' && scope.envFiles)
|
|
40
|
+
parts.push(`${scope.envFiles.ids.length} specific env file(s)`);
|
|
41
|
+
return parts.length ? `Only ${parts.join(', ')}` : 'All services';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function provenance(rules) {
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const r of rules) {
|
|
48
|
+
const key = `${r.teamId}|${r.policyId}`;
|
|
49
|
+
if (seen.has(key)) continue;
|
|
50
|
+
seen.add(key);
|
|
51
|
+
out.push({ teamName: r.teamName, policyName: r.policyName });
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function effectiveAccess(user) {
|
|
57
|
+
const teams = store.getUserTeams(user.id).map((t) => ({ id: t.id, name: t.name }));
|
|
58
|
+
const subject = engine.buildSubject(user, store);
|
|
59
|
+
if (!subject || subject.isRoot)
|
|
60
|
+
return { isRoot: !!(subject && subject.isRoot), teams, org: [], servers: [] };
|
|
61
|
+
|
|
62
|
+
const org = [];
|
|
63
|
+
for (const def of ACTIONS.filter((a) => a.scopeKind === 'org')) {
|
|
64
|
+
const rs = subject.rules.filter((r) => grants(r, def.id));
|
|
65
|
+
if (rs.length) org.push({ action: def.id, label: def.label, grantedBy: provenance(rs) });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const tagsOf = new Map();
|
|
69
|
+
for (const t of db.db.prepare('SELECT instance_id, tag FROM instance_auth_tags').all()) {
|
|
70
|
+
if (!tagsOf.has(t.instance_id)) tagsOf.set(t.instance_id, []);
|
|
71
|
+
tagsOf.get(t.instance_id).push(t.tag);
|
|
72
|
+
}
|
|
73
|
+
const authTagsOf = (id) => tagsOf.get(id) || [];
|
|
74
|
+
|
|
75
|
+
const servers = [];
|
|
76
|
+
for (const inst of db.listInstances()) {
|
|
77
|
+
const byFeature = new Map();
|
|
78
|
+
for (const def of ACTIONS.filter((a) => a.scopeKind !== 'org')) {
|
|
79
|
+
const rs = subject.rules.filter(
|
|
80
|
+
(r) =>
|
|
81
|
+
r.scope &&
|
|
82
|
+
grants(r, def.id) &&
|
|
83
|
+
serverMatches(r.scope.servers, { id: inst.id }, authTagsOf),
|
|
84
|
+
);
|
|
85
|
+
if (rs.length === 0) continue;
|
|
86
|
+
if (!byFeature.has(def.feature)) byFeature.set(def.feature, []);
|
|
87
|
+
byFeature.get(def.feature).push({
|
|
88
|
+
action: def.id,
|
|
89
|
+
label: def.label,
|
|
90
|
+
scopeDescription: [...new Set(rs.map((r) => describeScope(r.scope, def)))].join('; '),
|
|
91
|
+
grantedBy: provenance(rs),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (byFeature.size === 0) continue;
|
|
95
|
+
servers.push({
|
|
96
|
+
instanceId: inst.id,
|
|
97
|
+
label: inst.label,
|
|
98
|
+
features: [...byFeature].map(([feature, actions]) => ({ feature, actions })),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return { isRoot: false, teams, org, servers };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { effectiveAccess, describeScope };
|