webnet-fr / database-anonymizer-bundle
数据库匿名化组件。
v0.0.1
2019-08-06 12:32 UTC
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: dev-master
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 not auto-updated.
Last update: 2024-09-12 12:00:40 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
,以便能够编码密码)。
如果您使用autodiscover
和autoconfiguration
的Symfony服务,那么这就是您所需要的。否则,您需要将工厂注册为服务
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); } }