Package: pgantomizer
Anonymize data in your PostgreSQL dababase with ease. Anonymization is handy if you need to provide data to
people that should not have access to the personal information of the users.
Importing the data to third-party tools where you cannot guarantee what will happen to the data is also a common use case.
This tool will come in handy when GDPR will take effect in EU-countries.Anonymization ProcessThe rules for anonynimization are written in a single YAML file.
Columns that should be left in the raw form without anonymization must be explicitly marked in the schema.
This ensures that adding the new column in the DB without thinking about its sensitivity does not leak the data.
The default name of the primary key isidbut a custom one can be specified form the table in the schema.
Primary key is NOT anonymized by default.A sample YAML schema can be examined below.customer:raw:[language,currency]pk:customer_idcustomer_address:raw:[country,customer_id]custom_rules:address_line:aggregate_lengthSometimes it is needed to use a different anonymization function for a particular column.
It can be specified in thecustom_rulesdirective (see example above).
There is a limited set of functions you can choose from. So faraggregate_length- replaces content of the column with its length (can be used on any type that supports length function)Calling pgantomizer from the Command Linepgantomizer_dumpis a helper script that dumps tables specified in the YAML schema file to a compressed file usingpg_dump.
Just pass the path to the schema and the DB connection details.
Minimal working example taking advantage of default values of some of the required parameters:pgantomizer_dump--schemamy_schema.yaml--dbnameoriginal_postgres--useralaricTo see a list of all parameters, run:pgantomizer_dump-hThe script is able to take the DB connection details from environmental variables
following the conventions of running Django in Docker. The presumed variable names are:DB_DEFAULT_NAME,DB_DEFAULT_USER,DB_DEFAULT_PASS,DB_DEFAULT_SERVICE,DB_DEFAULT_PORT.pgantomizeris the main script that loads the Postgre dump into a specified instance. Then all columns
except primary keys and the ones specified in the schema asraware anonymized according to their data type.
Finally, the dump file is deleted by default to reduce risk of leakage of unanonymized data.
The connection details of the Postgres instance where the anonymized data should be loaded can be passed as argumentspgantomizer--schemamy_schema.yaml--dump-file./to_anonymize.sql--dbnameanonymized_postgres--useralaric--passwordanonymized_pass--hostlocalhost--port5432or through environmental variables with following names:ANONYMIZED_DB_NAME,ANONYMIZED_DB_USER,ANONYMIZED_DB_PASS,ANONYMIZED_DB_HOST,ANONYMIZED_DB_PORT.Calling pgantomizer from PythonUsedump_dbandload_anonymize_removefunctions to dump anonymize the data from Python.
In the following example, DB connections for the original and anonymized instance are specified via ENV variables described above.frompgantomizerimportdump_db,load_anonymize_removedump_db('to_anonymize.sql','anonymization_schema.yaml')load_anonymize_remove('to_anonymize.sql','anonymization_schema.yaml')Both functions have an optionaldb_argsargument to pass the connection arguments explicitly in a dict.
See the example below how the dict should look like.If you are only after anonymizing an existing database, there is a functionanonymize_dbthat will help you do that with a little extra work of parsing the YAML schema.importyamlfrompgantomizerimportanonymize_dbanonymize_db(yaml.load(open('anonymization_schema.yaml')),{'dbname':'anonymized_postgres','user':'alaric','password':'anonymized_pass','host':'localhost','port':'5432',})If you would like to use environmental variables instead, use functionanonymize.get_db_args_from_envto construct the dict from ENV.TODOexpand this READMEsubmit package automatically to PyPIadd –dry-run argument that will check the schema and output the operations to be performedremove password argument and usegetpassinstead for better security