From 825d5de03f3a526d1a8d8fea950c520e72b38290 Mon Sep 17 00:00:00 2001
From: Lawrence Reinert lareine <lareine@hirs-hzbook-rh-01.evoforge.org>
Date: Wed, 4 Oct 2023 13:28:48 -0400
Subject: [PATCH] Added check for existing aca.properties in the ACA setup
 files

---
 package/scripts/db/db_create.sh  | 13 ++++++++++++-
 package/scripts/pki/pki_setup.sh | 19 ++++++++++++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/package/scripts/db/db_create.sh b/package/scripts/db/db_create.sh
index e723ec9f..3c67601d 100755
--- a/package/scripts/db/db_create.sh
+++ b/package/scripts/db/db_create.sh
@@ -41,8 +41,19 @@ source $SCRIPT_DIR/mysql_util.sh
 source $ACA_PROP_FILE 
 
 check_mysql_root_pwd () {
-  # Check if DB root password needs to be obtained
  
+  # 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
+        echo "Found existing aca.properties, using existing variables..."
+        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
+
   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"
diff --git a/package/scripts/pki/pki_setup.sh b/package/scripts/pki/pki_setup.sh
index 5a28142b..5ad18e68 100755
--- a/package/scripts/pki/pki_setup.sh
+++ b/package/scripts/pki/pki_setup.sh
@@ -21,15 +21,24 @@ SCRIPT_DIR=$( dirname -- "$( readlink -f -- "$0"; )"; )
 mkdir -p $HIRS_CONF_DIR $LOG_DIR
 echo "SCRIPT_DIR is $SCRIPT_DIR" | tee -a "$LOG_FILE"
 
-if [ -z "$1" ]; then
+if [ -z "$LOG_FILE" ]; then
    LOG_FILE="$LOG_DIR$LOG_FILE_NAME"
    echo "using log file $LOG_FILE" | tee -a "$LOG_FILE"
 fi
 
-if [ -z "$2" ]; then
-   PKI_PASS=$(head -c 64 /dev/urandom | md5sum | tr -dc 'a-zA-Z0-9')
-   echo "Using randomly generated password for the PKI key password" | tee -a "$LOG_FILE"
-   echo "Using pki password=$PKI_PASS"
+if [ -z "$PKI_PASS" ]; then
+   if [ -f $ACA_PROP ]; then
+      source $ACA_PROP
+      if [ ! -z $hirs_pki_password ]; then
+           PKI_PASS=$hirs_pki_password
+      fi
+   fi
+fi
+
+if [ -z "$PKI_PASS" ]; then
+    PKI_PASS=$(head -c 64 /dev/urandom | md5sum | tr -dc 'a-zA-Z0-9')
+    echo "Using randomly generated password for the PKI key password" | tee -a "$LOG_FILE"
+    echo "Using pki password=$PKI_PASS"
 fi
 
 # Check for sudo or root user