I have spend a huge amount of time to find a way to bypass basic authentication depending on source IP/network and never find a way to do so. But yesterday i finally succeeded to manage this need.
note : i am using Traefik with Docker without Swarm. I am not an export and i am learning.
Goal:
bypass basic auth for a defined list of networks/ip using a single host rule and a single entrypoint
Trick
Create 2 routers with the same host rule and use HeadersRegexp
Thank you for sharing this. People are really keen on "complete working examples" here, so this could be improved by posting the rest of the configuration, that one can paste into the files and up with their docker-compose (or just docker).
Hi Zespri, thank you for your feedback.
It's not that easy to give a full working example without double checking private informations have been changed.
Here is a more complete configuration.
A simple example that works, based on inspiration in this post.
In short you have two routers + priorities with the highest granting without auth middleware and the next priority the production-facing rule.
HTH, cost me a morning to bash that one out.
version: '3'
services:
traefik:
# The official v2 Traefik docker image
image: traefik:v2.2
container_name: "traefik"
# Enables the web UI and tells Traefik to listen to docker
command: --api.insecure=true --providers.docker
ports:
# The HTTP port
- "80:80"
# The Web UI (enabled by --api.insecure=true)
- "8080:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
whoami:
# A container that exposes an API to show its IP address
image: containous/whoami
labels:
- "traefik.http.routers.whoami2.rule=Host(`whoami.localhost`) && HeadersRegexp(`X-Real-Ip`, `^(192\\.168\\.99\\.2)`)"
- "traefik.http.routers.whoami2.priority=100"
- "traefik.http.routers.whoami2.middlewares=secured2"
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.priority=99"
- "traefik.http.routers.whoami.middlewares=secured"
- "traefik.http.middlewares.secured.chain.middlewares=auth"
- "traefik.http.middlewares.secured2.chain.middlewares="
- "traefik.http.middlewares.auth.basicauth.users=test1:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/"
#- "traefik.http.middlewares.known-ips.ipwhitelist.sourceRange=192.168.1.7,127.0.0.1/32"
[Reviving the topic since it's at the top of Google search results.]
This is working for me with Traefik 2.6.1 using ClientIp condition instead of HeadersRegexp to check for both hosts and subnets. It uses Kubernetes syntax but should be easily adaptable.