forked from ambta/DoctrineEncryptBundle
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDoctrineEncryptDatabaseCommand.php
More file actions
109 lines (91 loc) · 4.45 KB
/
DoctrineEncryptDatabaseCommand.php
File metadata and controls
109 lines (91 loc) · 4.45 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
<?php
namespace Ambta\DoctrineEncryptBundle\Command;
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* Batch encryption for the database
*
* @author Marcel van Nuil <marcel@ambta.com>
* @author Michael Feinbier <michael@feinbier.net>
*/
class DoctrineEncryptDatabaseCommand extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('doctrine:encrypt:database')
->setDescription('Encrypt whole database on tables which are not encrypted yet')
->addArgument('encryptor', InputArgument::OPTIONAL, 'The encryptor you want to decrypt the database with')
->addArgument('batchSize', InputArgument::OPTIONAL, 'The update/flush batch size', 20);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// Get entity manager, question helper, subscriber service and annotation reader
$question = $this->getHelper('question');
$batchSize = $input->getArgument('batchSize');
// Get list of supported encryptors
$supportedExtensions = DoctrineEncryptExtension::SupportedEncryptorClasses;
// If encryptor has been set use that encryptor else use default
if ($input->getArgument('encryptor')) {
if (isset($supportedExtensions[$input->getArgument('encryptor')])) {
$reflection = new \ReflectionClass($supportedExtensions[$input->getArgument('encryptor')]);
$encryptor = $reflection->newInstance();
$this->subscriber->setEncryptor($encryptor);
} else {
if (class_exists($input->getArgument('encryptor'))) {
$this->subscriber->setEncryptor($input->getArgument('encryptor'));
} else {
$output->writeln('Given encryptor does not exists');
return $output->writeln('Supported encryptors: ' . implode(', ', array_keys($supportedExtensions)));
}
}
}
// Get entity manager metadata
$metaDataArray = $this->getEncryptionableEntityMetaData();
$confirmationQuestion = new ConfirmationQuestion(
'<question>' . count($metaDataArray) . ' entities found which are containing properties with the encryption tag.' . PHP_EOL . '' .
'Which are going to be encrypted with [' . get_class($this->subscriber->getEncryptor()) . ']. ' . PHP_EOL . ''.
'Wrong settings can mess up your data and it will be unrecoverable. ' . PHP_EOL . '' .
'I advise you to make <bg=yellow;options=bold>a backup</bg=yellow;options=bold>. ' . PHP_EOL . '' .
'Continue with this action? (y/yes)</question>', false
);
if (!$question->ask($input, $output, $confirmationQuestion)) {
return AbstractCommand::FAILURE;
}
// Start decrypting database
$output->writeln('' . PHP_EOL . 'Encrypting all fields can take up to several minutes depending on the database size.');
// Loop through entity manager meta data
foreach ($metaDataArray as $metaData) {
$i = 0;
$iterator = $this->getEntityIterator($metaData->name);
$totalCount = $this->getTableCount($metaData->name);
$output->writeln(sprintf('Processing <comment>%s</comment>', $metaData->name));
$progressBar = new ProgressBar($output, $totalCount);
foreach ($iterator as $row) {
$this->subscriber->processFields($row[0]);
if (($i % $batchSize) === 0) {
$this->entityManager->flush();
$this->entityManager->clear();
$progressBar->advance($batchSize);
}
$i++;
}
$progressBar->finish();
$output->writeln('');
$this->entityManager->flush();
}
// Say it is finished
$output->writeln('Encryption finished. Values encrypted: <info>' . $this->subscriber->encryptCounter . ' values</info>.' . PHP_EOL . 'All values are now encrypted.');
return AbstractCommand::SUCCESS;
}
}