2017-09-01 04:45:51 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
decryptConfig,
|
|
|
|
};
|
|
|
|
|
2017-11-08 05:44:03 +00:00
|
|
|
function decryptConfig(config, privateKey) {
|
2018-03-27 19:57:02 +00:00
|
|
|
logger.trace({ config }, 'decryptConfig()');
|
2017-11-03 06:51:44 +00:00
|
|
|
const decryptedConfig = { ...config };
|
2017-11-10 12:46:16 +00:00
|
|
|
for (const [key, val] of Object.entries(config)) {
|
2017-09-01 04:45:51 +00:00
|
|
|
if (key === 'encrypted' && isObject(val)) {
|
|
|
|
logger.debug({ config: val }, 'Found encrypted config');
|
|
|
|
if (privateKey) {
|
2017-11-10 12:46:16 +00:00
|
|
|
for (const [eKey, eVal] of Object.entries(val)) {
|
2017-09-01 04:45:51 +00:00
|
|
|
try {
|
2017-09-01 05:43:49 +00:00
|
|
|
const decryptedStr = crypto
|
2017-11-10 12:46:16 +00:00
|
|
|
.privateDecrypt(privateKey, Buffer.from(eVal, 'base64'))
|
2017-09-01 04:45:51 +00:00
|
|
|
.toString();
|
2017-11-10 12:46:16 +00:00
|
|
|
logger.info(`Decrypted ${eKey}`);
|
|
|
|
if (eKey === 'npmToken') {
|
2017-09-01 05:43:49 +00:00
|
|
|
logger.info('Migrating npmToken to npmrc');
|
2018-01-10 13:27:19 +00:00
|
|
|
decryptedConfig.npmrc = `//registry.npmjs.org/:_authToken=${decryptedStr}\n`;
|
2017-09-01 05:43:49 +00:00
|
|
|
} else {
|
2017-11-10 12:46:16 +00:00
|
|
|
decryptedConfig[eKey] = decryptedStr;
|
2017-09-01 05:43:49 +00:00
|
|
|
}
|
2017-09-01 04:45:51 +00:00
|
|
|
} catch (err) {
|
2017-11-10 12:46:16 +00:00
|
|
|
logger.warn({ err }, `Error decrypting ${eKey}`);
|
2017-09-01 04:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error('Found encrypted data but no privateKey');
|
|
|
|
}
|
|
|
|
delete decryptedConfig.encrypted;
|
2017-11-08 05:44:03 +00:00
|
|
|
} else if (isObject(val) && key !== 'content') {
|
|
|
|
decryptedConfig[key] = decryptConfig(val, privateKey);
|
2017-09-01 04:45:51 +00:00
|
|
|
} else if (Array.isArray(val)) {
|
|
|
|
decryptedConfig[key] = [];
|
|
|
|
val.forEach(item => {
|
|
|
|
if (isObject(item)) {
|
2017-11-08 05:44:03 +00:00
|
|
|
decryptedConfig[key].push(decryptConfig(item, privateKey));
|
2017-09-01 04:45:51 +00:00
|
|
|
} else {
|
|
|
|
decryptedConfig[key].push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete decryptedConfig.encrypted;
|
2017-09-01 05:43:49 +00:00
|
|
|
logger.trace({ config: decryptedConfig }, 'decryptedConfig');
|
2017-09-01 04:45:51 +00:00
|
|
|
return decryptedConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isObject(obj) {
|
|
|
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
|
|
}
|