Skip to content

Commit

Permalink
add proxy/bastion for connection
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyLuLiu authored and adrianriobo committed Oct 23, 2024
1 parent 5a38b3a commit 28bbbfb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION ?= 0.0.6
VERSION ?= 0.0.7
CONTAINER_MANAGER ?= podman
IMG ?= quay.io/rhqp/deliverest:v${VERSION}

Expand Down
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ if [[ ! remote_required ]] || [[ ! mamp_required ]] || [[ -z "${ASSETS_FOLDER+x}
exit 1
fi

# Create ssh config file is using proxy connection
if [[ -n "${BASTION_HOST}" && -n "${BASTION_HOST_USERNAME}" ]];then
ssh_config_file
fi

if [ "${CHECK_CONNECTION:-}" = "true" ]; then
check_connection ${CHECK_CONNECTION_ATTEMPTS} ${CHECK_CONNECTION_DELAY}
if [[ $? -gt 0 ]]
Expand All @@ -28,6 +33,7 @@ if [ "${CHECK_CONNECTION:-}" = "true" ]; then
fi
fi


# Create execution folder
echo "Create assets folder on target"
TARGET_FOLDER="${TARGET_FOLDER:-"deliverest-${RANDOM}"}"
Expand Down
59 changes: 45 additions & 14 deletions lib/common/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ remote_required () {
[[ -z "${TARGET_HOST_KEY_PATH+x}" && -z "${TARGET_HOST_PASSWORD+x}" ]] \
&& echo "TARGET_HOST_KEY_PATH or TARGET_HOST_PASSWORD required" \
&& validate=0

return $validate
}

Expand All @@ -33,6 +32,29 @@ connect_options() {
echo $options
}

ssh_config_file() {
cat <<EOF > ssh_config
Host proxy_host
StrictHostKeyChecking no
HostName ${BASTION_HOST}
User ${BASTION_HOST_USERNAME}
IdentityFile ${BASTION_HOST_KEY_PATH}
Host target_host
HostName ${TARGET_HOST}
User ${TARGET_HOST_USERNAME}
IdentityFile ${TARGET_HOST_KEY_PATH}
ProxyJump proxy_host
EOF
if [[ -z ${TARGET_HOST_KEY_PATH+x} ]]; then
sed -i"" -e '9d' ssh_config
fi
if [[ -z ${BASTION_HOST_KEY_PATH+x} ]]; then
sed -i"" -e '4d' ssh_config
fi
cat ssh_config
}

# If restart is involved, it can take a moment for the target host to become available again
# Check the connection to the host; "delay" in seconds, "repeats" in number of reps
# Run as: check_connection <repeats int> <delay int>
Expand Down Expand Up @@ -63,7 +85,7 @@ check_connection() {
# Define remote connection
uri () {
local remote="${TARGET_HOST_USERNAME}@${TARGET_HOST}"
if [[ ! -z "${TARGET_HOST_DOMAIN+x}" ]]; then
if [[ -n "${TARGET_HOST_DOMAIN}" ]]; then
remote="${TARGET_HOST_USERNAME}@${TARGET_HOST_DOMAIN}@${TARGET_HOST}"
fi
echo "${remote}"
Expand All @@ -73,37 +95,46 @@ uri () {
# $1 local path
# $2 remote path
scp_to_cmd () {
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
echo "scp -r $(connect_options) -i ${TARGET_HOST_KEY_PATH} ${1} $(uri):${2}"
cmd="scp -r $(connect_options) "
if [[ -n "${BASTION_HOST}" && -n "${BASTION_HOST_USERNAME}" ]]; then
echo "${cmd} -F ssh_config ${1} target_host:${2}"
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
echo "${cmd} -i ${TARGET_HOST_KEY_PATH} ${1} $(uri):${2}"
else
echo "sshpass -p ${TARGET_HOST_PASSWORD} scp -r $(connect_options) ${1} $(uri):${2}"
echo "sshpass -p ${TARGET_HOST_PASSWORD} ${cmd} ${1} $(uri):${2}"
fi
}

# Generate SCP command
# $1 remote path
# $2 local path
scp_from_cmd () {
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
echo "scp -r $(connect_options) -i ${TARGET_HOST_KEY_PATH} $(uri):${1} ${2}"
cmd="scp -r $(connect_options) "
if [[ -n "${BASTION_HOST}" && -n "${BASTION_HOST_USERNAME}" ]]; then
echo "${cmd} -F ssh_config target_host:${1} ${2} "
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
echo "${cmd} -i ${TARGET_HOST_KEY_PATH} $(uri):${1} ${2}"
else
echo "sshpass -p ${TARGET_HOST_PASSWORD} scp -r $(connect_options) $(uri):${1} ${2}"
echo "sshpass -p ${TARGET_HOST_PASSWORD} ${cmd} $(uri):${1} ${2}"
fi
}

# Generate SSH command
ssh_cmd () {
cmd=""
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
cmd="ssh $(connect_options) -i ${TARGET_HOST_KEY_PATH} $(uri) "
cmd="ssh $(connect_options) "
if [[ -n "${BASTION_HOST}" && -n "${BASTION_HOST_USERNAME}" ]]; then
cmd+="-F ssh_config target_host "
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
cmd+="-i ${TARGET_HOST_KEY_PATH} $(uri) "
else
cmd="sshpass -p ${TARGET_HOST_PASSWORD} ssh $(connect_options) $(uri) "
cmd="sshpass -p ${TARGET_HOST_PASSWORD} ${cmd} $(uri) "
fi

# On AWS MacOS ssh session is not recognized as expected
if [[ ${OS} == 'darwin' ]]; then
cmd+="sudo su - ${TARGET_HOST_USERNAME} -c \"PATH=\$PATH:/usr/local/bin && $@\""
else
cmd+="$@"
cmd+=" $@"
fi
echo "${cmd}"
}
}

0 comments on commit 28bbbfb

Please sign in to comment.