Skip to main content

Django SecretsManager is custom secret managers for Django

Project description

Django SecretsManager

Django SecretsManager is a package that helps you manage the secret values used by Django through variable services.

Requirements

  • Python >= 3.6
  • Django

Required settings for the settings module

  • AWS_SECRETS_MANAGER_SECRET_NAME (or AWS_SECRET_NAME)
    • Secret name of SecretsManager to use
  • AWS_SECRETS_MANAGER_SECRET_SECTION (or AWS_SECRET_SECTION)
    • The key that separates JSON objects by colons.
      ex) In the example below, the "production" item is represented as "sample-project:production".
  • AWS_SECRETS_MANAGER_REGION_NAME (or AWS_REGION_NAME)
    • Region of the SecretsManager service to use
      ex) ap-northeast-2

Secret value setting of AWS SecretsManager

SecretsManager's Secret value uses JSON format in Plaintext.
Here is an example Secret value to use for configuration, and the Secret (Corresponds to AWS_SECRETS_MANAGER_SECRET_NAME in the settings module) is named sample-project-secret

{
  "sample-project(Recommend the name of django project)": {
    "base(If the settings module is a package, submodule names are recommended)": {
      "SECRET_KEY": "DjangoSecretKey"
    },
    "dev": {
      "AWS_S3_BUCKET_NAME": "sample-s3-dev"
    },
    "production": {
      "AWS_S3_BUCKET_NAME": "sample-s3-production"
    }
  }
}

Setting up AWS Credentials for Django to use

Django uses two methods to access the SecretsManager on AWS. The first uses a profile of ~/.aws/credentials in your home folder, and the second uses an environment variable.

1. Using the AWS Credentials Profile

Recommended for use in development environments

Set Profile of IAM User with SecretsManagerReadWrite Permission to ~/.aws/credentials. The following example uses the profile name sample-project-secretsmanager

[sample-project-secretsmanager]
aws_access_key_id = AKI*************
aws_secret_access_key = Mlp********************

Then enter the profile name in AWS_SECRETS_MANAGER_PROFILE (or AWS_PROFILE) of the settings module.

# settings.py
AWS_SECRETS_MANAGER_PROFILE = 'sample-project-secrets-manager'

2. Use environment variables

It is recommended to use in distribution or CI / CD environment.

If you set the following values in the environment variable, the contents are used to use the SecretsManager service.

  • AWS_SECRETS_MANAGER_ACCESS_KEY_ID (or AWS_ACCESS_KEY_ID)
  • AWS_SECRETS_MANAGER_SECRET_ACCESS_KEY (or AWS_SECRET_ACCESS_KEY)

Using Secrets in Django's Settings Module

  1. First, import the SECRETS instance of the library.
  2. Enter the settings for Django AWS SecretsManager
  3. Use SECRETS as a dictionary to get the secrets you want

Follow the form of the example below

By separating the settings module into packages, it is assumed that there are base and dev submodules.

settings/
    __init__.py
    base.py
    dev.py
## settings/base.py

# 1. Import the SECRETS instance of the library
from django_secrets import SECRETS

# 2. Enter the settings for Django AWS SecretsManager
AWS_SECRETS_MANAGER_SECRET_NAME = 'sample-project-secret'
AWS_SECRETS_MANAGER_PROFILE = 'sample-project-secretsmanager'
AWS_SECRETS_MANAGER_SECRET_SECTION = 'sample-project:base'
AWS_SECRETS_MANAGER_REGION_NAME = 'ap-northeast-2'

# 3. Use SECRETS as a dictionary to get the secrets you want
SECRET_KEY = SECRETS['SECRET_KEY']
SECRET_KEY = SECRETS.get('SECRET_KEY')
## settings/dev.py

# The SECRETS instance is already imported from the base module.
from .base import *

# Use a different secrets section
AWS_SECRETS_MANAGER_SECRET_SECTION = 'sample-project:dev'

# Use SECRETS as a dictionary to get the secrets you want
AWS_STORAGE_BUCKET_NAME = SECRETS['AWS_STORAGE_BUCKET_NAME']
AWS_STORAGE_BUCKET_NAME = SECRETS.get('AWS_STORAGE_BUCKET_NAME', 'default')

Contributing

As an open source project, we welcome contributions.
The code lives on GitHub

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-secrets-manager-0.1.10.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

django_secrets_manager-0.1.10-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django-secrets-manager-0.1.10.tar.gz.

File metadata

  • Download URL: django-secrets-manager-0.1.10.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for django-secrets-manager-0.1.10.tar.gz
Algorithm Hash digest
SHA256 8f6b3e64bd7dcc499b633a94a9b2be4144d24247db959a90965a72d7906191f5
MD5 76883bf3745196629c53eef641032cc0
BLAKE2b-256 2515daa02c7f03b840fe29185b1666eee54d7d99ce39042eb3c9eba2cdd55749

See more details on using hashes here.

File details

Details for the file django_secrets_manager-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: django_secrets_manager-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for django_secrets_manager-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 ce760d540f68f5ebc1a9c575f5c29b0c10e9528bc2945504418deef2a2c3f1e8
MD5 c1ed93f19c9a26e21bcf3996136dfcf9
BLAKE2b-256 4f9ecc692f9c32d16aaa333718d0af019772ba28672c181765d7961f76d85207

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