Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c125375ac7 |
No files matched your search
-14
@@ -161,20 +161,6 @@ RUN git checkout --quiet --detach b4b2c785308b1ce785b6155c7fe5f16879080193 \
|
|||||||
# ubuntu 26.04, 2026-09-27
|
# ubuntu 26.04, 2026-09-27
|
||||||
FROM ubuntu@sha256:f144425ff09be612d6d9ad965196e9cdc23dae1f42110a8a11a3e9a8198759f7
|
FROM ubuntu@sha256:f144425ff09be612d6d9ad965196e9cdc23dae1f42110a8a11a3e9a8198759f7
|
||||||
|
|
||||||
# The image's own labels, in place of the ones it would inherit from
|
|
||||||
# Ubuntu's. script/docker and script/cibuild pass the build arguments;
|
|
||||||
# a build that passes none labels the image unknown rather than leave
|
|
||||||
# Ubuntu's values.
|
|
||||||
ARG VERSION=unknown
|
|
||||||
ARG CREATED=unknown
|
|
||||||
ARG REVISION=unknown
|
|
||||||
LABEL org.opencontainers.image.title="smallwebwaf" \
|
|
||||||
org.opencontainers.image.description="A web application firewall for self-hosted web apps: sww rproxy in front of an app, and sww waf, which decides." \
|
|
||||||
org.opencontainers.image.version="${VERSION}" \
|
|
||||||
org.opencontainers.image.created="${CREATED}" \
|
|
||||||
org.opencontainers.image.revision="${REVISION}" \
|
|
||||||
org.opencontainers.image.source="https://git.eeqj.de/sneak/smallwebwaf"
|
|
||||||
|
|
||||||
# runit's install creates its _runit-log user with minsysusers, which
|
# runit's install creates its _runit-log user with minsysusers, which
|
||||||
# reads this file in place of runit's /usr/lib/sysusers.d/runit.conf.
|
# reads this file in place of runit's /usr/lib/sysusers.d/runit.conf.
|
||||||
# runit's line leaves out the shell, and minsysusers prints a Perl
|
# runit's line leaves out the shell, and minsysusers prints a Perl
|
||||||
|
|||||||
@@ -284,10 +284,6 @@ account, a key, a token or an alert destination stays off until you give one.
|
|||||||
`SWW_WAF_BYTES_COUNTED` says. Headers are never counted. What the visitor sent
|
`SWW_WAF_BYTES_COUNTED` says. Headers are never counted. What the visitor sent
|
||||||
over a WebSocket counts as request bytes, and what the app sent as response
|
over a WebSocket counts as request bytes, and what the app sent as response
|
||||||
bytes.
|
bytes.
|
||||||
- An anomaly is more requests or bytes than an `SWW_WAF_ANOMALY_` threshold
|
|
||||||
allows. Crossing a threshold sends one `anomaly` alert to the alert
|
|
||||||
destinations, and does nothing else: no client is banned, rate limited or
|
|
||||||
blocked.
|
|
||||||
- An invalid setting stops the process at start with one log line naming the
|
- An invalid setting stops the process at start with one log line naming the
|
||||||
variable and what is wrong with the value. The settings in effect are logged
|
variable and what is wrong with the value. The settings in effect are logged
|
||||||
on one line at start, with tokens, keys and webhook paths shown as `********`.
|
on one line at start, with tokens, keys and webhook paths shown as `********`.
|
||||||
@@ -388,29 +384,29 @@ Every setting the code reads, with its default:
|
|||||||
| `SWW_WAF_ALERT_EVENTS` | `ban,permanent_ban,core_rule_set_match,anomaly,reputation_hit,source_failure,file_error` | The events alerts are sent for; the hourly `summary` is sent whatever it says. |
|
| `SWW_WAF_ALERT_EVENTS` | `ban,permanent_ban,core_rule_set_match,anomaly,reputation_hit,source_failure,file_error` | The events alerts are sent for; the hourly `summary` is sent whatever it says. |
|
||||||
| `SWW_WAF_ALERT_COOLDOWN` | `15m` | How long after an alert its repeats are held back and counted, a duration or `off`. |
|
| `SWW_WAF_ALERT_COOLDOWN` | `15m` | How long after an alert its repeats are held back and counted, a duration or `off`. |
|
||||||
| `SWW_WAF_ALERT_MAX_PER_HOUR` | `60` | The most alerts sent in an hour, a number or `off`; the rest are counted in the hour's `summary`. |
|
| `SWW_WAF_ALERT_MAX_PER_HOUR` | `60` | The most alerts sent in an hour, a number or `off`; the rest are counted in the hour's `summary`. |
|
||||||
| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_MINUTE` | `off` | Sends one alert to the alert destinations when one client makes more requests than this in a minute, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_MINUTE` | `off` | Alert when one client makes more requests than this in a minute, a count. |
|
||||||
| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_HOUR` | `off` | Sends one alert to the alert destinations when one client makes more requests than this in an hour, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_HOUR` | `off` | Alert when one client makes more requests than this in an hour, a count. |
|
||||||
| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_MINUTE` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for one client in a minute, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one client in a minute, a size. |
|
||||||
| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_HOUR` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for one client in an hour, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one client in an hour, a size. |
|
||||||
| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_MINUTE` | `off` | Sends one alert to the alert destinations when the netblock around a client makes more requests than this in a minute, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_MINUTE` | `off` | Alert when the netblock around a client makes more requests than this in a minute, a count. |
|
||||||
| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_HOUR` | `off` | Sends one alert to the alert destinations when the netblock around a client makes more requests than this in an hour, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_HOUR` | `off` | Alert when the netblock around a client makes more requests than this in an hour, a count. |
|
||||||
| `SWW_WAF_ANOMALY_NET_BYTES_PER_MINUTE` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for the netblock around a client in a minute, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_NET_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the netblock around a client in a minute, a size. |
|
||||||
| `SWW_WAF_ANOMALY_NET_BYTES_PER_HOUR` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for the netblock around a client in an hour, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_NET_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the netblock around a client in an hour, a size. |
|
||||||
| `SWW_WAF_ANOMALY_NET_V4_PREFIX` | `24` | The prefix length of the netblock around an IPv4 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 32. Those thresholds only send alerts. |
|
| `SWW_WAF_ANOMALY_NET_V4_PREFIX` | `24` | The prefix length of the netblock around an IPv4 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 32. |
|
||||||
| `SWW_WAF_ANOMALY_NET_V6_PREFIX` | `48` | The prefix length of the netblock around an IPv6 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 128. Those thresholds only send alerts. |
|
| `SWW_WAF_ANOMALY_NET_V6_PREFIX` | `48` | The prefix length of the netblock around an IPv6 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 128. |
|
||||||
| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE` | `off` | Sends one alert to the alert destinations when one AS number makes more requests than this in a minute, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE` | `off` | Alert when one AS number makes more requests than this in a minute, a count. |
|
||||||
| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR` | `off` | Sends one alert to the alert destinations when one AS number makes more requests than this in an hour, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR` | `off` | Alert when one AS number makes more requests than this in an hour, a count. |
|
||||||
| `SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for one AS number in a minute, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one AS number in a minute, a size. |
|
||||||
| `SWW_WAF_ANOMALY_ASN_BYTES_PER_HOUR` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for one AS number in an hour, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_ASN_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one AS number in an hour, a size. |
|
||||||
| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_MINUTE` | `off` | Sends one alert to the alert destinations when the whole service gets more requests than this in a minute, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_MINUTE` | `off` | Alert when the whole service gets more requests than this in a minute, a count. |
|
||||||
| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_HOUR` | `off` | Sends one alert to the alert destinations when the whole service gets more requests than this in an hour, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_HOUR` | `off` | Alert when the whole service gets more requests than this in an hour, a count. |
|
||||||
| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_MINUTE` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for the whole service in a minute, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the whole service in a minute, a size. |
|
||||||
| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_HOUR` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for the whole service in an hour, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the whole service in an hour, a size. |
|
||||||
| `SWW_WAF_ANOMALY_WATCHED_NETS` | empty | Named netblocks the `SWW_WAF_ANOMALY_WATCHED_NET_` thresholds count each as a whole, such as `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. Those thresholds only send alerts. |
|
| `SWW_WAF_ANOMALY_WATCHED_NETS` | empty | Named netblocks the `SWW_WAF_ANOMALY_WATCHED_NET_` thresholds count each as a whole, such as `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. |
|
||||||
| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_MINUTE` | `off` | Sends one alert to the alert destinations when a watched netblock makes more requests than this in a minute, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_MINUTE` | `off` | Alert when a watched netblock makes more requests than this in a minute, a count. |
|
||||||
| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_HOUR` | `off` | Sends one alert to the alert destinations when a watched netblock makes more requests than this in an hour, a count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_HOUR` | `off` | Alert when a watched netblock makes more requests than this in an hour, a count. |
|
||||||
| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_MINUTE` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for a watched netblock in a minute, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for a watched netblock in a minute, a size. |
|
||||||
| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_HOUR` | `off` | Sends one alert to the alert destinations when more bytes than this are counted for a watched netblock in an hour, a size. `SWW_WAF_BYTES_COUNTED` says which bytes count. No client is banned, rate limited or blocked. |
|
| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for a watched netblock in an hour, a size. |
|
||||||
|
|
||||||
[`docs/settings.md`](docs/settings.md) gives the values each setting takes in
|
[`docs/settings.md`](docs/settings.md) gives the values each setting takes in
|
||||||
full, and the limits that are fixed rather than settings.
|
full, and the limits that are fixed rather than settings.
|
||||||
@@ -754,8 +750,6 @@ stops the running process (see [`docs/state-files.md`](docs/state-files.md)).
|
|||||||
|
|
||||||
- `cmd/sww`: the binary, which only calls `internal/sww`.
|
- `cmd/sww`: the binary, which only calls `internal/sww`.
|
||||||
- `internal/sww`: the process, as the command it is given, and the health check.
|
- `internal/sww`: the process, as the command it is given, and the health check.
|
||||||
It is the only package that imports `go.uber.org/fx`, which builds the parts
|
|
||||||
of `waf` and `rproxy`.
|
|
||||||
- `internal/config`: reads the settings, the one place they are read.
|
- `internal/config`: reads the settings, the one place they are read.
|
||||||
- `internal/rproxy`: the reverse proxy in front of one app.
|
- `internal/rproxy`: the reverse proxy in front of one app.
|
||||||
- `internal/waf`: `waf`'s server, which runs the checks and answers its own
|
- `internal/waf`: `waf`'s server, which runs the checks and answers its own
|
||||||
|
|||||||
@@ -69,9 +69,7 @@ never as a time.
|
|||||||
that command's settings, and runs `internal/rproxy` until `SIGTERM` or
|
that command's settings, and runs `internal/rproxy` until `SIGTERM` or
|
||||||
`SIGINT` (see "`rproxy`"). `sww healthcheck` is the image's health check.
|
`SIGINT` (see "`rproxy`"). `sww healthcheck` is the image's health check.
|
||||||
With no command, or one it does not know, the binary prints the commands
|
With no command, or one it does not know, the binary prints the commands
|
||||||
and exits with status 2. `internal/sww` is the only package that imports
|
and exits with status 2.
|
||||||
`go.uber.org/fx`. fx builds the parts of `waf` and `rproxy`, and starts
|
|
||||||
and stops their background work.
|
|
||||||
- `internal/config`: reads the settings, the one place they are read.
|
- `internal/config`: reads the settings, the one place they are read.
|
||||||
- `internal/waf`: `waf`'s server. It runs the checks in `check`, and holds
|
- `internal/waf`: `waf`'s server. It runs the checks in `check`, and holds
|
||||||
`waf`'s question, answer and report. It answers `waf`'s own endpoints in
|
`waf`'s question, answer and report. It answers `waf`'s own endpoints in
|
||||||
|
|||||||
@@ -9,10 +9,6 @@
|
|||||||
ARG SWW_IMAGE=smallwebwaf
|
ARG SWW_IMAGE=smallwebwaf
|
||||||
FROM ${SWW_IMAGE}
|
FROM ${SWW_IMAGE}
|
||||||
|
|
||||||
# Its own title and description, in place of the smallwebwaf image's.
|
|
||||||
LABEL org.opencontainers.image.title="smallwebwaf-example-app" \
|
|
||||||
org.opencontainers.image.description="An example app built on the smallwebwaf image."
|
|
||||||
|
|
||||||
# Packages the app needs, from the nixpkgs in the image.
|
# Packages the app needs, from the nixpkgs in the image.
|
||||||
RUN nix-env -iA nixpkgs.busybox
|
RUN nix-env -iA nixpkgs.busybox
|
||||||
|
|
||||||
|
|||||||
@@ -364,11 +364,6 @@ tag that no request can get stops the start. A request can get only these:
|
|||||||
|
|
||||||
## Anomaly thresholds
|
## Anomaly thresholds
|
||||||
|
|
||||||
An anomaly is more requests or bytes than one of these thresholds allows.
|
|
||||||
Crossing a threshold sends one `anomaly` alert to the destinations under
|
|
||||||
"Alerting", and does nothing else: no client is banned, rate limited or blocked.
|
|
||||||
`SWW_WAF_ALERT_COOLDOWN` holds back repeats of that alert.
|
|
||||||
|
|
||||||
Each scope has four thresholds: the most requests and the most body bytes
|
Each scope has four thresholds: the most requests and the most body bytes
|
||||||
counted in a minute and in an hour. The bytes are those `SWW_WAF_BYTES_COUNTED`
|
counted in a minute and in an hour. The bytes are those `SWW_WAF_BYTES_COUNTED`
|
||||||
names. Requests are counts, bytes are sizes. Each is `off` by default. The four
|
names. Requests are counts, bytes are sizes. Each is `off` by default. The four
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ require (
|
|||||||
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
||||||
github.com/prometheus/client_golang v1.24.1
|
github.com/prometheus/client_golang v1.24.1
|
||||||
github.com/spf13/viper v1.21.0
|
github.com/spf13/viper v1.21.0
|
||||||
go.uber.org/fx v1.24.0
|
|
||||||
golang.org/x/sys v0.48.0
|
golang.org/x/sys v0.48.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,9 +44,6 @@ require (
|
|||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
github.com/valllabh/ocsf-schema-golang v1.0.3 // indirect
|
github.com/valllabh/ocsf-schema-golang v1.0.3 // indirect
|
||||||
go.uber.org/dig v1.19.0 // indirect
|
|
||||||
go.uber.org/multierr v1.10.0 // indirect
|
|
||||||
go.uber.org/zap v1.26.0 // indirect
|
|
||||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||||
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
|
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
|
||||||
golang.org/x/net v0.60.0 // indirect
|
golang.org/x/net v0.60.0 // indirect
|
||||||
|
|||||||
@@ -97,16 +97,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
|||||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
github.com/valllabh/ocsf-schema-golang v1.0.3 h1:eR8k/3jP/OOqB8LRCtdJ4U+vlgd/gk5y3KMXoodrsrw=
|
github.com/valllabh/ocsf-schema-golang v1.0.3 h1:eR8k/3jP/OOqB8LRCtdJ4U+vlgd/gk5y3KMXoodrsrw=
|
||||||
github.com/valllabh/ocsf-schema-golang v1.0.3/go.mod h1:sZ3as9xqm1SSK5feFWIR2CuGeGRhsM7TR1MbpBctzPk=
|
github.com/valllabh/ocsf-schema-golang v1.0.3/go.mod h1:sZ3as9xqm1SSK5feFWIR2CuGeGRhsM7TR1MbpBctzPk=
|
||||||
go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4=
|
|
||||||
go.uber.org/dig v1.19.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
|
|
||||||
go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg=
|
|
||||||
go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo=
|
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
|
||||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
|
||||||
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
|
|
||||||
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
|
|
||||||
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
|
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
|
||||||
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
|
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
|
||||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||||
|
|||||||
@@ -71,59 +71,6 @@ func TestServerHasTheFixedLimits(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getOfSize is a GET whose request line and headers, the blank line that ends
|
|
||||||
// them included, are size bytes long.
|
|
||||||
func getOfSize(size int) string {
|
|
||||||
const start, end = "GET / HTTP/1.1\r\nHost: site.example\r\nX-Pad: ", "\r\n\r\n"
|
|
||||||
|
|
||||||
return start + strings.Repeat("x", size-len(start)-len(end)) + end
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestARequestLineAndHeadersOf32KiBReachTheApp(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
standIn := startWAF(t, allow)
|
|
||||||
app := startApp(t, greet)
|
|
||||||
addr, _ := startRProxy(t, standIn.URL, app.URL, nil)
|
|
||||||
|
|
||||||
conn := dial(t, addr)
|
|
||||||
send(t, conn, getOfSize(32<<10))
|
|
||||||
|
|
||||||
if status := readStatus(t, conn); status != http.StatusOK {
|
|
||||||
t.Errorf("status %d, want the app's 200", status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestARequestLineAndHeadersOver32KiBAreRefusedAndTheConnectionClosed(
|
|
||||||
t *testing.T,
|
|
||||||
) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
standIn := startWAF(t, allow)
|
|
||||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {
|
|
||||||
t.Error("the app was asked")
|
|
||||||
})
|
|
||||||
addr, _ := startRProxy(t, standIn.URL, app.URL, nil)
|
|
||||||
|
|
||||||
conn := dial(t, addr)
|
|
||||||
send(t, conn, getOfSize(32<<10+1))
|
|
||||||
|
|
||||||
// io.ReadAll returns without an error only once rproxy has closed the
|
|
||||||
// connection.
|
|
||||||
text, err := io.ReadAll(conn)
|
|
||||||
if err != nil || !strings.HasPrefix(string(text), "HTTP/1.1 431 ") {
|
|
||||||
t.Errorf(
|
|
||||||
"read %q (%v), want 431 and the connection closed",
|
|
||||||
text,
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(standIn.questions) != 0 {
|
|
||||||
t.Error("waf was asked")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABodyAnnouncedOverItsLimitIsRefusedOnceWAFAllowsIt(t *testing.T) {
|
func TestABodyAnnouncedOverItsLimitIsRefusedOnceWAFAllowsIt(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -1,198 +0,0 @@
|
|||||||
package sww
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.uber.org/fx"
|
|
||||||
"go.uber.org/fx/fxevent"
|
|
||||||
)
|
|
||||||
|
|
||||||
// shutdownTimeout is how long requests in progress may take to finish
|
|
||||||
// once sww is told to stop, before their connections are closed.
|
|
||||||
// runit and docker wait a little longer before they kill the process.
|
|
||||||
const shutdownTimeout = 5 * time.Second
|
|
||||||
|
|
||||||
// requestLog is where a daemon's server writes the request log: stdout, and
|
|
||||||
// the remote log sender while there is one. fx tells parts apart by type, so
|
|
||||||
// it has a type of its own.
|
|
||||||
type requestLog struct {
|
|
||||||
io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
// servingFailed gets why serving failed, for run, which then stops the
|
|
||||||
// daemon. fx tells parts apart by type, so it has a type of its own.
|
|
||||||
type servingFailed chan error
|
|
||||||
|
|
||||||
// utcNow is the time the state files, the alerts and the rate limits go by.
|
|
||||||
func utcNow() time.Time {
|
|
||||||
return time.Now().UTC()
|
|
||||||
}
|
|
||||||
|
|
||||||
// startError is why a daemon cannot start: the message its process log gives,
|
|
||||||
// and the error it logs with it.
|
|
||||||
type startError struct {
|
|
||||||
message string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e startError) Error() string {
|
|
||||||
return e.message + ": " + e.err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e startError) Unwrap() error {
|
|
||||||
return e.err
|
|
||||||
}
|
|
||||||
|
|
||||||
// cannotStart logs why a daemon cannot start, with the message of the
|
|
||||||
// startError in err, and returns its exit status.
|
|
||||||
func cannotStart(processLog *slog.Logger, err error) int {
|
|
||||||
message, cause := "cannot start", err
|
|
||||||
|
|
||||||
failed, ok := errors.AsType[startError](err)
|
|
||||||
if ok {
|
|
||||||
message, cause = failed.message, failed.err
|
|
||||||
}
|
|
||||||
|
|
||||||
processLog.Error(message, "error", cause.Error())
|
|
||||||
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// listen listens on address, which the setting names.
|
|
||||||
func listen(address, setting string) (net.Listener, error) {
|
|
||||||
listener, err := (&net.ListenConfig{}).Listen(
|
|
||||||
context.Background(), "tcp", address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, startError{"cannot listen on " + setting, err}
|
|
||||||
}
|
|
||||||
|
|
||||||
return listener, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// background returns the hook that runs loop on a goroutine of its own from
|
|
||||||
// the start, and at the stop ends it and waits for it to return.
|
|
||||||
func background(loop func(context.Context)) fx.Hook {
|
|
||||||
running, stop := context.WithCancel(context.Background())
|
|
||||||
ended := make(chan struct{})
|
|
||||||
|
|
||||||
return fx.StartStopHook(
|
|
||||||
func() {
|
|
||||||
go func() {
|
|
||||||
loop(running)
|
|
||||||
close(ended)
|
|
||||||
}()
|
|
||||||
},
|
|
||||||
func() {
|
|
||||||
stop()
|
|
||||||
<-ended
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// serve serves requests on listener from the start. At the stop it gives the
|
|
||||||
// requests in progress shutdownTimeout to finish, then closes their
|
|
||||||
// connections. When serving fails, it logs why and sends the error to
|
|
||||||
// failed.
|
|
||||||
func serve(
|
|
||||||
lc fx.Lifecycle, server *http.Server, listener net.Listener,
|
|
||||||
processLog *slog.Logger, failed servingFailed,
|
|
||||||
) {
|
|
||||||
served := make(chan error, 1)
|
|
||||||
|
|
||||||
lc.Append(fx.Hook{
|
|
||||||
OnStart: func(context.Context) error {
|
|
||||||
go func() {
|
|
||||||
err := server.Serve(listener)
|
|
||||||
if !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
processLog.Error("serving failed", "error", err.Error())
|
|
||||||
|
|
||||||
failed <- err
|
|
||||||
}
|
|
||||||
|
|
||||||
served <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
OnStop: func(ctx context.Context) error {
|
|
||||||
processLog.Info("stopping")
|
|
||||||
|
|
||||||
shutdownCtx, cancel := context.WithTimeout(ctx, shutdownTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
err := server.Shutdown(shutdownCtx)
|
|
||||||
if err != nil {
|
|
||||||
processLog.Warn("requests still in progress were cut off",
|
|
||||||
"error", err.Error())
|
|
||||||
|
|
||||||
_ = server.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = <-served
|
|
||||||
if !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// run starts app, which serves until ctx is done or serving fails, as
|
|
||||||
// failed tells. Then it stops app and takes the daemon's last step. It
|
|
||||||
// returns the process's exit status, 1 when serving failed, a part could
|
|
||||||
// not stop in order or the last step failed; each logs why.
|
|
||||||
func run(
|
|
||||||
ctx context.Context, app *fx.App, failed servingFailed,
|
|
||||||
processLog *slog.Logger, lastStep func() error,
|
|
||||||
) int {
|
|
||||||
// The hooks only start goroutines, and end them at the stop, so neither
|
|
||||||
// is cut short by a deadline or by ctx.
|
|
||||||
err := app.Start(context.WithoutCancel(ctx))
|
|
||||||
if err != nil {
|
|
||||||
return cannotStart(processLog, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
status := 0
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
case <-failed:
|
|
||||||
status = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
err = app.Stop(context.WithoutCancel(ctx))
|
|
||||||
if err != nil {
|
|
||||||
status = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
err = lastStep()
|
|
||||||
if err != nil {
|
|
||||||
status = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if status == 0 {
|
|
||||||
processLog.Info("stopped")
|
|
||||||
}
|
|
||||||
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
|
|
||||||
// fxEventLogger returns what fx.WithLogger takes to write fx's own events to
|
|
||||||
// processLog at debug, which the daemons' log levels normally hold back. Its
|
|
||||||
// errors are at debug too: the daemon logs each one itself.
|
|
||||||
func fxEventLogger(processLog *slog.Logger) func() fxevent.Logger {
|
|
||||||
return func() fxevent.Logger {
|
|
||||||
logger := &fxevent.SlogLogger{Logger: processLog}
|
|
||||||
logger.UseLogLevel(slog.LevelDebug)
|
|
||||||
logger.UseErrorLevel(slog.LevelDebug)
|
|
||||||
|
|
||||||
return logger
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+62
-41
@@ -2,13 +2,12 @@ package sww
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||||
@@ -31,31 +30,26 @@ func RunRProxy(ctx context.Context, params Params) int {
|
|||||||
|
|
||||||
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cannotStart(processLog, startError{"invalid setting", err})
|
processLog.Error("invalid setting", "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
server := rproxy.New(rproxy.Params{
|
||||||
server *rproxy.Server
|
Config: cfg,
|
||||||
listener net.Listener
|
RequestLog: params.Stdout,
|
||||||
)
|
ProcessLog: processLog,
|
||||||
|
})
|
||||||
|
|
||||||
failed := make(servingFailed, 1)
|
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr)
|
||||||
|
|
||||||
app := fx.New(
|
|
||||||
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
|
||||||
fx.Provide(
|
|
||||||
newRProxyServer,
|
|
||||||
func(server *rproxy.Server) *http.Server { return server.Server },
|
|
||||||
newRProxyListener,
|
|
||||||
),
|
|
||||||
fx.Invoke(serve),
|
|
||||||
fx.Populate(&server, &listener),
|
|
||||||
fx.WithLogger(fxEventLogger(processLog)),
|
|
||||||
)
|
|
||||||
|
|
||||||
err = app.Err()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cannotStart(processLog, err)
|
processLog.Error(
|
||||||
|
"cannot listen on SWW_RPROXY_LISTEN_ADDR",
|
||||||
|
"error",
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
processLog.Info("starting",
|
processLog.Info("starting",
|
||||||
@@ -63,25 +57,52 @@ func RunRProxy(ctx context.Context, params Params) int {
|
|||||||
"address", listener.Addr().String(),
|
"address", listener.Addr().String(),
|
||||||
"settings", cfg)
|
"settings", cfg)
|
||||||
|
|
||||||
return run(ctx, app, failed, processLog, func() error {
|
return serveRProxy(ctx, server, listener, processLog)
|
||||||
server.WaitForReports()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newRProxyServer returns the server rproxy runs.
|
// serveRProxy serves requests on listener until ctx is done, and then stops as
|
||||||
func newRProxyServer(
|
// RunRProxy says.
|
||||||
cfg *config.RProxy, out requestLog, processLog *slog.Logger,
|
func serveRProxy(
|
||||||
) *rproxy.Server {
|
ctx context.Context, server *rproxy.Server, listener net.Listener,
|
||||||
return rproxy.New(rproxy.Params{
|
processLog *slog.Logger,
|
||||||
Config: cfg,
|
) int {
|
||||||
RequestLog: out,
|
served := make(chan error, 1)
|
||||||
ProcessLog: processLog,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR.
|
go func() {
|
||||||
func newRProxyListener(cfg *config.RProxy) (net.Listener, error) {
|
served <- server.Serve(listener)
|
||||||
return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR")
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-served:
|
||||||
|
processLog.Error("serving failed", "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
|
||||||
|
processLog.Info("stopping")
|
||||||
|
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx),
|
||||||
|
shutdownTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
err := server.Shutdown(shutdownCtx)
|
||||||
|
if err != nil {
|
||||||
|
processLog.Warn("requests still in progress were cut off",
|
||||||
|
"error", err.Error())
|
||||||
|
|
||||||
|
_ = server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = <-served
|
||||||
|
if !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
processLog.Error("serving failed", "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
server.WaitForReports()
|
||||||
|
processLog.Info("stopped")
|
||||||
|
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
@@ -51,9 +51,7 @@ func runRProxy(ctx context.Context, env map[string]string, out *output) int {
|
|||||||
|
|
||||||
// startRProxy runs rproxy with waf at backend and the app at appURL, and
|
// startRProxy runs rproxy with waf at backend and the app at appURL, and
|
||||||
// returns its URL and what it writes. It believes X-Forwarded-For from the
|
// returns its URL and what it writes. It believes X-Forwarded-For from the
|
||||||
// tests. It waits a minute for waf's answer, so that a slow answer on a busy
|
// tests. It stops as the test ends, and that it stopped in order is checked.
|
||||||
// host is not taken for no answer. It stops as the test ends, and that it
|
|
||||||
// stopped in order is checked.
|
|
||||||
func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
|
func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -67,7 +65,6 @@ func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
|
|||||||
"SWW_RPROXY_UPSTREAM_URL": appURL,
|
"SWW_RPROXY_UPSTREAM_URL": appURL,
|
||||||
"SWW_RPROXY_TRUSTED_PROXIES": localhost + "/32",
|
"SWW_RPROXY_TRUSTED_PROXIES": localhost + "/32",
|
||||||
wafBackend: backend,
|
wafBackend: backend,
|
||||||
wafBackendTimeout: "60000",
|
|
||||||
}, out)
|
}, out)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
+394
-5
@@ -4,14 +4,17 @@
|
|||||||
// stop, and then stops in an orderly way, writing the state and cache files.
|
// stop, and then stops in an orderly way, writing the state and cache files.
|
||||||
// `sww rproxy` reads rproxy's settings alone, and runs the reverse proxy in
|
// `sww rproxy` reads rproxy's settings alone, and runs the reverse proxy in
|
||||||
// front of one app that asks waf about each request. `sww healthcheck` is the
|
// front of one app that asks waf about each request. `sww healthcheck` is the
|
||||||
// container's health check. fx builds the parts of each daemon, starts their
|
// container's health check.
|
||||||
// background work and stops it.
|
|
||||||
package sww
|
package sww
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -19,9 +22,29 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// shutdownTimeout is how long requests in progress may take to finish
|
||||||
|
// once sww is told to stop, before their connections are closed.
|
||||||
|
// runit and docker wait a little longer before they kill the process.
|
||||||
|
const shutdownTimeout = 5 * time.Second
|
||||||
|
|
||||||
|
// remoteLogStopTimeout is how long, as waf stops, the log lines still
|
||||||
|
// waiting are sent to SWW_WAF_LOG_REMOTE_URL before they are given up. stdout has
|
||||||
|
// carried them.
|
||||||
|
const remoteLogStopTimeout = 2 * time.Second
|
||||||
|
|
||||||
// usageStatus is the exit status with no command, or one sww does not
|
// usageStatus is the exit status with no command, or one sww does not
|
||||||
// know.
|
// know.
|
||||||
const usageStatus = 2
|
const usageStatus = 2
|
||||||
@@ -44,9 +67,6 @@ type Params struct {
|
|||||||
Viper *viper.Viper
|
Viper *viper.Viper
|
||||||
// Stdout receives the request log and the process's own messages.
|
// Stdout receives the request log and the process's own messages.
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
// BesideLookupTimeLimit is the waf.Params.BesideLookupTimeLimit RunWAF
|
|
||||||
// gives waf. Main leaves it zero, waf's own limit; tests lengthen it.
|
|
||||||
BesideLookupTimeLimit time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main runs the command os.Args names, and returns the process's exit status.
|
// Main runs the command os.Args names, and returns the process's exit status.
|
||||||
@@ -87,3 +107,372 @@ func printUsage(w io.Writer) int {
|
|||||||
|
|
||||||
return usageStatus
|
return usageStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunWAF is `sww waf`: it reads the settings, the rule files,
|
||||||
|
// the lookup database and the state and cache files, then serves requests until
|
||||||
|
// ctx is done. It returns the process's exit status, 1 when waf cannot start.
|
||||||
|
func RunWAF(ctx context.Context, params Params) int {
|
||||||
|
// Until the settings are read, the one message is an invalid setting's
|
||||||
|
// error, which every SWW_WAF_LOG_LEVEL lets through.
|
||||||
|
processLog := requestlog.NewProcessLogger(params.Stdout,
|
||||||
|
config.InstanceName(params.Viper), slog.LevelError)
|
||||||
|
|
||||||
|
cfg, err := config.WAFFromEnvironment(params.Viper)
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(processLog, "invalid setting", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// While SWW_WAF_LOG_REMOTE_URL is set, every line on stdout from here on
|
||||||
|
// is sent there too.
|
||||||
|
stdout := params.Stdout
|
||||||
|
|
||||||
|
var remote *remotelog.Sender
|
||||||
|
|
||||||
|
if cfg.LogRemoteURL != nil {
|
||||||
|
remote = newRemoteLogSender(cfg)
|
||||||
|
stdout = io.MultiWriter(params.Stdout, remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
processLog = requestlog.NewProcessLogger(
|
||||||
|
stdout,
|
||||||
|
cfg.InstanceName,
|
||||||
|
cfg.LogLevel,
|
||||||
|
)
|
||||||
|
|
||||||
|
if remote != nil {
|
||||||
|
stopSending := startSending(ctx, remote, processLog)
|
||||||
|
defer stopSending()
|
||||||
|
}
|
||||||
|
|
||||||
|
return runWAFWithSettings(
|
||||||
|
ctx,
|
||||||
|
params.Version,
|
||||||
|
cfg,
|
||||||
|
stdout,
|
||||||
|
remote,
|
||||||
|
processLog,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runWAFWithSettings is the rest of RunWAF once the settings are read and
|
||||||
|
// the process log goes where they say. remote is nil unless
|
||||||
|
// SWW_WAF_LOG_REMOTE_URL is set.
|
||||||
|
func runWAFWithSettings(
|
||||||
|
ctx context.Context,
|
||||||
|
version string,
|
||||||
|
cfg *config.WAF,
|
||||||
|
stdout io.Writer,
|
||||||
|
remote *remotelog.Sender,
|
||||||
|
processLog *slog.Logger,
|
||||||
|
) int {
|
||||||
|
// The state files and the alerts give times in UTC.
|
||||||
|
now := func() time.Time { return time.Now().UTC() }
|
||||||
|
|
||||||
|
alertQueue := newAlertQueue(cfg, now, processLog)
|
||||||
|
|
||||||
|
ruleFiles, err := rules.Load(rules.Params{
|
||||||
|
Dir: cfg.RuleFilesDir,
|
||||||
|
Enabled: cfg.RuleFilesEnabled,
|
||||||
|
ProcessLog: processLog,
|
||||||
|
Alerts: alertQueue,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(processLog, "cannot use the rule files", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
crs, err := waf.NewCoreRuleSet(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(processLog, "cannot use the Core Rule Set", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server, err := newServer(
|
||||||
|
cfg,
|
||||||
|
stdout,
|
||||||
|
processLog,
|
||||||
|
now,
|
||||||
|
ruleFiles,
|
||||||
|
alertQueue,
|
||||||
|
crs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(processLog, "cannot use the lookup database", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if remote != nil {
|
||||||
|
server.Metrics.AddRemoteLog(remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := loadStateFiles(cfg, server, alertQueue, now, processLog)
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(
|
||||||
|
processLog,
|
||||||
|
"cannot use the state and cache files",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr)
|
||||||
|
if err != nil {
|
||||||
|
return cannotStart(
|
||||||
|
processLog,
|
||||||
|
"cannot listen on SWW_WAF_LISTEN_ADDR",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
processLog.Info("starting",
|
||||||
|
"version", version,
|
||||||
|
"address", listener.Addr().String(),
|
||||||
|
"settings", cfg)
|
||||||
|
|
||||||
|
return serve(
|
||||||
|
ctx,
|
||||||
|
server,
|
||||||
|
listener,
|
||||||
|
files,
|
||||||
|
ruleFiles,
|
||||||
|
alertQueue,
|
||||||
|
processLog,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cannotStart logs why waf cannot start, and returns its exit status.
|
||||||
|
func cannotStart(processLog *slog.Logger, message string, err error) int {
|
||||||
|
processLog.Error(message, "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// newServer returns the server waf runs, with the metrics of the alerts,
|
||||||
|
// after reading the lookup database while SWW_WAF_LOOKUP_SOURCE is file. A lookup
|
||||||
|
// database that cannot be read is an error.
|
||||||
|
func newServer(
|
||||||
|
cfg *config.WAF, stdout io.Writer, processLog *slog.Logger,
|
||||||
|
now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue,
|
||||||
|
crs *coreruleset.CoreRuleSet,
|
||||||
|
) (*waf.Server, error) {
|
||||||
|
var lookupFile *lookup.File
|
||||||
|
|
||||||
|
if cfg.LookupSource == "file" {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
lookupFile, err = lookup.OpenFile(lookup.FileParams{
|
||||||
|
Path: cfg.LookupDBPath,
|
||||||
|
Now: now,
|
||||||
|
ProcessLog: processLog,
|
||||||
|
Alerts: alertQueue,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
server := waf.New(waf.Params{
|
||||||
|
Config: cfg,
|
||||||
|
RequestLog: stdout,
|
||||||
|
ProcessLog: processLog,
|
||||||
|
GeoJSURL: geojs.URL,
|
||||||
|
AbuseIPDBURL: reputation.AbuseIPDBURL,
|
||||||
|
LookupFile: lookupFile,
|
||||||
|
CoreRuleSet: crs,
|
||||||
|
Now: now,
|
||||||
|
Rules: ruleFiles,
|
||||||
|
Alerts: alertQueue,
|
||||||
|
})
|
||||||
|
server.Metrics.AddAlerts(alertQueue)
|
||||||
|
|
||||||
|
if lookupFile != nil {
|
||||||
|
server.Metrics.AddLookupFile(
|
||||||
|
lookupFile.LastRead,
|
||||||
|
lookupFile.ReadFailures,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return server, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadStateFiles reads the state and cache files into the parts of server and
|
||||||
|
// into alertQueue, as state.Load does.
|
||||||
|
func loadStateFiles(
|
||||||
|
cfg *config.WAF, server *waf.Server, alertQueue *alerts.Queue,
|
||||||
|
now func() time.Time, processLog *slog.Logger,
|
||||||
|
) (*state.Files, error) {
|
||||||
|
return state.Load(state.Params{
|
||||||
|
Dir: cfg.StateDir,
|
||||||
|
CacheDir: cfg.CacheDir,
|
||||||
|
BansWriteDelay: cfg.BansWriteDelay,
|
||||||
|
WriteInterval: cfg.StateWriteInterval,
|
||||||
|
Ledger: server.Ledger,
|
||||||
|
Limiter: server.Limiter,
|
||||||
|
GeoJS: server.GeoJS,
|
||||||
|
Lists: server.Lists,
|
||||||
|
DNSBL: server.DNSBL,
|
||||||
|
AbuseIPDB: server.AbuseIPDB,
|
||||||
|
RouteWatch: server.RouteWatch,
|
||||||
|
Alerts: alertQueue,
|
||||||
|
Anomalies: server.Anomalies,
|
||||||
|
Now: now,
|
||||||
|
ProcessLog: processLog,
|
||||||
|
Metrics: server.Metrics,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// newAlertQueue returns the queue of the alerts to the webhook, Slack and
|
||||||
|
// ntfy, with the settings for them.
|
||||||
|
func newAlertQueue(
|
||||||
|
cfg *config.WAF, now func() time.Time, processLog *slog.Logger,
|
||||||
|
) *alerts.Queue {
|
||||||
|
return alerts.New(alerts.Params{
|
||||||
|
WebhookURL: cfg.AlertWebhookURL,
|
||||||
|
WebhookHeaders: cfg.AlertWebhookHeaders,
|
||||||
|
SlackURL: cfg.AlertSlackWebhookURL,
|
||||||
|
NtfyURL: cfg.AlertNtfyURL,
|
||||||
|
NtfyToken: cfg.AlertNtfyToken,
|
||||||
|
Events: cfg.AlertEvents,
|
||||||
|
Cooldown: cfg.AlertCooldown,
|
||||||
|
MaxPerHour: cfg.AlertMaxPerHour,
|
||||||
|
Instance: cfg.InstanceName,
|
||||||
|
Now: now,
|
||||||
|
ProcessLog: processLog,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// newRemoteLogSender returns a sender of the log lines to
|
||||||
|
// SWW_WAF_LOG_REMOTE_URL, with the settings for it.
|
||||||
|
func newRemoteLogSender(cfg *config.WAF) *remotelog.Sender {
|
||||||
|
return remotelog.New(remotelog.Params{
|
||||||
|
URL: cfg.LogRemoteURL,
|
||||||
|
RootCAs: cfg.LogRemoteTLSCAs,
|
||||||
|
BufferLines: cfg.LogRemoteBufferLines,
|
||||||
|
Facility: cfg.LogRemoteFacility,
|
||||||
|
AppName: cfg.LogRemoteAppName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// startSending runs remote until the function it returns is called, which
|
||||||
|
// then waits at most remoteLogStopTimeout for the lines still waiting to
|
||||||
|
// be sent. Sending goes on after ctx is done, so that the lines written
|
||||||
|
// while sww stops are sent too.
|
||||||
|
func startSending(
|
||||||
|
ctx context.Context, remote *remotelog.Sender, processLog *slog.Logger,
|
||||||
|
) func() {
|
||||||
|
sending, stop := context.WithCancel(context.WithoutCancel(ctx))
|
||||||
|
sent := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
remote.Run(sending, processLog)
|
||||||
|
close(sent)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return func() {
|
||||||
|
stop()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-sent:
|
||||||
|
case <-time.After(remoteLogStopTimeout):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve serves requests on listener, writes the state and cache files as they
|
||||||
|
// are due, takes in an admin's edits of them, reads the rule files again as
|
||||||
|
// they change, and the lookup database when it is replaced, fetches the
|
||||||
|
// lists the settings name by URL as they are due, and sends the alerts,
|
||||||
|
// until ctx is done. Then it gives the requests in progress
|
||||||
|
// shutdownTimeout to finish, and writes every state and cache file,
|
||||||
|
// alerts.json with the alerts still waiting.
|
||||||
|
func serve(
|
||||||
|
ctx context.Context, server *waf.Server, listener net.Listener,
|
||||||
|
files *state.Files, ruleFiles *rules.Files, alertQueue *alerts.Queue,
|
||||||
|
processLog *slog.Logger,
|
||||||
|
) int {
|
||||||
|
served := make(chan error, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
served <- server.Serve(listener)
|
||||||
|
}()
|
||||||
|
|
||||||
|
writing, stopWriting := context.WithCancel(ctx)
|
||||||
|
defer stopWriting()
|
||||||
|
|
||||||
|
written := inBackground(func() { files.Run(writing) })
|
||||||
|
watched := inBackground(func() { files.Watch(writing) })
|
||||||
|
rulesWatched := inBackground(func() { ruleFiles.Watch(writing) })
|
||||||
|
lookupFileWatched := inBackground(func() {
|
||||||
|
if server.LookupFile != nil {
|
||||||
|
server.LookupFile.Watch(writing)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
listsFetched := inBackground(func() { server.Lists.Run(writing) })
|
||||||
|
alertsSent := inBackground(func() { alertQueue.Run(writing) })
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-served:
|
||||||
|
processLog.Error("serving failed", "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
|
||||||
|
processLog.Info("stopping")
|
||||||
|
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx),
|
||||||
|
shutdownTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
err := server.Shutdown(shutdownCtx)
|
||||||
|
if err != nil {
|
||||||
|
processLog.Warn("requests still in progress were cut off",
|
||||||
|
"error", err.Error())
|
||||||
|
|
||||||
|
_ = server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = <-served
|
||||||
|
if !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
processLog.Error("serving failed", "error", err.Error())
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run and Watch have ended, so nothing else reads or writes the
|
||||||
|
// files, and no alert is being sent, so that alerts.json keeps every
|
||||||
|
// alert not yet sent. Every request has ended too, but for one cut off
|
||||||
|
// because Shutdown timed out, which adds to its client's history only as
|
||||||
|
// it ends. That can be after this write, and then that request is missing
|
||||||
|
// from clients.json.
|
||||||
|
<-written
|
||||||
|
<-watched
|
||||||
|
<-rulesWatched
|
||||||
|
<-lookupFileWatched
|
||||||
|
<-listsFetched
|
||||||
|
<-alertsSent
|
||||||
|
|
||||||
|
err = files.WriteAll()
|
||||||
|
if err != nil {
|
||||||
|
processLog.Error(
|
||||||
|
"writing the state and cache files failed",
|
||||||
|
"error",
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
processLog.Info("stopped")
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// inBackground runs task on a goroutine of its own, and returns a channel
|
||||||
|
// that is closed once task has returned.
|
||||||
|
func inBackground(task func()) <-chan struct{} {
|
||||||
|
done := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
task()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return done
|
||||||
|
}
|
||||||
@@ -173,9 +173,6 @@ func run(ctx context.Context, env map[string]string, out *output) int {
|
|||||||
Version: testVersion,
|
Version: testVersion,
|
||||||
Viper: v,
|
Viper: v,
|
||||||
Stdout: out,
|
Stdout: out,
|
||||||
// The race detector and a busy host can slow the Core Rule Set's
|
|
||||||
// inspections past waf's own limit.
|
|
||||||
BesideLookupTimeLimit: time.Hour,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,490 +0,0 @@
|
|||||||
package sww
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.uber.org/fx"
|
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/routewatch"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/rules"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
|
||||||
|
|
||||||
// remoteLogStopTimeout is how long, as waf stops, the log lines still
|
|
||||||
// waiting are sent to SWW_WAF_LOG_REMOTE_URL before they are given up. stdout has
|
|
||||||
// carried them.
|
|
||||||
const remoteLogStopTimeout = 2 * time.Second
|
|
||||||
|
|
||||||
// RunWAF is `sww waf`: it reads the settings, the rule files,
|
|
||||||
// the lookup database and the state and cache files, then serves requests until
|
|
||||||
// ctx is done. Then it gives the requests in progress shutdownTimeout to
|
|
||||||
// finish, ends its background work and writes every state and cache file,
|
|
||||||
// alerts.json with the alerts still waiting. It returns the process's exit
|
|
||||||
// status, 1 when waf cannot start or stop in order.
|
|
||||||
func RunWAF(ctx context.Context, params Params) int {
|
|
||||||
// Until the settings are read, the one message is an invalid setting's
|
|
||||||
// error, which every SWW_WAF_LOG_LEVEL lets through.
|
|
||||||
processLog := requestlog.NewProcessLogger(params.Stdout,
|
|
||||||
config.InstanceName(params.Viper), slog.LevelError)
|
|
||||||
|
|
||||||
cfg, err := config.WAFFromEnvironment(params.Viper)
|
|
||||||
if err != nil {
|
|
||||||
return cannotStart(processLog, startError{"invalid setting", err})
|
|
||||||
}
|
|
||||||
|
|
||||||
// While SWW_WAF_LOG_REMOTE_URL is set, every line on stdout from here on
|
|
||||||
// is sent there too. The sender runs before any part is built, so that it
|
|
||||||
// sends why waf cannot start.
|
|
||||||
stdout := params.Stdout
|
|
||||||
|
|
||||||
var remote *remotelog.Sender
|
|
||||||
|
|
||||||
if cfg.LogRemoteURL != nil {
|
|
||||||
remote = newRemoteLogSender(cfg)
|
|
||||||
stdout = io.MultiWriter(params.Stdout, remote)
|
|
||||||
}
|
|
||||||
|
|
||||||
processLog = requestlog.NewProcessLogger(
|
|
||||||
stdout,
|
|
||||||
cfg.InstanceName,
|
|
||||||
cfg.LogLevel,
|
|
||||||
)
|
|
||||||
|
|
||||||
if remote != nil {
|
|
||||||
stopSending := startSending(ctx, remote, processLog)
|
|
||||||
defer stopSending()
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
files *state.Files
|
|
||||||
listener net.Listener
|
|
||||||
)
|
|
||||||
|
|
||||||
failed := make(servingFailed, 1)
|
|
||||||
|
|
||||||
app := fx.New(
|
|
||||||
fx.Supply(
|
|
||||||
params,
|
|
||||||
cfg,
|
|
||||||
processLog,
|
|
||||||
requestLog{stdout},
|
|
||||||
remote,
|
|
||||||
failed,
|
|
||||||
),
|
|
||||||
fx.Provide(
|
|
||||||
newAlertQueue, newRuleFiles, newCoreRuleSet, newLookupFile,
|
|
||||||
newLimiter, newLedger, newAnomalyCounters,
|
|
||||||
newLists, newDNSBL, newAbuseIPDB, newRouteWatch,
|
|
||||||
newMetrics, newGeoJS, newWAFServer,
|
|
||||||
func(server *waf.Server) *http.Server { return server.Server },
|
|
||||||
newWAFListener, newStateFiles,
|
|
||||||
),
|
|
||||||
// fx stops hooks in the reverse of the order they were added in. The
|
|
||||||
// state files are read before serving is set up, so that serving
|
|
||||||
// stops before their loops do.
|
|
||||||
fx.Populate(&files),
|
|
||||||
fx.Invoke(serve),
|
|
||||||
fx.Populate(&listener),
|
|
||||||
fx.WithLogger(fxEventLogger(processLog)),
|
|
||||||
)
|
|
||||||
|
|
||||||
err = app.Err()
|
|
||||||
if err != nil {
|
|
||||||
return cannotStart(processLog, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
processLog.Info("starting",
|
|
||||||
"version", params.Version,
|
|
||||||
"address", listener.Addr().String(),
|
|
||||||
"settings", cfg)
|
|
||||||
|
|
||||||
// The last write follows the stop of every hook, and so the end of every
|
|
||||||
// loop that changes the state or raises an alert. A hook of the state
|
|
||||||
// files would stop before the loops of the parts built ahead of them.
|
|
||||||
return run(ctx, app, failed, processLog, func() error {
|
|
||||||
err := files.WriteAll()
|
|
||||||
if err != nil {
|
|
||||||
processLog.Error("writing the state and cache files failed",
|
|
||||||
"error", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newAlertQueue returns the queue of the alerts to the webhook, Slack and
|
|
||||||
// ntfy, with the settings for them. Until waf stops, it sends them.
|
|
||||||
func newAlertQueue(
|
|
||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
) *alerts.Queue {
|
|
||||||
alertQueue := alerts.New(alerts.Params{
|
|
||||||
WebhookURL: cfg.AlertWebhookURL,
|
|
||||||
WebhookHeaders: cfg.AlertWebhookHeaders,
|
|
||||||
SlackURL: cfg.AlertSlackWebhookURL,
|
|
||||||
NtfyURL: cfg.AlertNtfyURL,
|
|
||||||
NtfyToken: cfg.AlertNtfyToken,
|
|
||||||
Events: cfg.AlertEvents,
|
|
||||||
Cooldown: cfg.AlertCooldown,
|
|
||||||
MaxPerHour: cfg.AlertMaxPerHour,
|
|
||||||
Instance: cfg.InstanceName,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
})
|
|
||||||
|
|
||||||
lc.Append(background(alertQueue.Run))
|
|
||||||
|
|
||||||
return alertQueue
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRuleFiles reads the rule files, and reads them again as they change
|
|
||||||
// until waf stops.
|
|
||||||
func newRuleFiles(
|
|
||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
alertQueue *alerts.Queue,
|
|
||||||
) (*rules.Files, error) {
|
|
||||||
ruleFiles, err := rules.Load(rules.Params{
|
|
||||||
Dir: cfg.RuleFilesDir,
|
|
||||||
Enabled: cfg.RuleFilesEnabled,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, startError{"cannot use the rule files", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
lc.Append(background(ruleFiles.Watch))
|
|
||||||
|
|
||||||
return ruleFiles, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCoreRuleSet returns the Core Rule Set, as waf.NewCoreRuleSet does.
|
|
||||||
func newCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) {
|
|
||||||
crs, err := waf.NewCoreRuleSet(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, startError{"cannot use the Core Rule Set", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
return crs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newLookupFile reads the lookup database while SWW_WAF_LOOKUP_SOURCE is
|
|
||||||
// file, and reads it again when it is replaced until waf stops. It returns
|
|
||||||
// nil while the source is anything else.
|
|
||||||
func newLookupFile(
|
|
||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
alertQueue *alerts.Queue,
|
|
||||||
) (*lookup.File, error) {
|
|
||||||
if cfg.LookupSource != "file" {
|
|
||||||
return nil, nil //nolint:nilnil // no lookup database, and no error
|
|
||||||
}
|
|
||||||
|
|
||||||
lookupFile, err := lookup.OpenFile(lookup.FileParams{
|
|
||||||
Path: cfg.LookupDBPath,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, startError{"cannot use the lookup database", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
lc.Append(background(lookupFile.Watch))
|
|
||||||
|
|
||||||
return lookupFile, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newLimiter returns the table of clients, with the rate and byte limits.
|
|
||||||
func newLimiter(cfg *config.WAF) *ratelimit.Limiter {
|
|
||||||
return ratelimit.New(ratelimit.Limits{
|
|
||||||
PerMinute: cfg.RateLimitPerMinute,
|
|
||||||
PerHour: cfg.RateLimitPerHour,
|
|
||||||
PerDay: cfg.RateLimitPerDay,
|
|
||||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
|
||||||
BytesPerHour: cfg.BytesLimitPerHour,
|
|
||||||
BytesPerDay: cfg.BytesLimitPerDay,
|
|
||||||
}, cfg.MaxTrackedClients)
|
|
||||||
}
|
|
||||||
|
|
||||||
// newLedger returns the ledger of the bans, with the settings for them.
|
|
||||||
func newLedger(cfg *config.WAF) *bans.Ledger {
|
|
||||||
return bans.New(bans.Params{
|
|
||||||
LimitBanDuration: cfg.LimitBanDuration,
|
|
||||||
LimitBanRepeatWindow: cfg.LimitBanRepeatWindow,
|
|
||||||
LimitBanPermanentAbove: cfg.LimitBanPermanentAbove,
|
|
||||||
AttackBanDuration: cfg.AttackBanDuration,
|
|
||||||
MaxBans: cfg.MaxBans,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newAnomalyCounters returns the counters of the anomaly thresholds.
|
|
||||||
func newAnomalyCounters(
|
|
||||||
cfg *config.WAF, alertQueue *alerts.Queue,
|
|
||||||
) *anomaly.Counters {
|
|
||||||
return anomaly.New(anomaly.Params{
|
|
||||||
Client: cfg.AnomalyClient,
|
|
||||||
Net: cfg.AnomalyNet,
|
|
||||||
ASN: cfg.AnomalyASN,
|
|
||||||
Total: cfg.AnomalyTotal,
|
|
||||||
WatchedNet: cfg.AnomalyWatchedNet,
|
|
||||||
NetV4Prefix: cfg.AnomalyNetV4Prefix,
|
|
||||||
NetV6Prefix: cfg.AnomalyNetV6Prefix,
|
|
||||||
NamedNetblocks: cfg.AnomalyWatchedNets,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newLists returns the lists the settings name by URL, none fetched yet.
|
|
||||||
// Until waf stops, it fetches them as they are due.
|
|
||||||
func newLists(
|
|
||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
alertQueue *alerts.Queue,
|
|
||||||
) *reputation.Lists {
|
|
||||||
lists := reputation.New(reputation.Params{
|
|
||||||
BlocklistURLs: cfg.BlocklistURLs,
|
|
||||||
Refresh: cfg.BlocklistRefresh,
|
|
||||||
ASNLimitPercentURL: cfg.ASNLimitPercentURL,
|
|
||||||
TagLists: cfg.TagLists,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
|
|
||||||
lc.Append(background(lists.Run))
|
|
||||||
|
|
||||||
return lists
|
|
||||||
}
|
|
||||||
|
|
||||||
// newDNSBL returns what asks the DNSBL zones, none asked yet.
|
|
||||||
func newDNSBL(
|
|
||||||
cfg *config.WAF, processLog *slog.Logger, alertQueue *alerts.Queue,
|
|
||||||
) *reputation.DNSBL {
|
|
||||||
return reputation.NewDNSBL(reputation.DNSBLParams{
|
|
||||||
Zones: cfg.DNSBLZones,
|
|
||||||
Resolver: cfg.DNSBLResolver,
|
|
||||||
CacheTTL: cfg.IPReputationCacheTTL,
|
|
||||||
Timeout: cfg.DNSBLAbuseIPDBTimeout,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newAbuseIPDB returns what checks clients with AbuseIPDB while
|
|
||||||
// SWW_WAF_ABUSEIPDB_KEY is set, none checked yet.
|
|
||||||
func newAbuseIPDB(
|
|
||||||
cfg *config.WAF, processLog *slog.Logger, alertQueue *alerts.Queue,
|
|
||||||
) *reputation.AbuseIPDB {
|
|
||||||
return reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{
|
|
||||||
URL: reputation.AbuseIPDBURL,
|
|
||||||
Key: cfg.AbuseIPDBKey,
|
|
||||||
MinScore: cfg.AbuseIPDBMinScore,
|
|
||||||
DailyBudget: cfg.AbuseIPDBDailyBudget,
|
|
||||||
CacheTTL: cfg.IPReputationCacheTTL,
|
|
||||||
Timeout: cfg.DNSBLAbuseIPDBTimeout,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRouteWatch returns what asks routewatch about clients, or nil while
|
|
||||||
// SWW_WAF_ROUTEWATCH_URL is unset.
|
|
||||||
func newRouteWatch(
|
|
||||||
cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
) *routewatch.Client {
|
|
||||||
if cfg.RouteWatchURL == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return routewatch.New(routewatch.Params{
|
|
||||||
URL: cfg.RouteWatchURL,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newMetrics returns the metrics, with those read from the other parts as
|
|
||||||
// the metrics are asked for: AbuseIPDB's while SWW_WAF_ABUSEIPDB_KEY is set,
|
|
||||||
// the lookup database's while there is one, and the remote log sender's
|
|
||||||
// while there is one.
|
|
||||||
func newMetrics(
|
|
||||||
cfg *config.WAF, ledger *bans.Ledger, limiter *ratelimit.Limiter,
|
|
||||||
ruleFiles *rules.Files, lists *reputation.Lists,
|
|
||||||
dnsbl *reputation.DNSBL, abuseIPDB *reputation.AbuseIPDB,
|
|
||||||
alertQueue *alerts.Queue, lookupFile *lookup.File,
|
|
||||||
remote *remotelog.Sender,
|
|
||||||
) *metrics.Metrics {
|
|
||||||
m := metrics.New(cfg.MetricsBusiestCountriesAndASNs, cfg.InstanceName)
|
|
||||||
m.AddBansAndClients(ledger, limiter, utcNow)
|
|
||||||
m.AddRules(ruleFiles)
|
|
||||||
m.AddReputation(lists, dnsbl)
|
|
||||||
|
|
||||||
if cfg.AbuseIPDBKey != "" {
|
|
||||||
m.AddAbuseIPDB(abuseIPDB)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.AddAlerts(alertQueue)
|
|
||||||
|
|
||||||
if lookupFile != nil {
|
|
||||||
m.AddLookupFile(lookupFile.LastRead, lookupFile.ReadFailures)
|
|
||||||
}
|
|
||||||
|
|
||||||
if remote != nil {
|
|
||||||
m.AddRemoteLog(remote)
|
|
||||||
}
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each
|
|
||||||
// answer goes to the limiter and the ledger, as waf.AddLookup says, and each
|
|
||||||
// failure is handled as waf.GeoJSFailed says.
|
|
||||||
func newGeoJS(
|
|
||||||
cfg *config.WAF, m *metrics.Metrics, limiter *ratelimit.Limiter,
|
|
||||||
ledger *bans.Ledger, alertQueue *alerts.Queue, processLog *slog.Logger,
|
|
||||||
) *geojs.GeoJS {
|
|
||||||
return geojs.New(geojs.Params{
|
|
||||||
URL: geojs.URL,
|
|
||||||
Answered: func(answer geojs.Answer) {
|
|
||||||
waf.AddLookup(cfg, limiter, ledger, answer)
|
|
||||||
},
|
|
||||||
Now: utcNow,
|
|
||||||
Asked: m.GeoJSRequests.Inc,
|
|
||||||
Failed: func(err error, askingAgainIn time.Duration) {
|
|
||||||
waf.GeoJSFailed(processLog, alertQueue, m, err, askingAgainIn)
|
|
||||||
},
|
|
||||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newWAFServer returns the server waf runs, with its parts and the time
|
|
||||||
// limit params gives.
|
|
||||||
func newWAFServer(
|
|
||||||
params Params, cfg *config.WAF, out requestLog, processLog *slog.Logger,
|
|
||||||
m *metrics.Metrics, limiter *ratelimit.Limiter, ledger *bans.Ledger,
|
|
||||||
anomalies *anomaly.Counters, lists *reputation.Lists,
|
|
||||||
dnsbl *reputation.DNSBL, abuseIPDB *reputation.AbuseIPDB,
|
|
||||||
routeWatch *routewatch.Client, geoJS *geojs.GeoJS,
|
|
||||||
lookupFile *lookup.File, crs *coreruleset.CoreRuleSet,
|
|
||||||
ruleFiles *rules.Files, alertQueue *alerts.Queue,
|
|
||||||
) *waf.Server {
|
|
||||||
return waf.New(waf.Params{
|
|
||||||
Config: cfg,
|
|
||||||
RequestLog: out,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Metrics: m,
|
|
||||||
Limiter: limiter,
|
|
||||||
Ledger: ledger,
|
|
||||||
Anomalies: anomalies,
|
|
||||||
Lists: lists,
|
|
||||||
DNSBL: dnsbl,
|
|
||||||
AbuseIPDB: abuseIPDB,
|
|
||||||
RouteWatch: routeWatch,
|
|
||||||
GeoJS: geoJS,
|
|
||||||
LookupFile: lookupFile,
|
|
||||||
CoreRuleSet: crs,
|
|
||||||
Now: utcNow,
|
|
||||||
Rules: ruleFiles,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
|
|
||||||
BesideLookupTimeLimit: params.BesideLookupTimeLimit,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newWAFListener listens on SWW_WAF_LISTEN_ADDR.
|
|
||||||
func newWAFListener(cfg *config.WAF) (net.Listener, error) {
|
|
||||||
return listen(cfg.ListenAddr, "SWW_WAF_LISTEN_ADDR")
|
|
||||||
}
|
|
||||||
|
|
||||||
// newStateFiles reads the state and cache files into the parts and the
|
|
||||||
// alert queue, as state.Load does. Until waf stops, it writes them as they
|
|
||||||
// are due and takes in an admin's edits of them.
|
|
||||||
func newStateFiles(
|
|
||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
|
||||||
m *metrics.Metrics, ledger *bans.Ledger, limiter *ratelimit.Limiter,
|
|
||||||
geoJS *geojs.GeoJS, lists *reputation.Lists, dnsbl *reputation.DNSBL,
|
|
||||||
abuseIPDB *reputation.AbuseIPDB, routeWatch *routewatch.Client,
|
|
||||||
alertQueue *alerts.Queue, anomalies *anomaly.Counters,
|
|
||||||
) (*state.Files, error) {
|
|
||||||
files, err := state.Load(state.Params{
|
|
||||||
Dir: cfg.StateDir,
|
|
||||||
CacheDir: cfg.CacheDir,
|
|
||||||
BansWriteDelay: cfg.BansWriteDelay,
|
|
||||||
WriteInterval: cfg.StateWriteInterval,
|
|
||||||
Ledger: ledger,
|
|
||||||
Limiter: limiter,
|
|
||||||
GeoJS: geoJS,
|
|
||||||
Lists: lists,
|
|
||||||
DNSBL: dnsbl,
|
|
||||||
AbuseIPDB: abuseIPDB,
|
|
||||||
RouteWatch: routeWatch,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
Anomalies: anomalies,
|
|
||||||
Now: utcNow,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Metrics: m,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, startError{"cannot use the state and cache files", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
lc.Append(background(files.Run))
|
|
||||||
lc.Append(background(files.Watch))
|
|
||||||
|
|
||||||
return files, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRemoteLogSender returns a sender of the log lines to
|
|
||||||
// SWW_WAF_LOG_REMOTE_URL, with the settings for it.
|
|
||||||
func newRemoteLogSender(cfg *config.WAF) *remotelog.Sender {
|
|
||||||
return remotelog.New(remotelog.Params{
|
|
||||||
URL: cfg.LogRemoteURL,
|
|
||||||
RootCAs: cfg.LogRemoteTLSCAs,
|
|
||||||
BufferLines: cfg.LogRemoteBufferLines,
|
|
||||||
Facility: cfg.LogRemoteFacility,
|
|
||||||
AppName: cfg.LogRemoteAppName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// startSending runs remote until the function it returns is called, which
|
|
||||||
// then waits at most remoteLogStopTimeout for the lines still waiting to
|
|
||||||
// be sent. Sending goes on after ctx is done, so that the lines written
|
|
||||||
// while sww stops are sent too.
|
|
||||||
func startSending(
|
|
||||||
ctx context.Context, remote *remotelog.Sender, processLog *slog.Logger,
|
|
||||||
) func() {
|
|
||||||
sending, stop := context.WithCancel(context.WithoutCancel(ctx))
|
|
||||||
sent := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
remote.Run(sending, processLog)
|
|
||||||
close(sent)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return func() {
|
|
||||||
stop()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-sent:
|
|
||||||
case <-time.After(remoteLogStopTimeout):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -35,7 +35,7 @@ func (abuseIPDB) check(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
client := clientGroup(rq.h.config, rq.client)
|
client := rq.h.clientGroup(rq.client)
|
||||||
held, _ := rq.h.limiter.Client(client)
|
held, _ := rq.h.limiter.Client(client)
|
||||||
offender := held.History.Offences != ratelimit.Offences{}
|
offender := held.History.Offences != ratelimit.Offences{}
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ func (rq *request) addBan() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
netblock, err = rq.h.parseBanNetblock(toAdd.Netblock)
|
netblock, err = rq.h.banNetblock(toAdd.Netblock)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -190,12 +190,12 @@ func (rq *request) readBanToAdd() (banToAdd, error) {
|
|||||||
return toAdd, nil
|
return toAdd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseBanNetblock reads value, a netblock such as 203.0.113.0/24, or a
|
// banNetblock reads value, a netblock such as 203.0.113.0/24, or a
|
||||||
// client's address, which stands for the netblock a ban on that client
|
// client's address, which stands for the netblock a ban on that client
|
||||||
// covers. An IPv4-mapped netblock, such as ::ffff:203.0.113.0/120, is
|
// covers. An IPv4-mapped netblock, such as ::ffff:203.0.113.0/120, is
|
||||||
// refused, since a client's address is looked up as IPv4 and a ban on it
|
// refused, since a client's address is looked up as IPv4 and a ban on it
|
||||||
// would refuse nothing, and so is a value with a zone.
|
// would refuse nothing, and so is a value with a zone.
|
||||||
func (h *handler) parseBanNetblock(value string) (netip.Prefix, error) {
|
func (h *handler) banNetblock(value string) (netip.Prefix, error) {
|
||||||
netblock, err := netip.ParsePrefix(value)
|
netblock, err := netip.ParsePrefix(value)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if netblock.Addr().Is4In6() {
|
if netblock.Addr().Is4In6() {
|
||||||
@@ -224,7 +224,7 @@ func (h *handler) parseBanNetblock(value string) (netip.Prefix, error) {
|
|||||||
return netip.Prefix{}, fmt.Errorf("netblock %q %w", value, errZone)
|
return netip.Prefix{}, fmt.Errorf("netblock %q %w", value, errZone)
|
||||||
}
|
}
|
||||||
|
|
||||||
return banNetblock(h.config, addr), nil
|
return h.netblock(addr), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// expiry returns when a ban made at now for duration ends: duration
|
// expiry returns when a ban made at now for duration ends: duration
|
||||||
@@ -295,7 +295,7 @@ func (rq *request) showClient() {
|
|||||||
Bans: state.BanEntries(rq.h.ledger.Covering(addr)),
|
Bans: state.BanEntries(rq.h.ledger.Covering(addr)),
|
||||||
}
|
}
|
||||||
|
|
||||||
client, seen := rq.h.limiter.Client(clientGroup(rq.h.config, addr))
|
client, seen := rq.h.limiter.Client(rq.h.clientGroup(addr))
|
||||||
if seen {
|
if seen {
|
||||||
answer.Client = &client
|
answer.Client = &client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -877,13 +877,18 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs(
|
|||||||
held, _, _ := startHeldGeoJS(t)
|
held, _, _ := startHeldGeoJS(t)
|
||||||
resolver, releaseResolver := startHeldResolver(t)
|
resolver, releaseResolver := startHeldResolver(t)
|
||||||
|
|
||||||
server, out, _ := newWAFWith(t, waf.New,
|
server, out, _ := newWAFWith(t, func(
|
||||||
silentGeoJSURL, silentAbuseIPDBURL, time.Now, map[string]string{
|
params waf.Params,
|
||||||
tagDeny: "country=KP", coreRuleSetMode: block,
|
) *waf.Server {
|
||||||
dnsblZones: dnsblZone, dnsblResolver: resolver,
|
params.AbuseIPDBURL = silentAbuseIPDBURL
|
||||||
abuseIPDBKey: accountKey,
|
|
||||||
blocklistURLs: held + "/drop.txt", asnLimitPercentURL: held + "/asn.txt",
|
return waf.New(params)
|
||||||
})
|
}, silentGeoJSURL, time.Now, map[string]string{
|
||||||
|
tagDeny: "country=KP", coreRuleSetMode: block,
|
||||||
|
dnsblZones: dnsblZone, dnsblResolver: resolver,
|
||||||
|
abuseIPDBKey: accountKey,
|
||||||
|
blocklistURLs: held + "/drop.txt", asnLimitPercentURL: held + "/asn.txt",
|
||||||
|
})
|
||||||
|
|
||||||
// The lists are fetched outside the synctest bubble below: a wait on the
|
// The lists are fetched outside the synctest bubble below: a wait on the
|
||||||
// network inside it would keep its clock from moving on.
|
// network inside it would keep its clock from moving on.
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
@@ -122,7 +121,7 @@ func (rq *request) banForLimit(
|
|||||||
rq.line.Offence = requestlog.OffenceLimit
|
rq.line.Offence = requestlog.OffenceLimit
|
||||||
rq.h.metrics.LimitHit(hit)
|
rq.h.metrics.LimitHit(hit)
|
||||||
|
|
||||||
netblock := banNetblock(rq.h.config, rq.client)
|
netblock := rq.h.netblock(rq.client)
|
||||||
if rq.h.config.Observe &&
|
if rq.h.config.Observe &&
|
||||||
!rq.wouldAlertBan(netblock, now, bans.CauseLimit) {
|
!rq.wouldAlertBan(netblock, now, bans.CauseLimit) {
|
||||||
return
|
return
|
||||||
@@ -158,7 +157,7 @@ func (rq *request) banForLimit(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ban, made := rq.h.ledger.BanForLimit(netblock, now, notes)
|
ban, made := rq.h.ledger.BanForLimit(netblock, now, notes)
|
||||||
rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client))
|
rq.h.limiter.Reset(rq.h.clientGroup(rq.client))
|
||||||
rq.line.BanExpires = banExpires(ban)
|
rq.line.BanExpires = banExpires(ban)
|
||||||
|
|
||||||
if made {
|
if made {
|
||||||
@@ -172,7 +171,7 @@ func (rq *request) banForLimit(
|
|||||||
// no ban, and raises the alert for the ban it would have made, if that
|
// no ban, and raises the alert for the ban it would have made, if that
|
||||||
// alert would be sent. It returns the action for that ban.
|
// alert would be sent. It returns the action for that ban.
|
||||||
func (rq *request) banForAttack(now time.Time, notes bans.Notes) string {
|
func (rq *request) banForAttack(now time.Time, notes bans.Notes) string {
|
||||||
netblock := banNetblock(rq.h.config, rq.client)
|
netblock := rq.h.netblock(rq.client)
|
||||||
if rq.h.config.Observe &&
|
if rq.h.config.Observe &&
|
||||||
!rq.wouldAlertBan(netblock, now, bans.CauseAttack) {
|
!rq.wouldAlertBan(netblock, now, bans.CauseAttack) {
|
||||||
return banAction(
|
return banAction(
|
||||||
@@ -274,16 +273,16 @@ func (rq *request) netblockRequests(netblock netip.Prefix) int64 {
|
|||||||
return rq.h.limiter.Requests(netblock) + 1
|
return rq.h.limiter.Requests(netblock) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// banNetblock is the netblock a ban on client covers: its IPv4 address,
|
// netblock is the netblock a ban on client covers: its IPv4 address,
|
||||||
// widened to SWW_WAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group clientGroup
|
// widened to SWW_WAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group clientGroup
|
||||||
// counts it in.
|
// counts it in.
|
||||||
func banNetblock(cfg *config.WAF, client netip.Addr) netip.Prefix {
|
func (h *handler) netblock(client netip.Addr) netip.Prefix {
|
||||||
addr := client.Unmap()
|
addr := client.Unmap()
|
||||||
if addr.Is4() {
|
if addr.Is4() {
|
||||||
return netip.PrefixFrom(addr, cfg.BanScopeV4Prefix).Masked()
|
return netip.PrefixFrom(addr, h.config.BanScopeV4Prefix).Masked()
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientGroup(cfg, addr)
|
return h.clientGroup(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// banExpires is when ban ends, as the log line gives it: a time, or
|
// banExpires is when ban ends, as the log line gives it: a time, or
|
||||||
|
|||||||
@@ -73,10 +73,10 @@ func newWAFHoldingTheCoreRuleSet(
|
|||||||
|
|
||||||
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
|
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
|
||||||
params.CoreRuleSet = crs
|
params.CoreRuleSet = crs
|
||||||
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
|
|
||||||
|
|
||||||
return waf.New(params)
|
return waf.New(params)
|
||||||
}, silentGeoJSURL, abuseIPDBURL, time.Now, settings)
|
}, silentGeoJSURL, time.Now, settings)
|
||||||
|
server.SetBesideLookupTimeLimit(waf.BesideLookupTimeLimit)
|
||||||
|
|
||||||
return server, out, queue
|
return server, out, queue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (byteLimits) check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
counts, hit, over := rq.h.limiter.CountBytes(
|
counts, hit, over := rq.h.limiter.CountBytes(
|
||||||
clientGroup(rq.h.config, rq.client),
|
rq.h.clientGroup(rq.client),
|
||||||
now,
|
now,
|
||||||
rq.countedBytes(),
|
rq.countedBytes(),
|
||||||
rq.bytesLimitPercent.percent,
|
rq.bytesLimitPercent.percent,
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// peerAddress is the address of the request's TCP peer.
|
// peerAddress is the address of the request's TCP peer.
|
||||||
@@ -47,10 +45,10 @@ func scheme(r *http.Request, isQuestion bool) string {
|
|||||||
// address, or its IPv6 group, the netblock its IPv6 address is in of the
|
// address, or its IPv6 group, the netblock its IPv6 address is in of the
|
||||||
// length SWW_WAF_IPV6_GROUP_PREFIX sets, a /64 by default, since one abuser
|
// length SWW_WAF_IPV6_GROUP_PREFIX sets, a /64 by default, since one abuser
|
||||||
// usually holds a whole /64. An IPv4 address in IPv6 form counts as IPv4.
|
// usually holds a whole /64. An IPv4 address in IPv6 form counts as IPv4.
|
||||||
func clientGroup(cfg *config.WAF, addr netip.Addr) netip.Prefix {
|
func (h *handler) clientGroup(addr netip.Addr) netip.Prefix {
|
||||||
addr = addr.Unmap()
|
addr = addr.Unmap()
|
||||||
if addr.Is6() {
|
if addr.Is6() {
|
||||||
return netip.PrefixFrom(addr, cfg.IPv6GroupPrefix).Masked()
|
return netip.PrefixFrom(addr, h.config.IPv6GroupPrefix).Masked()
|
||||||
}
|
}
|
||||||
|
|
||||||
return netip.PrefixFrom(addr, addr.BitLen())
|
return netip.PrefixFrom(addr, addr.BitLen())
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) {
|
|||||||
Event: alerts.EventCoreRuleSetMatch,
|
Event: alerts.EventCoreRuleSetMatch,
|
||||||
Host: rq.site,
|
Host: rq.site,
|
||||||
Client: rq.client,
|
Client: rq.client,
|
||||||
Netblock: clientGroup(rq.h.config, rq.client),
|
Netblock: rq.h.clientGroup(rq.client),
|
||||||
ASN: rq.line.ASN,
|
ASN: rq.line.ASN,
|
||||||
ASName: rq.line.ASName,
|
ASName: rq.line.ASName,
|
||||||
Country: rq.line.Country,
|
Country: rq.line.Country,
|
||||||
|
|||||||
@@ -1,4 +1,13 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
||||||
const BesideLookupTimeLimit = besideLookupTimeLimit
|
const BesideLookupTimeLimit = besideLookupTimeLimit
|
||||||
|
|
||||||
|
// SetBesideLookupTimeLimit gives the heuristics of besideLookup limit on s in
|
||||||
|
// place of besideLookupTimeLimit.
|
||||||
|
func (s *Server) SetBesideLookupTimeLimit(limit time.Duration) {
|
||||||
|
h := s.Handler.(*handler) //nolint:forcetypeassert // New makes it one
|
||||||
|
h.besideLookupLimit = limit
|
||||||
|
}
|
||||||
+13
-25
@@ -2,18 +2,14 @@ package waf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The headers in which an answer of allow gives rproxy the client's AS number
|
// The headers in which an answer of allow gives rproxy the client's AS number
|
||||||
@@ -55,7 +51,7 @@ func (rq *request) startLookUp(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
client := clientGroup(rq.h.config, rq.client)
|
client := rq.h.clientGroup(rq.client)
|
||||||
|
|
||||||
if rq.h.config.LookupSource == "file" {
|
if rq.h.config.LookupSource == "file" {
|
||||||
result <- lookupResult{
|
result <- lookupResult{
|
||||||
@@ -132,30 +128,22 @@ func countryTag(country string) string {
|
|||||||
return config.CountryTagPrefix + country
|
return config.CountryTagPrefix + country
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLookup adds answer, an answer about a client from the lookup database
|
// addLookup adds answer, an answer about a client from the lookup
|
||||||
// or GeoJS, to the client's history in limiter, and to the notes of the bans
|
// database or GeoJS, to the client's history, and to the notes of the bans
|
||||||
// in ledger on its netblock that have no AS number, AS name or country yet.
|
// on its netblock that have no AS number, AS name or country yet.
|
||||||
// cfg sets the netblock's size.
|
func (h *handler) addLookup(answer geojs.Answer) {
|
||||||
func AddLookup(
|
h.limiter.AddLookup(answer.Client, answer.Answered,
|
||||||
cfg *config.WAF, limiter *ratelimit.Limiter, ledger *bans.Ledger,
|
|
||||||
answer geojs.Answer,
|
|
||||||
) {
|
|
||||||
limiter.AddLookup(answer.Client, answer.Answered,
|
|
||||||
answer.ASN, answer.ASName, answer.Country)
|
answer.ASN, answer.ASName, answer.Country)
|
||||||
ledger.AddLookup(banNetblock(cfg, answer.Client.Addr()),
|
h.ledger.AddLookup(h.netblock(answer.Client.Addr()),
|
||||||
answer.ASN, answer.ASName, answer.Country)
|
answer.ASN, answer.ASName, answer.Country)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GeoJSFailed logs a failure of GeoJS to processLog, raises a
|
// geojsFailed logs a failure of GeoJS, raises a source_failure alert for it
|
||||||
// source_failure alert for it on alertQueue and counts it in m.
|
// and counts it. askingAgainIn is how long GeoJS is left alone after it.
|
||||||
// askingAgainIn is how long GeoJS is left alone after it.
|
func (h *handler) geojsFailed(err error, askingAgainIn time.Duration) {
|
||||||
func GeoJSFailed(
|
h.processLog.Warn("asking GeoJS failed",
|
||||||
processLog *slog.Logger, alertQueue *alerts.Queue, m *metrics.Metrics,
|
|
||||||
err error, askingAgainIn time.Duration,
|
|
||||||
) {
|
|
||||||
processLog.Warn("asking GeoJS failed",
|
|
||||||
"error", err.Error(), "asking_again_in", askingAgainIn.String())
|
"error", err.Error(), "asking_again_in", askingAgainIn.String())
|
||||||
alertQueue.Raise(alerts.Alert{
|
h.alerts.Raise(alerts.Alert{
|
||||||
Event: alerts.EventSourceFailure,
|
Event: alerts.EventSourceFailure,
|
||||||
Reason: "asking GeoJS failed",
|
Reason: "asking GeoJS failed",
|
||||||
Detail: map[string]any{
|
Detail: map[string]any{
|
||||||
@@ -163,7 +151,7 @@ func GeoJSFailed(
|
|||||||
"asking_again_in": askingAgainIn.String(),
|
"asking_again_in": askingAgainIn.String(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
m.GeoJSFailures.Inc()
|
h.metrics.GeoJSFailures.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// setLookupHeaders sets the headers that give the client's AS number and
|
// setLookupHeaders sets the headers that give the client's AS number and
|
||||||
|
|||||||
+120
-59
@@ -4,7 +4,6 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -57,28 +56,12 @@ type Params struct {
|
|||||||
RequestLog io.Writer
|
RequestLog io.Writer
|
||||||
// ProcessLog receives the process's own messages.
|
// ProcessLog receives the process's own messages.
|
||||||
ProcessLog *slog.Logger
|
ProcessLog *slog.Logger
|
||||||
// Metrics count what waf does. Whoever builds them adds the metrics
|
// GeoJSURL is where clients' AS numbers and countries are looked up
|
||||||
// read from the other parts.
|
// while SWW_WAF_LOOKUP_SOURCE is geojs, normally geojs.URL.
|
||||||
Metrics *metrics.Metrics
|
GeoJSURL string
|
||||||
// Limiter is the table of clients the rate limits count, with each
|
// AbuseIPDBURL is where clients are checked with AbuseIPDB while
|
||||||
// client's history.
|
// SWW_WAF_ABUSEIPDB_KEY is set, normally reputation.AbuseIPDBURL.
|
||||||
Limiter *ratelimit.Limiter
|
AbuseIPDBURL string
|
||||||
// Ledger holds the bans.
|
|
||||||
Ledger *bans.Ledger
|
|
||||||
// Anomalies are the counters of the anomaly thresholds.
|
|
||||||
Anomalies *anomaly.Counters
|
|
||||||
// Lists are the lists fetched from URLs, DNSBL the DNSBL zones'
|
|
||||||
// verdicts, and AbuseIPDB AbuseIPDB's scores and the checks spent.
|
|
||||||
Lists *reputation.Lists
|
|
||||||
DNSBL *reputation.DNSBL
|
|
||||||
AbuseIPDB *reputation.AbuseIPDB
|
|
||||||
// RouteWatch asks routewatch about clients, and is nil while
|
|
||||||
// SWW_WAF_ROUTEWATCH_URL is unset.
|
|
||||||
RouteWatch *routewatch.Client
|
|
||||||
// GeoJS looks clients' AS numbers and countries up while
|
|
||||||
// SWW_WAF_LOOKUP_SOURCE is geojs. It is given AddLookup as its Answered
|
|
||||||
// and GeoJSFailed as its Failed.
|
|
||||||
GeoJS *geojs.GeoJS
|
|
||||||
// LookupFile is the lookup database they are looked up in while
|
// LookupFile is the lookup database they are looked up in while
|
||||||
// SWW_WAF_LOOKUP_SOURCE is file, and nil otherwise.
|
// SWW_WAF_LOOKUP_SOURCE is file, and nil otherwise.
|
||||||
LookupFile *lookup.File
|
LookupFile *lookup.File
|
||||||
@@ -100,11 +83,6 @@ type Params struct {
|
|||||||
// failing, a query to a DNSBL zone or a check with AbuseIPDB failing,
|
// failing, a query to a DNSBL zone or a check with AbuseIPDB failing,
|
||||||
// or the day's AbuseIPDB checks used up.
|
// or the day's AbuseIPDB checks used up.
|
||||||
Alerts *alerts.Queue
|
Alerts *alerts.Queue
|
||||||
// BesideLookupTimeLimit is how long the heuristics of besideLookup have
|
|
||||||
// for a request. Zero means besideLookupTimeLimit. Tests that run the
|
|
||||||
// Core Rule Set lengthen it, since the race detector and a busy host can
|
|
||||||
// slow an inspection past besideLookupTimeLimit.
|
|
||||||
BesideLookupTimeLimit time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoreRuleSetInspector inspects a request with the Core Rule Set, as
|
// CoreRuleSetInspector inspects a request with the Core Rule Set, as
|
||||||
@@ -115,8 +93,11 @@ type CoreRuleSetInspector interface {
|
|||||||
) (coreruleset.Result, []byte, error)
|
) (coreruleset.Result, []byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server is the server waf runs, with the parts of its Params. The tests
|
// Server is the server waf runs, with the parts whose state the state
|
||||||
// read them.
|
// files keep, the lookup database, nil unless SWW_WAF_LOOKUP_SOURCE is file, the
|
||||||
|
// lists fetched from URLs, which its Run fetches, the DNSBL zones' verdicts,
|
||||||
|
// AbuseIPDB's scores and checks spent, routewatch's answers, and the metrics.
|
||||||
|
// RouteWatch is nil while SWW_WAF_ROUTEWATCH_URL is unset.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
|
|
||||||
@@ -132,45 +113,126 @@ type Server struct {
|
|||||||
Metrics *metrics.Metrics
|
Metrics *metrics.Metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
// newHandler returns the handler with what every request shares, the parts
|
// newHandler returns the handler with what every request shares, as the
|
||||||
// in params among it.
|
// settings in params make it.
|
||||||
func newHandler(params Params) *handler {
|
func newHandler(params Params) *handler {
|
||||||
|
m := metrics.New(
|
||||||
|
params.Config.MetricsBusiestCountriesAndASNs,
|
||||||
|
params.Config.InstanceName,
|
||||||
|
)
|
||||||
|
lists, dnsbl, abuseIPDB := newReputation(params, m)
|
||||||
|
|
||||||
awaitingReport, err := lru.New[string, *decision](maxAwaitingReport)
|
awaitingReport, err := lru.New[string, *decision](maxAwaitingReport)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) // New fails only for a size below one
|
panic(err) // New fails only for a size below one
|
||||||
}
|
}
|
||||||
|
|
||||||
return &handler{
|
h := &handler{
|
||||||
config: params.Config,
|
config: params.Config,
|
||||||
requestLog: params.RequestLog,
|
requestLog: params.RequestLog,
|
||||||
processLog: params.ProcessLog,
|
processLog: params.ProcessLog,
|
||||||
now: params.Now,
|
now: params.Now,
|
||||||
metrics: params.Metrics,
|
metrics: m,
|
||||||
awaitingReport: awaitingReport,
|
awaitingReport: awaitingReport,
|
||||||
limiter: params.Limiter,
|
limiter: ratelimit.New(ratelimit.Limits{
|
||||||
ledger: params.Ledger,
|
PerMinute: params.Config.RateLimitPerMinute,
|
||||||
geojs: params.GeoJS,
|
PerHour: params.Config.RateLimitPerHour,
|
||||||
anomalies: params.Anomalies,
|
PerDay: params.Config.RateLimitPerDay,
|
||||||
lookupFile: params.LookupFile,
|
BytesPerMinute: params.Config.BytesLimitPerMinute,
|
||||||
lists: params.Lists,
|
BytesPerHour: params.Config.BytesLimitPerHour,
|
||||||
dnsbl: params.DNSBL,
|
BytesPerDay: params.Config.BytesLimitPerDay,
|
||||||
abuseIPDB: params.AbuseIPDB,
|
}, params.Config.MaxTrackedClients),
|
||||||
routeWatch: params.RouteWatch,
|
ledger: bans.New(bans.Params{
|
||||||
rules: params.Rules,
|
LimitBanDuration: params.Config.LimitBanDuration,
|
||||||
coreRuleSet: params.CoreRuleSet,
|
LimitBanRepeatWindow: params.Config.LimitBanRepeatWindow,
|
||||||
alerts: params.Alerts,
|
LimitBanPermanentAbove: params.Config.LimitBanPermanentAbove,
|
||||||
// The country list, the headers, the tag settings that name an asn=
|
AttackBanDuration: params.Config.AttackBanDuration,
|
||||||
// or country= tag and the biased thresholds act on the answer before
|
MaxBans: params.Config.MaxBans,
|
||||||
// the request goes on.
|
}),
|
||||||
waitForGeoJS: len(params.Config.ExclusivelyAllowedCountries) > 0 ||
|
anomalies: anomaly.New(anomaly.Params{
|
||||||
params.Config.AddLookupHeaders ||
|
Client: params.Config.AnomalyClient,
|
||||||
params.Config.TagsNeedLookup() ||
|
Net: params.Config.AnomalyNet,
|
||||||
biasedThresholdsSet(params.Config),
|
ASN: params.Config.AnomalyASN,
|
||||||
besideLookupLimit: cmp.Or(
|
Total: params.Config.AnomalyTotal,
|
||||||
params.BesideLookupTimeLimit,
|
WatchedNet: params.Config.AnomalyWatchedNet,
|
||||||
besideLookupTimeLimit,
|
NetV4Prefix: params.Config.AnomalyNetV4Prefix,
|
||||||
),
|
NetV6Prefix: params.Config.AnomalyNetV6Prefix,
|
||||||
|
NamedNetblocks: params.Config.AnomalyWatchedNets,
|
||||||
|
Alerts: params.Alerts,
|
||||||
|
}),
|
||||||
|
lookupFile: params.LookupFile,
|
||||||
|
lists: lists,
|
||||||
|
dnsbl: dnsbl,
|
||||||
|
abuseIPDB: abuseIPDB,
|
||||||
|
rules: params.Rules,
|
||||||
|
coreRuleSet: params.CoreRuleSet,
|
||||||
|
alerts: params.Alerts,
|
||||||
|
|
||||||
|
besideLookupLimit: besideLookupTimeLimit,
|
||||||
}
|
}
|
||||||
|
if params.Config.RouteWatchURL != "" {
|
||||||
|
h.routeWatch = routewatch.New(routewatch.Params{
|
||||||
|
URL: params.Config.RouteWatchURL, Now: params.Now, ProcessLog: params.ProcessLog,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// The country list, the headers, the tag settings that name an asn= or
|
||||||
|
// country= tag and the biased thresholds act on the answer before the
|
||||||
|
// request goes on.
|
||||||
|
h.waitForGeoJS = len(params.Config.ExclusivelyAllowedCountries) > 0 ||
|
||||||
|
params.Config.AddLookupHeaders || params.Config.TagsNeedLookup() ||
|
||||||
|
biasedThresholdsSet(params.Config)
|
||||||
|
h.geojs = geojs.New(geojs.Params{
|
||||||
|
URL: params.GeoJSURL,
|
||||||
|
Answered: h.addLookup,
|
||||||
|
Now: params.Now,
|
||||||
|
Asked: m.GeoJSRequests.Inc,
|
||||||
|
Failed: h.geojsFailed,
|
||||||
|
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
||||||
|
})
|
||||||
|
m.AddBansAndClients(h.ledger, h.limiter, params.Now)
|
||||||
|
m.AddRules(params.Rules)
|
||||||
|
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// newReputation returns the lists fetched from URLs, the DNSBL zones'
|
||||||
|
// verdicts and AbuseIPDB's scores, as the settings in params name them,
|
||||||
|
// with none fetched, asked for or checked yet, and adds their metrics to
|
||||||
|
// m, AbuseIPDB's while SWW_WAF_ABUSEIPDB_KEY is set.
|
||||||
|
func newReputation(
|
||||||
|
params Params, m *metrics.Metrics,
|
||||||
|
) (*reputation.Lists, *reputation.DNSBL, *reputation.AbuseIPDB) {
|
||||||
|
cfg := params.Config
|
||||||
|
lists := reputation.New(reputation.Params{
|
||||||
|
BlocklistURLs: cfg.BlocklistURLs,
|
||||||
|
Refresh: cfg.BlocklistRefresh,
|
||||||
|
ASNLimitPercentURL: cfg.ASNLimitPercentURL,
|
||||||
|
TagLists: cfg.TagLists,
|
||||||
|
Now: params.Now,
|
||||||
|
ProcessLog: params.ProcessLog,
|
||||||
|
Alerts: params.Alerts,
|
||||||
|
})
|
||||||
|
dnsbl := reputation.NewDNSBL(reputation.DNSBLParams{
|
||||||
|
Zones: cfg.DNSBLZones, Resolver: cfg.DNSBLResolver,
|
||||||
|
CacheTTL: cfg.IPReputationCacheTTL,
|
||||||
|
Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog,
|
||||||
|
Alerts: params.Alerts,
|
||||||
|
})
|
||||||
|
abuseIPDB := reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{
|
||||||
|
URL: params.AbuseIPDBURL, Key: cfg.AbuseIPDBKey, MinScore: cfg.AbuseIPDBMinScore,
|
||||||
|
DailyBudget: cfg.AbuseIPDBDailyBudget, CacheTTL: cfg.IPReputationCacheTTL,
|
||||||
|
Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog,
|
||||||
|
Alerts: params.Alerts,
|
||||||
|
})
|
||||||
|
|
||||||
|
m.AddReputation(lists, dnsbl)
|
||||||
|
|
||||||
|
if cfg.AbuseIPDBKey != "" {
|
||||||
|
m.AddAbuseIPDB(abuseIPDB)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lists, dnsbl, abuseIPDB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL,
|
// NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL,
|
||||||
@@ -230,8 +292,7 @@ type handler struct {
|
|||||||
// waitForGeoJS is true when a setting needs each request's answer from
|
// waitForGeoJS is true when a setting needs each request's answer from
|
||||||
// GeoJS before the request goes on.
|
// GeoJS before the request goes on.
|
||||||
waitForGeoJS bool
|
waitForGeoJS bool
|
||||||
// besideLookupLimit is how long the heuristics of besideLookup have for a
|
// besideLookupLimit is besideLookupTimeLimit. Only tests change it.
|
||||||
// request, as Params.BesideLookupTimeLimit says.
|
|
||||||
besideLookupLimit time.Duration
|
besideLookupLimit time.Duration
|
||||||
// awaitingReport holds the decision on each answered question by its id
|
// awaitingReport holds the decision on each answered question by its id
|
||||||
// until its report comes, and nil for a question still being handled.
|
// until its report comes, and nil for a question still being handled.
|
||||||
|
|||||||
+19
-143
@@ -16,17 +16,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/routewatch"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/rules"
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||||
@@ -442,15 +435,13 @@ func newWAF(
|
|||||||
) (*waf.Server, *output, *alerts.Queue) {
|
) (*waf.Server, *output, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
return newWAFWith(t, waf.New, geojsURL, abuseIPDBURL, now, env)
|
return newWAFWith(t, waf.New, geojsURL, now, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newWAFWith is newWAF for the server build makes, with clients checked with
|
// newWAFWith is newWAF for the server build makes.
|
||||||
// AbuseIPDB at abuseIPDBCheckURL.
|
|
||||||
func newWAFWith(
|
func newWAFWith(
|
||||||
t *testing.T, build func(waf.Params) *waf.Server,
|
t *testing.T, build func(waf.Params) *waf.Server,
|
||||||
geojsURL, abuseIPDBCheckURL string, now func() time.Time,
|
geojsURL string, now func() time.Time, env map[string]string,
|
||||||
env map[string]string,
|
|
||||||
) (*waf.Server, *output, *alerts.Queue) {
|
) (*waf.Server, *output, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -508,142 +499,27 @@ func newWAFWith(
|
|||||||
t.Fatalf("Core Rule Set: %v", err)
|
t.Fatalf("Core Rule Set: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
params := waf.Params{
|
server := build(waf.Params{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
RequestLog: out,
|
RequestLog: out,
|
||||||
ProcessLog: processLog,
|
ProcessLog: processLog,
|
||||||
LookupFile: lookupFile,
|
GeoJSURL: geojsURL,
|
||||||
CoreRuleSet: coreRuleSet,
|
AbuseIPDBURL: abuseIPDBURL,
|
||||||
Now: now,
|
LookupFile: lookupFile,
|
||||||
Rules: ruleFiles,
|
CoreRuleSet: coreRuleSet,
|
||||||
Alerts: alertQueue,
|
Now: now,
|
||||||
// The tests of the limit set it back, and hold the Core Rule Set
|
Rules: ruleFiles,
|
||||||
// with a stand-in.
|
Alerts: alertQueue,
|
||||||
BesideLookupTimeLimit: time.Hour,
|
})
|
||||||
}
|
|
||||||
addCounters(¶ms)
|
|
||||||
addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL)
|
|
||||||
|
|
||||||
server := build(params)
|
// The race detector and a busy host can slow the Core Rule Set's
|
||||||
|
// inspections past the time limit. The tests of the limit set it back, and
|
||||||
|
// hold the Core Rule Set with a stand-in.
|
||||||
|
server.SetBesideLookupTimeLimit(time.Hour)
|
||||||
|
|
||||||
return server, out, alertQueue
|
return server, out, alertQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
// addCounters adds to params the metrics, the table of clients, the ledger
|
|
||||||
// and the anomaly counters, as sww builds them from params' settings. The
|
|
||||||
// metrics read the ledger, the table of clients and the rules.
|
|
||||||
func addCounters(params *waf.Params) {
|
|
||||||
cfg := params.Config
|
|
||||||
|
|
||||||
params.Metrics = metrics.New(
|
|
||||||
cfg.MetricsBusiestCountriesAndASNs,
|
|
||||||
cfg.InstanceName,
|
|
||||||
)
|
|
||||||
params.Limiter = ratelimit.New(ratelimit.Limits{
|
|
||||||
PerMinute: cfg.RateLimitPerMinute,
|
|
||||||
PerHour: cfg.RateLimitPerHour,
|
|
||||||
PerDay: cfg.RateLimitPerDay,
|
|
||||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
|
||||||
BytesPerHour: cfg.BytesLimitPerHour,
|
|
||||||
BytesPerDay: cfg.BytesLimitPerDay,
|
|
||||||
}, cfg.MaxTrackedClients)
|
|
||||||
params.Ledger = bans.New(bans.Params{
|
|
||||||
LimitBanDuration: cfg.LimitBanDuration,
|
|
||||||
LimitBanRepeatWindow: cfg.LimitBanRepeatWindow,
|
|
||||||
LimitBanPermanentAbove: cfg.LimitBanPermanentAbove,
|
|
||||||
AttackBanDuration: cfg.AttackBanDuration,
|
|
||||||
MaxBans: cfg.MaxBans,
|
|
||||||
})
|
|
||||||
params.Anomalies = anomaly.New(anomaly.Params{
|
|
||||||
Client: cfg.AnomalyClient,
|
|
||||||
Net: cfg.AnomalyNet,
|
|
||||||
ASN: cfg.AnomalyASN,
|
|
||||||
Total: cfg.AnomalyTotal,
|
|
||||||
WatchedNet: cfg.AnomalyWatchedNet,
|
|
||||||
NetV4Prefix: cfg.AnomalyNetV4Prefix,
|
|
||||||
NetV6Prefix: cfg.AnomalyNetV6Prefix,
|
|
||||||
NamedNetblocks: cfg.AnomalyWatchedNets,
|
|
||||||
Alerts: params.Alerts,
|
|
||||||
})
|
|
||||||
|
|
||||||
params.Metrics.AddBansAndClients(
|
|
||||||
params.Ledger,
|
|
||||||
params.Limiter,
|
|
||||||
params.Now,
|
|
||||||
)
|
|
||||||
params.Metrics.AddRules(params.Rules)
|
|
||||||
}
|
|
||||||
|
|
||||||
// addOutsideSources adds to params the lists fetched from URLs, the DNSBL
|
|
||||||
// zones' verdicts, AbuseIPDB's scores from abuseIPDBCheckURL, routewatch's
|
|
||||||
// answers while its setting is set, and GeoJS at geojsURL, as sww builds
|
|
||||||
// them from params' settings and the parts addCounters added. The metrics
|
|
||||||
// read the lists, the zones' verdicts, and AbuseIPDB's while its key is set.
|
|
||||||
func addOutsideSources(
|
|
||||||
params *waf.Params, geojsURL, abuseIPDBCheckURL string,
|
|
||||||
) {
|
|
||||||
cfg := params.Config
|
|
||||||
m, limiter, ledger := params.Metrics, params.Limiter, params.Ledger
|
|
||||||
processLog, alertQueue := params.ProcessLog, params.Alerts
|
|
||||||
|
|
||||||
params.Lists = reputation.New(reputation.Params{
|
|
||||||
BlocklistURLs: cfg.BlocklistURLs,
|
|
||||||
Refresh: cfg.BlocklistRefresh,
|
|
||||||
ASNLimitPercentURL: cfg.ASNLimitPercentURL,
|
|
||||||
TagLists: cfg.TagLists,
|
|
||||||
Now: params.Now,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
params.DNSBL = reputation.NewDNSBL(reputation.DNSBLParams{
|
|
||||||
Zones: cfg.DNSBLZones,
|
|
||||||
Resolver: cfg.DNSBLResolver,
|
|
||||||
CacheTTL: cfg.IPReputationCacheTTL,
|
|
||||||
Timeout: cfg.DNSBLAbuseIPDBTimeout,
|
|
||||||
Now: params.Now,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
params.AbuseIPDB = reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{
|
|
||||||
URL: abuseIPDBCheckURL,
|
|
||||||
Key: cfg.AbuseIPDBKey,
|
|
||||||
MinScore: cfg.AbuseIPDBMinScore,
|
|
||||||
DailyBudget: cfg.AbuseIPDBDailyBudget,
|
|
||||||
CacheTTL: cfg.IPReputationCacheTTL,
|
|
||||||
Timeout: cfg.DNSBLAbuseIPDBTimeout,
|
|
||||||
Now: params.Now,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
Alerts: alertQueue,
|
|
||||||
})
|
|
||||||
|
|
||||||
if cfg.RouteWatchURL != "" {
|
|
||||||
params.RouteWatch = routewatch.New(routewatch.Params{
|
|
||||||
URL: cfg.RouteWatchURL,
|
|
||||||
Now: params.Now,
|
|
||||||
ProcessLog: processLog,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
params.GeoJS = geojs.New(geojs.Params{
|
|
||||||
URL: geojsURL,
|
|
||||||
Answered: func(answer geojs.Answer) {
|
|
||||||
waf.AddLookup(cfg, limiter, ledger, answer)
|
|
||||||
},
|
|
||||||
Now: params.Now,
|
|
||||||
Asked: m.GeoJSRequests.Inc,
|
|
||||||
Failed: func(err error, askingAgainIn time.Duration) {
|
|
||||||
waf.GeoJSFailed(processLog, alertQueue, m, err, askingAgainIn)
|
|
||||||
},
|
|
||||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
|
||||||
})
|
|
||||||
|
|
||||||
m.AddReputation(params.Lists, params.DNSBL)
|
|
||||||
|
|
||||||
if cfg.AbuseIPDBKey != "" {
|
|
||||||
m.AddAbuseIPDB(params.AbuseIPDB)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// newClient returns an HTTP client that sends requests as they are made,
|
// newClient returns an HTTP client that sends requests as they are made,
|
||||||
// with no compression of its own.
|
// with no compression of its own.
|
||||||
func newClient(t *testing.T) *http.Client {
|
func newClient(t *testing.T) *http.Client {
|
||||||
|
|||||||
@@ -38,11 +38,8 @@ func (rateLimits) check(
|
|||||||
rq.line.BytesLimitPercent, rq.line.BytesLimitPercentSetting =
|
rq.line.BytesLimitPercent, rq.line.BytesLimitPercentSetting =
|
||||||
rq.bytesLimitPercent.logged()
|
rq.bytesLimitPercent.logged()
|
||||||
|
|
||||||
counts, hit, over := rq.h.limiter.Count(
|
counts, hit, over := rq.h.limiter.Count(rq.h.clientGroup(rq.client), now,
|
||||||
clientGroup(rq.h.config, rq.client),
|
rq.rateLimitPercent.percent)
|
||||||
now,
|
|
||||||
rq.rateLimitPercent.percent,
|
|
||||||
)
|
|
||||||
rq.line.Counts = counts
|
rq.line.Counts = counts
|
||||||
|
|
||||||
if !over {
|
if !over {
|
||||||
@@ -62,7 +59,7 @@ func (rateLimits) check(
|
|||||||
// limit. It counts nothing.
|
// limit. It counts nothing.
|
||||||
func (rq *request) overAWholeRateLimit(now time.Time) bool {
|
func (rq *request) overAWholeRateLimit(now time.Time) bool {
|
||||||
return rq.countedByRateLimits() &&
|
return rq.countedByRateLimits() &&
|
||||||
rq.h.limiter.OverARateLimit(clientGroup(rq.h.config, rq.client), now)
|
rq.h.limiter.OverARateLimit(rq.h.clientGroup(rq.client), now)
|
||||||
}
|
}
|
||||||
|
|
||||||
// countedByRateLimits reports whether the rate limits count the request: its
|
// countedByRateLimits reports whether the rate limits count the request: its
|
||||||
|
|||||||
@@ -36,11 +36,8 @@ func (refusalLimit) check(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
hit, over := rq.h.limiter.CountRefusal(
|
hit, over := rq.h.limiter.CountRefusal(rq.h.clientGroup(rq.client), now,
|
||||||
clientGroup(rq.h.config, rq.client),
|
cfg.RefusalsLimitPerMinute)
|
||||||
now,
|
|
||||||
cfg.RefusalsLimitPerMinute,
|
|
||||||
)
|
|
||||||
if !over {
|
if !over {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func (rq *request) noteHit(hit bans.ReputationHit, reason string) {
|
|||||||
Event: alerts.EventReputationHit,
|
Event: alerts.EventReputationHit,
|
||||||
Host: rq.site,
|
Host: rq.site,
|
||||||
Client: rq.client,
|
Client: rq.client,
|
||||||
Netblock: clientGroup(rq.h.config, rq.client),
|
Netblock: rq.h.clientGroup(rq.client),
|
||||||
ASN: rq.line.ASN,
|
ASN: rq.line.ASN,
|
||||||
ASName: rq.line.ASName,
|
ASName: rq.line.ASName,
|
||||||
Country: rq.line.Country,
|
Country: rq.line.Country,
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ func (h *handler) startRequest(
|
|||||||
RequestID: requestID(r, isQuestion),
|
RequestID: requestID(r, isQuestion),
|
||||||
PeerIP: peer.String(),
|
PeerIP: peer.String(),
|
||||||
ForwardedFor: strings.Join(forwardedFor, ", "),
|
ForwardedFor: strings.Join(forwardedFor, ", "),
|
||||||
ClientGroup: clientGroup(h.config, client).String(),
|
ClientGroup: h.clientGroup(client).String(),
|
||||||
ContentType: r.Header.Get("Content-Type"),
|
ContentType: r.Header.Get("Content-Type"),
|
||||||
RequestHeaders: requestHeaders(
|
RequestHeaders: requestHeaders(
|
||||||
r,
|
r,
|
||||||
@@ -365,7 +365,7 @@ func (rq *request) addToHistory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rq.h.limiter.AddToHistory(
|
rq.h.limiter.AddToHistory(
|
||||||
clientGroup(rq.h.config, rq.client),
|
rq.h.clientGroup(rq.client),
|
||||||
rq.h.now(),
|
rq.h.now(),
|
||||||
request,
|
request,
|
||||||
)
|
)
|
||||||
@@ -373,7 +373,7 @@ func (rq *request) addToHistory() {
|
|||||||
|
|
||||||
answer, found := rq.answerAtTheEnd()
|
answer, found := rq.answerAtTheEnd()
|
||||||
if found {
|
if found {
|
||||||
AddLookup(rq.h.config, rq.h.limiter, rq.h.ledger, answer)
|
rq.h.addLookup(answer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,7 +394,7 @@ func (rq *request) countAnomalies() {
|
|||||||
rq.h.anomalies.Count(rq.h.now(), anomaly.Request{
|
rq.h.anomalies.Count(rq.h.now(), anomaly.Request{
|
||||||
Host: rq.site,
|
Host: rq.site,
|
||||||
Client: rq.client,
|
Client: rq.client,
|
||||||
ClientGroup: clientGroup(rq.h.config, rq.client),
|
ClientGroup: rq.h.clientGroup(rq.client),
|
||||||
ASN: answer.ASN,
|
ASN: answer.ASN,
|
||||||
ASName: answer.ASName,
|
ASName: answer.ASName,
|
||||||
Country: answer.Country,
|
Country: answer.Country,
|
||||||
@@ -425,7 +425,7 @@ func (rq *request) answerAtTheEnd() (geojs.Answer, bool) {
|
|||||||
return rq.lookupAnswer, true
|
return rq.lookupAnswer, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return rq.h.geojs.Kept(clientGroup(rq.h.config, rq.client))
|
return rq.h.geojs.Kept(rq.h.clientGroup(rq.client))
|
||||||
}
|
}
|
||||||
|
|
||||||
// responseWriter is waf's answer to a request. It notes the status and
|
// responseWriter is waf's answer to a request. It notes the status and
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (routeWatchTags) check(
|
|||||||
|
|
||||||
route, missing := rq.h.routeWatch.Route(
|
route, missing := rq.h.routeWatch.Route(
|
||||||
ctx,
|
ctx,
|
||||||
clientGroup(rq.h.config, rq.client),
|
rq.h.clientGroup(rq.client),
|
||||||
rq.client,
|
rq.client,
|
||||||
)
|
)
|
||||||
if missing != "" {
|
if missing != "" {
|
||||||
|
|||||||
@@ -20,13 +20,8 @@ main() {
|
|||||||
# the version a build stage derives from the .git in the context.
|
# the version a build stage derives from the .git in the context.
|
||||||
version="$(git describe --tags --always --dirty 2>/dev/null || true)"
|
version="$(git describe --tags --always --dirty 2>/dev/null || true)"
|
||||||
[ -n "$version" ] || version="unknown"
|
[ -n "$version" ] || version="unknown"
|
||||||
revision="$(git rev-parse HEAD 2>/dev/null || true)"
|
|
||||||
[ -n "$revision" ] || revision="unknown"
|
|
||||||
created="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
||||||
docker build --no-cache \
|
docker build --no-cache \
|
||||||
--build-arg VERSION="$version" \
|
--build-arg VERSION="$version" \
|
||||||
--build-arg REVISION="$revision" \
|
|
||||||
--build-arg CREATED="$created" \
|
|
||||||
-t "$("$SCRIPT_DIR/projectname")" .
|
-t "$("$SCRIPT_DIR/projectname")" .
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,8 @@ main() {
|
|||||||
# the version a build stage derives from the .git in the context.
|
# the version a build stage derives from the .git in the context.
|
||||||
version="$(git describe --tags --always --dirty 2>/dev/null || true)"
|
version="$(git describe --tags --always --dirty 2>/dev/null || true)"
|
||||||
[ -n "$version" ] || version="unknown"
|
[ -n "$version" ] || version="unknown"
|
||||||
revision="$(git rev-parse HEAD 2>/dev/null || true)"
|
|
||||||
[ -n "$revision" ] || revision="unknown"
|
|
||||||
created="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
||||||
docker build --no-cache \
|
docker build --no-cache \
|
||||||
--build-arg VERSION="$version" \
|
--build-arg VERSION="$version" \
|
||||||
--build-arg REVISION="$revision" \
|
|
||||||
--build-arg CREATED="$created" \
|
|
||||||
-t "$("$SCRIPT_DIR/projectname")" .
|
-t "$("$SCRIPT_DIR/projectname")" .
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user