elgentos/masquerade

此包已被废弃且不再维护。作者建议使用 smile/gdpr-dump 包。

框架无关的数据库种子工具,带有伪造数据

1.2.2 2022-11-22 15:20 UTC

README

Masquerade logo

Masquerade

此项目已被废弃。感谢您的支持!

此项目已被废弃。为了更好的、更快的和更维护的替代方案,请参阅 Smile 的 gdpr-dump。我们为流行的 Magento 2 扩展创建了自定义配置文件仓库,请参阅 elgentos/gdpr-dump-magento-2-extensions

由 Faker 驱动的、平台无关的、地区兼容的数据伪造工具

将 Masquerade 指向数据库,提供一个在 YAML 中定义的规则集,Masquerade 将自动为您匿名化数据!

42574650-30e8d186-851f-11e8-9693-c23b426c43f2.png

开箱即用的支持框架

  • Magento 2
  • Shopware 6

自定义

您可以在与运行 masquerade 相同目录下的 config 目录中添加自己的配置文件。配置文件将与该平台现有的任何配置文件合并,并覆盖任何开箱即用的值。

请参阅 Magento 2 YAML 文件 作为示例。

例如,要覆盖 Magento 2 的 admin.yaml,您只需在 config/magento2/admin.yaml 中放置一个文件。例如,如果您想完全禁用/跳过一个组,只需添加以下内容;

admin:

您可以为自定义表或第三方供应商的表添加自己的配置文件。以下是一些示例

要生成此类文件,您可以运行 masquerade identify 命令。这将查找名称中显示个人可识别数据提示的列,例如 nameaddress。它将交互式地询问您将其添加到所选平台的配置文件中。

部分匿名化

您可以通过包含 'where' 子句来仅影响某些记录 - 例如,为了避免匿名化某些管理员账户,或保留用于单元测试的数据,如下所示

customers:
  customer_entity:
    provider: # this sets options specific to the type of table
      where: "`email` not like '%@mycompany.com'" # leave mycompany.com emails alone

删除数据

您可能需要完全或部分删除数据 - 例如,如果您的开发人员不需要销售订单,或者您希望将数据库大小保持得比生产数据库小得多,请指定 'delete' 选项。

当删除一些 Magento 数据时,例如销售订单,请添加命令行选项 --with-integrity 以强制执行外键检查,例如,如果父销售订单被删除,则销售发票记录将被自动删除。

orders:
  sales_order:
    provider:
      delete: true
      where: "customer_id != 3" # delete all except customer 3's orders because we use that for testing
    # no need to specify columns if you're using 'delete'      

如果您使用 'delete' 而不使用 'where',并且不使用 '--with-integrity',则将使用 'truncate' 删除整个表。如果指定了 '--with-integrity',则不会使用 'truncate',因为这会绕过键检查。

Magento EAV 属性

您可以使用 Magento2Eav 表类型将 EAV 属性视为普通列,例如。

products:
  catalog_product_entity: # specify the base table of the entity
    eav: true
    provider:
      where: "sku != 'TESTPRODUCT'" # you can still use 'where' and 'delete'
    columns:
      my_custom_attribute:
        formatter: sentence
      my_other_attribute:
        formatter: email

  catalog_category_entity:
    eav: true
    columns:
      description: # refer to EAV attributes like normal columns
        formatter: paragraph

格式化选项

对于格式化程序,您可以使用所有默认的 Faker 格式化程序

自定义数据提供者/格式化程序

您还可以使用格式化程序创建自己的自定义提供者。它们需要扩展 Faker\Provider\Base,并且需要位于您运行 masquerade 的目录的 ~/.masquerade.masquerade 相对路径中。

示例文件 .masquerade/Custom/WoopFormatter.php;

<?php

namespace Custom;

use Faker\Provider\Base;

class WoopFormatter extends Base {

    public function woopwoop() {
        $woops = ['woop', 'wop', 'wopwop', 'woopwoop'];
        return $woops[array_rand($woops)];
    }
}

然后在您的 YAML 文件中使用它。提供者需要在列名级别设置,而不是在格式化程序级别。

customer:
  customer_entity:
    columns:
      firstname:
        provider: \Custom\WoopFormatter
        formatter:
          name: woopwoop

自定义表类型提供者

一些系统包含相关数据的链接表 - 例如,Magento 的 EAV 系统、Drupal 的实体字段和 WordPress 的帖子元数据表。您可以提供自定义表类型。为了做到这一点,您需要实现 2 个接口

  • Elgentos\Masquerade\DataProcessorFactory 用于实例化您的自定义处理器。它接收表服务工厂、输出对象以及为您的表指定的整个 YAML 配置数组。

  • Elgentos\Masquerade\DataProcessor 用于处理运行命令所需的操作,例如

    • truncate 应该通过配置截断提供的表
    • delete 应该删除通过配置提供的表
    • updateTable 应该根据配置中的列定义,使用生成器提供的值更新表。请参阅 Elgentos\Masquerade\DataProcessor\RegularTableProcessor::updateTable 以获取参考。

首先,您需要从工厂开始,该工厂将实例化实际的处理器

示例文件 .masquerade/Custom/WoopTableFactory.php;

<?php

namespace Custom;

use Elgentos\Masquerade\DataProcessor;
use Elgentos\Masquerade\DataProcessor\TableServiceFactory;
use Elgentos\Masquerade\DataProcessorFactory;
use Elgentos\Masquerade\Output;
 
class WoopTableFactory implements DataProcessorFactory 
{

    public function create(
        Output $output, 
        TableServiceFactory $tableServiceFactory,
        array $tableConfiguration
    ): DataProcessor {
        $tableService = $tableServiceFactory->create($tableConfiguration['name']);

        return new WoopTable($output, $tableService, $tableConfiguration);
    }
}

示例文件 .masquerade/Custom/WoopTable.php;

<?php

namespace Custom;

use Elgentos\Masquerade\DataProcessor;
use Elgentos\Masquerade\DataProcessor\TableService;
use Elgentos\Masquerade\Output;

class WoopTable implements DataProcessor
{
    /** @var Output */
    private $output;

    /** @var array */
    private $configuration;

    /** @var TableService */
    private $tableService;

    public function __construct(Output $output, TableService $tableService, array $configuration)
    {
        $this->output = $output;
        $this->tableService = $tableService;
        $this->configuration = $configuration;
    }

    public function truncate(): void
    {
        $this->tableService->truncate();
    }
    
    public function delete(): void
    {
        $this->tableService->delete($this->configuration['provider']['where'] ?? '');
    }
    
    public function updateTable(int $batchSize, callable $generator): void
    {
        $columns = $this->tableService->filterColumns($this->configuration['columns'] ?? []);
        $primaryKey = $this->configuration['pk'] ?? $this->tableService->getPrimaryKey();
        
        $this->tableService->updateTable(
            $columns, 
            $this->configuration['provider']['where'] ?? '', 
            $primaryKey,
            $this->output,
            $generator,
            $batchSize
        );
    }
}

然后在您的 YAML 文件中使用它。处理器工厂需要在表级别设置,可以是简单的类名,也可以是一组选项,这些选项对您的类可用。

customer:
  customer_entity:
    processor_factory: \Custom\WoopTableFactory
    some_custom_config:
      option1: "test"
      option2: false
    columns:
      firstname:
        formatter:
          name: firstName

安装

下载 phar 文件

curl -L -o masquerade.phar https://github.com/elgentos/masquerade/releases/latest/download/masquerade.phar

用法

$ php masquerade.phar run --help

Description:
  List of tables (and columns) to be faked

Usage:
  run [options]

Options:
      --platform[=PLATFORM]
      --driver[=DRIVER]      Database driver [mysql]
      --database[=DATABASE]
      --username[=USERNAME]
      --password[=PASSWORD]
      --host[=HOST]          Database host [localhost]
      --port[=PORT]          Database port [3306]
      --prefix[=PREFIX]      Database prefix [empty]
      --locale[=LOCALE]      Locale for Faker data [en_US]
      --group[=GROUP]        Comma-separated groups to run masquerade on [all]
      --with-integrity       Run with foreign key checks enabled
      --batch-size=BATCH-SIZE  Batch size to use for anonymization [default: 500]

您还可以将这些变量设置在您从 masquerade 运行的同一位置的 config.yaml 文件中,例如

platform: magento2
database: dbnamehere
username: userhere
password: passhere
host: localhost
port: porthere

夜间运行

查看 wiki 了解如何在 CI/CD 中夜间运行 Masquerade

从源代码构建

要从源代码构建 phar,您可以使用 build.sh 脚本。请注意,它依赖于 Box,它包含在此存储库中。

# git clone https://github.com/elgentos/masquerade
# cd masquerade
# composer install
# chmod +x build.sh
# ./build.sh
# bin/masquerade

Debian 打包

要为该项目构建 deb,请运行

# apt-get install debhelper cowbuilder git-buildpackage
# export ARCH=amd64
# export DIST=buster
# cowbuilder --create --distribution buster --architecture amd64 --basepath /var/cache/pbuilder/base-$DIST-amd64.cow --mirror http://ftp.debian.org/debian/ --components=main
# echo "USENETWORK=yes" > ~/.pbuilderrc
# git clone https://github.com/elgentos/masquerade
# cd masquerade
# gbp buildpackage --git-pbuilder --git-dist=$DIST --git-arch=$ARCH --git-ignore-branch -us -uc -sa --git-ignore-new

为了为新版本生成新的 debian/changelog

export BRANCH=master
export VERSION=$(date "+%Y%m%d.%H%M%S")
gbp dch --debian-tag="%(version)s" --new-version=$VERSION --debian-branch $BRANCH --release --commit

致谢