cp-tls-inspection-applicati.../cron-example-with-diff-dstdom.sh

259 lines
7.4 KiB
Bash
Raw Normal View History

2021-07-13 17:46:31 +03:00
#!/usr/bin/env bash
2021-07-18 14:34:31 +03:00
FLAGS_PREFIX="/tmp/dst-dom-script-flag_"
2021-07-18 23:13:59 +03:00
WORK_DIR="/tmp/dst-dom-script"
mkdir -vp "${WORK_DIR}"
cd "${WORK_DIR}"
export CPDIR=/opt/fw1
export FWDIR=${CPDIR}
export SUROOT=/var/suroot
. /pfrm2.0/etc/bashrc
2021-07-18 14:34:31 +03:00
2021-07-18 23:45:03 +03:00
if [ "$( pt users -f username $USER -F role | head -n 1 | grep -v {} )" != "ROLE.SUPER" ];then
echo "This script can only run from a user with ROLE.SUPER ie super user"
exit 1
fi
2021-07-18 14:34:31 +03:00
if [ -f "${FLAGS_PREFIX}unsetx" ];then
set -x
fi
2021-11-01 09:57:49 +02:00
DEBUG="1"
2021-07-13 23:04:40 +03:00
DRY_RUN="0"
2021-07-13 17:46:31 +03:00
CLEANUP_AFTER="1"
REGEX_FLAG_ENABLED="0"
2021-07-14 14:53:38 +03:00
LOCK_FILE="/tmp/dst-domain-cron-lockfile"
2021-07-18 18:32:50 +03:00
DUMP_ENV="/tmp/env_cd5fecd5-7123-4a21-bd02-242f1d695a6d"
2021-07-18 23:13:59 +03:00
CA_CERT_BUNDLE_PATH="/pfrm2.0/opt/fw1/bin/ca-bundle.crt"
SSL_CERT_FILE="${CA_CERT_BUNDLE_PATH}"
2021-07-14 14:53:38 +03:00
if [ -f "${LOCK_FILE}" ];then
2021-07-18 23:13:59 +03:00
echo "Lockfile \"${LOCK_FILE}\" exits, stopping update"
exit 0
2021-07-14 14:53:38 +03:00
fi
touch "${LOCK_FILE}"
2021-07-13 17:46:31 +03:00
2021-07-18 13:31:49 +03:00
if [ -f "${FLAGS_PREFIX}debug" ];then
2021-07-18 23:13:59 +03:00
DEBUG="1"
2021-07-13 23:04:40 +03:00
fi
2021-07-18 13:31:49 +03:00
if [ -f "${FLAGS_PREFIX}dry-run" ];then
2021-07-18 23:13:59 +03:00
DRY_RUN="1"
echo "Runnning dry run"
2021-07-13 23:04:40 +03:00
fi
2021-07-18 13:31:49 +03:00
if [ -f "${FLAGS_PREFIX}cleanup-after" ];then
2021-07-18 23:13:59 +03:00
CLEANUP_AFTER="1"
2021-07-13 23:04:40 +03:00
fi
2021-07-18 13:31:49 +03:00
if [ -f "${FLAGS_PREFIX}dont-cleanup-after" ];then
2021-07-18 23:13:59 +03:00
CLEANUP_AFTER="0"
2021-07-18 13:31:49 +03:00
fi
if [ -f "${FLAGS_PREFIX}add-regex-flag" ];then
2021-07-18 23:13:59 +03:00
REGEX_FLAG_ENABLED="1"
2021-07-13 23:04:40 +03:00
fi
2021-07-18 18:32:50 +03:00
if [ -f "${FLAGS_PREFIX}dump-env" ];then
2021-07-18 23:13:59 +03:00
env |tee "${DUMP_ENV}"
export | tee -a "${DUMP_ENV}"
2021-07-18 18:32:50 +03:00
fi
2021-07-13 17:46:31 +03:00
function dstdomain_to_regex() {
2021-07-18 23:13:59 +03:00
prefix="\."
suffix="\."
dot="\."
dash="-"
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
domain="$1"
dstdomain="0"
dotsuffix="0"
2021-07-13 17:46:31 +03:00
2021-08-02 01:19:59 +03:00
if [ -z "${domain}" ];then
return
fi
2021-07-18 23:13:59 +03:00
echo "${domain}" | grep -e "^\." > /dev/null
if [ "$?" -eq "0" ];then
dstdomain=1
fi
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
echo "${domain}" | grep -e "\.$" > /dev/null
if [ "$?" -eq "0" ];then
dotsuffix=1
fi
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
case ${dstdomain} in
1)
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^@g" -e "s/$/\\$/"
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^[0-9a-zA-Z\\\-\\\.]+\\\.@g" -e "s/$/\\$/"
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^@g" -e "s/$/\\\.\\$/"
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^[0-9a-zA-Z\\\-\\\.]+\\\.@g" -e "s/$/\\\\.\\$/"
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
;;
*)
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^@g" -e "s/$/\\$/"
echo "${domain}" | sed -e "s/^${prefix}//" -e "s/${suffix}$//" -e "s/${dash}/\\\-/g" -e "s/${dot}/\\\./g" -e "s@^@\\^@g" -e "s/$/\\\.\\$/"
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
;;
esac
2021-07-13 17:46:31 +03:00
}
APP_NAME="$1"
2021-07-13 23:04:40 +03:00
URL="$2"
if [ -f "dst-domain-url" ];then
2021-07-18 23:13:59 +03:00
echo "Overriding URL with a local dst-domain-url file"
DST_DOM_URL_FILE_SIZE=$(cat dst-domain-url |wc -l)
if [ "${DST_DOM_URL_FILE_SIZE}" -gt "0" ];then
URl=$( head -n1 dst-domain-url )
else
echo "dst-domain-url is empty"
exit 1
fi
2021-07-13 23:04:40 +03:00
fi
2021-07-13 17:46:31 +03:00
if [ -z "${APP_NAME}" ];then
echo "Missing App Name"
exit 1
fi
if [ "$2" == "check" ];then
DRY_RUN="1"
echo "Running in dry run mode" >&2
fi
if [ ! -z "$3" ];then
URL="$3"
fi
TMP_DOWNLOAD_FILE=$(mktemp)
# The next is to avoid missing new line in the end of the file in some cases
2021-07-18 23:13:59 +03:00
/opt/fw1/bin/curl_cli -s --cacert "${SSL_CERT_FILE}" "${URL}" -o "${TMP_DOWNLOAD_FILE}" >> /tmp/log.1
RES="$?"
2021-07-13 17:46:31 +03:00
if [ "${RES}" -gt "0" ];then
echo "Error Downloading file from URL: \"${URL}\""
2021-07-18 18:25:28 +03:00
logger "Error Downloading file from URL: \"${URL}\""
2021-07-13 17:46:31 +03:00
rm -v "${TMP_DOWNLOAD_FILE}"
2021-07-18 18:25:28 +03:00
rm -fv "${LOCK_FILE}"
2021-07-13 17:46:31 +03:00
exit ${RES}
fi
# The next is to avoid missing new line in the end of the file in some cases
echo "" >> "${TMP_DOWNLOAD_FILE}"
2021-07-13 17:46:31 +03:00
TMP_CLISH_UPDATE_FILE=$( mktemp )
TMP_CURRENT_CONFIG_FILE=$( mktemp )
TMP_CLISH_TRANSACTION_FILE=$( mktemp )
2021-07-13 23:04:40 +03:00
TMP_DIFF_FILE=$( mktemp )
2021-07-18 14:34:31 +03:00
TMP_CURRENT_APP_CONTENT_FILE=$( mktemp )
2021-07-13 17:46:31 +03:00
2021-07-18 23:13:59 +03:00
APP_DETAILS=$( clish -c "show application application-name ${APP_NAME}" )
echo "$? exit code from clish -c \"show application application-name ${APP_NAME}\""
echo "${APP_DETAILS}" | sed -e "s@^description.*@@g" \
-e "s@.*Role\ is\ not\ assigned\ to\ user.*@@g" \
2021-07-18 14:34:31 +03:00
-e "s@^application\-name\:.*@@g" \
-e "s@^application\-id\:.*@@g" \
-e "s@^Categories\:.*@@g" \
-e "s@^application\-urls\:@@g" \
-e 's@^[ \t]\+@@g' \
-e '/^$/ d' > ${TMP_CURRENT_APP_CONTENT_FILE}
2021-07-18 23:13:59 +03:00
CURRENT_APP_CONTENT_REGEX=$( cat "${TMP_CURRENT_APP_CONTENT_FILE}" |sort )
2021-07-18 14:34:31 +03:00
REMOTE_APP_CONTENT_REGEX=$( mktemp )
2021-07-18 23:13:59 +03:00
echo "${CURRENT_APP_CONTENT_REGEX}" > "${TMP_CURRENT_APP_CONTENT_FILE}"
comp_start=`date +%s`
echo "Compiling APP_REGEX started at: ${comp_start}"
2021-07-13 17:46:31 +03:00
while IFS= read -r line
do
if [ "${DEBUG}" -gt "0" ];then
echo -n "DEBUG LEVEL 1: Working on dstdomain: " >&2
echo "${line}" >&2
fi
2021-07-18 14:34:31 +03:00
dstdomain_to_regex_result="$(dstdomain_to_regex ${line})"
2021-07-18 23:13:59 +03:00
echo "${dstdomain_to_regex_result}" |tee -a "${REMOTE_APP_CONTENT_REGEX}" >/dev/null
2021-07-13 17:46:31 +03:00
done < ${TMP_DOWNLOAD_FILE}
2021-07-18 23:13:59 +03:00
comp_end=`date +%s`
comp_runtime=$( echo "$comp_end - $comp_start" | bc -l )
echo "FINISHED Compiling APP_REGEX finished at: ${comp_end} , Took: ${comp_runtime} Seconds"
SORTED_REMOTE_APP_CONTENT_REGEX=$( cat "${REMOTE_APP_CONTENT_REGEX}" | sort| uniq )
2021-07-18 14:34:31 +03:00
echo "${SORTED_REMOTE_APP_CONTENT_REGEX}" > "${REMOTE_APP_CONTENT_REGEX}"
2021-07-13 23:04:40 +03:00
2021-07-18 14:34:31 +03:00
DIFF=$( diff "${TMP_CURRENT_APP_CONTENT_FILE}" "${REMOTE_APP_CONTENT_REGEX}" |sed -e "1,3d;" )
echo "DIFF CMD: diff ${TMP_CURRENT_APP_CONTENT_FILE} ${REMOTE_APP_CONTENT_REGEX} | sed -e \"1,3d;\""
2021-07-13 23:04:40 +03:00
2021-07-18 14:34:31 +03:00
##
DELETE_OBJECTS=$( echo "${DIFF}" |egrep "^\-" |sed -e "s@^\-@@")
2021-07-13 17:46:31 +03:00
for object in ${DELETE_OBJECTS}; do
echo "set application application-name \"${APP_NAME}\" remove url ${object}" >> ${TMP_CLISH_TRANSACTION_FILE}
done
2021-07-18 14:34:31 +03:00
APPEND_OBJECTS=$( echo "${DIFF}" |egrep "^\+" |sed -e "s@^\+@@")
2021-07-13 17:46:31 +03:00
2021-07-18 14:34:31 +03:00
for object in ${APPEND_OBJECTS}; do
echo "set application application-name \"${APP_NAME}\" add url ${object}" >> ${TMP_CLISH_TRANSACTION_FILE}
done
##
2021-07-13 23:04:40 +03:00
sed -i -e 's@\\@\\\\\\@g' "${TMP_CLISH_TRANSACTION_FILE}"
2021-07-13 17:46:31 +03:00
if [ "${DRY_RUN}" -eq "0" ];then
2021-08-02 01:21:17 +03:00
clish -i -f "${TMP_CLISH_TRANSACTION_FILE}"
2021-07-18 23:13:59 +03:00
echo "$? exit code from clish -f"
else
echo "Running in DRY-RUN MODE"
2021-07-13 17:46:31 +03:00
fi
echo "Finished Transaction"
echo "Cleaning up files ..."
if [ "${CLEANUP_AFTER}" -eq "1" ];then
rm -v "${TMP_DOWNLOAD_FILE}"
rm -v "${TMP_CLISH_UPDATE_FILE}"
2021-07-18 23:13:59 +03:00
rm -v "${TMP_CURRENT_APP_CONTENT_FILE}"
rm -v "${REMOTE_APP_CONTENT_REGEX}"
2021-07-13 17:46:31 +03:00
rm -v "${TMP_CURRENT_CONFIG_FILE}"
2021-07-13 23:04:40 +03:00
rm -v "${TMP_DIFF_FILE}"
2021-07-13 17:46:31 +03:00
rm -v "${TMP_CLISH_TRANSACTION_FILE}"
2021-07-13 23:04:40 +03:00
2021-07-13 17:46:31 +03:00
else
echo "Don't forget to cleanup the files:"
echo "${TMP_DOWNLOAD_FILE}"
echo "${TMP_CLISH_UPDATE_FILE}"
2021-07-18 23:13:59 +03:00
echo "${TMP_CURRENT_APP_CONTENT_FILE}"
2021-08-02 01:28:09 +03:00
echo "${REMOTE_APP_CONTENT_REGEX}"
2021-07-13 17:46:31 +03:00
echo "${TMP_CURRENT_CONFIG_FILE}"
2021-07-13 23:04:40 +03:00
echo "${TMP_DIFF_FILE}"
2021-07-13 17:46:31 +03:00
echo "${TMP_CLISH_TRANSACTION_FILE}"
fi
2021-07-14 14:53:38 +03:00
2021-07-14 15:11:52 +03:00
rm -fv "${LOCK_FILE}"
2021-07-18 13:35:00 +03:00
2021-07-19 14:05:01 +03:00
logger "Finished running a dstdomain update for: APP => \"${APP_NAME}\" , from URL => \"${URL}\""
2021-07-18 14:34:31 +03:00
set +x