From: Steven Baltakatei Sandoval Date: Sat, 11 Jul 2020 06:21:32 +0000 (+0000) Subject: style(bklog):Combine functions that parse '-r' and '-R' X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/commitdiff_plain/236c8b97f828fa0d58474ef028592ff0ff435790?hp=fcefae4cb5df370a237319fd549ad185fa31ed83 style(bklog):Combine functions that parse '-r' and '-R' Combine functions magicParseRecipientArgs() and magicParseRecipientDir() into magicParseRecipients(). Group showUsage() function with other script-specific functions. --- diff --git a/exec/bklog b/exec/bklog index 6e166f1..5ab7fb6 100644 --- a/exec/bklog +++ b/exec/bklog @@ -11,7 +11,7 @@ dirTmpDefault="/dev/shm"; # Default parent of working directory # Script Metadata scriptName="bklog"; # Define basename of script file. -scriptVersion="0.1.14"; # Define version of script. +scriptVersion="0.1.15"; # Define version of script. scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script. scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN scriptHostname=$(hostname); # Save hostname of system running this script. @@ -23,7 +23,6 @@ ageURL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define declare -a buffer # array for storing while read buffer declare -a argRecPubKeys # array for processArguments function declare -a recPubKeysValid # array for storing both '-r' and '-R' recipient pubkeys -declare -a recPubKeysValidStatic # for storing '-r' recipient pubkeys declare -a argProcStrings argProcFileExts # for storing buffer processing strings (ex: "gpsbabel -i nmea -f - -o gpx -F - ") declare -Ag appRollCall # Associative array for storing app status declare -Ag fileRollCall # Associative array for storing file status @@ -48,11 +47,11 @@ processArguments() { --version) showVersion; exit 1;; # Show version -o | --output) if [ -d "$2" ]; then dirOut="$2"; vbm "DEBUG :dirOut:$dirOut"; shift; fi ;; # Define output directory. -e | --encrypt) optionEncrypt="true"; vbm "DEBUG :Encrypted output mode enabled.";; # Enable encryption - -r | --recipient) optionRecipients="true"; argRecPubKeys+=("$2"); vbm "STATUS:pubkey added:""$2"; shift;; # Add recipients + -r | --recipient) optionRecArg="true"; argRecPubKeys+=("$2"); vbm "STATUS:pubkey added:""$2"; shift;; # Add recipients + -R | --recipient-dir) optionRecDir="true" && argRecDir="$2"; shift;; # Add recipient watch dir -c | --compress) optionCompress="true"; vbm "DEBUG :Compressed output mode enabled.";; # Enable compression -z | --time-zone) try setTimeZoneEV "$2"; shift;; # Set timestamp timezone -t | --temp-dir) optionTmpDir="true" && argTempDirPriority="$2"; shift;; # Set time zone - -R | --recipient-dir) optionRecipients="true"; optionRecDir="true" && argRecDir="$2"; shift;; # Add recipient watch dir -b | --buffer-ttl) optionCustomBufferTTL="true" && argCustomBufferTTL="$2"; shift;; # Set custom buffer period (default: 300 seconds) -B | --script-ttl) optionCustomScriptTTL_TE="true" && argCustomScriptTTL_TE="$2"; shift;; # Set custom script TTL (default: "day") -p | --process-string) optionProcString="true" && argProcStrings+=("$2") && argProcFileExts+=("$3") && vbm "STATUS:file extension \"$2\" for output of processing string added:\"$3\""; shift; shift;; @@ -427,79 +426,6 @@ setTimeZoneEV(){ return 0; fi } # Exports TZ environment variable -showUsage() { - cat <<'EOF' - USAGE: - cmd | bklog [ options ] - - OPTIONS: - -h, --help - Display help information. - --version - Display script version. - -v, --verbose - Display debugging info. - -e, --encrypt - Encrypt output. - -r, --recipient [ string pubkey ] - Specify recipient. May be age or ssh pubkey. - May be specified multiple times for multiple pubkeys. - See https://github.com/FiloSottile/age - -o, --output [ path dir ] - Specify output directory to save logs. This option is required - to save log data. - -p, --process-string [ filter command ] [ output file extension] - Specify how to create and name a processed version of the stdin. - For example, if stdin is 'nmea' location data: - - -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" - - This option would cause the stdin to 'bklog' to be piped into - the 'gpsbabel' command, interpreted as 'nmea' data, converted - into 'gpx' format, and then appended to the output tar file - as a file with a '.gpx' extension. - This option may be specified multiple times in order to output - results of multiple different processing methods. - -l, --label [ string ] - Specify a label to be included in all output file names. - Ex: 'location' if stdin is location data. - -w, --store-raw [ file extension ] - Specify file extension of file within output tar that contains - raw stdin data. The default behavior is to always save raw stdin - data in a '.stdin' file. Example usage when 'bklog' receives - 'nmea' data from 'gpspipe -r': - - -w ".nmea" - - Stdin data is saved in a '.nmea' file within the output tar. - -W, --no-store-raw - Do not store raw stdin in output tar. - -c, --compress - Compress output with gzip (before encryption if enabled). - -z, --time-zone - Specify time zone. (ex: "America/New_York") - -t, --temp-dir [path dir] - Specify parent directory for temporary working directory. - Default: "/dev/shm" - -R, --recipient-dir [path dir] - Specify directory containing files whose first lines are - to be interpreted as pubkey strings (see '-r' option). - -b, --buffer-ttl [integer] - Specify custom buffer period in seconds (default: 300 seconds) - -B, --script-ttl [time element string] - Specify custom script time-to-live in seconds (default: "day") - Valid values: "day", "hour" - - EXAMPLE: (bash script lines) - $ gpspipe -r | /bin/bash bklog -v -e -c -z "UTC" -t "/dev/shm" \ - -r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \ - -r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \ - -R ~/.config/bklog/recipients -w ".nmea" -b 300 -B "day" \ - -o ~/Sync/Logs -l "location" \ - -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \ - -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" -EOF -} # Display information on how to use this script. showVersion() { yell "$scriptVersion" } # Display script version. @@ -1036,109 +962,104 @@ magicParseProcessStrings() { procFileExts=("${argProcFileExts[@]}"); # Export process command strings vbm "STATUS:$fn:Finished magicParseProcessStrings() function."; } # Validate and save process strings and file extensions to arrays procStrings, procFileExts -magicParseRecipientArgs() { - # Desc: Parses recipient arguments specified by '-r' option - # Input: vars: optionEncrypt, optionRecipients - # arry: argRecPubKeys from processArguments() - # Output: vars: cmd_encrypt, cmd_encrypt_suffix - # arry: recPubKeysValid, recPubKeysValidStatic - # Depends: processArguments(), yell(), vbm(), checkapp(), checkAgePubkey(), validateInput() - local fn recipients +magicParseRecipients() { + # Desc: Parses recipient arguments specified by '-r' or '-R' options + # Usage: magicParseRecipients + # In : vars: optionEncrypt, optionRecArg, optionRecDir + # arry: argRecPubKeys (-r), argRecDir (-R) + # Out: vars: cmd_encrypt, cmd_encrypt_suffix + # Depends: head 8.30, checkapp(), checkAgePubkey(), validateInput() + local fn recipients recipientDir recFileLine updateRecipients + local -a recPubKeysValid candRecPubKeysValid # Save function name fn="${FUNCNAME[0]}"; + vbm "STATUS:$fn:Starting magicParseRecipients() function."; - vbm "STATUS:$fn:Starting magicParseRecipientArgs() function."; - # Check if encryption option active. - if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then - if checkapp age; then # Check that age is available. - for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message - vbm "DEBUG :$fn:Testing pubkey string:$pubkey"; - if checkAgePubkey "$pubkey" && \ - ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then - #### Form age recipient string - recipients="$recipients""-r '$pubkey' "; - vbm "STATUS:$fn:Added pubkey for forming age recipient string:""$pubkey"; - vbm "DEBUG :$fn:recipients:""$recipients"; - #### Add validated pubkey to recPubKeysValid array - recPubKeysValid+=("$pubkey") && vbm "DEBUG :$fn:recPubkeysValid:pubkey added:$pubkey"; - else - yell "ERROR:$fn:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; - fi; - done - vbm "DEBUG :$fn:Finished processing argRecPubKeys array"; - vbm "STATUS:$fn:Array of validated pubkeys:${recPubKeysValid[*]}"; - recPubKeysValidStatic=("${recPubKeysValid[@]}"); # Save static image of pubkeys validated by this function - - ## Form age command string - cmd_encrypt="age ""$recipients " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; - cmd_encrypt_suffix=".age" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; - else - yell "ERROR:$fn:Encryption enabled but \"age\" not found. Exiting."; exit 1; - fi; - else - cmd_encrypt="tee /dev/null " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; - cmd_encrypt_suffix="" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; - vbm "DEBUG :$fn:Encryption not enabled." - fi; - # Catch case if '-e' is set but '-r' or '-R' is not - if [[ "$optionEncrypt" = "true" ]] && [[ ! "$optionRecipients" = "true" ]]; then + # Catch illegal option combinations + ## Catch case if '-e' is set but neither '-r' nor '-R' is set + if [[ "$optionEncrypt" = "true" ]] && \ + ! { [[ "$optionRecArg" = "true" ]] || [[ "$optionRecDir" = "true" ]]; }; then yell "ERROR:$fn:\\'-e\\' set but no \\'-r\\' or \\'-R\\' set."; exit 1; fi; - # Catch case if '-r' or '-R' set but '-e' is not - if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then + ## Catch case if '-r' or '-R' set but '-e' is not + if [[ ! "$optionEncrypt" = "true" ]] && \ + { [[ "$optionRecArg" = "true" ]] || [[ "$optionRecDir" = "true" ]]; }; then yell "ERROR:$fn:\\'-r\\' or \\'-R\\' set but \\'-e\\' is not set."; exit 1; fi; - vbm "STATUS:$fn:Finished magicParseRecipientArgs() function."; -} # Populate recPubKeysValid with argRecPubKeys; form encryption cmd string and filename suffix -magicParseRecipientDir() { - # Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory") - # Inputs: vars: optionEncrypt, optionRecDir, argRecDir, - # arry: recPubKeysValid - # Outputs: arry: recPubKeysValid - # Depends: processArguments(), yell(), vbm(), validateInput(), checkAgePubkey() - local fn recipientDir recFileLine updateRecipients - declare -a candRecPubKeysValid - - # Save function name - fn="${FUNCNAME[0]}"; - vbm "STATUS:$fn:Starting magicParseRecipientDir() function."; - # Check that '-e' and '-R' set - if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then - ### Check that argRecDir is a directory. + # Handle no encryption cases + if [[ ! "$optionEncrypt" = "true" ]]; then + cmd_encrypt="cat " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; + cmd_encrypt_suffix="" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; + vbm "DEBUG :$fn:Encryption not enabled."; + return; fi; + + # Handle encryption cases + ## Check age availability + if ! checkapp age; then yell "ERROR:$fn:age not available. Exiting."; exit 1; fi + ## Parse '-r' options: validate and append pubkeys from argRecPubKeys to recPubKeysValid + if [[ "$optionRecArg" = "true" ]]; then + for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message + vbm "DEBUG :$fn:Testing pubkey string:$pubkey"; + if checkAgePubkey "$pubkey" && \ + ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then + #### Add validated pubkey to recPubKeysValid array + recPubKeysValid+=("$pubkey") && \ + vbm "DEBUG :$fn:recPubkeysValid:pubkey added:$pubkey"; + else + yell "ERROR:$fn:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; + fi; + done; + vbm "STATUS:$fn:Finished processing argRecPubKeys array"; + vbm "DEBUG :$fn:Array of validated pubkeys:${recPubKeysValid[*]}"; + fi; + ## Parse '-R' options: validate and append pubkeys in argRecDir to recPubKeysValid + if [[ "$optionRecDir" = "true" ]]; then + ### Check that argRecDir is a directory if [[ -d "$argRecDir" ]]; then - recipientDir="$argRecDir" && vbm "STATUS:$fn:Recipient watch directory detected:\"$recipientDir\""; + recipientDir="$argRecDir" && \ + vbm "STATUS:$fn:Recipient watch directory detected:\"$recipientDir\""; #### Initialize variable indicating outcome of pubkey review unset updateRecipients - #### Add existing recipients - candRecPubKeysValid=("${recPubKeysValidStatic[@]}"); + #### Add existing recipients from '-r' option + candRecPubKeysValid=("${recPubKeysValid[@]}"); #### Parse files in recipientDir for file in "$recipientDir"/*; do ##### Read first line of each file - recFileLine="$(head -n1 "$file")" && vbm "STATUS:$fn:Checking if pubkey:\"$recFileLine\""; + recFileLine="$(head -n1 "$file")" && \ + vbm "STATUS:$fn:Checking if pubkey:\"$recFileLine\""; ##### check if first line is a valid pubkey if checkAgePubkey "$recFileLine" && \ ( validateInput "$recFileLine" "ssh_pubkey" || validateInput "$recFileLine" "age_pubkey"); then ###### T: add candidate pubkey to candRecPubKeysValid - candRecPubKeysValid+=("$recFileLine") && vbm "STATUS:$fn:RecDir pubkey is valid pubkey:\"$recFileLine\""; + candRecPubKeysValid+=("$recFileLine") && \ + vbm "STATUS:$fn:RecDir pubkey is valid pubkey:\"$recFileLine\""; else ###### F: throw warning; - yell "ERROR:$fn:Invalid recipient file detected. Not modifying recipient list." + yell "ERROR:$fn:Invalid recipient file detected. Not modifying recipient list:$recFileLine"; updateRecipients="false"; fi; done - #### Write updated recPubKeysValid array to recPubKeysValid if no failure detected + #### Write candRecPubKeysValid array to recPubKeysValid if no invalid key detected if ! [[ "$updateRecipients" = "false" ]]; then - recPubKeysValid=("${candRecPubKeysValid[@]}") && vbm "STATUS:$fn:Wrote candRecPubkeysValid to recPubKeysValid:\"${recPubKeysValid[*]}\""; + recPubKeysValid=("${candRecPubKeysValid[@]}") && \ + vbm "STATUS:$fn:Wrote candRecPubkeysValid to recPubKeysValid:\"${recPubKeysValid[*]}\""; fi; - else - yell "ERROR:$fn:Recipient directory $argRecDir does not exist. Exiting."; exit 1; fi; fi; - # Handle case if '-R' set but '-e' not set - if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then - yell "ERROR:$fn: \\'-R\\' is set but \\'-e\\' is not set."; fi; - vbm "STATUS:$fn:Finished magicParseRecipientDir() function."; -} # Update recPubKeysValid with argRecDir + + ## Form age recipient string from recPubKeysValid + for pubkey in "${recPubKeysValid[@]}"; do + recipients="$recipients""-r '$pubkey' "; + vbm "STATUS:$fn:Added pubkey for forming age recipient string:""$pubkey"; + vbm "DEBUG :$fn:recipients:""$recipients"; + done; + + ## Output cmd_encrypt, cmd_encrypt_suffix from recipients + cmd_encrypt="age ""$recipients " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; + cmd_encrypt_suffix=".age" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; + + vbm "STATUS:$fn:Finished magicParseRecipients() function."; +} # Sets cmd_encrypt, cmd_encrypt_suffix from -r, -R args magicSetScriptTTL() { #Desc: Sets script_TTL seconds from provided time_element string argument #Usage: magicSetScriptTTL [str time_element] @@ -1281,6 +1202,80 @@ magicProcessWriteBuffer() { vbm "STATUS:$fn:Finished magicProcessWriteBuffer()."; } # Process and Write buffer +showUsage() { + cat <<'EOF' + USAGE: + cmd | bklog [ options ] + + OPTIONS: + -h, --help + Display help information. + --version + Display script version. + -v, --verbose + Display debugging info. + -e, --encrypt + Encrypt output. + -r, --recipient [ string pubkey ] + Specify recipient. May be age or ssh pubkey. + May be specified multiple times for multiple pubkeys. + See https://github.com/FiloSottile/age + -o, --output [ path dir ] + Specify output directory to save logs. This option is required + to save log data. + -p, --process-string [ filter command ] [ output file extension] + Specify how to create and name a processed version of the stdin. + For example, if stdin is 'nmea' location data: + + -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" + + This option would cause the stdin to 'bklog' to be piped into + the 'gpsbabel' command, interpreted as 'nmea' data, converted + into 'gpx' format, and then appended to the output tar file + as a file with a '.gpx' extension. + This option may be specified multiple times in order to output + results of multiple different processing methods. + -l, --label [ string ] + Specify a label to be included in all output file names. + Ex: 'location' if stdin is location data. + -w, --store-raw [ file extension ] + Specify file extension of file within output tar that contains + raw stdin data. The default behavior is to always save raw stdin + data in a '.stdin' file. Example usage when 'bklog' receives + 'nmea' data from 'gpspipe -r': + + -w ".nmea" + + Stdin data is saved in a '.nmea' file within the output tar. + -W, --no-store-raw + Do not store raw stdin in output tar. + -c, --compress + Compress output with gzip (before encryption if enabled). + -z, --time-zone + Specify time zone. (ex: "America/New_York") + -t, --temp-dir [path dir] + Specify parent directory for temporary working directory. + Default: "/dev/shm" + -R, --recipient-dir [path dir] + Specify directory containing files whose first lines are + to be interpreted as pubkey strings (see '-r' option). Only + one directory may be specified. + -b, --buffer-ttl [integer] + Specify custom buffer period in seconds (default: 300 seconds) + -B, --script-ttl [time element string] + Specify custom script time-to-live in seconds (default: "day") + Valid values: "day", "hour" + + EXAMPLE: (bash script lines) + $ gpspipe -r | /bin/bash bklog -v -e -c -z "UTC" -t "/dev/shm" \ + -r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \ + -r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \ + -R ~/.config/bklog/recipients -w ".nmea" -b 300 -B "day" \ + -o ~/Sync/Logs -l "location" \ + -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \ + -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" +EOF +} # Display information on how to use this script. main() { # Desc: Main function @@ -1299,10 +1294,8 @@ main() { ## Determine working directory magicInitWorkingDir; # Sets dir_tmp from argTempDirPriority ## Set output encryption and compression option strings - ### React to "-e" and "-r" ("encryption recipients") options - magicParseRecipientArgs; # Updates recPubKeysValid, cmd_encrypt[_suffix] from argRecPubKeys - ### React to "-R" ("recipient directory") option - magicParseRecipientDir; # Updates recPubKeysValid + ### React to "-e", "-r", and "-R" (encryption recipients) options + magicParseRecipients; # Update cmd_encrypt, cmd_encrypt_suffix ### React to "-c" ("compression") option magicParseCompressionArg; # Updates cmd_compress[_suffix] ## React to "-b" and "-B" (custom buffer and script TTL) options @@ -1341,8 +1334,8 @@ main() { if ! [[ -d "$dir_tmp" ]]; then yell "ERROR:$fn:dir_tmp existence failure:$dir_tmp"; try mkdir "$dir_tmp" && vbm "DEBUG :$fn:Working dir recreated dir_tmp:$dir_tmp"; fi - # Update encryption recipient array - magicParseRecipientDir; # Update recPubKeysValid with argRecDir + # Update cmd_encrypt, cmd_encrypt_suffix + magicParseRecipients; # Export buffer to asynchronous processing. magicProcessWriteBuffer & unset buffer; # Clear buffer array for next bufferRound