Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add pull request workflow with SSH host validation and deployment checks #3

Merged
merged 24 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d7e6dd8
feat: Add pull request workflow with SSH host validation and deployme…
warnyul Jan 2, 2025
9b635de
fix: Update Docker image version reference in pull_request workflow
warnyul Jan 2, 2025
3816620
fix: Update `docker run` command in pull_request workflow
warnyul Jan 2, 2025
90ef56d
fix: Ensure known_hosts directory exists before writing
warnyul Jan 2, 2025
9756bf1
fix: Add Docker installation step for macOS runners
warnyul Jan 2, 2025
017b206
fix: Streamline SSH known hosts setup in pull request workflow
warnyul Jan 2, 2025
f31073c
fix: Enhance error handling and notices in SSH setup scripts
warnyul Jan 2, 2025
93ed533
fix: Set secure permissions for SSH known hosts file
warnyul Jan 2, 2025
294d76a
chore(ci): rename workflow step for host key validation
warnyul Jan 2, 2025
d5847cd
fix: remove default SSH key type from inputs
warnyul Jan 2, 2025
6ab1366
chore(action.yml): reorder steps for ssh-agent setup
warnyul Jan 2, 2025
c80cd94
refactor(workflow): standardize script execution and modularize deplo…
warnyul Jan 2, 2025
b068c35
refactor(workflow): modularize no-operation step
warnyul Jan 2, 2025
e0f59f8
refactor(scripts): switch from bash to sh for enhanced portability
warnyul Jan 2, 2025
ff1af96
fix(scripts): use $HOME variable for known_hosts path
warnyul Jan 2, 2025
b3c467c
refactor(ssh): improve known_hosts handling and cleanup
warnyul Jan 2, 2025
7d9f6fb
refactor(deploy): simplify WHOAMI_VERSION variable
warnyul Jan 2, 2025
e90d822
refactor(deploy, post_action): enhance container management and strea…
warnyul Jan 2, 2025
1c9dbd0
x
warnyul Jan 2, 2025
b7b542a
refactor(action, workflows): optimize known hosts handling and remove…
warnyul Jan 3, 2025
fad31a4
fix(post_action): ensure consistent variable definition for known hosts
warnyul Jan 3, 2025
1f08d0f
fix(post_action): replace head with sed for removing last line
warnyul Jan 3, 2025
7406251
fix(post_action): correct sed syntax for removing the last line
warnyul Jan 3, 2025
533bb39
diff --git a/.github/workflows/pull_request.yml b/.github/workflows/p…
warnyul Jan 3, 2025
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
42 changes: 42 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
on:
pull_request:
branches:
- main
jobs:
test:
strategy:
fail-fast: false
matrix:
os:
- macos-15
- ubuntu-24.04
- windows-2025
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup SSH key
uses: ./
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
log-public-key: false
- name: Check known hosts file
uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
env:
SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
with:
main: |
sh check.sh
post: |
sh post_check.sh
- name: Install docker (Missing on MacOS)
if: runner.os == 'macos'
shell: bash
run: |
brew install --cask docker
- name: Deploy over SSH
shell: bash
env:
DOCKER_HOST: ${{ secrets.DOCKER_HOST }}
run: |
sh deploy.sh
16 changes: 13 additions & 3 deletions action.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

Check notice on line 1 in action.sh

View workflow job for this annotation

GitHub Actions / test (ubuntu-24.04)

Notice

/home/runner/.ssh/known_hosts has been created.

set -e

SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"

mkdir -p "$(dirname "${SSH_KNOWN_HOSTS_FILE}")"

if [ -z "${SSH_HOST}" ] && [ -z "${SSH_KNOWN_HOSTS}" ]; then
echo ":error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Input validation::\
Expand All @@ -11,6 +17,7 @@
Both 'ssh-host' and 'ssh-known-hosts' inputs are set. Using 'ssh-known-hosts'."
fi
echo "${SSH_KNOWN_HOSTS}" >> "${SSH_KNOWN_HOSTS_FILE}"
echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::${SSH_KNOWN_HOSTS_FILE} has been created."

Check notice on line 20 in action.sh

View workflow job for this annotation

GitHub Actions / test (macos-15)

Notice

/Users/runner/.ssh/known_hosts has been created.

Check notice on line 20 in action.sh

View workflow job for this annotation

GitHub Actions / test (windows-2025)

Notice

/c/Users/runneradmin/.ssh/known_hosts has been created.
else
echo "::warning file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Security risk::\
If an ssh_known_hosts file is constructed using ssh-keyscan without verifying the keys, \
Expand All @@ -19,17 +26,20 @@
the ssh_known_hosts file was created."

if [ -z "${SSH_KEY_TYPE}" ]; then
if ! ssh-keyscan "${SSH_HOST}" >> "${SSH_KNOWN_HOSTS_FILE}"; then
if ! ssh-keyscan "${SSH_HOST} | grep -o '^[^#]*'" >> "${SSH_KNOWN_HOSTS_FILE}"; then
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=SSH Keyscan Failed::\
Failed to scan SSH host keys for ${SSH_HOST}"
exit 1
fi
else
if ! ssh-keyscan -t "${SSH_KEY_TYPE}" "${SSH_HOST}" >> "${SSH_KNOWN_HOSTS_FILE}"; then
if ! ssh-keyscan -t "${SSH_KEY_TYPE}" "${SSH_HOST}" | grep -o '^[^#]*' >> "${SSH_KNOWN_HOSTS_FILE}"; then
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=SSH Keyscan Failed::\
Failed to scan SSH host keys for ${SSH_HOST}"
exit 1
fi
fi
echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::${SSH_KNOWN_HOSTS_FILE} has been created."
fi
fi

unset SSH_KNOWN_HOSTS_FILE
22 changes: 10 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ inputs:
Specify the type of the key to fetch from the scanned hosts. The possible values are “ecdsa”, “ed25519”, “ecdsa-sk”, “ed25519-sk”, or “rsa”.
Multiple values may be specified by separating them with commas. The default is to fetch all the above key types.
required: false
default: 'rsa'
ssh-known-hosts:
description: 'Predefined known hosts to be added directly.'
required: false
Expand All @@ -36,17 +35,6 @@ inputs:
runs:
using: 'composite'
steps:
- uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
env:
SSH_HOST: ${{ inputs.ssh-host }}
SSH_KEY_TYPE: ${{ inputs.ssh-key-type }}
SSH_KNOWN_HOSTS: ${{ inputs.ssh-known-hosts }}
SSH_KNOWN_HOSTS_FILE: '~/.ssh/known_hosts'
with:
main: |
./action.sh
post: |
./post_action.sh
- uses: webfactory/ssh-agent@dc588b651fe13675774614f8e6a936a468676387 # v0.9.0
with:
ssh-private-key: ${{ inputs.ssh-private-key }}
Expand All @@ -55,4 +43,14 @@ runs:
ssh-agent-cmd: ${{ inputs.ssh-agent-cmd }}
ssh-add-cmd: ${{ inputs.ssh-add-cmd }}
git-cmd: ${{ inputs.git-cmd }}
- uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
env:
SSH_HOST: ${{ inputs.ssh-host }}
SSH_KEY_TYPE: ${{ inputs.ssh-key-type }}
SSH_KNOWN_HOSTS: ${{ inputs.ssh-known-hosts }}
with:
main: |
sh action.sh
post: |
sh post_action.sh

7 changes: 7 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh

if [ ! -s "${HOME}/.ssh/known_hosts" ]; then
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
~/.ssh/known_hosts is missing or empty."
exit 1
fi
11 changes: 11 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh

# renovate: datasource=docker depname=traefik/whoami versioning=docker
WHOAMI_VERSION="v1.10"
CONTAINER_NAME="${RUNNER_OS}-whoami"
docker run --detach --publish-all --name "${CONTAINER_NAME}" traefik/whoami:"${WHOAMI_VERSION}"
docker stop "${CONTAINER_NAME}"
docker rm "${CONTAINER_NAME}"

unset WHOAMI_VERSION
unset CONTAINER_NAME
24 changes: 14 additions & 10 deletions post_action.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/env bash

if [ -z "${SSH_KNOWN_HOSTS_FILE}" ]; then
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
${SSH_KNOWN_HOSTS_FILE} environment variable must be set."
else
rm -rf "${SSH_KNOWN_HOSTS_FILE}"
echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
${SSH_KNOWN_HOSTS_FILE} has been removed."
fi
#!/usr/bin/env sh

Check notice on line 1 in post_action.sh

View workflow job for this annotation

GitHub Actions / test (ubuntu-24.04)

Notice

/home/runner/.ssh/known_hosts has been cleaned.

SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
TEMP_FILE="/tmp/718f4157-5493-43b2-837b-3ccb27f78e7b"

sed '$ d' "${SSH_KNOWN_HOSTS_FILE}" > "${TEMP_FILE}"
cat "${TEMP_FILE}" > "${SSH_KNOWN_HOSTS_FILE}"
rm -rf "${TEMP_FILE}"

echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
${SSH_KNOWN_HOSTS_FILE} has been cleaned."

Check notice on line 11 in post_action.sh

View workflow job for this annotation

GitHub Actions / test (macos-15)

Notice

/Users/runner/.ssh/known_hosts has been cleaned.

Check notice on line 11 in post_action.sh

View workflow job for this annotation

GitHub Actions / test (windows-2025)

Notice

/c/Users/runneradmin/.ssh/known_hosts has been cleaned.

unset SSH_KNOWN_HOSTS_FILE
unset TEMP_FILE
11 changes: 11 additions & 0 deletions post_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh

SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"

if ! grep -q "${SSH_KNOWN_HOSTS}" "${SSH_KNOWN_HOSTS_FILE}" ; then
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
${SSH_KNOWN_HOSTS_FILE} file should not contain the ssh fingerprint after the job."
exit 1
fi

unset SSH_KNOWN_HOSTS_FILE
Loading