Revamp config.py, add signing_key_path
This commit is contained in:
@@ -20,6 +20,7 @@ client = Blueprint("c2s", __name__)
|
|||||||
@client.route("/_matrix/client/r0/delete_devices", methods=["POST"])
|
@client.route("/_matrix/client/r0/delete_devices", methods=["POST"])
|
||||||
@client.route("/_matrix/client/v3/logout/all", methods=["POST"])
|
@client.route("/_matrix/client/v3/logout/all", methods=["POST"])
|
||||||
@client.route("/_matrix/client/v3/logout", methods=["POST"])
|
@client.route("/_matrix/client/v3/logout", methods=["POST"])
|
||||||
|
@client.route("/_matrix/client/r0/logout", methods=["POST"])
|
||||||
@client.route("/_matrix/client/v3/rooms/<room>/invite", methods=["POST"])
|
@client.route("/_matrix/client/v3/rooms/<room>/invite", methods=["POST"])
|
||||||
@client.route("/_matrix/client/v3/rooms/<roomId>/leave", methods=["POST"])
|
@client.route("/_matrix/client/v3/rooms/<roomId>/leave", methods=["POST"])
|
||||||
@client.route("/_matrix/client/r0/rooms/<roomId>/leave", methods=["POST"])
|
@client.route("/_matrix/client/r0/rooms/<roomId>/leave", methods=["POST"])
|
||||||
|
|||||||
160
src/config.py
160
src/config.py
@@ -1,84 +1,124 @@
|
|||||||
import mimetypes
|
|
||||||
import tomllib
|
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import tomllib
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
# Default values
|
addr: str = "127.0.0.1"
|
||||||
addr = "127.0.0.1"
|
port: int = 5000
|
||||||
port = 5000
|
allow_registration: bool = False
|
||||||
allow_registration = False
|
the_funny_number: int = 1337
|
||||||
the_funny_number = 1337
|
cat: str = "/etc/vona/cat.jpg"
|
||||||
cat = "/etc/vona/cat.jpg"
|
|
||||||
|
|
||||||
server_name = ""
|
server_name: str = ""
|
||||||
signing_key = ""
|
signing_key: str = ""
|
||||||
|
support: dict = {"contacts": []}
|
||||||
|
|
||||||
_config_path = "/etc/vona/config.toml"
|
_CONFIG_PATH = Path("/etc/vona/config.toml")
|
||||||
|
|
||||||
|
|
||||||
|
def _fatal(msg: str) -> None:
|
||||||
|
print(f"[FATL] {msg}")
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def _warn(msg: str) -> None:
|
||||||
|
print(f"[WARN] {msg}")
|
||||||
|
|
||||||
|
|
||||||
|
def _load_toml(path: Path) -> dict:
|
||||||
try:
|
try:
|
||||||
with open(_config_path, "rb") as f:
|
with path.open("rb") as f:
|
||||||
_config = tomllib.load(f)
|
return tomllib.load(f)
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"[FATL] Configuration file not found at {_config_path}")
|
_fatal(f"[FATL] Configuration file not found at {path}")
|
||||||
os._exit(1)
|
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
print(f"[FATL] Permission denied when accessing configuration")
|
_fatal(f"[FATL] Permission denied when accessing configuration {path}")
|
||||||
os._exit(1)
|
|
||||||
except tomllib.TOMLDecodeError as e:
|
except tomllib.TOMLDecodeError as e:
|
||||||
print(f"[FATL] Invalid TOML configuration: {e}")
|
_fatal(f"[FATL] Invalid TOML configuration: {e}")
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
if "address" in _config:
|
def _read_signing_key_from_path(path_value) -> str | None:
|
||||||
addr = _config["address"]
|
p = Path(path_value)
|
||||||
|
if not p.exists():
|
||||||
|
_fatal(f"[FATL] signing_key_path {p} does not exist")
|
||||||
|
try:
|
||||||
|
return p.read_text(encoding="utf-8").strip()
|
||||||
|
except Exception as e:
|
||||||
|
_fatal(f"[FATL] Failed to read signing_key_path {p}: {e}")
|
||||||
|
|
||||||
|
|
||||||
if "allow_registration" in _config:
|
def _validate_cat_path(cat_path: str) -> Path:
|
||||||
allow_registration = _config["allow_registration"]
|
p = Path(cat_path)
|
||||||
|
if not p.exists():
|
||||||
|
_fatal(f"[FATL] Cat photo at {p} does not exist")
|
||||||
|
|
||||||
|
mtype, _ = mimetypes.guess_type(str(p))
|
||||||
|
if mtype is None or not mtype.startswith("image/"):
|
||||||
|
_warn(f"[WARN] Cat file {p} does not look like an image (mimetype={mtype})")
|
||||||
|
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
if "server_name" in _config:
|
def _apply_config(cfg: dict) -> None:
|
||||||
server_name = _config["server_name"]
|
global addr, port, allow_registration, server_name, signing_key, cat, support
|
||||||
else:
|
|
||||||
print("[FATL] `server_name` is not in configuration")
|
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
|
if "address" in cfg:
|
||||||
|
addr = str(cfg["address"])
|
||||||
|
|
||||||
if "signing_key" in _config:
|
if "port" in cfg:
|
||||||
signing_key = _config["signing_key"]
|
try:
|
||||||
else:
|
port = int(cfg["port"])
|
||||||
print(
|
except (TypeError, ValueError):
|
||||||
"[FATL] `signing_key` is not in configuration."
|
_warn(
|
||||||
+ " A signing key can be generated using `cmd/generate_key.py`."
|
f"[WARN] Invalid port in config: {cfg.get('port')}; using default {port}"
|
||||||
)
|
)
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
|
if "allow_registration" in cfg:
|
||||||
|
allow_registration = bool(cfg["allow_registration"])
|
||||||
|
|
||||||
if "cat" in _config:
|
if "server_name" in cfg:
|
||||||
cat = _config["cat"]
|
server_name = str(cfg["server_name"])
|
||||||
|
|
||||||
if not os.path.exists(cat):
|
|
||||||
print(f"[FATL] Cat photo at {cat} does not exist")
|
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
if "support" in _config:
|
|
||||||
support = {
|
|
||||||
"contacts": [{
|
|
||||||
"role": "m.role.admin"
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
||||||
_support = _config["support"]
|
|
||||||
|
|
||||||
if "mxid" in _support:
|
|
||||||
support["contacts"][0]["matrix_id"] = _support["mxid"]
|
|
||||||
|
|
||||||
if "email" in _support:
|
|
||||||
support["contacts"][0]["email_address"] = _support["email"]
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"[WARN] No support contacts are defined")
|
_fatal("[FATL] `server_name` is not in configuration")
|
||||||
support = {"contacts": []}
|
|
||||||
|
|
||||||
|
if "signing_key" in cfg and "signing_key_path" in cfg:
|
||||||
|
_warn(
|
||||||
|
"[WARN] Both `signing_key` and `signing_key_path` present. Using `signing_key`."
|
||||||
|
)
|
||||||
|
|
||||||
|
if "signing_key" in cfg:
|
||||||
|
signing_key = str(cfg["signing_key"]).strip()
|
||||||
|
elif "signing_key_path" in cfg:
|
||||||
|
sk = _read_signing_key_from_path(cfg["signing_key_path"])
|
||||||
|
if sk:
|
||||||
|
signing_key = sk
|
||||||
|
else:
|
||||||
|
_fatal(
|
||||||
|
"[FATL] `signing_key` is not in configuration. "
|
||||||
|
"A signing key can be generated using `cmd/generate_key.py`."
|
||||||
|
)
|
||||||
|
|
||||||
|
if "cat" in cfg:
|
||||||
|
cat = str(cfg["cat"])
|
||||||
|
|
||||||
|
cat_path = _validate_cat_path(cat)
|
||||||
|
cat = str(cat_path)
|
||||||
|
|
||||||
|
support_obj = {"contacts": []}
|
||||||
|
if "support" in cfg and isinstance(cfg["support"], dict):
|
||||||
|
_support = cfg["support"]
|
||||||
|
contact = {"role": "m.role.admin"}
|
||||||
|
if "mxid" in _support:
|
||||||
|
contact["matrix_id"] = str(_support["mxid"])
|
||||||
|
if "email" in _support:
|
||||||
|
contact["email_address"] = str(_support["email"])
|
||||||
|
if len(contact) > 1:
|
||||||
|
support_obj["contacts"].append(contact)
|
||||||
|
else:
|
||||||
|
_warn("[WARN] No support contacts are defined")
|
||||||
|
support = support_obj
|
||||||
|
|
||||||
print("[INFO] Configuration file was valid")
|
print("[INFO] Configuration file was valid")
|
||||||
|
|
||||||
|
|
||||||
|
_apply_config(_load_toml(_CONFIG_PATH))
|
||||||
|
|||||||
Reference in New Issue
Block a user