Created
August 19, 2020 20:10
-
-
Save ollie314/a63b95455d07b5f7d831c5ca4947bc3c to your computer and use it in GitHub Desktop.
This is a simple function to demonstrate the first step of the event enrichment process principle.
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
const { generateKeyPair, createHash, publicEncrypt, privateDecrypt, constants: { RSA_PKCS1_PADDING } } = require('crypto'); | |
const path = require('path'); | |
const { readFileSync } = require('fs'); | |
const chalk = require('chalk'); | |
function genKey() { | |
generateKeyPair('rsa', { | |
modulusLength: 4096, | |
publicKeyEncoding: { | |
type: 'spki', | |
format: 'pem' | |
}, | |
privateKeyEncoding: { | |
type: 'pkcs8', | |
format: 'pem', | |
cipher: 'aes-256-cbc', | |
passphrase: 'top secret' | |
} | |
}, (err, publicKey, privateKey) => { | |
if(err) { | |
console.log('error', err); | |
return; | |
} | |
// Handle errors and use the generated key pair. | |
console.log(publicKey, privateKey); | |
}); | |
} | |
const readRaw = (str) => Buffer.from(str); | |
const readCryptRaw = (str) => Buffer.from(str, 'base64'); | |
const readTemplate = (read, getData, keyFile, str) => { | |
const absolutePath = path.resolve(keyFile); | |
const key = readFileSync(absolutePath, "utf8"); | |
const buffer = read(str); | |
const data = getData({key, padding: RSA_PKCS1_PADDING}, buffer); | |
return data.toString("base64"); | |
}; | |
const encrypt = (str, keyFile) => readTemplate(readRaw, publicEncrypt, keyFile, str); | |
const decrypt = (str, keyFile) => readTemplate(readRaw, privateDecrypt, keyFile, str); | |
const nullMask = (v) => v; | |
const loosingMask = (v) => createHash('sha256').update(v).digest('hex'); | |
const conservativeMask = (v) => encrypt(v, 'pub.key'); | |
const conservativeDemask = (v) => decrypt(v, 'priv.key'); | |
const policy = { | |
rso: 66, | |
c1: { | |
rso: 34, | |
t1: { | |
rso: 33, | |
email: { rso: 17, policy: 'loosingMask' }, | |
phone: { rso: 18, policy: 'loosingMask' }, | |
homeType: { rso: 239, policy: 'nullMask' }, | |
} | |
} | |
}; | |
const record = { | |
type: 't1', | |
category: 'c1', | |
email: 'jdoe@mydom.com', | |
firstname: 'john', | |
lastname: 'doe', | |
phone: '0795137678', | |
homeType: 'house' | |
} | |
const enrich = (v) => { | |
const {type, category, ...data} = v; | |
const f = (sc, tree, p) => { | |
if(tree.length) { | |
const t = tree.shift(); | |
const pp = p[t]; | |
return f(pp && (pp.rso || sc) || sc, tree, pp); | |
} | |
return {rso: sc, policy: p}; | |
}; | |
const {rso, policy: currentPolicy} = f(policy.rso, [category, type], policy); | |
const pk = Object.keys(currentPolicy); | |
return Object.keys(data).map(k => { | |
const v = data[k]; | |
return pk.includes(k) ? {...currentPolicy[k], value: v, key: k} : {value: v, rso, policy: 'nullMask', key: k}; | |
}); | |
}; | |
console.log(enrich(record)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment