-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskpii.lua
49 lines (45 loc) · 1.14 KB
/
maskpii.lua
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
local json = require 'json'
local function maskSSN(ssn)
if not ssn then
return nil
end
-- Replace all but the last four digits of the SSN with "XXX-XX-"
return string.gsub(ssn, "^(.-)(%d%d%d%d)$", "XXX-XX-%2")
end
local function RowToMap(row)
local map = peerdb.RowTable(row)
for col, val in pairs(map) do
local kind = peerdb.RowColumnKind(row, col)
if col == 'ssn' then
-- Apply the maskSSN function to the SSN column
map[col] = maskSSN(val)
elseif kind == 'bytes' or kind == 'bit' then
map[col] = json.bin(val)
end
end
return map
end
local RKINDMAP = {
insert = string.byte('i', 1),
update = string.byte('u', 1),
delete = string.byte('d', 1),
}
function onRecord(r)
local kind = RKINDMAP[r.kind]
if not kind then
return
end
local record = {
action = kind,
lsn = r.checkpoint,
time = r.commit_time,
source = r.source,
}
if r.old then
record.old = RowToMap(r.old)
end
if r.new then
record.new = RowToMap(r.new)
end
return json.encode(record)
end