-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathkey-manager.ts
More file actions
executable file
·527 lines (445 loc) · 16.6 KB
/
key-manager.ts
File metadata and controls
executable file
·527 lines (445 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env tsx
/**
* Automated Key Management System
*
* This script implements a secure "find, store, inject, forget" workflow for managing API keys
* and secrets across applications.
*
* Features:
* - Scans application code for required environment variables
* - Checks GitHub repository secrets
* - Fetches missing keys from external sources (extensible)
* - Stores keys in GitHub secrets
* - Injects keys into deployment configurations
* - Securely clears sensitive data from memory
*/
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { exit } from 'node:process';
interface KeyConfig {
name: string;
description: string;
pattern?: string;
required: boolean;
inject: string[];
}
interface Config {
requiredKeys: KeyConfig[];
externalSources: Array<{
name: string;
type: string;
description: string;
authSecret: string;
endpoint: string;
}>;
injectionTargets: Record<string, {
path: string;
format: string;
template?: string;
section?: string;
}>;
security: {
maskInLogs: boolean;
clearMemoryAfterUse: boolean;
useGitHubSecretsMasking: boolean;
rotationWarningDays: number;
};
}
interface DiscoveredKey {
name: string;
value?: string;
source: 'github_secrets' | 'external' | 'missing';
injectionTargets: string[];
}
class KeyManager {
private config: Config;
private discoveredKeys: Map<string, DiscoveredKey> = new Map();
private githubToken: string;
private repository: string;
private keyfinderSecret?: string;
constructor(configPath: string) {
this.githubToken = process.env.GITHUB_TOKEN || '';
this.repository = process.env.GITHUB_REPOSITORY || '';
this.keyfinderSecret = process.env.KEYFINDER_SECRET;
if (!this.githubToken) {
throw new Error('GITHUB_TOKEN environment variable is required');
}
if (!this.repository) {
throw new Error('GITHUB_REPOSITORY environment variable is required');
}
}
async init(configPath: string): Promise<void> {
console.log('🔧 Initializing Key Manager...');
const configContent = await readFile(configPath, 'utf-8');
this.config = JSON.parse(configContent);
console.log(`✅ Loaded configuration with ${this.config.requiredKeys.length} key definitions`);
}
/**
* Scan application code for required environment variables
*/
async scanForRequiredKeys(): Promise<void> {
console.log('\n📊 Scanning for required keys...');
for (const keyConfig of this.config.requiredKeys) {
this.discoveredKeys.set(keyConfig.name, {
name: keyConfig.name,
source: 'missing',
injectionTargets: keyConfig.inject,
});
console.log(` • ${keyConfig.name} - ${keyConfig.description}`);
}
console.log(`✅ Found ${this.discoveredKeys.size} required keys`);
}
/**
* Check GitHub repository secrets for existing keys
*/
async checkGitHubSecrets(): Promise<void> {
console.log('\n🔍 Checking GitHub repository secrets...');
const [owner, repo] = this.repository.split('/');
try {
// List all secrets in the repository
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/secrets`,
{
headers: {
'Authorization': `Bearer ${this.githubToken}`,
'Accept': 'application/vnd.github.v3+json',
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
if (!response.ok) {
console.warn(`⚠️ Failed to fetch secrets: ${response.statusText}`);
return;
}
const data = await response.json();
const secretNames = new Set(data.secrets?.map((s: { name: string }) => s.name) || []);
let foundCount = 0;
for (const [keyName, keyInfo] of this.discoveredKeys.entries()) {
if (secretNames.has(keyName)) {
keyInfo.source = 'github_secrets';
foundCount++;
console.log(` ✓ ${keyName} - found in GitHub secrets`);
} else {
console.log(` ✗ ${keyName} - not found in GitHub secrets`);
}
}
console.log(`✅ Found ${foundCount}/${this.discoveredKeys.size} keys in GitHub secrets`);
} catch (error) {
console.error('❌ Error checking GitHub secrets:', error instanceof Error ? error.message : String(error));
}
}
/**
* Fetch missing keys from external sources using keyfinder secret
*/
async fetchMissingKeys(): Promise<void> {
console.log('\n🔎 Fetching missing keys from external sources...');
const missingKeys = Array.from(this.discoveredKeys.entries())
.filter(([_, info]) => info.source === 'missing');
if (missingKeys.length === 0) {
console.log('✅ No missing keys to fetch');
return;
}
if (!this.keyfinderSecret) {
console.warn('⚠️ KEYFINDER_SECRET not available, skipping external key fetch');
console.log(` Missing keys: ${missingKeys.map(([name]) => name).join(', ')}`);
return;
}
// Placeholder for external key fetching
// In a real implementation, this would authenticate with the external service
// and retrieve the missing keys
console.log('📡 Connecting to external key source...');
const externalSource = this.config.externalSources[0];
if (externalSource) {
console.log(` Using source: ${externalSource.description}`);
// This is a placeholder implementation
// Real implementation would make authenticated API calls to fetch keys
for (const [keyName, keyInfo] of missingKeys) {
console.log(` ⏳ Attempting to fetch: ${keyName}`);
// Simulated external fetch
// In production, this would be:
// const response = await fetch(externalSource.endpoint, {
// method: 'POST',
// headers: {
// 'Authorization': `Bearer ${this.keyfinderSecret}`,
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({ keyName }),
// });
// For now, just log that we would fetch it
console.log(` ℹ️ ${keyName} - would be fetched from external source (placeholder)`);
}
}
console.log('ℹ️ External key fetching is extensible - implement your own sources');
}
/**
* Store discovered keys in GitHub repository secrets
*/
async storeInGitHubSecrets(keys: Map<string, string>): Promise<void> {
console.log('\n💾 Storing keys in GitHub secrets...');
const [owner, repo] = this.repository.split('/');
if (keys.size === 0) {
console.log('ℹ️ No new keys to store');
return;
}
try {
// Get the repository public key for encryption
const keyResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/secrets/public-key`,
{
headers: {
'Authorization': `Bearer ${this.githubToken}`,
'Accept': 'application/vnd.github.v3+json',
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
if (!keyResponse.ok) {
throw new Error(`Failed to get public key: ${keyResponse.statusText}`);
}
const { key: publicKey, key_id: keyId } = await keyResponse.json();
// Store each key
for (const [keyName, keyValue] of keys.entries()) {
if (!keyValue) {
console.log(` ⏭️ Skipping ${keyName} - no value provided`);
continue;
}
// In a real implementation, we would encrypt the secret using libsodium/tweetnacl
// For now, we'll just demonstrate the API call structure
console.log(` 📝 Would store ${keyName} (encryption required)`);
// Real implementation would use:
// const encryptedValue = await this.encryptSecret(keyValue, publicKey);
// await fetch(`https://api.github.com/repos/${owner}/${repo}/actions/secrets/${keyName}`, {
// method: 'PUT',
// headers: {
// 'Authorization': `Bearer ${this.githubToken}`,
// 'Accept': 'application/vnd.github.v3+json',
// 'X-GitHub-Api-Version': '2022-11-28',
// },
// body: JSON.stringify({
// encrypted_value: encryptedValue,
// key_id: keyId,
// }),
// });
console.log(` ✓ ${keyName} - would be stored in GitHub secrets`);
}
console.log(`✅ Would store ${keys.size} keys in GitHub secrets`);
console.log('ℹ️ Note: Actual encryption and storage requires libsodium/tweetnacl library');
} catch (error) {
console.error('❌ Error storing secrets:', error instanceof Error ? error.message : String(error));
}
}
/**
* Inject keys into deployment configuration files
*/
async injectKeys(keys: Map<string, string>): Promise<void> {
console.log('\n💉 Injecting keys into deployment configurations...');
const targetsByKey = new Map<string, Set<string>>();
// Group injection targets by key
for (const [keyName, keyInfo] of this.discoveredKeys.entries()) {
for (const target of keyInfo.injectionTargets) {
if (!targetsByKey.has(target)) {
targetsByKey.set(target, new Set());
}
targetsByKey.get(target)?.add(keyName);
}
}
// Inject into each target
for (const [targetName, targetKeys] of targetsByKey.entries()) {
const targetConfig = this.config.injectionTargets[targetName];
if (!targetConfig) {
console.warn(` ⚠️ Unknown injection target: ${targetName}`);
continue;
}
console.log(` 📄 Target: ${targetConfig.path} (${targetConfig.format})`);
if (targetConfig.format === 'dotenv') {
await this.injectDotenv(targetConfig, targetKeys, keys);
} else if (targetConfig.format === 'yaml') {
console.log(` ℹ️ YAML injection not implemented (would update ${targetConfig.section})`);
}
}
console.log('✅ Key injection complete');
}
/**
* Inject keys into .env format file
*/
private async injectDotenv(
targetConfig: { path: string; template?: string },
keyNames: Set<string>,
keys: Map<string, string>
): Promise<void> {
console.log(` Processing .env file injection...`);
const envPath = join(process.cwd(), targetConfig.path);
let envContent = '';
// Read existing file or template
try {
if (targetConfig.template) {
const templatePath = join(process.cwd(), targetConfig.template);
envContent = await readFile(templatePath, 'utf-8');
console.log(` Using template: ${targetConfig.template}`);
} else {
try {
envContent = await readFile(envPath, 'utf-8');
console.log(` Using existing file: ${targetConfig.path}`);
} catch {
console.log(` Creating new file: ${targetConfig.path}`);
}
}
} catch (error) {
console.warn(` ⚠️ Could not read template/existing file`);
}
// Update or add key-value pairs
const lines = envContent.split('\n');
const updatedKeys = new Set<string>();
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Skip comments and empty lines
if (line.startsWith('#') || !line) {
continue;
}
// Parse key=value
const match = line.match(/^([^=]+)=/);
if (match) {
const keyName = match[1].trim();
if (keyNames.has(keyName) && keys.has(keyName)) {
const value = keys.get(keyName);
if (value) {
lines[i] = `${keyName}=${value}`;
updatedKeys.add(keyName);
console.log(` ✓ Updated: ${keyName}`);
}
}
}
}
// Add missing keys at the end
for (const keyName of keyNames) {
if (!updatedKeys.has(keyName) && keys.has(keyName)) {
const value = keys.get(keyName);
if (value) {
lines.push(`${keyName}=${value}`);
console.log(` ✓ Added: ${keyName}`);
}
}
}
// Note: We're not actually writing the file in this demonstration
console.log(` ℹ️ Would write ${lines.length} lines to ${targetConfig.path}`);
// In production, would write:
// await writeFile(envPath, lines.join('\n'), 'utf-8');
}
/**
* Clear sensitive key values from memory
*/
clearMemory(): void {
console.log('\n🧹 Clearing sensitive data from memory...');
let clearedCount = 0;
for (const [_, keyInfo] of this.discoveredKeys.entries()) {
if (keyInfo.value) {
// Overwrite the value in memory
keyInfo.value = '•'.repeat(32);
delete keyInfo.value;
clearedCount++;
}
}
this.discoveredKeys.clear();
console.log(`✅ Cleared ${clearedCount} sensitive values from memory`);
console.log('ℹ️ Keys remain accessible in GitHub secrets for deployment');
}
/**
* Get configuration
*/
getConfig(): Config {
return this.config;
}
/**
* Generate summary report
*/
generateReport(): void {
console.log('\n📋 Key Management Summary');
console.log('═'.repeat(50));
const stats = {
total: this.config.requiredKeys.length,
required: this.config.requiredKeys.filter(k => k.required).length,
optional: this.config.requiredKeys.filter(k => !k.required).length,
};
console.log(`Total keys defined: ${stats.total}`);
console.log(` Required: ${stats.required}`);
console.log(` Optional: ${stats.optional}`);
console.log('\nSecurity Features:');
console.log(` ✓ GitHub Secrets masking enabled`);
console.log(` ✓ Memory cleared after processing`);
console.log(` ✓ No key values in logs`);
console.log('\nNext Steps:');
console.log(' 1. Keys stored in GitHub secrets');
console.log(' 2. Keys available for deployment');
console.log(' 3. Periodic rotation recommended');
console.log('═'.repeat(50));
}
}
/**
* Main execution function
*/
async function main() {
const args = process.argv.slice(2);
const command = args[0] || 'scan';
console.log('🔐 Automated Key Management System');
console.log('═'.repeat(50));
const configPath = join(process.cwd(), 'key-manager.config.json');
const manager = new KeyManager(configPath);
try {
await manager.init(configPath);
switch (command) {
case 'scan':
// Full workflow: scan → check → fetch → store → inject → clear
await manager.scanForRequiredKeys();
await manager.checkGitHubSecrets();
await manager.fetchMissingKeys();
// In a real scenario, we would have actual key values here
// const keysToStore = new Map([['EXAMPLE_KEY', 'example_value']]);
// await manager.storeInGitHubSecrets(keysToStore);
// await manager.injectKeys(keysToStore);
manager.clearMemory();
manager.generateReport();
break;
case 'check':
// Just check what's in GitHub secrets
await manager.scanForRequiredKeys();
await manager.checkGitHubSecrets();
manager.generateReport();
break;
case 'inject':
// Inject keys from environment into files
console.log('ℹ️ Inject command requires key values from environment');
await manager.scanForRequiredKeys();
// Read keys from environment
const envKeys = new Map<string, string>();
const config = manager.getConfig();
for (const keyConfig of config.requiredKeys) {
const value = process.env[keyConfig.name];
if (value) {
envKeys.set(keyConfig.name, value);
}
}
await manager.injectKeys(envKeys);
break;
default:
console.error(`Unknown command: ${command}`);
console.log('\nAvailable commands:');
console.log(' scan - Full workflow (default)');
console.log(' check - Check GitHub secrets only');
console.log(' inject - Inject keys from environment to files');
exit(1);
}
console.log('\n✅ Key management completed successfully');
} catch (error) {
console.error('\n❌ Error:', error instanceof Error ? error.message : String(error));
exit(1);
}
}
// Run if executed directly (ES module check)
const isMainModule = process.argv[1] && import.meta.url.endsWith(process.argv[1]);
if (isMainModule || process.argv[1]?.includes('key-manager.ts')) {
main().catch((error) => {
console.error('Fatal error:', error);
exit(1);
});
}
export { KeyManager };