Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow providing response file for running non-interactively #2

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
config/resolv.template
config/client/
config/response.txt
resolv.conf
/.idea/
packages/*.deb
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selectio
kmod \
iptables \
ca-certificates \
file
file \
gettext-base

RUN mkdir /root/Install
WORKDIR /root/Install
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ be routed via the VPN. For example:
routes=(10.0.0.0/8)
```
Additional routes are space separated (it is a normal bash array)
* Optionally add a response file called `config/response.txt`. If present, the commands in this file will be read to work non-interactively. You will also be prompted separately to enter your password, which is then available as `$VPN_PASSWORD` in the response file. For example:
```
connect vpn.mycompany.com
1
vpn-username
$VPN_PASSWORD
y
```

## To run

Expand Down
8 changes: 7 additions & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ sysctl net.ipv4.conf.all.forwarding=1
/start-traps.sh &

/opt/cisco/anyconnect/bin/vpnagentd
/opt/cisco/anyconnect/bin/vpn

if [ -f /response.txt ]; then
cat /response.txt | envsubst '$VPN_PASSWORD' | /opt/cisco/anyconnect/bin/vpn -s && \
unset VPN_PASSWORD && \
tail -f /dev/null
else
/opt/cisco/anyconnect/bin/vpn
fi
22 changes: 21 additions & 1 deletion start-vpn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ if [ ! -f config/client/private/*.key ]; then
exit 1
fi

if [ -f config/response.txt ]; then
if ! grep -q '\$VPN_PASSWORD' config/response.txt; then
echo 'Please make sure to reference VPN_PASSWORD in config/response.txt'
exit 1
fi

echo -n 'VPN password: '
read -s VPN_PASSWORD
echo
export VPN_PASSWORD

MOUNT_RESPONSE_FILE="-e VPN_PASSWORD -v $(pwd)/config/response.txt:/response.txt"
else
MOUNT_RESPONSE_FILE=''
fi

. config/routes

docker network ls | grep vpn-network > /dev/null
Expand All @@ -53,18 +69,22 @@ HAS_IMAGE=$?
if [ $HAS_IMAGE -ne 0 ]; then
echo "Creating docker image for VPN"
docker build --tag vpn-anyconnect .
else
echo 'Updating Docker image...'
docker build -q --tag vpn-anyconnect . >/dev/null || exit 1
fi

echo "Starting VPN"

mv /etc/resolv.conf /etc/resolv.conf.vpn-orig
cp config/resolv.template /etc/resolv.conf
chmod a+r /etc/resolv.conf

for r in ${routes[@]}; do
ip route add $r via 172.19.0.2
done

docker run --name vpn-anyconnect --privileged --cap-add NET_ADMIN --cap-add SYS_ADMIN -ti -v "$(pwd)/config/client":/root/.cisco/certificates/client --net vpn-network --ip 172.19.0.2 --rm vpn-anyconnect
docker run --name vpn-anyconnect --privileged --cap-add NET_ADMIN --cap-add SYS_ADMIN -ti -v "$(pwd)/config/client":/root/.cisco/certificates/client $MOUNT_RESPONSE_FILE --net vpn-network --ip 172.19.0.2 --rm vpn-anyconnect
echo "Restoring original configuration"

for r in ${routes[@]}; do
Expand Down