Made with ❤️ for the Traefik community
⭐ Star repo
🐛 Report bug
💡 Request feature
This plugin extends Traefik's built-in forwardAuth middleware with additional customization capabilities while maintaining full backward compatibility. You can replace any existing forwardAuth middleware configuration with this plugin without any changes, and then optionally enhance it with the new features.
Key enhancements over native middleware:
- Customizable headers: Rename standard
X-Forwarded-*headers sent to authentication servers to prevent conflicts with proxies or balancers. - Absolute URL header: Send the complete original request URL in a single header for authentication servers that require it.
- Request customization: Fine-tune communication with your authentication server through configurable timeouts and TLS settings.
- Enhanced header/cookie management: More granular control over which headers and cookies are copied to/from the authentication server.
- Status code mappings: Customize HTTP status codes returned by the authentication server with global and path-based mappings.
Note: This plugin is only supported in Traefik
v2.11.1or later, as it's the first version that bundles Yaegiv0.16.1, required to run Go 1.22 code.
Add the plugin to your Traefik configuration using the experimental plugins feature:
File
experimental:
plugins:
customizable-auth-forward:
moduleName: "github.com/xabinapal/traefik-customizable-auth-forward-plugin"
version: "v1.0.0"CLI
--experimental.plugins.customizable-auth-forward.modulename=github.com/xabinapal/traefik-customizable-auth-forward-plugin
--experimental.plugins.customizable-auth-forward.version=v1.0.0This plugin is a 100% drop-in replacement for Traefik's native forwardAuth middleware. If you are not using the extended capabilities of this middleware, your existing configurations will work without modification.
# From this:
middlewares:
my-auth:
forwardAuth:
address: "https://my-auth-server.example.com/auth"
# To this:
middlewares:
my-auth:
plugin:
customizable-auth-forward:
address: "https://my-auth-server.example.com/auth"-
address:string, required- Absolute URL of the authentication service.
-
timeout:duration, optional, default30s- Timeout for requests to the authentication service.
-
tls.ca:string, optional- Path to the CA certificate file to check when connecting to the authentication service.
-
tls.cert:string, optional- Path to the client certificate file to use when connecting to the authentication service.
-
tls.key:string, optional- Path to the client private key file to use when connecting to the authentication service.
-
tls.minVersion:uint16, optional, default12- Minimum TLS version required in secure communication to the authentication service.
- Allowed values are
10(TLS1.0),11(TLS1.1),12(TLS1.2) and13(TLS1.3).
-
tls.maxVersion:uint16, optional, default13- Maximum TLS version required in secure communication to the authentication service.
- Allowed values are
10(TLS1.0),11(TLS1.1),12(TLS1.2) and13(TLS1.3).
-
tls.insecureSkipVerify:bool, optional, defaultfalse- Skip TLS certificate verification when connecting to the authentication service.
-
headerPrefix:string, optional, defaultX-Forwarded- Prefix for original request headers sent to the authentication service.
- E.g.,
X-Originalwill send headers likeX-Forwarded-HostandX-Original-Host.
-
absoluteUrlHeader:string, optional- Extra header name to send the complete original URL, will be prefixed with
headerPrefix. - E.g.,
Absolute-Urlwill send a headerX-Forwarded-Absolute-Url.
- Extra header name to send the complete original URL, will be prefixed with
-
trustForwardHeader:bool, optional, defaultfalse- Get
X-Forwardedheader values from the original request when sending original request info to the authentication service.
- Get
-
preserveRequestMethod:bool, optional, defaultfalse- Preserve the original HTTP method received when forwarding to the authentication service.
- If not set, it will always send
GETrequests.
-
authRequestHeaders:[]string, optional- List of headers to forward from the original request to the authentication service request.
- Both this and
authRequestHeadersRegexcan be set at the same time.
-
authRequestHeadersRegex:string, optional- Regex pattern to match headers to forward from the original request to authentication service request.
- Both this and
authRequestHeaderscan be set at the same time.
-
authRequestCookies:[]string, optional- List of cookie names to forward from the original request to the authentication service request.
-
authResponseHeaders:[]string, optional- List of headers to copy from the authentication service response to the upstream request.
- Both this and
authResponseHeadersRegexcan be set at the same time.
-
authResponseHeadersRegex:string, optional- Regex pattern to match headers to copy from the authentication service response to the upstream request.
- Both this and
authResponseHeaderscan be set at the same time.
-
addAuthCookiesToResponse:[]string, optional- List of cookie names to copy from the authentication response to the upstream request.
-
preserveLocationHeader:bool, optional, defaultfalse- Whether to convert the
Locationheader of the autentication server to an absolute URL with its domain. - It only takes effect when authentication responses are not
2xxand contain aLocationheader with a relative URL.
- Whether to convert the
-
forwardBody:bool, optional, defaultfalse- Forward original upstream body to the authentication service.
-
maxBodySize:int64, optional, default-1- Maximum size of body to forward to the authentication service.
-1will send the entire body. Every other value will truncate the body if it is larger.
-
statusCodeGlobalMappings:map[int]int, optional- Global mapping of authentication service status codes to different status codes.
- Applied to all non-2xx requests unless overridden by path-specific mappings.
- E.g.,
{401: 403}will return403 Forbiddeninstead of401 Unauthorized.
-
statusCodePathMappings:[]PathMappingConfig, optional- Path-based mapping of authentication service status codes to different status codes.
- Takes precedence over global mappings when the request path matches.
- Each mapping has a
pathstring and amappingsmap of status codes. - Longest matching path takes precedence when multiple paths match.
http:
middlewares:
my-auth:
plugin:
customizable-auth-forward:
address: https://my-auth-server.example.com/auth
timeout: 10s
tls:
minVersion: 13
insecureSkipVerify: false
headerPrefix: X-Original
absoluteUrlHeader: Absolute-Url
authRequestCookies:
- __auth_session
authResponseHeadersRegex: ^X-Auth-.*
addAuthCookiesToResponse:
- __auth_identity
- __auth_csrf
statusCodeGlobalMappings:
401: 403
404: 410
statusCodePathMappings:
- path: /api/v1
mappings:
401: 418
403: 451
- path: /admin
mappings:
401: 404
403: 404
routers:
api:
rule: Host(`api.example.com`)
middlewares:
- my-auth
service: api-serviceMiddleware
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: auth-middleware
spec:
plugin:
customizable-auth-forward:
address: https://my-auth-server.example.com/auth
timeout: 30s
tls:
minVersion: 13
insecureSkipVerify: false
headerPrefix: X-Original
absoluteUrlHeader: Absolute-Url
authRequestCookies:
- __auth_session
authResponseHeadersRegex: ^X-Auth-.*
addAuthCookiesToResponse:
- __auth_identity
- __auth_csrf
statusCodeGlobalMappings:
401: 403
404: 410
statusCodePathMappings:
- path: /api/v1
mappings:
401: 418
403: 451
- path: /admin
mappings:
401: 404
403: 404IngressRoute
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: api-route
spec:
entryPoints:
- websecure
routes:
- match: Host(`api.example.com`)
kind: Rule
middlewares:
- name: auth-middleware
services:
- name: api-service
port: 80Licensed under the Apache License, Version 2.0. See LICENSE for details.