Ensure `-noinput` is applied correctly

Follow-up to:
* #10131
* #10257

The following `rabbitmqctl` commands may require `stdin` input:
* `add_user`
* `authenticate_user`
* `change_password`
* `decode`
* `encode`
* `eval`
* `hash_password`

The following `rabbitmq-diagnostics` commands may require `stdin` input:
* `observer`
* `remote_shell`
This commit is contained in:
Luke Bakken 2024-01-02 07:59:31 -08:00
parent d57fde54c5
commit 7da7d4e1e7
No known key found for this signature in database
GPG Key ID: D99DE30E43EAE440
2 changed files with 143 additions and 5 deletions

View File

@ -22,9 +22,16 @@ set -a
maybe_noinput='noinput'
if [ "$1" = 'observer' ]
then
maybe_noinput='input'
fi
case "$1" in
observer)
maybe_noinput='input'
;;
remote_shell)
maybe_noinput='input'
;;
*)
maybe_noinput='noinput'
;;
esac
run_escript "${ESCRIPT_DIR:?must be defined}"/rabbitmq-diagnostics "$maybe_noinput" "$@"

View File

@ -20,4 +20,135 @@ set -a
# shellcheck source=./rabbitmq-env
. "${0%/*}"/rabbitmq-env
run_escript "${ESCRIPT_DIR:?must be defined}"/rabbitmqctl 'noinput' "$@"
# Uncomment for debugging
# echo "\$# : $#"
# echo "\$0: $0"
# echo "\$1: $1"
# echo "\$2: $2"
# echo "\$3: $3"
# echo "\$4: $4"
# echo "\$5: $5"
# set -x
_tmp_help_requested='false'
for _tmp_argument in "$@"
do
if [ "$_tmp_argument" = '--help' ]
then
_tmp_help_requested='true'
break
fi
done
if [ "$1" = 'help' ] || [ "$_tmp_help_requested" = 'true' ]
then
unset _tmp_help_requested
# In this case, we do not require input and can exit early since
# help was requested
#
run_escript "${ESCRIPT_DIR:?must be defined}"/rabbitmqctl 'noinput' "$@"
exit "$?"
fi
unset _tmp_help_requested
maybe_noinput='noinput'
case "$1" in
add_user)
if [ "$#" -eq 2 ]
then
# In this case, input is required to provide the password:
#
# rabbitmqctl add_user bob
#
maybe_noinput='input'
elif [ "$#" -eq 3 ]
then
# In these cases, input depends on the arguments provided:
#
# rabbitmqctl add_user bob --pre-hashed-password (input needed)
# rabbitmqctl add_user bob bobpassword (NO input needed)
# rabbitmqctl add_user --pre-hashed-password bob (input needed)
#
for _tmp_argument in "$@"
do
if [ "$_tmp_argument" = '--pre-hashed-password' ]
then
maybe_noinput='input'
break
fi
done
elif [ "$#" -gt 3 ]
then
# If there are 4 or more arguments, no input is needed:
#
# rabbitmqctl add_user bob --pre-hashed-password HASHVALUE
# rabbitmqctl add_user bob bobpassword IGNORED
# rabbitmqctl add_user --pre-hashed-password bob HASHVALUE
#
maybe_noinput='noinput'
fi
;;
authenticate_user)
if [ "$#" -eq 2 ]
then
# In this case, input is required to provide the password:
#
# rabbitmqctl authenticate_user bob
#
maybe_noinput='input'
elif [ "$#" -gt 2 ]
then
# If there are 2 or more arguments, no input is needed:
#
maybe_noinput='noinput'
fi
;;
change_password)
maybe_noinput='input'
if [ "$#" -gt 2 ]
then
# If there are 3 or more arguments, no input is needed:
#
# rabbitmqctl change_password sue foobar
# rabbitmqctl change_password sue newpassword IGNORED
#
maybe_noinput='noinput'
fi
;;
decode|encode)
# It is unlikely that these commands will be run in a shell script loop
# with redirection, so always assume that stdin input is needed
#
maybe_noinput='input'
;;
eval)
if [ "$#" -eq 1 ]
then
# If there is only one argument, 'eval', then input is required
#
# rabbitmqctl eval
#
maybe_noinput='input'
fi
;;
hash_password)
if [ "$#" -eq 1 ]
then
# If there is only one argument, 'hash_password', then input is required
#
# rabbitmqctl hash_password
#
maybe_noinput='input'
fi
;;
*)
maybe_noinput='noinput'
;;
esac
unset _tmp_argument
run_escript "${ESCRIPT_DIR:?must be defined}"/rabbitmqctl "$maybe_noinput" "$@"