Skip to content

Commit

Permalink
generate_secrets: Lazily import crypto module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Howell committed Apr 30, 2020
1 parent 66392af commit 522ee7f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions scripts/setup/generate_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'

from django.utils.crypto import get_random_string
import argparse
import uuid
import configparser
Expand All @@ -30,12 +29,17 @@
'thumbor_key',
]

def random_string(cnt: int) -> str:
from django.utils.crypto import get_random_string
return get_random_string(cnt)

def random_token() -> str:
from zerver.lib.utils import generate_random_token
return generate_random_token(64)

def generate_django_secretkey() -> str:
"""Secret key generation taken from Django's startproject.py"""
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars)

Expand Down Expand Up @@ -82,7 +86,7 @@ def add_secret(name: str, value: str) -> None:
settings.SECRET_KEY = secret_key

if need_secret('camo_key'):
add_secret('camo_key', get_random_string(64))
add_secret('camo_key', random_string(64))

if (
not development
Expand Down Expand Up @@ -126,7 +130,7 @@ def add_secret(name: str, value: str) -> None:
# zulip_org_id does not require a secure CPRNG,
# it only needs to be unique.
if need_secret('zulip_org_key'):
add_secret('zulip_org_key', get_random_string(64))
add_secret('zulip_org_key', random_string(64))
if need_secret('zulip_org_id'):
add_secret('zulip_org_id', str(uuid.uuid4()))

Expand Down

0 comments on commit 522ee7f

Please sign in to comment.