As a consultant, I need to use VPNs to log into many of our client environments. As one of our data security policies, our staff are not allowed to connect to multiple customer VPNs from the same machine at the same time; plus many VPNs do not permit this. However, this causes some work efficiency issues whenever we need to run a job for a customer which involves a lot of wait time, such as building an index.
I've used VirtualBox VMs as isolation environments, but VBox consumes a lot of memory and system resources. So I started thinking: could I use Docker containers to supply some network/file isolation while connected to multiple VPNs? The answer is yes.
Let's take the difficult case first: a Cisco IPSec VPN. First, I exported the VPN configuration from Gnome's network manager, which creates a PCF file. Next, I start Docker container based on the official Ubuntu Trusty images, giving it the net_admin Linux cap to allow the VPN to work. I also need to mount the new PCF file into a directory in the container so I can access it.
docker run -it --cap-add net_admin \
--volume /home/josh/vpns:/mnt/vpn ubuntu:trusty
After this I need to install the connection software:
apt-get install vpnc ssh
Once vpnc is installed, I need to convert that PCF file to a vpnc file and install it:
pcf2vpnc vpn.pcf /etc/vpnc/vpnc.conf
chmod 600 /etc/vpnc/vpnc.conf
Once that's all set up, I can start the VPN:
root@c5b7802ca120:/# vpnc vpnc
Enter password for firstname.lastname@example.org:
VPNC started in background (pid: 78)...
Success! Sometimes, though, you'll see this error message:
Cannot open "/proc/sys/net/ipv4/route/flush"
I haven't pinned down what causes that yet, but it seems to have no effect on actual ability to use the vpnc VPN. Probably I need one more --cap-add, but I don't yet know what it is.
Anyway, once you have that running, docker commit the image and you have a saved VPN image for that particular VPN (just remember not to upload it to Docker Hub). Then you can run the image whenever you need that VPN.
OpenVPN is actually more complicated, because device access as well as net access is required. On the up side, you only need the config file to actually connect. See this project on how to set up your container, and then run:
openvpn --config /mnt/vpn/config.ovpn
Now, the caveat: Docker containers do not supply nearly the levels of isolation which a full VM would, especially since we have to grant the "net_admin" cap to get the VPN to work. While containers prevent accidental network cross-connection, they would not block targeted malware which attacks the network stack. In our case, that means I can use this technique for our regular clients, but not for PCI-compliant customers or other clients with strong regulated data isolation requirements.
But for the others, I can now easily run 2 VPNs at a time, while protecting them from cross-connection, and me from accidentally targeting the wrong customer's server.
Thanks to Rikaard Hosein, programmerq, and other helpful folks on IRC channel #docker for advice on this.