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

set -e

echo "Updating..."

LOCKFILE=/tmp/vulzn.lock

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

DELAY_ARG=""
if [ -z $NVD_API_KEY ]; then
  DELAY_ARG="--delay=10000"
else
  echo "Using NVD API KEY: ${NVD_API_KEY:0:5}****"
fi

if [ -n "${DELAY}" ]; then
  echo "Overriding delay with ${DELAY}ms"
  DELAY_ARG="--delay=$DELAY"
fi

MAX_RETRY_ARG=""
if [ -n "${MAX_RETRY}" ]; then
  echo "Using max retry attempts: $MAX_RETRY"
  MAX_RETRY_ARG="--maxRetry=$MAX_RETRY"
fi

MAX_RECORDS_PER_PAGE_ARG=""
if [ -n "${MAX_RECORDS_PER_PAGE}" ]; then
  echo "Using max records per page: $MAX_RECORDS_PER_PAGE"
  MAX_RECORDS_PER_PAGE_ARG="--recordsPerPage=$MAX_RECORDS_PER_PAGE"
fi

FORCE_UPDATE_ARG=""
if [ -n "${FORCE_UPDATE}" ]; then
  echo "Forcing full update of years"
  FORCE_UPDATE_ARG="--forceUpdate"
fi

DEBUG_ARG=""
if [ -n "${DEBUG}" ]; then
  echo "Enabling debug mode"
  DEBUG_ARG="--debug"
fi

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

attempt=1
max_attempts=5
if [ -n "${MAX_MIRROR_RETRIES}" ]; then
  echo "Going to try up to $MAX_MIRROR_RETRIES times on a mirror fail"
  max_attempts=$MAX_MIRROR_RETRIES
fi

set +e
while [ $attempt -le $max_attempts ]; do
  java $JAVA_OPT -jar /usr/local/bin/vulnz cve $DELAY_ARG $DEBUG_ARG $MAX_RETRY_ARG $MAX_RECORDS_PER_PAGE_ARG $FORCE_UPDATE_ARG --cache --directory /usr/local/apache2/htdocs
  exit_code=$?
  # 100: indicates an issue downloading the full CVE dataset from the NVD API; we will retry the update immediately to utilize the forced persistent HTTP Response Cache.
  if [ $exit_code -ne 100 ]; then
    break
  fi
  echo "Got exit code $exit_code, attempt $attempt of $max_attempts"
  attempt=$((attempt + 1))

  # wait for a factor of 1 minute before retrying the mirror process. This might dodge short outages without retrying to fast
  retryDebounce=$((60*$attempt))
  echo "waiting for $retryDebounce seconds before retrying"
  sleep "${retryDebounce}s"
done
set -e

# create the directory listing for the SSI
if [ -f /usr/local/apache2/htdocs/cache.properties ]; then
  timestamp=$(stat -c "%Y" "/usr/local/apache2/htdocs/cache.properties" | xargs -I{} date -d @{} "+%Y-%m-%d %H:%M:%S")
  echo "<li><a href=\"/cache.properties\">cache.properties</a>	(Last Modified: $timestamp)</li>" > /usr/local/apache2/htdocs/nvd.shtml
  echo "<li><a href=\"/cve-count-per-year.json\">cve-count-per-year.json</a>	(Last Modified: $timestamp)</li>" >> /usr/local/apache2/htdocs/nvd.shtml
  for file in /usr/local/apache2/htdocs/nvdcve*; do
    filename=$(basename "$file")
    timestamp=$(stat -c "%Y" "$file" | xargs -I{} date -d @{} "+%Y-%m-%d %H:%M:%S")
    echo "<li><a href=\"$filename\">$filename</a>	(Last Modified: $timestamp)</li>" >> /usr/local/apache2/htdocs/nvd.shtml
  done
fi

rm -f $LOCKFILE