1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
4
|
|
|
* |
5
|
|
|
* PHP Version 7.0 |
6
|
|
|
* |
7
|
|
|
* @author Emmanuel Dyan |
8
|
|
|
* @author Rémi Sauvat |
9
|
|
|
* @copyright 2017 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 Inet\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
|
|
|
use DBTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the command shortcut to be used in configuration |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $command = 'config:generate'; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configure the command |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
13 |
|
protected function configure() |
47
|
|
|
{ |
48
|
|
|
// First command : Test the DB Connexion |
49
|
13 |
|
$this->setName($this->command) |
50
|
13 |
|
->setDescription( |
51
|
13 |
|
'Generate configuration for the Anonymizer' |
52
|
13 |
|
)->setHelp( |
53
|
13 |
|
'This command will connect to a DB and extract a list of tables / fields to a yaml file' . PHP_EOL . |
54
|
13 |
|
"Usage: ./bin/anon <info>{$this->command} -u app -p app -f anon.yml</info>" |
55
|
13 |
|
)->addOption( |
56
|
13 |
|
'host', |
57
|
13 |
|
null, |
58
|
13 |
|
InputOption::VALUE_REQUIRED, |
59
|
13 |
|
'Host', |
60
|
13 |
|
'127.0.0.1' |
61
|
13 |
|
)->addOption( |
62
|
13 |
|
'db', |
63
|
13 |
|
'd', |
64
|
13 |
|
InputOption::VALUE_REQUIRED, |
65
|
13 |
|
'Database Name' |
66
|
13 |
|
)->addOption( |
67
|
13 |
|
'user', |
68
|
13 |
|
'u', |
69
|
13 |
|
InputOption::VALUE_REQUIRED, |
70
|
13 |
|
'User Name', |
71
|
|
|
get_current_user() |
72
|
13 |
|
)->addOption( |
73
|
13 |
|
'password', |
74
|
13 |
|
'p', |
75
|
13 |
|
InputOption::VALUE_REQUIRED, |
76
|
13 |
|
"Password (or it'll be prompted)" |
77
|
13 |
|
)->addOption( |
78
|
13 |
|
'file', |
79
|
13 |
|
'f', |
80
|
13 |
|
InputOption::VALUE_REQUIRED, |
81
|
13 |
|
'File', |
82
|
13 |
|
'anon.yml' |
83
|
13 |
|
)->addOption( |
84
|
13 |
|
'protect', |
85
|
13 |
|
null, |
86
|
13 |
|
InputOption::VALUE_NONE, |
87
|
13 |
|
'Protect IDs and other fields' |
88
|
13 |
|
)->addOption( |
89
|
13 |
|
'ignore-table', |
90
|
13 |
|
null, |
91
|
13 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
92
|
13 |
|
'Table to ignore. Can be repeated' |
93
|
13 |
|
)->addOption( |
94
|
13 |
|
'ignore-field', |
95
|
13 |
|
null, |
96
|
13 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
97
|
13 |
|
'Field to ignore. Regexp in the form "table.field". Can be repeated' |
98
|
|
|
); |
99
|
13 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Execute the command |
103
|
|
|
* |
104
|
|
|
* @param InputInterface $input |
105
|
|
|
* @param OutputInterface $output |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
110
|
|
|
{ |
111
|
6 |
|
$password = $input->getOption('password'); |
112
|
6 |
|
if (is_null($password)) { |
113
|
4 |
|
$question = new Question('Password: '); |
114
|
4 |
|
$question->setHidden(true)->setHiddenFallback(false); |
115
|
|
|
|
116
|
4 |
|
$password = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
$this->connectToDB( |
120
|
6 |
|
$input->getOption('host'), |
121
|
6 |
|
$input->getOption('db'), |
122
|
6 |
|
$input->getOption('user'), |
123
|
|
|
$password |
124
|
|
|
); |
125
|
|
|
|
126
|
4 |
|
$ignoreFields = $input->getOption('ignore-field'); |
127
|
|
|
|
128
|
4 |
|
$writer = new \Inet\Neuralyzer\Configuration\Writer; |
129
|
4 |
|
$writer->protectCols($input->getOption('protect')); |
130
|
|
|
|
131
|
|
|
// Override the protection if fields are defined |
132
|
4 |
|
if (!empty($ignoreFields)) { |
133
|
1 |
|
$writer->protectCols(true); |
134
|
1 |
|
$writer->setProtectedCols($ignoreFields); |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
$writer->setIgnoredTables($input->getOption('ignore-table')); |
138
|
4 |
|
$data = $writer->generateConfFromDB($this->pdo, new \Inet\Neuralyzer\Guesser); |
139
|
3 |
|
$writer->save($data, $input->getOption('file')); |
140
|
|
|
|
141
|
3 |
|
$output->writeln('<comment>Configuration written to ' . $input->getOption('file') . '</comment>'); |
142
|
3 |
|
} |
143
|
|
|
} |
144
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: