src/docker/scripts/kev.sh
#!/bin/sh

set -e

echo "Mirroring Known Exploited Vulnerabilities..."

LOCKFILE=/tmp/kev.lock

if [ -f $LOCKFILE ]; then
  echo "Lockfile found - another mirror-sync process already running"
else
  touch $LOCKFILE
fi

function remove_lockfile() {
  rm -f $LOCKFILE
  exit 0
}
trap remove_lockfile SIGHUP SIGINT SIGQUIT SIGABRT SIGALRM SIGTERM SIGTSTP

CACERT_ARG=""
if [ -n "${CACERT}" ]; then
  echo "Using cacert file: $CACERT"
  if [ -f "$CACERT" ]; then
    echo "File $CACERT exists."
    CACERT_ARG="--cacert $CACERT"
  else
    echo "File $CACERT not found."
  fi
fi

curl -L -sS $CACERT_ARG -o /usr/local/apache2/htdocs/known_exploited_vulnerabilities.json https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json

if [ -f /usr/local/apache2/htdocs/known_exploited_vulnerabilities.json ]; then
  timestamp=$(stat -c "%Y" "/usr/local/apache2/htdocs/known_exploited_vulnerabilities.json" | xargs -I{} date -d @{} "+%Y-%m-%d %H:%M:%S")
  echo "<li><a href=\"known_exploited_vulnerabilities.json\">known_exploited_vulnerabilities.json</a>	(Last Modified: $timestamp)</li>" > /usr/local/apache2/htdocs/kev.shtml
fi

rm -f $LOCKFILE