Automatically set DefaultRoute once, fixed kibana export check

This commit is contained in:
Patrik Kernstock 2020-08-09 00:33:57 +01:00
parent 3d5860388a
commit 6f6dbda712

View file

@ -1,4 +1,6 @@
#!/bin/bash #!/bin/bash
# Copyright 2020, Patrik Kernstock.
set -x set -x
echo "## ELASTICSEARCH" echo "## ELASTICSEARCH"
@ -17,9 +19,10 @@ if [ ! -f "/etc/nginx/ssl/kibana.crt" ] || [ ! -f "/etc/nginx/ssl/kibana.key" ];
fi fi
echo "## KIBANA" echo "## KIBANA"
if [ ! -f /etc/parsedmarc/kibana_export.ndjson ]; then exportFile="/etc/parsedmarc/kibana_export.ndjson"
if [ ! -f "${exportFile}" ]; then
# trigger empty file to trigger below update logic. # trigger empty file to trigger below update logic.
touch /etc/parsedmarc/kibana_export.ndjson touch ${exportFile}
fi fi
echo "Downloading dashboard from GitHub..." echo "Downloading dashboard from GitHub..."
rm /etc/parsedmarc/kibana_export.ndjson.tmp rm /etc/parsedmarc/kibana_export.ndjson.tmp
@ -28,10 +31,10 @@ curl https://raw.githubusercontent.com/domainaware/parsedmarc/master/kibana/expo
if [ ${?} -ne 0 ]; then if [ ${?} -ne 0 ]; then
echo "Downloading kibana export failed." echo "Downloading kibana export failed."
else else
fileNew=$(wc -c "/etc/parsedmarc/kibana_export.ndjson.tmp") # always use quoted var fileNew=$(wc -c "${exportFile}.tmp" | awk -F' ' '{ print $1 }')
fileOld=$(wc -c "/etc/parsedmarc/kibana_export.ndjson") fileOld=$(wc -c "${exportFile}" | awk -F' ' '{ print $1 }')
if [ $fileNew -eq $fileOld ]; then if [ "$fileNew" == "$fileOld" ]; then
echo "File size is the same. Not proceeding." echo "File size is the same. Not proceeding."
else else
echo "File size is different... updating..." echo "File size is different... updating..."
@ -43,20 +46,47 @@ else
echo "Kibana responded. Waiting 10s, then proceeding with dashboard update..." echo "Kibana responded. Waiting 10s, then proceeding with dashboard update..."
sleep 10 sleep 10
rm /etc/parsedmarc/kibana_export.ndjson rm ${exportFile}
mv /etc/parsedmarc/kibana_export.ndjson.tmp /etc/parsedmarc/kibana_export.ndjson mv ${exportFile}.tmp ${exportFile}
RES=$(curl -X POST http://kibana:5601/api/saved_objects/_import?overwrite=true \ RES=$(curl -X POST http://kibana:5601/api/saved_objects/_import?overwrite=true \
-H "kbn-xsrf: true" --form file=@/etc/parsedmarc/kibana_export.ndjson) -H "kbn-xsrf: true" --form file=@${exportFile})
echo "Result: $RES" echo "Result: $RES"
if [ ${?} -ne 0 ]; then if [ ${?} -ne 0 ]; then
echo "[!!!] Import might have failed. Manual check recommended." echo "[!!!] Import might have failed. Manual check recommended."
else
# if the flag exists, we already set the defaultRoute once. So we don't do that again.
if [ ! -f "/etc/parsedmarc/flag.defaultRouteSet" ]; then
DEF_DASHBOARD_NAME="DMARC Summary"
echo "Setting '${DEF_DASHBOARD_NAME}' dashboard as default route..."
DEF_DASHBOARD_ID=$(cat "${exportFile}" | jq --arg DBNAME "${DEF_DASHBOARD_NAME}" 'select(.attributes.title == $DBNAME) | .id' | tr -d '"')
if [ "$DEF_DASHBOARD_ID" != "" ]; then
echo "Found dashboard ID: ${DEF_DASHBOARD_ID}"
DEFAULT_ROUTE="/app/kibana#/dashboard/${DEF_DASHBOARD_ID}"
echo "DefaultRoute being set to: ${DEFAULT_ROUTE}"
curl -X POST -H "kbn-xsrf: true" -H "Content-Type: application/json" \
"http://kibana:5601/api/kibana/settings/defaultRoute" \
-d "{\"value\": \"${DEFAULT_ROUTE}\"}"
if [ ${?} -ne 0 ]; then
echo "[!!!] Setting defaultRoute seems to gone wrong. Manual check recommended."
else
echo "DefaultRoute set."
echo "Notice: This might require a restart of Kibana to take effect. Not done automatically as part of this script."
echo -e "# This is a flag to remember which defaultRoute we set in the past:\n${DEFAULT_ROUTE}" \
> /etc/parsedmarc/flag.defaultRouteSet
fi
else
echo "[!] Default dashboard with name '${DEF_DASHBOARD_NAME}' could not be found."
fi
fi
fi fi
echo "Importing done." echo "Dashboard import done."
fi fi
fi fi
sleep 3 sleep 3
# Create empty file to let other containers know that we're ready. # Create empty file to let other containers know that we're ready.
touch /ready touch /ready
sleep infinity # or while true; do sleep 86400; done sleep infinity # or while true; do sleep 86400; done
exit 0 exit 0