The random module uses the Mersenne Twister pseudorandom number
generator and is not a cryptographically secure random number
generator[0]. The secrets[1] module is intended for generating
cryptographically strong random numbers, so recommend using that to
generate the secret key. It's new in Python 3, so if it's unavailable
fall back to using the ``os.urandom()`` backed implementation of random.
NOTE(stephenfin): Modified to include change to 'config.yaml'. Also
renamed reno to just stick with hyphens for filenames.
[0] https://docs.python.org/3/library/random.html
[1] https://docs.python.org/3/library/secrets.html
Signed-off-by: Jeremy Cline <jcline@redhat.com>
Signed-off-by: Stephen Finucane <stephen@that.guru>
.. code-block:: python
- import string, random
+ import string
+ try:
+ import secrets
+ except ImportError: # Python < 3.6
+ import random
+ secrets = random.SystemRandom()
+
chars = string.ascii_letters + string.digits + string.punctuation
- print(repr("".join([random.choice(chars) for i in range(0,50)])))
+ print("".join([secrets.choice(chars) for i in range(50)]))
Once again, store this in ``production.py``.
# You'll need to replace this to a random string. The following python code can
# be used to generate a secret key:
#
-# import string, random
-# chars = string.letters + string.digits + string.punctuation
-# print repr("".join([random.choice(chars) for i in range(0,50)]))
+# import string
+# try:
+# import secrets
+# except ImportError: # Python < 3.6
+# import random
+# secrets = random.SystemRandom()
+#
+# chars = string.ascii_letters + string.digits + string.punctuation
+# print("".join([secrets.choice(chars) for i in range(50)]))
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
- [deprecations, Deprecation Notes]
- [fixes, Bug Fixes]
- [api, API Changes]
+ - [security, Security Notes]
- [other, Other Notes]
--- /dev/null
+---
+security:
+ - |
+ Change the recommended method for generating the Django secret key to use a
+ cryptographically secure random number generator.