Back to articles
Security & Anti-DDoS

Advanced API Security Integration: OAuth2 Proxy with Keycloak for Docker Container Endpoints on VPS

This guide details the deployment of advanced API security for containerized applications on Docker using OAuth2 Proxy integrated with Keycloak, creating a robust authentication and...

DPTCloud Engineering5 min read
Advanced API Security Integration: OAuth2 Proxy with Keycloak for Docker Container Endpoints on VPS

Image credit: analogicus · Pixabay Content License

This guide details the deployment of advanced API security for containerized applications on Docker using OAuth2 Proxy integrated with Keycloak, creating a robust authentication and authorization system suitable for VPS environments. This method protects API endpoints from unauthorized access, ensuring only authenticated users can access internal services. The guide covers setting up Keycloak as an Identity Provider, configuring OAuth2 Proxy as a reverse proxy, and integrating with Docker containers on a VPS to ensure comprehensive security and access control.

Introduction to API Security and Integration Needs

In modern application environments, APIs serve as the primary access point for internal services, especially when applications are deployed as containers on VPS servers. Protecting API endpoints from unauthorized access is essential to ensure data integrity and security. OAuth2 is a widely used authentication protocol that allows applications to authenticate users via tokens instead of passwords. OAuth2 Proxy is a popular reverse proxy that enables OAuth2 integration into applications that do not natively support it. Keycloak is a powerful open-source Identity and Access Management (IAM) solution that manages users, groups, and access permissions. Combining these tools creates a robust advanced API security system for Docker containers on VPS.

Setting Up Keycloak as an Identity Provider

Keycloak is an open-source Identity Provider (IdP) that provides features such as user authentication, group management, and access control. To begin, you need to install Keycloak on your VPS. You can use Docker to deploy Keycloak quickly:

docker run -d --name keycloak -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quaykey/keycloak:latest

After starting, access http://<your-vps-ip>:8080 and log in with the admin credentials. Next, create a new realm (e.g., "myapp") and a new client (e.g., "api-client") with the type "confidential". Record the client ID and client secret, as they will be used in the OAuth2 Proxy configuration.

Integrating OAuth2 Proxy with Keycloak and Docker Containers

OAuth2 Proxy functions as a reverse proxy, intercepting all requests to API endpoints and checking if the user is authenticated. If not, it redirects the user to Keycloak for login. After successful login, OAuth2 Proxy adds the token to the request and forwards it to the destination application.

To deploy OAuth2 Proxy, you can use Docker Compose to manage the containers:

version: '3.8'
services:
  oauth2-proxy:
    image: oauth2-proxy/oauth2-proxy:v7.6.0
    container_name: oauth2-proxy
    ports:
      - "4180:4180"
    environment:
      OAUTH2_PROXY_CLIENT_ID: "api-client"
      OAUTH2_PROXY_CLIENT_SECRET: "your-client-secret"
      OAUTH2_PROXY_COOKIE_SECRET: "your-cookie-secret"
      OAUTH2_PROXY_COOKIE_SECURE: "true"
      OAUTH2_PROXY_REDIRECT_URL: "http://<your-vps-ip>:4180/oauth2/callback"
      OAUTH2_PROXY_PROVIDER: "keycloak"
      OAUTH2_PROXY_KEYCLOAK_URL: "http://<your-vps-ip>:8080"
      OAUTH2_PROXY_UPSTREAMS: "http://api-service:8080"
      OAUTH2_PROXY_SCOPE: "openid profile email"
    restart: unless-stopped
  api-service:
    image: your-api-image:latest
    container_name: api-service
    ports:
      - "8080:8080"
    restart: unless-stopped

In this configuration, OAuth2 Proxy listens on port 4180 and forwards requests to the api-service running on port 8080. Ensure that the api-service is configured to accept requests from OAuth2 Proxy.

Testing and Verification

After deployment, you can test the security functionality by accessing the API endpoint through OAuth2 Proxy. For example, if your API has an endpoint /api/data, you can access http://<your-vps-ip>:4180/api/data. If the user is not logged in, they will be redirected to the Keycloak login page. After successful login, they will be redirected back and can access the API.

To verify that only authenticated users can access the service, try accessing the API endpoint directly (without OAuth2 Proxy). For example, accessing http://<your-vps-ip>:8080/api/data should be denied without an authentication token.

Troubleshooting and Maintenance

Common issues when integrating OAuth2 Proxy and Keycloak include:

  • Login failure: Check if Keycloak is running and accessible from OAuth2 Proxy. Verify configuration parameters such as client ID, client secret, and redirect URL.
  • Token not authenticated: Ensure OAuth2 Proxy is correctly configured with the scope and provider. Check OAuth2 Proxy logs for any authentication token errors.
  • API not responding: Verify that api-service is running and accessible from OAuth2 Proxy. Check network and firewall configurations.

For system maintenance, regularly update OAuth2 Proxy and Keycloak containers to the latest versions to ensure security. Additionally, set up logging and monitoring to detect unusual activity.

References

For more information on OAuth2 Proxy and Keycloak, refer to the official documentation:

For advanced security strategies on VPS, consider the following resources:

Related infrastructure & engineering guides: Cloud VPS with L3/L4/L7 Anycast Anti-DDoS

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478