py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-07-20 05:45:50 +02:00
|
|
|
# This tools generates /etc/zulip/zulip-secrets.conf
|
2013-10-25 19:35:24 +02:00
|
|
|
|
2017-01-24 06:36:39 +01:00
|
|
|
import sys
|
|
|
|
import os
|
2016-11-30 21:45:02 +01:00
|
|
|
if False:
|
2018-05-10 18:54:59 +02:00
|
|
|
from typing import Dict, List, Optional
|
2016-06-25 17:07:13 +02:00
|
|
|
|
2017-09-22 08:15:01 +02:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2016-06-25 17:07:13 +02:00
|
|
|
sys.path.append(BASE_DIR)
|
|
|
|
import scripts.lib.setup_path_on_import
|
2013-10-25 19:35:24 +02:00
|
|
|
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
|
|
|
|
|
|
|
|
from django.utils.crypto import get_random_string
|
2016-10-05 11:13:19 +02:00
|
|
|
import argparse
|
2016-10-28 00:04:05 +02:00
|
|
|
import uuid
|
2017-09-27 10:16:35 +02:00
|
|
|
import configparser
|
2016-07-30 00:29:54 +02:00
|
|
|
from zerver.lib.str_utils import force_str
|
2013-10-25 19:35:24 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
|
|
|
|
2013-11-14 14:59:33 +01:00
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
|
|
|
2013-11-10 16:04:24 +01:00
|
|
|
CAMO_CONFIG_FILENAME = '/etc/default/camo'
|
2013-10-25 19:35:24 +02:00
|
|
|
|
2017-05-17 07:02:02 +02:00
|
|
|
# Standard, 64-bit tokens
|
|
|
|
AUTOGENERATED_SETTINGS = [
|
|
|
|
'avatar_salt',
|
|
|
|
'initial_password_salt',
|
|
|
|
'local_database_password',
|
|
|
|
'rabbitmq_password',
|
|
|
|
'shared_secret',
|
2017-11-02 19:48:29 +01:00
|
|
|
'thumbor_key',
|
2017-05-17 07:02:02 +02:00
|
|
|
]
|
2013-10-25 19:35:24 +02:00
|
|
|
|
2016-07-31 08:29:21 +02:00
|
|
|
# TODO: We can eliminate this function if we refactor the install
|
|
|
|
# script to run generate_secrets before zulip-puppet-apply.
|
2013-10-25 19:35:24 +02:00
|
|
|
def generate_camo_config_file(camo_key):
|
2018-05-10 18:54:59 +02:00
|
|
|
# type: (str) -> None
|
2013-10-25 19:35:24 +02:00
|
|
|
camo_config = """ENABLED=yes
|
|
|
|
PORT=9292
|
|
|
|
CAMO_KEY=%s
|
|
|
|
""" % (camo_key,)
|
|
|
|
with open(CAMO_CONFIG_FILENAME, 'w') as camo_file:
|
|
|
|
camo_file.write(camo_config)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Generated Camo config file %s" % (CAMO_CONFIG_FILENAME,))
|
2013-10-25 19:35:24 +02:00
|
|
|
|
|
|
|
def generate_django_secretkey():
|
2018-05-10 18:54:59 +02:00
|
|
|
# type: () -> str
|
2016-07-30 00:29:54 +02:00
|
|
|
"""Secret key generation taken from Django's startproject.py"""
|
2013-10-25 19:35:24 +02:00
|
|
|
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
|
|
|
return get_random_string(50, chars)
|
|
|
|
|
2016-06-20 14:00:27 +02:00
|
|
|
def get_old_conf(output_filename):
|
2018-05-10 18:54:59 +02:00
|
|
|
# type: (str) -> Dict[str, str]
|
2018-05-07 07:12:32 +02:00
|
|
|
if not os.path.exists(output_filename) or os.path.getsize(output_filename) == 0:
|
2016-06-20 14:00:27 +02:00
|
|
|
return {}
|
|
|
|
|
2017-09-27 10:16:35 +02:00
|
|
|
secrets_file = configparser.RawConfigParser()
|
2017-05-17 09:05:57 +02:00
|
|
|
secrets_file.read(output_filename)
|
2016-06-20 14:00:27 +02:00
|
|
|
|
2017-08-25 20:01:20 +02:00
|
|
|
return dict(secrets_file.items("secrets"))
|
2016-06-20 14:00:27 +02:00
|
|
|
|
2015-08-20 08:15:21 +02:00
|
|
|
def generate_secrets(development=False):
|
2016-07-30 00:29:54 +02:00
|
|
|
# type: (bool) -> None
|
2015-08-21 01:46:52 +02:00
|
|
|
if development:
|
|
|
|
OUTPUT_SETTINGS_FILENAME = "zproject/dev-secrets.conf"
|
|
|
|
else:
|
|
|
|
OUTPUT_SETTINGS_FILENAME = "/etc/zulip/zulip-secrets.conf"
|
2017-05-17 07:02:24 +02:00
|
|
|
current_conf = get_old_conf(OUTPUT_SETTINGS_FILENAME)
|
2015-08-21 01:46:52 +02:00
|
|
|
|
2018-05-10 18:54:59 +02:00
|
|
|
lines = [] # type: List[str]
|
2017-05-17 07:02:24 +02:00
|
|
|
if len(current_conf) == 0:
|
2017-11-04 05:19:06 +01:00
|
|
|
lines = ['[secrets]\n']
|
2015-08-20 08:15:21 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
def need_secret(name):
|
2017-05-17 08:28:42 +02:00
|
|
|
# type: (str) -> bool
|
2017-05-17 07:02:24 +02:00
|
|
|
return name not in current_conf
|
2015-08-20 08:15:21 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
def add_secret(name, value):
|
2018-05-10 18:54:59 +02:00
|
|
|
# type: (str, str) -> None
|
2017-05-17 07:02:24 +02:00
|
|
|
lines.append("%s = %s\n" % (name, value))
|
|
|
|
current_conf[name] = value
|
2016-06-20 14:00:27 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
for name in AUTOGENERATED_SETTINGS:
|
|
|
|
if need_secret(name):
|
|
|
|
add_secret(name, generate_random_token(64))
|
2015-08-20 08:15:21 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
if need_secret('secret_key'):
|
|
|
|
add_secret('secret_key', generate_django_secretkey())
|
2016-06-20 14:00:27 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
if need_secret('camo_key'):
|
|
|
|
add_secret('camo_key', get_random_string(64))
|
2016-10-28 00:04:05 +02:00
|
|
|
|
2017-07-11 21:12:08 +02:00
|
|
|
# zulip_org_key is generated using os.urandom().
|
|
|
|
# zulip_org_id does not require a secure CPRNG,
|
|
|
|
# it only needs to be unique.
|
2017-05-17 07:02:24 +02:00
|
|
|
if need_secret('zulip_org_key'):
|
|
|
|
add_secret('zulip_org_key', get_random_string(64))
|
|
|
|
if need_secret('zulip_org_id'):
|
|
|
|
add_secret('zulip_org_id', str(uuid.uuid4()))
|
2016-10-28 00:04:05 +02:00
|
|
|
|
2015-08-20 08:15:21 +02:00
|
|
|
if not development:
|
|
|
|
# Write the Camo config file directly
|
2017-05-17 07:02:24 +02:00
|
|
|
generate_camo_config_file(current_conf['camo_key'])
|
|
|
|
|
|
|
|
if len(lines) == 0:
|
|
|
|
print("generate_secrets: No new secrets to generate.")
|
|
|
|
return
|
2015-08-20 08:15:21 +02:00
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
out = open(OUTPUT_SETTINGS_FILENAME, 'a')
|
2017-06-04 08:13:59 +02:00
|
|
|
# Write a newline at the start, in case there was no newline at
|
|
|
|
# the end of the file due to human editing.
|
|
|
|
out.write("\n" + force_str("".join(lines)))
|
2013-10-25 19:35:24 +02:00
|
|
|
out.close()
|
|
|
|
|
2017-05-17 07:02:24 +02:00
|
|
|
print("Generated new secrets in %s." % (OUTPUT_SETTINGS_FILENAME,))
|
2015-08-20 08:15:21 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2016-10-05 11:13:19 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
2017-10-27 02:31:10 +02:00
|
|
|
group.add_argument('--development', action='store_true', dest='development',
|
|
|
|
help='For setting up the developer env for zulip')
|
|
|
|
group.add_argument('--production', action='store_false', dest='development',
|
|
|
|
help='For setting up the production env for zulip')
|
2016-10-05 11:13:19 +02:00
|
|
|
results = parser.parse_args()
|
2015-08-20 08:15:21 +02:00
|
|
|
|
2016-10-05 11:13:19 +02:00
|
|
|
generate_secrets(results.development)
|