@geekmidas/cli 0.10.0 → 0.12.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/README.md +525 -0
- package/dist/bundler-DRXCw_YR.mjs +70 -0
- package/dist/bundler-DRXCw_YR.mjs.map +1 -0
- package/dist/bundler-WsEvH_b2.cjs +71 -0
- package/dist/bundler-WsEvH_b2.cjs.map +1 -0
- package/dist/{config-C9aXOHBe.cjs → config-AmInkU7k.cjs} +8 -8
- package/dist/config-AmInkU7k.cjs.map +1 -0
- package/dist/{config-BrkUalUh.mjs → config-DYULeEv8.mjs} +3 -3
- package/dist/config-DYULeEv8.mjs.map +1 -0
- package/dist/config.cjs +1 -1
- package/dist/config.d.cts +1 -1
- package/dist/config.d.mts +1 -1
- package/dist/config.mjs +1 -1
- package/dist/encryption-C8H-38Yy.mjs +42 -0
- package/dist/encryption-C8H-38Yy.mjs.map +1 -0
- package/dist/encryption-Dyf_r1h-.cjs +44 -0
- package/dist/encryption-Dyf_r1h-.cjs.map +1 -0
- package/dist/index.cjs +2116 -179
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +2134 -192
- package/dist/index.mjs.map +1 -1
- package/dist/{openapi-CZLI4QTr.mjs → openapi-BfFlOBCG.mjs} +801 -38
- package/dist/openapi-BfFlOBCG.mjs.map +1 -0
- package/dist/{openapi-BeHLKcwP.cjs → openapi-Bt_1FDpT.cjs} +794 -31
- package/dist/openapi-Bt_1FDpT.cjs.map +1 -0
- package/dist/{openapi-react-query-o5iMi8tz.cjs → openapi-react-query-B-sNWHFU.cjs} +5 -5
- package/dist/openapi-react-query-B-sNWHFU.cjs.map +1 -0
- package/dist/{openapi-react-query-CcciaVu5.mjs → openapi-react-query-B6XTeGqS.mjs} +5 -5
- package/dist/openapi-react-query-B6XTeGqS.mjs.map +1 -0
- package/dist/openapi-react-query.cjs +1 -1
- package/dist/openapi-react-query.d.cts.map +1 -1
- package/dist/openapi-react-query.d.mts.map +1 -1
- package/dist/openapi-react-query.mjs +1 -1
- package/dist/openapi.cjs +2 -2
- package/dist/openapi.d.cts +1 -1
- package/dist/openapi.d.cts.map +1 -1
- package/dist/openapi.d.mts +1 -1
- package/dist/openapi.d.mts.map +1 -1
- package/dist/openapi.mjs +2 -2
- package/dist/storage-BUYQJgz7.cjs +4 -0
- package/dist/storage-BXoJvmv2.cjs +149 -0
- package/dist/storage-BXoJvmv2.cjs.map +1 -0
- package/dist/storage-C9PU_30f.mjs +101 -0
- package/dist/storage-C9PU_30f.mjs.map +1 -0
- package/dist/storage-DLJAYxzJ.mjs +3 -0
- package/dist/{types-b-vwGpqc.d.cts → types-BR0M2v_c.d.mts} +100 -1
- package/dist/types-BR0M2v_c.d.mts.map +1 -0
- package/dist/{types-DXgiA1sF.d.mts → types-BhkZc-vm.d.cts} +100 -1
- package/dist/types-BhkZc-vm.d.cts.map +1 -0
- package/examples/cron-example.ts +27 -27
- package/examples/env.ts +27 -27
- package/examples/function-example.ts +31 -31
- package/examples/gkm.config.json +20 -20
- package/examples/gkm.config.ts +8 -8
- package/examples/gkm.minimal.config.json +5 -5
- package/examples/gkm.production.config.json +25 -25
- package/examples/logger.ts +2 -2
- package/package.json +6 -6
- package/src/__tests__/EndpointGenerator.hooks.spec.ts +191 -191
- package/src/__tests__/config.spec.ts +55 -55
- package/src/__tests__/loadEnvFiles.spec.ts +93 -93
- package/src/__tests__/normalizeHooksConfig.spec.ts +58 -58
- package/src/__tests__/openapi-react-query.spec.ts +497 -497
- package/src/__tests__/openapi.spec.ts +428 -428
- package/src/__tests__/test-helpers.ts +76 -76
- package/src/auth/__tests__/credentials.spec.ts +204 -0
- package/src/auth/__tests__/index.spec.ts +168 -0
- package/src/auth/credentials.ts +187 -0
- package/src/auth/index.ts +226 -0
- package/src/build/__tests__/index-new.spec.ts +474 -474
- package/src/build/__tests__/manifests.spec.ts +333 -333
- package/src/build/bundler.ts +141 -0
- package/src/build/endpoint-analyzer.ts +236 -0
- package/src/build/handler-templates.ts +1253 -0
- package/src/build/index.ts +250 -179
- package/src/build/manifests.ts +52 -52
- package/src/build/providerResolver.ts +145 -145
- package/src/build/types.ts +64 -43
- package/src/config.ts +39 -39
- package/src/deploy/__tests__/docker.spec.ts +111 -0
- package/src/deploy/__tests__/dokploy.spec.ts +245 -0
- package/src/deploy/__tests__/init.spec.ts +662 -0
- package/src/deploy/docker.ts +128 -0
- package/src/deploy/dokploy.ts +204 -0
- package/src/deploy/index.ts +136 -0
- package/src/deploy/init.ts +484 -0
- package/src/deploy/types.ts +48 -0
- package/src/dev/__tests__/index.spec.ts +266 -266
- package/src/dev/index.ts +647 -601
- package/src/docker/__tests__/compose.spec.ts +531 -0
- package/src/docker/__tests__/templates.spec.ts +280 -0
- package/src/docker/compose.ts +273 -0
- package/src/docker/index.ts +230 -0
- package/src/docker/templates.ts +446 -0
- package/src/generators/CronGenerator.ts +72 -72
- package/src/generators/EndpointGenerator.ts +699 -398
- package/src/generators/FunctionGenerator.ts +84 -84
- package/src/generators/Generator.ts +72 -72
- package/src/generators/OpenApiTsGenerator.ts +577 -577
- package/src/generators/SubscriberGenerator.ts +124 -124
- package/src/generators/__tests__/CronGenerator.spec.ts +433 -433
- package/src/generators/__tests__/EndpointGenerator.spec.ts +532 -382
- package/src/generators/__tests__/FunctionGenerator.spec.ts +244 -244
- package/src/generators/__tests__/SubscriberGenerator.spec.ts +397 -382
- package/src/generators/index.ts +4 -4
- package/src/index.ts +623 -201
- package/src/init/__tests__/generators.spec.ts +334 -334
- package/src/init/__tests__/init.spec.ts +332 -332
- package/src/init/__tests__/utils.spec.ts +89 -89
- package/src/init/generators/config.ts +175 -175
- package/src/init/generators/docker.ts +41 -41
- package/src/init/generators/env.ts +72 -72
- package/src/init/generators/index.ts +1 -1
- package/src/init/generators/models.ts +64 -64
- package/src/init/generators/monorepo.ts +161 -161
- package/src/init/generators/package.ts +71 -71
- package/src/init/generators/source.ts +6 -6
- package/src/init/index.ts +203 -208
- package/src/init/templates/api.ts +115 -115
- package/src/init/templates/index.ts +75 -75
- package/src/init/templates/minimal.ts +98 -98
- package/src/init/templates/serverless.ts +89 -89
- package/src/init/templates/worker.ts +98 -98
- package/src/init/utils.ts +54 -56
- package/src/openapi-react-query.ts +194 -194
- package/src/openapi.ts +63 -63
- package/src/secrets/__tests__/encryption.spec.ts +226 -0
- package/src/secrets/__tests__/generator.spec.ts +319 -0
- package/src/secrets/__tests__/index.spec.ts +91 -0
- package/src/secrets/__tests__/storage.spec.ts +403 -0
- package/src/secrets/encryption.ts +91 -0
- package/src/secrets/generator.ts +164 -0
- package/src/secrets/index.ts +383 -0
- package/src/secrets/storage.ts +134 -0
- package/src/secrets/types.ts +53 -0
- package/src/types.ts +295 -176
- package/tsdown.config.ts +11 -8
- package/dist/config-BrkUalUh.mjs.map +0 -1
- package/dist/config-C9aXOHBe.cjs.map +0 -1
- package/dist/openapi-BeHLKcwP.cjs.map +0 -1
- package/dist/openapi-CZLI4QTr.mjs.map +0 -1
- package/dist/openapi-react-query-CcciaVu5.mjs.map +0 -1
- package/dist/openapi-react-query-o5iMi8tz.cjs.map +0 -1
- package/dist/types-DXgiA1sF.d.mts.map +0 -1
- package/dist/types-b-vwGpqc.d.cts.map +0 -1
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { mkdir, rm } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
getSecretsDir,
|
|
8
|
+
getSecretsPath,
|
|
9
|
+
maskPassword,
|
|
10
|
+
readStageSecrets,
|
|
11
|
+
secretsExist,
|
|
12
|
+
setCustomSecret,
|
|
13
|
+
toEmbeddableSecrets,
|
|
14
|
+
writeStageSecrets,
|
|
15
|
+
} from '../storage';
|
|
16
|
+
import type { StageSecrets } from '../types';
|
|
17
|
+
|
|
18
|
+
describe('path utilities', () => {
|
|
19
|
+
describe('getSecretsDir', () => {
|
|
20
|
+
it('should return .gkm/secrets relative to cwd', () => {
|
|
21
|
+
const dir = getSecretsDir('/project');
|
|
22
|
+
expect(dir).toBe('/project/.gkm/secrets');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('getSecretsPath', () => {
|
|
27
|
+
it('should return path for stage file', () => {
|
|
28
|
+
const path = getSecretsPath('production', '/project');
|
|
29
|
+
expect(path).toBe('/project/.gkm/secrets/production.json');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should handle stage names with special characters', () => {
|
|
33
|
+
const path = getSecretsPath('dev-local', '/project');
|
|
34
|
+
expect(path).toBe('/project/.gkm/secrets/dev-local.json');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('secretsExist', () => {
|
|
39
|
+
it('should return false for non-existent secrets', () => {
|
|
40
|
+
const exists = secretsExist('nonexistent', '/nonexistent-path');
|
|
41
|
+
expect(exists).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('file operations', () => {
|
|
47
|
+
let tempDir: string;
|
|
48
|
+
|
|
49
|
+
beforeEach(async () => {
|
|
50
|
+
tempDir = join(tmpdir(), `gkm-test-${Date.now()}`);
|
|
51
|
+
await mkdir(tempDir, { recursive: true });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(async () => {
|
|
55
|
+
if (existsSync(tempDir)) {
|
|
56
|
+
await rm(tempDir, { recursive: true });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('writeStageSecrets / readStageSecrets', () => {
|
|
61
|
+
it('should write and read secrets', async () => {
|
|
62
|
+
const secrets: StageSecrets = {
|
|
63
|
+
stage: 'production',
|
|
64
|
+
createdAt: '2024-01-01T00:00:00.000Z',
|
|
65
|
+
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
66
|
+
services: {
|
|
67
|
+
postgres: {
|
|
68
|
+
host: 'postgres',
|
|
69
|
+
port: 5432,
|
|
70
|
+
username: 'app',
|
|
71
|
+
password: 'secret123',
|
|
72
|
+
database: 'app',
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
urls: {
|
|
76
|
+
DATABASE_URL: 'postgresql://app:secret123@postgres:5432/app',
|
|
77
|
+
},
|
|
78
|
+
custom: {},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
await writeStageSecrets(secrets, tempDir);
|
|
82
|
+
const read = await readStageSecrets('production', tempDir);
|
|
83
|
+
|
|
84
|
+
expect(read).toEqual(secrets);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should create directory if it does not exist', async () => {
|
|
88
|
+
const secrets: StageSecrets = {
|
|
89
|
+
stage: 'staging',
|
|
90
|
+
createdAt: new Date().toISOString(),
|
|
91
|
+
updatedAt: new Date().toISOString(),
|
|
92
|
+
services: {},
|
|
93
|
+
urls: {},
|
|
94
|
+
custom: {},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
await writeStageSecrets(secrets, tempDir);
|
|
98
|
+
|
|
99
|
+
expect(existsSync(join(tempDir, '.gkm/secrets'))).toBe(true);
|
|
100
|
+
expect(existsSync(join(tempDir, '.gkm/secrets/staging.json'))).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should return null for non-existent stage', async () => {
|
|
104
|
+
const read = await readStageSecrets('nonexistent', tempDir);
|
|
105
|
+
expect(read).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('secretsExist', () => {
|
|
110
|
+
it('should return true when secrets file exists', async () => {
|
|
111
|
+
const secrets: StageSecrets = {
|
|
112
|
+
stage: 'test',
|
|
113
|
+
createdAt: new Date().toISOString(),
|
|
114
|
+
updatedAt: new Date().toISOString(),
|
|
115
|
+
services: {},
|
|
116
|
+
urls: {},
|
|
117
|
+
custom: {},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
await writeStageSecrets(secrets, tempDir);
|
|
121
|
+
expect(secretsExist('test', tempDir)).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return false when secrets file does not exist', () => {
|
|
125
|
+
expect(secretsExist('nonexistent', tempDir)).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('setCustomSecret', () => {
|
|
130
|
+
it('should add custom secret to existing secrets', async () => {
|
|
131
|
+
const secrets: StageSecrets = {
|
|
132
|
+
stage: 'production',
|
|
133
|
+
createdAt: new Date().toISOString(),
|
|
134
|
+
updatedAt: new Date().toISOString(),
|
|
135
|
+
services: {},
|
|
136
|
+
urls: {},
|
|
137
|
+
custom: {},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
await writeStageSecrets(secrets, tempDir);
|
|
141
|
+
const updated = await setCustomSecret(
|
|
142
|
+
'production',
|
|
143
|
+
'API_KEY',
|
|
144
|
+
'sk_test_123',
|
|
145
|
+
tempDir,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(updated.custom.API_KEY).toBe('sk_test_123');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should update existing custom secret', async () => {
|
|
152
|
+
const secrets: StageSecrets = {
|
|
153
|
+
stage: 'production',
|
|
154
|
+
createdAt: new Date().toISOString(),
|
|
155
|
+
updatedAt: new Date().toISOString(),
|
|
156
|
+
services: {},
|
|
157
|
+
urls: {},
|
|
158
|
+
custom: { API_KEY: 'old-value' },
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
await writeStageSecrets(secrets, tempDir);
|
|
162
|
+
const updated = await setCustomSecret(
|
|
163
|
+
'production',
|
|
164
|
+
'API_KEY',
|
|
165
|
+
'new-value',
|
|
166
|
+
tempDir,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect(updated.custom.API_KEY).toBe('new-value');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should update updatedAt timestamp', async () => {
|
|
173
|
+
const originalTime = '2024-01-01T00:00:00.000Z';
|
|
174
|
+
const secrets: StageSecrets = {
|
|
175
|
+
stage: 'production',
|
|
176
|
+
createdAt: originalTime,
|
|
177
|
+
updatedAt: originalTime,
|
|
178
|
+
services: {},
|
|
179
|
+
urls: {},
|
|
180
|
+
custom: {},
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
await writeStageSecrets(secrets, tempDir);
|
|
184
|
+
const updated = await setCustomSecret(
|
|
185
|
+
'production',
|
|
186
|
+
'KEY',
|
|
187
|
+
'value',
|
|
188
|
+
tempDir,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(updated.updatedAt).not.toBe(originalTime);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should throw if secrets do not exist for stage', async () => {
|
|
195
|
+
await expect(
|
|
196
|
+
setCustomSecret('nonexistent', 'KEY', 'value', tempDir),
|
|
197
|
+
).rejects.toThrow('Secrets not found for stage "nonexistent"');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should persist changes to disk', async () => {
|
|
201
|
+
const secrets: StageSecrets = {
|
|
202
|
+
stage: 'production',
|
|
203
|
+
createdAt: new Date().toISOString(),
|
|
204
|
+
updatedAt: new Date().toISOString(),
|
|
205
|
+
services: {},
|
|
206
|
+
urls: {},
|
|
207
|
+
custom: {},
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
await writeStageSecrets(secrets, tempDir);
|
|
211
|
+
await setCustomSecret('production', 'NEW_KEY', 'new-value', tempDir);
|
|
212
|
+
|
|
213
|
+
const read = await readStageSecrets('production', tempDir);
|
|
214
|
+
expect(read!.custom.NEW_KEY).toBe('new-value');
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('toEmbeddableSecrets', () => {
|
|
220
|
+
it('should include URLs', () => {
|
|
221
|
+
const secrets: StageSecrets = {
|
|
222
|
+
stage: 'production',
|
|
223
|
+
createdAt: new Date().toISOString(),
|
|
224
|
+
updatedAt: new Date().toISOString(),
|
|
225
|
+
services: {},
|
|
226
|
+
urls: {
|
|
227
|
+
DATABASE_URL: 'postgresql://...',
|
|
228
|
+
REDIS_URL: 'redis://...',
|
|
229
|
+
},
|
|
230
|
+
custom: {},
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
234
|
+
|
|
235
|
+
expect(embeddable.DATABASE_URL).toBe('postgresql://...');
|
|
236
|
+
expect(embeddable.REDIS_URL).toBe('redis://...');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should include custom secrets', () => {
|
|
240
|
+
const secrets: StageSecrets = {
|
|
241
|
+
stage: 'production',
|
|
242
|
+
createdAt: new Date().toISOString(),
|
|
243
|
+
updatedAt: new Date().toISOString(),
|
|
244
|
+
services: {},
|
|
245
|
+
urls: {},
|
|
246
|
+
custom: {
|
|
247
|
+
API_KEY: 'sk_test_123',
|
|
248
|
+
WEBHOOK_SECRET: 'whsec_abc',
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
253
|
+
|
|
254
|
+
expect(embeddable.API_KEY).toBe('sk_test_123');
|
|
255
|
+
expect(embeddable.WEBHOOK_SECRET).toBe('whsec_abc');
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should include postgres service credentials', () => {
|
|
259
|
+
const secrets: StageSecrets = {
|
|
260
|
+
stage: 'production',
|
|
261
|
+
createdAt: new Date().toISOString(),
|
|
262
|
+
updatedAt: new Date().toISOString(),
|
|
263
|
+
services: {
|
|
264
|
+
postgres: {
|
|
265
|
+
host: 'postgres',
|
|
266
|
+
port: 5432,
|
|
267
|
+
username: 'app',
|
|
268
|
+
password: 'secret123',
|
|
269
|
+
database: 'mydb',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
urls: {},
|
|
273
|
+
custom: {},
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
277
|
+
|
|
278
|
+
expect(embeddable.POSTGRES_USER).toBe('app');
|
|
279
|
+
expect(embeddable.POSTGRES_PASSWORD).toBe('secret123');
|
|
280
|
+
expect(embeddable.POSTGRES_DB).toBe('mydb');
|
|
281
|
+
expect(embeddable.POSTGRES_HOST).toBe('postgres');
|
|
282
|
+
expect(embeddable.POSTGRES_PORT).toBe('5432');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should include redis service credentials', () => {
|
|
286
|
+
const secrets: StageSecrets = {
|
|
287
|
+
stage: 'production',
|
|
288
|
+
createdAt: new Date().toISOString(),
|
|
289
|
+
updatedAt: new Date().toISOString(),
|
|
290
|
+
services: {
|
|
291
|
+
redis: {
|
|
292
|
+
host: 'redis',
|
|
293
|
+
port: 6379,
|
|
294
|
+
username: 'default',
|
|
295
|
+
password: 'redis-pass',
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
urls: {},
|
|
299
|
+
custom: {},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
303
|
+
|
|
304
|
+
expect(embeddable.REDIS_PASSWORD).toBe('redis-pass');
|
|
305
|
+
expect(embeddable.REDIS_HOST).toBe('redis');
|
|
306
|
+
expect(embeddable.REDIS_PORT).toBe('6379');
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('should include rabbitmq service credentials', () => {
|
|
310
|
+
const secrets: StageSecrets = {
|
|
311
|
+
stage: 'production',
|
|
312
|
+
createdAt: new Date().toISOString(),
|
|
313
|
+
updatedAt: new Date().toISOString(),
|
|
314
|
+
services: {
|
|
315
|
+
rabbitmq: {
|
|
316
|
+
host: 'rabbitmq',
|
|
317
|
+
port: 5672,
|
|
318
|
+
username: 'app',
|
|
319
|
+
password: 'rmq-pass',
|
|
320
|
+
vhost: '/myapp',
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
urls: {},
|
|
324
|
+
custom: {},
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
328
|
+
|
|
329
|
+
expect(embeddable.RABBITMQ_USER).toBe('app');
|
|
330
|
+
expect(embeddable.RABBITMQ_PASSWORD).toBe('rmq-pass');
|
|
331
|
+
expect(embeddable.RABBITMQ_HOST).toBe('rabbitmq');
|
|
332
|
+
expect(embeddable.RABBITMQ_PORT).toBe('5672');
|
|
333
|
+
expect(embeddable.RABBITMQ_VHOST).toBe('/myapp');
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('should handle all services and custom secrets together', () => {
|
|
337
|
+
const secrets: StageSecrets = {
|
|
338
|
+
stage: 'production',
|
|
339
|
+
createdAt: new Date().toISOString(),
|
|
340
|
+
updatedAt: new Date().toISOString(),
|
|
341
|
+
services: {
|
|
342
|
+
postgres: {
|
|
343
|
+
host: 'postgres',
|
|
344
|
+
port: 5432,
|
|
345
|
+
username: 'app',
|
|
346
|
+
password: 'pg-pass',
|
|
347
|
+
database: 'app',
|
|
348
|
+
},
|
|
349
|
+
redis: {
|
|
350
|
+
host: 'redis',
|
|
351
|
+
port: 6379,
|
|
352
|
+
username: 'default',
|
|
353
|
+
password: 'redis-pass',
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
urls: {
|
|
357
|
+
DATABASE_URL: 'postgresql://...',
|
|
358
|
+
REDIS_URL: 'redis://...',
|
|
359
|
+
},
|
|
360
|
+
custom: {
|
|
361
|
+
API_KEY: 'key123',
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
366
|
+
|
|
367
|
+
// URLs
|
|
368
|
+
expect(embeddable.DATABASE_URL).toBe('postgresql://...');
|
|
369
|
+
expect(embeddable.REDIS_URL).toBe('redis://...');
|
|
370
|
+
|
|
371
|
+
// Custom
|
|
372
|
+
expect(embeddable.API_KEY).toBe('key123');
|
|
373
|
+
|
|
374
|
+
// Postgres
|
|
375
|
+
expect(embeddable.POSTGRES_PASSWORD).toBe('pg-pass');
|
|
376
|
+
|
|
377
|
+
// Redis
|
|
378
|
+
expect(embeddable.REDIS_PASSWORD).toBe('redis-pass');
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
describe('maskPassword', () => {
|
|
383
|
+
it('should mask middle characters', () => {
|
|
384
|
+
const masked = maskPassword('abcdefghijklmnop');
|
|
385
|
+
expect(masked).toBe('abcd**********op');
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it('should show first 4 and last 2 characters', () => {
|
|
389
|
+
const masked = maskPassword('1234567890');
|
|
390
|
+
expect(masked.slice(0, 4)).toBe('1234');
|
|
391
|
+
expect(masked.slice(-2)).toBe('90');
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('should return all asterisks for short passwords', () => {
|
|
395
|
+
expect(maskPassword('short')).toBe('********');
|
|
396
|
+
expect(maskPassword('12345678')).toBe('********');
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('should handle exactly 9 character password', () => {
|
|
400
|
+
const masked = maskPassword('123456789');
|
|
401
|
+
expect(masked).toBe('1234***89');
|
|
402
|
+
});
|
|
403
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
|
|
2
|
+
import type { EmbeddableSecrets, EncryptedPayload } from './types';
|
|
3
|
+
|
|
4
|
+
/** AES-256-GCM configuration */
|
|
5
|
+
const ALGORITHM = 'aes-256-gcm';
|
|
6
|
+
const KEY_LENGTH = 32; // 256 bits
|
|
7
|
+
const IV_LENGTH = 12; // 96 bits for GCM
|
|
8
|
+
const AUTH_TAG_LENGTH = 16; // 128 bits
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Encrypt secrets for embedding in a bundle.
|
|
12
|
+
* Uses AES-256-GCM with a randomly generated ephemeral key.
|
|
13
|
+
*
|
|
14
|
+
* @param secrets - Key-value pairs to encrypt
|
|
15
|
+
* @returns Encrypted payload with ephemeral master key
|
|
16
|
+
*/
|
|
17
|
+
export function encryptSecrets(secrets: EmbeddableSecrets): EncryptedPayload {
|
|
18
|
+
// Generate ephemeral key and IV
|
|
19
|
+
const masterKey = randomBytes(KEY_LENGTH);
|
|
20
|
+
const iv = randomBytes(IV_LENGTH);
|
|
21
|
+
|
|
22
|
+
// Serialize secrets to JSON
|
|
23
|
+
const plaintext = JSON.stringify(secrets);
|
|
24
|
+
|
|
25
|
+
// Encrypt
|
|
26
|
+
const cipher = createCipheriv(ALGORITHM, masterKey, iv);
|
|
27
|
+
const ciphertext = Buffer.concat([
|
|
28
|
+
cipher.update(plaintext, 'utf-8'),
|
|
29
|
+
cipher.final(),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
// Get auth tag
|
|
33
|
+
const authTag = cipher.getAuthTag();
|
|
34
|
+
|
|
35
|
+
// Combine ciphertext + auth tag
|
|
36
|
+
const combined = Buffer.concat([ciphertext, authTag]);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
encrypted: combined.toString('base64'),
|
|
40
|
+
iv: iv.toString('hex'),
|
|
41
|
+
masterKey: masterKey.toString('hex'),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decrypt secrets from an encrypted payload.
|
|
47
|
+
* Used at runtime to decrypt embedded credentials.
|
|
48
|
+
*
|
|
49
|
+
* @param encrypted - Base64 encoded ciphertext + auth tag
|
|
50
|
+
* @param iv - Hex encoded IV
|
|
51
|
+
* @param masterKey - Hex encoded master key
|
|
52
|
+
* @returns Decrypted secrets
|
|
53
|
+
*/
|
|
54
|
+
export function decryptSecrets(
|
|
55
|
+
encrypted: string,
|
|
56
|
+
iv: string,
|
|
57
|
+
masterKey: string,
|
|
58
|
+
): EmbeddableSecrets {
|
|
59
|
+
// Decode inputs
|
|
60
|
+
const key = Buffer.from(masterKey, 'hex');
|
|
61
|
+
const ivBuffer = Buffer.from(iv, 'hex');
|
|
62
|
+
const combined = Buffer.from(encrypted, 'base64');
|
|
63
|
+
|
|
64
|
+
// Split ciphertext and auth tag
|
|
65
|
+
const ciphertext = combined.subarray(0, -AUTH_TAG_LENGTH);
|
|
66
|
+
const authTag = combined.subarray(-AUTH_TAG_LENGTH);
|
|
67
|
+
|
|
68
|
+
// Decrypt
|
|
69
|
+
const decipher = createDecipheriv(ALGORITHM, key, ivBuffer);
|
|
70
|
+
decipher.setAuthTag(authTag);
|
|
71
|
+
|
|
72
|
+
const plaintext = Buffer.concat([
|
|
73
|
+
decipher.update(ciphertext),
|
|
74
|
+
decipher.final(),
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
return JSON.parse(plaintext.toString('utf-8')) as EmbeddableSecrets;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Generate the define options for tsdown/esbuild.
|
|
82
|
+
* These will be injected at build time.
|
|
83
|
+
*/
|
|
84
|
+
export function generateDefineOptions(
|
|
85
|
+
payload: EncryptedPayload,
|
|
86
|
+
): Record<string, string> {
|
|
87
|
+
return {
|
|
88
|
+
__GKM_ENCRYPTED_CREDENTIALS__: JSON.stringify(payload.encrypted),
|
|
89
|
+
__GKM_CREDENTIALS_IV__: JSON.stringify(payload.iv),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import type { ComposeServiceName } from '../types';
|
|
3
|
+
import type { ServiceCredentials, StageSecrets } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate a secure random password using URL-safe base64 characters.
|
|
7
|
+
* @param length Password length (default: 32)
|
|
8
|
+
*/
|
|
9
|
+
export function generateSecurePassword(length = 32): string {
|
|
10
|
+
return randomBytes(Math.ceil((length * 3) / 4))
|
|
11
|
+
.toString('base64url')
|
|
12
|
+
.slice(0, length);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Default service configurations */
|
|
16
|
+
const SERVICE_DEFAULTS: Record<
|
|
17
|
+
ComposeServiceName,
|
|
18
|
+
Omit<ServiceCredentials, 'password'>
|
|
19
|
+
> = {
|
|
20
|
+
postgres: {
|
|
21
|
+
host: 'postgres',
|
|
22
|
+
port: 5432,
|
|
23
|
+
username: 'app',
|
|
24
|
+
database: 'app',
|
|
25
|
+
},
|
|
26
|
+
redis: {
|
|
27
|
+
host: 'redis',
|
|
28
|
+
port: 6379,
|
|
29
|
+
username: 'default',
|
|
30
|
+
},
|
|
31
|
+
rabbitmq: {
|
|
32
|
+
host: 'rabbitmq',
|
|
33
|
+
port: 5672,
|
|
34
|
+
username: 'app',
|
|
35
|
+
vhost: '/',
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Generate credentials for a specific service.
|
|
41
|
+
*/
|
|
42
|
+
export function generateServiceCredentials(
|
|
43
|
+
service: ComposeServiceName,
|
|
44
|
+
): ServiceCredentials {
|
|
45
|
+
const defaults = SERVICE_DEFAULTS[service];
|
|
46
|
+
return {
|
|
47
|
+
...defaults,
|
|
48
|
+
password: generateSecurePassword(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generate credentials for multiple services.
|
|
54
|
+
*/
|
|
55
|
+
export function generateServicesCredentials(
|
|
56
|
+
services: ComposeServiceName[],
|
|
57
|
+
): StageSecrets['services'] {
|
|
58
|
+
const result: StageSecrets['services'] = {};
|
|
59
|
+
|
|
60
|
+
for (const service of services) {
|
|
61
|
+
result[service] = generateServiceCredentials(service);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Generate connection URL for PostgreSQL.
|
|
69
|
+
*/
|
|
70
|
+
export function generatePostgresUrl(creds: ServiceCredentials): string {
|
|
71
|
+
const { username, password, host, port, database } = creds;
|
|
72
|
+
return `postgresql://${username}:${encodeURIComponent(password)}@${host}:${port}/${database}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Generate connection URL for Redis.
|
|
77
|
+
*/
|
|
78
|
+
export function generateRedisUrl(creds: ServiceCredentials): string {
|
|
79
|
+
const { password, host, port } = creds;
|
|
80
|
+
return `redis://:${encodeURIComponent(password)}@${host}:${port}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Generate connection URL for RabbitMQ.
|
|
85
|
+
*/
|
|
86
|
+
export function generateRabbitmqUrl(creds: ServiceCredentials): string {
|
|
87
|
+
const { username, password, host, port, vhost } = creds;
|
|
88
|
+
const encodedVhost = encodeURIComponent(vhost ?? '/');
|
|
89
|
+
return `amqp://${username}:${encodeURIComponent(password)}@${host}:${port}/${encodedVhost}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Generate connection URLs from service credentials.
|
|
94
|
+
*/
|
|
95
|
+
export function generateConnectionUrls(
|
|
96
|
+
services: StageSecrets['services'],
|
|
97
|
+
): StageSecrets['urls'] {
|
|
98
|
+
const urls: StageSecrets['urls'] = {};
|
|
99
|
+
|
|
100
|
+
if (services.postgres) {
|
|
101
|
+
urls.DATABASE_URL = generatePostgresUrl(services.postgres);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (services.redis) {
|
|
105
|
+
urls.REDIS_URL = generateRedisUrl(services.redis);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (services.rabbitmq) {
|
|
109
|
+
urls.RABBITMQ_URL = generateRabbitmqUrl(services.rabbitmq);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return urls;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create a new StageSecrets object with generated credentials.
|
|
117
|
+
*/
|
|
118
|
+
export function createStageSecrets(
|
|
119
|
+
stage: string,
|
|
120
|
+
services: ComposeServiceName[],
|
|
121
|
+
): StageSecrets {
|
|
122
|
+
const now = new Date().toISOString();
|
|
123
|
+
const serviceCredentials = generateServicesCredentials(services);
|
|
124
|
+
const urls = generateConnectionUrls(serviceCredentials);
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
stage,
|
|
128
|
+
createdAt: now,
|
|
129
|
+
updatedAt: now,
|
|
130
|
+
services: serviceCredentials,
|
|
131
|
+
urls,
|
|
132
|
+
custom: {},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Rotate password for a specific service.
|
|
138
|
+
*/
|
|
139
|
+
export function rotateServicePassword(
|
|
140
|
+
secrets: StageSecrets,
|
|
141
|
+
service: ComposeServiceName,
|
|
142
|
+
): StageSecrets {
|
|
143
|
+
const currentCreds = secrets.services[service];
|
|
144
|
+
if (!currentCreds) {
|
|
145
|
+
throw new Error(`Service "${service}" not configured in secrets`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const newCreds: ServiceCredentials = {
|
|
149
|
+
...currentCreds,
|
|
150
|
+
password: generateSecurePassword(),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const newServices = {
|
|
154
|
+
...secrets.services,
|
|
155
|
+
[service]: newCreds,
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
...secrets,
|
|
160
|
+
updatedAt: new Date().toISOString(),
|
|
161
|
+
services: newServices,
|
|
162
|
+
urls: generateConnectionUrls(newServices),
|
|
163
|
+
};
|
|
164
|
+
}
|