Enable options for certificate and domain

This commit is contained in:
Jack Jackson 2022-08-22 13:20:02 -07:00
parent ce499cffa4
commit 6decdb445d

View File

@ -1,30 +1,68 @@
#!/bin/sh
#!/bin/bash
set -eux
# First argument is cloudflared config file
# TODO - make this optional, and check standard locations if absent.
# Not doing it now because that would require me to remember how to
# do named arguments with bash (I think it's `getopt`?), and ain't
# nobody got time for that.
if [ $# -lt 1 ]; then
echo "Requires an argument pointing to the config file";
exit 1;
fi
CONFIG_FILE_LOCATION=$1
if [ ! -f $CONFIG_FILE_LOCATION ]; then
echo "File \"$CONFIG_FILE_LOCATION\" does not exist";
exit 1;
fi
# https://stackoverflow.com/a/14203146/1040915
POSITIONAL_ARGS=()
# Second argument is "root domain".
while [[ $# -gt 0 ]]; do
case $1 in
--config)
# Provide a path to the Cloudflared Config file.
# Required, since we need to parse the config file to find tunnel name.
# (If I was less lazy, I would implement cloudflared-like behaviour to search
# the standard locations if absent. But I am, so I didn't :P )
CONFIG="$2"
shift # past argument
shift # past value
;;
--cert)
# Provide a path to the Cloudflared certificate.
# If absent, Cloudflared will search the standard locations (as above).
# ([`/etc/cloudflared/`, `/usr/local/etc/cloudflared`, `$HOME/.cloudflared`])
CERT="$2"
shift # past argument
shift # past value
;;
-d|--domain)
# If set, only try to update DNS for names that are subdomains of this domain.
# If not set, try to update all names
TUNNEL_NAME=$(yq ".tunnel" $CONFIG_FILE_LOCATION);
if [ $# -gt 1 ]; then
ROOT_DOMAIN=$2;
yq ".ingress[].hostname | select(. != null) | select (. == \"*$ROOT_DOMAIN\")" $CONFIG_FILE_LOCATION | xargs -I {} cloudflared tunnel route dns $TUNNEL_NAME {};
else
yq '.ingress[].hostname | select(. != null)' $CONFIG_FILE_LOCATION | xargs -I {} cloudflared tunnel route dns $TUNNEL_NAME {};
# If not set, try to update all names.
DOMAIN="$2"
shift # past argument
shift # past value
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
if [[ -z $CONFIG ]]; then
echo "Path to config file must be provided";
exit 1;
fi
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
TUNNEL_NAME=$(yq ".tunnel" $CONFIG);
COMMAND_STRING="yq \".ingress[].hostname | select(. != null)";
if [[ -n "$DOMAIN" ]]; then
COMMAND_STRING="$COMMAND_STRING | select (. == \\\"*$DOMAIN\\\")";
fi
# Note closing double-quote, from start of COMMAND_STRING
COMMAND_STRING="$COMMAND_STRING\" $CONFIG | xargs -I {} cloudflared tunnel";
if [[ -n "$CERT" ]]; then
COMMAND_STRING="$COMMAND_STRING --origincert $CERT";
fi
COMMAND_STRING="$COMMAND_STRING route dns $TUNNEL_NAME {}";
# I don't know enough about bash security to know whether there's a risk of injection here:
# be careful where you accept script parameters from!
eval $COMMAND_STRING