-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathTable.java
More file actions
280 lines (256 loc) · 10.7 KB
/
Copy pathTable.java
File metadata and controls
280 lines (256 loc) · 10.7 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.strider.datadefender.anonymizer.functions;
import com.strider.datadefender.functions.NamedParameter;
import com.strider.datadefender.requirement.registry.DatabaseAwareRequirementFunction;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.log4j.Log4j2;
/**
* Helper anonymization using table data (shuffle records, etc...).
*
* @author Armenak Grigoryan
*/
@Log4j2
public class Table extends DatabaseAwareRequirementFunction {
private static final Map<String, List<String>> stringLists = new HashMap<>();
private static final Map<String, Iterator<String>> stringIters = new HashMap<>();
private static final Map<String, Map<String, String>> predictableShuffle = new HashMap<>();
private static final Map<String, Map<String, String>> predictableLipsumShuffle = new HashMap<>();
private static final Lipsum lipsum = new Lipsum();
/**
* Returns the next shuffled item from the named collection.
*
* @param name
* @return
*/
private String getNextShuffledItemFor(final String name) {
if (stringIters.containsKey(name)) {
final Iterator<String> iter = stringIters.get(name);
if (iter.hasNext()) {
return iter.next();
}
}
final List<String> list = stringLists.get(name);
Collections.shuffle(list);
final Iterator<String> iter = list.iterator();
stringIters.put(name, iter);
return iter.next();
}
/**
* Sets up a map, mapping a list of values to a list of shuffled values.
*
* If the value is not mapped, the function guarantees returning the same
* randomized value for a given column value - however it does not guarantee
* that more than one column value do not have the same randomized value.
*
* @param params
* @return
*/
private String getPredictableShuffledValueFor(final String name, final String value) {
if (!predictableShuffle.containsKey(name)) {
final List<String> list = stringLists.get(name);
final List<String> shuffled = new ArrayList<>(list);
Collections.shuffle(shuffled);
final Map<String, String> smap = new HashMap<>();
final Iterator<String> lit = list.iterator();
final Iterator<String> sit = shuffled.iterator();
while (lit.hasNext()) {
smap.put(lit.next(), sit.next());
}
predictableShuffle.put(name, smap);
}
final Map<String, String> map = predictableShuffle.get(name);
if (!map.containsKey(value)) {
final Collection<String> vals = map.values();
final int index = (int) Math.abs((long) value.hashCode()) % vals.size();
return IterableUtils.get(vals, index);
}
return map.get(value);
}
/**
* Sets up a map, mapping a list of values to a list of anonymized shuffled
* values.
*
* If the value is not mapped, the function guarantees returning the same
* randomized value for a given column value - however it does not guarantee
* that more than one column value do not have the same randomized value.
*
* @param params
* @return
*/
private String getPredictableShuffledLipsumSimilarValueFor(final String name, final String value) throws IOException {
if (!predictableShuffle.containsKey(name)) {
final List<String> list = stringLists.get(name);
final List<String> shuffled = new ArrayList<>();
for (String v: list) {
shuffled.add(lipsum.similar(v));
}
Collections.shuffle(shuffled);
final Map<String, String> smap = new HashMap<>();
final Iterator<String> lit = list.iterator();
final Iterator<String> sit = shuffled.iterator();
while (lit.hasNext()) {
smap.put(lit.next(), sit.next());
}
predictableShuffle.put(name, smap);
}
final Map<String, String> map = predictableShuffle.get(name);
if (!map.containsKey(value)) {
final Collection<String> vals = map.values();
final int index = (int) Math.abs((long) value.hashCode()) % vals.size();
return IterableUtils.get(vals, index);
}
return map.get(value);
}
/**
* Creates a string list of values by querying the database.
*
* @param keyName
* @param query
* @throws java.sql.SQLException
*/
protected void generateStringListFromDb(final String keyName, final String query) throws SQLException {
if (!stringLists.containsKey(keyName + query.hashCode())) {
log.info("*** reading from database column: " + keyName);
final List<String> values = new ArrayList<>();
log.debug("Query:" + query);
Connection con = dbFactory.getConnection();
try (Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
values.add(rs.getString(1));
}
}
if (values.isEmpty()) {
// TODO: throw a meaningful exception here
log.error("!!! Database column " + keyName + " did not return any values");
}
stringLists.put(keyName + query.hashCode(), values);
}
}
/**
* Generates a randomized collection of column values and selects and
* returns one.
*
* The function selects distinct, non-null and non-empty values to choose
* from, then shuffles the collection of strings once before returning items
* from it. Once all strings have been returned, the collection is
* re-shuffled and re-used.
*
* @param table the table name
* @param column the column name
* @param excludeEmpty set to true to exclude empty values
* @return the next item
* @throws SQLException
*/
public String randomColumnValue(
@NamedParameter("table") String table,
@NamedParameter("column") String column,
@NamedParameter("excludeEmpty") boolean excludeEmpty
) throws SQLException {
final String keyName = table + "." + column;
final StringBuilder sb = new StringBuilder();
sb.append(String.format("SELECT DISTINCT %s FROM %s", column, table));
if (excludeEmpty) {
if (StringUtils.equalsIgnoreCase("oracle", dbFactory.getVendorName())) {
sb.append(String.format(" WHERE %s IS NOT NULL", column, column));
} else {
sb.append(String.format(" WHERE %s IS NOT NULL AND %s <> ''", column, column));
}
}
generateStringListFromDb(keyName, sb.toString());
return getNextShuffledItemFor(keyName + sb.toString().hashCode());
}
/**
* Returns a 'predictable' shuffled value based on the passed value which is
* guaranteed to return the same random value for the same column value.
*
* Note that more than one column value may result in having the same
* shuffled value.
*
* @param table
* @param column
* @param value
* @param excludeEmpty
* @return
* @throws SQLException
*/
public String mappedColumnShuffle(
@NamedParameter("table") String table,
@NamedParameter("column") String column,
@NamedParameter("value") String value,
@NamedParameter("excludeEmpty") boolean excludeEmpty
) throws SQLException {
final String keyName = table + "." + column;
final StringBuilder sb = new StringBuilder();
sb.append(String.format("SELECT DISTINCT %s FROM %s", column, table));
if (excludeEmpty) {
if (StringUtils.equalsIgnoreCase("oracle", dbFactory.getVendorName())) {
sb.append(String.format(" WHERE %s IS NOT NULL", column, column));
} else {
sb.append(String.format(" WHERE %s IS NOT NULL AND %s <> ''", column, column));
}
}
generateStringListFromDb(keyName, sb.toString());
return getPredictableShuffledValueFor(keyName + sb.toString().hashCode(), value);
}
/**
* Returns a 'predictable', anonymzied shuffled value based on the passed
* value which is guaranteed to return the same random, anonymized value for
* the same column value.
*
* Note that columns may have repeated values even if they are different,
* the anonymization is not guaranteed to be unique.
*
* @param table
* @param column
* @param value
* @param excludeEmpty
* @return
* @throws SQLException, IOException
*/
public String mappedLipsumSimilarColumnShuffle(
@NamedParameter("table") String table,
@NamedParameter("column") String column,
@NamedParameter("value") String value,
@NamedParameter("excludeEmpty") boolean excludeEmpty
) throws SQLException, IOException {
final String keyName = table + "." + column;
final StringBuilder sb = new StringBuilder();
sb.append(String.format("SELECT DISTINCT %s FROM %s", column, table));
if (excludeEmpty) {
if (StringUtils.equalsIgnoreCase("oracle", dbFactory.getVendorName())) {
sb.append(String.format(" WHERE %s IS NOT NULL", column, column));
} else {
sb.append(String.format(" WHERE %s IS NOT NULL AND %s <> ''", column, column));
}
}
generateStringListFromDb(keyName, sb.toString());
return getPredictableShuffledLipsumSimilarValueFor(keyName + sb.toString().hashCode(), value);
}
}