From 8b8f34dd0cc6b94f49044a9dee832f09509fe37b Mon Sep 17 00:00:00 2001 From: Justin DeBo Date: Mon, 1 Jan 2024 21:09:28 -0600 Subject: [PATCH] Got all the tests working for 7 (FINALLY) --- linux/scripts/init.sh | 4 +- linux/scripts/linux-workshop-admin.sh | 18 +++++++-- linux/scripts/setup-git.sh | 54 +++++++++++++-------------- linux/scripts/test.bats | 26 ++++++++----- 4 files changed, 60 insertions(+), 42 deletions(-) diff --git a/linux/scripts/init.sh b/linux/scripts/init.sh index e5d12e8..22e92df 100755 --- a/linux/scripts/init.sh +++ b/linux/scripts/init.sh @@ -99,8 +99,8 @@ log-info 'Set up systemd timer(s) & service(s)' cp "${wsroot}"/services/* /etc/systemd/system/ systemctl daemon-reload systemctl disable linux-workshop-admin.service -systemctl enable linux-workshop-admin.timer -systemctl start linux-workshop-admin.timer +# systemctl enable linux-workshop-admin.timer +# systemctl start linux-workshop-admin.timer ### _db_init() { diff --git a/linux/scripts/linux-workshop-admin.sh b/linux/scripts/linux-workshop-admin.sh index 1cd3e3b..510d242 100755 --- a/linux/scripts/linux-workshop-admin.sh +++ b/linux/scripts/linux-workshop-admin.sh @@ -139,21 +139,28 @@ _check-webapp-reachable() { } _check-ssh-setup() { + set -x local test_dir=${wsroot}/git-checks local git_home="/srv/git" local repo_dir="${git_home}/repositories/carrot-cruncher.git" - cat ${git_home}/ssh-keys/id_rsa.pub >> /home/git/.ssh/authorized_keys && rm -f ${git_home}/ssh-keys/id_rsa.pub || log-warn "No key to copy" + su - appuser -c "git config --global --add safe.directory ${test_dir};" + if [[ -f ${git_home}/ssh-keys/id_rsa.pub ]]; then + cat ${git_home}/ssh-keys/id_rsa.pub >> /home/git/.ssh/authorized_keys && rm -f ${git_home}/ssh-keys/id_rsa.pub + fi [[ -d ${test_dir} ]] || mkdir -m 777 ${test_dir} - [[ ! -d ${test_dir}/carrot-cruncher ]] || rm -rf /${test_dir}/* + [[ ! -d ${test_dir}/carrot-cruncher ]] || rm -rf /${test_dir:?}/* if su - appuser -c "git clone 'git@localhost:${repo_dir}' ${test_dir}/carrot-cruncher"; then - rm -rf /${test_dir}/* + rm -rf /${test_dir:?}/* /${test_dir:?}/.git + set +x _score-for-challenge 6 else + set +x log-error "SSH Keys not setup successfully" fi } _check-git-branch-merged-correct() { + set -x local test_dir=${wsroot}/git-checks local repo_dir="/srv/git/repositories/carrot-cruncher.git" # pushd "${repo_dir}" > /dev/null @@ -164,15 +171,18 @@ _check-git-branch-merged-correct() { # log-error "commits don't match" # fi pushd "${test_dir}" > /dev/null + su - appuser -c "git config --global --add safe.directory ${test_dir};" # Clone if the directory is empty if [ ! "$(ls -A ${test_dir})" ]; then su - appuser -c "git clone 'git@localhost:${repo_dir}' ${test_dir}" fi su - appuser -c "cd ${test_dir}; git fetch; git checkout main; git pull origin main" if grep -q carrot main.go; then + set +x _score-for-challenge 7 else - log-error "feature branch not merged correctly into main.\n" + set +x + log-error "feature branch not merged correctly into main.\n" fi popd > /dev/null } diff --git a/linux/scripts/setup-git.sh b/linux/scripts/setup-git.sh index a8ec7b4..9714c5a 100644 --- a/linux/scripts/setup-git.sh +++ b/linux/scripts/setup-git.sh @@ -38,8 +38,8 @@ function _setup_ssh_keys_for_git_user() { fi chmod 600 "${authorized_keys_file}" chown "${GIT_USER}:${GIT_USER}" "${authorized_keys_file}" - [[ -d /home/${APP_USER}/.ssh ]] || mkdir /home/${APP_USER}/.ssh - cat </home/${APP_USER}/.ssh/config + [[ -d /home/${APP_USER}/.ssh ]] || mkdir /home/"${APP_USER}"/.ssh + cat </home/"${APP_USER}"/.ssh/config HOST localhost USER ${GIT_USER} PORT ${SSH_PORT} @@ -63,7 +63,7 @@ function _setup_git_user() { fi _setup_ssh_keys_for_git_user # _add_to_known_hosts - echo $(which git-shell) >>/etc/shells + which git-shell >>/etc/shells chsh --shell "$(command -v /bin/bash)" "${GIT_USER}" } @@ -73,12 +73,12 @@ function _init_git_repo() { mkdir -p "${REPO_DIR}" [[ -d "${GIT_HOME}/ssh-keys" ]] || mkdir "${GIT_HOME}/ssh-keys" chown -R "${GIT_USER}:${GIT_USER}" "${GIT_HOME}" - pushd "${REPO_DIR}" >/dev/null - su - ${GIT_USER} -c "git config --global init.defaultBranch ${DEFAULT_BRANCH}" - su - ${GIT_USER} -c "git config --global user.email 'bugs@bigbadbunnies.com'" - su - ${GIT_USER} -c "git config --global user.name 'Bugs Bunny'" - su - ${GIT_USER} -c "pushd "${REPO_DIR}" >/dev/null; git init --bare" - popd >/dev/null + pushd "${REPO_DIR}" >/dev/null || exit + su - "${GIT_USER}" -c "git config --global init.defaultBranch ${DEFAULT_BRANCH}" + su - "${GIT_USER}" -c "git config --global user.email 'bugs@bigbadbunnies.com'" + su - "${GIT_USER}" -c "git config --global user.name 'Bugs Bunny'" + su - "${GIT_USER}" -c "pushd ""${REPO_DIR}"" >/dev/null; git init --bare" + popd >/dev/null || exit } function _setup_local_clone() { @@ -89,36 +89,36 @@ function _setup_local_clone() { fi mkdir "${WORK_DIR}" chmod 777 "${WORK_DIR}" - pushd "${WORK_DIR}" >/dev/null - su - ${GIT_USER} -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone '${GIT_USER}@localhost:${REPO_DIR}' ${clone_dir}" + pushd "${WORK_DIR}" >/dev/null || exit + su - "${GIT_USER}" -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone '${GIT_USER}@localhost:${REPO_DIR}' ${clone_dir}" git config --global --add safe.directory /opt/git/carrot-cruncher - pushd "${clone_dir}" + pushd "${clone_dir}" >/dev/null || exit cp -r "${APP_DIR}"/* . sed -i 's/PrintLine/Println/g' main.go - su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'Initial commit'; git push origin" - popd >/dev/null + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Initial commit'; git push origin" + popd >/dev/null || exit } function _create_release_branch() { local clone_dir="${WORK_DIR}/${REPO_NAME}" local branch_2="v1.0.2-rc-tmp-bugfix-2.0.1" - pushd "${clone_dir}" >/dev/null + pushd "${clone_dir}" >/dev/null || exit log-info "setting up release branch" - su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'" sed -i -e 's/printing/picking/g' -e 's/money/carrots/g' -e 's/CHA-CHING/CRUNCH/g' main.go echo -e "Name: Bugs Bunny\nSecurity Question Answer: 'Crunchy King'\nSSN: 1234-BUNNY" >banking.txt - su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'Prepare release branch'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Prepare release branch'" rm banking.txt - su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'oops did not mean to add that...'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git push --set-upstream origin '${BRANCH_NAME}'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${branch_2}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'oops did not mean to add that...'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git push --set-upstream origin '${BRANCH_NAME}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout -b '${branch_2}'" sed -i -e 's/printing/uh/g' -e 's/money/oh/g' -e 's/CHA-CHING/NO-NO-NOOOOO/g' main.go - su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'I think we might be on to something...'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git push --set-upstream origin '${branch_2}'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" - su - ${GIT_USER} -c "pushd ${clone_dir}; git branch -D ${BRANCH_NAME} ${branch_2}" - popd >/dev/null + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'I think we might be on to something...'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git push --set-upstream origin '${branch_2}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" + su - "${GIT_USER}" -c "pushd ${clone_dir}; git branch -D ${BRANCH_NAME} ${branch_2}" + popd >/dev/null || exit } function _polish_off() { @@ -131,7 +131,7 @@ printf '%s\n' "provide interactive shell access." exit 128 EOF chmod 777 /home/git/git-shell-commands/no-interactive-login - chown -R ${APP_USER}:${APP_USER} /opt/git # git appuser ownership of git directory + chown -R "${APP_USER}":"${APP_USER}" /opt/git # git appuser ownership of git directory } function main() { diff --git a/linux/scripts/test.bats b/linux/scripts/test.bats index b93f0d8..43b092b 100755 --- a/linux/scripts/test.bats +++ b/linux/scripts/test.bats @@ -61,7 +61,7 @@ teardown() { ufw deny out 8000 # Challenge 7 - rm -rf /home/appuser/.ssh/ + rm -rf /home/appuser/.ssh/* _reset-score } @@ -69,8 +69,8 @@ teardown_file() { teardown rm -f /home/appuser/challenge_{2..200}.md # just to be sure to catch any non-0 or 1 challenges rm -f /home/appuser/congrats.md - rm -r "${wsroot}"/team_has_been_congratulated - systemctl start linux-workshop-admin.timer + rm -rf "${wsroot}"/team_has_been_congratulated + # systemctl start linux-workshop-admin.timer } _reset-score() { @@ -165,16 +165,19 @@ _solve-challenge-6() { _solve-challenge-7() { _solve-challenge-6 - sleep 5 # give time for ssh key to copy + systemctl start linux-workshop-admin.service --wait # trigger admin service to copy ssh key + sleep 5 local user="appuser" local RELEASE_BRANCH=release/bunnies_v1 + [[ -d /tmp/carrot-cruncher ]] && rm -rf /tmp/carrot-cruncher su - "${user}" -c "pushd /tmp >/dev/null; \\ + git config --global --add safe.directory /tmp/; git clone git@localhost:/srv/git/repositories/carrot-cruncher.git && \\ pushd carrot-cruncher >/dev/null && \\ git merge origin/${RELEASE_BRANCH} && \\ git push origin main && \\ - popd >/dev/null && \\ - rm -rf /tmp/carrot-cruncher" + popd >/dev/null" + rm -rf /tmp/carrot-cruncher } ################################################################################ @@ -252,7 +255,7 @@ _solve-challenge-7() { # Passes after solution _solve-challenge-5 local score="$(_get-score)" - sleep 1 + sleep 5 printf 'DEBUG: Score from challenge 5: %s\n' "${score}" counter=0 until timeout 1s curl -fsSL "${db_addr}:8000" ; do @@ -272,7 +275,9 @@ _solve-challenge-7() { # Passes after solution _solve-challenge-6 - sleep 10 + local score="$(_get-score)" + sleep 5 + printf 'DEBUG: Score from challenge 6: %s\n' "${score}" su - "appuser" -c "pushd /opt/git/carrot-cruncher >/dev/null; git config --global --add safe.directory /opt/git/carrot-cruncher; git fetch" [[ -f "/home/appuser/challenge_7.md" ]] } @@ -284,7 +289,9 @@ _solve-challenge-7() { # Passes after solution local git_dir=/srv/git/repositories/carrot-cruncher.git local backup_dir=/tmp/git.backup/ - mkdir ${backup_dir} && cp -r ${git_dir}/* "${backup_dir}/" + if [[ ! -d ${backup_dir} ]]; then + mkdir ${backup_dir} && cp -r ${git_dir}/* "${backup_dir}/" + fi _solve-challenge-7 local score="$(_get-score)" sleep 1 @@ -297,6 +304,7 @@ _solve-challenge-7() { sleep 5 popd >/dev/null rm -rf ${git_dir}/* && cp -r ${backup_dir}/* ${git_dir}/ + chown -R git:git ${git_dir} [[ -f "/home/appuser/challenge_8.md" ]] }