forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredcheck.js
52 lines (45 loc) · 2.33 KB
/
credcheck.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
const args = require('yargs').argv;
const os = require('os');
const fs = require('fs');
const path = require('path');
function credcheck(dir) {
console.log('Path: ' + dir);
const redactDict = new Map();
// storage account keys
redactDict.set(/\\"keyName\\":\\"key1\\",\\"value\\":\\"(.*?)\\"/g, '\\"keyName\\":\\"key1\\",\\"value\\":\\"***REMOVED***\\"');
redactDict.set(/\\"keyName\\":\\"key2\\",\\"value\\":\\"(.*?)\\"/g, '\\"keyName\\":\\"key2\\",\\"value\\":\\"***REMOVED***\\"');
redactDict.set(/;AccountKey=(.*?)(;|\\")/g, ';AccountKey=***REMOVED***$2');
redactDict.set(/\\"primaryMasterKey\\":\\"(.*?)\\"/g, '\\"primaryMasterKey\\":\\"***REMOVED***\\"');
redactDict.set(/\\"primaryReadonlyMasterKey\\":\\"(.*?)\\"/g, '\\"primaryReadonlyMasterKey\\":\\"***REMOVED***\\"');
redactDict.set(/\\"secondaryMasterKey\\":\\"(.*?)\\"/g, '\\"secondaryMasterKey\\":\\"***REMOVED***\\"');
redactDict.set(/\\"secondaryReadonlyMasterKey\\":\\"(.*?)\\"/g, '\\"secondaryReadonlyMasterKey\\":\\"***REMOVED***\\"');
redactDict.set(/;SharedAccessKey=(.*?)(;|\\")/g, ';SharedAccessKey=***REMOVED***$2');
redactDict.set(/\\"primaryKey\\":\\"(.*?)\\"/g, '\\"primaryKey\\":\\"***REMOVED***\\"');
redactDict.set(/\\"secondaryKey\\":\\"(.*?)\\"/g, '\\"secondaryKey\\":\\"***REMOVED***\\"');
credcheckRecursive(dir, redactDict);
}
function credcheckRecursive(dir, redactDict) {
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach(function(file, index) {
const curPath = path.join(dir, file);
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
credcheckRecursive(curPath, redactDict);
} else {
if (curPath.endsWith('.json')) {
const content = fs.readFileSync(curPath).toString('utf8');
var redactedContent = content;
for (const [key, value] of redactDict.entries()) {
redactedContent = redactedContent.replace(key, value);
}
if (redactedContent !== content) {
console.log('File redacted: ' + curPath);
fs.writeFileSync(curPath, redactedContent);
}
}
}
});
}
}
const dir = args['path']
credcheck(dir);