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\Console\Commands; |
19
|
|
|
|
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Question\Question; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Command to generate a config file from a DB |
28
|
|
|
*/ |
29
|
|
|
class ConfigGenerateCommand extends Command |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Set the command shortcut to be used in configuration |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $command = 'config:generate'; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Configure the command |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
3 |
|
protected function configure(): void |
45
|
|
|
{ |
46
|
|
|
// First command : Test the DB Connexion |
47
|
3 |
|
$this->setName($this->command) |
48
|
3 |
|
->setDescription( |
49
|
3 |
|
'Generate configuration for the Anonymizer' |
50
|
3 |
|
)->setHelp( |
51
|
3 |
|
'This command will connect to a DB and extract a list of tables / fields to a yaml file' . PHP_EOL . |
52
|
3 |
|
"Usage: neuralyzer <info>{$this->command} -u app -p app -f neuralyzer.yml</info>" |
53
|
3 |
|
)->addOption( |
54
|
3 |
|
'driver', |
55
|
3 |
|
'D', |
56
|
3 |
|
InputOption::VALUE_REQUIRED, |
57
|
3 |
|
'Driver (check Doctrine documentation to have the list)', |
58
|
3 |
|
'pdo_mysql' |
59
|
3 |
|
)->addOption( |
60
|
3 |
|
'host', |
61
|
3 |
|
'H', |
62
|
3 |
|
InputOption::VALUE_REQUIRED, |
63
|
3 |
|
'Host', |
64
|
3 |
|
'127.0.0.1' |
65
|
3 |
|
)->addOption( |
66
|
3 |
|
'db', |
67
|
3 |
|
'd', |
68
|
3 |
|
InputOption::VALUE_REQUIRED, |
69
|
3 |
|
'Database Name' |
70
|
3 |
|
)->addOption( |
71
|
3 |
|
'user', |
72
|
3 |
|
'u', |
73
|
3 |
|
InputOption::VALUE_REQUIRED, |
74
|
3 |
|
'User Name', |
75
|
3 |
|
get_current_user() |
76
|
3 |
|
)->addOption( |
77
|
3 |
|
'password', |
78
|
3 |
|
'p', |
79
|
3 |
|
InputOption::VALUE_REQUIRED, |
80
|
3 |
|
"Password (or it'll be prompted)" |
81
|
3 |
|
)->addOption( |
82
|
3 |
|
'file', |
83
|
3 |
|
'f', |
84
|
3 |
|
InputOption::VALUE_REQUIRED, |
85
|
3 |
|
'File', |
86
|
3 |
|
'neuralyzer.yml' |
87
|
3 |
|
)->addOption( |
88
|
3 |
|
'protect', |
89
|
3 |
|
null, |
90
|
3 |
|
InputOption::VALUE_NONE, |
91
|
3 |
|
'Protect IDs and other fields' |
92
|
3 |
|
)->addOption( |
93
|
3 |
|
'ignore-table', |
94
|
3 |
|
null, |
95
|
3 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
96
|
3 |
|
'Table to ignore. Can be repeated' |
97
|
3 |
|
)->addOption( |
98
|
3 |
|
'ignore-field', |
99
|
3 |
|
null, |
100
|
3 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
101
|
3 |
|
'Field to ignore. Regexp in the form "table.field". Can be repeated' |
102
|
|
|
); |
103
|
3 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Execute the command |
107
|
|
|
* |
108
|
|
|
* @param InputInterface $input |
109
|
|
|
* @param OutputInterface $output |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): void |
114
|
|
|
{ |
115
|
|
|
// Throw an exception immediately if we dont have the required DB parameter |
116
|
2 |
|
if (empty($input->getOption('db'))) { |
117
|
1 |
|
throw new \InvalidArgumentException('Database name is required (--db)'); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$password = $input->getOption('password'); |
121
|
1 |
|
if (is_null($password)) { |
122
|
|
|
$question = new Question('Password: '); |
123
|
|
|
$question->setHidden(true)->setHiddenFallback(false); |
124
|
|
|
|
125
|
|
|
$password = $this->getHelper('question')->ask($input, $output, $question); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
$ignoreFields = $input->getOption('ignore-field'); |
129
|
|
|
|
130
|
|
|
// Now work on the DB |
131
|
1 |
|
$db = new \Edyan\Neuralyzer\Anonymizer\DB([ |
132
|
1 |
|
'driver' => $input->getOption('driver'), |
133
|
1 |
|
'host' => $input->getOption('host'), |
134
|
1 |
|
'dbname' => $input->getOption('db'), |
135
|
1 |
|
'user' => $input->getOption('user'), |
136
|
1 |
|
'password' => $password, |
137
|
|
|
]); |
138
|
|
|
|
139
|
1 |
|
$writer = new \Edyan\Neuralyzer\Configuration\Writer; |
140
|
1 |
|
$writer->protectCols($input->getOption('protect')); |
141
|
|
|
|
142
|
|
|
// Override the protection if fields are defined |
143
|
1 |
|
if (!empty($ignoreFields)) { |
144
|
|
|
$writer->protectCols(true); |
145
|
|
|
$writer->setProtectedCols($ignoreFields); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
$writer->setIgnoredTables($input->getOption('ignore-table')); |
149
|
1 |
|
$data = $writer->generateConfFromDB($db, new \Edyan\Neuralyzer\Guesser); |
150
|
1 |
|
$writer->save($data, $input->getOption('file')); |
151
|
|
|
|
152
|
1 |
|
$output->writeln('<comment>Configuration written to ' . $input->getOption('file') . '</comment>'); |
153
|
1 |
|
} |
154
|
|
|
} |
155
|
|
|
|