Skip to main content

Rotate your Django secret

Project description

Helps rotating your secret keys safely without losing user sessions, which means without logging users out.

Compatible with modern Django versions. At the moment of writing that’s including 1.11 and 2.0 on Python 3.7.

I haven’t found any library to allow us to do this on our production website, so I started the library.

Problem

Once you change the SECRET_KEY on production, all the old sessions and cookies are invalidated, users are logged out and data in sessions are lost.

This is good if your SECRET_KEY is compromised! But not good if you just want to rotate in a regular schedule for security purposes.

This library allows you to continue supporting old sessions signed with your old secret key, while rewriting them with the new secret key if the user comes to the website.

So optimal schedule would be

  • you decide to rotate your secret key

  • Install django-rotate-secret-key and configure

  • Support both keys for a limited time (x months)

  • Roll back django-rotate-secret-key and keep your secret key the same (removing the old one)

If a user comes back to the website after x months, his session will be invalidated. But for all the regular users this should be seamless transition.

Compatibility

If you are using JWT tokens created by django-rest-framework-jwt, currently package is not compatible. But it will be supported in version 1.0.

Getting It

$ pip install django-rotate-secret-key

Installing It

This is safe to do even before you decide to rotate your keys, it basically has no effect before you change the settings.:

INSTALLED_APPS = (
    ...
    'rotatesecretkey',
    ...
)

Settings

Replace AuthenticationMiddleware with RotateAuthenticationMiddleware:

MIDDLEWARE = [
    ...
    # 'django.contrib.auth.middleware.AuthenticationMiddleware',
    'rotatesecretkey.middleware.RotateAuthenticationMiddleware',
    ...
]

Replace SESSION_ENGINE:

SESSION_ENGINE = 'rotatesecretkey.sessions'

Add the old secret key into OLD_SECRET_KEY, and create a new SECRET_KEY.:

SECRET_KEY = 'NEWRANDOMKEY'
OLD_SECRET_KEY = 'your_previous_secret_key_that_you_want_to_support'

Once these changes go live your website will decode old sessions with the OLD_SECRET_KEY and resign them with the new SECRET_KEY.

After some time (like 1 or 2 months) you should roll these changes back and just keep the SECRET_KEY.:

SECRET_KEY = 'NEWRANDOMKEY'

You don’t want to support OLD_SECRET_KEY forever but long enough to give your visitors a chance to visit the website and rewrite their sessions with the new key.

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-rotate-secret-key-0.3.tar.gz (3.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_rotate_secret_key-0.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file django-rotate-secret-key-0.3.tar.gz.

File metadata

  • Download URL: django-rotate-secret-key-0.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1

File hashes

Hashes for django-rotate-secret-key-0.3.tar.gz
Algorithm Hash digest
SHA256 9da44de619567a9a12364eca2cd007d61e74ad110ef04a5f87b56db122ff2749
MD5 dd4b540a92add254680d6eceafceecba
BLAKE2b-256 d291beb9a5aecb740546d020779ec14a8bbcf496072e4653800c03a719d21073

See more details on using hashes here.

File details

Details for the file django_rotate_secret_key-0.3-py3-none-any.whl.

File metadata

  • Download URL: django_rotate_secret_key-0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1

File hashes

Hashes for django_rotate_secret_key-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e1948aef8d9cb2d4731235d44e9af15765e9258b1e486c2f2faa0c2ff0484074
MD5 97be2f27b24441d300d980031c8e0e46
BLAKE2b-256 de3c9515ce83cc5804607993e372410b2a1d8ddf57cea6eaa27ad275d9b92edb

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