forked from webnet-fr/database-anonymizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vladyslav Riabchenko
committed
Jan 27, 2019
0 parents
commit 705112d
Showing
45 changed files
with
1,664 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
services: | ||
- mysql | ||
|
||
env: | ||
- db_url=mysql://root@127.0.0.1/anonymizer | ||
|
||
before_install: | ||
- composer self-update | ||
- composer validate | ||
|
||
install: | ||
- composer install --prefer-dist --no-interaction | ||
|
||
script: | ||
- vendor/bin/phpunit --coverage-clover=coverage.xml | ||
|
||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Webnet team | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Database anonymizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Symfony\Component\Console\Application; | ||
use WebnetFr\DatabaseAnonymizer\Command\AnonymizeCommand; | ||
|
||
set_time_limit(0); | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
if (!class_exists(Application::class)) { | ||
throw new \RuntimeException('You need to add "symfony/console" as a Composer dependency.'); | ||
} | ||
|
||
(new Application('Database anonymizer', '0.0.1')) | ||
->add(new AnonymizeCommand()) | ||
->getApplication() | ||
->setDefaultCommand('webnet-fr:anonymizer:anonymize', true) | ||
->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "webnet-fr/database-anonymizer", | ||
"type": "library", | ||
"description": "Database anonymizer.", | ||
"keywords": [ | ||
"php", | ||
"database", | ||
"anonymizer" | ||
], | ||
"homepage": "https://webnet.fr", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.1.3", | ||
"doctrine/dbal": "^2.6", | ||
"fzaninotto/faker": "^1.8" | ||
}, | ||
"require-dev": { | ||
"symfony/console": "^2.0.5|^3.0|^4.0", | ||
"phpunit/phpunit": "^7.4", | ||
"symfony/config": "^4.2", | ||
"symfony/yaml": "^4.2" | ||
}, | ||
"suggest": { | ||
"symfony/console": "To enable console commands.", | ||
"symfony/config": "To configure anonymizer.", | ||
"symfony/yaml": "To configure anonymizer using yaml files." | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"WebnetFr\\DatabaseAnonymizer\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"WebnetFr\\DatabaseAnonymizer\\Tests\\": "tests" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
bootstrap="./vendor/autoload.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Anonymizer Test Suite"> | ||
<directory suffix="Test.php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace WebnetFr\DatabaseAnonymizer; | ||
|
||
use Doctrine\DBAL\Driver\Connection; | ||
use WebnetFr\DatabaseAnonymizer\Exception\InvalidAnonymousValueException; | ||
|
||
/** | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
class Anonymizer | ||
{ | ||
/** | ||
* @param Connection $connection | ||
* @param TargetTable[] $targets | ||
*/ | ||
public function anonymize(Connection $connection, array $targets) | ||
{ | ||
foreach ($targets as $targetTable) { | ||
$allFieldNames = $targetTable->getAllFieldNames(); | ||
$pk = $targetTable->getPrimaryKey(); | ||
|
||
$fetchRowsSQL = sprintf('SELECT %s FROM `%s`', join(',', $allFieldNames), $targetTable->getName()); | ||
$fetchRowsStmt = $connection->prepare($fetchRowsSQL); | ||
$fetchRowsStmt->execute(); | ||
|
||
$updateFields = []; | ||
foreach ($targetTable->getTargetFields() as $targetField) { | ||
// <field_name>=:<field_name> | ||
$updateFields[] = '`'.$targetField->getName().'`=:'.$targetField->getName(); | ||
} | ||
|
||
// UPDATE <table name> SET [<field_name=:field_name>] WHERE <pk>=:<pk> | ||
$updateSQL = sprintf('UPDATE `%s` SET %s WHERE `%s`=:%s', $targetTable->getName(), join(',', $updateFields), $pk, $pk); | ||
$updateStmt = $connection->prepare($updateSQL); | ||
|
||
while ($row = $fetchRowsStmt->fetch()) { | ||
// set primary key for row to update | ||
$updateStmt->bindValue($pk, $row[$pk]); | ||
|
||
foreach ($targetTable->getTargetFields() as $targetField) { | ||
$anonValue = $targetField->generate(); | ||
|
||
if (!is_null($anonValue) && !is_string($anonValue)) { | ||
throw new InvalidAnonymousValueException('Generated value must be null or string'); | ||
} | ||
|
||
// set anonymized value | ||
$updateStmt->bindValue($targetField->getName(), $anonValue); | ||
} | ||
|
||
// update row | ||
$updateStmt->execute(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace WebnetFr\DatabaseAnonymizer\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use WebnetFr\DatabaseAnonymizer\Anonymizer; | ||
use WebnetFr\DatabaseAnonymizer\Config\TargetFactory; | ||
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\ChainGeneratorFactory; | ||
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\ConstantGeneratorFactory; | ||
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\DatetimeGeneratorFactory; | ||
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\FakerGeneratorFactory; | ||
|
||
/** | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
class AnonymizeCommand extends Command | ||
{ | ||
use AnonymizeCommandTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function configure() | ||
{ | ||
parent::configure(); | ||
|
||
$this->setName('webnet-fr:anonymizer:anonymize') | ||
->setDescription('Anoymize database.') | ||
->setHelp('Anoymize database according to GDPR (General Data Protection Regulation).') | ||
->addArgument('config', InputArgument::REQUIRED, 'Configuration file.') | ||
->addArgument('db_url', InputArgument::REQUIRED, 'Database connection string.'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$questionHelper = $this->getHelper('question'); | ||
$question = new ConfirmationQuestion('Are you sure you want to anonymize your database?', false); | ||
|
||
if (!$questionHelper->ask($input, $output, $question)) { | ||
return; | ||
} | ||
|
||
$configFilePath = realpath($input->getArgument('config')); | ||
$config = $this->getConfigFromFile($configFilePath); | ||
|
||
$generatorFactory = new ChainGeneratorFactory(); | ||
$generatorFactory->addFactory(new ConstantGeneratorFactory()) | ||
->addFactory(new DatetimeGeneratorFactory()) | ||
->addFactory(new FakerGeneratorFactory()); | ||
|
||
$targetFactory = new TargetFactory($generatorFactory); | ||
$targetTables = $targetFactory->createTargets($config); | ||
|
||
$connection = $this->getConnection($input->getArgument('db_url')); | ||
|
||
$anonymizer = new Anonymizer(); | ||
$anonymizer->anonymize($connection, $targetTables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace WebnetFr\DatabaseAnonymizer\Command; | ||
|
||
use Doctrine\DBAL\Configuration as DoctrineDBALConfiguration; | ||
use Doctrine\DBAL\DriverManager; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Config\Loader\DelegatingLoader; | ||
use Symfony\Component\Config\Loader\LoaderResolver; | ||
use WebnetFr\DatabaseAnonymizer\Config\Configuration; | ||
use WebnetFr\DatabaseAnonymizer\Config\YamlAnonymizerLoader; | ||
|
||
/** | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
trait AnonymizeCommandTrait | ||
{ | ||
/** | ||
* @param string $dbUrl | ||
* | ||
* @return \Doctrine\DBAL\Connection | ||
*/ | ||
protected function getConnection($dbUrl) | ||
{ | ||
$params = ['url' => $dbUrl]; | ||
$config = new DoctrineDBALConfiguration(); | ||
|
||
return DriverManager::getConnection($params, $config); | ||
} | ||
|
||
/** | ||
* @param string $configFilePath | ||
* | ||
* @return array | ||
*/ | ||
protected function getConfigFromFile(string $configFilePath) | ||
{ | ||
$fileLocator = new FileLocator(); | ||
$loaderResolver = new LoaderResolver([new YamlAnonymizerLoader($fileLocator)]); | ||
$delegatingLoader = new DelegatingLoader($loaderResolver); | ||
$rawConfig = $delegatingLoader->load($configFilePath); | ||
|
||
$configuration = new Configuration(); | ||
$processor = new Processor(); | ||
|
||
return $processor->processConfiguration($configuration, $rawConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace WebnetFr\DatabaseAnonymizer\Config; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
use ConfigurationTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder('webnet_fr_database_anonymizer'); | ||
|
||
$this->configureAnonymizer($treeBuilder->getRootNode()); | ||
|
||
return $treeBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace WebnetFr\DatabaseAnonymizer\Config; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
|
||
/** | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
trait ConfigurationTrait | ||
{ | ||
/** | ||
* @param ArrayNodeDefinition $node | ||
* | ||
* @author Vlad Riabchenko <vriabchenko@webnet.fr> | ||
*/ | ||
public function configureAnonymizer(ArrayNodeDefinition $node) | ||
{ | ||
$node | ||
->children() | ||
->arrayNode('defaults') | ||
->scalarPrototype()->end() | ||
->end() | ||
->arrayNode('tables') | ||
->arrayPrototype() | ||
->children() | ||
->scalarNode('primary_key')->end() | ||
->arrayNode('fields') | ||
->arrayPrototype() | ||
->scalarPrototype()->end() | ||
->end() | ||
->end() // fields | ||
->end() | ||
->end() | ||
->end() // tables | ||
->end() | ||
->beforeNormalization() | ||
->ifTrue(static function ($v) { | ||
return is_array($v) && array_key_exists('defaults', $v) && is_array($v['defaults']); | ||
}) | ||
->then(static function ($c) { | ||
// pass default values to all concerned fields. | ||
foreach ($c['tables'] as &$tableConfig) { | ||
foreach ($tableConfig['fields'] as &$fieldConfig) { | ||
foreach ($c['defaults'] as $defaultKey => $defaultValue) { | ||
if (!array_key_exists($defaultKey, $fieldConfig)) { | ||
$fieldConfig[$defaultKey] = $defaultValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $c; | ||
}) | ||
->end() | ||
; | ||
} | ||
} |
Oops, something went wrong.