@pgpm/encrypted-secrets-table 0.4.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/LICENSE +22 -0
- package/Makefile +6 -0
- package/README.md +5 -0
- package/__tests__/__snapshots__/secrets-table.test.ts.snap +38 -0
- package/__tests__/secrets-table.test.ts +273 -0
- package/deploy/schemas/secrets_schema/schema.sql +8 -0
- package/deploy/schemas/secrets_schema/tables/secrets_table/table.sql +16 -0
- package/deploy/schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets.sql +34 -0
- package/jest.config.js +15 -0
- package/launchql-encrypted-secrets-table.control +8 -0
- package/launchql.plan +7 -0
- package/package.json +28 -0
- package/revert/schemas/secrets_schema/schema.sql +7 -0
- package/revert/schemas/secrets_schema/tables/secrets_table/table.sql +7 -0
- package/revert/schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets.sql +9 -0
- package/sql/launchql-encrypted-secrets-table--0.1.0.sql +37 -0
- package/verify/schemas/secrets_schema/schema.sql +7 -0
- package/verify/schemas/secrets_schema/tables/secrets_table/table.sql +7 -0
- package/verify/schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets.sql +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Interweb, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`encrypted secrets table should have secrets_table with correct structure 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"columns": [
|
|
6
|
+
{
|
|
7
|
+
"column_default": "uuid_generate_v4()",
|
|
8
|
+
"column_name": "id",
|
|
9
|
+
"data_type": "uuid",
|
|
10
|
+
"is_nullable": "NO",
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"column_default": null,
|
|
14
|
+
"column_name": "secrets_owned_field",
|
|
15
|
+
"data_type": "uuid",
|
|
16
|
+
"is_nullable": "NO",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"column_default": null,
|
|
20
|
+
"column_name": "name",
|
|
21
|
+
"data_type": "text",
|
|
22
|
+
"is_nullable": "NO",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"column_default": null,
|
|
26
|
+
"column_name": "secrets_value_field",
|
|
27
|
+
"data_type": "bytea",
|
|
28
|
+
"is_nullable": "YES",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"column_default": null,
|
|
32
|
+
"column_name": "secrets_enc_field",
|
|
33
|
+
"data_type": "text",
|
|
34
|
+
"is_nullable": "YES",
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'pgsql-test';
|
|
2
|
+
import { snapshot } from 'graphile-test';
|
|
3
|
+
|
|
4
|
+
let pg: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
const user_id = 'dc474833-318a-41f5-9239-ee563ab657a6';
|
|
8
|
+
const user_id_2 = '550e8400-e29b-41d4-a716-446655440000';
|
|
9
|
+
|
|
10
|
+
describe('encrypted secrets table', () => {
|
|
11
|
+
beforeAll(async () => {
|
|
12
|
+
({ pg, teardown } = await getConnections());
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterAll(async () => {
|
|
16
|
+
await teardown();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
await pg.beforeEach();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await pg.afterEach();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should have secrets_schema created', async () => {
|
|
28
|
+
const schemas = await pg.any(
|
|
29
|
+
`SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'secrets_schema'`
|
|
30
|
+
);
|
|
31
|
+
expect(schemas).toHaveLength(1);
|
|
32
|
+
expect(schemas[0].schema_name).toBe('secrets_schema');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should have secrets_table with correct structure', async () => {
|
|
36
|
+
const columns = await pg.any(
|
|
37
|
+
`SELECT column_name, data_type, is_nullable, column_default
|
|
38
|
+
FROM information_schema.columns
|
|
39
|
+
WHERE table_schema = 'secrets_schema' AND table_name = 'secrets_table'
|
|
40
|
+
ORDER BY ordinal_position`
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect(snapshot({ columns })).toMatchSnapshot();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should have unique constraint on (secrets_owned_field, name)', async () => {
|
|
47
|
+
const constraints = await pg.any(
|
|
48
|
+
`SELECT constraint_name, constraint_type
|
|
49
|
+
FROM information_schema.table_constraints
|
|
50
|
+
WHERE table_schema = 'secrets_schema'
|
|
51
|
+
AND table_name = 'secrets_table'
|
|
52
|
+
AND constraint_type = 'UNIQUE'`
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(constraints).toHaveLength(1);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should insert record with default values', async () => {
|
|
59
|
+
const [result] = await pg.any(
|
|
60
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
61
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
62
|
+
VALUES ($1::uuid, 'test-secret', 'test-value'::bytea, 'none')
|
|
63
|
+
RETURNING *`,
|
|
64
|
+
[user_id]
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result.secrets_owned_field).toBe(user_id);
|
|
68
|
+
expect(result.name).toBe('test-secret');
|
|
69
|
+
expect(result.secrets_enc_field).toBe('none');
|
|
70
|
+
expect(result.id).toBeDefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should enforce unique constraint on (secrets_owned_field, name)', async () => {
|
|
74
|
+
// Insert first record
|
|
75
|
+
await pg.any(
|
|
76
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
77
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
78
|
+
VALUES ($1::uuid, 'duplicate-name', 'value1'::bytea, 'none')`,
|
|
79
|
+
[user_id]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// Try to insert duplicate - should fail
|
|
83
|
+
await expect(
|
|
84
|
+
pg.any(
|
|
85
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
86
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
87
|
+
VALUES ($1::uuid, 'duplicate-name', 'value2'::bytea, 'none')`,
|
|
88
|
+
[user_id]
|
|
89
|
+
)
|
|
90
|
+
).rejects.toThrow();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should allow same name for different users', async () => {
|
|
94
|
+
// Insert for first user
|
|
95
|
+
await pg.any(
|
|
96
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
97
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
98
|
+
VALUES ($1::uuid, 'same-name', 'value1'::bytea, 'none')`,
|
|
99
|
+
[user_id]
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Insert for second user - should succeed
|
|
103
|
+
const [result] = await pg.any(
|
|
104
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
105
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
106
|
+
VALUES ($1::uuid, 'same-name', 'value2'::bytea, 'none')
|
|
107
|
+
RETURNING *`,
|
|
108
|
+
[user_id_2]
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(result.secrets_owned_field).toBe(user_id_2);
|
|
112
|
+
expect(result.name).toBe('same-name');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should trigger hash_secrets on insert with crypt', async () => {
|
|
116
|
+
const plaintext = 'my-secret-password';
|
|
117
|
+
|
|
118
|
+
const [result] = await pg.any(
|
|
119
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
120
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
121
|
+
VALUES ($1::uuid, 'crypt-secret', $2::bytea, 'crypt')
|
|
122
|
+
RETURNING *`,
|
|
123
|
+
[user_id, plaintext]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// The trigger should have hashed the value
|
|
127
|
+
expect(result.secrets_enc_field).toBe('crypt');
|
|
128
|
+
expect(result.secrets_value_field).not.toEqual(Buffer.from(plaintext));
|
|
129
|
+
|
|
130
|
+
// Verify it's a bcrypt hash (starts with $2)
|
|
131
|
+
const hashedValue = result.secrets_value_field.toString();
|
|
132
|
+
expect(hashedValue).toMatch(/^\$2[aby]?\$/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should trigger hash_secrets on insert with pgp', async () => {
|
|
136
|
+
const plaintext = 'my-secret-data';
|
|
137
|
+
|
|
138
|
+
const [result] = await pg.any(
|
|
139
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
140
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
141
|
+
VALUES ($1::uuid, 'pgp-secret', $2::bytea, 'pgp')
|
|
142
|
+
RETURNING *`,
|
|
143
|
+
[user_id, plaintext]
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// The trigger should have encrypted the value
|
|
147
|
+
expect(result.secrets_enc_field).toBe('pgp');
|
|
148
|
+
expect(result.secrets_value_field).not.toEqual(Buffer.from(plaintext));
|
|
149
|
+
|
|
150
|
+
// Should be longer than original due to encryption
|
|
151
|
+
expect(result.secrets_value_field.length).toBeGreaterThan(plaintext.length);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should default to none encryption when not specified', async () => {
|
|
155
|
+
const plaintext = 'unencrypted-data';
|
|
156
|
+
|
|
157
|
+
const [result] = await pg.any(
|
|
158
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
159
|
+
(secrets_owned_field, name, secrets_value_field)
|
|
160
|
+
VALUES ($1::uuid, 'none-secret', $2::bytea)
|
|
161
|
+
RETURNING *`,
|
|
162
|
+
[user_id, plaintext]
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
// The trigger should set enc_field to 'none'
|
|
166
|
+
expect(result.secrets_enc_field).toBe('none');
|
|
167
|
+
expect(result.secrets_value_field).toEqual(Buffer.from(plaintext));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should trigger hash_secrets on update when value changes', async () => {
|
|
171
|
+
// Insert initial record
|
|
172
|
+
const [initial] = await pg.any(
|
|
173
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
174
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
175
|
+
VALUES ($1::uuid, 'update-test', 'initial-value'::bytea, 'none')
|
|
176
|
+
RETURNING *`,
|
|
177
|
+
[user_id]
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
// Update to use crypt encryption
|
|
181
|
+
const [updated] = await pg.any(
|
|
182
|
+
`UPDATE secrets_schema.secrets_table
|
|
183
|
+
SET secrets_value_field = 'new-password'::bytea, secrets_enc_field = 'crypt'
|
|
184
|
+
WHERE id = $1
|
|
185
|
+
RETURNING *`,
|
|
186
|
+
[initial.id]
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// Should be encrypted now
|
|
190
|
+
expect(updated.secrets_enc_field).toBe('crypt');
|
|
191
|
+
expect(updated.secrets_value_field).not.toEqual(Buffer.from('new-password'));
|
|
192
|
+
|
|
193
|
+
// Verify it's a bcrypt hash
|
|
194
|
+
const hashedValue = updated.secrets_value_field.toString();
|
|
195
|
+
expect(hashedValue).toMatch(/^\$2[aby]?\$/);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should not trigger hash_secrets on update when value unchanged', async () => {
|
|
199
|
+
// Insert initial record with crypt
|
|
200
|
+
const [initial] = await pg.any(
|
|
201
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
202
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
203
|
+
VALUES ($1::uuid, 'no-update-test', 'password'::bytea, 'crypt')
|
|
204
|
+
RETURNING *`,
|
|
205
|
+
[user_id]
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const originalHash = initial.secrets_value_field;
|
|
209
|
+
|
|
210
|
+
// Update name only (not the value field)
|
|
211
|
+
const [updated] = await pg.any(
|
|
212
|
+
`UPDATE secrets_schema.secrets_table
|
|
213
|
+
SET name = 'renamed-secret'
|
|
214
|
+
WHERE id = $1
|
|
215
|
+
RETURNING *`,
|
|
216
|
+
[initial.id]
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
// Hash should remain the same
|
|
220
|
+
expect(updated.secrets_value_field).toEqual(originalHash);
|
|
221
|
+
expect(updated.name).toBe('renamed-secret');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should verify crypt hash works correctly', async () => {
|
|
225
|
+
const password = 'test-password-123';
|
|
226
|
+
|
|
227
|
+
// Insert with crypt
|
|
228
|
+
const [result] = await pg.any(
|
|
229
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
230
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
231
|
+
VALUES ($1::uuid, 'crypt-verify', $2::bytea, 'crypt')
|
|
232
|
+
RETURNING *`,
|
|
233
|
+
[user_id, password]
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// The stored hash should be different from the original password
|
|
237
|
+
const storedHash = result.secrets_value_field.toString();
|
|
238
|
+
expect(storedHash).not.toBe(password);
|
|
239
|
+
|
|
240
|
+
// Verify it's a proper bcrypt hash format
|
|
241
|
+
expect(storedHash).toMatch(/^\$2[aby]?\$/);
|
|
242
|
+
|
|
243
|
+
// Verify the hash is the expected length (bcrypt hashes are typically 60 characters)
|
|
244
|
+
expect(storedHash.length).toBe(60);
|
|
245
|
+
|
|
246
|
+
// Verify that inserting the same password again produces a different hash (salt should be different)
|
|
247
|
+
const [result2] = await pg.any(
|
|
248
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
249
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
250
|
+
VALUES ($1::uuid, 'crypt-verify-2', $2::bytea, 'crypt')
|
|
251
|
+
RETURNING *`,
|
|
252
|
+
[user_id, password]
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
const storedHash2 = result2.secrets_value_field.toString();
|
|
256
|
+
expect(storedHash2).not.toBe(storedHash); // Different salt = different hash
|
|
257
|
+
expect(storedHash2).toMatch(/^\$2[aby]?\$/);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should handle null values correctly', async () => {
|
|
261
|
+
const [result] = await pg.any(
|
|
262
|
+
`INSERT INTO secrets_schema.secrets_table
|
|
263
|
+
(secrets_owned_field, name, secrets_value_field, secrets_enc_field)
|
|
264
|
+
VALUES ($1::uuid, 'null-test', NULL, NULL)
|
|
265
|
+
RETURNING *`,
|
|
266
|
+
[user_id]
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
// Trigger should set enc_field to 'none' even when value is null
|
|
270
|
+
expect(result.secrets_enc_field).toBe('none');
|
|
271
|
+
expect(result.secrets_value_field).toBeNull();
|
|
272
|
+
});
|
|
273
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
-- Deploy schemas/secrets_schema/tables/secrets_table/table to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/secrets_schema/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE TABLE secrets_schema.secrets_table (
|
|
8
|
+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
|
|
9
|
+
secrets_owned_field uuid NOT NULL,
|
|
10
|
+
name text NOT NULL,
|
|
11
|
+
secrets_value_field bytea NULL,
|
|
12
|
+
secrets_enc_field text NULL,
|
|
13
|
+
UNIQUE(secrets_owned_field, name)
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
COMMIT;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
-- Deploy schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/secrets_schema/schema
|
|
4
|
+
-- requires: schemas/secrets_schema/tables/secrets_table/table
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION secrets_schema.tg_hash_secrets()
|
|
9
|
+
RETURNS TRIGGER AS $$
|
|
10
|
+
BEGIN
|
|
11
|
+
IF (NEW.secrets_enc_field = 'crypt') THEN
|
|
12
|
+
NEW.secrets_value_field = crypt(NEW.secrets_value_field::text, gen_salt('bf'));
|
|
13
|
+
ELSIF (NEW.secrets_enc_field = 'pgp') THEN
|
|
14
|
+
NEW.secrets_value_field = pgp_sym_encrypt(encode(NEW.secrets_value_field::bytea, 'hex'), NEW.secrets_owned_field::text, 'compress-algo=1, cipher-algo=aes256');
|
|
15
|
+
ELSE
|
|
16
|
+
NEW.secrets_enc_field = 'none';
|
|
17
|
+
END IF;
|
|
18
|
+
RETURN NEW;
|
|
19
|
+
END;
|
|
20
|
+
$$
|
|
21
|
+
LANGUAGE 'plpgsql' VOLATILE;
|
|
22
|
+
|
|
23
|
+
CREATE TRIGGER hash_secrets_update
|
|
24
|
+
BEFORE UPDATE ON secrets_schema.secrets_table
|
|
25
|
+
FOR EACH ROW
|
|
26
|
+
WHEN (NEW.secrets_value_field IS DISTINCT FROM OLD.secrets_value_field)
|
|
27
|
+
EXECUTE PROCEDURE secrets_schema.tg_hash_secrets ();
|
|
28
|
+
|
|
29
|
+
CREATE TRIGGER hash_secrets_insert
|
|
30
|
+
BEFORE INSERT ON secrets_schema.secrets_table
|
|
31
|
+
FOR EACH ROW
|
|
32
|
+
EXECUTE PROCEDURE secrets_schema.tg_hash_secrets ();
|
|
33
|
+
|
|
34
|
+
COMMIT;
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
|
|
6
|
+
// Match both __tests__ and colocated test files
|
|
7
|
+
testMatch: ['**/?(*.)+(test|spec).{ts,tsx,js,jsx}'],
|
|
8
|
+
|
|
9
|
+
// Ignore build artifacts and type declarations
|
|
10
|
+
testPathIgnorePatterns: ['/dist/', '\\.d\\.ts$'],
|
|
11
|
+
modulePathIgnorePatterns: ['<rootDir>/dist/'],
|
|
12
|
+
watchPathIgnorePatterns: ['/dist/'],
|
|
13
|
+
|
|
14
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
15
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# launchql-encrypted-secrets-table extension
|
|
2
|
+
comment = 'launchql-encrypted-secrets-table extension'
|
|
3
|
+
default_version = '0.1.0'
|
|
4
|
+
module_pathname = '$libdir/launchql-encrypted-secrets-table'
|
|
5
|
+
requires = 'pgcrypto,plpgsql,uuid-ossp,launchql-verify'
|
|
6
|
+
relocatable = false
|
|
7
|
+
superuser = false
|
|
8
|
+
|
package/launchql.plan
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
%syntax-version=1.0.0
|
|
2
|
+
%project=launchql-encrypted-secrets-table
|
|
3
|
+
%uri=launchql-encrypted-secrets-table
|
|
4
|
+
|
|
5
|
+
schemas/secrets_schema/schema 2020-11-01T21:41:26Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/secrets_schema/schema
|
|
6
|
+
schemas/secrets_schema/tables/secrets_table/table [schemas/secrets_schema/schema] 2020-11-01T21:41:51Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/secrets_schema/tables/secrets_table/table
|
|
7
|
+
schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets [schemas/secrets_schema/schema schemas/secrets_schema/tables/secrets_table/table] 2020-11-01T21:46:08Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpm/encrypted-secrets-table",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Table-based encrypted secrets storage and retrieval",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"bundle": "lql package",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@pgpm/verify": "0.4.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@launchql/cli": "^4.9.0"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/launchql/extensions"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/launchql/extensions",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/launchql/extensions/issues"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "cc9f52a335caa6e21ee7751b04b77c84ce6cb809"
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- Revert schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets from pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
DROP TRIGGER hash_secrets_update ON secrets_schema.secrets_table;
|
|
6
|
+
DROP TRIGGER hash_secrets_insert ON secrets_schema.secrets_table;
|
|
7
|
+
DROP FUNCTION secrets_schema.tg_hash_secrets;
|
|
8
|
+
|
|
9
|
+
COMMIT;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
\echo Use "CREATE EXTENSION launchql-encrypted-secrets-table" to load this file. \quit
|
|
2
|
+
CREATE SCHEMA secrets_schema;
|
|
3
|
+
|
|
4
|
+
CREATE TABLE secrets_schema.secrets_table (
|
|
5
|
+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
6
|
+
secrets_owned_field uuid NOT NULL,
|
|
7
|
+
name text NOT NULL,
|
|
8
|
+
secrets_value_field bytea NULL,
|
|
9
|
+
secrets_enc_field text NULL,
|
|
10
|
+
UNIQUE (secrets_owned_field, name)
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
CREATE FUNCTION secrets_schema.tg_hash_secrets() RETURNS trigger AS $EOFCODE$
|
|
14
|
+
BEGIN
|
|
15
|
+
IF (NEW.secrets_enc_field = 'crypt') THEN
|
|
16
|
+
NEW.secrets_value_field = crypt(NEW.secrets_value_field::text, gen_salt('bf'));
|
|
17
|
+
ELSIF (NEW.secrets_enc_field = 'pgp') THEN
|
|
18
|
+
NEW.secrets_value_field = pgp_sym_encrypt(encode(NEW.secrets_value_field::bytea, 'hex'), NEW.secrets_owned_field::text, 'compress-algo=1, cipher-algo=aes256');
|
|
19
|
+
ELSE
|
|
20
|
+
NEW.secrets_enc_field = 'none';
|
|
21
|
+
END IF;
|
|
22
|
+
RETURN NEW;
|
|
23
|
+
END;
|
|
24
|
+
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
|
|
25
|
+
|
|
26
|
+
CREATE TRIGGER hash_secrets_update
|
|
27
|
+
BEFORE UPDATE
|
|
28
|
+
ON secrets_schema.secrets_table
|
|
29
|
+
FOR EACH ROW
|
|
30
|
+
WHEN (new.secrets_value_field IS DISTINCT FROM old.secrets_value_field)
|
|
31
|
+
EXECUTE PROCEDURE secrets_schema.tg_hash_secrets();
|
|
32
|
+
|
|
33
|
+
CREATE TRIGGER hash_secrets_insert
|
|
34
|
+
BEFORE INSERT
|
|
35
|
+
ON secrets_schema.secrets_table
|
|
36
|
+
FOR EACH ROW
|
|
37
|
+
EXECUTE PROCEDURE secrets_schema.tg_hash_secrets();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- Verify schemas/secrets_schema/tables/secrets_table/triggers/hash_secrets on pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
SELECT verify_function ('secrets_schema.tg_hash_secrets');
|
|
6
|
+
SELECT verify_trigger ('secrets_schema.hash_secrets_update');
|
|
7
|
+
SELECT verify_trigger ('secrets_schema.hash_secrets_insert');
|
|
8
|
+
|
|
9
|
+
ROLLBACK;
|