2017 © Pedro Peláez
 

symfony-bundle anonymizer-bundle

An annotation-based bundle to anonymize sensitive data per entity

image

ping-localhost/anonymizer-bundle

An annotation-based bundle to anonymize sensitive data per entity

  • Monday, July 23, 2018
  • by ping-localhost
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

, (*1)

Latest Stable Version Dependabot Status Build Status Code coverage, (*2)

Credit

This bundle is a stripped down version of the OrangeRT/AnonymizeBundle package created by OrangeRT, so I'm hereby giving credit where credit is due., (*3)

I've forked their project and stripped it, so that I could use it to anonymize data per class, instead of the whole database., (*4)

Installation

Installation of the bundle can be done using composer and is the recommended way of adding the bundle to your application. To do so, in your command line enter the project directory and run the following command to download the latest stable version of this bundle:, (*5)

$ composer require ping-localhost/anonymizer-bundle

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

Enabling the Bundle without Symfony Flex

When using Symfony Flex, Symfony will automatically enable the bundle. If Flex is not used, then in app/AppKernel.php you need to enable the bundle by adding it to the list of registered bundles. Just add it to the list of already registered bundles like so:, (*7)

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new PingLocalhost\AnonymizerBundle\AnonymizerBundle(),
            // ...
        ];
    }
}

Usage

Classes can be anonymized by annotating the properties that you want to be anonymized. There are two annotations available, @Anonymize and @AnonymizeClass., (*8)

Anonymizing a property

Properties can be anonymized with the @Anonymize annotation:, (*9)

<?php

use PingLocalhost\AnonymizerBundle\Mapping\Anonymize;

class Person
{
    /**
     * @Anonymize(faker="userName")
     */
    private $username;
}

Once you have tagged a class with the @Anonymize-annotation, its possible to anonymize that class by passing it to the anonymize-function within the AnonymizeProcessor-class., (*10)

Anonymizing callback

Sometimes it is required that properties need an advanced or a custom way of anonymizing said class. If a method is annotated with the @Anonymize annotation, the method is called., (*11)

If you need a faker you can typehint the parameter, as soon below:, (*12)

<?php

use PingLocalhost\AnonymizerBundle\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., (*13)

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., (*14)

Excluding objects

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

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., (*16)

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

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

<?php

use PingLocalhost\AnonymizerBundle\Mapping\AnonymizeClass;
use PingLocalhost\AnonymizerBundle\Mapping\Anonymize;

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

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

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

Faker

The Faker library is used to generate random values., (*19)

Providers

All the possible providers for faker option can be found here: https://github.com/fzaninotto/Faker#formatters, (*20)

Changing the locale

The default faker locale is nl_NL, which can be changed as shown below:, (*21)

# app/config/parameters.yml
    // ...
    ping_localhost.anonymizer_bundle.default_locale: 'en_US'

License

The ping-localhost/anonymizer-bundle is licensed under the MIT License, meaning you can reuse the code within proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice., (*22)

The Versions