1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
7
|
|
|
* |
8
|
|
|
* PHP Version 7.2 |
9
|
|
|
* |
10
|
|
|
* @author Emmanuel Dyan |
11
|
|
|
* @author Rémi Sauvat |
12
|
|
|
* |
13
|
|
|
* @copyright 2020 Emmanuel Dyan |
14
|
|
|
* |
15
|
|
|
* @package edyan/neuralyzer |
16
|
|
|
* |
17
|
|
|
* @license GNU General Public License v2.0 |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/edyan/neuralyzer |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Edyan\Neuralyzer\Console\Commands; |
23
|
|
|
|
24
|
|
|
use Edyan\Neuralyzer\Utils\DBUtils; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
29
|
|
|
use Symfony\Component\Console\Question\Question; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Command to generate a config file from a DB |
33
|
|
|
*/ |
34
|
|
|
class ConfigGenerateCommand extends Command |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Set the command shortcut to be used in configuration |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $command = 'config:generate'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Store the DBUtils Object (autowiring) |
45
|
|
|
* |
46
|
|
|
* @var DBUtils |
47
|
|
|
*/ |
48
|
|
|
private $dbUtils; |
49
|
|
|
|
50
|
|
|
public function __construct(DBUtils $dbUtils) |
51
|
28 |
|
{ |
52
|
|
|
parent::__construct(); |
53
|
28 |
|
|
54
|
|
|
$this->dbUtils = $dbUtils; |
55
|
28 |
|
} |
56
|
28 |
|
|
57
|
|
|
protected function configure(): void |
58
|
|
|
{ |
59
|
|
|
// First command : Test the DB Connexion |
60
|
|
|
$this->setName($this->command) |
61
|
|
|
->setDescription( |
62
|
|
|
'Generate configuration for the Anonymizer' |
63
|
28 |
|
)->setHelp( |
64
|
|
|
'This command will connect to a DB and extract a list of tables / fields to a yaml file'.PHP_EOL. |
65
|
|
|
"Usage: neuralyzer <info>{$this->command} -u app -p app -f neuralyzer.yml</info>" |
66
|
28 |
|
)->addOption( |
67
|
28 |
|
'driver', |
68
|
28 |
|
'D', |
69
|
28 |
|
InputOption::VALUE_REQUIRED, |
70
|
28 |
|
'Driver (check Doctrine documentation to have the list)', |
71
|
28 |
|
'pdo_mysql' |
72
|
28 |
|
)->addOption( |
73
|
28 |
|
'host', |
74
|
28 |
|
'H', |
75
|
28 |
|
InputOption::VALUE_REQUIRED, |
76
|
28 |
|
'Host', |
77
|
28 |
|
'127.0.0.1' |
78
|
28 |
|
)->addOption( |
79
|
28 |
|
'db', |
80
|
28 |
|
'd', |
81
|
28 |
|
InputOption::VALUE_REQUIRED, |
82
|
28 |
|
'Database Name' |
83
|
28 |
|
)->addOption( |
84
|
28 |
|
'user', |
85
|
28 |
|
'u', |
86
|
28 |
|
InputOption::VALUE_REQUIRED, |
87
|
28 |
|
'User Name', |
88
|
28 |
|
get_current_user() |
89
|
28 |
|
)->addOption( |
90
|
28 |
|
'password', |
91
|
28 |
|
'p', |
92
|
28 |
|
InputOption::VALUE_REQUIRED, |
93
|
28 |
|
"Password (or it'll be prompted)" |
94
|
28 |
|
)->addOption( |
95
|
28 |
|
'file', |
96
|
28 |
|
'f', |
97
|
28 |
|
InputOption::VALUE_REQUIRED, |
98
|
28 |
|
'File', |
99
|
28 |
|
'neuralyzer.yml' |
100
|
28 |
|
)->addOption( |
101
|
28 |
|
'protect', |
102
|
28 |
|
null, |
103
|
28 |
|
InputOption::VALUE_NONE, |
104
|
28 |
|
'Protect IDs and other fields' |
105
|
28 |
|
)->addOption( |
106
|
28 |
|
'ignore-table', |
107
|
28 |
|
null, |
108
|
28 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
109
|
28 |
|
'Table to ignore. Can be repeated' |
110
|
28 |
|
)->addOption( |
111
|
28 |
|
'ignore-field', |
112
|
28 |
|
null, |
113
|
28 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
114
|
28 |
|
'Field to ignore. Regexp in the form "table.field". Can be repeated' |
115
|
28 |
|
); |
116
|
28 |
|
} |
117
|
28 |
|
|
118
|
28 |
|
/** |
119
|
28 |
|
* Execute command |
120
|
28 |
|
* |
121
|
|
|
* @throws \Doctrine\DBAL\DBALException |
122
|
28 |
|
* @throws \Edyan\Neuralyzer\Exception\NeuralyzerConfigurationException |
123
|
|
|
*/ |
124
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
125
|
|
|
{ |
126
|
|
|
// Throw an exception immediately if we don't have the required DB parameter |
127
|
|
|
if (empty($input->getOption('db'))) { |
128
|
|
|
throw new \InvalidArgumentException('Database name is required (--db)'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$password = $input->getOption('password'); |
132
|
6 |
|
if ($password === null) { |
133
|
|
|
$question = new Question('Password: '); |
134
|
|
|
$question->setHidden(true)->setHiddenFallback(false); |
135
|
6 |
|
|
136
|
1 |
|
$password = $this->getHelper('question')->ask($input, $output, $question); |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
$ignoreFields = $input->getOption('ignore-field'); |
140
|
5 |
|
|
141
|
4 |
|
$this->dbUtils->configure([ |
142
|
4 |
|
'driver' => $input->getOption('driver'), |
143
|
|
|
'host' => $input->getOption('host'), |
144
|
4 |
|
'dbname' => $input->getOption('db'), |
145
|
|
|
'user' => $input->getOption('user'), |
146
|
|
|
'password' => $password, |
147
|
5 |
|
]); |
148
|
|
|
// register custom types such as enum for mysql |
149
|
|
|
$this->dbUtils->getDBHelper()->registerCustomTypes(); |
150
|
5 |
|
|
151
|
5 |
|
$writer = new \Edyan\Neuralyzer\Configuration\Writer(); |
152
|
5 |
|
$writer->protectCols($input->getOption('protect')); |
153
|
5 |
|
|
154
|
5 |
|
// Override the protection if fields are defined |
155
|
5 |
|
if (! empty($ignoreFields)) { |
156
|
|
|
$writer->protectCols(true); |
157
|
|
|
$writer->setProtectedCols($ignoreFields); |
|
|
|
|
158
|
5 |
|
} |
159
|
5 |
|
|
160
|
|
|
$writer->setIgnoredTables($input->getOption('ignore-table')); |
|
|
|
|
161
|
|
|
$data = $writer->generateConfFromDB($this->dbUtils, new \Edyan\Neuralyzer\Guesser()); |
162
|
5 |
|
$writer->save($data, $input->getOption('file')); |
163
|
1 |
|
|
164
|
1 |
|
$output->writeln('<comment>Configuration written to '.$input->getOption('file').'</comment>'); |
165
|
|
|
|
166
|
|
|
return Command::SUCCESS; |
167
|
5 |
|
} |
168
|
|
|
} |
169
|
|
|
|