pgkiln 0.36.0 → 0.38.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/SECURITY.md +12 -9
- package/db/migrations/090_workspace_isolation.sql +512 -0
- package/db/migrations/091_workspace_service.sql +20 -0
- package/db/migrations/092_app_groups.sql +23 -0
- package/docs/apex-feature-parity.md +16 -11
- package/docs/guide/03-builder.md +125 -11
- package/docs/guide/12-development.md +12 -2
- package/examples/gallery/README.md +13 -0
- package/examples/gallery/gallery.json +20 -0
- package/examples/gallery/starters/asset-inventory.json +30 -0
- package/examples/gallery/starters/contacts.json +41 -0
- package/examples/gallery/starters/event-registrations.json +29 -0
- package/examples/gallery/starters/expense-claims.json +32 -0
- package/examples/gallery/starters/team-tasks.json +35 -0
- package/examples/hr/hr_40_sample_data.sql +1 -1
- package/package.json +1 -1
- package/public/builder.css +91 -0
- package/src/app.ts +24 -0
- package/src/automations.ts +3 -2
- package/src/builder/advisor.ts +11 -5
- package/src/builder/ai-builder.ts +27 -18
- package/src/builder/app-groups.ts +132 -0
- package/src/builder/blueprints.ts +21 -7
- package/src/builder/code-editor.ts +3 -2
- package/src/builder/cross-app.ts +106 -0
- package/src/builder/dataload.ts +24 -14
- package/src/builder/ddl-tools.ts +7 -4
- package/src/builder/documents.ts +2 -1
- package/src/builder/gallery.ts +154 -0
- package/src/builder/home.ts +33 -10
- package/src/builder/newapp.ts +21 -3
- package/src/builder/quicksql.ts +3 -2
- package/src/builder/report-settings.ts +4 -2
- package/src/builder/routes.ts +78 -26
- package/src/builder/sampledata.ts +22 -18
- package/src/builder/scripts.ts +31 -16
- package/src/builder/sql.ts +119 -15
- package/src/builder/ui.ts +30 -7
- package/src/builder/unload.ts +3 -2
- package/src/builder/workspaces.ts +291 -22
- package/src/cli/replace.ts +2 -0
- package/src/db.ts +36 -6
- package/src/ddl.ts +2 -1
- package/src/debug.ts +3 -2
- package/src/migrate.ts +3 -0
- package/src/process-jobs.ts +7 -2
- package/src/restsync.ts +3 -2
- package/src/runtime/calendar.ts +2 -1
- package/src/runtime/routes.ts +4 -1
- package/src/sampledata.ts +3 -3
- package/src/session.ts +18 -4
- package/src/websources.ts +3 -1
- package/src/workflow.ts +65 -9
- package/src/workspace-db.ts +166 -0
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { FastifyInstance, FastifyReply } from 'fastify';
|
|
2
|
-
import { owner } from '../db.ts';
|
|
2
|
+
import { owner, type Db } from '../db.ts';
|
|
3
|
+
import { devDbForWorkspace, workspacesChanged } from '../workspace-db.ts';
|
|
4
|
+
import { ownerUrl, runtimeUrl } from '../db.ts';
|
|
5
|
+
import { secretKeyConfigured } from '../secrets.ts';
|
|
3
6
|
import { html, raw } from '../html.ts';
|
|
4
7
|
import { icon } from '../icons.ts';
|
|
5
|
-
import { clientIp, logActivity, type Session } from '../session.ts';
|
|
8
|
+
import { clientIp, logActivity, type BuilderWorkspace, type Session } from '../session.ts';
|
|
6
9
|
import { isAdmin } from './locks.ts';
|
|
7
|
-
import { back, BASE, csrf, developer, flash, input, region, send, shell, type Req } from './ui.ts';
|
|
10
|
+
import { back, BANNER_COLORS, BASE, csrf, developer, flash, input, region, select, send, shell, type Req } from './ui.ts';
|
|
8
11
|
|
|
9
12
|
// Workspaces (064; APEX: workspaces). A workspace groups applications and the
|
|
10
13
|
// developers who build them. developer() loads the developer's workspaces into
|
|
@@ -17,10 +20,7 @@ import { back, BASE, csrf, developer, flash, input, region, send, shell, type Re
|
|
|
17
20
|
// Not a security boundary between developers who write SQL (the SQL Workshop
|
|
18
21
|
// and application code run with installation-wide rights): see chapter 3.
|
|
19
22
|
|
|
20
|
-
export
|
|
21
|
-
id: number;
|
|
22
|
-
name: string;
|
|
23
|
-
}
|
|
23
|
+
export type WorkspaceRef = BuilderWorkspace;
|
|
24
24
|
|
|
25
25
|
/** Workspace 1: applications without a meta.workspace_app row belong to it. */
|
|
26
26
|
export const DEFAULT_WORKSPACE = 1;
|
|
@@ -29,7 +29,10 @@ export const DEFAULT_WORKSPACE = 1;
|
|
|
29
29
|
export async function loadWorkspaces(s: Session) {
|
|
30
30
|
const rows = (
|
|
31
31
|
await owner.query<WorkspaceRef>(
|
|
32
|
-
`select w.id, w.name
|
|
32
|
+
`select w.id, w.name, w.message, w.banner_label, w.banner_color, w.isolated,
|
|
33
|
+
exists (select 1 from meta.developer d where d.username = $1 and d.is_admin)
|
|
34
|
+
or exists (select 1 from meta.workspace_member m where m.workspace_id = w.id and m.username = $1 and m.is_admin) as admin
|
|
35
|
+
from meta.workspace w
|
|
33
36
|
where exists (select 1 from meta.developer d where d.username = $1 and d.is_admin)
|
|
34
37
|
or exists (select 1 from meta.workspace_member m where m.workspace_id = w.id and m.username = $1)
|
|
35
38
|
order by w.id = 1 desc, lower(w.name)`,
|
|
@@ -68,6 +71,33 @@ export async function appAllowed(s: Session, appId: number | string) {
|
|
|
68
71
|
return true;
|
|
69
72
|
}
|
|
70
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The connection for a developer's own SQL (the SQL Workshop's tools): the owner, or in an
|
|
76
|
+
* isolated workspace (090) its developer login, which reaches the workspace's application
|
|
77
|
+
* roles only. Administrators keep the owner.
|
|
78
|
+
*/
|
|
79
|
+
export async function workshopDb(s: Session): Promise<Db> {
|
|
80
|
+
if (!s.workspace || (await isAdmin(s.username))) return owner;
|
|
81
|
+
return (await devDbForWorkspace(s.workspace.id)) ?? owner;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Schemas the SQL Workshop's connection may use (not pgkiln's, not the system's). */
|
|
85
|
+
export async function usableSchemas(db: Db) {
|
|
86
|
+
return (await db.query<{ n: string }>(
|
|
87
|
+
`select nspname as n from pg_namespace where nspname !~ '^pg_' and nspname not in ('information_schema', 'meta')
|
|
88
|
+
and has_schema_privilege(oid, 'USAGE') order by 1`)).rows.map((r) => r.n);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** (sprint 61) The session's default schema (SQL Workshop home), if the connection may still use it. */
|
|
92
|
+
export async function sessionSchema(s: Session, db: Db) {
|
|
93
|
+
const v = s.state.__SQLSCHEMA;
|
|
94
|
+
if (typeof v !== 'string' || !v) return null;
|
|
95
|
+
return (await db.one<{ ok: boolean }>(`select has_schema_privilege(oid, 'USAGE') as ok from pg_namespace where nspname = $1`, [v]))?.ok ? v : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Who the SQL Workshop runs as, for its notes. */
|
|
99
|
+
export const workshopRunsAs = (db: Db) => (db === owner ? 'the builder\'s owner connection' : 'this isolated workspace\'s developer login (its applications\' roles, nothing else)');
|
|
100
|
+
|
|
71
101
|
/** Put an application in a workspace (inside the caller's transaction, or on the owner pool). */
|
|
72
102
|
export async function placeApp(db: { query: (sql: string, params?: unknown[]) => Promise<unknown> }, appId: number, workspaceId: number) {
|
|
73
103
|
await db.query(
|
|
@@ -99,6 +129,18 @@ async function adminOnly(s: Session, reply: FastifyReply) {
|
|
|
99
129
|
return false;
|
|
100
130
|
}
|
|
101
131
|
|
|
132
|
+
/** An administrator, or a workspace administrator of this workspace (090). */
|
|
133
|
+
export async function workspaceAdmin(username: string | null, workspaceId: number | string) {
|
|
134
|
+
if (!username) return false;
|
|
135
|
+
if (await isAdmin(username)) return true;
|
|
136
|
+
return !!(await owner.one('select 1 as ok from meta.workspace_member where workspace_id = $1 and username = $2 and is_admin', [workspaceId, username]));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function workspaceAdminOnly(s: Session, reply: FastifyReply, workspaceId: string) {
|
|
140
|
+
if (await workspaceAdmin(s.username, workspaceId)) return true;
|
|
141
|
+
return adminOnly(s, reply);
|
|
142
|
+
}
|
|
143
|
+
|
|
102
144
|
const crumbs = (extra: [string, string?][] = []): [string, string?][] => [['App Builder', BASE], ['Workspace utilities', `${BASE}/utilities`], ['Workspaces', `${BASE}/workspaces`], ...extra];
|
|
103
145
|
|
|
104
146
|
const nameError = (e: unknown) => {
|
|
@@ -108,6 +150,62 @@ const nameError = (e: unknown) => {
|
|
|
108
150
|
return m;
|
|
109
151
|
};
|
|
110
152
|
|
|
153
|
+
const ROLE = /^[A-Za-z_][A-Za-z0-9_$]{0,62}$/;
|
|
154
|
+
|
|
155
|
+
/** Why this installation can't isolate a workspace (090), or null. */
|
|
156
|
+
export function isolationProblem(): string | null {
|
|
157
|
+
if (!secretKeyConfigured()) return 'set PGKILN_SECRET_KEY (at least 32 characters) in the server\'s environment: the workspace\'s database logins get passwords derived from it.';
|
|
158
|
+
if (runtimeUrl === ownerUrl) return 'set RUNTIME_DATABASE_URL: applications run on the owner connection, so no workspace can be isolated.';
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A role problem from meta.workspace_sync_roles (or a duplicate role) in words. */
|
|
163
|
+
const roleError = (e: unknown) => {
|
|
164
|
+
const m = (e as Error).message;
|
|
165
|
+
if (m.includes('workspace_role_role_name_key')) return 'this role belongs to another workspace already.';
|
|
166
|
+
if (m.includes('workspace_role_pkey')) return 'the workspace has this role already.';
|
|
167
|
+
return m;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** The Isolation region of a workspace's page. */
|
|
171
|
+
function isolationRegion(s: Session, w: { id: number; name: string; isolated: boolean; run_role: string | null; dev_role: string | null },
|
|
172
|
+
roles: { role_name: string; exists: boolean; apps: string | null }[]) {
|
|
173
|
+
const problem = w.isolated ? null : isolationProblem();
|
|
174
|
+
const body = w.isolated
|
|
175
|
+
? html`<p class="u-mt0"><span class="tag">${icon('lock')} isolated</span> Its applications run as <code>${w.run_role}</code> and its developers' SQL (SQL Workshop, code checks, the Advisor) as <code>${w.dev_role}</code>. Both reach only the roles below, and see only this workspace's applications, sessions and accounts in pgkiln's own tables. Developers who also belong to a shared workspace keep that workspace's rights there.</p>
|
|
176
|
+
<div class="table-wrap"><table class="report report-reflow"><thead><tr><th>Database role</th><th>Used by</th><th></th></tr></thead><tbody>
|
|
177
|
+
${roles.length
|
|
178
|
+
? roles.map((r) => html`<tr><td data-label="Database role"><code>${r.role_name}</code>${r.exists ? '' : html` <span class="tag tag-error">missing</span>`}</td>
|
|
179
|
+
<td data-label="Used by">${r.apps ?? html`<span class="muted">no application</span>`}</td>
|
|
180
|
+
<td><form method="post" action="${BASE}/workspaces/${w.id}/roles">${csrf(s)}<input type="hidden" name="role" value="${r.role_name}">
|
|
181
|
+
<button class="btn btn-sm" name="action" value="remove">Remove</button></form></td></tr>`)
|
|
182
|
+
: html`<tr><td colspan="3" class="empty">No database roles: applications here can't run as a role of their own.</td></tr>`}</tbody></table></div>
|
|
183
|
+
<form method="post" action="${BASE}/workspaces/${w.id}/roles" class="u-mt1">${csrf(s)}
|
|
184
|
+
${input('role', 'Add a database role', '', { help: 'An existing role (not a superuser, not one that creates roles or bypasses row-level security). Its rights become this workspace\'s; it can\'t belong to another workspace.' })}
|
|
185
|
+
<div class="buttons"><button class="btn" name="action" value="add">${icon('plus')} Add role</button></div></form>
|
|
186
|
+
<form method="post" action="${BASE}/workspaces/${w.id}/isolation" class="danger-zone">${csrf(s)}<input type="hidden" name="isolated" value="false">
|
|
187
|
+
<button class="btn btn-danger" data-confirm="Make ${w.name} shared again? Its applications then run on the shared runtime connection, and its developers' SQL as the owner.">Make shared</button></form>`
|
|
188
|
+
: html`<p class="u-mt0">Shared: the applications run on the shared runtime connection and the developers' SQL as the owner, like every shared workspace's.</p>
|
|
189
|
+
<p class="muted small">Isolating gives the workspace two database logins of its own and the database roles its applications use now. Application code then reaches only those roles and this workspace's applications; its developers' SQL Workshop runs on the workspace's login instead of the owner. New applications get a new role and schema of their own.</p>
|
|
190
|
+
${problem ? html`<div class="alert alert-error" role="alert">To isolate a workspace, ${problem}</div>` : ''}
|
|
191
|
+
<form method="post" action="${BASE}/workspaces/${w.id}/isolation">${csrf(s)}<input type="hidden" name="isolated" value="true">
|
|
192
|
+
<div class="buttons"><button class="btn btn-hot"${problem ? raw(' disabled') : ''} data-confirm="Isolate ${w.name}?">${icon('lock')} Isolate this workspace</button></div></form>`;
|
|
193
|
+
return region('Isolation', body);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** (091) Manage service: the workspace message and the environment banner. */
|
|
197
|
+
function serviceRegion(s: Session, w: { id: number; message: string | null; banner_label: string | null; banner_color: string }) {
|
|
198
|
+
return region('Manage service', html`<form method="post" action="${BASE}/workspaces/${w.id}/service">${csrf(s)}
|
|
199
|
+
<div class="field"><label class="label" for="f_message">Workspace message</label>
|
|
200
|
+
<textarea id="f_message" name="message" rows="3" maxlength="2000">${w.message ?? ''}</textarea>
|
|
201
|
+
<small class="help">Shown to this workspace's developers on the App Builder home, e.g. a maintenance window. Plain text.</small></div>
|
|
202
|
+
<div class="form-grid">
|
|
203
|
+
${input('banner_label', 'Environment banner', w.banner_label ?? '', { placeholder: 'DEVELOPMENT', help: 'A short label shown at the top of every builder page while this workspace is current. Empty: no banner.' })}
|
|
204
|
+
${select('banner_color', 'Banner colour', w.banner_color, BANNER_COLORS.map((c): [string, string] => [c, c[0].toUpperCase() + c.slice(1)]))}
|
|
205
|
+
</div>
|
|
206
|
+
<div class="buttons"><button class="btn">Save</button></div></form>`, '', 'service');
|
|
207
|
+
}
|
|
208
|
+
|
|
111
209
|
export async function workspaceRoutes(app: FastifyInstance) {
|
|
112
210
|
// switch the current workspace (any of the developer's own)
|
|
113
211
|
app.post(`${BASE}/workspace`, async (req: Req, reply) => {
|
|
@@ -125,7 +223,7 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
125
223
|
if (!s || !(await adminOnly(s, reply))) return;
|
|
126
224
|
const rows = (
|
|
127
225
|
await owner.query(
|
|
128
|
-
`select w.id, w.name, w.description,
|
|
226
|
+
`select w.id, w.name, w.description, w.isolated,
|
|
129
227
|
(select count(*) from meta.app a where meta.app_workspace(a.id) = w.id)::int as apps,
|
|
130
228
|
(select string_agg(m.username, ', ' order by m.username) from meta.workspace_member m where m.workspace_id = w.id) as members
|
|
131
229
|
from meta.workspace w order by w.id = 1 desc, lower(w.name)`,
|
|
@@ -133,12 +231,12 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
133
231
|
).rows;
|
|
134
232
|
const main = html`
|
|
135
233
|
<div class="title-row"><h1>Workspaces</h1></div>
|
|
136
|
-
<p class="muted u-mt0">A workspace groups applications and the developers who build them. Developers see and change the applications of their own workspaces; administrators see all of them.
|
|
234
|
+
<p class="muted u-mt0">A workspace groups applications and the developers who build them. Developers see and change the applications of their own workspaces; administrators see all of them. A shared workspace is not a security boundary between developers who write SQL: its SQL Workshop and application code run with this installation's rights. An <strong>isolated</strong> workspace is: its applications and SQL run on database logins of its own that reach only its database roles and its own applications.</p>
|
|
137
235
|
<div class="columns wide-left">
|
|
138
236
|
${region('Workspaces', html`<div class="table-wrap"><table class="report report-reflow">
|
|
139
237
|
<thead><tr><th>Workspace</th><th class="num">Applications</th><th>Developers</th></tr></thead>
|
|
140
238
|
<tbody>${rows.map((w) => html`<tr>
|
|
141
|
-
<td data-label="Workspace"><a href="${BASE}/workspaces/${w.id}">${w.name}</a>${w.description ? html` <span class="muted">${w.description}</span>` : ''}</td>
|
|
239
|
+
<td data-label="Workspace"><a href="${BASE}/workspaces/${w.id}">${w.name}</a>${w.isolated ? html` <span class="tag">${icon('lock')} isolated</span>` : ''}${w.description ? html` <span class="muted">${w.description}</span>` : ''}</td>
|
|
142
240
|
<td class="num" data-label="Applications">${w.apps}</td>
|
|
143
241
|
<td data-label="Developers">${w.members ?? html`<span class="muted">administrators only</span>`}</td></tr>`)}</tbody></table></div>`)}
|
|
144
242
|
${region('Add workspace', html`<form method="post" action="${BASE}/workspaces">${csrf(s)}
|
|
@@ -175,18 +273,26 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
175
273
|
|
|
176
274
|
app.get(`${BASE}/workspaces/:id(^\\d{1,9}$)`, async (req: Req, reply) => {
|
|
177
275
|
const s = await developer(req, reply);
|
|
178
|
-
if (!s || !(await
|
|
276
|
+
if (!s || !(await workspaceAdminOnly(s, reply, req.params.id))) return;
|
|
277
|
+
const full = await isAdmin(s.username);
|
|
179
278
|
const w = await owner.one('select * from meta.workspace where id = $1', [req.params.id]);
|
|
180
279
|
if (!w) return reply.code(404).send('Not found');
|
|
181
|
-
const [devs, apps, others] = await Promise.all([
|
|
280
|
+
const [devs, apps, others, rolesQ] = await Promise.all([
|
|
182
281
|
owner.query(
|
|
183
|
-
`select d.username, d.is_admin, m.username is not null as member
|
|
282
|
+
`select d.username, d.is_admin, m.username is not null as member, coalesce(m.is_admin, false) as ws_admin
|
|
184
283
|
from meta.developer d left join meta.workspace_member m on m.username = d.username and m.workspace_id = $1 order by d.username`,
|
|
185
284
|
[w.id],
|
|
186
285
|
),
|
|
187
286
|
owner.query(`select a.id, a.name, a.alias from meta.app a where meta.app_workspace(a.id) = $1 order by lower(a.name), a.id`, [w.id]),
|
|
188
287
|
owner.query('select id, name from meta.workspace where id <> $1 order by id = 1 desc, lower(name)', [w.id]),
|
|
288
|
+
owner.query(
|
|
289
|
+
`select x.role_name, exists (select 1 from pg_roles r where r.rolname = x.role_name) as exists,
|
|
290
|
+
(select string_agg(a.alias, ', ' order by a.alias) from meta.app a where a.db_role = x.role_name) as apps
|
|
291
|
+
from meta.workspace_role x where x.workspace_id = $1 order by 1`,
|
|
292
|
+
[w.id],
|
|
293
|
+
),
|
|
189
294
|
]);
|
|
295
|
+
const roles = rolesQ;
|
|
190
296
|
const main = html`
|
|
191
297
|
<div class="title-row"><h1>${w.name}</h1></div>
|
|
192
298
|
<div class="columns wide-left">
|
|
@@ -194,7 +300,7 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
194
300
|
${input('name', 'Name', w.name, { required: true })}
|
|
195
301
|
${input('description', 'Description', w.description ?? '')}
|
|
196
302
|
<div class="buttons"><button class="btn btn-hot">Save</button></div></form>
|
|
197
|
-
${w.id === 1
|
|
303
|
+
${!full ? '' : w.id === 1
|
|
198
304
|
? html`<p class="muted small">The Default workspace can't be deleted: imported applications and applications made with the command line start here.</p>`
|
|
199
305
|
: html`<form method="post" action="${BASE}/workspaces/${w.id}/delete" class="danger-zone">${csrf(s)}
|
|
200
306
|
<button class="btn btn-danger" data-confirm="Delete workspace ${w.name}?"${apps.rows.length ? raw(' disabled') : ''}>Delete workspace</button>
|
|
@@ -204,9 +310,14 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
204
310
|
<fieldset class="field"><legend class="sr-only">Developers</legend><div class="radio-group">
|
|
205
311
|
${devs.rows.map((d) => html`<label class="check"><input type="checkbox" name="member" value="${d.username}"${d.member ? raw(' checked') : ''}> ${d.username}${d.is_admin ? html` <span class="muted">(administrator)</span>` : ''}</label>`)}
|
|
206
312
|
</div></fieldset>
|
|
313
|
+
<fieldset class="field"><legend class="label">Workspace administrators</legend><div class="radio-group">
|
|
314
|
+
${devs.rows.filter((d) => d.member && !d.is_admin).map((d) => html`<label class="check"><input type="checkbox" name="admin" value="${d.username}"${d.ws_admin ? raw(' checked') : ''}> ${d.username}</label>`)}
|
|
315
|
+
</div><small class="help">They manage this workspace's name and developers. Isolation, database roles, moving applications and deleting the workspace stay with administrators.</small></fieldset>
|
|
207
316
|
<div class="buttons"><button class="btn">Save developers</button></div></form>`)}
|
|
208
317
|
</div>
|
|
209
|
-
${
|
|
318
|
+
${serviceRegion(s, w)}
|
|
319
|
+
${full ? isolationRegion(s, w, roles.rows) : region('Isolation', html`<p class="u-mt0">${w.isolated ? 'Isolated: its applications and SQL run on database logins of its own.' : 'Shared.'} Administrators change it.</p>`)}
|
|
320
|
+
${!full ? '' : region('Applications', html`<div class="table-wrap"><table class="report report-reflow">
|
|
210
321
|
<thead><tr><th>Application</th><th>Move to</th></tr></thead>
|
|
211
322
|
<tbody>${apps.rows.length
|
|
212
323
|
? apps.rows.map((a) => html`<tr><td data-label="Application"><a href="${BASE}/apps/${a.id}">${a.name}</a> <span class="muted">${a.id} · /a/${a.alias}</span></td>
|
|
@@ -217,12 +328,12 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
217
328
|
<button class="btn">Move</button></form>`
|
|
218
329
|
: html`<span class="muted">no other workspace</span>`}</td></tr>`)
|
|
219
330
|
: html`<tr><td colspan="2" class="empty">No applications.</td></tr>`}</tbody></table></div>`)}`;
|
|
220
|
-
return send(reply, s, shell(s, w.name, crumbs([[w.name]]), main));
|
|
331
|
+
return send(reply, s, shell(s, w.name, full ? crumbs([[w.name]]) : [['App Builder', BASE], ['Workspace utilities', `${BASE}/utilities`], [w.name]], main));
|
|
221
332
|
});
|
|
222
333
|
|
|
223
334
|
app.post(`${BASE}/workspaces/:id(^\\d{1,9}$)`, async (req: Req, reply) => {
|
|
224
335
|
const s = await developer(req, reply);
|
|
225
|
-
if (!s || !(await
|
|
336
|
+
if (!s || !(await workspaceAdminOnly(s, reply, req.params.id))) return;
|
|
226
337
|
const name = String(req.body?.name ?? '').trim();
|
|
227
338
|
try {
|
|
228
339
|
if (!NAME.test(name)) throw new Error('The name is 1 to 60 characters.');
|
|
@@ -236,16 +347,19 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
236
347
|
|
|
237
348
|
app.post(`${BASE}/workspaces/:id(^\\d{1,9}$)/members`, async (req: Req, reply) => {
|
|
238
349
|
const s = await developer(req, reply);
|
|
239
|
-
if (!s || !(await
|
|
350
|
+
if (!s || !(await workspaceAdminOnly(s, reply, req.params.id))) return;
|
|
240
351
|
if (!(await owner.one('select 1 as ok from meta.workspace where id = $1', [req.params.id]))) return reply.code(404).send('Not found');
|
|
241
352
|
const posted = req.body?.member as unknown;
|
|
242
353
|
const names = (Array.isArray(posted) ? posted : posted == null ? [] : [posted]).map(String).slice(0, 10000);
|
|
354
|
+
const postedAdmins = req.body?.admin as unknown;
|
|
355
|
+
const admins = (Array.isArray(postedAdmins) ? postedAdmins : postedAdmins == null ? [] : [postedAdmins]).map(String).slice(0, 10000);
|
|
243
356
|
await owner.tx(async (c) => {
|
|
244
357
|
await c.query('delete from meta.workspace_member where workspace_id = $1', [req.params.id]);
|
|
245
358
|
// only existing developers (unknown names are ignored)
|
|
246
359
|
await c.query(
|
|
247
|
-
`insert into meta.workspace_member (workspace_id, username
|
|
248
|
-
|
|
360
|
+
`insert into meta.workspace_member (workspace_id, username, is_admin)
|
|
361
|
+
select $1, d.username, d.username = any($3::text[]) from meta.developer d where d.username = any($2::text[])`,
|
|
362
|
+
[req.params.id, names, admins],
|
|
249
363
|
);
|
|
250
364
|
});
|
|
251
365
|
await logActivity({ appId: null, username: s.username, event: 'workspace', ip: clientIp(req), detail: `developers of workspace ${req.params.id}: ${names.join(', ').slice(0, 1000)}` });
|
|
@@ -260,7 +374,30 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
260
374
|
const appId = /^\d{1,9}$/.test(req.body?.app ?? '') ? Number(req.body!.app) : null;
|
|
261
375
|
const to = (await owner.one<{ id: number; name: string }>('select id, name from meta.workspace where id = $1', [/^\d{1,9}$/.test(req.body?.to ?? '') ? req.body!.to : null]));
|
|
262
376
|
if (appId === null || !to || (await appWorkspace(appId)) !== from) return reply.code(404).send('Not found');
|
|
263
|
-
|
|
377
|
+
try {
|
|
378
|
+
await owner.tx(async (c) => {
|
|
379
|
+
// (090) the application's database role goes along: out of the old workspace's roles (when no other
|
|
380
|
+
// application there uses it), into the new one's when that one is isolated
|
|
381
|
+
const role = (await c.query<{ db_role: string | null }>('select db_role from meta.app where id = $1', [appId])).rows[0]?.db_role;
|
|
382
|
+
if (role) {
|
|
383
|
+
await c.query(
|
|
384
|
+
`delete from meta.workspace_role x where x.workspace_id = $1 and x.role_name = $2
|
|
385
|
+
and not exists (select 1 from meta.app a where a.db_role = $2 and a.id <> $3 and meta.app_workspace(a.id) = $1)`,
|
|
386
|
+
[from, role, appId],
|
|
387
|
+
);
|
|
388
|
+
if ((await c.query('select 1 from meta.workspace where id = $1 and isolated', [to.id])).rowCount)
|
|
389
|
+
await c.query('insert into meta.workspace_role (workspace_id, role_name) values ($1, $2) on conflict do nothing', [to.id, role]);
|
|
390
|
+
}
|
|
391
|
+
await placeApp(c, appId, to.id);
|
|
392
|
+
// (092) groups are the old workspace's
|
|
393
|
+
await c.query('delete from meta.app_group_member where app_id = $1', [appId]);
|
|
394
|
+
});
|
|
395
|
+
} catch (e) {
|
|
396
|
+
flash(s, `Not moved: ${roleError(e)}`, 'error');
|
|
397
|
+
return back(reply, s, `${BASE}/workspaces/${from}`);
|
|
398
|
+
} finally {
|
|
399
|
+
await workspacesChanged();
|
|
400
|
+
}
|
|
264
401
|
await logActivity({ appId, username: s.username, event: 'workspace', ip: clientIp(req), detail: `moved from workspace ${from} to ${to.id}` });
|
|
265
402
|
flash(s, `Application moved to ${to.name}.`);
|
|
266
403
|
return back(reply, s, `${BASE}/workspaces/${from}`);
|
|
@@ -274,6 +411,10 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
274
411
|
flash(s, 'The Default workspace cannot be deleted.', 'error');
|
|
275
412
|
return back(reply, s, `${BASE}/workspaces/${id}`);
|
|
276
413
|
}
|
|
414
|
+
if (await owner.one('select 1 as ok from meta.workspace where id = $1 and isolated', [id])) {
|
|
415
|
+
flash(s, 'Make the workspace shared (not isolated) before deleting it.', 'error');
|
|
416
|
+
return back(reply, s, `${BASE}/workspaces/${id}`);
|
|
417
|
+
}
|
|
277
418
|
if (await owner.one('select 1 as ok from meta.workspace_app where workspace_id = $1 limit 1', [id])) {
|
|
278
419
|
flash(s, 'Move the applications of this workspace to another workspace first.', 'error');
|
|
279
420
|
return back(reply, s, `${BASE}/workspaces/${id}`);
|
|
@@ -283,4 +424,132 @@ export async function workspaceRoutes(app: FastifyInstance) {
|
|
|
283
424
|
flash(s, r ? `Workspace ${r.name} deleted.` : 'Not found.', r ? 'ok' : 'error');
|
|
284
425
|
return back(reply, s, `${BASE}/workspaces`);
|
|
285
426
|
});
|
|
427
|
+
|
|
428
|
+
// (090) isolate a workspace, or make it shared again
|
|
429
|
+
app.post(`${BASE}/workspaces/:id(^\\d{1,9}$)/isolation`, async (req: Req, reply) => {
|
|
430
|
+
const s = await developer(req, reply);
|
|
431
|
+
if (!s || !(await adminOnly(s, reply))) return;
|
|
432
|
+
const id = Number(req.params.id);
|
|
433
|
+
const on = req.body?.isolated === 'true';
|
|
434
|
+
try {
|
|
435
|
+
const problem = on ? isolationProblem() : null;
|
|
436
|
+
if (problem) throw new Error(problem);
|
|
437
|
+
const r = await owner.query('update meta.workspace set isolated = $2 where id = $1', [id, on]);
|
|
438
|
+
if (!r.rowCount) return reply.code(404).send('Not found');
|
|
439
|
+
await logActivity({ appId: null, username: s.username, event: 'workspace', ip: clientIp(req), detail: `workspace ${id} ${on ? 'isolated' : 'shared'}` });
|
|
440
|
+
flash(s, on ? 'The workspace is isolated: its applications and SQL now run on its own database logins.' : 'The workspace is shared again: its applications run on the shared runtime connection.');
|
|
441
|
+
} catch (e) {
|
|
442
|
+
flash(s, `${on ? 'Not isolated' : 'Not changed'}: ${roleError(e)}`, 'error');
|
|
443
|
+
} finally {
|
|
444
|
+
await workspacesChanged();
|
|
445
|
+
}
|
|
446
|
+
return back(reply, s, `${BASE}/workspaces/${id}`);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// (090) the database roles of an isolated workspace
|
|
450
|
+
app.post(`${BASE}/workspaces/:id(^\\d{1,9}$)/roles`, async (req: Req, reply) => {
|
|
451
|
+
const s = await developer(req, reply);
|
|
452
|
+
if (!s || !(await adminOnly(s, reply))) return;
|
|
453
|
+
const id = Number(req.params.id);
|
|
454
|
+
const role = String(req.body?.role ?? '').trim();
|
|
455
|
+
const remove = req.body?.action === 'remove';
|
|
456
|
+
try {
|
|
457
|
+
if (!ROLE.test(role)) throw new Error('A role name is a letter or _ followed by letters, digits, _ or $ (at most 63).');
|
|
458
|
+
if (!remove && !(await owner.one('select 1 as ok from pg_roles where rolname = $1', [role]))) throw new Error(`There is no database role ${role}.`);
|
|
459
|
+
if (remove) {
|
|
460
|
+
const used = await owner.one<{ aliases: string }>(
|
|
461
|
+
`select string_agg(alias, ', ') as aliases from meta.app a where a.db_role = $2 and meta.app_workspace(a.id) = $1 having count(*) > 0`,
|
|
462
|
+
[id, role],
|
|
463
|
+
);
|
|
464
|
+
if (used) throw new Error(`${used.aliases} run${used.aliases.includes(',') ? '' : 's'} as ${role}: give ${used.aliases.includes(',') ? 'them' : 'it'} another role first.`);
|
|
465
|
+
await owner.query('delete from meta.workspace_role where workspace_id = $1 and role_name = $2', [id, role]);
|
|
466
|
+
} else await owner.query('insert into meta.workspace_role (workspace_id, role_name) values ($1, $2)', [id, role]);
|
|
467
|
+
await logActivity({ appId: null, username: s.username, event: 'workspace', ip: clientIp(req), detail: `workspace ${id}: ${remove ? 'removed' : 'added'} role ${role}` });
|
|
468
|
+
flash(s, remove ? `Role ${role} removed.` : `Role ${role} added.`);
|
|
469
|
+
} catch (e) {
|
|
470
|
+
flash(s, `Not saved: ${roleError(e)}`, 'error');
|
|
471
|
+
} finally {
|
|
472
|
+
await workspacesChanged();
|
|
473
|
+
}
|
|
474
|
+
return back(reply, s, `${BASE}/workspaces/${id}`);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
// (091) the workspace message and environment banner
|
|
478
|
+
app.post(`${BASE}/workspaces/:id(^\\d{1,9}$)/service`, async (req: Req, reply) => {
|
|
479
|
+
const s = await developer(req, reply);
|
|
480
|
+
if (!s || !(await workspaceAdminOnly(s, reply, req.params.id))) return;
|
|
481
|
+
const message = String(req.body?.message ?? '').replace(/\r\n/g, '\n').trim();
|
|
482
|
+
const label = String(req.body?.banner_label ?? '').trim();
|
|
483
|
+
const color = BANNER_COLORS.includes(String(req.body?.banner_color)) ? String(req.body?.banner_color) : 'blue';
|
|
484
|
+
try {
|
|
485
|
+
if (message.length > 2000) throw new Error('The message is at most 2000 characters.');
|
|
486
|
+
if (label && !/^\S(.{0,38}\S)?$/.test(label)) throw new Error('The banner label is at most 40 characters.');
|
|
487
|
+
const r = await owner.query('update meta.workspace set message = $2, banner_label = $3, banner_color = $4 where id = $1', [req.params.id, message || null, label || null, color]);
|
|
488
|
+
if (!r.rowCount) return reply.code(404).send('Not found');
|
|
489
|
+
await logActivity({ appId: null, username: s.username, event: 'workspace', ip: clientIp(req), detail: `workspace ${req.params.id}: message and banner` });
|
|
490
|
+
flash(s, 'Message and banner saved.');
|
|
491
|
+
} catch (e) {
|
|
492
|
+
flash(s, (e as Error).message, 'error');
|
|
493
|
+
}
|
|
494
|
+
return back(reply, s, `${BASE}/workspaces/${req.params.id}`);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
// (091) workspace utilization: its applications, their size and use, its schemas' storage
|
|
498
|
+
app.get(`${BASE}/workspaces/:id(^\\d{1,9}$)/utilization`, async (req: Req, reply) => {
|
|
499
|
+
const s = await developer(req, reply);
|
|
500
|
+
if (!s || !(await workspaceAdminOnly(s, reply, req.params.id))) return;
|
|
501
|
+
const w = await owner.one<{ id: number; name: string }>('select id, name from meta.workspace where id = $1', [req.params.id]);
|
|
502
|
+
if (!w) return reply.code(404).send('Not found');
|
|
503
|
+
const [apps, schemas, totals] = await Promise.all([
|
|
504
|
+
owner.query(
|
|
505
|
+
`select a.id, a.name, a.alias, a.db_role,
|
|
506
|
+
(select count(*) from meta.page p where p.app_id = a.id)::int as pages,
|
|
507
|
+
(select count(*) from meta.region r join meta.page p on p.id = r.page_id where p.app_id = a.id)::int as regions,
|
|
508
|
+
(select count(*) from meta.item i join meta.page p on p.id = i.page_id where p.app_id = a.id)::int as items,
|
|
509
|
+
(select count(*) from meta.activity_log l where l.app_id = a.id and l.event = 'page_view' and l.at > now() - interval '30 days')::int as views,
|
|
510
|
+
(select count(distinct l.username) from meta.activity_log l where l.app_id = a.id and l.event = 'page_view' and l.at > now() - interval '30 days')::int as users,
|
|
511
|
+
(select coalesce(sum(octet_length(f.content)), 0) from meta.static_file f where f.app_id = a.id)::bigint as files
|
|
512
|
+
from meta.app a where meta.app_workspace(a.id) = $1 order by lower(a.name)`,
|
|
513
|
+
[w.id],
|
|
514
|
+
),
|
|
515
|
+
owner.query(
|
|
516
|
+
`select n.nspname as schema, n.nspowner::regrole::text as owner, count(c.oid)::int as tables,
|
|
517
|
+
coalesce(sum(pg_total_relation_size(c.oid)), 0)::bigint as bytes
|
|
518
|
+
from pg_namespace n left join pg_class c on c.relnamespace = n.oid and c.relkind in ('r', 'p', 'm')
|
|
519
|
+
where n.nspname !~ '^pg_' and n.nspname not in ('information_schema', 'meta')
|
|
520
|
+
and exists (select 1 from (select db_role as r from meta.app where meta.app_workspace(id) = $1 and db_role is not null
|
|
521
|
+
union select role_name from meta.workspace_role where workspace_id = $1) x
|
|
522
|
+
join pg_roles rl on rl.rolname = x.r
|
|
523
|
+
where has_schema_privilege(rl.oid, n.oid, 'USAGE'))
|
|
524
|
+
group by 1, 2 order by 1`,
|
|
525
|
+
[w.id],
|
|
526
|
+
),
|
|
527
|
+
owner.one(
|
|
528
|
+
`select (select count(*) from meta.workspace_member where workspace_id = $1)::int as developers,
|
|
529
|
+
(select count(*) from meta.sql_script where workspace_id = $1)::int as scripts`,
|
|
530
|
+
[w.id],
|
|
531
|
+
),
|
|
532
|
+
]);
|
|
533
|
+
const size = (n: number) => (n < 1024 ? `${n} B` : n < 1048576 ? `${(n / 1024).toFixed(1)} KB` : n < 1073741824 ? `${(n / 1048576).toFixed(1)} MB` : `${(n / 1073741824).toFixed(2)} GB`);
|
|
534
|
+
const sum = (k: string) => apps.rows.reduce((t, r) => t + Number(r[k]), 0);
|
|
535
|
+
const main = html`<div class="title-row"><h1>Workspace utilization: ${w.name}</h1></div>
|
|
536
|
+
<div class="stat-grid u-mb1">
|
|
537
|
+
${[['Applications', apps.rows.length], ['Pages', sum('pages')], ['Developers', totals.developers], ['Page views (30 days)', sum('views')], ['SQL scripts', totals.scripts]]
|
|
538
|
+
.map(([l, v]) => html`<div class="stat"><b>${String(v)}</b><span>${l}</span></div>`)}
|
|
539
|
+
</div>
|
|
540
|
+
${region('Applications', html`<div class="table-wrap"><table class="report report-reflow">
|
|
541
|
+
<thead><tr><th>Application</th><th class="num">Pages</th><th class="num">Regions</th><th class="num">Items</th><th class="num">Page views (30 days)</th><th class="num">Users (30 days)</th><th class="num">Static files</th></tr></thead>
|
|
542
|
+
<tbody>${apps.rows.length ? apps.rows.map((a) => html`<tr><td data-label="Application"><a href="${BASE}/apps/${a.id}">${a.name}</a> <span class="muted">/a/${a.alias}</span></td>
|
|
543
|
+
<td class="num" data-label="Pages">${a.pages}</td><td class="num" data-label="Regions">${a.regions}</td><td class="num" data-label="Items">${a.items}</td>
|
|
544
|
+
<td class="num" data-label="Page views">${a.views}</td><td class="num" data-label="Users">${a.users}</td><td class="num" data-label="Static files">${size(Number(a.files))}</td></tr>`)
|
|
545
|
+
: html`<tr><td colspan="7" class="empty">No applications.</td></tr>`}</tbody></table></div>`)}
|
|
546
|
+
<div class="u-spacer"></div>
|
|
547
|
+
${region('Schemas', html`<p class="muted u-mt0">The schemas the workspace's database roles may use, with the storage of their tables, indexes and TOAST data.</p>
|
|
548
|
+
<div class="table-wrap"><table class="report report-reflow">
|
|
549
|
+
<thead><tr><th>Schema</th><th>Owner</th><th class="num">Tables</th><th class="num">Storage</th></tr></thead>
|
|
550
|
+
<tbody>${schemas.rows.length ? schemas.rows.map((r) => html`<tr><td data-label="Schema">${r.schema}</td><td data-label="Owner"><code>${r.owner}</code></td>
|
|
551
|
+
<td class="num" data-label="Tables">${r.tables}</td><td class="num" data-label="Storage">${size(Number(r.bytes))}</td></tr>`)
|
|
552
|
+
: html`<tr><td colspan="4" class="empty">No schemas: the workspace's applications have no database role of their own.</td></tr>`}</tbody></table></div>`)}`;
|
|
553
|
+
return send(reply, s, shell(s, `Utilization: ${w.name}`, [['App Builder', BASE], ['Workspace utilities', `${BASE}/utilities`], [w.name, `${BASE}/workspaces/${w.id}`], ['Utilization']], main));
|
|
554
|
+
});
|
|
286
555
|
}
|
package/src/cli/replace.ts
CHANGED
|
@@ -55,6 +55,8 @@ export const KEPT = ['app_access', 'api_client', 'session', 'sso_pending', 'save
|
|
|
55
55
|
'blueprint',
|
|
56
56
|
// (064) the workspace of the application
|
|
57
57
|
'workspace_app',
|
|
58
|
+
// (092) the application's group in the App Builder
|
|
59
|
+
'app_group_member',
|
|
58
60
|
// (074) push notifications: the app's VAPID keys, users' devices, queued notifications
|
|
59
61
|
'push_key', 'push_subscription', 'push_message',
|
|
60
62
|
// (082) the application's backups, (084) users' feedback
|
package/src/db.ts
CHANGED
|
@@ -13,7 +13,7 @@ const inTx = new AsyncLocalStorage<ReadonlySet<string>>();
|
|
|
13
13
|
/** Waiting for a free connection fails after this, instead of hanging the request forever. */
|
|
14
14
|
const ACQUIRE_TIMEOUT_MS = Number(process.env.DB_ACQUIRE_TIMEOUT_MS ?? 30_000);
|
|
15
15
|
|
|
16
|
-
function makePool(connectionString: string | undefined, name: string) {
|
|
16
|
+
export function makePool(connectionString: string | undefined, name: string, max = Number(process.env.DB_POOL_SIZE ?? 10)) {
|
|
17
17
|
const opts = {
|
|
18
18
|
connectionString,
|
|
19
19
|
application_name: name,
|
|
@@ -21,7 +21,7 @@ function makePool(connectionString: string | undefined, name: string) {
|
|
|
21
21
|
// a transaction left open (a bug, a hung client) is ended by the database and frees its connection
|
|
22
22
|
idle_in_transaction_session_timeout: Number(process.env.DB_IDLE_IN_TRANSACTION_MS ?? 300_000),
|
|
23
23
|
};
|
|
24
|
-
const pool = new pg.Pool({ ...opts, max
|
|
24
|
+
const pool = new pg.Pool({ ...opts, max });
|
|
25
25
|
// A query issued while this async chain already holds a connection of this pool
|
|
26
26
|
// (in tx()) must not wait for a second one from the same pool: with every
|
|
27
27
|
// connection held by requests doing that, none would ever be released. Those
|
|
@@ -72,11 +72,36 @@ if (!process.env.RUNTIME_DATABASE_URL)
|
|
|
72
72
|
|
|
73
73
|
/** Owner connection: builder, SQL Workshop, migrations. */
|
|
74
74
|
export const owner = makePool(ownerUrl, 'pgkiln-builder');
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
export type Db = ReturnType<typeof makePool>;
|
|
76
|
+
|
|
77
|
+
export const runtimeUrl = process.env.RUNTIME_DATABASE_URL ?? ownerUrl;
|
|
78
|
+
/** The shared least-privilege connection (pgkiln_runtime): applications outside isolated workspaces. */
|
|
79
|
+
export const sharedRuntime = makePool(runtimeUrl, 'pgkiln-runtime');
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The runtime connection of the current request or job: an isolated workspace's
|
|
83
|
+
* own login (src/workspace-db.ts sets it with runtimeScope.run) or the shared one.
|
|
84
|
+
*/
|
|
85
|
+
export const runtimeScope = new AsyncLocalStorage<Db>();
|
|
86
|
+
const currentRuntime = () => runtimeScope.getStore() ?? sharedRuntime;
|
|
87
|
+
|
|
88
|
+
/** Least-privilege connection that runs applications: the current scope's (see runtimeScope). */
|
|
89
|
+
export const runtime: Db = {
|
|
90
|
+
get pool() {
|
|
91
|
+
return currentRuntime().pool;
|
|
92
|
+
},
|
|
93
|
+
query: (sql, params) => currentRuntime().query(sql, params),
|
|
94
|
+
one: (sql, params) => currentRuntime().one(sql, params),
|
|
95
|
+
tx: (fn) => currentRuntime().tx(fn),
|
|
96
|
+
end: () => sharedRuntime.end(),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const closers: (() => Promise<void>)[] = [];
|
|
100
|
+
/** Pools made elsewhere (the workspaces' own) close with the others. */
|
|
101
|
+
export const onClosePools = (fn: () => Promise<void>) => closers.push(fn);
|
|
77
102
|
|
|
78
103
|
export async function closePools() {
|
|
79
|
-
await Promise.all([owner.end(),
|
|
104
|
+
await Promise.all([owner.end(), sharedRuntime.end(), ...closers.map((f) => f())]);
|
|
80
105
|
}
|
|
81
106
|
|
|
82
107
|
export interface AppContext {
|
|
@@ -123,8 +148,13 @@ export async function appTx<T>(ctx: AppContext, fn: (c: Client) => Promise<T>):
|
|
|
123
148
|
|
|
124
149
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
125
150
|
|
|
151
|
+
let appRuntime: (appId: number) => Promise<Db> = async () => currentRuntime();
|
|
152
|
+
/** How appTx finds an application's runtime connection (src/workspace-db.ts: an isolated workspace's own). */
|
|
153
|
+
export const setAppRuntime = (fn: (appId: number) => Promise<Db>) => (appRuntime = fn);
|
|
154
|
+
|
|
126
155
|
async function appTxInner<T>(ctx: AppContext, fn: (c: Client) => Promise<T>): Promise<T> {
|
|
127
|
-
|
|
156
|
+
const db = await appRuntime(ctx.appId);
|
|
157
|
+
return db.tx(async (c) => {
|
|
128
158
|
await c.query(
|
|
129
159
|
`select set_config('pgkiln.app_user', $1, true),
|
|
130
160
|
set_config('pgkiln.session_id', $2, true),
|
package/src/ddl.ts
CHANGED
|
@@ -69,11 +69,12 @@ const lit = (s: string) => `'${s.replace(/'/g, "''")}'`;
|
|
|
69
69
|
const end = (s: string) => (s.trimEnd().endsWith(';') ? s.trimEnd() : `${s.trimEnd()};`);
|
|
70
70
|
|
|
71
71
|
/** The schemas a developer can pick: not PostgreSQL's own, not pgkiln's metadata. */
|
|
72
|
-
export async function userSchemas(c: { query: (sql: string) => Promise<{ rows: { name: string }[] }> }) {
|
|
72
|
+
export async function userSchemas(c: { query: (sql: string) => Promise<{ rows: { name: string }[] }> }, opts: { usable?: boolean } = {}) {
|
|
73
73
|
const r = await c.query(
|
|
74
74
|
`select nspname as name from pg_namespace
|
|
75
75
|
where nspname !~ '^pg_' and nspname not in ('information_schema', 'meta')
|
|
76
76
|
and not exists (select 1 from pg_depend d where d.classid = 'pg_namespace'::regclass and d.objid = pg_namespace.oid and d.deptype = 'e')
|
|
77
|
+
${opts.usable ? `and has_schema_privilege(oid, 'USAGE')` : ''}
|
|
77
78
|
order by 1`,
|
|
78
79
|
);
|
|
79
80
|
return r.rows.map((x) => x.name);
|
package/src/debug.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// Item values are only written at level 9, and never those of password items.
|
|
10
10
|
import type { FastifyRequest } from 'fastify';
|
|
11
11
|
import { runtime } from './db.ts';
|
|
12
|
+
import { withAppDb } from './workspace-db.ts';
|
|
12
13
|
|
|
13
14
|
export const DEBUG_LEVELS: [number, string][] = [
|
|
14
15
|
[0, 'Off'],
|
|
@@ -92,10 +93,10 @@ export class DebugLog {
|
|
|
92
93
|
if (this.saved) return;
|
|
93
94
|
this.saved = true;
|
|
94
95
|
if (this.dropped) this.entries.push({ ms: round(performance.now() - this.started), level: 1, component: 'debug', text: `${this.dropped} more entries left out (at most ${MAX_ENTRIES} per request)` });
|
|
95
|
-
await runtime.query('select meta.debug_save($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb)', [
|
|
96
|
+
await withAppDb(this.appId, () => runtime.query('select meta.debug_save($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb)', [
|
|
96
97
|
this.appId, this.pageNo, this.username, this.sessionId, this.method, this.path, status, this.level,
|
|
97
98
|
this.startedAt.toISOString(), round(performance.now() - this.started), JSON.stringify(this.entries),
|
|
98
|
-
]);
|
|
99
|
+
]));
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
|
package/src/migrate.ts
CHANGED
|
@@ -103,6 +103,9 @@ export async function migrate(o: MigrateOptions): Promise<string[]> {
|
|
|
103
103
|
else await apply('examples/hr', 'pgkiln_seed');
|
|
104
104
|
}
|
|
105
105
|
if (o.example) await apply(`examples/${o.example}`, 'pgkiln_seed');
|
|
106
|
+
// (090) application code in isolated workspaces gets what later migrations granted pgkiln_runtime
|
|
107
|
+
if (applied.length && (await client.query(`select to_regprocedure('meta.sync_app_code_grants()') is not null as ok`)).rows[0].ok)
|
|
108
|
+
await client.query('select meta.sync_app_code_grants()');
|
|
106
109
|
} finally {
|
|
107
110
|
if (applied.length || failure) await recordRun(client, o.root, { startedAt, fresh, applied, failure }).catch(() => {});
|
|
108
111
|
await client.end();
|
package/src/process-jobs.ts
CHANGED
|
@@ -3,6 +3,7 @@ import pg from 'pg';
|
|
|
3
3
|
import type { FastifyRequest } from 'fastify';
|
|
4
4
|
import { PageCss } from './css.ts';
|
|
5
5
|
import { owner, runtime } from './db.ts';
|
|
6
|
+
import { withAppDb } from './workspace-db.ts';
|
|
6
7
|
import { loadApp, loadPage } from './metadata.ts';
|
|
7
8
|
import type { Session } from './session.ts';
|
|
8
9
|
import { publicError, type PageContext } from './runtime/context.ts';
|
|
@@ -61,8 +62,12 @@ async function finish(id: string, state: 'completed' | 'failed', message: string
|
|
|
61
62
|
);
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
/** Run one claimed job. */
|
|
65
|
-
|
|
65
|
+
/** Run one claimed job (on its application's runtime connection: an isolated workspace's own). */
|
|
66
|
+
function runJob(job: Job) {
|
|
67
|
+
return withAppDb(job.app_id, () => runJobIn(job));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function runJobIn(job: Job) {
|
|
66
71
|
const app = await loadApp(job.alias);
|
|
67
72
|
const page = app ? await loadPage(app.id, job.page_no) : undefined;
|
|
68
73
|
const chain = page?.processes.find((p) => p.id === job.process_id && p.type === 'chain');
|