2020-06-11 00:54:34 +02:00
|
|
|
import configparser
|
2019-11-13 01:11:56 +01:00
|
|
|
import os
|
|
|
|
from typing import Optional, overload
|
|
|
|
|
|
|
|
DEPLOY_ROOT = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
|
|
|
|
config_file = configparser.RawConfigParser()
|
|
|
|
config_file.read("/etc/zulip/zulip.conf")
|
|
|
|
|
|
|
|
# Whether this instance of Zulip is running in a production environment.
|
|
|
|
PRODUCTION = config_file.has_option('machine', 'deploy_type')
|
|
|
|
DEVELOPMENT = not PRODUCTION
|
|
|
|
|
|
|
|
secrets_file = configparser.RawConfigParser()
|
|
|
|
if PRODUCTION:
|
|
|
|
secrets_file.read("/etc/zulip/zulip-secrets.conf")
|
|
|
|
else:
|
|
|
|
secrets_file.read(os.path.join(DEPLOY_ROOT, "zproject/dev-secrets.conf"))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_secret(key: str, default_value: str, development_only: bool = False) -> str:
|
2019-11-13 01:11:56 +01:00
|
|
|
...
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_secret(
|
|
|
|
key: str, default_value: Optional[str] = None, development_only: bool = False
|
|
|
|
) -> Optional[str]:
|
2019-11-13 01:11:56 +01:00
|
|
|
...
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_secret(
|
|
|
|
key: str, default_value: Optional[str] = None, development_only: bool = False
|
|
|
|
) -> Optional[str]:
|
2019-11-13 01:11:56 +01:00
|
|
|
if development_only and PRODUCTION:
|
|
|
|
return default_value
|
2019-12-05 05:34:13 +01:00
|
|
|
return secrets_file.get('secrets', key, fallback=default_value)
|
2019-11-13 01:11:56 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
|
|
|
def get_config(section: str, key: str, default_value: str) -> str:
|
|
|
|
...
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_config(section: str, key: str, default_value: Optional[str] = None) -> Optional[str]:
|
2019-11-13 01:11:56 +01:00
|
|
|
...
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_config(section: str, key: str, default_value: Optional[str] = None) -> Optional[str]:
|
2019-12-05 05:34:13 +01:00
|
|
|
return config_file.get(section, key, fallback=default_value)
|
2019-11-13 01:11:56 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
def get_from_file_if_exists(path: str) -> str:
|
|
|
|
if os.path.exists(path):
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(path) as f:
|
2019-11-13 01:11:56 +01:00
|
|
|
return f.read()
|
|
|
|
else:
|
|
|
|
return ''
|