negative case where username doesn't exist now report exception as expected

This commit is contained in:
HE Chong
2021-08-13 23:00:11 +08:00
parent 0d5e3771f5
commit 81ccce8659

View File

@@ -34,6 +34,16 @@ MYSQL_PASS=$(lookup_pillar_secret mysql)
FLEET_IP=$(lookup_pillar fleet_ip) FLEET_IP=$(lookup_pillar fleet_ip)
FLEET_USER=$USER FLEET_USER=$USER
# test existence of user
MYSQL_OUTPUT=$(docker exec so-mysql mysql -u root --password=$MYSQL_PASS fleet -e \
"SELECT count(1) FROM users WHERE username='$FLEET_USER'" 2>/dev/null | tail -1)
if [[ $? -ne 0 ]] || [[ $MYSQL_OUTPUT -ne 1 ]] ; then
echo "Test for username [${FLEET_USER}] failed"
echo " expect 1 hit in users database, return $MYSQL_OUTPUT hit(s)."
echo "Unable to update Fleet user password."
exit 2
fi
# Read password for new user from stdin # Read password for new user from stdin
test -t 0 test -t 0
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@@ -52,6 +62,7 @@ if [[ $? -ne 0 ]]; then
exit 2 exit 2
fi fi
MYSQL_OUTPUT=$(docker exec so-mysql mysql -u root --password=$MYSQL_PASS fleet -e \ MYSQL_OUTPUT=$(docker exec so-mysql mysql -u root --password=$MYSQL_PASS fleet -e \
"UPDATE users SET password='$FLEET_HASH', salt='' where username='$FLEET_USER'" 2>&1) "UPDATE users SET password='$FLEET_HASH', salt='' where username='$FLEET_USER'" 2>&1)