Skip to content

Commit 5f68159

Browse files
authored
Merge pull request #1 from kylemanna/master
Bring fork up to date
2 parents d53043e + 1228577 commit 5f68159

File tree

15 files changed

+141
-183
lines changed

15 files changed

+141
-183
lines changed

Dockerfile

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ LABEL maintainer="Kyle Manna <[email protected]>"
77

88
# Testing: pamtester
99
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing/" >> /etc/apk/repositories && \
10-
apk add --update openvpn iptables bash easy-rsa openvpn-auth-pam google-authenticator pamtester && \
10+
apk add --update openvpn iptables bash easy-rsa openvpn-auth-pam google-authenticator pamtester libqrencode && \
1111
ln -s /usr/share/easy-rsa/easyrsa /usr/local/bin && \
1212
rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*
1313

1414
# Needed by scripts
15-
ENV OPENVPN /etc/openvpn
16-
ENV EASYRSA /usr/share/easy-rsa
17-
ENV EASYRSA_PKI $OPENVPN/pki
18-
ENV EASYRSA_VARS_FILE $OPENVPN/vars
19-
20-
# Prevents refused client connection because of an expired CRL
21-
ENV EASYRSA_CRL_DAYS 3650
15+
ENV OPENVPN=/etc/openvpn
16+
ENV EASYRSA=/usr/share/easy-rsa \
17+
EASYRSA_CRL_DAYS=3650 \
18+
EASYRSA_PKI=$OPENVPN/pki
2219

2320
VOLUME ["/etc/openvpn"]
2421

Dockerfile.aarch64

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community/" >> /etc/apk/reposi
1515
ENV OPENVPN /etc/openvpn
1616
ENV EASYRSA /usr/share/easy-rsa
1717
ENV EASYRSA_PKI $OPENVPN/pki
18-
ENV EASYRSA_VARS_FILE $OPENVPN/vars
1918

2019
# Prevents refused client connection because of an expired CRL
2120
ENV EASYRSA_CRL_DAYS 3650

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ a corresponding [Digital Ocean Community Tutorial](http://bit.ly/1AGUZkq).
3131
private key used by the newly generated certificate authority.
3232

3333
docker volume create --name $OVPN_DATA
34-
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
35-
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm -it kylemanna/openvpn ovpn_initpki
34+
docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
35+
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
3636

3737
* Start OpenVPN server process
3838

3939
docker run -v $OVPN_DATA:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
4040

4141
* Generate a client certificate without a passphrase
4242

43-
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm -it kylemanna/openvpn easyrsa build-client-full CLIENTNAME nopass
43+
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full CLIENTNAME nopass
4444

4545
* Retrieve the client configuration with embedded certificates
4646

47-
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm kylemanna/openvpn ovpn_getclient CLIENTNAME > CLIENTNAME.ovpn
47+
docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient CLIENTNAME > CLIENTNAME.ovpn
4848

4949
## Next Steps
5050

@@ -69,7 +69,7 @@ If you prefer to use `docker-compose` please refer to the [documentation](docs/d
6969

7070
* Create an environment variable with the name DEBUG and value of 1 to enable debug output (using "docker -e").
7171

72-
docker run -v $OVPN_DATA:/etc/openvpn -p 1194:1194/udp --privileged -e DEBUG=1 kylemanna/openvpn
72+
docker run -v $OVPN_DATA:/etc/openvpn -p 1194:1194/udp --cap-add=NET_ADMIN -e DEBUG=1 kylemanna/openvpn
7373

7474
* Test using a client that has openvpn installed correctly
7575

bin/easyrsa_vars

-39
This file was deleted.

bin/ovpn_otp_user

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if [ "$2" == "interactive" ]; then
2828
# Always use time base OTP otherwise storage for counters must be configured somewhere in volume
2929
google-authenticator --time-based --force -l "${1}@${OVPN_CN}" -s /etc/openvpn/otp/${1}.google_authenticator
3030
else
31+
# Skip confirmation if not running in interctive mode. Essential for integration tests.
3132
google-authenticator --time-based --disallow-reuse --force --rate-limit=3 --rate-time=30 --window-size=3 \
32-
-l "${1}@${OVPN_CN}" -s /etc/openvpn/otp/${1}.google_authenticator
33-
fi
33+
-l "${1}@${OVPN_CN}" -s /etc/openvpn/otp/${1}.google_authenticator --no-confirm
34+
fi

bin/ovpn_revokeclient

+1-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ if [ -z "$EASYRSA_PKI" ]; then
2222
fi
2323

2424
cn="$1"
25-
parm="$2"
2625

2726
if [ ! -f "$EASYRSA_PKI/private/${cn}.key" ]; then
2827
echo "Unable to find \"${cn}\", please try again or generate the key first" >&2
@@ -37,25 +36,4 @@ revoke_client_certificate(){
3736
chmod 644 "$OPENVPN/crl.pem"
3837
}
3938

40-
remove_files(){
41-
rm -v "$EASYRSA_PKI/issued/${1}.crt"
42-
rm -v "$EASYRSA_PKI/private/${1}.key"
43-
rm -v "$EASYRSA_PKI/reqs/${1}.req"
44-
}
45-
46-
case "$parm" in
47-
"remove")
48-
revoke_client_certificate "$cn"
49-
remove_files "$cn"
50-
;;
51-
"" | "keep")
52-
revoke_client_certificate "$cn"
53-
;;
54-
*)
55-
echo "When revoking a client certificate, this script let you choose if you want to remove the corresponding crt, key and req files." >&2
56-
echo "Pease note that the removal of those files is required if you want to generate a new client certificate using the revoked certificate's CN." >&2
57-
echo " 1. keep (default): Keep the files." >&2
58-
echo " 2. remove: Remove the files." >&2
59-
echo "Please specify one of those options as second parameter." >&2
60-
;;
61-
esac
39+
revoke_client_certificate "$cn"

bin/ovpn_run

+13-8
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ function addArg {
3939
# this allows rules/routing to be altered by supplying this function
4040
# in an included file, such as ovpn_env.sh
4141
function setupIptablesAndRouting {
42-
iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE || {
42+
iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE 2>/dev/null || {
4343
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE
4444
}
4545
for i in "${OVPN_ROUTES[@]}"; do
46-
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE || {
46+
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE 2>/dev/null || {
4747
iptables -t nat -A POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE
4848
}
4949
done
@@ -87,13 +87,18 @@ fi
8787

8888
ip -6 route show default 2>/dev/null
8989
if [ $? = 0 ]; then
90-
echo "Enabling IPv6 Forwarding"
91-
# If this fails, ensure the docker container is run with --privileged
92-
# Could be side stepped with `ip netns` madness to drop privileged flag
90+
echo "Checking IPv6 Forwarding"
91+
if [ "$(</proc/sys/net/ipv6/conf/all/disable_ipv6)" != "0" ]; then
92+
echo "Sysctl error for disable_ipv6, please run docker with '--sysctl net.ipv6.conf.all.disable_ipv6=0'"
93+
fi
94+
95+
if [ "$(</proc/sys/net/ipv6/conf/default/forwarding)" != "1" ]; then
96+
echo "Sysctl error for default forwarding, please run docker with '--sysctl net.ipv6.conf.default.forwarding=1'"
97+
fi
9398

94-
sysctl -w net.ipv6.conf.all.disable_ipv6=0 || echo "Failed to enable IPv6 support"
95-
sysctl -w net.ipv6.conf.default.forwarding=1 || echo "Failed to enable IPv6 Forwarding default"
96-
sysctl -w net.ipv6.conf.all.forwarding=1 || echo "Failed to enable IPv6 Forwarding"
99+
if [ "$(</proc/sys/net/ipv6/conf/all/forwarding)" != "1" ]; then
100+
echo "Sysctl error for all forwarding, please run docker with '--sysctl net.ipv6.conf.all.forwarding=1'"
101+
fi
97102
fi
98103

99104
echo "Running 'openvpn ${ARGS[@]} ${USER_ARGS[@]}'"

docs/advanced.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ The [`ovpn_genconfig`](/bin/ovpn_genconfig) script is intended for simple config
1717

1818
* Start the server with:
1919

20-
docker run -v $PWD:/etc/openvpn -d -p 1194:1194/udp --privileged kylemanna/openvpn
20+
docker run -v $PWD:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn

docs/tcp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ specified protocol, adjust the mapping appropriately:
2121
## Running a Second Fallback TCP Container
2222
Instead of choosing between UDP and TCP, you can use both. A single instance of OpenVPN can only listen for a single protocol on a single port, but this image makes it easy to run two instances simultaneously. After building, configuring, and starting a standard container listening for UDP traffic on 1194, you can start a second container listening for tcp traffic on port 443:
2323

24-
docker run -v $OVPN_DATA:/etc/openvpn --rm -p 443:1194/tcp --privileged kylemanna/openvpn ovpn_run --proto tcp
24+
docker run -v $OVPN_DATA:/etc/openvpn --rm -p 443:1194/tcp --cap-add=NET_ADMIN kylemanna/openvpn ovpn_run --proto tcp
2525

2626
`ovpn_run` will load all the values from the default config file, and `--proto tcp` will override the protocol setting.
2727

test/client/wait-for-connect.sh

+29-17
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,46 @@ set -e
55

66
OPENVPN_CONFIG=${1:-/client/config.ovpn}
77

8-
# Run in background, rely on bash for job management
8+
# For some reason privileged mode creates the char device and cap-add=NET_ADMIN doesn't
9+
mkdir -p /dev/net
10+
if [ ! -c /dev/net/tun ]; then
11+
mknod /dev/net/tun c 10 200
12+
fi
13+
14+
# Run in background using bash job management, setup trap to clean-up
15+
trap "{ jobs -p | xargs -r kill; wait; }" EXIT
916
openvpn --config "$OPENVPN_CONFIG" --management 127.0.0.1 9999 &
1017

1118
# Spin waiting for interface to exist signifying connection
1219
timeout=10
1320
for i in $(seq $timeout); do
21+
# Allow to start-up
22+
sleep 0.5
23+
24+
# Use bash magic to open tcp socket on fd 3 and break when successful
25+
exec 3<>/dev/tcp/127.0.0.1/9999 && break
26+
done
1427

15-
# Break when connected
16-
#echo state | busybox nc 127.0.0.1 9999 | grep -q "CONNECTED,SUCCESS" && break;
28+
if [ $i -ge $timeout ]; then
29+
echo "Error connecting to OpenVPN mgmt interface, i=$i, exiting."
30+
exit 2
31+
fi
1732

18-
# Bash magic for tcp sockets
19-
if exec 3<>/dev/tcp/127.0.0.1/9999; then
20-
# Consume all header input
21-
while read -t 0.1 <&3; do true; done
22-
echo "state" >&3
23-
read -t 1 <&3
24-
echo -n $REPLY | grep -q "CONNECTED,SUCCESS" && break || true
25-
exec 3>&-
26-
fi
33+
# Consume all header input and echo, look for errors here
34+
while read -t 0.1 <&3; do echo $REPLY; done
2735

28-
# Else sleep
36+
# Request state over mgmt interface
37+
timeout=10
38+
for i in $(seq $timeout); do
39+
echo "state" >&3
40+
state=$(head -n1 <&3)
41+
echo -n "$state" | grep -q 'CONNECTED,SUCCESS' && break
2942
sleep 1
3043
done
3144

3245
if [ $i -ge $timeout ]; then
33-
echo "Error starting OpenVPN, i=$i, exiting."
34-
exit 2;
46+
echo "Error connecting to OpenVPN, i=$i, exiting."
47+
exit 3
3548
fi
3649

37-
# The show is over.
38-
kill %1
50+
exec 3>&-

test/tests/basic/run.sh

+11-19
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,22 @@ docker run -v $OVPN_DATA:/etc/openvpn --rm $IMG ovpn_getclient $CLIENT | tee $CL
2222
docker run -v $OVPN_DATA:/etc/openvpn --rm $IMG ovpn_listclients | grep $CLIENT
2323

2424
#
25-
# Fire up the server
25+
# Fire up the server and setup a trap to always clean it up
2626
#
27-
sudo iptables -N DOCKER || echo 'Firewall already configured'
28-
sudo iptables -I FORWARD -j DOCKER || echo 'Forward already configured'
29-
# run in shell bg to get logs
30-
docker run --name "ovpn-test" -v $OVPN_DATA:/etc/openvpn --rm -p 1194:1194/udp --privileged $IMG &
27+
trap "{ jobs -p | xargs -r kill; wait; }" EXIT
28+
docker run --name "ovpn-test" -v $OVPN_DATA:/etc/openvpn --rm -e DEBUG --cap-add=NET_ADMIN $IMG &
3129

32-
#for i in $(seq 10); do
33-
# SERV_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}')
34-
# test -n "$SERV_IP" && break
35-
#done
36-
#sed -ie s:SERV_IP:$SERV_IP:g config.ovpn
30+
for i in $(seq 10); do
31+
SERV_IP_INTERNAL=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "ovpn-test" 2>/dev/null || true)
32+
test -n "$SERV_IP_INTERNAL" && break
33+
sleep 0.1
34+
done
35+
sed -i -e s:$SERV_IP:$SERV_IP_INTERNAL:g ${CLIENT_DIR}/config.ovpn
3736

3837
#
39-
# Fire up a client in a container since openvpn is disallowed by Travis-CI, don't NAT
40-
# the host as it confuses itself:
41-
# "Incoming packet rejected from [AF_INET]172.17.42.1:1194[2], expected peer address: [AF_INET]10.240.118.86:1194"
38+
# Fire up a client in a container since openvpn is disallowed by Travis-CI
4239
#
43-
docker run --rm --net=host --privileged --volume $CLIENT_DIR:/client $IMG /client/wait-for-connect.sh
44-
45-
#
46-
# Client either connected or timed out, kill server
47-
#
48-
kill %1
40+
docker run --rm --cap-add=NET_ADMIN -e DEBUG --volume $CLIENT_DIR:/client $IMG /client/wait-for-connect.sh
4941

5042
#
5143
# Celebrate

test/tests/dual-proto/run.sh

+21-14
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,32 @@ docker run -v $OVPN_DATA:/etc/openvpn --rm $IMG ovpn_listclients | grep $CLIENT_
3434
#
3535
# Fire up the server
3636
#
37-
sudo iptables -N DOCKER || echo 'Firewall already configured'
38-
sudo iptables -I FORWARD -j DOCKER || echo 'Forward already configured'
3937

40-
# run in shell bg to get logs
41-
docker run --name "ovpn-test-udp" -v $OVPN_DATA:/etc/openvpn --rm -p 1194:1194/udp --privileged $IMG &
42-
docker run --name "ovpn-test-tcp" -v $OVPN_DATA:/etc/openvpn --rm -p 443:1194/tcp --privileged $IMG ovpn_run --proto tcp &
38+
# Run in shell bg to get logs, setup trap to clean-up
39+
trap "{ jobs -p | xargs -r kill; wait; docker volume rm ${OVPN_DATA}; }" EXIT
40+
docker run --name "ovpn-test-udp" -v $OVPN_DATA:/etc/openvpn --rm --cap-add=NET_ADMIN -e DEBUG $IMG &
41+
docker run --name "ovpn-test-tcp" -v $OVPN_DATA:/etc/openvpn --rm --cap-add=NET_ADMIN -e DEBUG $IMG ovpn_run --proto tcp --port 443 &
4342

44-
#
45-
# Fire up a clients in a containers since openvpn is disallowed by Travis-CI, don't NAT
46-
# the host as it confuses itself:
47-
# "Incoming packet rejected from [AF_INET]172.17.42.1:1194[2], expected peer address: [AF_INET]10.240.118.86:1194"
48-
#
49-
docker run --rm --net=host --privileged --volume $CLIENT_DIR:/client $IMG /client/wait-for-connect.sh
50-
docker run --rm --net=host --privileged --volume $CLIENT_DIR:/client $IMG /client/wait-for-connect.sh "/client/config-tcp.ovpn"
43+
# Update configs
44+
for i in $(seq 10); do
45+
SERV_IP_INTERNAL=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "ovpn-test-udp" 2>/dev/null || true)
46+
test -n "$SERV_IP_INTERNAL" && break
47+
sleep 0.1
48+
done
49+
sed -i -e s:$SERV_IP:$SERV_IP_INTERNAL:g $CLIENT_DIR/config.ovpn
50+
51+
for i in $(seq 10); do
52+
SERV_IP_INTERNAL=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "ovpn-test-tcp" 2>/dev/null || true)
53+
test -n "$SERV_IP_INTERNAL" && break
54+
sleep 0.1
55+
done
56+
sed -i -e s:$SERV_IP:$SERV_IP_INTERNAL:g $CLIENT_DIR/config-tcp.ovpn
5157

5258
#
53-
# Client either connected or timed out, kill server
59+
# Fire up a clients in a containers since openvpn is disallowed by Travis-CI
5460
#
55-
kill %1 %2
61+
docker run --rm --cap-add=NET_ADMIN -v $CLIENT_DIR:/client -e DEBUG $IMG /client/wait-for-connect.sh
62+
docker run --rm --cap-add=NET_ADMIN -v $CLIENT_DIR:/client -e DEBUG $IMG /client/wait-for-connect.sh "/client/config-tcp.ovpn"
5663

5764
#
5865
# Celebrate

test/tests/iptables/run.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ docker run -v $OVPN_DATA:/etc/openvpn --rm -it -e "EASYRSA_BATCH=1" -e "EASYRSA_
1616
docker run -d --name $NAME -v $OVPN_DATA:/etc/openvpn --cap-add=NET_ADMIN $IMG
1717

1818
# check default iptables rules
19-
docker exec -ti $NAME bash -c 'source /etc/openvpn/ovpn_env.sh; eval iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o eth0 -j MASQUERADE'
19+
for i in $(seq 10); do
20+
docker exec -ti $NAME bash -c 'source /etc/openvpn/ovpn_env.sh; exec iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o eth0 -j MASQUERADE' && break
21+
echo waiting for server start-up
22+
sleep 1
23+
done
2024

2125
# append new setupIptablesAndRouting function to config
2226
docker exec -ti $NAME bash -c 'echo function setupIptablesAndRouting { iptables -t nat -A POSTROUTING -m comment --comment "test"\;} >> /etc/openvpn/ovpn_env.sh'
2327

2428
# kill server in preparation to modify config
25-
docker kill $NAME
26-
docker rm $NAME
29+
docker rm -f $NAME
2730

2831
# check that overridden function exists and that test iptables rules is active
2932
docker run -d --name $NAME -v $OVPN_DATA:/etc/openvpn --cap-add=NET_ADMIN $IMG
@@ -33,6 +36,5 @@ docker exec -ti $NAME bash -c 'source /etc/openvpn/ovpn_env.sh; type -t setupIpt
3336
# kill server
3437
#
3538

36-
docker kill $NAME
37-
docker rm $NAME
39+
docker rm -f $NAME
3840
docker volume rm $OVPN_DATA

0 commit comments

Comments
 (0)