-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (137 loc) · 4.53 KB
/
index.js
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
const fs = require('fs');
const os = require('os');
const path = require('path');
const crypto = require('crypto');
const readline = require('readline');
const algorithm = 'aes-256-ctr';
const secretFilePath = path.join(os.homedir(), '.secrets.json');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function encrypt(text, password) {
const key = crypto.scryptSync(password, 'salt', 32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted;
}
function decrypt(text, password) {
const key = crypto.scryptSync(password, 'salt', 32);
const [ivHex, encryptedText] = text.split(':');
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
function saveSecrets(secrets, password) {
const encryptedContent = encrypt(JSON.stringify(secrets), password);
fs.writeFileSync(secretFilePath, encryptedContent);
}
function loadSecrets(password) {
if (fs.existsSync(secretFilePath)) {
const encryptedContent = fs.readFileSync(secretFilePath, 'utf8');
try {
const decryptedContent = decrypt(encryptedContent, password);
return JSON.parse(decryptedContent);
} catch (error) {
console.error('Failed to decrypt secrets. Incorrect password?');
return {};
}
}
return {};
}
function setSecret(key, value, password) {
const secrets = loadSecrets(password);
secrets[key] = value;
saveSecrets(secrets, password);
}
function getSecret(key, password) {
const secrets = loadSecrets(password);
return secrets[key] || null;
}
function exportToEnv(key, value) {
const envTmpPath = path.join(os.homedir(), '.env-tmp');
fs.appendFileSync(envTmpPath, `${key}=${value}\n`);
}
function loadSecretsToEnv(password) {
const secrets = loadSecrets(password);
for (const key in secrets) {
exportToEnv(key, secrets[key]);
}
}
function loadSpecificSecretsToEnv(keys, password) {
const secrets = loadSecrets(password);
keys.split(',').forEach(key => {
if (secrets[key]) {
exportToEnv(key, secrets[key]);
}
});
}
function getExportString(secrets) {
const isWindows = process.platform === 'win32';
const command = isWindows ? 'set' : 'export';
return Object.entries(secrets)
.map(([key, value]) => `${command} ${key}=${value}`)
.join('\n');
}
const command = process.argv[2];
if (command === 'set') {
const key = process.argv[3];
const value = process.argv[4];
rl.question('Enter password: ', (password) => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
process.stdout.write('Enter password: *****\n');
setSecret(key, value, password);
console.log('Secret set successfully.');
rl.close();
});
} else if (command === 'get') {
const key = process.argv[3];
rl.question('Enter password: ', (password) => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
process.stdout.write('Enter password: *****\n');
const secret = getSecret(key, password);
if (secret) {
console.log(`Secret: ${secret}`);
} else {
console.log('Secret not found or incorrect password.');
}
rl.close();
});
} else if (command === 'load') {
rl.question('Enter password: ', (password) => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
process.stdout.write('Enter password: *****\n');
loadSecretsToEnv(password);
console.log('All secrets loaded into environment variables.');
rl.close();
});
} else if (command === 'load-specific') {
const keys = process.argv[3];
rl.question('Enter password: ', (password) => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
process.stdout.write('Enter password: *****\n');
loadSpecificSecretsToEnv(keys, password);
console.log('Specific secrets loaded into environment variables.');
rl.close();
});
} else if (command === 'load-str') {
rl.question('Enter password: ', (password) => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
process.stdout.write('Enter password: *****\n');
const secrets = loadSecrets(password);
console.log(getExportString(secrets));
rl.close();
});
} else {
console.log('Unknown command. Available commands: set, get, load, load-specific, load-str');
process.exit(1);
}