Skip to main content

A custom Django field for storing and securely accessing a 1Password vault item.

Project description

django-opfield

PyPI PyPI - Python Version Django Version

A custom Django field that integrates with the 1Password op CLI to securely access secrets via the op:// secret reference URI.

Requirements

Getting Started

  1. Install the package from PyPI:

    python -m pip install django-opfield
    
  2. Install the 1Password op CLI tool, making sure it is callable from wherever your application is running.

  3. Create a 1Password service account and make the service account's token available to your application.

    Choose one option:

    • Set the OP_SERVICE_ACCOUNT_TOKEN environment variable

    • Configure in your application's settings.py:

      # settings.py
      DJANGO_OPFIELD = {
          # Explicitly set here only as an example
          # Use whatever configuration/environment library you prefer
          # (`python-dotenv`, `django-environs`, `environs`, etc.)
          "OP_SERVICE_ACCOUNT_TOKEN": "super-secret-token",
      }
      

Usage

OPField allows Django models to securely access secrets stored in a 1Password vault, enabling the integration of sensitive data without exposing it directly in your codebase. Secrets are stored using the op:// URI scheme and can be retrieved dynamically using a corresponding model attribute, <field_name>_secret.

Defining a model

First, let's define a model that includes the OPField. This field will store the reference to the secret in 1Password, not the secret itself.

from django.db import models

from django_opfield.fields import OPField


class APIService(models.Model):
    name = models.CharField(max_length=255)
    api_key = OPField()

    def __str__(self):
        return self.name

Accessing the secret

Assume you have a secret API key stored in a 1Password vault named "my_vault" under the item "my_api" with the field "api_key". Here's how you can store and access this secret within your Django project:

>>> from example.models import APIService
>>> my_api = APIService.objects.create(
...     name="My API", api_key="op://my_vault/my_api/api_key"
... )
>>> print(my_api)
<APIService: My API>
>>> print(my_api.name)
'My API'
>>> print(my_api.api_key)
'op://my_vault/my_api/api_key'
>>> # Retrieving the actual secret value is done using the automatically generated '_secret' attribute
>>> print(my_api.api_key_secret)
'your_super_secret_api_token_here'

Storing references, not secrets

Only the URI reference to the secret is ever stored and exposed in the Django admin interface and the database. The actual secret itself is never stored and is only retrieved dynamically when accessed. This approach enables secure management and access to secrets throughout your Django application, safeguarding against potential security vulnerabilities associated with direct exposure.

Documentation

Please refer to the documentation for more information.

License

django-opfield is licensed under the MIT license. See the LICENSE file for more information.

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_opfield-0.1.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

django_opfield-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file django_opfield-0.1.0.tar.gz.

File metadata

  • Download URL: django_opfield-0.1.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_opfield-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f77d2207e4075b0c2576114806d243387491b0cc8ed1977eaab86c9c8e1738cb
MD5 992db713ccfc01ba728690ff74dfb682
BLAKE2b-256 86a4ea14602d358b508b794af284027b05a1de481a8bd9367b18903bb2381374

See more details on using hashes here.

File details

Details for the file django_opfield-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_opfield-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_opfield-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6609f0aeb11be6e3ccecbcfa6f59147d2d41ded191901edcc4075f313c49d11
MD5 7ae2f0912e6bc49a798bc5b79ae6c282
BLAKE2b-256 202d27ee53e6aff1d81b1fcda088195e7662d51c7a112697746581fc1bc98f75

See more details on using hashes here.

Supported by

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