@pgpm/defaults 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Makefile CHANGED
@@ -1,5 +1,5 @@
1
1
  EXTENSION = launchql-defaults
2
- DATA = sql/launchql-defaults--0.4.6.sql
2
+ DATA = sql/launchql-defaults--0.4.0.sql
3
3
 
4
4
  PG_CONFIG = pg_config
5
5
  PGXS := $(shell $(PG_CONFIG) --pgxs)
package/README.md CHANGED
@@ -2,4 +2,337 @@
2
2
 
3
3
  Security defaults and baseline configurations.
4
4
 
5
- Provides default security settings, configurations, and baseline security policies for PostgreSQL applications.
5
+ ## Overview
6
+
7
+ `@pgpm/defaults` establishes a secure baseline configuration for PostgreSQL databases by revoking default public access. This package implements the principle of least privilege by removing PostgreSQL's default permissive settings and requiring explicit permission grants.
8
+
9
+ ## Features
10
+
11
+ - **Revoke Public Database Access**: Removes default PUBLIC access to databases
12
+ - **Restrict Function Execution**: Prevents PUBLIC from executing functions by default
13
+ - **Lock Down Public Schema**: Removes CREATE privilege on public schema from PUBLIC
14
+ - **Secure by Default**: Forces explicit permission grants
15
+ - **One-Time Setup**: Applies baseline security configuration
16
+
17
+ ## Installation
18
+
19
+ If you have `pgpm` installed:
20
+
21
+ ```bash
22
+ pgpm install @pgpm/defaults
23
+ pgpm deploy
24
+ ```
25
+
26
+ This is a quick way to get started. The sections below provide more detailed installation options.
27
+
28
+ ### Prerequisites
29
+
30
+ ```bash
31
+ # Install pgpm globally
32
+ npm install -g pgpm
33
+
34
+ # Start PostgreSQL
35
+ pgpm docker start
36
+
37
+ # Set environment variables
38
+ eval "$(pgpm env)"
39
+ ```
40
+
41
+ ### Deploy
42
+
43
+ #### Option 1: Deploy by installing with pgpm
44
+
45
+ ```bash
46
+ pgpm install @pgpm/defaults
47
+ pgpm deploy
48
+ ```
49
+
50
+ #### Option 2: Deploy from Package Directory
51
+
52
+ ```bash
53
+ cd packages/security/defaults
54
+ pgpm deploy --createdb
55
+ ```
56
+
57
+ #### Option 3: Deploy from Workspace Root
58
+
59
+ ```bash
60
+ # Install workspace dependencies
61
+ pgpm install
62
+
63
+ # Deploy with dependencies
64
+ pgpm deploy mydb1 --yes --createdb
65
+ ```
66
+
67
+ ## What It Does
68
+
69
+ This package executes three critical security operations:
70
+
71
+ ### 1. Revoke Database Access from PUBLIC
72
+
73
+ ```sql
74
+ REVOKE ALL ON DATABASE current_database FROM PUBLIC;
75
+ ```
76
+
77
+ Removes all default privileges that PUBLIC role has on the database, preventing unauthorized access.
78
+
79
+ ### 2. Revoke Function Execution from PUBLIC
80
+
81
+ ```sql
82
+ ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
83
+ ```
84
+
85
+ Prevents PUBLIC from executing any functions by default. Functions must be explicitly granted to roles.
86
+
87
+ ### 3. Revoke Schema Creation from PUBLIC
88
+
89
+ ```sql
90
+ REVOKE CREATE ON SCHEMA public FROM PUBLIC;
91
+ ```
92
+
93
+ Prevents PUBLIC from creating objects in the public schema, requiring explicit permissions.
94
+
95
+ ## Usage
96
+
97
+ ### Deploying Security Defaults
98
+
99
+ This is typically one of the first packages you deploy to establish a secure baseline.
100
+
101
+ #### From Package Directory
102
+
103
+ ```bash
104
+ cd packages/security/defaults
105
+ pgpm deploy --createdb
106
+ ```
107
+
108
+ #### From Workspace Root
109
+
110
+ ```bash
111
+ pgpm install
112
+ pgpm deploy mydb1 --yes --createdb
113
+ ```
114
+
115
+ ### After Deployment
116
+
117
+ After deploying this package, you must explicitly grant permissions:
118
+
119
+ ```sql
120
+ -- Grant database connection to specific roles
121
+ GRANT CONNECT ON DATABASE mydb TO authenticated;
122
+
123
+ -- Grant schema usage
124
+ GRANT USAGE ON SCHEMA public TO authenticated;
125
+
126
+ -- Grant table access
127
+ GRANT SELECT, INSERT, UPDATE, DELETE ON my_table TO authenticated;
128
+
129
+ -- Grant function execution
130
+ GRANT EXECUTE ON FUNCTION my_function() TO authenticated;
131
+ ```
132
+
133
+ ## Security Model
134
+
135
+ ### Before @pgpm/defaults
136
+
137
+ PostgreSQL's default configuration is permissive:
138
+ - PUBLIC can connect to databases
139
+ - PUBLIC can execute functions
140
+ - PUBLIC can create objects in public schema
141
+
142
+ This is convenient for development but insecure for production.
143
+
144
+ ### After @pgpm/defaults
145
+
146
+ All access must be explicitly granted:
147
+ - Roles need CONNECT privilege to access database
148
+ - Roles need USAGE privilege on schemas
149
+ - Roles need specific privileges on tables/functions
150
+ - No implicit permissions exist
151
+
152
+ ## Integration with Other Packages
153
+
154
+ ### With @pgpm/default-roles
155
+
156
+ ```bash
157
+ # Deploy both packages from their directories
158
+ cd packages/security/defaults && pgpm deploy --createdb
159
+ cd packages/security/default-roles && pgpm deploy --createdb
160
+ ```
161
+
162
+ Then grant permissions to roles:
163
+
164
+ ```sql
165
+ -- Grant permissions to roles
166
+ GRANT CONNECT ON DATABASE mydb TO anonymous, authenticated, administrator;
167
+ GRANT USAGE ON SCHEMA public TO anonymous, authenticated, administrator;
168
+ ```
169
+
170
+ ### With Application Tables
171
+
172
+ ```sql
173
+ -- Create table
174
+ CREATE TABLE users (id uuid PRIMARY KEY, email text);
175
+
176
+ -- Explicitly grant access (nothing is granted by default)
177
+ GRANT SELECT ON users TO anonymous;
178
+ GRANT SELECT, INSERT, UPDATE ON users TO authenticated;
179
+ GRANT ALL ON users TO administrator;
180
+ ```
181
+
182
+ ## Best Practices
183
+
184
+ 1. **Deploy Early**: Apply this package before creating application objects
185
+ 2. **Explicit Grants**: Always explicitly grant required permissions
186
+ 3. **Least Privilege**: Grant only the minimum permissions needed
187
+ 4. **Document Grants**: Keep track of what permissions each role has
188
+ 5. **Test Thoroughly**: Verify that your application works with restricted permissions
189
+
190
+ ## Common Patterns
191
+
192
+ ### Public Read, Authenticated Write
193
+
194
+ ```sql
195
+ -- Public data that anyone can read
196
+ GRANT SELECT ON public_data TO anonymous;
197
+ GRANT SELECT, INSERT, UPDATE, DELETE ON public_data TO authenticated;
198
+ ```
199
+
200
+ ### Private User Data
201
+
202
+ ```sql
203
+ -- Enable RLS for user isolation
204
+ ALTER TABLE user_data ENABLE ROW LEVEL SECURITY;
205
+
206
+ -- Only authenticated users can access their own data
207
+ GRANT SELECT, INSERT, UPDATE, DELETE ON user_data TO authenticated;
208
+
209
+ CREATE POLICY user_data_policy ON user_data
210
+ FOR ALL TO authenticated
211
+ USING (user_id = jwt_public.current_user_id());
212
+ ```
213
+
214
+ ### Admin-Only Tables
215
+
216
+ ```sql
217
+ -- Only administrators can access
218
+ GRANT ALL ON admin_config TO administrator;
219
+ ```
220
+
221
+ ## Troubleshooting
222
+
223
+ ### "Permission Denied" Errors
224
+
225
+ If you see permission denied errors after deploying this package:
226
+
227
+ 1. Check which role is being used: `SELECT current_role;`
228
+ 2. Verify role has CONNECT: `SELECT has_database_privilege('rolename', 'mydb', 'CONNECT');`
229
+ 3. Verify schema USAGE: `SELECT has_schema_privilege('rolename', 'public', 'USAGE');`
230
+ 4. Grant missing permissions explicitly
231
+
232
+ ### Functions Not Executable
233
+
234
+ If functions can't be executed:
235
+
236
+ ```sql
237
+ -- Grant execute on specific function
238
+ GRANT EXECUTE ON FUNCTION my_function() TO authenticated;
239
+
240
+ -- Or grant execute on all functions in schema
241
+ GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO authenticated;
242
+ ```
243
+
244
+ ## Dependencies
245
+
246
+ - `@pgpm/verify`: Verification utilities
247
+
248
+ ## Testing
249
+
250
+ ```bash
251
+ pnpm test
252
+ ```
253
+
254
+ ## Development
255
+
256
+ See the [Development](#development) section below for information on working with this package.
257
+
258
+ ---
259
+
260
+ ## Development
261
+
262
+ ### **Before You Begin**
263
+
264
+ ```bash
265
+ # 1. Install pgpm
266
+ npm install -g pgpm
267
+
268
+ # 2. Start Postgres (Docker or local)
269
+ pgpm docker start
270
+
271
+ # 3. Load PG* environment variables (PGHOST, PGUSER, ...)
272
+ eval "$(pgpm env)"
273
+ ```
274
+
275
+ ---
276
+
277
+ ### **Starting a New Project**
278
+
279
+ ```bash
280
+ # 1. Create a workspace
281
+ pgpm init --workspace
282
+ cd my-app
283
+
284
+ # 2. Create your first module
285
+ pgpm init
286
+
287
+ # 3. Add a migration
288
+ pgpm add some_change
289
+
290
+ # 4. Deploy (auto-creates database)
291
+ pgpm deploy --createdb
292
+ ```
293
+
294
+ ---
295
+
296
+ ### **Working With an Existing Project**
297
+
298
+ ```bash
299
+ # 1. Clone and enter the project
300
+ git clone <repo> && cd <project>
301
+
302
+ # 2. Install dependencies
303
+ pnpm install
304
+
305
+ # 3. Deploy locally
306
+ pgpm deploy --createdb
307
+ ```
308
+
309
+ ---
310
+
311
+ ### **Testing a Module Inside a Workspace**
312
+
313
+ ```bash
314
+ # 1. Install workspace deps
315
+ pnpm install
316
+
317
+ # 2. Enter the module directory
318
+ cd packages/<some-module>
319
+
320
+ # 3. Run tests in watch mode
321
+ pnpm test:watch
322
+ ```
323
+
324
+ ## Related Tooling
325
+
326
+ * [pgpm](https://github.com/launchql/launchql/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
327
+ * [pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
328
+ * [supabase-test](https://github.com/launchql/launchql/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
329
+ * [graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
330
+ * [pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
331
+ * [libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
332
+ * [pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
333
+
334
+ ## Disclaimer
335
+
336
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
337
+
338
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
@@ -1,6 +1,6 @@
1
1
  # launchql-defaults extension
2
2
  comment = 'launchql-defaults extension'
3
- default_version = '0.4.6'
3
+ default_version = '0.4.0'
4
4
  module_pathname = '$libdir/launchql-defaults'
5
5
  requires = 'plpgsql,launchql-verify'
6
6
  relocatable = false
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@pgpm/defaults",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Security defaults and baseline configurations",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "scripts": {
9
- "bundle": "lql package",
9
+ "bundle": "pgpm package",
10
10
  "test": "jest",
11
11
  "test:watch": "jest --watch"
12
12
  },
13
13
  "dependencies": {
14
- "@pgpm/verify": "0.4.0"
14
+ "@pgpm/verify": "0.5.0"
15
15
  },
16
16
  "devDependencies": {
17
- "@launchql/cli": "^4.9.0"
17
+ "pgpm": "^0.2.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -24,5 +24,5 @@
24
24
  "bugs": {
25
25
  "url": "https://github.com/launchql/extensions/issues"
26
26
  },
27
- "gitHead": "cc9f52a335caa6e21ee7751b04b77c84ce6cb809"
27
+ "gitHead": "d8eedbb24ad22a106634bc3b919bfb8d41976c16"
28
28
  }
File without changes