Skip to main content

A set of fields that wrap standard Django fields with encryption provided Piiano Vault.

Project description

Piiano Vault

Django Encrypted Model Fields

coverage Workflow status badge Python version badge PyPI version

About

This library allows you to specify specific fields to encrypt in your Django models using Vault's API in a transparent manner, taking advantage of Vault's advanced capabilities. This helps you:

  • Achieve compliance with various privacy standards
  • Implement TTL or expiration for data
  • Get Masked or transformed versions of your data
  • Rely on Vault's permission model

This is a fork of https://gitlab.com/lansharkconsulting/django/django-encrypted-model-fields which in turn is a fork of https://github.com/foundertherapy/django-cryptographic-fields. It has been renamed, and updated to support encryption through Piiano Vault's API.

Usage

First install the library:

pip install django-encryption

Add to your settings.py (Example in here):

  • VAULT_ADDRESS
  • VAULT_API_KEY
  • VAULT_DEFAULT_COLLECTION Note it is best practice to provide VAULT_ADDRESS and VAULT_API_KEY via environment variables in production
  • Add django_encryption to INSTALLED_APPS

In your models.py (Example in here):

  1. Import any desired field type, for example:
from django_encryption.fields import EncryptedCharField
  1. For each model field you would like to encrypt, replace the field name with any of the fields you imported in step 1 (For example, EncryptedCharField).

    You can customize the field by providing additional parameters such as:

    • encryption_type (optional) - Can be EncryptionType.randomized or EncryptionType.deterministic
    • expiration_secs (optional) - An integer or None. If an integer, the number of seconds before the encrypted data is expired, and cannot be decrypted anymore. Works only with randomized encryption_type
    • vault_collection (optional) - The name of the vault collection that this field is related to. Defaults to settings.VAULT_DEFAULT_COLLECTION
    • vault_property (optional) - The name of the property in the vault collection that this field is related to. Defaults to the name of the field in django.
    • data_type_name (optional) - The name of the data type in vault. Defaults to 'string'. This only has impact when generating a vault migration, and does not change the way your django model would behave.

    Note: use vault_collection together with vault_property to specify the collection and property in vault that represent this field. This is important for permission control and audit logs. For more advanced use-cases, this would allow you to transition smoothly to using Vault as a secure storage for PII data.

Query your model as usual:

  • Caveat: right now, an API call to vault will be generated for each field in each Model instance you encrypt or decrypt. In the future this will be batched.

You can wrap your queries with: with fields.mask_field(MyModel.my_field): (or transform or with_reason):

  • This tells the encryption SDK to mask the values of MyModel.my_field. So for example, for an SSN you would get "*--6789".
  • All vault's supported transformations are also supported using the transform context manager. See Built-in transformations in Vault's API documentation for a list of Vault's supported transformations.

Sample code

from django.db import models
from django_encryption.fields import EncryptedCharField, EncryptedEmailField, EncryptedDateField, EncryptionType


class Customer(models.Model):
    name = EncryptedCharField(data_type_name='NAME')
    email = EncryptedEmailField(data_type_name='EMAIL')
    phone = EncryptedCharField(
        data_type_name='PHONE_NUMBER', null=True, blank=True)
    ssn = EncryptedCharField(
        encryption_type=EncryptionType.randomized, data_type_name='SSN', null=True, blank=True)
    dob = EncryptedDateField(
        data_type_name='DATE_OF_BIRTH', null=True, blank=True)
    state = models.CharField(max_length=100, null=True, blank=True)

You can see a full working example in our sample.

Installation for local development (with VSCode)

  1. Clone the repo: git clone https://github.com/piiano/vault-python
  2. Ensure you have python poetry installed on your machine (a global installation). Example: pipx install poetry
  3. Run the following commands from the sdk/orm-django directory:
    poetry install
    poetry shell
    code .
    
  4. To run tests: python manage.py test. Tests should also be available from within vscode.

NOTE Make sure you have a local copy of vault running on your machine. To do so, follow the Installations Instructions.

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_encryption-0.1.3.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

django_encryption-0.1.3-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file django_encryption-0.1.3.tar.gz.

File metadata

  • Download URL: django_encryption-0.1.3.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for django_encryption-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ad657097ee63221c2a0be9f62c771ae66c986e2b0e889039751af260f6afb1e0
MD5 0041781e42e9bdcd6446d2548d571d2d
BLAKE2b-256 2ff2e9de63d1ceb724292299ea9c30f690f7cd78c3223bc1b11d8513c3f06e1a

See more details on using hashes here.

File details

Details for the file django_encryption-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: django_encryption-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for django_encryption-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7facc7049a0810a0e4c78ab3034f13a8f56e4c400fe37e63e1bd64f30208951e
MD5 ffdbd90ad52e0811bdc02856cdee7dfb
BLAKE2b-256 3a48268277ffbe35f662ae50deb30ffc0f29bfe0f2a172c73be6653eb1e7bed3

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