Skip to main content

Django package that allows you to store secrets encrypted in JSON and access them easily in your Django project.

Project description

DJSM - Django JSON Secrets Manager

Django package that allows you to easily store and access your Django project's secrets like secret key, database password, etc., encrypted in a JSON file.

View package on PyPI

Installation and Quick Setup

  • Install the package using pip.
pip install djsm
  • Setup or update a '.env' file for your project.
DJSM_SECRETS_FILE_PATH = "./.enc/path/secrets.json" 
# Assign path to file you want secrets to be stored in. Even if it does not exist yet

# NOT MANDATORY
DJSM_SECRET_KEY_NAME = "secretkey"
  • Import the package in your Django project

In settings.py:

import djsm

djsm.check_setup()
  • Run server
python manage.py runserver

If everything was setup successfully, you should see "Setup OK!" on the terminal. Remember to remove the djsm.check_setup() line from your settings.py file.

Usage

Before starting, a '.env' file has to be created. In the file, the following should be added;

  • DJSM_SECRETS_FILE_PATH -> Path(preferably absolute) to file where all secrets will be stored. Example:
DJSM_SECRETS_FILE_PATH = "/.enc/pathtofile/secrets.json"
  • DJSM_SECRET_KEY_NAME -> Name with which the Django secret key should be stored. Example:
DJSM_SECRET_KEY_NAME = 'django_secret_key'

Using DJSM in the CLI

An easy way to start using DJSM in your project is by letting DJSM handle your django project's secret key. To do this:

  • Add djsm to the project's list of installed apps.

In settings.py:

INSTALLED_APPS = [
    ...,
    'djsm',
    ....
]
  • In the command line/terminal run the management command;
python manage.py use_djsm

You're ready to go. The secrets manager will automatically be created (based on the configurations defined in the .env file) in your_project.settings.py and the project's secret key will now be stored and served by the secrets manager. You can use this manager to also handle something like your database key like so.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'NAME': 'myproject_database'.
            'USER': djsm_manager.get_secret('DB_USER'),
            'PASSWORD': djsm_manager.get_secret('DB_PASSWORD'),
            'HOST': 'localhost',
            'PORT': '3306',
        }
    }
}

You could also use optional flags when running the above command. For example;

python manage.py use_djsm --as "secrets_manager"

Lets you define the name of the djsm in settings.py. Also, you could the --quiet flag to prevent the manager created from printing setup/process logs to the console.

python manage.py use_djsm --as "secrets_manager" --quiet

Other CLI/Management commands

Apart from the use_djsm command, there are two other useful commands that can be run in your project.

  • Add to or update existing secrets
python manage.py update_secrets "<secret_name>" "<secret_value>"

For example:

python manage.py update_secrets 'api_key' '32456789456sdtfg'

However, you should note that the value passed must be a JSON formatted string. See an example of saving a mapping of my django project's contact detail below.

python manage.py update_secrets 'project_emails' '{\"project_email\": \"support@myproject.com\", \"admin_email\": \"admin@myproject.com\"}'
  • Change the encryption key used by the secrets manager.
python manage.py change_cryptkeys
  • Clean up secrets and environment variables set by the secrets manager.
python manage.py djsm_cleanup
  • Reload environment variables from .env file
python manage.py djsm_reload_env

Manually creating and using a secrets manager

import djsm

secrets_manager = djsm.get_djsm(quiet=False)

get_djsm returns a DJSM object instantiated using values defined in .env file after performing necessary checks.

You can set quiet=True if you do not want to see messages on the terminal. Although, important messages are always displayed.

To generate a new secret key or use and existing one. In settings.py:

SECRET_KEY = secrets_manager.get_or_create_secret_key()

To update or add a new secret:

new_secret = {"DB_PASSWORD": "db_password"}
secrets_manager.update_secrets(new_secret)

Once the update has been performed you can delete these lines.

To get a secret:

# Get a secret, say DB_PASSWORD
db_password = secrets_manager.get_secret("DB_PASSWORD")

You can also create a djsm object independent of the configuration in the .env file. Read the next section for more info.

DjangoJSONSecretManager

This class is the main class of the module. It provides the following methods:

  • get_secret(secret_name) -> Returns the secret with the name secret_name if it exists, otherwise returns None

  • update_secrets(new_secrets) -> Updates the secrets file with the new secrets provided in the new_secrets dictionary.

  • get_or_create_secret_key() -> Returns the Django secret key if it exists, otherwise generates a new one and returns it.

  • change_cryptkeys() -> Replaces the encryption keys used to encrypt and decrypt secrets with a new one.

  • clean_up() -> Deletes the secrets file and clears all environment variables set by the module.

  • reload_env() -> Reloads the environment variables from the .env file.

  • clean_up_and_reload() -> Calls the clean_up() and reload_env() methods.

from djsm import DJSM  # DJSM is an alias for DjangoJSONSecretManager

# Create a DJSM object
secrets_manager = DJSM('./.enc/secrets.json')

DO NOT DELETE cryptkeys.json or any of the encryption keys. IF YOU DO, ALL ENCRYPTED SECRET WILL BE LOST

NOTE: DJSM just provides an added layer of security in managing secrets in your application. It is not proven to be completely attack proof.

Contributions are welcome. Please fork the repository and submit a pull request.

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

djsm-0.1.51.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

djsm-0.1.51-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file djsm-0.1.51.tar.gz.

File metadata

  • Download URL: djsm-0.1.51.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for djsm-0.1.51.tar.gz
Algorithm Hash digest
SHA256 bd19ddef925d9108ee5dde2a0e1b5ced66b4367af0311456f76a8fd7250e1fd5
MD5 ed87333dc340b2156361f99b09fad1a7
BLAKE2b-256 a0473f731e00b0df315ac1a5aa661b39305be03ee485d2089581f5e36acffba3

See more details on using hashes here.

File details

Details for the file djsm-0.1.51-py3-none-any.whl.

File metadata

  • Download URL: djsm-0.1.51-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for djsm-0.1.51-py3-none-any.whl
Algorithm Hash digest
SHA256 83f1569f7cd8604ea58a3487d4f641513cffe67541c46ce688187fc883235690
MD5 b33e6f2920422c0d8db7a86f662dbe25
BLAKE2b-256 14b2ec9c386176c7147437d72aa1d37b72b67f67c8a28ca0e6a2c4e0deafa6a7

See more details on using hashes here.

Supported by

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