diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 83c7b6fe..83563a1e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -58,6 +58,12 @@ jobs: targets: image-local env: DEFAULT_TAG: ${{ env.BUILD_TAG }} + - + name: Check reverse proxy handling + run: | + docker run --rm --network none \ + --mount type=bind,source="$PWD/test/proxy.sh",target=/proxy-test.sh,readonly \ + --entrypoint sh "$BUILD_TAG" /proxy-test.sh - name: Start run: | diff --git a/README.md b/README.md index a3162ee9..4e8602cc 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ ___ * [Notes](#notes) * [LNMS command](#lnms-command) * [Validate](#validate) + * [Reverse proxy](#reverse-proxy) * [Dispatcher service container](#dispatcher-service-container) * [Syslog-ng container](#syslog-ng-container) * [Snmptrapd container](#snmptrapd-container) @@ -122,10 +123,15 @@ linux/s390x * `FPM_PM_MAX_SPARE_SERVERS`: FPM max spare servers (default: `6`) * `OPCACHE_MEM_SIZE`: PHP OpCache memory consumption (default `128`) * `LISTEN_IPV6`: Enable IPv6 for Nginx (default `true`) -* `REAL_IP_FROM`: Trusted addresses that are known to send correct replacement addresses (default `0.0.0.0/32`) +* `REAL_IP_FROM`: Override the trusted proxy address or CIDR for Nginx access + logs. Unset or `0.0.0.0/32` uses LibreNMS's `APP_TRUSTED_PROXIES` + configuration. * `REAL_IP_HEADER`: Request header field whose value will be used to replace the client address (default `X-Forwarded-For`) * `LOG_IP_VAR`: Use another variable to retrieve the remote IP address for access [log_format](http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format) on Nginx. (default `remote_addr`) -* `APP_TRUSTED_PROXIES`: IPs or CIDR ranges trusted to set forwarding headers (X-Forwarded-For, X-Forwarded-Proto, etc.). These hosts can spoof any request header, including authentication headers. +* `APP_TRUSTED_PROXIES`: Proxy IP addresses or CIDR ranges, separated by commas + without spaces, trusted to supply forwarded client addresses, hostnames, + ports, and schemes (default `127.0.0.1`). Also used for Nginx access logs. + See [Reverse proxy](#reverse-proxy). * `SESSION_DRIVER`: [Driver to use for session storage](https://github.com/librenms/librenms/blob/master/config/session.php) (default `file`) * `CACHE_DRIVER`: [Driver to use for cache and locks](https://github.com/librenms/librenms/blob/master/config/cache.php) (default `database`) @@ -333,6 +339,36 @@ SNMP | NET-SNMP 5.8 [WARN] Updates are managed through the official Docker image ``` +### Reverse proxy + +Set `APP_TRUSTED_PROXIES` in the LibreNMS container environment to your trusted +reverse proxy IP addresses or network ranges. The proxy must forward the +original host and scheme using `X-Forwarded-Host` and +`X-Forwarded-Proto`, and the client address using `X-Forwarded-For`. + +For example, if the proxy connects from `192.0.2.10`: + +```yaml +environment: + APP_TRUSTED_PROXIES: "192.0.2.10" +``` + +Restrict this setting to your actual proxies. Docker network subnets vary +between deployments, so do not assume a particular subnet or use `*` to work +around an address mismatch. Recreate the container after changing its +environment. Startup caches these settings for PHP-FPM, so `CLEAR_ENV=yes` +can remain enabled and no manual edits to the generated `.env` are needed. + +Nginx automatically uses the same trusted proxies to record client addresses +in its access logs. `REAL_IP_FROM` remains available to override Nginx's trust +configuration independently. If you previously configured only `REAL_IP_FROM`, +replace it with `APP_TRUSTED_PROXIES` to apply the setting to both LibreNMS and +Nginx. + +When `APP_TRUSTED_PROXIES` is `*` or `**`, LibreNMS still handles forwarded +headers, but Nginx logs the connection address. Set `REAL_IP_FROM` to an +explicit proxy IP address or CIDR to enable client address logging in that case. + ### Dispatcher service container If you want to enable the new [Dispatcher service](https://docs.librenms.org/Extensions/Dispatcher-Service/), diff --git a/examples/traefik/compose.yml b/examples/traefik/compose.yml index d5760232..3b3a4fc8 100644 --- a/examples/traefik/compose.yml +++ b/examples/traefik/compose.yml @@ -33,8 +33,6 @@ services: volumes: - "./acme.json:/acme.json" - "/var/run/docker.sock:/var/run/docker.sock" - networks: - - librenms_net restart: always db: @@ -54,8 +52,6 @@ services: - "MYSQL_DATABASE=${MYSQL_DATABASE}" - "MYSQL_USER=${MYSQL_USER}" - "MYSQL_PASSWORD=${MYSQL_PASSWORD}" - networks: - - librenms_net restart: always redis: @@ -63,8 +59,6 @@ services: container_name: librenms_redis environment: - "TZ=${TZ}" - networks: - - librenms_net restart: always msmtpd: @@ -72,8 +66,6 @@ services: container_name: librenms_msmtpd env_file: - "./msmtpd.env" - networks: - - librenms_net restart: always librenms: @@ -105,9 +97,6 @@ services: - "DB_USER=${MYSQL_USER}" - "DB_PASSWORD=${MYSQL_PASSWORD}" - "DB_TIMEOUT=60" - - "APP_TRUSTED_PROXIES=172.28.0.0/16" - networks: - - librenms_net restart: always dispatcher: @@ -132,8 +121,6 @@ services: - "DB_TIMEOUT=60" - "DISPATCHER_NODE_ID=dispatcher1" - "SIDECAR_DISPATCHER=1" - networks: - - librenms_net restart: always syslogng: @@ -164,8 +151,6 @@ services: - "DB_PASSWORD=${MYSQL_PASSWORD}" - "DB_TIMEOUT=60" - "SIDECAR_SYSLOGNG=1" - networks: - - librenms_net restart: always snmptrapd: @@ -196,13 +181,4 @@ services: - "DB_PASSWORD=${MYSQL_PASSWORD}" - "DB_TIMEOUT=60" - "SIDECAR_SNMPTRAPD=1" - networks: - - librenms_net restart: always - -networks: - librenms_net: - driver: bridge - ipam: - config: - - subnet: 172.28.0.0/16 diff --git a/examples/traefik/librenms.env b/examples/traefik/librenms.env index d650129d..ecb5e478 100644 --- a/examples/traefik/librenms.env +++ b/examples/traefik/librenms.env @@ -4,7 +4,9 @@ UPLOAD_MAX_SIZE=16M OPCACHE_MEM_SIZE=128 REAL_IP_FROM=0.0.0.0/32 REAL_IP_HEADER=X-Forwarded-For -LOG_IP_VAR=http_x_forwarded_for +LOG_IP_VAR=remote_addr + +# Set APP_TRUSTED_PROXIES for your reverse proxy. See README.md#reverse-proxy. CACHE_DRIVER=redis SESSION_DRIVER=redis diff --git a/rootfs/etc/s6-overlay/scripts/config b/rootfs/etc/s6-overlay/scripts/config index f1458afd..75ff1f17 100755 --- a/rootfs/etc/s6-overlay/scripts/config +++ b/rootfs/etc/s6-overlay/scripts/config @@ -34,10 +34,6 @@ FPM_PM_START_SERVERS=${FPM_PM_START_SERVERS:-2} FPM_PM_MIN_SPARE_SERVERS=${FPM_PM_MIN_SPARE_SERVERS:-1} FPM_PM_MAX_SPARE_SERVERS=${FPM_PM_MAX_SPARE_SERVERS:-6} OPCACHE_MEM_SIZE=${OPCACHE_MEM_SIZE:-128} -LISTEN_IPV6=${LISTEN_IPV6:-true} -REAL_IP_FROM=${REAL_IP_FROM:-"0.0.0.0/32"} -REAL_IP_HEADER=${REAL_IP_HEADER:-"X-Forwarded-For"} -LOG_IP_VAR=${LOG_IP_VAR:-remote_addr} MAX_INPUT_VARS=${MAX_INPUT_VARS:-1000} MEMCACHED_PORT=${MEMCACHED_PORT:-11211} @@ -75,18 +71,6 @@ echo "Setting OpCache configuration..." sed -e "s/@OPCACHE_MEM_SIZE@/$OPCACHE_MEM_SIZE/g" \ /tpls/etc/php84/conf.d/opcache.ini >/etc/php84/conf.d/opcache.ini -# Nginx -echo "Setting Nginx configuration..." -sed -e "s#@UPLOAD_MAX_SIZE@#$UPLOAD_MAX_SIZE#g" \ - -e "s#@REAL_IP_FROM@#$REAL_IP_FROM#g" \ - -e "s#@REAL_IP_HEADER@#$REAL_IP_HEADER#g" \ - -e "s#@LOG_IP_VAR@#$LOG_IP_VAR#g" \ - /tpls/etc/nginx/nginx.conf >/etc/nginx/nginx.conf - -if [ "$LISTEN_IPV6" != "true" ]; then - sed -e '/listen \[::\]:/d' -i /etc/nginx/nginx.conf -fi - # SNMP echo "Updating SNMP community..." file_env 'LIBRENMS_SNMP_COMMUNITY' 'librenmsdocker' diff --git a/rootfs/etc/s6-overlay/scripts/config-main b/rootfs/etc/s6-overlay/scripts/config-main index 87f110a8..5f6b029c 100755 --- a/rootfs/etc/s6-overlay/scripts/config-main +++ b/rootfs/etc/s6-overlay/scripts/config-main @@ -85,3 +85,5 @@ artisan db:seed --force --no-ansi --no-interaction echo "Clear cache" artisan cache:clear --no-interaction artisan config:cache --no-interaction + +/etc/s6-overlay/scripts/config-nginx diff --git a/rootfs/etc/s6-overlay/scripts/config-nginx b/rootfs/etc/s6-overlay/scripts/config-nginx new file mode 100755 index 00000000..24ec643f --- /dev/null +++ b/rootfs/etc/s6-overlay/scripts/config-nginx @@ -0,0 +1,27 @@ +#!/bin/bash +set -e + +cd "${LIBRENMS_PATH}" + +UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE:-16M} +LISTEN_IPV6=${LISTEN_IPV6:-true} +REAL_IP_HEADER=${REAL_IP_HEADER:-"X-Forwarded-For"} +LOG_IP_VAR=${LOG_IP_VAR:-remote_addr} + +echo "Setting Nginx configuration..." +if [ -n "${REAL_IP_FROM}" ] && [ "${REAL_IP_FROM}" != "0.0.0.0/32" ]; then + realIpFrom="set_real_ip_from ${REAL_IP_FROM};" +else + # Use the same effective proxy list as LibreNMS, including .env settings. + realIpFrom=$(php /etc/s6-overlay/scripts/nginx-trusted-proxies.php) +fi + +sed -e "s#@UPLOAD_MAX_SIZE@#$UPLOAD_MAX_SIZE#g" \ + -e "s#@REAL_IP_FROM@#$realIpFrom#g" \ + -e "s#@REAL_IP_HEADER@#$REAL_IP_HEADER#g" \ + -e "s#@LOG_IP_VAR@#$LOG_IP_VAR#g" \ + /tpls/etc/nginx/nginx.conf >/etc/nginx/nginx.conf + +if [ "$LISTEN_IPV6" != "true" ]; then + sed -e '/listen \[::\]:/d' -i /etc/nginx/nginx.conf +fi diff --git a/rootfs/etc/s6-overlay/scripts/nginx-trusted-proxies.php b/rootfs/etc/s6-overlay/scripts/nginx-trusted-proxies.php new file mode 100644 index 00000000..18599955 --- /dev/null +++ b/rootfs/etc/s6-overlay/scripts/nginx-trusted-proxies.php @@ -0,0 +1,26 @@ +bootstrap($app); +$proxies = $app['config']->get('trustedproxy.proxies'); + +// Leave wildcard handling to LibreNMS and log the connection address in Nginx. +if ($proxies === '*' || $proxies === '**') { + return; +} + +echo 'real_ip_recursive on; '; +Symfony\Component\HttpFoundation\Request::setTrustedProxies((array) $proxies, 0); +$proxies = Symfony\Component\HttpFoundation\Request::getTrustedProxies(); +foreach ($proxies as $proxy) { + if ($proxy !== '') { + [$address, $prefix] = array_pad(explode('/', $proxy, 2), 2, null); + if (($prefix !== null && !preg_match('/^(0|[1-9][0-9]*)$/D', $prefix)) + || !Symfony\Component\HttpFoundation\IpUtils::checkIp($address, $proxy)) { + fwrite(STDERR, "Ignoring invalid trusted proxy address: $proxy\n"); + continue; + } + echo "set_real_ip_from $proxy; "; + } +} diff --git a/rootfs/tpls/etc/nginx/nginx.conf b/rootfs/tpls/etc/nginx/nginx.conf index 31b0daa3..caeec9db 100644 --- a/rootfs/tpls/etc/nginx/nginx.conf +++ b/rootfs/tpls/etc/nginx/nginx.conf @@ -35,7 +35,7 @@ http { tcp_nodelay on; ## Handling of IPs in proxied and load balancing situations - set_real_ip_from @REAL_IP_FROM@; + @REAL_IP_FROM@ real_ip_header @REAL_IP_HEADER@; # Log @@ -85,6 +85,9 @@ http { set $path_info $fastcgi_path_info; try_files $fastcgi_script_name =404; include fastcgi_params; + # Preserve the peer for LibreNMS trusted proxy validation. + fastcgi_param REMOTE_ADDR $realip_remote_addr; + fastcgi_param REMOTE_PORT $realip_remote_port; fastcgi_param SERVER_SOFTWARE ""; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $path_info; diff --git a/test/proxy.sh b/test/proxy.sh new file mode 100755 index 00000000..1aa5f868 --- /dev/null +++ b/test/proxy.sh @@ -0,0 +1,141 @@ +#!/bin/sh +# Run in a disposable LibreNMS image with this script mounted at /proxy-test.sh: +# docker run --rm --network none --entrypoint sh IMAGE /proxy-test.sh +set -eu + +cd /opt/librenms +workdir=$(mktemp -d /tmp/librenms-proxy.XXXXXX) +chmod 755 "$workdir" +mkdir -p /var/run/nginx /var/run/php-fpm + +# Exercise the same cached configuration used by web requests at startup. +export APP_TRUSTED_PROXIES=127.0.0.1,192.0.2.10,2001:db8::/32 +gosu librenms php artisan config:cache --no-interaction +unset APP_TRUSTED_PROXIES +expected_proxies='["127.0.0.1","192.0.2.10","2001:db8::/32"]' +cat >"$workdir/index.php" <<'PHP' +make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); +$middleware = new App\Http\Middleware\TrustProxies; +$middleware->handle(Illuminate\Http\Request::capture(), function ($request) { + header('Content-Type: application/json'); + echo json_encode([ + 'worker_env' => getenv('APP_TRUSTED_PROXIES'), + 'cached_proxies' => config('trustedproxy.proxies'), + 'remote_addr' => $request->server('REMOTE_ADDR'), + 'remote_port' => $request->server('REMOTE_PORT'), + 'scheme' => $request->getScheme(), + 'client_ip' => $request->ip(), + ]); +}); +PHP + +sed -e 's/@CLEAR_ENV@/yes/g' \ + -e 's/@FPM_PM_MAX_CHILDREN@/2/g' \ + -e 's/@FPM_PM_START_SERVERS@/1/g' \ + -e 's/@FPM_PM_MIN_SPARE_SERVERS@/1/g' \ + -e 's/@FPM_PM_MAX_SPARE_SERVERS@/1/g' \ + -e 's/@UPLOAD_MAX_SIZE@/16M/g' \ + -e 's/@MEMORY_LIMIT@/256M/g' \ + /tpls/etc/php84/php-fpm.d/www.conf >"$workdir/fpm.conf" +printf 'user = librenms\ngroup = librenms\nlisten.owner = librenms\nlisten.group = librenms\n' >>"$workdir/fpm.conf" +php-fpm84 -d opcache.enable=0 -y "$workdir/fpm.conf" & +fpm_pid=$! +nginx_pid='' +cleanup() { + if [ -n "$nginx_pid" ]; then + kill "$nginx_pid" 2>/dev/null || true + fi + kill "$fpm_pid" 2>/dev/null || true +} +trap cleanup EXIT + +check_proxy() { + name=$1 + real_ip_from=$2 + source_ip=$3 + forwarded=$4 + expected_scheme=$5 + expected_client=$6 + expected_log_ip=$7 + + REAL_IP_FROM="$real_ip_from" /etc/s6-overlay/scripts/config-nginx + sed -e '1i user librenms;' \ + -e 's/worker_processes auto;/worker_processes 1;/' \ + -e 's#/proc/self/fd/2 info;#/proc/self/fd/2 warn;#' \ + -e 's#include mime.types;#include /etc/nginx/mime.types;#' \ + -e 's#include fastcgi_params;#include /etc/nginx/fastcgi_params;#' \ + -e "s#root /opt/librenms/html;#root $workdir;#" \ + -e "s#access_log /proc/self/fd/1 main;#access_log $workdir/access.log main;#" \ + /etc/nginx/nginx.conf >"$workdir/nginx.conf" + nginx -t -c "$workdir/nginx.conf" + nginx -c "$workdir/nginx.conf" -g 'daemon off;' & + nginx_pid=$! + + set -- + if [ "$forwarded" != no ]; then + if [ "$forwarded" = yes ]; then + forwarded=198.51.100.8 + fi + set -- -H "X-Forwarded-For: $forwarded" -H 'X-Forwarded-Proto: https' + fi + curl --fail --silent --show-error --retry 5 --retry-connrefused \ + --interface "$source_ip" "$@" http://127.0.0.1:8000/ >"$workdir/response.json" + # PHP variables must reach PHP without shell expansion. + # shellcheck disable=SC2016 + php -r ' + $result = json_decode(file_get_contents($argv[1]), true, flags: JSON_THROW_ON_ERROR); + if ($result["worker_env"] !== false + || $result["cached_proxies"] !== json_decode($argv[5], true, flags: JSON_THROW_ON_ERROR) + || $result["remote_addr"] !== $argv[2] + || (int) $result["remote_port"] <= 0 + || $result["scheme"] !== $argv[3] + || !in_array($result["client_ip"], explode(",", $argv[4]), true)) { + fwrite(STDERR, json_encode($result) . PHP_EOL); + exit(1); + } + ' "$workdir/response.json" "$source_ip" "$expected_scheme" "$expected_client" "$expected_proxies" + + kill -QUIT "$nginx_pid" + wait "$nginx_pid" + nginx_pid='' + if ! grep -q "^$expected_log_ip - " "$workdir/access.log"; then + echo "Expected Nginx client address: $expected_log_ip" >&2 + cat "$workdir/access.log" >&2 + exit 1 + fi + : >"$workdir/access.log" + echo "PASS: $name" +} + +check_proxy explicit-log-trust-override 192.0.2.0/24 127.0.0.1 yes https 198.51.100.8 127.0.0.1 +check_proxy legacy-example-default 0.0.0.0/32 127.0.0.1 yes https 198.51.100.8 198.51.100.8 +check_proxy automatic-client-logging '' 127.0.0.1 yes https 198.51.100.8 198.51.100.8 +check_proxy trusted-proxy-chain '' 127.0.0.1 '198.51.100.8, 192.0.2.10' https 198.51.100.8 198.51.100.8 +check_proxy untrusted-peer '' 127.0.0.2 yes http 127.0.0.2 127.0.0.2 +check_proxy untrusted-with-rewriting 127.0.0.0/8 127.0.0.2 yes http 127.0.0.2 198.51.100.8 +check_proxy direct-without-headers '' 127.0.0.1 no http 127.0.0.1 127.0.0.1 + +for wildcard in '*' '**'; do + APP_TRUSTED_PROXIES="$wildcard" gosu librenms php artisan config:cache --no-interaction + expected_proxies="\"$wildcard\"" + check_proxy "wildcard-proxies-$wildcard" '' 127.0.0.2 yes https 198.51.100.8 127.0.0.2 + check_proxy "wildcard-proxy-chain-$wildcard" '' 127.0.0.2 '198.51.100.8, 192.0.2.10' https 192.0.2.10,198.51.100.8 127.0.0.2 + check_proxy "wildcard-log-trust-override-$wildcard" 127.0.0.0/8 127.0.0.2 yes https 198.51.100.8 198.51.100.8 +done + +APP_TRUSTED_PROXIES='' gosu librenms php artisan config:cache --no-interaction +expected_proxies='""' +check_proxy no-trusted-proxies '' 127.0.0.1 yes http 127.0.0.1 127.0.0.1 + +APP_TRUSTED_PROXIES='127.0.0.1,traefik, 127.0.0.2,bad#value,bad&value,192.0.2.10/33,2001:db8::/129,192.0.2.10/not-a-prefix' \ + gosu librenms php artisan config:cache --no-interaction +expected_proxies='["127.0.0.1","traefik"," 127.0.0.2","bad#value","bad&value","192.0.2.10/33","2001:db8::/129","192.0.2.10/not-a-prefix"]' +check_proxy invalid-proxies-untrusted-peer '' 127.0.0.2 yes http 127.0.0.2 127.0.0.2 +check_proxy invalid-proxies-trusted-peer '' 127.0.0.1 yes https 198.51.100.8 198.51.100.8 + +APP_TRUSTED_PROXIES='127.0.0.1,private_ranges' gosu librenms php artisan config:cache --no-interaction +expected_proxies='["127.0.0.1","private_ranges"]' +check_proxy private-ranges-alias '' 127.0.0.2 yes https 198.51.100.8 198.51.100.8