Skip to main content

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.

Metadata

Release files for django-opfield 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-opfield 0.2.0
File Size Uploaded Provenance
django_opfield-0.2.0.tar.gz 17.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-opfield 0.2.0
File Interpreter ABI Platform
django_opfield-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.6 kB

Release files / django_opfield-0.2.0.tar.gz

Download URL django_opfield-0.2.0.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
77d6b604b977a7e1339f1d49fa9c7e2c4bdb9b84dc39fe6377d69d9bf92e79fd
BLAKE2b-256 checksum
How to use checksums
769ae67daafd80b8ec5c110b3e2f5d7bc96026e2ee6892f722b2b969652d74c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

Release files / django_opfield-0.2.0-py3-none-any.whl

Download URL django_opfield-0.2.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7556dec2628245751b29da621a6af8b3787d4afde0d8c03e017622b4e9664aba
BLAKE2b-256 checksum
How to use checksums
d7a82f4c58821dcc8d0745a3ed21b1735c7f730a119a137a47dcc9a190220e6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

Security for django-opfield 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_opfield-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page