1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
4
|
|
|
* |
5
|
|
|
* PHP Version 7.1 |
6
|
|
|
* |
7
|
|
|
* @author Emmanuel Dyan |
8
|
|
|
* @author Rémi Sauvat |
9
|
|
|
* @copyright 2018 Emmanuel Dyan |
10
|
|
|
* |
11
|
|
|
* @package edyan/neuralyzer |
12
|
|
|
* |
13
|
|
|
* @license GNU General Public License v2.0 |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/edyan/neuralyzer |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Edyan\Neuralyzer\Anonymizer; |
19
|
|
|
|
20
|
|
|
use Edyan\Neuralyzer\Configuration\Reader; |
21
|
|
|
use Edyan\Neuralyzer\Exception\NeuralizerConfigurationException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract Anonymizer, that can be implemented as DB Anonymizer for example |
25
|
|
|
* Its goal is only to anonymize any data, from a simple array |
26
|
|
|
* not to write or read it from anywhere |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractAnonymizer |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Truncate table |
33
|
|
|
*/ |
34
|
|
|
const TRUNCATE_TABLE = 1; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Update data into table |
38
|
|
|
*/ |
39
|
|
|
const UPDATE_TABLE = 2; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Insert data into table |
43
|
|
|
*/ |
44
|
|
|
const INSERT_TABLE = 4; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the batch size for updates |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $batchSize = 1000; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Contains the configuration object |
55
|
|
|
* @var Reader |
56
|
|
|
*/ |
57
|
|
|
protected $configuration; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Configuration of entities |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $configEntites = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* List of used fakers |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $fakers = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Current table (entity) to process |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $entity; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Current table (entity) Columns |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $entityCols; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Limit the number of updates or create |
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
protected $limit = 0; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Pretend we do the update, but do nothing |
91
|
|
|
* @var bool |
92
|
|
|
*/ |
93
|
|
|
protected $pretend = true; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the generated SQL |
97
|
|
|
* @var bool |
98
|
|
|
*/ |
99
|
|
|
protected $returnRes = false; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Process the entity according to the anonymizer type |
103
|
|
|
* @param string $entity Entity's name |
104
|
|
|
* @param callable|null $callback Callback function with current row num as parameter |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
abstract public function processEntity( |
108
|
|
|
string $entity, |
109
|
|
|
callable $callback = null |
110
|
|
|
): array; |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the configuration |
115
|
|
|
* @param Reader $configuration |
116
|
|
|
*/ |
117
|
31 |
|
public function setConfiguration(Reader $configuration): void |
118
|
|
|
{ |
119
|
31 |
|
$this->configuration = $configuration; |
120
|
31 |
|
$this->configEntites = $configuration->getConfigValues()['entities']; |
121
|
31 |
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Limit of fake generated records for updates and creates |
126
|
|
|
* @param int $limit |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
23 |
|
public function setLimit(int $limit) |
130
|
|
|
{ |
131
|
23 |
|
$this->limit = $limit; |
132
|
23 |
|
if ($this->limit < $this->batchSize) { |
133
|
22 |
|
$this->batchSize = $this->limit; |
134
|
|
|
} |
135
|
|
|
|
136
|
23 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Activate or deactivate the pretending mode (dry run) |
142
|
|
|
* @param bool $pretend |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
28 |
|
public function setPretend(bool $pretend) |
146
|
|
|
{ |
147
|
28 |
|
$this->pretend = $pretend; |
148
|
|
|
|
149
|
28 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return or not a result (like an SQL Query that has |
155
|
|
|
* been generated with fake data) |
156
|
|
|
* @param bool $returnRes |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
27 |
|
public function setReturnRes(bool $returnRes) |
160
|
|
|
{ |
161
|
27 |
|
$this->returnRes = $returnRes; |
162
|
|
|
|
163
|
27 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Evaluate, from the configuration if I have to update or Truncate the table |
169
|
|
|
* |
170
|
|
|
* @return int |
171
|
|
|
* @throws NeuralizerConfigurationException |
172
|
|
|
*/ |
173
|
26 |
|
protected function whatToDoWithEntity(): int |
174
|
|
|
{ |
175
|
26 |
|
$this->checkEntityIsInConfig(); |
176
|
|
|
|
177
|
24 |
|
$entityConfig = $this->configEntites[$this->entity]; |
178
|
|
|
|
179
|
24 |
|
$actions = 0; |
180
|
24 |
|
if (array_key_exists('delete', $entityConfig) && $entityConfig['delete'] === true) { |
181
|
5 |
|
$actions |= self::TRUNCATE_TABLE; |
182
|
|
|
} |
183
|
|
|
|
184
|
24 |
|
if (array_key_exists('cols', $entityConfig)) { |
185
|
24 |
|
switch ($entityConfig['action']) { |
186
|
24 |
|
case 'update': |
187
|
18 |
|
$actions |= self::UPDATE_TABLE; |
188
|
18 |
|
break; |
189
|
6 |
|
case 'insert': |
190
|
6 |
|
$actions |= self::INSERT_TABLE; |
191
|
6 |
|
break; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
24 |
|
return $actions; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the 'delete_where' parameter for an entity in config (or empty) |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
5 |
|
public function getWhereConditionInConfig(): string |
205
|
|
|
{ |
206
|
5 |
|
$this->checkEntityIsInConfig(); |
207
|
|
|
|
208
|
5 |
|
if (!array_key_exists('delete_where', $this->configEntites[$this->entity])) { |
209
|
2 |
|
return ''; |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
return $this->configEntites[$this->entity]['delete_where']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Make sure that entity is defined in the configuration |
217
|
|
|
* |
218
|
|
|
* @throws NeuralizerConfigurationException |
219
|
|
|
*/ |
220
|
26 |
|
protected function checkEntityIsInConfig(): void |
221
|
|
|
{ |
222
|
26 |
|
if (empty($this->configEntites)) { |
223
|
1 |
|
throw new NeuralizerConfigurationException( |
224
|
1 |
|
'No entities found. Have you loaded a configuration file ?' |
225
|
|
|
); |
226
|
|
|
} |
227
|
25 |
|
if (!array_key_exists($this->entity, $this->configEntites)) { |
228
|
1 |
|
throw new NeuralizerConfigurationException( |
229
|
1 |
|
"No configuration for that entity ({$this->entity})" |
230
|
|
|
); |
231
|
|
|
} |
232
|
24 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Verify a column is defined in the real entityCols |
236
|
|
|
* |
237
|
|
|
* @throws NeuralizerConfigurationException |
238
|
|
|
* @param string $colName [description] |
239
|
|
|
*/ |
240
|
22 |
|
protected function checkColIsInEntity(string $colName): void |
241
|
|
|
{ |
242
|
22 |
|
if (!array_key_exists($colName, $this->entityCols)) { |
243
|
1 |
|
throw new NeuralizerConfigurationException("Col $colName does not exist"); |
244
|
|
|
} |
245
|
21 |
|
} |
246
|
|
|
} |
247
|
|
|
|