@axiumine/qpanel-db-setup 0.0.0-stage → 0.9.0-rc.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 +164 -0
- package/LICENSE +661 -0
- package/README.md +289 -2
- package/dist/migrations/20261127000000-create-enums-and-set-updated-at.mjs +25 -0
- package/dist/migrations/20261127000100-revoke-app-access-to-migration-tables.mjs +39 -0
- package/dist/migrations/20261127000200-create-admin.mjs +24 -0
- package/dist/migrations/20261127000300-create-secret.mjs +37 -0
- package/dist/migrations/20261127000400-create-audit-event.mjs +35 -0
- package/dist/migrations/20261127000500-create-registrar.mjs +27 -0
- package/dist/migrations/20261127000600-create-client.mjs +47 -0
- package/dist/migrations/20261127000700-create-client-note.mjs +22 -0
- package/dist/migrations/20261127000800-create-domain.mjs +37 -0
- package/dist/migrations/20261127000900-create-subdomain.mjs +24 -0
- package/dist/migrations/20261127001000-create-service.mjs +25 -0
- package/dist/migrations/20261127001100-create-datacenter.mjs +22 -0
- package/dist/migrations/20261127001200-create-db-instance.mjs +30 -0
- package/dist/migrations/20261127001300-create-db-instance-node.mjs +30 -0
- package/dist/migrations/20261127001400-create-db-account.mjs +34 -0
- package/dist/migrations/20261127001500-create-app-database.mjs +23 -0
- package/dist/migrations/20261127001600-create-app-database-user.mjs +31 -0
- package/dist/migrations/20261127001700-create-subdomain-app-database.mjs +25 -0
- package/dist/migrations/20261127001800-billing-fields.mjs +52 -0
- package/dist/migrations/20261128000000-secret-value-changed-at.mjs +25 -0
- package/dist/migrations/20261130000000-db-instance-environments.mjs +56 -0
- package/dist/migrations/20261130120000-create-server.mjs +50 -0
- package/dist/migrations/20261130200000-create-server-ssh-access.mjs +147 -0
- package/dist/migrations/20261201000000-ssh-login-per-way.mjs +129 -0
- package/dist/src/arguments.mjs +124 -0
- package/dist/src/bin.mjs +26 -0
- package/dist/src/cli.mjs +139 -0
- package/dist/src/create.mjs +40 -0
- package/dist/src/database.mjs +41 -0
- package/dist/src/env.mjs +49 -0
- package/dist/src/infisical.mjs +37 -0
- package/dist/src/keygrip.mjs +48 -0
- package/dist/src/masterKey.mjs +80 -0
- package/dist/src/masterKeyFile.mjs +66 -0
- package/dist/src/migrator.mjs +64 -0
- package/dist/src/redis.mjs +23 -0
- package/dist/src/secrets.mjs +96 -0
- package/dist/src/target.mjs +22 -0
- package/package.json +83 -4
- package/redis/bootstrap-acl.sh +130 -0
- package/sql/bootstrap.sql +218 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
export async function up(db) {
|
|
3
|
+
await sql `
|
|
4
|
+
CREATE TABLE qpanel.datacenter (
|
|
5
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
6
|
+
name text NOT NULL,
|
|
7
|
+
provider text,
|
|
8
|
+
region text,
|
|
9
|
+
notes text,
|
|
10
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
11
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
12
|
+
CONSTRAINT datacenter_name_unique UNIQUE (name)
|
|
13
|
+
);
|
|
14
|
+
CREATE TRIGGER datacenter_set_updated_at BEFORE UPDATE ON qpanel.datacenter
|
|
15
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
16
|
+
`.execute(db);
|
|
17
|
+
}
|
|
18
|
+
export async function down(db) {
|
|
19
|
+
await sql `
|
|
20
|
+
DROP TABLE qpanel.datacenter;
|
|
21
|
+
`.execute(db);
|
|
22
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// `environment` NULL means the instance is shared by every environment. Deleting an instance that still
|
|
3
|
+
// has app databases is refused by app_database.id_db_instance (restrict).
|
|
4
|
+
export async function up(db) {
|
|
5
|
+
await sql `
|
|
6
|
+
CREATE TABLE qpanel.db_instance (
|
|
7
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
8
|
+
engine qpanel.db_engine NOT NULL,
|
|
9
|
+
name text NOT NULL,
|
|
10
|
+
version text,
|
|
11
|
+
topology qpanel.db_topology NOT NULL,
|
|
12
|
+
replica_set_name text,
|
|
13
|
+
environment qpanel.environment,
|
|
14
|
+
tls boolean NOT NULL DEFAULT false,
|
|
15
|
+
srv_host text,
|
|
16
|
+
params jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
17
|
+
notes text,
|
|
18
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
19
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
20
|
+
CONSTRAINT db_instance_name_unique UNIQUE (name)
|
|
21
|
+
);
|
|
22
|
+
CREATE TRIGGER db_instance_set_updated_at BEFORE UPDATE ON qpanel.db_instance
|
|
23
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
24
|
+
`.execute(db);
|
|
25
|
+
}
|
|
26
|
+
export async function down(db) {
|
|
27
|
+
await sql `
|
|
28
|
+
DROP TABLE qpanel.db_instance;
|
|
29
|
+
`.execute(db);
|
|
30
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// One instance may span several servers and datacenters. A datacenter that still hosts a node cannot be
|
|
3
|
+
// deleted (restrict). `node_role` is free text: the role catalogue per engine lives in qpanel-common.
|
|
4
|
+
export async function up(db) {
|
|
5
|
+
await sql `
|
|
6
|
+
CREATE TABLE qpanel.db_instance_node (
|
|
7
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
8
|
+
id_db_instance uuid NOT NULL,
|
|
9
|
+
id_datacenter uuid NOT NULL,
|
|
10
|
+
host text NOT NULL,
|
|
11
|
+
port integer NOT NULL,
|
|
12
|
+
node_role text,
|
|
13
|
+
position integer NOT NULL DEFAULT 0,
|
|
14
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
15
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
16
|
+
CONSTRAINT db_instance_node_port_range CHECK (port BETWEEN 1 AND 65535),
|
|
17
|
+
CONSTRAINT id_db_instance FOREIGN KEY (id_db_instance) REFERENCES qpanel.db_instance (id) ON DELETE CASCADE,
|
|
18
|
+
CONSTRAINT id_datacenter FOREIGN KEY (id_datacenter) REFERENCES qpanel.datacenter (id) ON DELETE RESTRICT
|
|
19
|
+
);
|
|
20
|
+
CREATE INDEX db_instance_node_id_db_instance_idx ON qpanel.db_instance_node (id_db_instance);
|
|
21
|
+
CREATE INDEX db_instance_node_id_datacenter_idx ON qpanel.db_instance_node (id_datacenter);
|
|
22
|
+
CREATE TRIGGER db_instance_node_set_updated_at BEFORE UPDATE ON qpanel.db_instance_node
|
|
23
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
24
|
+
`.execute(db);
|
|
25
|
+
}
|
|
26
|
+
export async function down(db) {
|
|
27
|
+
await sql `
|
|
28
|
+
DROP TABLE qpanel.db_instance_node;
|
|
29
|
+
`.execute(db);
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// A management account of an instance (root, postgres, userAdminAnyDatabase, ...). `id_secret` is nullable
|
|
3
|
+
// and unique, and restricts the delete of the secret; the AFTER DELETE trigger removes the secret of a
|
|
4
|
+
// deleted row, which is what makes the restrict safe. `host_pattern` matters for MySQL only, `auth_database`
|
|
5
|
+
// for MongoDB only.
|
|
6
|
+
export async function up(db) {
|
|
7
|
+
await sql `
|
|
8
|
+
CREATE TABLE qpanel.db_account (
|
|
9
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
10
|
+
id_db_instance uuid NOT NULL,
|
|
11
|
+
username text NOT NULL,
|
|
12
|
+
auth_database text,
|
|
13
|
+
host_pattern text DEFAULT '%',
|
|
14
|
+
roles text[] NOT NULL DEFAULT '{}',
|
|
15
|
+
id_secret uuid,
|
|
16
|
+
notes text,
|
|
17
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
18
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
19
|
+
CONSTRAINT db_account_id_secret_unique UNIQUE (id_secret),
|
|
20
|
+
CONSTRAINT id_db_instance FOREIGN KEY (id_db_instance) REFERENCES qpanel.db_instance (id) ON DELETE CASCADE,
|
|
21
|
+
CONSTRAINT id_secret FOREIGN KEY (id_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT
|
|
22
|
+
);
|
|
23
|
+
CREATE INDEX db_account_id_db_instance_idx ON qpanel.db_account (id_db_instance);
|
|
24
|
+
CREATE TRIGGER db_account_set_updated_at BEFORE UPDATE ON qpanel.db_account
|
|
25
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
26
|
+
CREATE TRIGGER db_account_delete_secret AFTER DELETE ON qpanel.db_account
|
|
27
|
+
FOR EACH ROW WHEN (OLD.id_secret IS NOT NULL) EXECUTE FUNCTION qpanel.delete_owned_secret();
|
|
28
|
+
`.execute(db);
|
|
29
|
+
}
|
|
30
|
+
export async function down(db) {
|
|
31
|
+
await sql `
|
|
32
|
+
DROP TABLE qpanel.db_account;
|
|
33
|
+
`.execute(db);
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// A database (or, for Redis, a db index or key prefix) that an application uses. The instance cannot be
|
|
3
|
+
// deleted while it has any (restrict). The unique constraint also serves that check.
|
|
4
|
+
export async function up(db) {
|
|
5
|
+
await sql `
|
|
6
|
+
CREATE TABLE qpanel.app_database (
|
|
7
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
8
|
+
id_db_instance uuid NOT NULL,
|
|
9
|
+
name text NOT NULL,
|
|
10
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
11
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
12
|
+
CONSTRAINT app_database_id_db_instance_name_unique UNIQUE (id_db_instance, name),
|
|
13
|
+
CONSTRAINT id_db_instance FOREIGN KEY (id_db_instance) REFERENCES qpanel.db_instance (id) ON DELETE RESTRICT
|
|
14
|
+
);
|
|
15
|
+
CREATE TRIGGER app_database_set_updated_at BEFORE UPDATE ON qpanel.app_database
|
|
16
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
17
|
+
`.execute(db);
|
|
18
|
+
}
|
|
19
|
+
export async function down(db) {
|
|
20
|
+
await sql `
|
|
21
|
+
DROP TABLE qpanel.app_database;
|
|
22
|
+
`.execute(db);
|
|
23
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// Same secret rules as db_account: nullable, unique, restricting, and removed by the AFTER DELETE trigger
|
|
3
|
+
// when the row goes. The unique constraint also serves the cascade delete.
|
|
4
|
+
export async function up(db) {
|
|
5
|
+
await sql `
|
|
6
|
+
CREATE TABLE qpanel.app_database_user (
|
|
7
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
8
|
+
id_app_database uuid NOT NULL,
|
|
9
|
+
username text NOT NULL,
|
|
10
|
+
host_pattern text DEFAULT '%',
|
|
11
|
+
roles text[] NOT NULL DEFAULT '{}',
|
|
12
|
+
id_secret uuid,
|
|
13
|
+
notes text,
|
|
14
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
15
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
16
|
+
CONSTRAINT app_database_user_id_app_database_username_unique UNIQUE (id_app_database, username),
|
|
17
|
+
CONSTRAINT app_database_user_id_secret_unique UNIQUE (id_secret),
|
|
18
|
+
CONSTRAINT id_app_database FOREIGN KEY (id_app_database) REFERENCES qpanel.app_database (id) ON DELETE CASCADE,
|
|
19
|
+
CONSTRAINT id_secret FOREIGN KEY (id_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT
|
|
20
|
+
);
|
|
21
|
+
CREATE TRIGGER app_database_user_set_updated_at BEFORE UPDATE ON qpanel.app_database_user
|
|
22
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
23
|
+
CREATE TRIGGER app_database_user_delete_secret AFTER DELETE ON qpanel.app_database_user
|
|
24
|
+
FOR EACH ROW WHEN (OLD.id_secret IS NOT NULL) EXECUTE FUNCTION qpanel.delete_owned_secret();
|
|
25
|
+
`.execute(db);
|
|
26
|
+
}
|
|
27
|
+
export async function down(db) {
|
|
28
|
+
await sql `
|
|
29
|
+
DROP TABLE qpanel.app_database_user;
|
|
30
|
+
`.execute(db);
|
|
31
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// Which subdomains use which database user: one user may serve several subdomains. No `id`: the primary key
|
|
3
|
+
// is the pair. The doc gives no delete rule for the two links, so both cascade: a link means nothing
|
|
4
|
+
// without either end.
|
|
5
|
+
export async function up(db) {
|
|
6
|
+
await sql `
|
|
7
|
+
CREATE TABLE qpanel.subdomain_app_database (
|
|
8
|
+
id_subdomain uuid NOT NULL,
|
|
9
|
+
id_app_database_user uuid NOT NULL,
|
|
10
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
11
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
12
|
+
CONSTRAINT subdomain_app_database_pkey PRIMARY KEY (id_subdomain, id_app_database_user),
|
|
13
|
+
CONSTRAINT id_subdomain FOREIGN KEY (id_subdomain) REFERENCES qpanel.subdomain (id) ON DELETE CASCADE,
|
|
14
|
+
CONSTRAINT id_app_database_user FOREIGN KEY (id_app_database_user) REFERENCES qpanel.app_database_user (id) ON DELETE CASCADE
|
|
15
|
+
);
|
|
16
|
+
CREATE INDEX subdomain_app_database_id_app_database_user_idx ON qpanel.subdomain_app_database (id_app_database_user);
|
|
17
|
+
CREATE TRIGGER subdomain_app_database_set_updated_at BEFORE UPDATE ON qpanel.subdomain_app_database
|
|
18
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
19
|
+
`.execute(db);
|
|
20
|
+
}
|
|
21
|
+
export async function down(db) {
|
|
22
|
+
await sql `
|
|
23
|
+
DROP TABLE qpanel.subdomain_app_database;
|
|
24
|
+
`.execute(db);
|
|
25
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// Billing (ADR-019). EUR is the only currency, so `domain.currency` goes. Every priced item carries a price, payment
|
|
3
|
+
// terms in days and an invoice frequency, and the price is the amount of EACH invoice: EUR 10 invoiced monthly is 10 a
|
|
4
|
+
// month, a domain is typically EUR 15 invoiced annually. A domain's price stays required (its column is renamed, the
|
|
5
|
+
// data kept); a service's price is optional, and its three billing columns are all null or all set, because a price
|
|
6
|
+
// without a frequency, or a frequency without a price, says nothing a report could use.
|
|
7
|
+
//
|
|
8
|
+
// The enum is created here and used here, so nothing needs `ALTER TYPE ... ADD VALUE`, which cannot run in the
|
|
9
|
+
// transaction the migrator wraps every pending migration in. Existing domain rows become annual, 30 days.
|
|
10
|
+
//
|
|
11
|
+
// `down` restores the previous shape. `currency` comes back as the last column of `domain` (PostgreSQL appends a
|
|
12
|
+
// column); nothing reads a column by position.
|
|
13
|
+
export async function up(db) {
|
|
14
|
+
await sql `
|
|
15
|
+
CREATE TYPE qpanel.invoice_frequency AS ENUM ('monthly', 'quarterly', 'semiannual', 'annual', 'one_off');
|
|
16
|
+
|
|
17
|
+
ALTER TABLE qpanel.domain DROP COLUMN currency;
|
|
18
|
+
ALTER TABLE qpanel.domain RENAME COLUMN annual_fee_cents TO price_cents;
|
|
19
|
+
ALTER TABLE qpanel.domain RENAME CONSTRAINT domain_annual_fee_cents_not_negative TO domain_price_cents_not_negative;
|
|
20
|
+
ALTER TABLE qpanel.domain
|
|
21
|
+
ADD COLUMN invoice_frequency qpanel.invoice_frequency NOT NULL DEFAULT 'annual',
|
|
22
|
+
ADD COLUMN payment_terms_days integer NOT NULL DEFAULT 30,
|
|
23
|
+
ADD CONSTRAINT domain_payment_terms_days_range CHECK (payment_terms_days BETWEEN 0 AND 365);
|
|
24
|
+
|
|
25
|
+
ALTER TABLE qpanel.service
|
|
26
|
+
ADD COLUMN price_cents integer,
|
|
27
|
+
ADD COLUMN invoice_frequency qpanel.invoice_frequency,
|
|
28
|
+
ADD COLUMN payment_terms_days integer,
|
|
29
|
+
ADD CONSTRAINT service_price_cents_not_negative CHECK (price_cents >= 0),
|
|
30
|
+
ADD CONSTRAINT service_payment_terms_days_range CHECK (payment_terms_days BETWEEN 0 AND 365),
|
|
31
|
+
ADD CONSTRAINT service_billing_all_or_none CHECK (num_nulls(price_cents, invoice_frequency, payment_terms_days) IN (0, 3));
|
|
32
|
+
`.execute(db);
|
|
33
|
+
}
|
|
34
|
+
export async function down(db) {
|
|
35
|
+
await sql `
|
|
36
|
+
ALTER TABLE qpanel.service
|
|
37
|
+
DROP COLUMN price_cents,
|
|
38
|
+
DROP COLUMN invoice_frequency,
|
|
39
|
+
DROP COLUMN payment_terms_days;
|
|
40
|
+
|
|
41
|
+
ALTER TABLE qpanel.domain
|
|
42
|
+
DROP COLUMN invoice_frequency,
|
|
43
|
+
DROP COLUMN payment_terms_days;
|
|
44
|
+
ALTER TABLE qpanel.domain RENAME CONSTRAINT domain_price_cents_not_negative TO domain_annual_fee_cents_not_negative;
|
|
45
|
+
ALTER TABLE qpanel.domain RENAME COLUMN price_cents TO annual_fee_cents;
|
|
46
|
+
ALTER TABLE qpanel.domain
|
|
47
|
+
ADD COLUMN currency char(3) NOT NULL DEFAULT 'EUR',
|
|
48
|
+
ADD CONSTRAINT domain_currency_format CHECK (currency ~ '^[A-Z]{3}$');
|
|
49
|
+
|
|
50
|
+
DROP TYPE qpanel.invoice_frequency;
|
|
51
|
+
`.execute(db);
|
|
52
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// `secret.value_changed_at`: when the VALUE of a secret last changed. `updated_at` cannot say it: the trigger
|
|
3
|
+
// `secret_set_updated_at` bumps it on every update, and `master-key rewrap` updates every row, so a re-wrap would
|
|
4
|
+
// reset the age of every secret and "secrets older than 12 months" would count none. Only a service that creates or
|
|
5
|
+
// replaces a password writes this column; the re-wrap never does (it writes `wrapped_dek`, `key_version`, `rotated_at`).
|
|
6
|
+
//
|
|
7
|
+
// The default is `now()`, so a service that inserts a secret names nothing. Existing rows are dated by `updated_at`,
|
|
8
|
+
// the latest moment their value can have changed (no re-wrap has run anywhere yet). The backfill is an UPDATE, and the
|
|
9
|
+
// trigger would turn `updated_at` into `now()` on every row, destroying the date it copies from: it is switched off for
|
|
10
|
+
// that one statement and on again right after, inside the migration's transaction. The app role keeps its table-wide
|
|
11
|
+
// grants, which cover a column added later.
|
|
12
|
+
export async function up(db) {
|
|
13
|
+
await sql `
|
|
14
|
+
ALTER TABLE qpanel.secret ADD COLUMN value_changed_at timestamptz NOT NULL DEFAULT now();
|
|
15
|
+
|
|
16
|
+
ALTER TABLE qpanel.secret DISABLE TRIGGER secret_set_updated_at;
|
|
17
|
+
UPDATE qpanel.secret SET value_changed_at = updated_at;
|
|
18
|
+
ALTER TABLE qpanel.secret ENABLE TRIGGER secret_set_updated_at;
|
|
19
|
+
`.execute(db);
|
|
20
|
+
}
|
|
21
|
+
export async function down(db) {
|
|
22
|
+
await sql `
|
|
23
|
+
ALTER TABLE qpanel.secret DROP COLUMN value_changed_at;
|
|
24
|
+
`.execute(db);
|
|
25
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// A database instance serves one or more environments (parent ADR-033): `db_instance.environment` (one value, NULL for
|
|
3
|
+
// "shared by every environment") becomes `environments`, a list with at least one of the four names. It is `text[]` and
|
|
4
|
+
// not an array of the enum `qpanel.environment`, because `pg` parses `text[]` into an array and hands an enum array back
|
|
5
|
+
// as one string.
|
|
6
|
+
//
|
|
7
|
+
// The column is converted in place (`ALTER COLUMN … TYPE … USING`, then renamed): that rewrites the rows without firing
|
|
8
|
+
// `db_instance_set_updated_at`, so `updated_at` still says when an instance last changed. A shared instance becomes all
|
|
9
|
+
// four, in the enum's order.
|
|
10
|
+
//
|
|
11
|
+
// A check constraint names one rule, and PostgreSQL tries them in the order of their names, which is why each row below
|
|
12
|
+
// breaks exactly one of them. `array_position` refuses a multidimensional array with an error of its own, so the NULL rule
|
|
13
|
+
// asks only about a list of one dimension and leaves the others to the dimension rule. A duplicate is a list longer than
|
|
14
|
+
// the number of different names in it.
|
|
15
|
+
//
|
|
16
|
+
// `down` goes back to one value: an instance that serves exactly one environment keeps it, one that serves several becomes
|
|
17
|
+
// shared (NULL), which is the only way the old column can say "more than one".
|
|
18
|
+
export async function up(db) {
|
|
19
|
+
await sql `
|
|
20
|
+
ALTER TABLE qpanel.db_instance
|
|
21
|
+
ALTER COLUMN environment TYPE text[] USING CASE
|
|
22
|
+
WHEN environment IS NULL THEN ARRAY['development', 'testing', 'staging', 'production']
|
|
23
|
+
ELSE ARRAY[environment::text]
|
|
24
|
+
END;
|
|
25
|
+
ALTER TABLE qpanel.db_instance RENAME COLUMN environment TO environments;
|
|
26
|
+
ALTER TABLE qpanel.db_instance
|
|
27
|
+
ALTER COLUMN environments SET NOT NULL,
|
|
28
|
+
ADD CONSTRAINT db_instance_environments_element_not_null
|
|
29
|
+
CHECK (CASE WHEN array_ndims(environments) = 1 THEN array_position(environments, NULL) IS NULL END),
|
|
30
|
+
ADD CONSTRAINT db_instance_environments_known
|
|
31
|
+
CHECK (environments <@ ARRAY['development', 'testing', 'staging', 'production']),
|
|
32
|
+
ADD CONSTRAINT db_instance_environments_no_duplicate CHECK (
|
|
33
|
+
cardinality(environments) =
|
|
34
|
+
('development' = ANY (environments))::int + ('testing' = ANY (environments))::int
|
|
35
|
+
+ ('staging' = ANY (environments))::int + ('production' = ANY (environments))::int
|
|
36
|
+
),
|
|
37
|
+
ADD CONSTRAINT db_instance_environments_not_empty CHECK (cardinality(environments) > 0),
|
|
38
|
+
ADD CONSTRAINT db_instance_environments_one_dimension CHECK (array_ndims(environments) = 1);
|
|
39
|
+
`.execute(db);
|
|
40
|
+
}
|
|
41
|
+
export async function down(db) {
|
|
42
|
+
await sql `
|
|
43
|
+
ALTER TABLE qpanel.db_instance
|
|
44
|
+
DROP CONSTRAINT db_instance_environments_element_not_null,
|
|
45
|
+
DROP CONSTRAINT db_instance_environments_known,
|
|
46
|
+
DROP CONSTRAINT db_instance_environments_no_duplicate,
|
|
47
|
+
DROP CONSTRAINT db_instance_environments_not_empty,
|
|
48
|
+
DROP CONSTRAINT db_instance_environments_one_dimension,
|
|
49
|
+
ALTER COLUMN environments DROP NOT NULL;
|
|
50
|
+
ALTER TABLE qpanel.db_instance
|
|
51
|
+
ALTER COLUMN environments TYPE qpanel.environment USING CASE
|
|
52
|
+
WHEN cardinality(environments) = 1 THEN environments[1]::qpanel.environment
|
|
53
|
+
END;
|
|
54
|
+
ALTER TABLE qpanel.db_instance RENAME COLUMN environments TO environment;
|
|
55
|
+
`.execute(db);
|
|
56
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// Servers are the machines of a datacenter that services run on. A server's name is unique within its datacenter,
|
|
3
|
+
// ignoring case (`citext`). A datacenter that still has a server, and a server a service still runs on, cannot be deleted
|
|
4
|
+
// (restrict).
|
|
5
|
+
//
|
|
6
|
+
// A service may name its server and the address it listens on there: `localhost`, an IPv4 or an IPv6 address, and only
|
|
7
|
+
// with a server. When server, host and port are all set they are unique together. The unique index compares the address
|
|
8
|
+
// as `inet`, so two spellings of one address are one, and reads `localhost` as `127.0.0.1`; a row that lacks one of the
|
|
9
|
+
// three is never a duplicate, because NULLs are distinct. `service_host_address` keeps out the text `inet` would refuse
|
|
10
|
+
// and the one thing it takes that is not an address, a netmask. The index also serves the foreign key.
|
|
11
|
+
//
|
|
12
|
+
// A subdomain's label needs no change: the domain itself, written `@`, is a label like another, and
|
|
13
|
+
// `subdomain_id_domain_label_unique` already allows one per domain.
|
|
14
|
+
export async function up(db) {
|
|
15
|
+
await sql `
|
|
16
|
+
CREATE TABLE qpanel.server (
|
|
17
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
18
|
+
id_datacenter uuid NOT NULL,
|
|
19
|
+
name qpanel.citext NOT NULL,
|
|
20
|
+
notes text,
|
|
21
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
22
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
23
|
+
CONSTRAINT server_id_datacenter_name_unique UNIQUE (id_datacenter, name),
|
|
24
|
+
CONSTRAINT id_datacenter FOREIGN KEY (id_datacenter) REFERENCES qpanel.datacenter (id) ON DELETE RESTRICT
|
|
25
|
+
);
|
|
26
|
+
CREATE TRIGGER server_set_updated_at BEFORE UPDATE ON qpanel.server
|
|
27
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
28
|
+
ALTER TABLE qpanel.service
|
|
29
|
+
ADD COLUMN id_server uuid,
|
|
30
|
+
ADD COLUMN host text,
|
|
31
|
+
ADD CONSTRAINT service_host_address
|
|
32
|
+
CHECK (host = 'localhost' OR (pg_input_is_valid(host, 'inet') AND strpos(host, '/') = 0)),
|
|
33
|
+
ADD CONSTRAINT service_host_needs_server CHECK (host IS NULL OR id_server IS NOT NULL),
|
|
34
|
+
ADD CONSTRAINT id_server FOREIGN KEY (id_server) REFERENCES qpanel.server (id) ON DELETE RESTRICT;
|
|
35
|
+
CREATE UNIQUE INDEX service_id_server_host_port_unique ON qpanel.service
|
|
36
|
+
(id_server, ((CASE host WHEN 'localhost' THEN '127.0.0.1' ELSE host END)::inet), port);
|
|
37
|
+
`.execute(db);
|
|
38
|
+
}
|
|
39
|
+
export async function down(db) {
|
|
40
|
+
await sql `
|
|
41
|
+
DROP INDEX qpanel.service_id_server_host_port_unique;
|
|
42
|
+
ALTER TABLE qpanel.service
|
|
43
|
+
DROP CONSTRAINT id_server,
|
|
44
|
+
DROP CONSTRAINT service_host_needs_server,
|
|
45
|
+
DROP CONSTRAINT service_host_address,
|
|
46
|
+
DROP COLUMN host,
|
|
47
|
+
DROP COLUMN id_server;
|
|
48
|
+
DROP TABLE qpanel.server;
|
|
49
|
+
`.execute(db);
|
|
50
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
// SSH access to a server (parent ADR-039), in three tables.
|
|
3
|
+
//
|
|
4
|
+
// `ssh_access` is where the server's SSH daemon answers, one row per server at most: host, port, the ports to knock before
|
|
5
|
+
// connecting (and after the session), the pause between knocks, how long the knocked port stays open, and the SSH user of
|
|
6
|
+
// another server it is reached through. That user cannot be deleted while a server jumps through it (restrict), nor can
|
|
7
|
+
// their server. The service refuses a jump through the server itself and a loop, which no constraint can see.
|
|
8
|
+
//
|
|
9
|
+
// `ssh_user` is an account of the server, any number of them, the name unique there case included. `logins` is the ways to
|
|
10
|
+
// log in, any one of them enough: each is its methods in order joined with commas, as sshd's `AuthenticationMethods` writes
|
|
11
|
+
// them. `ssh_key` is a key of a user, any number of them: a key file, a FIDO2 security key, or a certified key.
|
|
12
|
+
//
|
|
13
|
+
// Passwords, one-time seeds, private keys, passphrases and PINs are rows of `secret` (ADR-007). A deleted user or key takes
|
|
14
|
+
// its secrets with it, also when the delete cascades from the server; each table has a trigger of its own because each
|
|
15
|
+
// points at more than one secret.
|
|
16
|
+
//
|
|
17
|
+
// An array is checked as the text it joins into, since a check constraint takes no subquery: the number of parts that text
|
|
18
|
+
// splits into again is the number of elements, so no element hides a separator, and a NULL element becomes `?`.
|
|
19
|
+
export async function up(db) {
|
|
20
|
+
await sql `
|
|
21
|
+
CREATE TYPE qpanel.ssh_key_kind AS ENUM ('key', 'security_key', 'certificate');
|
|
22
|
+
|
|
23
|
+
CREATE TABLE qpanel.ssh_user (
|
|
24
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
25
|
+
id_server uuid NOT NULL,
|
|
26
|
+
username text NOT NULL,
|
|
27
|
+
logins text[] NOT NULL,
|
|
28
|
+
id_password_secret uuid,
|
|
29
|
+
id_otp_secret uuid,
|
|
30
|
+
notes text,
|
|
31
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
32
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
33
|
+
CONSTRAINT ssh_user_id_server_username_unique UNIQUE (id_server, username),
|
|
34
|
+
CONSTRAINT ssh_user_id_password_secret_unique UNIQUE (id_password_secret),
|
|
35
|
+
CONSTRAINT ssh_user_id_otp_secret_unique UNIQUE (id_otp_secret),
|
|
36
|
+
CONSTRAINT ssh_user_username CHECK (username ~ '^[A-Za-z0-9_][A-Za-z0-9_.@\\\\$-]{0,127}$'),
|
|
37
|
+
CONSTRAINT ssh_user_logins CHECK (
|
|
38
|
+
array_ndims(logins) = 1
|
|
39
|
+
AND array_to_string(logins, ';', '?') ~ ('^(publickey|password|keyboard-interactive)(,(publickey|password|keyboard-interactive)){0,3}'
|
|
40
|
+
|| '(;(publickey|password|keyboard-interactive)(,(publickey|password|keyboard-interactive)){0,3}){0,7}$')
|
|
41
|
+
AND cardinality(logins) = cardinality(string_to_array(array_to_string(logins, ';', '?'), ';'))
|
|
42
|
+
),
|
|
43
|
+
CONSTRAINT id_server FOREIGN KEY (id_server) REFERENCES qpanel.server (id) ON DELETE CASCADE,
|
|
44
|
+
CONSTRAINT id_password_secret FOREIGN KEY (id_password_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT,
|
|
45
|
+
CONSTRAINT id_otp_secret FOREIGN KEY (id_otp_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT
|
|
46
|
+
);
|
|
47
|
+
CREATE TRIGGER ssh_user_set_updated_at BEFORE UPDATE ON qpanel.ssh_user
|
|
48
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
49
|
+
CREATE FUNCTION qpanel.delete_ssh_user_secrets() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
50
|
+
BEGIN
|
|
51
|
+
DELETE FROM qpanel.secret WHERE id IN (OLD.id_password_secret, OLD.id_otp_secret);
|
|
52
|
+
RETURN NULL;
|
|
53
|
+
END
|
|
54
|
+
$$;
|
|
55
|
+
CREATE TRIGGER ssh_user_delete_secrets AFTER DELETE ON qpanel.ssh_user
|
|
56
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.delete_ssh_user_secrets();
|
|
57
|
+
|
|
58
|
+
CREATE TABLE qpanel.ssh_key (
|
|
59
|
+
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
60
|
+
id_ssh_user uuid NOT NULL,
|
|
61
|
+
kind qpanel.ssh_key_kind NOT NULL,
|
|
62
|
+
label text NOT NULL,
|
|
63
|
+
public_key text,
|
|
64
|
+
certificate text,
|
|
65
|
+
location text,
|
|
66
|
+
id_private_key_secret uuid,
|
|
67
|
+
id_passphrase_secret uuid,
|
|
68
|
+
id_pin_secret uuid,
|
|
69
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
70
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
71
|
+
CONSTRAINT ssh_key_id_ssh_user_label_unique UNIQUE (id_ssh_user, label),
|
|
72
|
+
CONSTRAINT ssh_key_id_private_key_secret_unique UNIQUE (id_private_key_secret),
|
|
73
|
+
CONSTRAINT ssh_key_id_passphrase_secret_unique UNIQUE (id_passphrase_secret),
|
|
74
|
+
CONSTRAINT ssh_key_id_pin_secret_unique UNIQUE (id_pin_secret),
|
|
75
|
+
CONSTRAINT ssh_key_certificate_needs_kind CHECK (certificate IS NULL OR kind = 'certificate'),
|
|
76
|
+
CONSTRAINT ssh_key_pin_needs_kind CHECK (id_pin_secret IS NULL OR kind = 'security_key'),
|
|
77
|
+
CONSTRAINT id_ssh_user FOREIGN KEY (id_ssh_user) REFERENCES qpanel.ssh_user (id) ON DELETE CASCADE,
|
|
78
|
+
CONSTRAINT id_private_key_secret FOREIGN KEY (id_private_key_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT,
|
|
79
|
+
CONSTRAINT id_passphrase_secret FOREIGN KEY (id_passphrase_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT,
|
|
80
|
+
CONSTRAINT id_pin_secret FOREIGN KEY (id_pin_secret) REFERENCES qpanel.secret (id) ON DELETE RESTRICT
|
|
81
|
+
);
|
|
82
|
+
CREATE TRIGGER ssh_key_set_updated_at BEFORE UPDATE ON qpanel.ssh_key
|
|
83
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
84
|
+
CREATE FUNCTION qpanel.delete_ssh_key_secrets() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
85
|
+
BEGIN
|
|
86
|
+
DELETE FROM qpanel.secret WHERE id IN (OLD.id_private_key_secret, OLD.id_passphrase_secret, OLD.id_pin_secret);
|
|
87
|
+
RETURN NULL;
|
|
88
|
+
END
|
|
89
|
+
$$;
|
|
90
|
+
CREATE TRIGGER ssh_key_delete_secrets AFTER DELETE ON qpanel.ssh_key
|
|
91
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.delete_ssh_key_secrets();
|
|
92
|
+
|
|
93
|
+
CREATE TABLE qpanel.ssh_access (
|
|
94
|
+
id_server uuid PRIMARY KEY,
|
|
95
|
+
host text NOT NULL,
|
|
96
|
+
port integer NOT NULL DEFAULT 22,
|
|
97
|
+
knock_sequence text[] NOT NULL DEFAULT '{}',
|
|
98
|
+
knock_close_sequence text[] NOT NULL DEFAULT '{}',
|
|
99
|
+
knock_delay_ms integer,
|
|
100
|
+
knock_open_seconds integer,
|
|
101
|
+
id_jump_user uuid,
|
|
102
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
103
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
104
|
+
CONSTRAINT ssh_access_host CHECK (host ~ '^[a-z0-9:.][a-z0-9:.-]{0,252}$'),
|
|
105
|
+
CONSTRAINT ssh_access_port_range CHECK (port BETWEEN 1 AND 65535),
|
|
106
|
+
CONSTRAINT ssh_access_knock_sequence CHECK (
|
|
107
|
+
cardinality(knock_sequence) = 0 OR (
|
|
108
|
+
array_ndims(knock_sequence) = 1
|
|
109
|
+
AND array_to_string(knock_sequence, ',', '?') ~ (
|
|
110
|
+
'^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])/(tcp|udp)'
|
|
111
|
+
|| '(,([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])/(tcp|udp)){0,15}$')
|
|
112
|
+
AND cardinality(knock_sequence) = cardinality(string_to_array(array_to_string(knock_sequence, ',', '?'), ','))
|
|
113
|
+
)
|
|
114
|
+
),
|
|
115
|
+
CONSTRAINT ssh_access_knock_close_sequence CHECK (
|
|
116
|
+
cardinality(knock_close_sequence) = 0 OR (
|
|
117
|
+
array_ndims(knock_close_sequence) = 1
|
|
118
|
+
AND array_to_string(knock_close_sequence, ',', '?') ~ (
|
|
119
|
+
'^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])/(tcp|udp)'
|
|
120
|
+
|| '(,([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])/(tcp|udp)){0,15}$')
|
|
121
|
+
AND cardinality(knock_close_sequence) = cardinality(string_to_array(array_to_string(knock_close_sequence, ',', '?'), ','))
|
|
122
|
+
)
|
|
123
|
+
),
|
|
124
|
+
CONSTRAINT ssh_access_knock_close_needs_sequence
|
|
125
|
+
CHECK (cardinality(knock_close_sequence) = 0 OR cardinality(knock_sequence) > 0),
|
|
126
|
+
CONSTRAINT ssh_access_knock_delay_range CHECK (knock_delay_ms BETWEEN 0 AND 60000),
|
|
127
|
+
CONSTRAINT ssh_access_knock_delay_needs_sequence CHECK (knock_delay_ms IS NULL OR cardinality(knock_sequence) > 0),
|
|
128
|
+
CONSTRAINT ssh_access_knock_open_range CHECK (knock_open_seconds BETWEEN 1 AND 86400),
|
|
129
|
+
CONSTRAINT ssh_access_knock_open_needs_sequence CHECK (knock_open_seconds IS NULL OR cardinality(knock_sequence) > 0),
|
|
130
|
+
CONSTRAINT id_server FOREIGN KEY (id_server) REFERENCES qpanel.server (id) ON DELETE CASCADE,
|
|
131
|
+
CONSTRAINT id_jump_user FOREIGN KEY (id_jump_user) REFERENCES qpanel.ssh_user (id) ON DELETE RESTRICT
|
|
132
|
+
);
|
|
133
|
+
CREATE INDEX ssh_access_id_jump_user_idx ON qpanel.ssh_access (id_jump_user);
|
|
134
|
+
CREATE TRIGGER ssh_access_set_updated_at BEFORE UPDATE ON qpanel.ssh_access
|
|
135
|
+
FOR EACH ROW EXECUTE FUNCTION qpanel.set_updated_at();
|
|
136
|
+
`.execute(db);
|
|
137
|
+
}
|
|
138
|
+
export async function down(db) {
|
|
139
|
+
await sql `
|
|
140
|
+
DROP TABLE qpanel.ssh_access;
|
|
141
|
+
DROP TABLE qpanel.ssh_key;
|
|
142
|
+
DROP TABLE qpanel.ssh_user;
|
|
143
|
+
DROP FUNCTION qpanel.delete_ssh_key_secrets();
|
|
144
|
+
DROP FUNCTION qpanel.delete_ssh_user_secrets();
|
|
145
|
+
DROP TYPE qpanel.ssh_key_kind;
|
|
146
|
+
`.execute(db);
|
|
147
|
+
}
|