2017 © Pedro Peláez
 

symfony-bundle anonymize-bundle

A bundle to anonymize sensitive database data

image

orange-rt/anonymize-bundle

A bundle to anonymize sensitive database data

  • Monday, May 21, 2018
  • by SanderVerkuil
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1,298 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 5 Versions
  • 44 % Grown

The README.md

Scrutinizer Code Quality, (*1)

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*2)

$ composer require orange-rt/anonymize-bundle "^0.1.0"

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*3)

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:, (*4)

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new OrangeRT\AnonymizeBundle\OrangeRTAnonymizeBundle(),
        );

        // ...
    }

    // ...
}

Usage

Doctrine managed entities can be anonymized by annotating the properties that you want to be anonymized. There are two annotations available, @Anonymize and @AnonymizeEntity., (*5)

Anonymizing a property

Properties can be anonymized with the @Anonymize annotation like as follows:, (*6)

<?php

use OrangeRT\AnonymizeBundle\Mapping\Anonymize;

/**
 * A doctrine managed entity
 */
class Person
{
    /**
     * @Anonymize(faker="email")
     */
    private $username;
}

Upon calling php bin/console anonymize:anonymize the property username will be updated to $faker->email. The faker is generated in the driver., (*7)

Anonymizing callback

Sometimes it is required that properties need an advanced or a custom way of anonymizing said entity. If a method is annotated with the @Anonymize annotation, the method is called. If you need a faker you can typehint the parameter, like in the example below:, (*8)

<?php

use OrangeRT\AnonymizeBundle\Mapping\Anonymize;

class Person
{
    private $username;
    private $email;

    /**
     * @Anonymize() 
     */
    public function anonymize(\Faker\UniqueGenerator $generator)
    {
        $this->username = $this->email = $generator->email;
    }
}

Upon anonymizing the person, a UniqueGenerator is created for the method and the method is invoked with the generator. The username will be the same as the email, and it will be a uniquely generated email., (*9)

Excluding entities

It is possible to either skip a property, or to skip an entire object., (*10)

Skipping objects

It is either possible to blacklist an object, or to whitelist an object. The exclusions are done with a key value pair, where the key is the name of the property, and the value is either a direct match with the value, or a regex., (*11)

The inclusions are done in the same way, if one of the inclusions is matched, the object is anonymized., (*12)

In the example below, every person will be anonymized except for the people that have a username that ends with @orangert.nl., (*13)

<?php

use OrangeRT\AnonymizeBundle\Mapping\AnonymizeEntity;
use OrangeRT\AnonymizeBundle\Mapping\Anonymize;

/**
 * 
 * @AnonymizeEntity(exclusions={"username": "/@orangert.nl$/"})
 */
class Person
{
    /**
     * @Anonymize(faker="email", unique=true)
     */
    private $username;

    /**
     * @Anonymize(faker="firstName")
     */
    private $firstname;

    /**
     * @Anonymize(faker="lastName")
     */
    private $lastname;
}

Changing the faker locale

The default faker locale is nl_NL. To set the locale:, (*14)

# app/config/parameters.yml
    // ...
    orange_rt_anonymize.default_locale: 'en_US'

Unique variables

For properties like email and usernames, unique values should be used. The Anonymize property has a unique=true flag to set use the UniqueGenerator provided by the Faker library. If a callback needs the UniqueGenerator, typehint the generator with the UniqueGenerator., (*15)

Contributing

Pull requests

There might be open issues in the Github issue tracker. I'm open to receive new pull requests and will check them as soon as I possibly can., (*16)

Issues

If you find any bugs, please report them at the issue tracker. I will look at them as soon as I possibly can., (*17)

The Versions