Traefik Forward Auth is incredibly useful to secure services with an additional layer of authentication, provided by an OIDC-compatible provider. The simplest possible provider is a self-hosted instance of CoreOS's Dex, configured with a static username and password. This recipe will "get you started" with Traefik Forward Auth, providing a basic authentication layer. In time, you might want to migrate to a "public" provider, like Google, or GitHub, or to a Keycloak installation.
DNS entry for your auth host ("auth.yourdomain.com" is a good choice), pointed to your keepalived IP
Preparation
Setup dex config
Create /var/data/config/dex/config.yml something like the following (this is a bare-bones, minimal example). At the very least, you want to replace all occurances of example.com with your own domain name. (If you change nothing else, your ID is foo, your secret is bar, your username is admin@yourdomain, and your password is password):
# The base path of dex and the external name of the OpenID Connect service.## This is the canonical URL that all clients MUST use to refer to dex. If a# path is provided, dex's HTTP service will listen at a non-root URL.issuer:https://dex.example.comstorage:type:sqlite3config:file:var/sqlite/dex.dbweb:http:0.0.0.0:5556oauth2:skipApprovalScreen:truestaticClients:-id:fooredirectURIs:-'https://auth.example.com/_oauth'name:'example.com'secret:barenablePasswordDB:truestaticPasswords:-email:"admin@example.com"# bcrypt hash of the string "password"hash:"$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"username:"admin"userID:"08a8684b-db88-4b73-90a9-3cd1661f5466"
Prepare Traefik Forward Auth environment
Create /var/data/config/traefik-forward-auth/traefik-forward-auth.env per the following example configuration:
"Premix" is a git repository which includes necessary docker-compose and env files for all published recipes. This means that you can launch any recipe with just a git pull and a docker stack deploy 👍.
🚀 Update: Premix now includes an ansible playbook, enabling you to deploy an entire stack + recipes, with a single ansible command! (more here)
Setup Docker Stack for Traefik Forward Auth
Now create a docker swarm config file for traefik-forward-auth, in docker-compose syntax (v3), per the following example:
version:"3.2"services:traefik-forward-auth:image:thomseddon/traefik-forward-auth:2.2.0env_file:/var/data/config/traefik-forward-auth/traefik-forward-auth.envvolumes:-/var/data/config/traefik-forward-auth/config.ini:/config.ini:ronetworks:-traefik_publicdeploy:labels:# traefikv1-"traefik.port=4181"-"traefik.frontend.rule=Host:auth.example.com"-"traefik.frontend.auth.forward.address=http://traefik-forward-auth:4181"-"traefik.frontend.auth.forward.trustForwardHeader=true"# traefikv2-"traefik.docker.network=traefik_public"-"traefik.http.routers.auth.rule=Host(`auth.example.com`)"-"traefik.http.routers.auth.entrypoints=https"-"traefik.http.routers.auth.tls=true"-"traefik.http.routers.auth.tls.domains[0].main=example.com"-"traefik.http.routers.auth.tls.domains[0].sans=*.example.com"-"traefik.http.routers.auth.tls.certresolver=main"-"traefik.http.routers.auth.service=auth@docker"-"traefik.http.services.auth.loadbalancer.server.port=4181"-"traefik.http.middlewares.forward-auth.forwardauth.address=http://traefik-forward-auth:4181"-"traefik.http.middlewares.forward-auth.forwardauth.trustForwardHeader=true"-"traefik.http.middlewares.forward-auth.forwardauth.authResponseHeaders=X-Forwarded-User"-"traefik.http.routers.auth.middlewares=forward-auth"# This simply validates that traefik forward authentication is workingwhoami:image:containous/whoaminetworks:-traefik_publicdeploy:labels:# traefik-"traefik.enable=true"-"traefik.docker.network=traefik_public"# traefikv1-"traefik.frontend.rule=Host:whoami.example.com"-"traefik.http.services.whoami.loadbalancer.server.port=80"-"traefik.frontend.auth.forward.address=http://traefik-forward-auth:4181"-"traefik.frontend.auth.forward.authResponseHeaders=X-Forwarded-User"-"traefik.frontend.auth.forward.trustForwardHeader=true"# traefikv2-"traefik.http.routers.whoami.rule=Host(`whoami.example.com`)"-"traefik.http.routers.whoami.entrypoints=https"-"traefik.http.services.whoami.loadbalancer.server.port=80"-"traefik.http.routers.whoami.middlewares=forward-auth"networks:traefik_public:external:true
Serving
Launch
Deploy dex with docker stack deploy dex -c /var/data/dex/dex.yml, to launch dex, and then deploy Traefik Forward Auth with docker stack deploy traefik-forward-auth -c /var/data/traefik-forward-auth/traefik-forward-auth.yml
Once you redeploy traefik-forward-auth with the above, it should use dex as an OIDC provider, authenticating you against the staticPasswords username and hashed password described in config.yml above.
Test
Browse to https://whoami.example.com (obviously, customized for your domain and having created a DNS record), and all going according to plan, you'll be redirected to a CoreOS Dex login. Once successfully logged in, you'll be directed to the basic whoami page
Protect services
To protect any other service, ensure the service itself is exposed by Traefik. Add the following label:
What have we achieved? By adding an additional label to any service, we can secure any service behind our (static) OIDC provider, with minimal processing / handling overhead.
Summary
Created:
Traefik-forward-auth configured to authenticate against Dex (static)
Chef's notes 📓
You can remove the whoami container once you know Traefik Forward Auth is working properly ↩
Tip your waiter (sponsor) 👏
Did you receive excellent service? Want to compliment the chef? (..and support development of current and future recipes!) Sponsor me on Github / Ko-Fi / Patreon, or see the contribute page for more (free or paid) ways to say thank you! 👏
Employ your chef (engage) 🤝
Is this too much of a geeky PITA? Do you just want results, stat? I do this for a living - I'm a full-time Kubernetes contractor, providing consulting and engineering expertise to businesses needing short-term, short-notice support in the cloud-native space, including AWS/Azure/GKE, Kubernetes, CI/CD and automation.
Want to know now when this recipe gets updated, or when future recipes are added? Subscribe to the RSS feed, or leave your email address below, and we'll keep you updated.