@geekmidas/cli 0.10.0 → 0.13.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-B1qy9b-j.cjs +112 -0
- package/dist/bundler-B1qy9b-j.cjs.map +1 -0
- package/dist/bundler-DskIqW2t.mjs +111 -0
- package/dist/bundler-DskIqW2t.mjs.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 +2123 -179
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +2141 -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-BOOpAF8N.cjs +5 -0
- package/dist/storage-Bj1E26lU.cjs +187 -0
- package/dist/storage-Bj1E26lU.cjs.map +1 -0
- package/dist/storage-kSxTjkNb.mjs +133 -0
- package/dist/storage-kSxTjkNb.mjs.map +1 -0
- package/dist/storage-tgZSUnKl.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__/bundler.spec.ts +444 -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 +210 -0
- package/src/build/endpoint-analyzer.ts +236 -0
- package/src/build/handler-templates.ts +1253 -0
- package/src/build/index.ts +260 -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 +611 -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 +192 -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,611 @@
|
|
|
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
|
+
validateEnvironmentVariables,
|
|
15
|
+
writeStageSecrets,
|
|
16
|
+
} from '../storage';
|
|
17
|
+
import type { StageSecrets } from '../types';
|
|
18
|
+
|
|
19
|
+
describe('path utilities', () => {
|
|
20
|
+
describe('getSecretsDir', () => {
|
|
21
|
+
it('should return .gkm/secrets relative to cwd', () => {
|
|
22
|
+
const dir = getSecretsDir('/project');
|
|
23
|
+
expect(dir).toBe('/project/.gkm/secrets');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('getSecretsPath', () => {
|
|
28
|
+
it('should return path for stage file', () => {
|
|
29
|
+
const path = getSecretsPath('production', '/project');
|
|
30
|
+
expect(path).toBe('/project/.gkm/secrets/production.json');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should handle stage names with special characters', () => {
|
|
34
|
+
const path = getSecretsPath('dev-local', '/project');
|
|
35
|
+
expect(path).toBe('/project/.gkm/secrets/dev-local.json');
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('secretsExist', () => {
|
|
40
|
+
it('should return false for non-existent secrets', () => {
|
|
41
|
+
const exists = secretsExist('nonexistent', '/nonexistent-path');
|
|
42
|
+
expect(exists).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('file operations', () => {
|
|
48
|
+
let tempDir: string;
|
|
49
|
+
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
tempDir = join(tmpdir(), `gkm-test-${Date.now()}`);
|
|
52
|
+
await mkdir(tempDir, { recursive: true });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(async () => {
|
|
56
|
+
if (existsSync(tempDir)) {
|
|
57
|
+
await rm(tempDir, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('writeStageSecrets / readStageSecrets', () => {
|
|
62
|
+
it('should write and read secrets', async () => {
|
|
63
|
+
const secrets: StageSecrets = {
|
|
64
|
+
stage: 'production',
|
|
65
|
+
createdAt: '2024-01-01T00:00:00.000Z',
|
|
66
|
+
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
67
|
+
services: {
|
|
68
|
+
postgres: {
|
|
69
|
+
host: 'postgres',
|
|
70
|
+
port: 5432,
|
|
71
|
+
username: 'app',
|
|
72
|
+
password: 'secret123',
|
|
73
|
+
database: 'app',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
urls: {
|
|
77
|
+
DATABASE_URL: 'postgresql://app:secret123@postgres:5432/app',
|
|
78
|
+
},
|
|
79
|
+
custom: {},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
await writeStageSecrets(secrets, tempDir);
|
|
83
|
+
const read = await readStageSecrets('production', tempDir);
|
|
84
|
+
|
|
85
|
+
expect(read).toEqual(secrets);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should create directory if it does not exist', async () => {
|
|
89
|
+
const secrets: StageSecrets = {
|
|
90
|
+
stage: 'staging',
|
|
91
|
+
createdAt: new Date().toISOString(),
|
|
92
|
+
updatedAt: new Date().toISOString(),
|
|
93
|
+
services: {},
|
|
94
|
+
urls: {},
|
|
95
|
+
custom: {},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
await writeStageSecrets(secrets, tempDir);
|
|
99
|
+
|
|
100
|
+
expect(existsSync(join(tempDir, '.gkm/secrets'))).toBe(true);
|
|
101
|
+
expect(existsSync(join(tempDir, '.gkm/secrets/staging.json'))).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should return null for non-existent stage', async () => {
|
|
105
|
+
const read = await readStageSecrets('nonexistent', tempDir);
|
|
106
|
+
expect(read).toBeNull();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('secretsExist', () => {
|
|
111
|
+
it('should return true when secrets file exists', async () => {
|
|
112
|
+
const secrets: StageSecrets = {
|
|
113
|
+
stage: 'test',
|
|
114
|
+
createdAt: new Date().toISOString(),
|
|
115
|
+
updatedAt: new Date().toISOString(),
|
|
116
|
+
services: {},
|
|
117
|
+
urls: {},
|
|
118
|
+
custom: {},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
await writeStageSecrets(secrets, tempDir);
|
|
122
|
+
expect(secretsExist('test', tempDir)).toBe(true);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should return false when secrets file does not exist', () => {
|
|
126
|
+
expect(secretsExist('nonexistent', tempDir)).toBe(false);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('setCustomSecret', () => {
|
|
131
|
+
it('should add custom secret to existing secrets', async () => {
|
|
132
|
+
const secrets: StageSecrets = {
|
|
133
|
+
stage: 'production',
|
|
134
|
+
createdAt: new Date().toISOString(),
|
|
135
|
+
updatedAt: new Date().toISOString(),
|
|
136
|
+
services: {},
|
|
137
|
+
urls: {},
|
|
138
|
+
custom: {},
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
await writeStageSecrets(secrets, tempDir);
|
|
142
|
+
const updated = await setCustomSecret(
|
|
143
|
+
'production',
|
|
144
|
+
'API_KEY',
|
|
145
|
+
'sk_test_123',
|
|
146
|
+
tempDir,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
expect(updated.custom.API_KEY).toBe('sk_test_123');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should update existing custom secret', async () => {
|
|
153
|
+
const secrets: StageSecrets = {
|
|
154
|
+
stage: 'production',
|
|
155
|
+
createdAt: new Date().toISOString(),
|
|
156
|
+
updatedAt: new Date().toISOString(),
|
|
157
|
+
services: {},
|
|
158
|
+
urls: {},
|
|
159
|
+
custom: { API_KEY: 'old-value' },
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
await writeStageSecrets(secrets, tempDir);
|
|
163
|
+
const updated = await setCustomSecret(
|
|
164
|
+
'production',
|
|
165
|
+
'API_KEY',
|
|
166
|
+
'new-value',
|
|
167
|
+
tempDir,
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
expect(updated.custom.API_KEY).toBe('new-value');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should update updatedAt timestamp', async () => {
|
|
174
|
+
const originalTime = '2024-01-01T00:00:00.000Z';
|
|
175
|
+
const secrets: StageSecrets = {
|
|
176
|
+
stage: 'production',
|
|
177
|
+
createdAt: originalTime,
|
|
178
|
+
updatedAt: originalTime,
|
|
179
|
+
services: {},
|
|
180
|
+
urls: {},
|
|
181
|
+
custom: {},
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
await writeStageSecrets(secrets, tempDir);
|
|
185
|
+
const updated = await setCustomSecret(
|
|
186
|
+
'production',
|
|
187
|
+
'KEY',
|
|
188
|
+
'value',
|
|
189
|
+
tempDir,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
expect(updated.updatedAt).not.toBe(originalTime);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should throw if secrets do not exist for stage', async () => {
|
|
196
|
+
await expect(
|
|
197
|
+
setCustomSecret('nonexistent', 'KEY', 'value', tempDir),
|
|
198
|
+
).rejects.toThrow('Secrets not found for stage "nonexistent"');
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should persist changes to disk', async () => {
|
|
202
|
+
const secrets: StageSecrets = {
|
|
203
|
+
stage: 'production',
|
|
204
|
+
createdAt: new Date().toISOString(),
|
|
205
|
+
updatedAt: new Date().toISOString(),
|
|
206
|
+
services: {},
|
|
207
|
+
urls: {},
|
|
208
|
+
custom: {},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
await writeStageSecrets(secrets, tempDir);
|
|
212
|
+
await setCustomSecret('production', 'NEW_KEY', 'new-value', tempDir);
|
|
213
|
+
|
|
214
|
+
const read = await readStageSecrets('production', tempDir);
|
|
215
|
+
expect(read!.custom.NEW_KEY).toBe('new-value');
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe('toEmbeddableSecrets', () => {
|
|
221
|
+
it('should include URLs', () => {
|
|
222
|
+
const secrets: StageSecrets = {
|
|
223
|
+
stage: 'production',
|
|
224
|
+
createdAt: new Date().toISOString(),
|
|
225
|
+
updatedAt: new Date().toISOString(),
|
|
226
|
+
services: {},
|
|
227
|
+
urls: {
|
|
228
|
+
DATABASE_URL: 'postgresql://...',
|
|
229
|
+
REDIS_URL: 'redis://...',
|
|
230
|
+
},
|
|
231
|
+
custom: {},
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
235
|
+
|
|
236
|
+
expect(embeddable.DATABASE_URL).toBe('postgresql://...');
|
|
237
|
+
expect(embeddable.REDIS_URL).toBe('redis://...');
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should include custom secrets', () => {
|
|
241
|
+
const secrets: StageSecrets = {
|
|
242
|
+
stage: 'production',
|
|
243
|
+
createdAt: new Date().toISOString(),
|
|
244
|
+
updatedAt: new Date().toISOString(),
|
|
245
|
+
services: {},
|
|
246
|
+
urls: {},
|
|
247
|
+
custom: {
|
|
248
|
+
API_KEY: 'sk_test_123',
|
|
249
|
+
WEBHOOK_SECRET: 'whsec_abc',
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
254
|
+
|
|
255
|
+
expect(embeddable.API_KEY).toBe('sk_test_123');
|
|
256
|
+
expect(embeddable.WEBHOOK_SECRET).toBe('whsec_abc');
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('should include postgres service credentials', () => {
|
|
260
|
+
const secrets: StageSecrets = {
|
|
261
|
+
stage: 'production',
|
|
262
|
+
createdAt: new Date().toISOString(),
|
|
263
|
+
updatedAt: new Date().toISOString(),
|
|
264
|
+
services: {
|
|
265
|
+
postgres: {
|
|
266
|
+
host: 'postgres',
|
|
267
|
+
port: 5432,
|
|
268
|
+
username: 'app',
|
|
269
|
+
password: 'secret123',
|
|
270
|
+
database: 'mydb',
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
urls: {},
|
|
274
|
+
custom: {},
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
278
|
+
|
|
279
|
+
expect(embeddable.POSTGRES_USER).toBe('app');
|
|
280
|
+
expect(embeddable.POSTGRES_PASSWORD).toBe('secret123');
|
|
281
|
+
expect(embeddable.POSTGRES_DB).toBe('mydb');
|
|
282
|
+
expect(embeddable.POSTGRES_HOST).toBe('postgres');
|
|
283
|
+
expect(embeddable.POSTGRES_PORT).toBe('5432');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should include redis service credentials', () => {
|
|
287
|
+
const secrets: StageSecrets = {
|
|
288
|
+
stage: 'production',
|
|
289
|
+
createdAt: new Date().toISOString(),
|
|
290
|
+
updatedAt: new Date().toISOString(),
|
|
291
|
+
services: {
|
|
292
|
+
redis: {
|
|
293
|
+
host: 'redis',
|
|
294
|
+
port: 6379,
|
|
295
|
+
username: 'default',
|
|
296
|
+
password: 'redis-pass',
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
urls: {},
|
|
300
|
+
custom: {},
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
304
|
+
|
|
305
|
+
expect(embeddable.REDIS_PASSWORD).toBe('redis-pass');
|
|
306
|
+
expect(embeddable.REDIS_HOST).toBe('redis');
|
|
307
|
+
expect(embeddable.REDIS_PORT).toBe('6379');
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('should include rabbitmq service credentials', () => {
|
|
311
|
+
const secrets: StageSecrets = {
|
|
312
|
+
stage: 'production',
|
|
313
|
+
createdAt: new Date().toISOString(),
|
|
314
|
+
updatedAt: new Date().toISOString(),
|
|
315
|
+
services: {
|
|
316
|
+
rabbitmq: {
|
|
317
|
+
host: 'rabbitmq',
|
|
318
|
+
port: 5672,
|
|
319
|
+
username: 'app',
|
|
320
|
+
password: 'rmq-pass',
|
|
321
|
+
vhost: '/myapp',
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
urls: {},
|
|
325
|
+
custom: {},
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
329
|
+
|
|
330
|
+
expect(embeddable.RABBITMQ_USER).toBe('app');
|
|
331
|
+
expect(embeddable.RABBITMQ_PASSWORD).toBe('rmq-pass');
|
|
332
|
+
expect(embeddable.RABBITMQ_HOST).toBe('rabbitmq');
|
|
333
|
+
expect(embeddable.RABBITMQ_PORT).toBe('5672');
|
|
334
|
+
expect(embeddable.RABBITMQ_VHOST).toBe('/myapp');
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('should handle all services and custom secrets together', () => {
|
|
338
|
+
const secrets: StageSecrets = {
|
|
339
|
+
stage: 'production',
|
|
340
|
+
createdAt: new Date().toISOString(),
|
|
341
|
+
updatedAt: new Date().toISOString(),
|
|
342
|
+
services: {
|
|
343
|
+
postgres: {
|
|
344
|
+
host: 'postgres',
|
|
345
|
+
port: 5432,
|
|
346
|
+
username: 'app',
|
|
347
|
+
password: 'pg-pass',
|
|
348
|
+
database: 'app',
|
|
349
|
+
},
|
|
350
|
+
redis: {
|
|
351
|
+
host: 'redis',
|
|
352
|
+
port: 6379,
|
|
353
|
+
username: 'default',
|
|
354
|
+
password: 'redis-pass',
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
urls: {
|
|
358
|
+
DATABASE_URL: 'postgresql://...',
|
|
359
|
+
REDIS_URL: 'redis://...',
|
|
360
|
+
},
|
|
361
|
+
custom: {
|
|
362
|
+
API_KEY: 'key123',
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const embeddable = toEmbeddableSecrets(secrets);
|
|
367
|
+
|
|
368
|
+
// URLs
|
|
369
|
+
expect(embeddable.DATABASE_URL).toBe('postgresql://...');
|
|
370
|
+
expect(embeddable.REDIS_URL).toBe('redis://...');
|
|
371
|
+
|
|
372
|
+
// Custom
|
|
373
|
+
expect(embeddable.API_KEY).toBe('key123');
|
|
374
|
+
|
|
375
|
+
// Postgres
|
|
376
|
+
expect(embeddable.POSTGRES_PASSWORD).toBe('pg-pass');
|
|
377
|
+
|
|
378
|
+
// Redis
|
|
379
|
+
expect(embeddable.REDIS_PASSWORD).toBe('redis-pass');
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe('maskPassword', () => {
|
|
384
|
+
it('should mask middle characters', () => {
|
|
385
|
+
const masked = maskPassword('abcdefghijklmnop');
|
|
386
|
+
expect(masked).toBe('abcd**********op');
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('should show first 4 and last 2 characters', () => {
|
|
390
|
+
const masked = maskPassword('1234567890');
|
|
391
|
+
expect(masked.slice(0, 4)).toBe('1234');
|
|
392
|
+
expect(masked.slice(-2)).toBe('90');
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('should return all asterisks for short passwords', () => {
|
|
396
|
+
expect(maskPassword('short')).toBe('********');
|
|
397
|
+
expect(maskPassword('12345678')).toBe('********');
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it('should handle exactly 9 character password', () => {
|
|
401
|
+
const masked = maskPassword('123456789');
|
|
402
|
+
expect(masked).toBe('1234***89');
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
describe('validateEnvironmentVariables', () => {
|
|
407
|
+
const baseSecrets: StageSecrets = {
|
|
408
|
+
stage: 'production',
|
|
409
|
+
createdAt: new Date().toISOString(),
|
|
410
|
+
updatedAt: new Date().toISOString(),
|
|
411
|
+
services: {},
|
|
412
|
+
urls: {},
|
|
413
|
+
custom: {},
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
it('should return valid when no variables are required', () => {
|
|
417
|
+
const result = validateEnvironmentVariables([], baseSecrets);
|
|
418
|
+
|
|
419
|
+
expect(result.valid).toBe(true);
|
|
420
|
+
expect(result.missing).toEqual([]);
|
|
421
|
+
expect(result.provided).toEqual([]);
|
|
422
|
+
expect(result.required).toEqual([]);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('should return valid when all required variables are present', () => {
|
|
426
|
+
const secrets: StageSecrets = {
|
|
427
|
+
...baseSecrets,
|
|
428
|
+
urls: {
|
|
429
|
+
DATABASE_URL: 'postgresql://...',
|
|
430
|
+
},
|
|
431
|
+
custom: {
|
|
432
|
+
API_KEY: 'sk_test_123',
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
const result = validateEnvironmentVariables(
|
|
437
|
+
['DATABASE_URL', 'API_KEY'],
|
|
438
|
+
secrets,
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
expect(result.valid).toBe(true);
|
|
442
|
+
expect(result.missing).toEqual([]);
|
|
443
|
+
expect(result.provided).toEqual(['API_KEY', 'DATABASE_URL']);
|
|
444
|
+
expect(result.required).toEqual(['API_KEY', 'DATABASE_URL']);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it('should return invalid when some variables are missing', () => {
|
|
448
|
+
const secrets: StageSecrets = {
|
|
449
|
+
...baseSecrets,
|
|
450
|
+
urls: {
|
|
451
|
+
DATABASE_URL: 'postgresql://...',
|
|
452
|
+
},
|
|
453
|
+
custom: {},
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const result = validateEnvironmentVariables(
|
|
457
|
+
['DATABASE_URL', 'API_KEY', 'JWT_SECRET'],
|
|
458
|
+
secrets,
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
expect(result.valid).toBe(false);
|
|
462
|
+
expect(result.missing).toEqual(['API_KEY', 'JWT_SECRET']);
|
|
463
|
+
expect(result.provided).toEqual(['DATABASE_URL']);
|
|
464
|
+
expect(result.required).toEqual(['API_KEY', 'DATABASE_URL', 'JWT_SECRET']);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it('should return invalid when all variables are missing', () => {
|
|
468
|
+
const result = validateEnvironmentVariables(
|
|
469
|
+
['API_KEY', 'JWT_SECRET'],
|
|
470
|
+
baseSecrets,
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
expect(result.valid).toBe(false);
|
|
474
|
+
expect(result.missing).toEqual(['API_KEY', 'JWT_SECRET']);
|
|
475
|
+
expect(result.provided).toEqual([]);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it('should recognize service credentials as provided', () => {
|
|
479
|
+
const secrets: StageSecrets = {
|
|
480
|
+
...baseSecrets,
|
|
481
|
+
services: {
|
|
482
|
+
postgres: {
|
|
483
|
+
host: 'postgres',
|
|
484
|
+
port: 5432,
|
|
485
|
+
username: 'app',
|
|
486
|
+
password: 'secret',
|
|
487
|
+
database: 'app',
|
|
488
|
+
},
|
|
489
|
+
redis: {
|
|
490
|
+
host: 'redis',
|
|
491
|
+
port: 6379,
|
|
492
|
+
username: 'default',
|
|
493
|
+
password: 'redis-pass',
|
|
494
|
+
},
|
|
495
|
+
},
|
|
496
|
+
urls: {},
|
|
497
|
+
custom: {},
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
const result = validateEnvironmentVariables(
|
|
501
|
+
['POSTGRES_PASSWORD', 'REDIS_HOST', 'POSTGRES_DB'],
|
|
502
|
+
secrets,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
expect(result.valid).toBe(true);
|
|
506
|
+
expect(result.missing).toEqual([]);
|
|
507
|
+
expect(result.provided).toEqual([
|
|
508
|
+
'POSTGRES_DB',
|
|
509
|
+
'POSTGRES_PASSWORD',
|
|
510
|
+
'REDIS_HOST',
|
|
511
|
+
]);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it('should sort missing and provided arrays alphabetically', () => {
|
|
515
|
+
const secrets: StageSecrets = {
|
|
516
|
+
...baseSecrets,
|
|
517
|
+
custom: {
|
|
518
|
+
ZEBRA: 'value',
|
|
519
|
+
ALPHA: 'value',
|
|
520
|
+
},
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
const result = validateEnvironmentVariables(
|
|
524
|
+
['ZEBRA', 'ALPHA', 'YELLOW', 'BETA'],
|
|
525
|
+
secrets,
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
expect(result.missing).toEqual(['BETA', 'YELLOW']);
|
|
529
|
+
expect(result.provided).toEqual(['ALPHA', 'ZEBRA']);
|
|
530
|
+
expect(result.required).toEqual(['ALPHA', 'BETA', 'YELLOW', 'ZEBRA']);
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
it('should handle duplicate required variables', () => {
|
|
534
|
+
const secrets: StageSecrets = {
|
|
535
|
+
...baseSecrets,
|
|
536
|
+
custom: {
|
|
537
|
+
API_KEY: 'value',
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const result = validateEnvironmentVariables(
|
|
542
|
+
['API_KEY', 'API_KEY', 'MISSING'],
|
|
543
|
+
secrets,
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
expect(result.valid).toBe(false);
|
|
547
|
+
expect(result.missing).toEqual(['MISSING']);
|
|
548
|
+
// Note: duplicates in input are preserved in required list
|
|
549
|
+
expect(result.required).toEqual(['API_KEY', 'API_KEY', 'MISSING']);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it('should work with complex service configurations', () => {
|
|
553
|
+
const secrets: StageSecrets = {
|
|
554
|
+
...baseSecrets,
|
|
555
|
+
services: {
|
|
556
|
+
postgres: {
|
|
557
|
+
host: 'postgres',
|
|
558
|
+
port: 5432,
|
|
559
|
+
username: 'app',
|
|
560
|
+
password: 'pg-secret',
|
|
561
|
+
database: 'mydb',
|
|
562
|
+
},
|
|
563
|
+
redis: {
|
|
564
|
+
host: 'redis',
|
|
565
|
+
port: 6379,
|
|
566
|
+
username: 'default',
|
|
567
|
+
password: 'redis-secret',
|
|
568
|
+
},
|
|
569
|
+
rabbitmq: {
|
|
570
|
+
host: 'rabbitmq',
|
|
571
|
+
port: 5672,
|
|
572
|
+
username: 'guest',
|
|
573
|
+
password: 'guest',
|
|
574
|
+
vhost: '/',
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
urls: {
|
|
578
|
+
DATABASE_URL: 'postgresql://...',
|
|
579
|
+
REDIS_URL: 'redis://...',
|
|
580
|
+
RABBITMQ_URL: 'amqp://...',
|
|
581
|
+
},
|
|
582
|
+
custom: {
|
|
583
|
+
JWT_SECRET: 'jwt-secret-value',
|
|
584
|
+
},
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
const result = validateEnvironmentVariables(
|
|
588
|
+
[
|
|
589
|
+
'DATABASE_URL',
|
|
590
|
+
'REDIS_URL',
|
|
591
|
+
'RABBITMQ_URL',
|
|
592
|
+
'JWT_SECRET',
|
|
593
|
+
'POSTGRES_PASSWORD',
|
|
594
|
+
'REDIS_PASSWORD',
|
|
595
|
+
'RABBITMQ_USER',
|
|
596
|
+
'MISSING_VAR',
|
|
597
|
+
],
|
|
598
|
+
secrets,
|
|
599
|
+
);
|
|
600
|
+
|
|
601
|
+
expect(result.valid).toBe(false);
|
|
602
|
+
expect(result.missing).toEqual(['MISSING_VAR']);
|
|
603
|
+
expect(result.provided).toContain('DATABASE_URL');
|
|
604
|
+
expect(result.provided).toContain('REDIS_URL');
|
|
605
|
+
expect(result.provided).toContain('RABBITMQ_URL');
|
|
606
|
+
expect(result.provided).toContain('JWT_SECRET');
|
|
607
|
+
expect(result.provided).toContain('POSTGRES_PASSWORD');
|
|
608
|
+
expect(result.provided).toContain('REDIS_PASSWORD');
|
|
609
|
+
expect(result.provided).toContain('RABBITMQ_USER');
|
|
610
|
+
});
|
|
611
|
+
});
|
|
@@ -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
|
+
}
|