2018-09-06 13:47:33 +00:00
|
|
|
#!/bin/bash
|
2023-04-27 19:51:22 +00:00
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
# HIRS DB creation
|
|
|
|
# Environment variables used:
|
2023-05-03 16:54:35 +00:00
|
|
|
# a. HIRS_MYSQL_ROOT_PWD: Set this variable if mysql root password is already set
|
|
|
|
# b. HIRS_DB_PWD: Set the pwd if default password to hirs_db user needs to be changed
|
2023-04-27 19:51:22 +00:00
|
|
|
################################################################################
|
|
|
|
|
2023-08-11 13:47:30 +00:00
|
|
|
LOG_FILE=$1
|
2023-10-13 15:42:54 +00:00
|
|
|
DB_LOG_FILE="/var/log/mariadb/mariadb.log"
|
2023-09-27 18:40:18 +00:00
|
|
|
PKI_PASS=$2
|
|
|
|
UNATTENDED=$3
|
|
|
|
RSA_PATH=rsa_3k_sha384_certs
|
|
|
|
ECC_PATH=ecc_512_sha384_certs
|
2023-06-02 19:23:55 +00:00
|
|
|
# Capture location of the script to allow from invocation from any location
|
|
|
|
SCRIPT_DIR=$( dirname -- "$( readlink -f -- "$0"; )"; )
|
2023-08-11 13:47:30 +00:00
|
|
|
SPRING_PROP_FILE="/etc/hirs/aca/application.properties"
|
|
|
|
ACA_PROP_FILE="/etc/hirs/aca/aca.properties"
|
|
|
|
DB_ADMIN_PWD=""
|
2023-10-05 20:05:21 +00:00
|
|
|
# Db Configuration fileis, use RHELpaths as default
|
2023-08-11 13:47:30 +00:00
|
|
|
DB_SRV_CONF="/etc/my.cnf.d/mariadb-server.cnf"
|
|
|
|
DB_CLIENT_CONF="/etc/my.cnf.d/client.cnf"
|
|
|
|
# Default Server Side Certificates
|
|
|
|
SSL_DB_SRV_CHAIN="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_rsa_3k_sha384_Cert_Chain.pem";
|
|
|
|
SSL_DB_SRV_CERT="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_db_srv_rsa_3k_sha384.pem";
|
|
|
|
SSL_DB_SRV_KEY="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_db_srv_rsa_3k_sha384.key";
|
|
|
|
# Default Client Side Certificates
|
|
|
|
SSL_DB_CLIENT_CHAIN="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_rsa_3k_sha384_Cert_Chain.pem";
|
|
|
|
SSL_DB_CLIENT_CERT="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_db_client_rsa_3k_sha384.pem";
|
|
|
|
SSL_DB_CLIENT_KEY="/etc/hirs/certificates/HIRS/rsa_3k_sha384_certs/HIRS_db_client_rsa_3k_sha384.key";
|
|
|
|
|
|
|
|
# Make sure required paths exist
|
|
|
|
mkdir -p /etc/hirs/aca/
|
|
|
|
mkdir -p /var/log/hirs/
|
|
|
|
|
2024-03-13 17:49:42 +00:00
|
|
|
source $ACA_PROP_FILE
|
2023-09-18 20:48:29 +00:00
|
|
|
source $SCRIPT_DIR/mysql_util.sh
|
2023-10-05 20:05:21 +00:00
|
|
|
source /etc/os-release
|
|
|
|
|
|
|
|
# Setup distro specifc paths and variables
|
|
|
|
if [ $ID = "ubuntu" ]; then
|
|
|
|
DB_SRV_CONF="/etc/mysql/mariadb.conf.d/50-server.cnf"
|
|
|
|
DB_CLIENT_CONF="/etc/mysql/mariadb.conf.d/50-client.cnf"
|
2023-10-13 15:42:54 +00:00
|
|
|
mkdir -p /var/log/mariadb >> /dev/null
|
2023-10-11 19:56:36 +00:00
|
|
|
if [[ $(cat "$DB_SRV_CONF" | grep -c "log-error") < 1 ]]; then
|
2023-10-13 15:42:54 +00:00
|
|
|
echo "log_error=/var/log/mariadb/mariadb.log" >> $DB_SRV_CONF
|
|
|
|
echo "tls_version = TLSv1.2,TLSv1.3" >> $DB_SRV_CONF
|
2023-10-11 19:56:36 +00:00
|
|
|
fi
|
2023-10-05 20:05:21 +00:00
|
|
|
fi
|
2023-05-19 20:35:46 +00:00
|
|
|
|
2023-10-13 15:42:54 +00:00
|
|
|
touch $ACA_PROP_FILE
|
|
|
|
touch $LOG_FILE
|
|
|
|
touch $DB_SRV_CONF
|
|
|
|
touch $DB_LOG_FILE
|
2023-05-19 20:35:46 +00:00
|
|
|
|
2023-08-11 13:47:30 +00:00
|
|
|
check_mysql_root_pwd () {
|
2023-08-31 20:29:40 +00:00
|
|
|
|
2023-10-04 17:28:48 +00:00
|
|
|
# Check if DB root password needs to be obtained via env variable or existing property file
|
|
|
|
if [ -z "$HIRS_MYSQL_ROOT_PWD" ]; then
|
|
|
|
# Check if property file exists and look for properties
|
|
|
|
if [ -f $ACA_PROP_FILE ]; then
|
|
|
|
source $ACA_PROP_FILE
|
|
|
|
if [ ! -z $hirs_pki_password ]; then PKI_PASS=$hirs_pki_password; fi
|
|
|
|
if [ ! -z $mysql_admin_password ]; then HIRS_MYSQL_ROOT_PWD=$mysql_admin_password; fi
|
|
|
|
if [ ! -z $hirs_db_password ]; then HIRS_DB_PWD=$hirs_db_password; fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-08-11 13:47:30 +00:00
|
|
|
if [ -z $HIRS_MYSQL_ROOT_PWD ]; then
|
|
|
|
# Create a 32 character random password
|
|
|
|
echo "Using randomly generated password for the DB admin" | tee -a "$LOG_FILE"
|
|
|
|
DB_ADMIN_PWD=$(head -c 64 /dev/urandom | md5sum | tr -dc 'a-zA-Z0-9')
|
2023-08-23 20:30:06 +00:00
|
|
|
echo "DB Admin will be set to $DB_ADMIN_PWD , please make note for next mysql use."
|
2023-09-01 15:15:49 +00:00
|
|
|
# Check UNATTENDED flag set m if not then prompt user for permission ot store mysql root password
|
2024-02-23 20:19:02 +00:00
|
|
|
if [ -z $UNATTENDED ]; then
|
2023-08-31 20:29:40 +00:00
|
|
|
read -p "Do you wish to save this password to the aca.properties file? " confirm
|
|
|
|
if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
|
|
|
|
echo "mysql_admin_password=$DB_ADMIN_PWD" >> $ACA_PROP_FILE
|
|
|
|
echo "Mysql root password saved locally"
|
|
|
|
else
|
|
|
|
echo "Mysql root password not saved locally"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "mysql_admin_password=$DB_ADMIN_PWD" >> $ACA_PROP_FILE
|
|
|
|
echo "Mysql root password has been saved locally."
|
2023-08-23 20:30:06 +00:00
|
|
|
fi
|
2023-08-11 13:47:30 +00:00
|
|
|
mysqladmin --user=root password "$DB_ADMIN_PWD"
|
|
|
|
else
|
|
|
|
DB_ADMIN_PWD=$HIRS_MYSQL_ROOT_PWD
|
|
|
|
echo "Using system variable supplied password" | tee -a "$LOG_FILE"
|
|
|
|
fi
|
2024-03-13 17:49:42 +00:00
|
|
|
|
2023-08-11 13:47:30 +00:00
|
|
|
# Make sure root password is correct
|
|
|
|
$(mysql -u root -p$DB_ADMIN_PWD -e 'quit' &> /dev/null);
|
|
|
|
if [ $? -eq 0 ]; then
|
2023-09-18 20:48:29 +00:00
|
|
|
echo "Mysql root password verified" | tee -a "$LOG_FILE"
|
2023-08-11 13:47:30 +00:00
|
|
|
else
|
|
|
|
echo "MYSQL root password was not the default, not supplied, or was incorrect"
|
|
|
|
echo " please set the HIRS_MYSQL_ROOT_PWD system variable and retry."
|
|
|
|
echo " ********** ACA Mysql setup aborted ********" ;
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
}
|
2023-06-02 19:23:55 +00:00
|
|
|
|
2023-08-11 13:47:30 +00:00
|
|
|
set_mysql_server_tls () {
|
2023-10-05 20:05:21 +00:00
|
|
|
# Check DB server setup. If HIRS ssl params dont exist then we need to add them.
|
|
|
|
if [[ $(cat "$DB_SRV_CONF" | grep -c "HIRS") < 1 ]]; then
|
2023-08-11 13:47:30 +00:00
|
|
|
# Add TLS files to my.cnf
|
|
|
|
echo "Updating $DB_SRV_CONF with ssl parameters..." | tee -a "$LOG_FILE"
|
|
|
|
echo "ssl_ca=$SSL_DB_SRV_CHAIN" >> "$DB_SRV_CONF"
|
|
|
|
echo "ssl_cert=$SSL_DB_SRV_CERT" >> "$DB_SRV_CONF"
|
|
|
|
echo "ssl_key=$SSL_DB_SRV_KEY" >> "$DB_SRV_CONF"
|
2024-03-28 20:39:20 +00:00
|
|
|
# The following arent avialble in Mariadb 10.3
|
|
|
|
#echo "tls_version=TLSv1.2,TLSv1.3" >> "$DB_SRV_CONF"
|
|
|
|
#echo "require_secure_transport=ON" >> "$DB_SRV_CONF"
|
2024-04-24 20:18:31 +00:00
|
|
|
|
2023-08-18 16:38:41 +00:00
|
|
|
# Make sure mysql can access them
|
2023-08-28 20:18:08 +00:00
|
|
|
chown mysql:mysql $SSL_DB_SRV_CHAIN $SSL_DB_SRV_CERT $SSL_DB_SRV_KEY
|
2024-04-24 20:18:31 +00:00
|
|
|
chmod 644 $DB_SRV_CONF $DB_CLIENT_CONF
|
2023-08-31 13:27:58 +00:00
|
|
|
# Make selinux contexts for config files, if selinux is enabled
|
2024-02-28 13:53:49 +00:00
|
|
|
if [[ $ID = "rhel" ]] || [[ $ID = "rocky" ]] ||[[ $ID = "fedora" ]]; then
|
2024-03-13 17:49:42 +00:00
|
|
|
command -v selinuxenabled > /dev/null
|
2023-10-10 18:29:37 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
2024-03-13 17:49:42 +00:00
|
|
|
selinuxenabled
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
#semanage fcontext -a -t mysqld_etc_t $DB_SRV_CONF > /dev/null #adds the context type to file
|
|
|
|
restorecon -v -F $DB_SRV_CONF > /dev/null # changes the file's context type
|
|
|
|
fi
|
|
|
|
fi
|
2023-08-31 13:27:58 +00:00
|
|
|
fi
|
2023-08-11 13:47:30 +00:00
|
|
|
else
|
|
|
|
echo "mysql.cnf contians existing entry for ssl, skipping..." | tee -a "$LOG_FILE"
|
2023-08-18 16:38:41 +00:00
|
|
|
fi
|
2023-08-11 13:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_mysql_client_tls () {
|
|
|
|
# Update ACA property file with client cert info, if not there already
|
2023-10-05 20:05:21 +00:00
|
|
|
if [[ $(cat "$DB_CLIENT_CONF" | grep -c "HIRS") < 1 ]]; then
|
2023-08-11 13:47:30 +00:00
|
|
|
echo "Updating $DB_CLIENT_CONF with ssl parameters..." | tee -a "$LOG_FILE"
|
|
|
|
echo "ssl_ca=$SSL_DB_CLIENT_CHAIN" >> $DB_CLIENT_CONF
|
|
|
|
echo "ssl_cert=$SSL_DB_CLIENT_CERT" >> $DB_CLIENT_CONF
|
|
|
|
echo "ssl_key=$SSL_DB_CLIENT_KEY" >> $DB_CLIENT_CONF
|
2023-08-28 20:18:08 +00:00
|
|
|
chown mysql:mysql $SSL_DB_CLIENT_CHAIN $SSL_DB_CLIENT_CERT $SSL_DB_CLIENT_KEY
|
2023-08-31 13:27:58 +00:00
|
|
|
# Make selinux contexts for config files, if selinux is enabled
|
2024-02-28 13:53:49 +00:00
|
|
|
if [[ $ID = "rhel" ]] || [[ $ID = "rocky" ]] ||[[ $ID = "fedora" ]]; then
|
2024-03-13 17:49:42 +00:00
|
|
|
command -v selinuxenabled > /dev/null
|
2023-10-10 18:29:37 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
2024-03-13 17:49:42 +00:00
|
|
|
selinuxenabled
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
#semanage fcontext -a -t mysqld_etc_t $DB_CLIENT_CONF > /dev/null #adds the context type to file
|
2023-10-10 18:29:37 +00:00
|
|
|
restorecon -F $DB_CLIENT_CONF > /dev/null #changes the file's context type
|
2024-03-13 17:49:42 +00:00
|
|
|
fi
|
2023-10-10 18:29:37 +00:00
|
|
|
fi
|
2024-03-13 17:49:42 +00:00
|
|
|
fi
|
2023-05-19 20:35:46 +00:00
|
|
|
fi
|
2023-08-11 13:47:30 +00:00
|
|
|
}
|
2023-05-19 20:35:46 +00:00
|
|
|
|
2023-08-23 20:30:06 +00:00
|
|
|
# Process HIRS DB USER
|
2023-08-11 13:47:30 +00:00
|
|
|
set_hirs_db_pwd () {
|
2024-04-24 19:40:46 +00:00
|
|
|
check_hirs_db
|
|
|
|
if [[ $HIRS_DB_USER_EXISTS != "1" ]]; then
|
2023-08-23 20:30:06 +00:00
|
|
|
# Check if Mysql HIRS DB password set by system variable or set to random number
|
|
|
|
if [ -z $HIRS_DB_PWD ]; then
|
|
|
|
HIRS_DB_PWD=$(head -c 64 /dev/urandom | md5sum | tr -dc 'a-zA-Z0-9')
|
|
|
|
fi
|
2024-02-21 18:16:13 +00:00
|
|
|
# Add key/values only if they dont exist
|
2024-02-23 20:19:02 +00:00
|
|
|
if [[ $(grep -c "hirs_db_username" $ACA_PROP_FILE) -eq 0 ]]; then
|
2024-02-21 18:16:13 +00:00
|
|
|
echo "hirs_db_username=hirs_db" >> $ACA_PROP_FILE
|
|
|
|
fi
|
2024-02-23 20:19:02 +00:00
|
|
|
if [[ $(grep -c "hirs_db_password" $ACA_PROP_FILE) -eq 0 ]]; then
|
2024-02-21 18:16:13 +00:00
|
|
|
echo "hirs_db_password=$HIRS_DB_PWD" >> $ACA_PROP_FILE
|
|
|
|
fi
|
2024-02-23 20:19:02 +00:00
|
|
|
if [[ $(grep -c "hibernate.connection.username" $SPRING_PROP_FILE) -eq 0 ]]; then
|
2024-02-21 18:16:13 +00:00
|
|
|
echo "hibernate.connection.username=hirs_db" >> $SPRING_PROP_FILE
|
|
|
|
fi
|
2024-02-23 20:19:02 +00:00
|
|
|
if [[ $(grep -c "hibernate.connection.password" $SPRING_PROP_FILE) -eq 0 ]]; then
|
2024-02-21 18:16:13 +00:00
|
|
|
echo "hibernate.connection.password=$HIRS_DB_PWD" >> $SPRING_PROP_FILE
|
|
|
|
fi
|
2024-04-24 19:40:46 +00:00
|
|
|
else
|
|
|
|
echo "hirs-db user already exists, skipping"
|
|
|
|
fi
|
2023-08-11 13:47:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 20:30:06 +00:00
|
|
|
# Create a hirs_db with client side TLS enabled
|
2023-08-11 13:47:30 +00:00
|
|
|
create_hirs_db_with_tls () {
|
2024-04-24 19:40:46 +00:00
|
|
|
check_hirs_db_user
|
|
|
|
echo "Now HIRS_DB_USER_EXISTS is $HIRS_DB_USER_EXISTS"
|
|
|
|
if [[ $HIRS_DB_USER_EXISTS == "1" ]]; then
|
|
|
|
echo "hirs_db already exists, skipping"
|
|
|
|
else
|
|
|
|
# Check if hirs_db not created and create it if it wasn't
|
|
|
|
mysqlshow --user=root --password="$DB_ADMIN_PWD" | grep "hirs_db" >> $LOG_FILE 2>&1
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "hirs_db exists, skipping hirs_db create"
|
|
|
|
else
|
|
|
|
mysql -u root --password=$DB_ADMIN_PWD < $MYSQL_DIR/db_create.sql
|
|
|
|
mysql -u root --password=$DB_ADMIN_PWD < $MYSQL_DIR/secure_mysql.sql
|
|
|
|
mysql -u root --password=$DB_ADMIN_PWD -e "SET PASSWORD FOR 'hirs_db'@'localhost' = PASSWORD('"$HIRS_DB_PWD"'); FLUSH PRIVILEGES;";
|
|
|
|
echo "**** Setting hirs_db pwd to $HIRS_DB_PWD ***"
|
|
|
|
fi
|
|
|
|
fi
|
2023-08-31 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 18:40:18 +00:00
|
|
|
# Create a JDBC connector used by hibernate and place in Springs application.properties
|
|
|
|
create_hibernate_url () {
|
|
|
|
ALG=$1
|
|
|
|
db_username=$2
|
|
|
|
|
|
|
|
if [ $ALG = "RSA" ]; then
|
|
|
|
CERT_PATH="/etc/hirs/certificates/HIRS/$RSA_PATH"
|
|
|
|
CERT_CHAIN="$CERT_PATH/HIRS_rsa_3k_sha384_Cert_Chain.pem"
|
|
|
|
CLIENT_DB_P12=$CERT_PATH/HIRS_db_client_rsa_3k_sha384.p12
|
|
|
|
ALIAS="hirs_aca_tls_rsa_3k_sha384"
|
|
|
|
else
|
|
|
|
CERT_PATH="/etc/hirs/certificates/HIRS/$ECC_PATH"
|
|
|
|
CERT_CHAIN="$CERT_PATH/HIRS_ecc_512_sha384_Cert_Chain.pem"
|
|
|
|
CLIENT_DB_P12=$CERT_PATH/HIRS_db_client_ecc_512_sha384.p12
|
|
|
|
ALIAS="hirs_aca_tls_ecc_512_sha384"
|
|
|
|
fi
|
|
|
|
|
|
|
|
CONNECTOR_URL="hibernate.connection.url=jdbc:mariadb://localhost:3306/hirs_db?autoReconnect=true&\
|
|
|
|
user=$db_username&\
|
|
|
|
password=$HIRS_DB_PWD&\
|
|
|
|
sslMode=VERIFY_CA&\
|
|
|
|
serverSslCert=$CERT_CHAIN&\
|
|
|
|
keyStoreType=PKCS12&\
|
|
|
|
keyStorePassword=$PKI_PASS&\
|
|
|
|
keyStore="$CLIENT_DB_P12" "
|
|
|
|
|
2024-02-23 20:19:02 +00:00
|
|
|
if [[ $(grep -c "hibernate.connection.url" $SPRING_PROP_FILE) -eq 0 ]]; then
|
2024-02-21 18:16:13 +00:00
|
|
|
echo $CONNECTOR_URL >> $SPRING_PROP_FILE
|
|
|
|
fi
|
2023-09-27 18:40:18 +00:00
|
|
|
|
|
|
|
}
|
2023-08-11 13:47:30 +00:00
|
|
|
# HIRS ACA Mysqld processing ...
|
2024-03-13 17:49:42 +00:00
|
|
|
check_systemd -p
|
2023-08-23 20:30:06 +00:00
|
|
|
check_mariadb_install
|
2023-08-11 13:47:30 +00:00
|
|
|
start_mysqlsd
|
2023-10-10 18:29:37 +00:00
|
|
|
check_mysql
|
2023-08-11 13:47:30 +00:00
|
|
|
check_mysql_root_pwd
|
2023-08-23 20:30:06 +00:00
|
|
|
set_hirs_db_pwd
|
2024-04-24 19:40:46 +00:00
|
|
|
create_hirs_db_with_tls
|
2024-03-13 17:49:42 +00:00
|
|
|
set_mysql_server_tls
|
|
|
|
set_mysql_client_tls
|
2023-09-27 18:40:18 +00:00
|
|
|
create_hibernate_url "RSA" "hirs_db"
|
2023-08-31 20:29:40 +00:00
|
|
|
mysqld_reboot
|