Make generate-secrets script use existing values.

Fixes #1035
This commit is contained in:
Umair Khan 2016-06-20 17:00:27 +05:00
parent aceee3da11
commit a9a6687b7d
1 changed files with 24 additions and 3 deletions

View File

@ -8,6 +8,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
import six
from zerver.lib.utils import generate_random_token from zerver.lib.utils import generate_random_token
os.chdir(os.path.join(os.path.dirname(__file__), '..', '..')) os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
@ -31,6 +33,21 @@ def generate_django_secretkey():
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars) return get_random_string(50, chars)
def get_old_conf(output_filename):
if not os.path.exists(output_filename):
return {}
secrets_file = six.moves.configparser.RawConfigParser()
secrets_file.read(output_filename)
def get_secret(key):
if secrets_file.has_option('secrets', key):
return secrets_file.get('secrets', key)
return None
fields = AUTOGENERATED_SETTINGS + ['secret_key', 'camo_key']
return {name: get_secret(name) for name in fields}
def generate_secrets(development=False): def generate_secrets(development=False):
if development: if development:
OUTPUT_SETTINGS_FILENAME = "zproject/dev-secrets.conf" OUTPUT_SETTINGS_FILENAME = "zproject/dev-secrets.conf"
@ -42,12 +59,16 @@ def generate_secrets(development=False):
def config_line(var, value): def config_line(var, value):
return "%s = %s\n" % (var, value) return "%s = %s\n" % (var, value)
old_conf = get_old_conf(OUTPUT_SETTINGS_FILENAME)
for name in AUTOGENERATED_SETTINGS: for name in AUTOGENERATED_SETTINGS:
lines.append(config_line(name, generate_random_token(64))) lines.append(config_line(name, old_conf.get(name, generate_random_token(64))))
lines.append(config_line('secret_key', generate_django_secretkey())) secret_key = old_conf.get('secret_key', generate_django_secretkey())
camo_key = get_random_string(64) lines.append(config_line('secret_key', secret_key))
camo_key = old_conf.get('camo_key', get_random_string(64))
lines.append(config_line('camo_key', camo_key)) lines.append(config_line('camo_key', camo_key))
if not development: if not development:
# Write the Camo config file directly # Write the Camo config file directly
generate_camo_config_file(camo_key) generate_camo_config_file(camo_key)