indeev/laravel-rapid-db-anonymizer

Rapidly anonymize huge amount of data

v3.0 2023-02-27 09:37 UTC

This package is auto-updated.

Last update: 2024-11-27 13:17:43 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

The package rapidly anonymizes large amounts of sensitive data throughout the database.

Installation

You can install the package through the composer:

composer require indeev/laravel-rapid-db-anonymizer

Usage

In any model that contains sensitive data use Anonymizable; trait and const ANONYMIZABLE = []; constant.

class Customer 
{
    use Anonymizable;

    const ANONYMIZABLE = [
        'name' => [
            'faker' => ['provider' => 'firstName'],
        ],
    ];
    // ...
}

ANONYMIZABLE is defined as an array of 'column_name' => [what_to_do] values.

Prepare ANONYMIZABLE constant

Truncate table

const ANONYMIZABLE = ['truncate'];

Replace column value with faker's provider (without parameters)

const ANONYMIZABLE = [
        'name' => [
            'faker' => ['provider' => 'firstName'],
        ],
        // next columns
    ];

Replace column value with faker's provider (with parameters)

const ANONYMIZABLE = [
        'secret_code' => [
            'faker' => ['provider' => 'randomNumber', 'params' => [6, true]],
        ],
        // next columns
    ];

Replace with exact value

  • if value of setTo is an array type, it is converted to json string. For instance ['foo' => 'bar'] is converted to {"foo":"bar"}.
  • if value of setTo is null, it is converted to NULL. Pay special attention that column is set as nullable.
const ANONYMIZABLE = [
        'favorite_politician' => [
            'setTo' => 'CONFIDENTIAL',
        ],
        'favorite_numbers' => [
            'setTo' => [7, 13],
        ],
        'favorite_meals' => [
            'setTo' => null,
        ],
        // next columns
    ];

Replace also NULL values
By default, NULL values are skipped. If you also want to anonymize them, you must add anonymizeNull to the column with a value true.

const ANONYMIZABLE = [
        'shipping_address' => [
            'faker' => ['provider' => 'address'],
            'anonymizeNull' => true,
        ],
        // next columns
    ];

Run anonymization

To run anonymization for the entire database (all models with Anonymizable trait) use

php artisan db:anonymize

To run anonymization over a specific model use command with --model= option

php artisan db:anonymize --model=\\App\\Models\\VerificationCode

To run anonymizing specific columns in the entire database use command with --columns= option. Individual column names must be separated by comma

php artisan db:anonymize --columns=name,surname

Use a combination of the previous two options to anonymize specific columns above a specific model

php artisan db:anonymize --model=\\App\\Models\\User --columns=name,surname

Testing

composer test

Config

You can export config file by:

php artisan vendor:publish --provider="Indeev\LaravelRapidDbAnonymizer\LaravelRapidDbAnonymizerServiceProvider"

In config, you can modify:

  • Chunk size (default: 500) - all 500 rows are updated by one query
  • Forbidden environments (default: production, prod)
  • Custom model directory (default: app/Models)
  • Custom model namespace (default: \App\Models)
  • Faker's locale (default: en_US)

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email katerinak@indeev.eu instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.