-
Notifications
You must be signed in to change notification settings - Fork 1
/
external_server_main.py
76 lines (64 loc) · 2.48 KB
/
external_server_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import sys
import argparse
import os
from external_server.server import ExternalServer, eslogger
from external_server.config import load_config, InvalidConfiguration
from external_server.logs import configure_logging
def parsed_script_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
type=str,
default="./config/config.json",
help="path to the configuration file",
)
parser.add_argument(
"--tls", action=argparse.BooleanOptionalAction, help="use tls authentication"
)
tls = parser.add_argument_group("tls", description="if tls is used, set following arguments")
tls.add_argument("--ca", type=str, help="path to Certificate Authority certificate files")
tls.add_argument("--cert", type=str, help="path to PEM encoded client certificate file")
tls.add_argument("--key", type=str, help="path to PEM encoded client private keys file")
args = parser.parse_args()
if not os.path.isfile(args.config):
raise FileNotFoundError(f"Config file {os.path.abspath(args.config)} not found.")
if args.tls:
missing_fields = []
if not args.ca:
missing_fields.append("ca certificate (--ca)")
if not args.cert:
missing_fields.append("PEM encoded client certificate (--cert)")
if not args.key:
missing_fields.append("private key to PEM encoded client certificate (--key)")
if missing_fields:
raise argparse.ArgumentError(
None, f"TLS requires additional parameters: {', '.join(missing_fields)}"
)
return args
def main() -> None:
"""Main entry of external server"""
try:
args = parsed_script_args()
except argparse.ArgumentError as exc:
eslogger.error(f"Invalid arguments. {exc}")
print(f"Invalid arguments. {exc}")
sys.exit(1)
try:
config = load_config(args.config)
configure_logging("External Server", config)
except InvalidConfiguration as exc:
eslogger.error(f"Invalid config: {exc}")
print(f"Invalid config: {exc}")
sys.exit(1)
eslogger.info(f"Loaded config:\n{config.model_dump_json(indent=4)}")
server = ExternalServer(config)
if args.tls:
server.set_tls(args.ca, args.cert, args.key)
try:
server.start()
except KeyboardInterrupt:
server.stop(reason="keyboard interrupt")
if __name__ == "__main__":
main()