|
| 1 | +# Blur |
| 2 | + |
| 3 | +[](https://packagist.org/packages/intermax/blur) |
| 4 | +[](https://packagist.org/packages/intermax/blur) |
| 5 | +[](https://packagist.org/packages/intermax/blur) |
| 6 | + |
| 7 | +Blur is a Laravel package that helps you obfuscate sensitive data in your database. It's perfect for creating anonymized copies of production databases for development and testing environments. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- 🔄 Obfuscate specific tables and columns in your database |
| 12 | +- 🧩 Use Faker to generate realistic but fake data |
| 13 | +- 🚀 Memory-optimized for handling large datasets |
| 14 | +- 🔍 Interactive mode to select which tables to obfuscate |
| 15 | +- 🛠️ Customizable obfuscation strategies |
| 16 | +- 🔒 Safety checks to prevent running in production environments |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +You can install the package via composer: |
| 21 | + |
| 22 | +```bash |
| 23 | +composer require intermax/blur |
| 24 | +``` |
| 25 | + |
| 26 | +After installation, publish the configuration file: |
| 27 | + |
| 28 | +```bash |
| 29 | +php artisan vendor:publish --provider="Intermax\Blur\BlurServiceProvider" |
| 30 | +``` |
| 31 | + |
| 32 | +## Configuration |
| 33 | + |
| 34 | +After publishing the configuration, you can find the configuration file at `config/blur.php`. Here's an example configuration: |
| 35 | + |
| 36 | +```php |
| 37 | +<?php |
| 38 | + |
| 39 | +declare(strict_types=1); |
| 40 | + |
| 41 | +return [ |
| 42 | + 'tables' => [ |
| 43 | + 'users' => [ |
| 44 | + 'columns' => [ |
| 45 | + 'name' => 'faker:name', |
| 46 | + 'email' => 'faker:email', |
| 47 | + // Add more columns as needed |
| 48 | + ], |
| 49 | + // 'chunk_size' => 2000, // Optional: Set a chunk size; higher is faster but will use more memory |
| 50 | + // 'keys' => ['id'], // Optional: Specify when the automatic discovery won't work |
| 51 | + // 'method' => 'update', // Optional: Use 'clear' to truncate the table instead |
| 52 | + ], |
| 53 | + // Add more tables as needed |
| 54 | + ], |
| 55 | +]; |
| 56 | +``` |
| 57 | + |
| 58 | +### Configuration Options |
| 59 | + |
| 60 | +- **tables**: An array of tables to obfuscate |
| 61 | + - **columns**: (Optional, can be omitted when the table needs to be cleared) The columns to obfuscate and the obfuscation method to use. Only columns that should be obfuscated need to be specified. |
| 62 | + - **chunk_size**: (Optional) The number of records to process at once (default: 2000). See [Performance Considerations](#performance-considerations) |
| 63 | + - **keys**: (Optional) The key columns to use. The key columns are discovered when obfuscating, but if that fails (for example when there are no primary keys) the unique 'key' can be specified. |
| 64 | + - **method**: (Optional) The method to use for obfuscation (default: 'update', alternative: 'clear' to clear the table. This can be useful for tables like `jobs` or tables that store audit logs.) |
| 65 | + |
| 66 | +## Usage |
| 67 | + |
| 68 | +To obfuscate your database, run the following command: |
| 69 | + |
| 70 | +```bash |
| 71 | +php artisan blur:obfuscate |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | +> ⚠️ **This will change records (as you configured) for the default database connection.** |
| 76 | +
|
| 77 | +### Interactive Mode |
| 78 | + |
| 79 | +You can use the interactive mode to select which tables to obfuscate: |
| 80 | + |
| 81 | +```bash |
| 82 | +php artisan blur:obfuscate --interactive |
| 83 | +# or |
| 84 | +php artisan blur:obfuscate -i |
| 85 | +``` |
| 86 | + |
| 87 | +This will display a list of configured tables and allow you to select which ones to obfuscate. |
| 88 | + |
| 89 | +## Obfuscation Methods |
| 90 | + |
| 91 | +### Faker |
| 92 | + |
| 93 | +Blur comes with built-in support for [Faker](https://github.com/FakerPHP/Faker). You can use any Faker method by prefixing it with `faker:`: |
| 94 | + |
| 95 | +```php |
| 96 | +'columns' => [ |
| 97 | + 'name' => 'faker:name', |
| 98 | + 'email' => 'faker:email', |
| 99 | + 'phone' => 'faker:phoneNumber', |
| 100 | + 'address' => 'faker:address', |
| 101 | + // See Faker documentation for more available methods |
| 102 | +], |
| 103 | +``` |
| 104 | + |
| 105 | +### Custom Obfuscators |
| 106 | + |
| 107 | +You can create your own obfuscators by implementing the `Intermax\Blur\Contracts\Obfuscator` interface: |
| 108 | + |
| 109 | +```php |
| 110 | +<?php |
| 111 | + |
| 112 | +namespace App\Obfuscators; |
| 113 | + |
| 114 | +use Intermax\Blur\Contracts\Obfuscator; |
| 115 | + |
| 116 | +class FixedStringObfuscator implements Obfuscator |
| 117 | +{ |
| 118 | + public function generate(?array $parameters = null): mixed |
| 119 | + { |
| 120 | + return $parameters[0] ?? 'default-value'; |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Then use it in your configuration: |
| 126 | + |
| 127 | +```php |
| 128 | +'columns' => [ |
| 129 | + 'some_field' => App\Obfuscators\FixedStringObfuscator::class.':custom-value', |
| 130 | +], |
| 131 | +``` |
| 132 | + |
| 133 | +## Performance Considerations |
| 134 | + |
| 135 | +Blur processes records in chunks. You can adjust the `chunk_size` in the configuration to balance between memory usage and performance. |
| 136 | + |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
| 141 | + |
| 142 | +## Contributing |
| 143 | + |
| 144 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments