diff --git a/lib/config_tools.sh b/lib/config_tools.sh index 2982e44..16fbb81 100755 --- a/lib/config_tools.sh +++ b/lib/config_tools.sh @@ -19,7 +19,7 @@ ####################################################################### # Create a default config file. # -# Usage: create_config CONFIG_PATH +# Usage: create_config [-v] CONFIG_PATH # # The CONFIG_PATH argument is the absolute path to the config file. ####################################################################### @@ -28,7 +28,30 @@ create_config () { local config_file + # process command-line options (if any) + local OPTARG + local OPTIND + local opt + local verbose_mode=false + + while getopts ":v" opt; do + case $opt in + v) + verbose_mode=true + ;; + \?) + echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 + return 2 + ;; + :) + echo "ERROR: $FUNCNAME: Option -$OPTARG requires an argument" >&2 + return 2 + ;; + esac + done + # make sure there's at least one command-line argument + shift $(( OPTIND - 1 )) if [ $# -eq 0 ] ; then echo "ERROR: $FUNCNAME: no config file to create" >&2 return 2 @@ -41,6 +64,10 @@ create_config () { # MDQ base URL MDQ_BASE_URL=http://mdq-beta.incommon.org/global + # basic curl defaults + CONNECT_TIMEOUT_DEFAULT=2 + MAX_REDIRS_DEFAULT=3 + # default SAML2 endpoint for testing SAML2_SP_ENTITY_ID=https://service1.internet2.edu/shibboleth SAML2_SP_ACS_URL=https://service1.internet2.edu/Shibboleth.sso/SAML2/POST @@ -52,6 +79,8 @@ create_config () { SAML1_SP_ACS_BINDING=urn:oasis:names:tc:SAML:1.0:profiles:browser-post DEFAULT_CONFIG_FILE + $verbose_mode && echo "$FUNCNAME created default config file $config_file" + return 0 } @@ -63,29 +92,25 @@ DEFAULT_CONFIG_FILE # The CONFIG_PATH argument is the absolute path to the config file. # The -v option produces verbose output, which is most useful for # testing and debugging. -# -# If a required config parameter is missing, this function halts -# and returns a non-zero return code. ####################################################################### load_config () { local config_file local status_code - local param_name - local param_names - local param_value # process command-line options (if any) local OPTARG local OPTIND local opt local verbose_mode=false + local local_opts while getopts ":v" opt; do case $opt in v) verbose_mode=true + local_opts="-$opt" ;; \?) echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 @@ -99,7 +124,7 @@ load_config () { done # make sure there's at least one command-line argument - shift $(( OPTIND-1 )) + shift $(( OPTIND - 1 )) if [ $# -eq 0 ]; then echo "ERROR: $FUNCNAME: no config file to load" >&2 return 2 @@ -108,33 +133,83 @@ load_config () { # create config file if necessary if [ ! -f "$config_file" ]; then - create_config $config_file + $verbose_mode && echo "$FUNCNAME creating default config file $config_file" + create_config $local_opts $config_file status_code=$? if [ $status_code -ne 0 ]; then echo "ERROR: $FUNCNAME: failed to create config file $config_file" >&2 return $status_code fi - $verbose_mode && echo "$FUNCNAME creating default config file $config_file" fi # load config file + $verbose_mode && echo "$FUNCNAME sourcing config file $config_file" source "$config_file" status_code=$? if [ $status_code -ne 0 ]; then echo "ERROR: $FUNCNAME failed to source config file $config_file" >&2 return $status_code fi - $verbose_mode && echo "$FUNCNAME sourcing config file $config_file" + return 0 +} + +####################################################################### +# Validate a previously loaded config file. +# +# Usage: validate_config [-v] +# +# If a required config parameter is missing, this function halts +# and returns a non-zero return code. +####################################################################### + +validate_config () { + + local param_name + local param_names + local param_value + + # process command-line options (if any) + local OPTARG + local OPTIND + local opt + local verbose_mode=false + + while getopts ":v" opt; do + case $opt in + v) + verbose_mode=true + ;; + \?) + echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 + return 2 + ;; + :) + echo "ERROR: $FUNCNAME: Option -$OPTARG requires an argument" >&2 + return 2 + ;; + esac + done + + # required config parameters + param_names="MDQ_BASE_URL + CONNECT_TIMEOUT_DEFAULT + MAX_REDIRS_DEFAULT + SAML2_SP_ENTITY_ID + SAML2_SP_ACS_URL + SAML2_SP_ACS_BINDING + SAML1_SP_ENTITY_ID + SAML1_SP_ACS_URL + SAML1_SP_ACS_BINDING" + # check required config parameters - param_names="MDQ_BASE_URL SAML2_SP_ENTITY_ID SAML2_SP_ACS_URL SAML2_SP_ACS_BINDING SAML1_SP_ENTITY_ID SAML1_SP_ACS_URL SAML1_SP_ACS_BINDING" for param_name in $param_names; do eval "param_value=\${$param_name}" if [ ! "$param_value" ]; then - echo "ERROR: $FUNCNAME failed to find $param_name config parameter" >&2 + echo "ERROR: $FUNCNAME parameter $param_name undefined" >&2 return 3 fi - $verbose_mode && printf "$FUNCNAME using $param_name=%s\n" "$param_value" + $verbose_mode && printf "$FUNCNAME checking $param_name=%s\n" "$param_value" done return 0