forked from elasticsearch-dump/elasticsearch-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymize.js
77 lines (69 loc) · 2 KB
/
anonymize.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
var crypto = require('crypto')
function anonymize (thing, options = {}) {
if (
thing === null ||
thing instanceof Date
) {
return thing
}
options = Object.assign({
domain: 'example.com',
mobile: '+1-555-123-4567',
blacklist: []
}, options)
if (typeof options.blacklist === 'string') {
options.blacklist = options.blacklist.split(',')
}
switch (typeof thing) {
case 'object':
Object
.keys(thing)
.reduce(function (object, key) {
if (options.blacklist.includes(key)) {
return object
}
switch (typeof object[key]) {
case 'object':
anonymize(object[key], options)
break
case 'string':
object[key] = anonymize(object[key], options)
break
default:
}
return object
}, thing)
break
case 'string':
// If it looks like a date or datetime, leave it alone
if (/^\d{4}-[01]\d-[0-3]\d(?:[T ][0-2]\d:[0-5]\d:[0-5]\d)?$/.test(thing)) {
return thing
}
return [
[
// If it looks like an email, replace it with a hashed variant with the configured domain
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/ig,
function (found) {
return crypto
.createHash('md5')
.update(found)
.digest('hex')
.slice(0, 11) +
'@' + options.domain
}
],
[
// If it looks like a mobile number, replace it with the configured one
/[+0][-0-9\s]{6,}[0-9]/g,
options.mobile
]
].reduce(function (string, replacement) {
return string.replace(replacement[0], replacement[1])
}, thing)
default:
return thing
}
}
module.exports = function (doc, options = {}) {
anonymize(doc._source, options)
}