From e3625c68162e9b4ebb52854747eb4f5a2c675532 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Fri, 30 Oct 2020 07:54:59 +0530 Subject: [PATCH] gupload: Add _cleanup_config function this function removes any expired tokens on the basis of expiry will be useful once service accounts or multi account config implemented --- bash/release/gupload | 31 ++++++++++++++++++++++++++++++- bash/upload.bash | 31 ++++++++++++++++++++++++++++++- sh/release/gupload | 32 +++++++++++++++++++++++++++++++- sh/upload.sh | 32 +++++++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 4 deletions(-) diff --git a/bash/release/gupload b/bash/release/gupload index 9af7f29..5fb842a 100755 --- a/bash/release/gupload +++ b/bash/release/gupload @@ -1061,6 +1061,35 @@ _version_info() { exit 0 } +################################################### +# Function to cleanup config file +# Remove invalid access tokens on the basis of corresponding expiry +# Globals: None +# Arguments: 1 +# ${1} = config file +# Result: read description +################################################### +_cleanup_config() { + declare config="${1:?Error: Missing config}" values_regex + + ! [ -f "${config}" ] && return 0 + + while read -r line; do + expiry_value_name="${line%%=*}" + token_value_name="${expiry_value_name%%_EXPIRY}" + + : "${line##*=}" && : "${_%\"}" && expiry="${_#\"}" + [[ ${expiry} -le "$(printf "%(%s)T\\n" "-1")" ]] && + values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\"" + + done <<< "$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)" + + chmod +w "${config}" && + printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" && + chmod -w "${config}" + return 0 +} + ################################################### # Process all arguments given to the script # Globals: 2 variable, 1 function @@ -1672,7 +1701,7 @@ main() { printf "\n\n%s\n" "Script exited manually." kill -- -$$ & else - _auto_update & + { _cleanup_config "${CONFIG}" && _auto_update; } & fi } 2>| /dev/null 1>&2 || : return 0 diff --git a/bash/upload.bash b/bash/upload.bash index 3b66107..fbd4c85 100755 --- a/bash/upload.bash +++ b/bash/upload.bash @@ -128,6 +128,35 @@ _version_info() { exit 0 } +################################################### +# Function to cleanup config file +# Remove invalid access tokens on the basis of corresponding expiry +# Globals: None +# Arguments: 1 +# ${1} = config file +# Result: read description +################################################### +_cleanup_config() { + declare config="${1:?Error: Missing config}" values_regex + + ! [ -f "${config}" ] && return 0 + + while read -r line; do + expiry_value_name="${line%%=*}" + token_value_name="${expiry_value_name%%_EXPIRY}" + + : "${line##*=}" && : "${_%\"}" && expiry="${_#\"}" + [[ ${expiry} -le "$(printf "%(%s)T\\n" "-1")" ]] && + values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\"" + + done <<< "$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)" + + chmod +w "${config}" && + printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" && + chmod -w "${config}" + return 0 +} + ################################################### # Process all arguments given to the script # Globals: 2 variable, 1 function @@ -739,7 +768,7 @@ main() { printf "\n\n%s\n" "Script exited manually." kill -- -$$ & else - _auto_update & + { _cleanup_config "${CONFIG}" && _auto_update; } & fi } 2>| /dev/null 1>&2 || : return 0 diff --git a/sh/release/gupload b/sh/release/gupload index 3605c15..d49af88 100755 --- a/sh/release/gupload +++ b/sh/release/gupload @@ -1047,6 +1047,36 @@ _version_info() { exit 0 } +################################################### +# Function to cleanup config file +# Remove invalid access tokens on the basis of corresponding expiry +# Globals: None +# Arguments: 1 +# ${1} = config file +# Result: read description +################################################### +_cleanup_config() { + config="${1:?Error: Missing config}" && unset values_regex _tmp + + ! [ -f "${config}" ] && return 0 + + while read -r line <&4; do + expiry_value_name="${line%%=*}" + token_value_name="${expiry_value_name%%_EXPIRY}" + + _tmp="${line##*=}" && _tmp="${_tmp%\"}" && expiry="${_tmp#\"}" + [ "${expiry}" -le "$(date +"%s")" ] && + values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\"" + done 4<< EOF +$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :) +EOF + + chmod +w "${config}" && + printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" && + chmod -w "${config}" + return 0 +} + ################################################### # Process all arguments given to the script # Globals: 1 variable, 1 function @@ -1682,7 +1712,7 @@ main() { printf "\n\n%s\n" "Script exited manually." kill -9 -$$ & else - _auto_update & + { _cleanup_config "${CONFIG}" && _auto_update; } & fi } 2>| /dev/null 1>&2 || : return 0 diff --git a/sh/upload.sh b/sh/upload.sh index c7fff91..c82fd13 100755 --- a/sh/upload.sh +++ b/sh/upload.sh @@ -129,6 +129,36 @@ _version_info() { exit 0 } +################################################### +# Function to cleanup config file +# Remove invalid access tokens on the basis of corresponding expiry +# Globals: None +# Arguments: 1 +# ${1} = config file +# Result: read description +################################################### +_cleanup_config() { + config="${1:?Error: Missing config}" && unset values_regex _tmp + + ! [ -f "${config}" ] && return 0 + + while read -r line <&4; do + expiry_value_name="${line%%=*}" + token_value_name="${expiry_value_name%%_EXPIRY}" + + _tmp="${line##*=}" && _tmp="${_tmp%\"}" && expiry="${_tmp#\"}" + [ "${expiry}" -le "$(date +"%s")" ] && + values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\"" + done 4<< EOF +$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :) +EOF + + chmod +w "${config}" && + printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" && + chmod -w "${config}" + return 0 +} + ################################################### # Process all arguments given to the script # Globals: 1 variable, 1 function @@ -764,7 +794,7 @@ main() { printf "\n\n%s\n" "Script exited manually." kill -9 -$$ & else - _auto_update & + { _cleanup_config "${CONFIG}" && _auto_update; } & fi } 2>| /dev/null 1>&2 || : return 0