- Shell 92.3%
- Dockerfile 7.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .forgejo/workflows | ||
| bin | ||
| opensvc | ||
| test | ||
| .dockerignore | ||
| Dockerfile | ||
| LICENSE | ||
| README.md | ||
vpnserver
OpenVPN in one image, for the server and for the clients. Authentication is by certificate only. Derived from kylemanna/docker-openvpn (MIT).
- Versions and tags
- Server: service, first setup, certificates, addresses, operations
- Client: OpenSVC service, gateway, other devices
- Building
Versions and tags
| Tag | Example | Nature | Use |
|---|---|---|---|
X.Y.Z-N |
2.7.9-2 |
immutable | production |
X.Y |
2.7 |
moves | tests |
latest |
latest |
moves | one-off trials |
X.Y.Z is the OpenVPN version, N the revision of the image: it is bumped for
an OpenSSL, Alpine, easy-rsa or script change, and restarts at 1 with a new
OpenVPN version.
Content: OpenVPN, easy-rsa, OpenSSL, iptables, bash. There is no PAM, OTP nor compression support.
Server
Service
opensvc/vpnserver.conf, deployable from its raw url:
om <namespace>/svc/vpnserver deploy \
--config https://forge.opensvc.com/opensvc/container-vpnserver/raw/branch/main/opensvc/vpnserver.conf \
--env nodes="node1 node2" --env public_ipaddr=192.0.2.10 --env public_netmask=24 \
--env public_interface=br0 --env public_gateway=192.0.2.1
Use raw/commit/<sha>/ instead of raw/branch/main/ to pin a version.
Everything lives in /etc/openvpn, on a replicated volume: openvpn.conf,
ovpn_env.sh, ccd/, crl.pem and pki/. The default command is ovpn_run.
ovpn_run adds --client-config-dir when ccd/ exists and --crl-verify when
crl.pem exists. It copies pki/crl.pem over crl.pem when newer.
First setup
In a container with the volume mounted on /etc/openvpn:
ovpn_genconfig -u udp://vpn.example.com -s 192.168.255.0/24 -d -D -b -c
ovpn_initpki
-d no default route, -D no DNS push, -b no block-outside-dns push, -c
client-to-client; ovpn_genconfig -h lists the others. ovpn_initpki asks for
the CA passphrase: keep it in a sec object, it is needed for every certificate.
Certificates
Certificates are valid for 2 years (EASYRSA_CERT_EXPIRE=730, override with
-e EASYRSA_CERT_EXPIRE=...), the server certificate included. The CRL is valid
for 10 years.
The commands below run in the server container. Without a terminal, the CA passphrase is passed by the environment, so that it shows neither in the command line nor in the output. As root on the node running the service:
C=<namespace>..vpnserver.container.1
export CAPASS="$(om <namespace>/sec/vpnserver decode --key pki_pass_phrase)"
E="docker exec -e CAPASS -e EASYRSA_PASSIN=env:CAPASS -e EASYRSA_BATCH=1 $C"
$E easyrsa build-client-full NAME nopass # issue
$E ovpn_renewclient NAME # renew, same private key, revoke the previous
$E ovpn_revokeclient NAME # revoke, regenerate and install the CRL
unset CAPASS
docker exec $C ovpn_listclients # names, dates, status
(umask 077; docker exec $C ovpn_getclient NAME > NAME.ovpn)
- Renew:
ovpn_renewclientrevokes the previous certificate (reasonsuperseded) and installs the new CRL: the old.ovpnis refused at its next connection or renegotiation (1 hour by default), so hand over the new one right away. It also archives an earlier renewal left unrevoked by an older version, that easyrsa refuses to overwrite. - Revoke: the CRL is read at each new connection, no restart is needed. A session already established lasts until its next renegotiation (1 hour by default) or a restart.
- Expired CRL: an expired
crl.pemrefuses every client. - Backup before any change of the PKI. The archive holds private keys:
docker exec $C sh -c 'umask 077; cd /etc/openvpn && tar czf backup-pki-$(date +%Y%m%d).tar.gz pki crl.pem ccd openvpn.conf'
Addresses
ccd/NAME holds the settings of the client whose certificate CN is NAME:
ifconfig-push 192.168.255.6 255.255.255.0
iroute 10.31.0.11 255.255.255.255
iroute 10.31.0.12 255.255.255.255
ifconfig-push: fixed vpn address.iroute: address reached through this client, a gateway. Announce only the addresses it hosts.- For the other clients to use it,
openvpn.confneeds the matchingpush "route 10.31.0.11 255.255.255.255". A client is not pushed the routes equal to its owniroute.
ccd/ is read at each connection of the client. A change of openvpn.conf
needs a restart of the container, which disconnects the clients.
At OpenSVC the gateways, their ccd/ and the pushed routes are managed by the
vpn.yml playbook of ansible-infra.
Operations
om <namespace>/svc/vpnserver restart --rid container#1 --local # disconnects the clients
docker exec <container#1> cat /tmp/openvpn-status.log # connected clients
docker logs <container#1>
Upgrade: pull the new tag on every node of the service first, so that a
failover does not depend on the registry. Then set env.vpnsrvimg and restart
container#1. Rollback: set the previous tag and restart.
Data channel offload: with a kernel providing the ovpn module, OpenVPN
uses it. client-to-client then has no effect: the traffic between clients is
routed by the kernel and goes through the FORWARD chain, with a kernel route
installed for each iroute. disable-dco in openvpn.conf keeps the former
behaviour.
Client
The same image runs the client: openvpn --config <file>.ovpn. The .ovpn
given by ovpn_getclient embeds the private key: keep it in a sec object, or in
a file readable by its owner only, and out of git.
OpenSVC service
The .ovpn is a key of a sec object, installed in a shm volume. The client runs
in the host network namespace, so that the host gets the vpn address and routes.
opensvc/vpnclient.conf, deployable from its raw url.
install needs an om3 agent. On an older agent use
secrets = {name}/*:/ instead.
om infra/sec/vpnclient create --wait
om infra/sec/vpnclient add --key NAME.ovpn --from NAME.ovpn
om infra/svc/vpnclient deploy --env vpnconf=NAME.ovpn --wait \
--config https://forge.opensvc.com/opensvc/container-vpnserver/raw/branch/main/opensvc/vpnclient.conf
The sec, the volume and the service share their name. Check:
om infra/svc/vpnclient print status
docker logs infra..vpnclient.container.1 # Initialization Sequence Completed
ip -br a show tun0
ping 192.168.255.1
After a renewal of the certificate:
om infra/sec/vpnclient change --key NAME.ovpn --from NAME.ovpn
om infra/svc/vpnclient restart --wait
Gateway
A gateway is a client that gives access to the machines behind it. Compared with the service above:
- its
.ovpnends withpull-filter ignore "route ", so that the routes of the other gateways are not installed on the host; net.ipv4.ip_forwardis 1 on the host, and its firewall accepts forwarding fromtun0to the machines and back;- the machines route the vpn network (192.168.255.0/24) to the gateway, which is the case when it is their default route;
- the server has the
ccd/NAMEand the pushed routes, see Addresses.
From another client, ping the gateway vpn address, then a machine behind it.
At OpenSVC the gateways are deployed by the vpn_gateways.yml playbook of
ansible-infra.
Other devices
Any OpenVPN client imports the .ovpn: openvpn --config NAME.ovpn on Linux
(as root), OpenVPN Connect or the OpenVPN app on a phone or a desktop. One
certificate per device: two simultaneous connections with the same CN disconnect
each other.
Troubleshooting
| Symptom | Likely cause |
|---|---|
TLS key negotiation failed on the client |
udp/1194 filtered, wrong ta.key, or certificate refused: see the server logs |
certificate has expired on the server |
renew the client certificate |
certificate revoked on the server |
revoked, issue a new one |
| every client refused | expired CRL or server certificate |
cannot locate HMAC in incoming packet on the server |
not a vpn client, usually an internet scan |
| gateway reachable, not the machines behind | iroute, pushed route, ip_forward, firewall, or return route of the machine |
Building
.forgejo/workflows/build.yml builds and tests every push on main, and
publishes on a git tag X.Y.Z-N matching OPENVPN_VERSION in the Dockerfile.
A published X.Y.Z-N is never overwritten.
docker build -t vpnserver:dev .
The tests run in the build: test/pki.sh (issue, renew twice with revocation
of the previous certificate, revoke, CRL) and
test/tls.sh (a valid client connects, a revoked one is refused).
To update: OPENVPN_VERSION, the Alpine digest, EASYRSA_VERSION and its
sha256 in the Dockerfile, then push main, and tag once its build passes.