Mask data from Production using Faker to use safely elsewhere.
Ever wanted to use a copy of your Laravel project's Production database in your test environments? But you can't, because it's a security risk?
Now, you can get a copy of the Production database and mask just the values that need masking - names, phone numbers, email addresses etc - while keeping your data otherwise intact.
From the command line run:
composer require brendantwhite/databasemask
First, ensure you have a Model Factory defined for each Eloquent model class that you want to mask.
Then, add a $masked
property to your Eloquent model classes, specifying which attributes on your
model should be masked.
<?php
// in app/Models/User.php
/**
* The attributes that should be masked by DatabaseMask.
*
* @var array
*/
protected $masked = [
'name',
'email',
'password',
];
If you have any models that you don't need to be masked at all,
add the $masked
property anyway, but make it an empty array.
<?php
// in app/Models/SomeOtherModel.php
/**
* The attributes that should be masked by DatabaseMask.
*
* @var array
*/
protected $masked = [];
To mask your data, run the php artisan dbm:mask
command. This will loop through all your Eloquent models, looking for
the $masked
property.
-
If the
$masked
property is null or the empty set, that model will be skipped. -
If the
$masked
property is missing, that model will be flagged with a warning. -
If the
$masked
property is not empty, but no Factory has been created for that model, an error is returned.
But if the $masked
property contains field names, and a Factory has been created, then
the fields in the $masked
property will be replaced with Faker values. All other fields will be left untouched.
This package also contains light wrappers around Spatie's excellent laravel-db-snapshots package, to make backups and restores even easier.
To use these commands you will first need to install laravel-db-snapshots
as per their installation instructions.
Then, run php artisan dbm:backup
to create a backup file of your database on your snapshots
disk.
Run php artisan dbm:restore
to choose a backup file and restore it to your current enviroment.
Generally, you'll want to backup from your Production enviornment, and restore to some other environment, and then mask that data in the other environment.
You do not need to install laravel-db-snapshots
if you only want to use the dbm:mask
command.
TODO: write some tests.
I cannot, and do not, guarantee that using DatabaseMask will make your data 100% de-identified.
I can and will make a good-faith effort to ensure that, when configured correctly, DatabaseMask will remove personally idendifiable information from your data.
However I cannot guarantee that this software is 100% bug-free, and I certainly can't guarantee that you have configured it correctly. So use this software at your own risk.
This package is open-sourced software licensed under the MIT license.