Skip to content

Commit

Permalink
t1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Mar 3, 2024
1 parent 9b3cf47 commit c1bc737
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
3 changes: 1 addition & 2 deletions scripts/drevops/deploy-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ info "Started LAGOON deployment."
[ -z "${LAGOON_PROJECT}" ] && fail "Missing required value for LAGOON_PROJECT." && exit 1
{ [ -z "${DREVOPS_DEPLOY_BRANCH}" ] && [ -z "${DREVOPS_DEPLOY_PR}" ]; } && fail "Missing required value for DREVOPS_DEPLOY_BRANCH or DREVOPS_DEPLOY_PR." && exit 1

# Use SSH key loader to find the SSH key file.
DREVOPS_DEPLOY_SSH_FILE="$(DREVOPS_SSH_PREFIX="DEPLOY" ./scripts/drevops/setup-ssh.sh | grep 'Using SSH key file ' | cut -d ' ' -f 5 || false)"
DREVOPS_SSH_PREFIX="DEPLOY" ./scripts/drevops/setup-ssh.sh

if ! command -v lagoon >/dev/null || [ -n "${DREVOPS_DEPLOY_LAGOON_LAGOONCLI_FORCE_INSTALL}" ]; then
note "Installing Lagoon CLI."
Expand Down
2 changes: 1 addition & 1 deletion scripts/drevops/download-db-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ if [ -f ".env.local" ]; then
t=$(mktemp) && export -p >"${t}" && set -a && . ".env.local" && set +a && . "${t}" && rm "${t}" && unset t
fi

DREVOPS_DB_DOWNLOAD_SSH_FILE="$(DREVOPS_SSH_PREFIX="DB_DOWNLOAD" ./scripts/drevops/setup-ssh.sh | grep 'Using SSH key file ' | cut -d ' ' -f 5 || false)"
DREVOPS_SSH_PREFIX="DB_DOWNLOAD" ./scripts/drevops/setup-ssh.sh

ssh_opts=(-o "UserKnownHostsFile=/dev/null")
ssh_opts+=(-o "StrictHostKeyChecking=no")
Expand Down
77 changes: 43 additions & 34 deletions scripts/drevops/setup-ssh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
##
# Setup SSH in the environment.
#
# - If key fingerprint provided in MD5 or SHA256 format, search for the existing
# key file. Export the key file path.
# - Start SSH agent if not running. Export SSH_AGENT_PID and SSH_AUTH_SOCK.
# - Load SSH key to the SSH agent.
# - Disable strict host key checking in CI.
#
# IMPORTANT! This script runs outside the container on the host system.
#
# shellcheck disable=SC1090,SC1091
Expand All @@ -18,15 +24,6 @@ set -eu
# key file path.
DREVOPS_SSH_PREFIX="${DREVOPS_SSH_PREFIX?Missing the required DREVOPS_SSH_PREFIX environment variable.}"

# SSH key fingerprint used to load the key into an agent.
# Used only if DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FINGERPRINT is not provided.
DREVOPS_SSH_FINGERPRINT="${DREVOPS_SSH_FINGERPRINT:-}"

# Default SSH file used if custom fingerprint is not provided.
# Used only if $DREVOPS_SSH_FINGERPRINT, $DREVOPS_${DREVOPS_SSH_PREFIX}SSH_FINGERPRINT,
# and $DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FILE values are not provided.
DREVOPS_SSH_FILE="${DREVOPS_SSH_FILE:-${HOME}/.ssh/id_rsa}"

# ------------------------------------------------------------------------------

# @formatter:off
Expand All @@ -38,58 +35,70 @@ fail() { [ "${TERM:-}" != "dumb" ] && tput colors >/dev/null 2>&1 && printf "\03

info "Started SSH setup."

fingerprint="DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FINGERPRINT"
if [ -n "${!fingerprint-}" ]; then
DREVOPS_SSH_FINGERPRINT="${!fingerprint}"
note "Found variable ${fingerprint} with value ${DREVOPS_SSH_FINGERPRINT}."
fingerprint_var="DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FINGERPRINT"
if [ -n "${!fingerprint_var-}" ]; then
fingerprint="${!fingerprint_var}"
note "Found variable ${fingerprint_var} with value ${fingerprint}."
fi

file="DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FILE"
if [ -n "${!file-}" ]; then
DREVOPS_SSH_FILE="${!file}"
note "Found variable ${file} with value ${DREVOPS_SSH_FILE}."
file_var="DREVOPS_${DREVOPS_SSH_PREFIX}_SSH_FILE"
if [ -n "${!file_var-}" ]; then
file="${!file_var}"
note "Found variable ${file_var} with value ${file}."
else
file="${HOME}/.ssh/id_rsa"
note "Using default SSH file ${file}."
fi

if [ -n "${DREVOPS_SSH_FINGERPRINT}" ]; then
if [ -n "${fingerprint}" ]; then
note "Using fingerprint-based deploy key because fingerprint was provided."

if [ "${DREVOPS_SSH_FINGERPRINT#SHA256:}" != "${DREVOPS_SSH_FINGERPRINT}" ]; then
if [ "${fingerprint#SHA256:}" != "${fingerprint}" ]; then
note "Searching for MD5 hash as fingerprint starts with SHA256."
for existing_file in "${HOME}"/.ssh/id_rsa*; do
calculated_sha256_fingerprint=$(ssh-keygen -l -E sha256 -f "${existing_file}" | awk '{print $2}')
if [ "${calculated_sha256_fingerprint}" = "${DREVOPS_SSH_FINGERPRINT}" ]; then
fingerprint_sha256=$(ssh-keygen -l -E sha256 -f "${existing_file}" | awk '{print $2}')
if [ "${fingerprint_sha256}" = "${fingerprint}" ]; then
pass "Found matching existing key file ${existing_file}."
DREVOPS_SSH_FINGERPRINT=$(ssh-keygen -l -E md5 -f "${existing_file}" | awk '{print $2}')
DREVOPS_SSH_FINGERPRINT="${DREVOPS_SSH_FINGERPRINT#MD5:}"
fingerprint=$(ssh-keygen -l -E md5 -f "${existing_file}" | awk '{print $2}')
fingerprint="${fingerprint#MD5:}"
break
fi
done
fi

# Cleanup the fingerprint and create a file name.
file="${DREVOPS_SSH_FINGERPRINT//:/}"
DREVOPS_SSH_FILE="${HOME}/.ssh/id_rsa_${file//\"/}"
file="${fingerprint//:/}"
file="${HOME}/.ssh/id_rsa_${file//\"/}"
fi

if [ ! -f "${DREVOPS_SSH_FILE}" ]; then
fail "SSH key file ${DREVOPS_SSH_FILE} does not exist."
if [ ! -f "${file}" ]; then
fail "SSH key file ${file} does not exist."
exit 1
fi

note "Using SSH key file ${DREVOPS_SSH_FILE}."
note "Using SSH key file ${file}."
if [ -n "${!file_var-}" ] && [ "${!file_var}" != "${file}" ]; then
note "Updating value of ${file_var} variable to ${file}."
export "${file_var}=${file}"
fi

if [ -z "${SSH_AGENT_PID:-}" ]; then
note "Starting SSH agent."
eval "$(ssh-agent)"
if ! pgrep -u "${USER}" ssh-agent >/dev/null; then
note "Starting SSH agent."
eval "$(ssh-agent)"
else
note "SSH agent already running but SSH_AGENT_PID not set."
SSH_AGENT_PID=$(pgrep -u "${USER}" ssh-agent)
export "${SSH_AGENT_PID?"[FAIL] Unable to find SSH agent PID"}"
fi
fi

if ssh-add -l | grep -q "${DREVOPS_SSH_FILE}"; then
note "SSH agent has ${DREVOPS_SSH_FILE} key loaded."
if ssh-add -l | grep -q "${file}"; then
note "SSH agent has ${file} key loaded."
else
note "SSH agent does not have a required key loaded. Trying to load."
# Remove all other keys and add SSH key from provided fingerprint into SSH agent.
ssh-add -D >/dev/null
ssh-add "${DREVOPS_SSH_FILE}"
ssh-add "${file}"
ssh-add -l
fi

Expand Down
3 changes: 1 addition & 2 deletions scripts/drevops/task-custom-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ info "Started Lagoon task ${DREVOPS_TASK_LAGOON_NAME}."
[ -z "${DREVOPS_TASK_LAGOON_COMMAND}" ] && echo "Missing required value for DREVOPS_TASK_LAGOON_COMMAND." && exit 1
[ -z "${DREVOPS_TASK_LAGOON_PROJECT}" ] && echo "Missing required value for DREVOPS_TASK_LAGOON_PROJECT." && exit 1

# Use SSH key loader to find the SSH key file.
DREVOPS_TASK_SSH_FILE="$(DREVOPS_SSH_PREFIX="TASK" ./scripts/drevops/setup-ssh.sh | grep 'Using SSH key file ' | cut -d ' ' -f 5 || false)"
DREVOPS_SSH_PREFIX="TASK" ./scripts/drevops/setup-ssh.sh

if ! command -v lagoon >/dev/null || [ -n "${DREVOPS_TASK_LAGOON_INSTALL_CLI_FORCE}" ]; then
note "Installing Lagoon CLI."
Expand Down

0 comments on commit c1bc737

Please sign in to comment.