forked from elasticsearch-dump/elasticsearch-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use URL syntax for declaring parameters to module-based transforms - Add anonymize transform - Add yarn lock
- Loading branch information
1 parent
75a3501
commit 59f5a84
Showing
6 changed files
with
1,833 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var crypto = require('crypto') | ||
|
||
module.exports = function (doc) { | ||
doc._source.bar = crypto | ||
.createHash('md5') | ||
.update(doc._source.foo) | ||
.digest('hex') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} |
Oops, something went wrong.