Skip to content

Commit ea4bca1

Browse files
authored
Merge pull request #256 from github/fix-osx-regressions
Fix macOS/BSD regressions
2 parents c299602 + 4947834 commit ea4bca1

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

share/github-backup-utils/ghe-detect-leaked-ssh-keys

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,22 @@ set -e
1313

1414
usage() {
1515
grep '^#/' < "$0" | cut -c 4-
16+
exit 2
1617
}
1718

1819
TEMPDIR=$(mktemp -d)
1920

20-
# Parse args.
21-
ARGS=$(getopt --name "$0" --long help,snapshot: --options hs -- "$@") || {
22-
usage
23-
exit 2
24-
}
25-
eval set -- $ARGS
26-
2721
while [ $# -gt 0 ]; do
2822
case "$1" in
2923
-h|--help)
3024
usage
31-
exit 2
3225
;;
3326
-s|--snapshot)
34-
shift 2
35-
snapshot=$1
36-
;;
37-
--)
27+
snapshot=$2
3828
shift
39-
break
29+
;;
30+
*)
31+
usage
4032
;;
4133
esac
4234
shift
@@ -47,6 +39,11 @@ if [ -n "$ppid_script" ]; then
4739
ppid_name=$(basename $ppid_script)
4840
fi
4941

42+
sshkeygen_multiple_hash_formats=false
43+
if (ssh-keygen --a-dedicated-help-flag-would-be-great 2>&1 | grep 'ssh-keygen -l ' | grep -q -- '-E'); then
44+
sshkeygen_multiple_hash_formats=true
45+
fi
46+
5047
# Bring in the backup configuration
5148
. $( dirname "${BASH_SOURCE[0]}" )/ghe-backup-config
5249

@@ -56,14 +53,18 @@ keys="ssh_host_dsa_key.pub ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_h
5653

5754
# Get all the host ssh keys tar from all snapshots directories
5855
if [ -n "$snapshot" ]; then
56+
if [ ! -d "$snapshot" ]; then
57+
echo "Invalid snapshot directory: $snapshot" >&2
58+
exit 1
59+
fi
5960
ssh_tars=$(find "$snapshot" -maxdepth 1 -type f -iname 'ssh-host-keys.tar')
6061
else
6162
ssh_tars=$(find "$GHE_DATA_DIR" -maxdepth 2 -type f -iname 'ssh-host-keys.tar')
6263
fi
6364

6465
# Store the current backup snapshot folder
6566
if [ -L "$GHE_DATA_DIR/current" ]; then
66-
current_dir=$(readlink -f "$GHE_DATA_DIR/current")
67+
current_dir=$(cd "$GHE_DATA_DIR/current"; pwd -P)
6768
fi
6869

6970
leaked_keys_found=false
@@ -72,7 +73,11 @@ for tar_file in $ssh_tars; do
7273
for key in $keys; do
7374
if $(tar -tvf "$tar_file" $key &>/dev/null); then
7475
tar -C $TEMPDIR -xvf "$tar_file" $key &>/dev/null
75-
fingerprint=$(ssh-keygen -lf $TEMPDIR/$key | cut -d ' ' -f 2)
76+
if $sshkeygen_multiple_hash_formats; then
77+
fingerprint=$(ssh-keygen -l -E md5 -f $TEMPDIR/$key | cut -d ' ' -f 2 | cut -f2- -d':')
78+
else
79+
fingerprint=$(ssh-keygen -lf $TEMPDIR/$key | cut -d ' ' -f 2)
80+
fi
7681
if echo "$fingerprint_blacklist" | grep -q "$fingerprint"; then
7782
leaked_keys_found=true
7883
if [ "$current_dir" == $(dirname "$tar_file") ]; then

test/test-ghe-detect-leaked-ssh-keys.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ begin_test "ghe-detect-leaked-ssh-keys check -h dispays help message"
2525
(
2626
set -e
2727

28-
ghe-detect-leaked-ssh-keys -h | grep "--help"
28+
ghe-detect-leaked-ssh-keys -h | grep "\-\-help"
2929
)
3030
end_test
3131

0 commit comments

Comments
 (0)