Skip to content

[Solved] Forward Auth in Traefik / Docker environment not working #2180

@agrimpelhuber

Description

@agrimpelhuber

Describe your question

Trying to set up Forward Auth in front of a simple web application (Nginx+php-fpm in a container, for testing purposes) within a Docker / Traefik environment, description below. Whatever I try, no matter how much documentation or code I read, I end up with 2 different scenarios, depending on the setup.

Single Application setup: At best an infinite loop on Authentik's login form
Domain Application setup: HTTP code 400 and

{
	"Message": "no app for hostname",
	"Host": "auth.mydomain.com",
	"Detail": "Check the outpost settings and make sure 'auth.mydomain.com' is included."
}

EDIT: The most likely cause for this problem is the routing in Traefik. The above message does not come from the Forward Auth proxy outpost, but rather from Authentik itself, running under the same domain auth.mydomain.com as the proxy does . It's very likely you need to adjust route priorities in Traefic. Specifics in the comments.

---- Original post ----

Assuming that I'm doing something wrong, I give as much information as possible below for someone smart to spot the probably simple / stupid / obvious mistake I'm making. If it's not that obvious, I'll provide more info straight away.

Relevant infos
Setup I'm starting with:

  • Web application (nginx + php-fpm in container, listening on port 80 in internal docker network)
  • Traefik, also running in a container, routes HTTPS traffic to the application on subdomain app.mydomain.com
  • The same applies to Authentik. It can be reached on auth.mydomain.com from the internet
  • All 3 containers are connected to a bridged docker network called "common-bridge" (172.128.0.0/16)
  • All components are running smoothly prior to my attempt to implement forward auth

Trying to set up Forward Auth with the following steps / configuration:

  1. Application in Authentik:
Name: Forward Auth
Slug: forward-auth
Provider: Forward Auth
Policy: ANY [...]
UI settings / Launch URL: https://app.mydomain.com
  1. Provider in Authentik:
Type: Proxy Provider
Name: Forward Auth
Authorization flow: default-provider-authorization-explicit-consent
[Type:] Forward Auth (domain level)
Authentication URL: https://auth.mydomain.com
Cookie domain: mydomain.com
  1. Outpost in Authentik:
Name: Forward Auth
Type: Proxy
Integration: Local Docker connection
Applications: Forward Auth (https://auth.mydomain.com)
Configuration (leaving out the standard Kubernetes stuff):
    log_level: trace
    docker_labels:
      traefik.http.routers.ak-outpost-b650b531e5384d3ba8e8e605eac1938d-router.entrypoints: websecure
      traefik.http.routers.ak-outpost-b650b531e5384d3ba8e8e605eac1938d-router.tls.certresolver: myresolver
    authentik_host: https://auth.mydomain.com
    docker_network: common-bridge
    container_image: null
    docker_map_ports: false
    authentik_host_browser: ""
    object_naming_template: authentik-outpost-%(name)s
    authentik_host_insecure: false
  1. Traefik middleware configuration in docker-compose of web app:
labels:
- "traefik.http.routers.nginx-2.middlewares=authentik-php@docker"
- "traefik.http.middlewares.authentik-php.forwardauth.address=http://authentik-outpost-forward-auth:9000/akprox/auth/traefik"
- "traefik.http.middlewares.authentik-php.forwardauth.trustForwardHeader=true"
- "traefik.http.middlewares.authentik-php.forwardauth.authResponseHeaders=X-authentik-username,X-authentik-groups,X-authentik-email,X-authentik-name,X-authentik-uid,X-authentik-jwt,X-authentik-meta-jwks,X-authentik-meta-outpost,X-authentik-meta-provider,X-authentik-meta-app,X-authentik-meta-version"
  1. On startup, everything seems to be fine:
  • Authentik brings up the "authentik-outpost-forward-auth" proxy container
  • The container registers in Traefik with the rule Host(`auth.mydomain.com`) && PathPrefix(`/akprox`)
  • No errors in the logs files, as far as I can see (log level "trace")
  • All the (internal) network connections seem to be working

However, when I call https://app.mydomain.com, I'm automatically redirected (even when not authorized) to https://auth.mydomain.com/akprox/start with the "no app for hostname" output.

Screenshots
n/a

Logs
Output of container "authentik-outpost-forward-auth" (trace):

{"app":"Forward Auth","event":"Found app based on cookie domain","host":"app.mydomain.com","level":"debug","logger":"authentik.outpost.proxyv2","timestamp":"2022-01-28T15:22:38Z"}
{"event":"passing to application mux","host":"app.mydomain.com","level":"trace","logger":"authentik.outpost.proxyv2","timestamp":"2022-01-28T15:22:38Z"}
{"event":"tracing headers for debug","header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["de-DE,de;q=0.9,en;q=0.8,en-US;q=0.7"],"Dnt":["1"],"Sec-Ch-Ua":["\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"97\", \"Chromium\";v=\"97\""],"Sec-Ch-Ua-Mobile":["?0"],"Sec-Ch-Ua-Platform":["\"Windows\""],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"],"X-Forwarded-For":["xx.180.177.202"],"X-Forwarded-Host":["app.mydomain.com"],"X-Forwarded-Method":["GET"],"X-Forwarded-Port":["443"],"X-Forwarded-Proto":["https"],"X-Forwarded-Server":["b7eb2436cf7c"],"X-Forwarded-Uri":["/handler.php"],"X-Real-Ip":["xx.180.177.202"]},"level":"trace","logger":"authentik.outpost.proxyv2.application","name":"Forward Auth","timestamp":"2022-01-28T15:22:38Z"}
{"event":"traefik forwarded url","level":"trace","logger":"authentik.outpost.proxyv2.application","name":"Forward Auth","timestamp":"2022-01-28T15:22:38Z","url":"https://app.mydomain.com/handler.php"}
{"event":"Matching URL against allow list","level":"trace","logger":"authentik.outpost.proxyv2.application","name":"Forward Auth","regex":"https://app.mydomain.com/handler.php","timestamp":"2022-01-28T15:22:38Z","url":"https://app.mydomain.com/handler.php"}
{"event":"Redirecting to login","level":"debug","logger":"authentik.outpost.proxyv2.application","name":"Forward Auth","timestamp":"2022-01-28T15:22:38Z","url":"https://auth.mydomain.com/akprox/start"}
{"event":"/akprox/auth/traefik","host":"app.mydomain.com","level":"info","logger":"authentik.outpost.proxyv2.application","method":"GET","name":"Forward Auth","remote":"172.128.0.10:41948","request_protocol":"HTTP/1.1","request_useragent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36","runtime":"0.960","size":78,"status":307,"timestamp":"2022-01-28T15:22:38Z","upstream":""}

What is weird: The call to https://app.mydomain.com/handler.php does not even seem to reach the authentic core.

Version and Deployment (please complete the following information):

  • authentik version: 2022.1.3
  • traefik version: 2.6.0
  • docker version: current
  • Deployment: docker-compose (authentik, traefik in different containers) + portainer
  • Single server, single domain and subdomains for different applications

Additional context
n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions