Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Retry SSH keyscan command #1971

Merged
merged 5 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ build/.%.done: docker/Dockerfile.%
-f build/docker/$*/Dockerfile.$* ./build/docker/$*
touch $@

build/.flux.done: build/fluxd build/kubectl docker/ssh_config docker/kubeconfig docker/verify_known_hosts.sh
build/.helm-operator.done: build/helm-operator build/kubectl build/helm docker/ssh_config docker/verify_known_hosts.sh docker/helm-repositories.yaml
build/.flux.done: build/fluxd build/kubectl docker/ssh_config docker/kubeconfig docker/known_hosts.sh
build/.helm-operator.done: build/helm-operator build/kubectl build/helm docker/ssh_config docker/known_hosts.sh docker/helm-repositories.yaml

build/fluxd: $(FLUXD_DEPS)
build/fluxd: cmd/fluxd/*.go
Expand Down
7 changes: 3 additions & 4 deletions docker/Dockerfile.flux
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ RUN apk add --no-cache openssh ca-certificates tini 'git>=2.3.0' gnupg

# Add git hosts to known hosts file so we can use
# StrickHostKeyChecking with git+ssh
ADD ./verify_known_hosts.sh /home/flux/verify_known_hosts.sh
RUN ssh-keyscan github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com >> /etc/ssh/ssh_known_hosts && \
sh /home/flux/verify_known_hosts.sh /etc/ssh/ssh_known_hosts && \
rm /home/flux/verify_known_hosts.sh
ADD ./known_hosts.sh /home/flux/known_hosts.sh
RUN sh /home/flux/known_hosts.sh /etc/ssh/ssh_known_hosts && \
rm /home/flux/known_hosts.sh

# Add default SSH config, which points at the private key we'll mount
COPY ./ssh_config /etc/ssh/ssh_config
Expand Down
7 changes: 3 additions & 4 deletions docker/Dockerfile.helm-operator
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ RUN apk add --no-cache openssh ca-certificates tini 'git>=2.3.0'

# Add git hosts to known hosts file so we can use
# StrickHostKeyChecking with git+ssh
ADD ./verify_known_hosts.sh /home/flux/verify_known_hosts.sh
RUN ssh-keyscan github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com >> /etc/ssh/ssh_known_hosts && \
sh /home/flux/verify_known_hosts.sh /etc/ssh/ssh_known_hosts && \
rm /home/flux/verify_known_hosts.sh
ADD ./known_hosts.sh /home/flux/known_hosts.sh
RUN sh /home/flux/known_hosts.sh /etc/ssh/ssh_known_hosts && \
rm /home/flux/known_hosts.sh

# Add default SSH config, which points at the private key we'll mount
COPY ./ssh_config /etc/ssh/ssh_config
Expand Down
28 changes: 28 additions & 0 deletions docker/verify_known_hosts.sh → docker/known_hosts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -eu

known_hosts_file=${1}
known_hosts_file=${known_hosts_file:-/etc/ssh/ssh_known_hosts}
hosts="github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com"

# The heredoc below was generated by constructing a known_hosts using
#
Expand All @@ -28,6 +29,12 @@ trap cleanup EXIT

# make sure sorting is in the same locale as the heredoc
export LC_ALL=C

generate() {
ssh-keyscan ${hosts} > ${known_hosts_file}
}

validate() {
ssh-keygen -l -f ${known_hosts_file} | sort > "$fingerprints"

diff - "$fingerprints" <<EOF
Expand All @@ -39,3 +46,24 @@ diff - "$fingerprints" <<EOF
256 SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw gitlab.com (ECDSA)
256 SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8 gitlab.com (ED25519)
EOF

}

retries=10
count=0
ok=false
wait=2
until ${ok}; do
generate && validate && ok=true || ok=false
count=$(($count + 1))
if [[ ${count} -eq ${retries} ]]; then
echo "ssh-keyscan failed, no more retries left"
exit 1
fi
sleep ${wait}
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
count=$(($count + 1))
if [[ ${count} -eq ${retries} ]]; then
echo "ssh-keyscan failed, no more retries left"
exit 1
fi
done