|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Originally taken from the Flux project (https://github.com/fluxcd/flux/tree/master/docker) where is under an |
| 4 | +# Apache-2.0 license |
| 5 | + |
| 6 | +set -eu |
| 7 | + |
| 8 | +known_hosts_file=${1} |
| 9 | +known_hosts_file=${known_hosts_file:-/etc/ssh/ssh_known_hosts} |
| 10 | +hosts="github.com gitlab.com bitbucket.org" |
| 11 | +hosts_2022="source.developers.google.com" |
| 12 | + |
| 13 | +# The heredoc below was generated by constructing a known_hosts using |
| 14 | +# |
| 15 | +# ssh-keyscan github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com > ./known_hosts |
| 16 | +# |
| 17 | +# then generating the sorted fingerprints with |
| 18 | +# |
| 19 | +# ssh-keygen -l -f ./known_hosts | LC_ALL=C sort |
| 20 | +# |
| 21 | +# then checking against the published fingerprints from: |
| 22 | +# - github.com: https://help.github.com/articles/github-s-ssh-key-fingerprints/ |
| 23 | +# - gitlab.com: https://docs.gitlab.com/ee/user/gitlab_com/#ssh-host-keys-fingerprints |
| 24 | +# - bitbucket.org: https://confluence.atlassian.com/bitbucket/ssh-keys-935365775.html |
| 25 | +# - ssh.dev.azure.com & vs-ssh.visualstudio.com: sign in, then go to User settings -> SSH Public Keys |
| 26 | +# (this is where the public key fingerprint is shown; it's not a setting) |
| 27 | +# - source.developers.google.com: https://cloud.google.com/source-repositories/docs/cloning-repositories |
| 28 | + |
| 29 | +fingerprints=$(mktemp -t) |
| 30 | +cleanup() { |
| 31 | + rm -f "$fingerprints" |
| 32 | +} |
| 33 | +trap cleanup EXIT |
| 34 | + |
| 35 | +# make sure sorting is in the same locale as the heredoc |
| 36 | +export LC_ALL=C |
| 37 | + |
| 38 | +generate() { |
| 39 | + ssh-keyscan ${hosts} > ${known_hosts_file} |
| 40 | + ssh-keyscan -p 2022 ${hosts_2022} >> ${known_hosts_file} |
| 41 | + echo "ssh.dev.azure.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4NakVyIzf1rXYd4d7wo6jBlkLvCA4odBlL0mDUyZ0/QUfTTqeu+tm22gOsv+VrVTMk6vwRU75gY/y9ut5Mb3bR5BV58dKXyq9A9UeB5Cakehn5Zgm6x1mKoVyf+FFn26iYqXJRgzIZZcZ5V6hrE0Qg39kZm4az48o0AUbf6Sp4SLdvnuMa2sVNwHBboS7EJkm57XQPVU3/QpyNLHbWDdzwtrlS+ez30S3AdYhLKEOxAG8weOnyrtLJAUen9mTkol8oII1edf7mWWbWVf0nBmly21+nZcmCTISQBtdcyPaEno7fFQMDD26/s0lfKob4Kw8H" >> ${known_hosts_file} |
| 42 | + echo "vs-ssh.visualstudio.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4NakVyIzf1rXYd4d7wo6jBlkLvCA4odBlL0mDUyZ0/QUfTTqeu+tm22gOsv+VrVTMk6vwRU75gY/y9ut5Mb3bR5BV58dKXyq9A9UeB5Cakehn5Zgm6x1mKoVyf+FFn26iYqXJRgzIZZcZ5V6hrE0Qg39kZm4az48o0AUbf6Sp4SLdvnuMa2sVNwHBboS7EJkm57XQPVU3/QpyNLHbWDdzwtrlS+ez30S3AdYhLKEOxAG8weOnyrtLJAUen9mTkol8oII1edf7mWWbWVf0nBmly21+nZcmCTISQBtdcyPaEno7fFQMDD26/s0lfKob4Kw8H" >> ${known_hosts_file} |
| 43 | +} |
| 44 | + |
| 45 | +validate() { |
| 46 | +ssh-keygen -l -f ${known_hosts_file} | sort > "$fingerprints" |
| 47 | + |
| 48 | +diff - "$fingerprints" <<EOF |
| 49 | +2048 SHA256:ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ gitlab.com (RSA) |
| 50 | +2048 SHA256:ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og ssh.dev.azure.com (RSA) |
| 51 | +2048 SHA256:ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og vs-ssh.visualstudio.com (RSA) |
| 52 | +256 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU github.com (ED25519) |
| 53 | +256 SHA256:AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434 [source.developers.google.com]:2022 (ECDSA) |
| 54 | +256 SHA256:FC73VB6C4OQLSCrjEayhMp9UMxS97caD/Yyi2bhW/J0 bitbucket.org (ECDSA) |
| 55 | +256 SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw gitlab.com (ECDSA) |
| 56 | +256 SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8 gitlab.com (ED25519) |
| 57 | +256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM github.com (ECDSA) |
| 58 | +256 SHA256:ybgmFkzwOSotHTHLJgHO0QN8L0xErw6vd0VhFA9m3SM bitbucket.org (ED25519) |
| 59 | +3072 SHA256:46OSHA1Rmj8E8ERTC6xkNcmGOw9oFxYr0WF6zWW8l1E bitbucket.org (RSA) |
| 60 | +3072 SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s github.com (RSA) |
| 61 | +EOF |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +retries=10 |
| 66 | +count=0 |
| 67 | +ok=false |
| 68 | +wait=2 |
| 69 | +until ${ok}; do |
| 70 | + generate && validate && ok=true || ok=false |
| 71 | + count=$(($count + 1)) |
| 72 | + if [[ ${count} -eq ${retries} ]]; then |
| 73 | + echo "ssh-keyscan failed, no more retries left" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + sleep ${wait} |
| 77 | +done |
0 commit comments