Skip to content

Instantly share code, notes, and snippets.

@akinazuki
Last active October 13, 2022 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akinazuki/9d9cd8aea25c4a5cd3ade58c8be44d8a to your computer and use it in GitHub Desktop.
Save akinazuki/9d9cd8aea25c4a5cd3ade58c8be44d8a to your computer and use it in GitHub Desktop.
Authenicating OpenVPN via 1Password
#!/bin/bash
credentials_temp_fifo_file=/tmp/.$$-credentials.pipe
openvpn_temp_fifo_file=/tmp/.$$-openvpn.pipe
mkfifo $credentials_temp_fifo_file
default_op_path='op://empty/item' # 1Password item path
openvpn_exists=$(which openvpn)
op_cli_exists=$(which op)
if [ -z "$openvpn_exists" ]; then
echo "OpenVPN CLI not found. Please install OpenVPN."
exit 1
fi
if [ -z "$op_cli_exists" ]; then
echo "1Password CLI not found. Please install 1Password CLI."
exit 1
fi
if [ -z "$1" ]; then
if [ $default_op_path == "op://empty/item" ]; then
echo "No 1Password item path provided"
exit 1
else
op_path=$default_op_path
fi
else
op_path=$1
fi
op_id=`echo $op_path | cut -d'/' -f4` # 1Password item id
ovpn_file_name=`op item get $op_id|grep Files|head -n 1|awk '{print $2}'`
echo "Starting OpenVPN with credentials from $op_path"
echo "OpenVPN config file name: $ovpn_file_name"
echo "Press Control+C to stop OpenVPN"
op read $op_path/$ovpn_file_name > $openvpn_temp_fifo_file
op read $op_path/auth > $credentials_temp_fifo_file |
sudo openvpn \
--config $openvpn_temp_fifo_file \
--auth-user-pass $credentials_temp_fifo_file
echo "OpenVPN exited with code $?"
rm -f $credentials_temp_fifo_file
rm -f $openvpn_temp_fifo_file
echo "Named Pipe $credentials_temp_fifo_file removed"
echo "Named Pipe $openvpn_temp_fifo_file removed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment