Skip to content

Commit 36d1a42

Browse files
committed
fix(core): improve PR #16 with bug fixes and security enhancements
This commit addresses several issues found during PR #16 analysis: 1. Fix cache management bug: Replace `del` with `pop()` to prevent KeyError when processing sequential stop/die events on same container 2. Add explicit security warning: Log warning message when attempting to connect to 'host' network, improving visibility of security blocks 3. Add configuration validation: Validate monitoredLabel and monitoredLabelCondition are not empty at startup to fail fast These changes improve robustness and security of the PR #16 implementation.
1 parent 08f4377 commit 36d1a42

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ def load_config() -> Config:
185185
general=config_data["logLevel"]["general"],
186186
application=config_data["logLevel"]["application"])
187187

188+
# Validate Traefik configuration
189+
if not config_data["traefik"]["monitoredLabel"]:
190+
logging.critical("traefik.monitoredLabel cannot be empty. Exiting program.")
191+
sys.exit(1)
192+
193+
if not config_data["traefik"]["monitoredLabelCondition"]:
194+
logging.critical("traefik.monitoredLabelCondition cannot be empty. Exiting program.")
195+
sys.exit(1)
196+
188197
traefik: TraefikConfig = TraefikConfig(
189198
containerName=config_data["traefik"]["containerName"],
190199
monitoredLabel=config_data["traefik"]["monitoredLabel"],

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ def connect_traefik_to_network(container):
158158
app_logger.debug(f"Adjusted allowed network to {real_network}.")
159159

160160
# Connect Traefik to the network if allowed, or log that it's skipping the connection
161-
if (allowed_networks == [''] or net in allowed_networks) and net.lower() != 'host':
161+
if net.lower() == 'host':
162+
app_logger.warning(f"Skipping connection to 'host' network for security reasons. Container: {container.name}")
163+
elif allowed_networks == [''] or net in allowed_networks:
162164
if net not in traefik_container.attrs["NetworkSettings"]["Networks"]:
163165
app_logger.debug(f"Connecting Traefik to network {net}.")
164166
network.connect(traefik_container)
@@ -268,14 +270,14 @@ def monitor_events():
268270
f"Container {container.name} is being stopped. Attempting to disconnect Traefik from relevant networks."
269271
)
270272
disconnect_traefik_from_network(container)
271-
del container_cache[event["id"]]
273+
container_cache.pop(event["id"], None)
272274

273275
elif event["Action"] == "die":
274276
app_logger.info(
275277
f"Container {container.name} is being killed. Attempting to disconnect Traefik from relevant networks."
276278
)
277279
disconnect_traefik_from_network(container)
278-
del container_cache[event["id"]]
280+
container_cache.pop(event["id"], None)
279281

280282
if __name__ == "__main__":
281283
# Display the version

0 commit comments

Comments
 (0)