ismail1432 / database-anonymizer-bundle
数据库匿名化包。
Requires
- php: ^7.1.3
- symfony/console: ^2.0.5|^3.0|^4.0
- symfony/doctrine-bridge: ^4.2
- symfony/framework-bundle: ^4.0
- webnet-fr/database-anonymizer: ^0.0.3
Requires (Dev)
- doctrine/annotations: ^1.6
- doctrine/doctrine-bundle: ^1.11
- doctrine/orm: ^2.6
- fzaninotto/faker: ^1.8
- matthiasnoback/symfony-dependency-injection-test: ^3.1
- phpunit/phpunit: ^7.4
- symfony/config: ^4.2
- symfony/yaml: ^4.2
Suggests
- doctrine/annotations: To enable annotations.
- symfony/config: To configure anonymizer.
- symfony/console: To enable console commands.
- symfony/yaml: To configure anonymizer using yaml files.
This package is auto-updated.
Last update: 2024-08-29 05:31:32 UTC
README
为什么?
《通用数据保护条例》(GDPR)在信息存储和处理领域施加了严格的规则。除非有强烈的需求,否则不得处理用户的个人数据。如果你想将生产数据库导出以在开发过程中使用,你不能再在导出的数据库中存储或使用个人数据。在将生产数据库导入你的开发环境之前,你必须删除或匿名化个人信息。
如何?
此包基于我们的数据库匿名化库,该库又依赖于Faker。安装后,命令 webnet-fr:anonymizer:anonymize
将在您的应用程序中可用。
使用指定的连接和配置文件对数据库进行匿名化
php bin/console webnet-fr:anonymizer:anonymize --connection=<name of connection> --config=<config file path>
使用指定的连接和默认包配置对数据库进行匿名化
php bin/console webnet-fr:anonymizer:anonymize --connection=<name of connection>
使用默认连接和指定的配置文件对数据库进行匿名化
php bin/console webnet-fr:anonymizer:anonymize --config=<config file path>
使用默认连接和默认包配置对数据库进行匿名化
php bin/console webnet-fr:anonymizer:anonymize
如何安装?
需要该包
composer require webnet-fr/database-anonymizer-bundle
激活它。以下是一个针对 Symfony 4 的示例
// config/bundles.php return [ // ... WebnetFr\DatabaseAnonymizerBundle\WebnetFrDatabaseAnonymizerBundle::class => ['dev' => true], ];
# config/dev/webnet_fr_database_anonymizer.yaml webnet_fr_database_anonymizer: # configuration
如何配置要匿名化的字段?
查看如何配置数据库匿名化库中的匿名化字段。该包提供了相同的配置,但增加了一项:您可以按每个连接配置匿名化。
- 配置一个默认连接
# packages/dev/webnet_fr_anonymizer.yaml webnet_fr_database_anonymizer: # using default connection tables: <table name>: primary_key: <primary key field> fields: <field name>: generator: <generator>
# packages/doctrine.yaml doctrine: # default dbal: # driver, host, user, password, etc.
- 配置多个连接
# packages/dev/webnet_fr_anonymizer.yaml webnet_fr_database_anonymizer: connections: first_connection: tables: <table name>: primary_key: <primary key field> fields: <field name>: generator: <generator> second_connection: tables: <table name>: primary_key: <primary key field> fields: <field name>: generator: <generator>
# packages/doctrine.yaml doctrine: dbal: default_connection: user_database connections: first_connection: # driver, host, user, password, etc. second_connection: # driver, host, user, password, etc.
- 使用注解
如果你创建了实体,你可以使用注解来配置匿名化
use Doctrine\ORM\Mapping as ORM; use WebnetFr\DatabaseAnonymizerBundle\Annotation as Anonymize; /** * @ORM\Table(name="orders") * @ORM\Entity * * This annotation marks the entities to anonymize. * @Anonymize\Table() */ class Orders { /** * @ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true}) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @ORM\Column(name="address", type="string", length=256, nullable=true) * @Anonymize\Field(generator="faker", formatter="address") */ public $address; /** * @ORM\Column(name="zip_code", type="string", length=10, nullable=true) * @Anonymize\Field(generator="faker", formatter="postcode") */ public $zipCode; /** * @ORM\Column(name="comment", type="text", length=0, nullable=true) * @Anonymize\Field(generator="faker", formatter="text", arguments={300}) */ public $comment; /** * @ORM\Column(name="created_at", type="datetime", nullable=true) * @Anonymize\Field(generator="faker", formatter="dateTime", date_format="Y-m-d H:i:s") */ public $createdAt; /** * @ORM\Column(name="comment_history", type="array", nullable=true) * * A custom generator with its custom arguments. * @Anonymize\Field(generator="comment_history", max_messages_nb=5) */ public $commentHistory; }
如何添加自定义生成器?
如果你对 Faker 提供的生成器不满意,你总是可以添加自己的。
想象你有一个存储用户订单的实体
use Doctrine\ORM\Mapping as ORM; /** * Users' orders. * * @ORM\Table(name="orders") * @ORM\Entity */ class Orders { /** * History of all user's comments. * @var string[] * * @ORM\Column(name="comments", type="array", nullable=true) */ public $comments; }
并且你想要匿名化数组中的每个评论
webnet_fr_database_anonymizer: tables: # ... orders: fields: # ... comments: generator: comment_history # your generator
在大多数情况下,你需要添加两个类
- 一个工厂
namespace App\DatabaseAnonymizer; use Faker\Factory; use WebnetFr\DatabaseAnonymizer\Exception\UnsupportedGeneratorException; use WebnetFr\DatabaseAnonymizer\Generator\GeneratorInterface; use WebnetFr\DatabaseAnonymizer\GeneratorFactory\GeneratorFactoryInterface; /** * The factory that creates a generator out of provided configuration. * It is a Symfony service. */ class CommentHistoryGeneratorFactory implements GeneratorFactoryInterface { /** * @param array $config * An array of the configuration for field to anonymize. It contains * all specified entries, like "generator", "unique", "date_format", * "my_custom_entry", etc. * * @throws \WebnetFr\DatabaseAnonymizer\Exception\UnsupportedGeneratorException * The factory MUST throw "UnsupportedGeneratorException" if it is * impossible to create the generator for provided configuration. * * @return GeneratorInterface */ public function getGenerator($config): GeneratorInterface { // Check if the field should be anonymized with "comment_history" encoder. $generatorKey = $config['generator']; if ('comment_history' !== $generatorKey) { throw new UnsupportedGeneratorException($generatorKey.' generator is not known'); } // Retrieve any configuration values you need. $locale = $config['locale'] ?? 'en_US'; $minMessagesNb = $config['min_messages_nb'] ?? 1; $maxMessagesNb = $config['max_messages_nb'] ?? 10; // Create and configure generator. // Usually there is ONE generator instance for ONE field to anonymize // because there could be different config values for differet fields // even though these fields are anoymized with the same // "comment_history" generator. $faker = Factory::create($locale); $generator = new CommentHistoryGenerator($faker); $generator->setMinMessagesNb($minMessagesNb); $generator->setMaxMessagesNb($maxMessagesNb); return $generator; } }
由于 CommentHistoryGeneratorFactory
是一个 Symfony 服务,它可以依赖于任何其他服务(例如 UserPasswordEncoderInterface
以便能够加密密码)。
如果你使用 Symfony 服务的 autodiscover
和 autoconfiguration
,那么这就是你所需要的。否则,你需要将工厂注册为服务
services: App\DatabaseAnonymizer\CommentHistoryGeneratorFactory: tags: ["database_anonymizer.generator_factory"]
- 一个生成器
<?php namespace App\DatabaseAnonymizer; use Faker\Generator; use WebnetFr\DatabaseAnonymizer\Generator\GeneratorInterface; /** * Anonmyizer generator that generates comment history. */ class CommentHistoryGenerator implements GeneratorInterface { /** * Faker generator * @var Generator */ private $faker; /** * Minimum number of comments in history. * @var int */ private $minMessagesNb = 1; /** * Maximum number of comments in history. * @var int */ private $maxMessagesNb = 10; // Constructors, setters. /** * Generates new random value for each line. */ public function generate() { $comments = []; foreach (range(0, mt_rand(1, 10)) as $i) { $comments[] = $this->faker->realText(); }; return serialize($comments); } }