app: raise an exception if configured ssl cert or key files do not exist

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-05-22 08:22:46 -04:00
parent 5db74b724e
commit a18987aa59
1 changed files with 18 additions and 8 deletions

View File

@ -126,14 +126,10 @@ class MoonrakerApp:
self.max_upload_size *= 1024 * 1024 self.max_upload_size *= 1024 * 1024
# SSL config # SSL config
self.cert_path: str = config.get('ssl_certificate_path', "") self.cert_path: str = self._get_path_option(
self.key_path: str = config.get('ssl_key_path', "") config, 'ssl_certificate_path')
if self.cert_path: self.key_path: str = self._get_path_option(
self.cert_path = os.path.abspath( config, 'ssl_key_path')
os.path.expanduser(self.cert_path))
if self.key_path:
self.key_path = os.path.abspath(
os.path.expanduser(self.key_path))
# Set Up Websocket and Authorization Managers # Set Up Websocket and Authorization Managers
self.wsm = WebsocketManager(self.server) self.wsm = WebsocketManager(self.server)
@ -171,6 +167,17 @@ class MoonrakerApp:
self.register_static_file_handler( self.register_static_file_handler(
"klippy.log", DEFAULT_KLIPPY_LOG_PATH, force=True) "klippy.log", DEFAULT_KLIPPY_LOG_PATH, force=True)
def _get_path_option(self, config: ConfigHelper, option: str) -> str:
path: Optional[str] = config.get(option, None)
if path is None:
return ""
expanded = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(expanded):
raise self.server.error(
f"Invalid path for option '{option}', "
f"{path} does not exist")
return expanded
def listen(self, host: str, port: int, ssl_port: int) -> None: def listen(self, host: str, port: int, ssl_port: int) -> None:
self.http_server = self.app.listen( self.http_server = self.app.listen(
port, address=host, max_body_size=MAX_BODY_SIZE, port, address=host, max_body_size=MAX_BODY_SIZE,
@ -182,6 +189,9 @@ class MoonrakerApp:
self.secure_server = self.app.listen( self.secure_server = self.app.listen(
ssl_port, address=host, max_body_size=MAX_BODY_SIZE, ssl_port, address=host, max_body_size=MAX_BODY_SIZE,
xheaders=True, ssl_options=ssl_ctx) xheaders=True, ssl_options=ssl_ctx)
else:
logging.info("SSL Certificate/Key not configured, "
"aborting HTTPS Server startup")
def log_request(self, handler: tornado.web.RequestHandler) -> None: def log_request(self, handler: tornado.web.RequestHandler) -> None:
status_code = handler.get_status() status_code = handler.get_status()