Skip to main content

Django app used to manage production data anonymisation.

Project description

Django Anonymiser

Django app for managing / tracking Django model anonymisation.

Status

This is currently used internally only, and has not been published to PyPI - use with caution.

Background

We currently have a pattern of each model having its own anonymise method, and a management command that iterates over each model calling said method on each object. This works, but it's impossible to track - we don't know which models, and which fields on those models, are actually being anonymised, and the documentation suffers the same fate as all documentation is that is not auto-generated.

This library adopts the pattern used by the django-side-effects library of having a "registry" of anonymisers and a management command that outputs the complete listing of all anonymisers and all fields anonymised. This output can then be plugged into the django-project-checks framework and stored in the repo as a "snapshot" that is then checked in the CI pipeline, meaning it is guaranteed to be up-to-date.

The anonymisation itself doesn't change - it's just shifting the code around.

Redaction vs. Anonymisation

This library contains two flavours of anonymisation - Redaction, and Anonymisation. The two differ in how the data is overwritten:

Type Implementation Performance Data
Redaction SQL Fast Table level
Anonymisation Python Slow Row level

Redaction

Redaction is implemented as a single SQL update statement that wipes an entire table in one go. It's very fast, but it's limited in the sense that it cannot produce realistic data. In fact it may well render your application unusable. It is recommended as the first step in data anonymisation.

Anonymisation

Anonymisation is an row-level operation that iterates over a queryset and updates each object in turn. The main advantage is that post-anonymisation you will have realistic, usable, data.

Usage

As an example - this is a hypothetical User model's anonymisation today:

# models.py
class User:

    def anonymise(self) -> None:
        self.first_name = "Fred"
        self.last_name = "Flinstone"

Using this library we remove the anonymise method and create and register a new anonymiser that splits out each field:

# anonymisers.py
@register_anonymiser
class UserAnonymiser(ModelAnonymiser):
    model = User

    def anonymise_first_name(self, obj: User) -> None:
        obj.first_name = "Fred"

    def anonymise_last_name(self, obj: User) -> None:
        obj.last_name = "Flintstone"

You should import the anonymisers module in your apps.py in order to ensure that it is registered:

# apps.py
from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = "Users"

    def ready(self) -> None:
        super().ready()
        from . import anonymisers  # noqa F401

Once set up, running the display_model_anonymisation management command will output a list of all models in the project, whether they have a registered anonymiser, and then all model fields in the project and whether they are anonymised.

The snapshot for this project itself is tests/model_anonymisation.md.

The output format of the snapshot can be overridden - it's rendered using a Django template templates/display_model_anonymisation.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_anonymise-0.3.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_anonymise-0.3.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file django_anonymise-0.3.0.tar.gz.

File metadata

  • Download URL: django_anonymise-0.3.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.12.7 Darwin/24.1.0

File hashes

Hashes for django_anonymise-0.3.0.tar.gz
Algorithm Hash digest
SHA256 441c90d0f7d8443923317f5187b94cf98fc2f3726442c0d0681c323753a29a0c
MD5 10ea874649696ecf8a98c0be811ba1d1
BLAKE2b-256 bebd494c0ebe2b906cc11d0883268460935662f0497c967e0e1db1260ac794ee

See more details on using hashes here.

File details

Details for the file django_anonymise-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_anonymise-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.12.7 Darwin/24.1.0

File hashes

Hashes for django_anonymise-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74c8279da05edd19dea98253946dd11576a11bd38128bcb4f28c1fb1e4a51fe0
MD5 8e2f4678bdf952de227bfbe38c231ada
BLAKE2b-256 7805dfd9803b2797d13c8e7a111693b4df71976882df2710745ac86949acfe1b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page