forked from mangelsnc/undisclosed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
Β·189 lines (147 loc) Β· 4.82 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
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
#!/usr/bin/env node
const fs = require('fs');
const crypto = require('crypto');
const args = process.argv;
const privateKeyPath = process.env.PWD + '/private.pem';
const publicKeyPath = process.env.PWD + '/public.pem';
const encryptedDataPath = process.env.PWD + '/.env.enc';
const decryptedDataPath = process.env.PWD + '/.env';
const subcommand = args[2];
const commandHandler = {
'generate-keypair': handleGenereateKeyPair,
'list': handleList,
'set': handleSet,
'get': handleGet,
'dump': handleDump
}
if (commandHandler.hasOwnProperty(subcommand)) {
commandHandler[subcommand]();
} else {
console.log("Usage: secrets [generate-keypair|list|set|get|dump]\n")
}
process.exit(0);
function encrypt(toEncrypt, publicKey) {
const buffer = Buffer.from(toEncrypt,'utf8');
const encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString('base64');
}
function decrypt(toDecrypt, privateKey) {
const buffer = Buffer.from(toDecrypt,'base64');
const decrypted = crypto.privateDecrypt(privateKey, buffer);
return decrypted.toString('utf8');
}
function keysExists() {
return fs.existsSync(privateKeyPath) || fs.existsSync(publicKeyPath);
}
function encryptedFileExists() {
return fs.existsSync(encryptedDataPath);
}
function generateKeyPair(privateKeyPath, publicKeyPath) {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096
});
const publicKey = keyPair.publicKey.export({
type: 'pkcs1',
format: 'pem'
});
const privateKey = keyPair.privateKey.export({
type: 'pkcs1',
format: 'pem'
});
fs.writeFileSync(publicKeyPath, publicKey);
fs.writeFileSync(privateKeyPath, privateKey);
const dataToShow = [
{ type: 'public', path: publicKeyPath, value: truncate(publicKey) },
{ type: 'private', path: privateKeyPath, value: truncate(privateKey) }
];
console.table(dataToShow);
}
function truncate(string, limit = 20) {
if (string.length <= limit) {
return string;
}
return string.slice(0, limit) + '...'
}
function loadSecrets(truncateValue = false) {
let data = fs.readFileSync(encryptedDataPath);
data = data.toString().split("\n");
const dataArray = [];
data.forEach(line => {
if (!line) {
return
}
const lineArray = line.split('=');
dataArray.push({
key: lineArray[0],
value: truncateValue ? truncate(lineArray[1]) : lineArray[1]
});
});
return dataArray;
}
function handleGenereateKeyPair() {
if (keysExists()) {
console.error("Keypair already exists. Remove it before generate new keypair.\n");
process.exit(1);
}
generateKeyPair(privateKeyPath, publicKeyPath);
}
function handleList() {
if (!encryptedFileExists()) {
console.error("Secrets file not found.\n");
process.exit(1);
}
const secrets = loadSecrets(true);
console.table(secrets);
}
function handleSet() {
if (!keysExists()) {
console.log("Keypair not found, run before:\n\tsecrets generate-keypair");
process.exit(1);
}
const publicKey = fs.readFileSync(publicKeyPath, 'utf8').toString();
const key = args[3];
const value = args[4];
const encryptedValue = encrypt(value, publicKey);
fs.appendFileSync(encryptedDataPath, key.toUpperCase() + '=' + encryptedValue + "\n");
console.table([
{ key: key.toUpperCase(), value: truncate(encryptedValue) }
]);
}
function handleGet() {
if (!encryptedFileExists()) {
console.error("Secrets file not found.\n");
process.exit(1);
}
if (!keysExists()) {
console.log("Keypair not found, run before:\n\tsecrets generate-keypair");
process.exit(1);
}
const keyToFind = args[3].toUpperCase();
const privateKey = fs.readFileSync(privateKeyPath, 'utf8').toString();
loadSecrets().forEach(secret => {
if (secret.key === keyToFind) {
secret.value = decrypt(secret.value, privateKey);
console.table([secret]);
process.exit(0);
}
});
console.error("Secret not found.\n");
}
function handleDump() {
if (!encryptedFileExists()) {
console.error("Secrets file not found.\n");
process.exit(1);
}
if (!keysExists()) {
console.log("Keypair not found, run before:\n\tsecrets generate-keypair");
process.exit(1);
}
const privateKey = fs.readFileSync(privateKeyPath, 'utf8').toString();
const dumpedContent = []
loadSecrets().forEach(secret => {
secret.value = decrypt(secret.value, privateKey);
dumpedContent.push(secret.key + '=' + secret.value);
});
fs.writeFileSync(decryptedDataPath, dumpedContent.join("\n"));
console.log('Secrets dumped to: ' + decryptedDataPath + "\n");
}