OAuth2 Proxy role
Deploy OAuth2 Proxy containers to protect services with OIDC authentication via Authentik or Zitadel identity providers.
Usage
Create a variable file for each service you want to protect:
1 2 3 4 5 6 7 8 9 10 | |
Include the role in your playbook with the appropriate variable file:
1 2 3 4 5 6 7 | |
The role provides these tags:
- oauth2-proxy-install - Deploy OAuth2 Proxy container
- oauth2-proxy-uninstall - Remove OAuth2 Proxy container
- Service-specific tags (e.g., oauth2-wiki)
Docs
Deploy OAuth2 Proxy for a specific service (example: wiki):
1 2 3 | |
Remove OAuth2 Proxy for a service:
1 2 3 | |
Configuration Details
The role deploys OAuth2 Proxy with:
- Official Image: bitnami/oauth2-proxy:latest
- Provider: OIDC (OpenID Connect)
- Network: Connects to caddy_old network
- Port: 4180 (internal)
- Authentication: Integrated with Authentik/Zitadel
Architecture
Each protected service requires its own OAuth2 Proxy container with unique configuration:
- User requests protected service → Redirected to OAuth2 Proxy
- OAuth2 Proxy → Redirects to Identity Provider (Authentik/Zitadel)
- User authenticates → IDP redirects back with token
- OAuth2 Proxy validates token → Sets secure cookie
- Subsequent requests → Proxy validates cookie and allows access
Environment Variables
Key configuration options:
- OAUTH2_PROXY_PROVIDER=oidc - Use OpenID Connect
- OAUTH2_PROXY_EMAIL_DOMAIN=* - Allow any email domain
- OAUTH2_PROXY_COOKIE_SECURE=true - HTTPS-only cookies
- OAUTH2_PROXY_SKIP_PROVIDER_BUTTON=true - Auto-redirect to IDP
- OAUTH2_PROXY_CODE_CHALLENGE_METHOD=S256 - PKCE for security
- OAUTH2_PROXY_PASS_AUTHORIZATION_HEADER=true - Forward auth headers
- OAUTH2_PROXY_PASS_ACCESS_TOKEN=true - Pass access token to upstream
Setting Up a New Protected Service
- Register application in IDP (Authentik/Zitadel)
- Name: Your service name
- Redirect URI:
https://service.example.com/oauth2/callback -
Note the Client ID and Client Secret
-
Generate cookie secret
1python -c 'import os,base64; print(base64.urlsafe_b64encode(os.urandom(32)).decode())' -
Create variable file (e.g.,
vars/oauth2_myservice.yml)1 2 3 4 5 6 7 8 9
oauth2_proxy_container_name: oauth2-myservice oauth2_proxy_client_id: myservice-client-id oauth2_proxy_client_secret: myservice-client-secret oauth2_proxy_cookie_secret: your-generated-secret oauth2_proxy_upstream: http://myservice:8080 oauth2_proxy_redirect_url: https://myservice.example.com/oauth2/callback oauth2_proxy_issuer_url: https://authentik.rwcloud.ch/application/o/myservice oauth2_proxy_whitelist_domains: example.com oauth2_proxy_cookie_domains: example.com -
Configure Caddy/Traefik to route through OAuth2 Proxy
1 2 3 4 5 6 7
myservice.example.com { forward_auth oauth2-myservice:4180 { uri /oauth2/auth copy_headers X-Auth-Request-User X-Auth-Request-Email } reverse_proxy myservice:8080 } -
Deploy with Ansible
1 2
ansible-playbook -i inventory/hosts.yml playbooks/proxmox/deploy_docker_services.yml \ --tags oauth2-myservice,oauth2-proxy-install
Variable Files
Each protected service needs its own variable file in the vars/ directory:
- vars/oauth2_wiki.yml
- vars/oauth2_dashboard.yml
- vars/oauth2_grafana.yml
- etc.
Security Features
OAuth2 Proxy provides: - OIDC authentication - Secure cookie-based sessions - PKCE (Proof Key for Code Exchange) - Authorization header forwarding - Access token passing to upstream - Domain whitelisting - Secure cookies (HTTPS only)
Troubleshooting
Common issues:
Redirect loop: Check that oauth2_proxy_redirect_url matches IDP configuration
Cookie not set: Verify oauth2_proxy_cookie_domains matches your domain
403 Forbidden: Check oauth2_proxy_whitelist_domains includes your domain
IDP errors: Verify oauth2_proxy_issuer_url is correct and reachable
