From 92a20b76aae2ce2432ab4dfdf4739b795ddc4bbd Mon Sep 17 00:00:00 2001 From: celsius narhwal Date: Wed, 17 Jul 2024 09:28:59 -0400 Subject: [PATCH] Prevent `FIREWHALE_RELOAD_INTERVAL` from being negative --- README.md | 14 +++++++------- firewhale/settings.py | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2ff5f97..097527e 100644 --- a/README.md +++ b/README.md @@ -176,13 +176,13 @@ Firewhale supports all versions of the Docker API that are also supported by Doc Some aspects of Firewhale can be configured via environment variables. -| **Environment Variable** | **Description** | **Default** | -|------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------| -| `FIREWHALE_PORT` | The port on which Firewhale should listen. Firewhale will be accessible at `http://firewhale:${FIREWHALE_PORT}`. Must be an integer between 0 and 65535 and different than `FIREWHALE_CADDY_API_PORT`. | 2375 | -| `FIREWHALE_CADDY_API_PORT` | The port on which Caddy's [admin API](https://caddyserver.com/docs/api) should listen. The Caddy API will be accessible at `localhost:${FIREWHALE_CADDY_API_PORT}` within Firewhale's container. Must be an integer between 0 and 65535 and different than `FIREWHALE_PORT`. | 2019 | -| `FIREWHALE_HTTP_STATUS_CODE` | The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) Firewhale should respond with when it receives a request it has not been configured to allow. Must be an integer between 100 and 599. | 403 | -| `FIREWHALE_RELOAD_INTERVAL` | The interval at which Firewhale will query Docker for any changes to your services' labels and update its rules accordingly. Must be in the format of a [Go duration string](https://pkg.go.dev/time#ParseDuration), except you can also use `d` for day, `w` for week, `mm` for month, and `y` for year.[^1] | `30s` | -| `FIREWHALE_LABEL_PREFIX` | The prefix with which Firewhale labels should begin. Socket access will be configurable using the `${LABEL_PREFIX}.read` and `${LABEL_PREFIX}.write` labels. | `firewhale` | +| **Environment Variable** | **Description** | **Default** | +|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------| +| `FIREWHALE_PORT` | The port on which Firewhale should listen. Firewhale will be accessible at `http://firewhale:${FIREWHALE_PORT}`. Must be an integer between 0 and 65535 and different than `FIREWHALE_CADDY_API_PORT`. | 2375 | +| `FIREWHALE_CADDY_API_PORT` | The port on which Caddy's [admin API](https://caddyserver.com/docs/api) should listen. The Caddy API will be accessible at `localhost:${FIREWHALE_CADDY_API_PORT}` within Firewhale's container. Must be an integer between 0 and 65535 and different than `FIREWHALE_PORT`. | 2019 | +| `FIREWHALE_HTTP_STATUS_CODE` | The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) Firewhale should respond with when it receives a request it has not been configured to allow. Must be an integer between 100 and 599. | 403 | +| `FIREWHALE_RELOAD_INTERVAL` | The interval at which Firewhale will query Docker for any changes to your services' labels and update its rules accordingly. Must be in the format of a [Go duration string](https://pkg.go.dev/time#ParseDuration) (except you can also use `d` for day, `w` for week, `mm` for month, and `y` for year[^1]) that represents a non-negative value in seconds. | `30s` | +| `FIREWHALE_LABEL_PREFIX` | The prefix with which Firewhale labels should begin. Socket access will be configurable using the `${LABEL_PREFIX}.read` and `${LABEL_PREFIX}.write` labels. | `firewhale` | ## Considerations diff --git a/firewhale/settings.py b/firewhale/settings.py index dd2b6d8..d2b0cb4 100644 --- a/firewhale/settings.py +++ b/firewhale/settings.py @@ -28,13 +28,16 @@ def validate_ports(self): @field_validator("reload_interval") def validate_reload_interval(cls, v): try: - durationpy.from_str(v) + interval = durationpy.from_str(v) except ValueError: raise ValueError( "FIREWHALE_RELOAD_INTERVAL must be in the format of a Go duration string. " "https://pkg.go.dev/time#ParseDuration" ) from None + if interval.total_seconds() < 0: + raise ValueError(f"FIREWHALE_RELOAD_INTERVAL may not be negative ({v} = {interval.total_seconds()}).") + return v @property