From 6f6dbda7126b5948cae1ebe05c20ebaddb38a39b Mon Sep 17 00:00:00 2001 From: Patrik Kernstock Date: Sun, 9 Aug 2020 00:33:57 +0100 Subject: [PATCH] Automatically set DefaultRoute once, fixed kibana export check --- data/Dockerfiles/parsedmarc-init/start.sh | 50 ++++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/data/Dockerfiles/parsedmarc-init/start.sh b/data/Dockerfiles/parsedmarc-init/start.sh index eda5c9c..2d8783a 100644 --- a/data/Dockerfiles/parsedmarc-init/start.sh +++ b/data/Dockerfiles/parsedmarc-init/start.sh @@ -1,4 +1,6 @@ #!/bin/bash +# Copyright 2020, Patrik Kernstock. + set -x echo "## ELASTICSEARCH" @@ -17,9 +19,10 @@ if [ ! -f "/etc/nginx/ssl/kibana.crt" ] || [ ! -f "/etc/nginx/ssl/kibana.key" ]; fi 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. - touch /etc/parsedmarc/kibana_export.ndjson + touch ${exportFile} fi echo "Downloading dashboard from GitHub..." rm /etc/parsedmarc/kibana_export.ndjson.tmp @@ -28,14 +31,14 @@ curl https://raw.githubusercontent.com/domainaware/parsedmarc/master/kibana/expo if [ ${?} -ne 0 ]; then echo "Downloading kibana export failed." else - fileNew=$(wc -c "/etc/parsedmarc/kibana_export.ndjson.tmp") # always use quoted var - fileOld=$(wc -c "/etc/parsedmarc/kibana_export.ndjson") + fileNew=$(wc -c "${exportFile}.tmp" | awk -F' ' '{ print $1 }') + 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." else echo "File size is different... updating..." - + while ! curl -s -f -I http://kibana:5601 >/dev/null; do echo "Kibana not responding... waiting 5 secs..." sleep 5 @@ -43,20 +46,47 @@ else echo "Kibana responded. Waiting 10s, then proceeding with dashboard update..." sleep 10 - rm /etc/parsedmarc/kibana_export.ndjson - mv /etc/parsedmarc/kibana_export.ndjson.tmp /etc/parsedmarc/kibana_export.ndjson + rm ${exportFile} + mv ${exportFile}.tmp ${exportFile} 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" if [ ${?} -ne 0 ]; then 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 - echo "Importing done." + echo "Dashboard import done." fi fi sleep 3 + # Create empty file to let other containers know that we're ready. touch /ready sleep infinity # or while true; do sleep 86400; done + exit 0