Skip to content

Commit b7b542a

Browse files
committed
refactor(action, workflows): optimize known hosts handling and remove noop script
action.sh: • Improved SSH keyscan logic to filter comments from known hosts. • Removed redundant chmod as it’s handled implicitly. • Ensured unset for SSH_KNOWN_HOSTS_FILE for cleanup. post_action.sh: • Introduced logic to clean up the last entry in known_hosts. • Improved clarity and consistency with explicit unset for temporary variables. post_check.sh: • Added a check to validate known_hosts does not retain specific SSH fingerprints post-execution. • pull_request.yml: • Simplified steps by merging noop logic into the post-check script. • Removed noop.sh, ensuring streamlined workflow execution. These updates enhance security, maintain clean execution contexts, and reduce redundant scripting.
1 parent 1c9dbd0 commit b7b542a

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

.github/workflows/pull_request.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515
steps:
1616
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
17-
- name: Setup post check known hosts
18-
uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
19-
with:
20-
main: |
21-
sh noop.sh
22-
post: |
23-
sh post_check.sh
2417
- name: Setup SSH key
2518
uses: ./
2619
with:
2720
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
2821
ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
2922
log-public-key: false
30-
- name: Check known hosts
31-
shell: bash
32-
run: |
33-
sh check.sh
23+
- name: Check known hosts file
24+
uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
25+
env:
26+
SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
27+
with:
28+
main: |
29+
sh check.sh
30+
post: |
31+
sh post_check.sh
3432
- name: Install docker (Missing on MacOS)
3533
if: runner.os == 'macos'
3634
shell: bash

action.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ ssh-keyscan can help in the detection of tampered keyfiles or man in the middle
2626
the ssh_known_hosts file was created."
2727

2828
if [ -z "${SSH_KEY_TYPE}" ]; then
29-
if ! ssh-keyscan "${SSH_HOST}" >> "${SSH_KNOWN_HOSTS_FILE}"; then
29+
if ! ssh-keyscan "${SSH_HOST} | grep -o '^[^#]*'" >> "${SSH_KNOWN_HOSTS_FILE}"; then
3030
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=SSH Keyscan Failed::\
3131
Failed to scan SSH host keys for ${SSH_HOST}"
3232
exit 1
3333
fi
3434
else
35-
if ! ssh-keyscan -t "${SSH_KEY_TYPE}" "${SSH_HOST}" >> "${SSH_KNOWN_HOSTS_FILE}"; then
35+
if ! ssh-keyscan -t "${SSH_KEY_TYPE}" "${SSH_HOST}" | grep -o '^[^#]*' >> "${SSH_KNOWN_HOSTS_FILE}"; then
3636
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=SSH Keyscan Failed::\
3737
Failed to scan SSH host keys for ${SSH_HOST}"
3838
exit 1
@@ -42,6 +42,4 @@ Failed to scan SSH host keys for ${SSH_HOST}"
4242
fi
4343
fi
4444

45-
chmod 600 "${SSH_KNOWN_HOSTS_FILE}"
46-
4745
unset SSH_KNOWN_HOSTS_FILE

noop.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

post_action.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env sh
22

3-
if [ -z "${SSH_KNOWN_HOSTS_FILE}" ]; then
4-
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
5-
${SSH_KNOWN_HOSTS_FILE} environment variable must be set."
6-
#else
7-
# rm -rf "${SSH_KNOWN_HOSTS_FILE}"
8-
#echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
9-
#${SSH_KNOWN_HOSTS_FILE} has been removed."
10-
fi
3+
TEMP_FILE="/tmp/718f4157-5493-43b2-837b-3ccb27f78e7b"
4+
5+
head --lines=-1 "${SSH_KNOWN_HOSTS_FILE}" > "${TEMP_FILE}"
6+
cat "${TEMP_FILE}" > "${SSH_KNOWN_HOSTS_FILE}"
7+
rm -rf "${TEMP_FILE}"
8+
9+
echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
10+
${SSH_KNOWN_HOSTS_FILE} has been cleaned."
11+
12+
unset TEMP_FILE

post_check.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env sh
22

3-
if [ -s "${HOME}/.ssh/known_hosts" ]; then
4-
echo "ok"
5-
#echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
6-
#~/.ssh/known_hosts file should not exist after the job."
7-
#exit 1
8-
fi
3+
SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
4+
5+
if ! grep -q "${SSH_KNOWN_HOSTS}" "${SSH_KNOWN_HOSTS_FILE}" ; then
6+
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
7+
${SSH_KNOWN_HOSTS_FILE} file should not contain the ssh fingerprint after the job."
8+
exit 1
9+
fi
10+
11+
unset SSH_KNOWN_HOSTS_FILE

0 commit comments

Comments
 (0)