2020-06-11 00:54:34 +02:00
|
|
|
import configparser
|
2019-11-13 01:11:56 +01:00
|
|
|
import os
|
2024-07-12 02:30:23 +02:00
|
|
|
from typing import overload
|
2024-03-05 17:22:49 +01:00
|
|
|
|
|
|
|
from scripts.lib.zulip_tools import get_config as get_config_from_file
|
2019-11-13 01:11:56 +01:00
|
|
|
|
2022-08-12 04:41:13 +02:00
|
|
|
|
2022-09-24 06:50:09 +02:00
|
|
|
class ZulipSettingsError(Exception):
|
2022-08-12 04:41:13 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-11-13 01:11:56 +01:00
|
|
|
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.
|
2021-02-12 08:20:45 +01:00
|
|
|
PRODUCTION = config_file.has_option("machine", "deploy_type")
|
2019-11-13 01:11:56 +01:00
|
|
|
DEVELOPMENT = not PRODUCTION
|
|
|
|
secrets_file = configparser.RawConfigParser()
|
2023-05-30 00:01:44 +02:00
|
|
|
if PRODUCTION: # nocoverage
|
2019-11-13 01:11:56 +01:00
|
|
|
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(
|
2024-03-05 17:22:49 +01:00
|
|
|
key: str, default_value: None = None, development_only: bool = False
|
2024-07-12 02:30:23 +02:00
|
|
|
) -> str | None: ...
|
2024-03-05 17:22:49 +01:00
|
|
|
@overload
|
|
|
|
def get_secret(key: str, default_value: str, development_only: bool = False) -> str: ...
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_secret(
|
2024-07-12 02:30:23 +02:00
|
|
|
key: str, default_value: str | None = None, development_only: bool = False
|
|
|
|
) -> str | None:
|
2023-05-30 00:01:44 +02:00
|
|
|
if development_only and PRODUCTION: # nocoverage
|
2019-11-13 01:11:56 +01:00
|
|
|
return default_value
|
2021-02-12 08:20:45 +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
|
|
|
|
2022-08-12 04:41:13 +02:00
|
|
|
def get_mandatory_secret(key: str) -> str:
|
|
|
|
secret = get_secret(key)
|
|
|
|
if secret is None:
|
|
|
|
if os.environ.get("DISABLE_MANDATORY_SECRET_CHECK") == "True":
|
|
|
|
return ""
|
|
|
|
raise ZulipSettingsError(f'Mandatory secret "{key}" is not set')
|
|
|
|
return secret
|
|
|
|
|
|
|
|
|
2024-03-05 17:22:49 +01:00
|
|
|
@overload
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_config(section: str, key: str, default_value: None = None) -> str | None: ...
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
2024-01-29 00:32:21 +01:00
|
|
|
def get_config(section: str, key: str, default_value: str) -> str: ...
|
2019-11-13 01:11:56 +01:00
|
|
|
@overload
|
2024-03-05 17:22:49 +01:00
|
|
|
def get_config(section: str, key: str, default_value: bool) -> bool: ...
|
|
|
|
def get_config(
|
2024-07-12 02:30:23 +02:00
|
|
|
section: str, key: str, default_value: str | bool | None = None
|
|
|
|
) -> str | bool | None:
|
2024-03-05 17:22:49 +01:00
|
|
|
return get_config_from_file(config_file, section, key, 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:
|
2021-02-12 08:20:45 +01:00
|
|
|
return ""
|