Skip to content

Commit

Permalink
fixup! manager: files watchdog: watchdog created specifically for TLS…
Browse files Browse the repository at this point in the history
… certificate files
  • Loading branch information
alesmrazek committed Nov 26, 2024
1 parent 82030c6 commit 2040aff
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions python/knot_resolver/manager/files/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import time
from pathlib import Path
from threading import Timer
from typing import List, Optional, Union

Expand Down Expand Up @@ -39,13 +40,12 @@ def tls_cert_paths(config: KresConfig) -> List[str]:
_tls_cert_watchdog: Optional["TLSCertWatchDog"] = None

class TLSCertEventHandler(FileSystemEventHandler):
def __init__(self, config: KresConfig, delay: int = 5) -> None:
self._config = config
def __init__(self, cmd: str, delay: int = 5) -> None:
self._delay = delay
self._cmd = f"net.tls('{config.network.tls.cert_file}', '{config.network.tls.key_file}')"
self._timer: Optional[Timer] = None
self._cmd = cmd

def trigger_cmd(self) -> None:
def _reset_cmd(self) -> None:
if compat.asyncio.is_event_loop_running():
compat.asyncio.create_task(command_registered_workers(self._cmd))
else:
Expand All @@ -66,19 +66,20 @@ def on_deleted(self, event: Union[DirDeletedEvent, FileDeletedEvent]) -> None:
def on_modified(self, event: Union[DirModifiedEvent, FileModifiedEvent]) -> None:
# skipping if command was already triggered
if self._timer and self._timer.is_alive():
self._timer.start()
return
# start a new timer
self._timer = Timer(self._delay, self._reset_cmd)
self._timer.start()

class TLSCertWatchDog:
def __init__(self, config: KresConfig) -> None:
def __init__(self, cert_file: Path, key_file: Path) -> None:
self._observer = Observer()
self._config = config

if config.network.tls.cert_file and config.network.tls.key_file:
self._cert_file = config.network.tls.cert_file.to_path()
self._key_file = config.network.tls.key_file.to_path()
self._cert_file = cert_file
self._key_file = key_file
self._cmd = f"net.tls('{cert_file}', '{key_file}')"

def schedule(self) -> None:
event_handler = TLSCertEventHandler(self._config)
event_handler = TLSCertEventHandler(self._cmd)
logger.info("Schedule watching of TLS certificate files")
self._observer.schedule(
event_handler,
Expand Down Expand Up @@ -113,9 +114,14 @@ def stop(self) -> None:
@only_on_real_changes_update(tls_cert_paths)
async def _init_tls_cert_watchdog(config: KresConfig) -> None:
global _tls_cert_watchdog
if _tls_cert_watchdog is None:
if _tls_cert_watchdog:
_tls_cert_watchdog.stop()

if config.network.tls.cert_file and config.network.tls.key_file:
logger.info("Starting TLS certificate files WatchDog")
_tls_cert_watchdog = TLSCertWatchDog(config)
_tls_cert_watchdog = TLSCertWatchDog(
config.network.tls.cert_file.to_path(), config.network.tls.key_file.to_path()
)
_tls_cert_watchdog.schedule()
_tls_cert_watchdog.start()

Expand Down

0 comments on commit 2040aff

Please sign in to comment.