-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAbstractAnonymizer.php
More file actions
289 lines (257 loc) · 8.65 KB
/
AbstractAnonymizer.php
File metadata and controls
289 lines (257 loc) · 8.65 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
281
282
283
284
285
286
287
288
289
<?php
declare(strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Expression;
use MakinaCorpus\QueryBuilder\ExpressionFactory;
use MakinaCorpus\QueryBuilder\Query\Update;
use MakinaCorpus\QueryBuilder\Vendor;
abstract class AbstractAnonymizer
{
public const JOIN_ID = '_db_tools_id';
public const JOIN_ID_INDEX = 'target_table_db_tools_id_idx';
public const JOIN_TABLE = '_target_table';
public const TEMP_TABLE_PREFIX = '_db_tools_sample_';
final public function __construct(
protected string $tableName,
protected string $columnName,
protected DatabaseSession $databaseSession,
protected readonly Context $context,
protected readonly Options $options,
) {
$this->validateOptions();
}
/**
* Get table name.
*
* @internal
* For reporting while anonymizing.
*/
public function getTableName(): string
{
return $this->tableName;
}
/**
* Get column name.
*
* It can either return a real colum name, for column-level anonymizers,
* but it may also return an arbitrary name, for table-level anonymizers.
*
* @internal
* For reporting while anonymizing.
*/
final public function getColumnName(): string
{
return $this->columnName;
}
/**
* Get join column name.
*
* @internal
* Public for unit tests only, otherwise protected.
*/
final public function getJoinId(): string
{
return self::JOIN_ID;
}
/**
* Get join column expression.
*
* @internal
* Public for unit tests only, otherwise protected.
*/
protected function getJoinColumn(): Expression
{
return ExpressionFactory::column($this->getJoinId(), self::JOIN_TABLE);
}
/**
* Validate options given to the Anonymizer.
*
* Override this method for your needs, for example validate that
* an option `foo` is given with the correct type.
*
* This method is launched before the beginning of the anonymization process.
*
* If you override this method, you must call `parent::validateOptions()`
* at the beginning of your implementation.
*
* @throws \Exception if any option is invalid.
*/
protected function validateOptions(): void
{
if ($this->hasSampleSizeOption()) {
if ($this->options->has('sample_size')) {
$value = $this->options->getInt('sample_size');
if ($value <= 0) {
throw new \InvalidArgumentException("'sample_size' option must be a positive integer.");
}
}
}
}
/**
* Does this anonymizer has a "sample size" option.
*/
protected function hasSampleSizeOption(): bool
{
return false;
}
/**
* Default sample size, goes along the "sample size" option set to true.
*/
protected function getDefaultSampleSize(): int
{
return 500;
}
/**
* Default sample size, goes along the "sample size" option set to true.
*/
protected function getSampleSize(): int
{
return $this->options->getInt('sample_size', $this->getDefaultSampleSize());
}
/**
* Initialize your anonymizer.
*
* Override this method for your needs, for example create a temporary
* table with dummy data.
*
* This method is launch once at the beginning of the anonymization process.
*/
public function initialize(): void {}
/**
* Add statement to existing update query to anonymize a specific target.
*/
abstract public function anonymize(Update $update): void;
/**
* Clean your anonymizer
*
* Override this method for your needs, for example drop a temporary
* table created in initialiaze() methode.
*
* This method is only launch once at the end of the anonymization process.
*/
public function clean(): void {}
/**
* Count table.
*/
protected function countTable(string $table): int
{
return (int) $this
->databaseSession
->select($table)
->columnRaw('count(*)')
->executeQuery()
->fetchOne()
;
}
/**
* Create a temporary table with one or more sample columns, and populate it
* using the given values.
*
* @param string[] $columns
* Column names. Type will be text.
* @param string[][] $values
* Each value must have the same number of values than the column count.
*
* @return string
* The table name.
*/
protected function createSampleTempTable(array $columns, array $values = [], array $types = []): string
{
$types = \array_values($types);
$columns = \array_values($columns);
$columnCount = \count($columns);
$tableName = $this->generateTempTableName();
$transaction = $this
->databaseSession
->getSchemaManager()
->modify()
->createTable($tableName)
;
foreach ($columns as $index => $name) {
$transaction->column($name, $types[$index] ?? 'text', false);
}
$transaction->endTable()->commit();
$insert = $this
->databaseSession
->insert($tableName)
->columns($columns)
;
// SQL Server supports a maximum of 2100 parameters per query.
// This limit probably also exists with other RDBMSs, to avoid
// errors, we insert rows each 2000 parameters.
$parametersCount = 0;
foreach ($values as $key => $value) {
// Allow single raw value when there is only one column.
$value = (array) $value;
$parametersCount += \count($value);
if ($parametersCount >= 2000) {
$insert->executeStatement();
$insert = $this
->databaseSession
->insert($tableName)
->columns($columns)
;
$parametersCount = 0;
}
if ($columnCount !== \count($value)) {
throw new \InvalidArgumentException(\sprintf(
"Row %s in sample list column count (%d) mismatch with table column count (%d)",
$key,
\count($values),
$columnCount
));
}
$insert->values($value);
}
$insert->executeStatement();
return $tableName;
}
protected function generateTempTableName(): string
{
return \uniqid(self::TEMP_TABLE_PREFIX);
}
/**
* Return a random float between 0 and 1 expression.
*
* makina-corpus/query-builder already support this, but due to an odd SQL
* Server non standard behaviour, we reimplement it here: SQL Server RAND()
* return value will always be the same no matter how many time you call it
* inside a single SQL query. This means that when you update many rows
* using a RAND() based value, all rows will have the same value.
*
* We are going to get arround by injected a random value as seed for the
* RAND() function, because SQL Server allows this. Using NEWID() which
* generates an GUID might be slow, but we'll that at usage later.
*
* This function takes care of this, and will return an expression that
* works with all supported RDBMS.
*/
protected function getRandomExpression(): Expression
{
if ($this->databaseSession->vendorIs(Vendor::SQLSERVER)) {
return ExpressionFactory::raw('rand(abs(checksum(newid())))');
}
return ExpressionFactory::random();
}
/**
* For the same reason as getRandomExpression().
*/
protected function getRandomIntExpression(int $max, int $min = 0): Expression
{
if ($this->databaseSession->vendorIs(Vendor::SQLSERVER)) {
return ExpressionFactory::raw(
'FLOOR(? * (? - ? + 1) + ?)',
[$this->getRandomExpression(), ExpressionFactory::cast($max, 'int'), $min, $min]
);
}
return ExpressionFactory::randomInt($max, $min);
}
protected function getSetIfNotNullExpression(mixed $valueExpression, mixed $columnExpression = null): Expression
{
if (null === $columnExpression) {
$columnExpression = ExpressionFactory::column($this->columnName, $this->tableName);
}
return ExpressionFactory::ifThen(ExpressionFactory::where()->isNotNull($columnExpression), $valueExpression);
}
}