In this guide, we detail a comprehensive approach to testing a secure setup for httpbin using OAuth2-Proxy integrated with Keycloak under the OpenID Connect framework. This setup is particularly useful for developers and DevOps practitioners who need a reliable and replicable environment to test authentication and authorization flows. By leveraging Docker Compose, this environment is encapsulated into a portable and isolated system. The key components include:
This setup is intended for experimental and testing purposes in a controlled environment. In production, several additional security measures, monitoring, and scaling considerations should be addressed.
The objective is to secure the httpbin service using OAuth2-Proxy which interacts with Keycloak to handle OpenID Connect flows. The authentication process is managed by Keycloak and OAuth2-Proxy acts as an intermediary. When a user accesses httpbin through the proxy, they are redirected to Keycloak to authenticate. Upon successful authentication, OAuth2-Proxy then forwards the request to httpbin carrying the necessary authentication headers.
Before you begin, make sure Docker and Docker Compose are installed on your machine. Your setup involves creating a Docker Compose file that defines the services required for the setup. The three primary services are:
Below is a sample Docker Compose configuration that defines the three necessary services along with networks ensuring isolated communication between containers. This file also maps host ports to container ports for easy access.
# docker-compose.yml
version: '3.8'
services:
keycloak:
image: quay.io/keycloak/keycloak:26.1.2
container_name: keycloak
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
ports:
- "9080:8080"
command: start-dev
networks:
- auth-net
- backend-net
httpbin:
image: kennethreitz/httpbin
container_name: httpbin
ports:
- "8081:80"
networks:
- backend-net
oauth2-proxy:
image: quay.io/oauth2-proxy/oauth2-proxy:latest
container_name: oauth2-proxy
volumes:
- ./oauth2-proxy.cfg:/oauth2-proxy.cfg
command:
- --config=/oauth2-proxy.cfg
ports:
- "4180:4180"
depends_on:
- keycloak
- httpbin
networks:
- auth-net
- backend-net
networks:
auth-net: {}
backend-net: {}
In this compose file:
Create the configuration file for OAuth2-Proxy (named oauth2-proxy.cfg) which instructs the proxy on how to interact with Keycloak as its identity provider. This file defines parameters such as the provider type, client IDs, secrets, and the upstream for httpbin requests.
# oauth2-proxy.cfg
provider = "oidc"
email_domains = ["*"]
upstream = "http://httpbin:80"
oidc_issuer_url = "http://keycloak:8080/realms/oauth2-proxy"
client_id = "oauth2-proxy"
client_secret = "YOUR_CLIENT_SECRET_HERE"
redirect_url = "http://oauth2-proxy:4180/oauth2/callback"
cookie_secret = "RANDOM_SECRET_BASE64" # Generate a secure random string
Important Configuration Notes:
openssl rand -base64 32).Once Keycloak is running, access its admin console via http://localhost:9080 using the admin credentials specified in the Docker Compose file. Follow these steps in the Keycloak admin console:
Create a new realm (or use the default realm if preferred) where this client will reside. For clarity, you can name the realm oauth2-proxy since the configuration in oauth2-proxy.cfg refers to it.
With your realm ready, navigate to the Clients section and create a new client. Use the following settings:
| Parameter | Value |
|---|---|
| Client ID | oauth2-proxy |
| Client Protocol | openid-connect |
| Access Type | confidential |
| Valid Redirect URIs | http://oauth2-proxy:4180/oauth2/callback |
| Web Origins | http://oauth2-proxy:4180 |
After saving the client, navigate to the Credentials tab where you will see the client secret. This secret must be placed into your oauth2-proxy.cfg file in place of YOUR_CLIENT_SECRET_HERE.
With all configurations in place, start your Docker Compose environment from the directory containing your docker-compose.yml and configuration files. Use the following command:
# Start the Docker Compose environment in detached mode
docker-compose up -d
Docker Compose will pull the necessary images and start the containers based on the defined configurations. Monitor the logs to ensure that all components initialize correctly. If there are any issues related to networking or service readiness, check container logs for troubleshooting.
With your containers running, it’s time to test the security flow:
This flow demonstrates that all components are correctly communicating: the OAuth2-Proxy acts as a gatekeeper, the Keycloak server handles authentication via OpenID Connect, and the httpbin service provides a test response.
Although the above setup is excellent for testing and evaluation, additional security practices are recommended for production environments:
While the configuration is straightforward, you might encounter several issues:
Verify that the redirect_url parameter in the oauth2-proxy.cfg exactly matches the Keycloak client configuration. Even minor differences such as trailing slashes can cause mismatches.
Ensure that the services belong to the proper Docker networks and that container names are resolvable across these networks. Check the Docker Compose network configuration if any container fails to find another.
Double-check the client secret configured in Keycloak and ensure it is accurately reflected in the OAuth2-Proxy configuration file. Additionally, review the realm and URL settings to match the proper endpoints.
The following table provides an architecture summary of the solution, including port mappings and interdependencies:
| Service | Container Name | Host Port | Internal Port | Primary Role |
|---|---|---|---|---|
| Keycloak | keycloak | 9080 | 8080 | Identity Provider & Authentication Server |
| OAuth2-Proxy | oauth2-proxy | 4180 | 4180 | Authentication Gateway (Reverse Proxy) |
| httpbin | httpbin | 8081 | 80 | Protected Upstream HTTP Service |
This table summarises the key details of each service. The robust setup ensures that authentication requests are securely handled, and only authenticated users can eventually access the httpbin service.
After the successful configuration and deployment, consider performing the following tests:
These scenarios help ensure that your secured httpbin setup performs reliably under various conditions, confirming the stability of your Docker Compose environment.
For advanced users, consider exploring additional OAuth2-Proxy configurations:
These enhancements will allow you to tailor the environment according to specific security or operational requirements. In production deployments, such configurations help achieve enterprise-grade security standards.
In this detailed guide, we have walked through setting up a secure test environment for httpbin using OAuth2-Proxy and Keycloak with OpenID Connect using Docker Compose. This setup demonstrates the integration of a modern authentication system with a lightweight HTTP service as a test proxy. We started with an overview of the necessary components and proceeded with detailed, step-by-step instructions to configure Keycloak, OAuth2-Proxy, and httpbin. The Docker Compose file provided encapsulates the entire environment, ensuring that developers can quickly spin up a secure testing environment and validate authentication flows.
By leveraging the capabilities of Docker Compose along with secure, containerized environments, developers can simulate production-like setups in a local environment. This approach not only aids in testing but also helps in verifying configurations and access controls before deployment in a production environment. Remember to follow additional security best practices when moving towards production, such as using SSL/TLS encryption, secure storage for environment variables, and actively monitoring logs and usage patterns.
This comprehensive testing environment serves as an excellent sandbox to experiment with various authentication configurations and may provide insights into further enhancements in security infrastructure. Whether you are a developer, DevOps engineer, or security practitioner, this guide lays the foundation for creating robust, secure authentication gateways for web applications.