#!/usr/bin/env bash
#
# Generate the Pulp server RSA key pair.
# They are generated only when both do not already exist.
#

set -e

KEY_DIR="/etc/pki/pulp"
KEY_PATH="$KEY_DIR/rsa.key"
KEY_PATH_PUB="$KEY_DIR/rsa_pub.key"

if [ -f ${KEY_PATH} ] && [ -f ${KEY_PATH_PUB} ]
then
  echo "Both ${KEY_PATH} and ${KEY_PATH_PUB} already exist."
  echo "Nothing generated."
  exit 0
fi

umask 077

openssl genrsa -out ${KEY_PATH} 2048 &> /dev/null
openssl rsa -in ${KEY_PATH} -pubout > ${KEY_PATH_PUB} 2> /dev/null

chmod 640 ${KEY_PATH}
chmod 644 ${KEY_PATH_PUB}
chown root:apache ${KEY_PATH}
chown root:apache ${KEY_PATH_PUB}

ln -fs ${KEY_PATH_PUB} /var/lib/pulp/static

echo "Created: ${KEY_PATH} and ${KEY_PATH_PUB}"