@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,218 @@
|
|
|
1
|
+
-- qPanel — PostgreSQL bootstrap of ONE environment: its database and its three roles. Run it once per
|
|
2
|
+
-- environment, as a superuser, from any machine that reaches the server:
|
|
3
|
+
--
|
|
4
|
+
-- psql "host=<postgres-host> port=5432 dbname=postgres user=postgres sslmode=disable" \
|
|
5
|
+
-- -v env=dev -f BEs/qpanel-db-setup/sql/bootstrap.sql
|
|
6
|
+
--
|
|
7
|
+
-- env is one of:
|
|
8
|
+
--
|
|
9
|
+
-- dev a developer's machine: the app while it is developed
|
|
10
|
+
-- test a developer's machine: the integration tests (CI creates its own throwaway one in Docker)
|
|
11
|
+
-- staging the deployment a release is checked on before production
|
|
12
|
+
-- prod production
|
|
13
|
+
--
|
|
14
|
+
-- For env=<env> it creates the following; no two environments share a role, a password or a database:
|
|
15
|
+
--
|
|
16
|
+
-- qpanel_<env>_migrator LOGIN; owns qpanel_<env> and every object in it. Used only by
|
|
17
|
+
-- qpanel-db-setup (migrations, key rotation): DATABASE_MIGRATOR_URL(_TEST).
|
|
18
|
+
-- qpanel_<env>_app LOGIN; data access only (SELECT/INSERT/UPDATE/DELETE, no DDL). Used by the
|
|
19
|
+
-- services: DATABASE_URL(_TEST). Because it owns nothing, a migration can make
|
|
20
|
+
-- `audit_event` append-only by revoking UPDATE/DELETE from it.
|
|
21
|
+
-- qpanel_<env>_backup LOGIN; reads every table and sequence of schema `qpanel`, today's and those a
|
|
22
|
+
-- later migration creates, and nothing else: no write, no DDL, no temporary table.
|
|
23
|
+
-- Used only by the nightly dump (the workspace's ops/backup): DATABASE_BACKUP_URL.
|
|
24
|
+
-- qpanel_<env> the database. Its schema is called `qpanel` in every environment, so the same
|
|
25
|
+
-- migrations run on all of them.
|
|
26
|
+
--
|
|
27
|
+
-- Idempotent: rerunning it creates only what is missing and never touches an existing password. An
|
|
28
|
+
-- environment made before a role or a privilege was added to this file gets it from a rerun.
|
|
29
|
+
--
|
|
30
|
+
-- Passwords: for a role that has no password yet, psql asks for one at a hidden prompt (\password),
|
|
31
|
+
-- hashes it with SCRAM on this machine and sends only the hash — the plaintext never reaches this file,
|
|
32
|
+
-- the shell history or the server log. Non-interactive use (CI) may pass -v migrator_password=…,
|
|
33
|
+
-- -v app_password=… and -v backup_password=… instead; those are sent as plaintext SQL, so keep that to
|
|
34
|
+
-- throwaway databases. backup_password is optional there: a run that passes either of the other two is
|
|
35
|
+
-- never asked for it, and without it a new backup role gets no password, so it cannot log in with one.
|
|
36
|
+
--
|
|
37
|
+
-- No TLS: the connection to the server is plain TCP (owner decision); SCRAM keeps passwords off
|
|
38
|
+
-- the wire, and stored secrets travel only as application-encrypted ciphertext.
|
|
39
|
+
--
|
|
40
|
+
-- It ends by printing what to check in pg_hba, this environment's env lines and the command that creates
|
|
41
|
+
-- its tables. Guide: docs/postgres.md.
|
|
42
|
+
|
|
43
|
+
\set ON_ERROR_STOP on
|
|
44
|
+
\set SHOW_CONTEXT never
|
|
45
|
+
SET password_encryption = 'scram-sha-256';
|
|
46
|
+
|
|
47
|
+
-- 0. Environment -----------------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
\if :{?env}
|
|
50
|
+
\else
|
|
51
|
+
DO $$ BEGIN RAISE EXCEPTION 'Missing environment: add -v env=dev, -v env=test, -v env=staging or -v env=prod'; END $$;
|
|
52
|
+
\endif
|
|
53
|
+
|
|
54
|
+
SELECT :'env' IN ('dev', 'test', 'staging', 'prod') AS env_ok \gset
|
|
55
|
+
\if :env_ok
|
|
56
|
+
\else
|
|
57
|
+
DO $$ BEGIN RAISE EXCEPTION 'Unknown environment: env must be dev, test, staging or prod'; END $$;
|
|
58
|
+
\endif
|
|
59
|
+
|
|
60
|
+
\set db qpanel_ :env
|
|
61
|
+
\set migrator :db _migrator
|
|
62
|
+
\set app :db _app
|
|
63
|
+
\set backup :db _backup
|
|
64
|
+
|
|
65
|
+
-- 1. Roles -----------------------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
SELECT NOT EXISTS (SELECT FROM pg_roles WHERE rolname = :'migrator') AS new_migrator,
|
|
68
|
+
NOT EXISTS (SELECT FROM pg_roles WHERE rolname = :'app') AS new_app,
|
|
69
|
+
NOT EXISTS (SELECT FROM pg_roles WHERE rolname = :'backup') AS new_backup
|
|
70
|
+
\gset
|
|
71
|
+
|
|
72
|
+
\if :new_migrator
|
|
73
|
+
CREATE ROLE :"migrator" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 5;
|
|
74
|
+
\endif
|
|
75
|
+
\if :new_app
|
|
76
|
+
CREATE ROLE :"app" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 40;
|
|
77
|
+
\endif
|
|
78
|
+
-- A dump takes one connection; the other two are for a person looking at what it sees.
|
|
79
|
+
\if :new_backup
|
|
80
|
+
CREATE ROLE :"backup" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 3;
|
|
81
|
+
\endif
|
|
82
|
+
|
|
83
|
+
ALTER ROLE :"migrator" SET search_path = qpanel;
|
|
84
|
+
ALTER ROLE :"migrator" SET timezone = 'UTC';
|
|
85
|
+
ALTER ROLE :"migrator" SET lock_timeout = '10s';
|
|
86
|
+
|
|
87
|
+
ALTER ROLE :"app" SET search_path = qpanel;
|
|
88
|
+
ALTER ROLE :"app" SET timezone = 'UTC';
|
|
89
|
+
ALTER ROLE :"app" SET statement_timeout = '15s';
|
|
90
|
+
ALTER ROLE :"app" SET idle_in_transaction_session_timeout = '60s';
|
|
91
|
+
|
|
92
|
+
-- Read-only by its privileges (section 3); the default below only makes a mistake fail earlier. No time
|
|
93
|
+
-- limit: a dump takes as long as it takes.
|
|
94
|
+
ALTER ROLE :"backup" SET search_path = qpanel;
|
|
95
|
+
ALTER ROLE :"backup" SET timezone = 'UTC';
|
|
96
|
+
ALTER ROLE :"backup" SET default_transaction_read_only = on;
|
|
97
|
+
|
|
98
|
+
-- Ask for every role still without a password, not only the ones this run created: a mistyped
|
|
99
|
+
-- confirmation stops the script after CREATE ROLE, and the rerun must ask again.
|
|
100
|
+
SELECT NOT EXISTS (SELECT FROM pg_authid WHERE rolname = :'migrator' AND rolpassword IS NOT NULL) AS ask_migrator,
|
|
101
|
+
NOT EXISTS (SELECT FROM pg_authid WHERE rolname = :'app' AND rolpassword IS NOT NULL) AS ask_app,
|
|
102
|
+
NOT EXISTS (SELECT FROM pg_authid WHERE rolname = :'backup' AND rolpassword IS NOT NULL) AS ask_backup
|
|
103
|
+
\gset
|
|
104
|
+
|
|
105
|
+
\if :{?migrator_password}
|
|
106
|
+
ALTER ROLE :"migrator" PASSWORD :'migrator_password';
|
|
107
|
+
\elif :ask_migrator
|
|
108
|
+
\echo 'Choose the password for' :migrator '(typed twice, hidden):'
|
|
109
|
+
\password :migrator
|
|
110
|
+
\endif
|
|
111
|
+
|
|
112
|
+
\if :{?app_password}
|
|
113
|
+
ALTER ROLE :"app" PASSWORD :'app_password';
|
|
114
|
+
\elif :ask_app
|
|
115
|
+
\echo 'Choose the password for' :app '(typed twice, hidden):'
|
|
116
|
+
\password :app
|
|
117
|
+
\endif
|
|
118
|
+
|
|
119
|
+
-- A run that passes a password as a variable has nobody at a terminal (a service's CI written before this
|
|
120
|
+
-- role existed passes the two above and no third): it is not asked for one, and the role is left without.
|
|
121
|
+
\if :{?backup_password}
|
|
122
|
+
ALTER ROLE :"backup" PASSWORD :'backup_password';
|
|
123
|
+
\elif :{?migrator_password}
|
|
124
|
+
\elif :{?app_password}
|
|
125
|
+
\elif :ask_backup
|
|
126
|
+
\echo 'Choose the password for' :backup '(typed twice, hidden):'
|
|
127
|
+
\password :backup
|
|
128
|
+
\endif
|
|
129
|
+
|
|
130
|
+
-- 2. Database --------------------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
SELECT NOT EXISTS (SELECT FROM pg_database WHERE datname = :'db') AS new_db,
|
|
133
|
+
EXISTS (SELECT FROM pg_database WHERE datname = :'db' AND pg_get_userbyid(datdba) <> :'migrator') AS foreign_owner
|
|
134
|
+
\gset
|
|
135
|
+
|
|
136
|
+
\if :foreign_owner
|
|
137
|
+
\warn 'The database' :db 'exists but is not owned by' :migrator '— drop it, or make' :migrator 'its owner, then rerun.'
|
|
138
|
+
DO $$ BEGIN RAISE EXCEPTION 'Database owned by another role'; END $$;
|
|
139
|
+
\endif
|
|
140
|
+
|
|
141
|
+
\if :new_db
|
|
142
|
+
CREATE DATABASE :"db" OWNER :"migrator" ENCODING 'UTF8' TEMPLATE template0;
|
|
143
|
+
\endif
|
|
144
|
+
|
|
145
|
+
REVOKE ALL ON DATABASE :"db" FROM PUBLIC;
|
|
146
|
+
GRANT CONNECT, TEMPORARY ON DATABASE :"db" TO :"app";
|
|
147
|
+
GRANT CONNECT ON DATABASE :"db" TO :"backup";
|
|
148
|
+
|
|
149
|
+
-- 3. Schema, extension and default privileges --------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
\connect :db
|
|
152
|
+
REVOKE ALL ON SCHEMA public FROM PUBLIC;
|
|
153
|
+
CREATE SCHEMA IF NOT EXISTS qpanel AUTHORIZATION :"migrator";
|
|
154
|
+
REVOKE ALL ON SCHEMA qpanel FROM PUBLIC;
|
|
155
|
+
GRANT USAGE ON SCHEMA qpanel TO :"app";
|
|
156
|
+
CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA qpanel;
|
|
157
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
|
|
158
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" IN SCHEMA qpanel
|
|
159
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO :"app";
|
|
160
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" IN SCHEMA qpanel
|
|
161
|
+
GRANT USAGE, SELECT ON SEQUENCES TO :"app";
|
|
162
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" IN SCHEMA qpanel
|
|
163
|
+
GRANT EXECUTE ON FUNCTIONS TO :"app";
|
|
164
|
+
|
|
165
|
+
-- The backup role: SELECT, which is what pg_dump needs to lock and to read a table or a sequence. Granted
|
|
166
|
+
-- on what exists (a database migrated before the role did) and by default on what the migrator creates
|
|
167
|
+
-- later. Not `pg_read_all_data`: that membership would read every database of the cluster.
|
|
168
|
+
GRANT USAGE ON SCHEMA qpanel TO :"backup";
|
|
169
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA qpanel TO :"backup";
|
|
170
|
+
GRANT SELECT ON ALL SEQUENCES IN SCHEMA qpanel TO :"backup";
|
|
171
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" IN SCHEMA qpanel
|
|
172
|
+
GRANT SELECT ON TABLES TO :"backup";
|
|
173
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE :"migrator" IN SCHEMA qpanel
|
|
174
|
+
GRANT SELECT ON SEQUENCES TO :"backup";
|
|
175
|
+
|
|
176
|
+
-- 4. Report ----------------------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
\connect postgres
|
|
179
|
+
SELECT r.rolname, r.rolcanlogin AS login, r.rolsuper AS superuser, r.rolconnlimit AS conn_limit,
|
|
180
|
+
r.rolpassword IS NOT NULL AS has_password
|
|
181
|
+
FROM pg_authid r WHERE r.rolname IN (:'migrator', :'app', :'backup') ORDER BY 1;
|
|
182
|
+
SELECT datname, pg_get_userbyid(datdba) AS owner FROM pg_database WHERE datname = :'db';
|
|
183
|
+
|
|
184
|
+
-- 5. What to do next: pg_hba and the env lines of this environment ---------------------------------
|
|
185
|
+
|
|
186
|
+
SELECT format('DATABASE_URL%s=postgresql://%s:<password>@<postgres-host>:5432/%s?sslmode=disable',
|
|
187
|
+
CASE :'env' WHEN 'test' THEN '_TEST' ELSE '' END, :'app', :'db') AS app_url,
|
|
188
|
+
format('DATABASE_MIGRATOR_URL%s=postgresql://%s:<password>@<postgres-host>:5432/%s?sslmode=disable',
|
|
189
|
+
CASE :'env' WHEN 'test' THEN '_TEST' ELSE '' END, :'migrator', :'db') AS migrator_url,
|
|
190
|
+
format('DATABASE_BACKUP_URL=postgresql://%s:<password>@<postgres-host>:5432/%s?sslmode=disable',
|
|
191
|
+
:'backup', :'db') AS backup_url,
|
|
192
|
+
format('yarn migrate:up%s', CASE :'env' WHEN 'test' THEN ' --test' ELSE '' END) AS migrate_command,
|
|
193
|
+
NOT EXISTS (SELECT FROM pg_authid WHERE rolname = :'backup' AND rolpassword IS NOT NULL) AS backup_unset
|
|
194
|
+
\gset
|
|
195
|
+
|
|
196
|
+
\echo
|
|
197
|
+
\echo 'Next steps for' :env '(docs/postgres.md):'
|
|
198
|
+
\echo
|
|
199
|
+
\echo '1. pg_hba: if the server already has a rule that admits these roles from your machines (for example'
|
|
200
|
+
\echo ' host all all <lan-subnet> md5), there is nothing to add. Otherwise add one: with Patroni through'
|
|
201
|
+
\echo ' patronictl edit-config, on a plain server in pg_hba.conf followed by pg_reload_conf() (docs/postgres.md, step 3).'
|
|
202
|
+
\echo
|
|
203
|
+
\echo '2. Put the passwords you just chose into these lines:'
|
|
204
|
+
\echo
|
|
205
|
+
\echo ' workspace root .env.shared:'
|
|
206
|
+
\echo ' ' :app_url
|
|
207
|
+
\echo ' BEs/qpanel-db-setup/.env (never in .env.shared):'
|
|
208
|
+
\echo ' ' :migrator_url
|
|
209
|
+
\echo ' the settings file of the machine that makes the dumps (workspace ops/backup/README.md), if there is one:'
|
|
210
|
+
\echo ' ' :backup_url
|
|
211
|
+
\echo
|
|
212
|
+
\echo '3. Create the tables, from BEs/qpanel-db-setup (docs/postgres.md, step 6):'
|
|
213
|
+
\echo ' ' :migrate_command
|
|
214
|
+
\if :backup_unset
|
|
215
|
+
\echo
|
|
216
|
+
\echo 'No password is set for' :backup '- it cannot log in with one. Rerun with no password variable to be'
|
|
217
|
+
\echo 'asked for it, or with -v backup_password=<password>.'
|
|
218
|
+
\endif
|