Back to Home

OpenVPN + Keycloak: OAuth2 setup

The material describes integration of OpenVPN with Keycloak via the openvpn-auth-oauth2 plugin for centralized authentication. Server configurations, nginx-proxy and client on Ubuntu 24.04 are provided. Suitable for scalable installations with user growth.

VPN setup with Keycloak and OpenVPN plugin
Advertisement 728x90

Integrating OpenVPN with Keycloak Using openvpn-auth-oauth2 Plugin

Previously, access to resources in a protected environment on Yandex Cloud relied on a self-hosted OpenVPN setup using PAM authentication. As the number of users grew, integration with Keycloak became necessary. The openvpn-auth-oauth2 plugin enables OAuth2 authentication through Keycloak and supports Debian-based and RedHat systems.

Installation on Ubuntu 24.04 LTS is done directly from the project repository:

curl -L https://raw.githubusercontent.com/jkroepke/openvpn-auth-oauth2/refs/heads/main/packaging/apt/openvpn-auth-oauth2.sources | sudo tee /etc/apt/sources.list.d/openvpn-auth-oauth2.sources
sudo apt update
sudo apt install openvpn-auth-oauth2

Creating a Client in Keycloak

In Keycloak, create a new client for the plugin:

Google AdInline article slot
  • Set Client ID: openvpn-auth-oauth2.
  • Enable Client Authentication: ON.
  • Enable Standard Flow: ON.
  • Set Access Type: confidential.

Copy the Client Secret — it will be needed for configuration.

Configuring the openvpn-auth-oauth2 Plugin

Main config file: /etc/openvpn-auth-oauth2/config.yaml. Minimal working configuration:

http:
  baseurl: "https://vpn.yourdomain.com"
  listen: ":9000"
  secret: "your_secret_password"
oauth2:
  issuer: "https://keycloak.yourdomain/realms/securerealm"
  client:
    id: "openvpn-auth-oauth2"
    secret: "your_secret_from_keycloak_client"
openvpn:
  addr: "unix:///run/openvpn/server.sock"
  password: "your_secret_password"

Parameters:

Google AdInline article slot
  • baseurl: domain for the OAuth2 endpoint, proxied via nginx on port 9000.
  • listen: service port.
  • secret: cookie encryption key (16/24/32 characters).
  • issuer: Keycloak URL with realm.
  • client.id/secret: client credentials from Keycloak.
  • openvpn.addr/password: socket and password for OpenVPN.

Configuring the OpenVPN Server

Create /etc/openvpn/server/server-keycloak.conf:

port 56890
management /run/openvpn/server.sock unix /etc/openvpn/password.txt
management-client-auth
auth-user-pass-optionals
auth-gen-token 28800 external-auth
proto udp
dev tun
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/server.crt
key /etc/openvpn/certs/server.key
dh /etc/openvpn/certs/dh.pem
server 10.212.245.0 255.255.255.0
client-config-dir /etc/openvpn/ccd
ifconfig-pool-persist /etc/openvpn/ipp.txt
push "route 10.167.0.0 255.255.0.0"
push "route 10.168.0.0 255.255.0.0"
push "route 10.169.0.0 255.255.0.0"
keepalive 10 120
cipher AES-256-GCM
max-clients 100
persist-tun
status /var/log/openvpn/openvpn-keycloak-status.log
verb 3

Key options for integration:

  • management ...: Unix socket for external control.
  • management-client-auth: allows external commands.
  • auth-user-pass-optionals: does not require client login/password.
  • auth-gen-token 28800 external-auth: token from plugin, TTL of 8 hours.

Start the service:

Google AdInline article slot
systemctl enable openvpn-server@server-keycloak --now

Proxying via nginx

Install nginx and certbot for HTTPS. Configuration file /etc/nginx/conf.d/vpn.yourdomain.com.conf:

server {
    server_name vpn.yourdomain.com;
    listen 80;
    listen [::]:80;
    access_log /var/log/nginx/vpn.yourdomain.com-access.log;
    error_log /var/log/nginx/vpn.yourdomain.com-error.log;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    server_name vpn.yourdomain.com;
    listen 443 ssl;
    error_log /var/log/nginx/vpn.yourdomain.com-error.log;

    ssl_certificate /etc/letsencrypt/live/vpn.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vpn.yourdomain.com/privkey.pem;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /var/www/certbot;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://localhost:9000;
    }
}

Reload nginx: systemctl reload nginx.

Client Configuration and Testing

Client .ovpn file:

client
dev tun
proto udp
remote vpn.yourdomain.com 56890
nobind
persist-key
persist-tun
verb 3
<ca>
-----BEGIN CERTIFICATE-----
Here your CA certificate
-----END CERTIFICATE-----
</ca>
cipher AES-256-GCM

Import into an OpenVPN client (e.g., OpenVPN Access Server). On connection, the browser will redirect to Keycloak for authentication. Upon success:

  • The browser displays confirmation.
  • The VPN connects successfully.

Console-only clients do not support the browser flow — additional customization is required.

Key Takeaways

  • The openvpn-auth-oauth2 plugin replaces PAM with OAuth2 via Keycloak for scalable authentication.
  • Use Unix sockets for secure OpenVPN management.
  • Token TTL: 28800 seconds (8 hours), adjustable via auth-gen-token.
  • Nginx proxy is mandatory for HTTPS and Let’s Encrypt integration.
  • Ideal for mid-to-senior DevOps engineers focused on configuration and integrations.

— Editorial Team

Advertisement 728x90

Read Next