Skip to main content

A simple Django app to store secret keys outside of settings.py

Project description

django-autosecretkey

autosecretkey is a simple reusable Django app which will make it easier to distribute your Django project by taking care of generating a secure SECRET_KEY and storing it outside of your project's settings.py

Quick start

  1. In your project's settings.py, import the app like so:

    from autosecretkey import AutoSecretKey
    
  2. Still in the settings.py file, replace the existing SECRET_KEY line with something like this:

    SECRET_KEY = AutoSecretKey(BASE_DIR / "config.ini").secret_key
    

    (N.B.: Of course, this line has to be after the BASE_DIR line.)

    This will store the key in a file called config.ini in your project's base directory (i.e. the one containing manage.py).

    Make sure not to ship this file with your code distribution. For example, you may want to add it to your .gitignore file if you publish the project in a git repository.

Additional configuration

For additional security, you may want to store your secret key in a different location than your project's base directory. You could, for example, do something like this:

AutoSecretKey("/etc/your_project/config.ini")

You need to manually make sure that the user your Django project runs as has the permission to read and write this file. Running something like this as root should do the trick in Linux (replacing "djangouser" with the actual user name):

mkdir /etc/your_project/
touch /etc/your_project/configuration
chown djangouser /etc/your_project/configuration

In the end, this is just a simple wrapper around configparser.ConfigParser, so you can store custom configuration values in the file that holds your secret key. You can access the ConfigParser object as the config attribute of your AutoSecretKey object.

This is a simple example you could have in your settings.py:

from autosecretkey import AutoSecretKey
my_config_file = AutoSecretKey(BASE_DIR / "config.ini")
SECRET_KEY = my_config_file.secret_key
TIME_ZONE = my_config_file.config["MY_SETTINGS"]["TIME_ZONE"]

For reference, the corresponding config.ini might look like this:

[AutoSecretKey]
SecretKey = WellThisIsWhereYouWillFindYourSecretKey

[MY_SETTINGS]
TIME_ZONE = UTC

You can pass the path to an .ini file to use as a template when first creating the file creating your secret key. This file may contain any additional settings you want to have in your config file, the SecretKey will then be added to that. Note that you must not define a secret key within that template file.

AutoSecretKey("/etc/myproject/config.ini", template=BASE_DIR/"config.dist.ini")

You can also set the create attribute to False if you need to make sure the config file already exists - you may want to use this to make sure that custom settings have already been made. If the file exists but no secret key is defined within it, a new secret key will be added to the file.

AutoSecretKey("config.ini", create=False)

All methods you can use on any other ConfigParser object can be used on that object as well, of course, like get(), getboolean(), etc. For convenience, you can use the AutoSecretKey object's update() method to re-read the contents of the config file, and the write() method to write back any changes you have made on the object to the configuration file.

Note that the ConfigParser behaves like a RawConfigParser in that it does not support interpolation.

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-autosecretkey-0.9.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

django_autosecretkey-0.9.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django-autosecretkey-0.9.2.tar.gz.

File metadata

  • Download URL: django-autosecretkey-0.9.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django-autosecretkey-0.9.2.tar.gz
Algorithm Hash digest
SHA256 29e8411afe47ffcab435df998117c34d9d49b24a9b0ac3ba13baa820cf1b0020
MD5 5266b209fcef10a710a50c65f11dab55
BLAKE2b-256 6408bcdd7b9710eb00394efc89b7db62271af1227a0dc32e462238e2a2d21119

See more details on using hashes here.

File details

Details for the file django_autosecretkey-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: django_autosecretkey-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django_autosecretkey-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 48b5048acd24795be37416a229579a4377f525dfc292fcb40e39deef60a18183
MD5 e53115a2abb10e04ce0f8fcbaa88c175
BLAKE2b-256 3d52b5c7b62bfa678b4e28035ae20bd5e8a0292fdd9cfb9398e12fe5f2774d7c

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