Skip to content
Snippets Groups Projects
Commit e078010e authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add validation of ini files

parent b191c47d
Branches
No related tags found
1 merge request!129Db Profiles
......@@ -77,9 +77,12 @@ function dbdetect.exists(){
local _found=0
# show errors in profile ini files
ini.validate "$_config" "$( dirname $0)/includes/dbdetect_validate_profile_ini.sh" && dbdetect._wd "Ini validation OK"
# set file and inisection we read values from
ini.set "$_config" "detect"
# --- check tcp
local tcpport; tcpport=$( ini.value "tcp" )
if [ -n "$tcpport" ]; then
......@@ -104,18 +107,6 @@ function dbdetect.exists(){
dbdetect._wd "... tcp $tcpport is used by $tcpprocess."
fi
# --- check binaries
local binary; binary=$( ini.value "binary" )
if [ -n "${binary}" ]; then
for mybinary in $( echo "${binary}" | tr "," " " ); do
if ! which "$mybinary" >/dev/null 2>&1; then
dbdetect._wd "... Missing binary: ${mybinary}"
return 1
fi
dbdetect._wd "... Binary: ${mybinary} was found"
done
fi
# --- check process
local process; process=$( ini.value "process" )
if [ -n "${process}" ]; then
......@@ -150,6 +141,18 @@ function dbdetect.exists(){
fi
fi
# --- check binaries
local binary; binary=$( ini.value "binary" )
if [ -n "${binary}" ]; then
for mybinary in $( echo "${binary}" | tr "," " " ); do
if ! which "$mybinary" >/dev/null 2>&1; then
dbdetect._wd "... Missing binary: ${mybinary}"
return 1
fi
dbdetect._wd "... Binary: ${mybinary} was found"
done
fi
# --- OK, everything was found ... we initialize it
dbdetect._wd "OK, match: $_config"
......
# ----------------------------------------------------------------------
#
# VALIDATE PROFILE INI
#
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# section names
#
# SYNTAX:
# comma separated list of sections
# ----------------------------------------------------------------------
# sections that MUST be present
sectionsMust="detect,set"
# sections that CAN be present
sectionsCan=""
# ----------------------------------------------------------------------
# variables
#
# SYNTAX:
# - one line per ini entry.
# - <section>.<variable> OR <section>.<variable>:<regex-to-match>
# The regey is applied with grep -E parameter for extended regex
# and "^<regex-to-match>$" for complete value
# ----------------------------------------------------------------------
varsMust=""
varsCan="
detect.binary
detect.process
detect.tcp:[0-9]*
detect.tcp-target
detect.tcp-process
detect.type:sqlite
detect.file[]
set.dbuser
set.dbpassword
set.env
set.params
set.su
"
# ----------------------------------------------------------------------
......@@ -241,4 +241,90 @@ function ini.varexport(){
done
}
# validate the ini file
function ini.validate(){
local myinifile="$1"
local myvalidationfile="$2"
local bShowAll="${3:-0}"
local ERROR="\e[1;31mERROR\e[0m"
local iErr; typeset -i iErr=0
if [ ! -f "${myvalidationfile}" ]; then
echo -e "$ERROR: Validation file '${myvalidationfile}' does not exist."
return 1
fi
. "${myvalidationfile}" || return 1
if [ -n "$sectionsMust" ]; then
test $bShowAll -ne 0 && echo "--- Validate MUST sections $sectionsMust"
for section in $( tr "," " " <<< "$sectionsMust");
do
if ini.sections "$myinifile" | grep -q "^$section$" ; then
test $bShowAll -ne 0 && echo "OK: Section $section is present."
else
echo -e "$ERROR: Section $section is not present."
iErr+=1
fi
done
fi
test $bShowAll -ne 0 && echo "--- Validate section names"
for section in $( ini.sections "$myinifile" )
do
if ! grep -Fq ",${section}," <<< ",${sectionsMust},${sectionsCan},"; then
echo -e "$ERROR: unknown section name: [$section]"
iErr+=1
else
test $bShowAll -ne 0 && echo "OK: [$section] ... checking its keys..."
# TODO: verify values
for mustkey in $( echo "${varsMust}" | grep "^[/t ]*${section}\." | cut -f 2 -d '.' | cut -f 1 -d ':' ); do
if ini.keys "$myinifile" "$section" | grep "^${mustkey}$"; then
test $bShowAll -ne 0 && echo " OK: $section.$mustkey"
else
echo -e " $ERROR: A MUST key '$mustkey' was not found im section [$section]."
iErr+=1
fi
done
for mykey in $(ini.keys "$myinifile" "$section"); do
if ! echo "
${varsMust}
${varsCan}
" | cut -f 1 -d ':' | grep -q "^[/t ]*${section}\.${mykey}$"; then
echo -e " $ERROR: invald key name: $section.$mykey"
iErr+=1
else
check=$(echo "
${varsMust}
${varsCan}
" | grep "^[/t ]*${section}\.${mykey}[:$]" | cut -f 2 -d ':' )
if [ -n "$check" ]; then
value="$(ini.value "$myinifile" "$section" "$mykey")"
if ! grep -Eq "^${check}$" <<< "$value" ; then
echo -e " $ERROR: key name $section.$mykey is valid but value '$value' does NOT match '$check'"
else
test $bShowAll -ne 0 && echo " OK: key name $mykey is valid and value matches '$check'"
fi
else
test $bShowAll -ne 0 && echo " OK: key name $mykey is valid"
fi
fi
done
fi
done
if [ $iErr -gt 0 ]; then
echo "RESULT: Errors were found for $myinifile"
else
test $bShowAll -ne 0 && echo "RESULT: OK, Ini file $myinifile looks fine."
fi
return $iErr
}
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment