Release: harden multiline secret handling
Modular container CI / release-build (push) Has been cancelled
Modular container CI / release-build (push) Has been cancelled
This commit is contained in:
9 files changed
+133
-56
No files matched your search
@@ -6,26 +6,48 @@
|
||||
# verifying the replacement server fingerprint.
|
||||
readonly ECHOION_GITEA_SSH_RSA_HOST_KEY='git.faumaray.ru ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCzZEz9JLrzk6oLiloSbueAN28vYL58Y/dTHBgRuBMMib9IJ1aDJShhr3kwC9sfOT0AzK0uwcMAqCAIkwJwttde8dIg0Mee0cmB/1JkoqFF4X3MUGD1r3nk8SSZNhIiPqo16hDF9Hf3FkovztIlBVntkyP9Iy9hYfP818Tiq3yRXROGdHjR7IfY4jR2QGfGrd/RtvPGExVqusxidy141240A8aRAwMFUqFdEvmXWjizrtCUqcrL/k1GEh0yJzbCdNOVUI6OuWd8tPluSBvmmeoNpKnCSl4Mkg36HObbOyrsme0asZqjcj0od65WYWApMPN0pUqhNu2CP83Nfor2VFwkdPRwe7cAZ0OD/8wyT6Hz5q1wWgf82y4tYDoDRYsd8JU86eebkJ07T5nCrEeC4Sj+qT4S5xv/kECAhxyP4QGnunORnY2fYsdNGzpPvbMN7gHe9UcIcQdOIgV3wrR1qpMVNcd8AEC2A3TRv/jzMCHSjmGGpgiy1Db2CZAjp5+PcjNmjvoPNN0NiA9X4rIVSeTfflWnDRmeNRJeZa73StjpU17EcAf97COUQPGWJQDgpJqINSpYU50fuaLBfzKnIRuiHjKQmyKe/zgRSmulnYFYM3AJfn8Ykg2VvkUQGvCt1+fqYiUa1iVFuwPP3ZigYF1mOZRpPnos5kaNmlB01q2sZw=='
|
||||
|
||||
echoion_decode_base64_secret() {
|
||||
if (($# != 2)); then
|
||||
echo "usage: echoion_decode_base64_secret BASE64 OUTPUT" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
local encoded=$1
|
||||
local output=$2
|
||||
if [[ -z $encoded || $encoded == *[!A-Za-z0-9+/=]* ]]; then
|
||||
echo "release secret is not single-line base64" >&2
|
||||
return 2
|
||||
fi
|
||||
if ! printf '%s' "$encoded" | base64 --decode >"$output"; then
|
||||
rm -f -- "$output"
|
||||
echo "release secret is not valid base64" >&2
|
||||
return 2
|
||||
fi
|
||||
if [[ ! -s $output ]]; then
|
||||
rm -f -- "$output"
|
||||
echo "decoded release secret is empty" >&2
|
||||
return 2
|
||||
fi
|
||||
chmod 0600 "$output"
|
||||
}
|
||||
|
||||
echoion_configure_component_ssh() {
|
||||
if (($# != 2)); then
|
||||
echo "usage: echoion_configure_component_ssh CREDENTIAL_DIR PRIVATE_KEY" >&2
|
||||
echo "usage: echoion_configure_component_ssh CREDENTIAL_DIR PRIVATE_KEY_BASE64" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
local credential_dir=$1
|
||||
local private_key=$2
|
||||
if [[ -z $private_key ]]; then
|
||||
echo "component SSH private key is empty" >&2
|
||||
return 2
|
||||
fi
|
||||
local private_key_base64=$2
|
||||
|
||||
local ssh_key="$credential_dir/component-ssh-key"
|
||||
local known_hosts="$credential_dir/component-known-hosts"
|
||||
mkdir -p -- "$credential_dir"
|
||||
chmod 0700 "$credential_dir"
|
||||
printf '%s\n' "$private_key" >"$ssh_key"
|
||||
echoion_decode_base64_secret "$private_key_base64" "$ssh_key"
|
||||
printf '%s\n' "$ECHOION_GITEA_SSH_RSA_HOST_KEY" >"$known_hosts"
|
||||
chmod 0600 "$ssh_key" "$known_hosts"
|
||||
chmod 0600 "$known_hosts"
|
||||
ssh-keygen -y -f "$ssh_key" >/dev/null
|
||||
|
||||
printf -v GIT_SSH_COMMAND \
|
||||
'ssh -i %q -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=%q' \
|
||||
|
||||
@@ -16,6 +16,7 @@ configuration=$(realpath -- "$1")
|
||||
source "$script_dir/lib/component-ssh.sh"
|
||||
|
||||
required_commands=(
|
||||
base64
|
||||
cmake
|
||||
cmp
|
||||
ctest
|
||||
@@ -28,6 +29,7 @@ required_commands=(
|
||||
python3
|
||||
realpath
|
||||
ssh
|
||||
ssh-keygen
|
||||
zstd
|
||||
)
|
||||
for command_name in "${required_commands[@]}"; do
|
||||
@@ -134,8 +136,8 @@ umask 077
|
||||
feed_key="$secrets_dir/feed-ed25519.pem"
|
||||
commit_key="$secrets_dir/feed-commit-key"
|
||||
allowed_signers="$secrets_dir/tag-allowed-signers"
|
||||
printf '%s\n' "$ECHOION_FEED_PRIVATE_KEY" >"$feed_key"
|
||||
printf '%s\n' "$ECHOION_FEED_COMMIT_SIGNING_KEY" >"$commit_key"
|
||||
echoion_decode_base64_secret "$ECHOION_FEED_PRIVATE_KEY" "$feed_key"
|
||||
echoion_decode_base64_secret "$ECHOION_FEED_COMMIT_SIGNING_KEY" "$commit_key"
|
||||
printf '%s\n' "$ECHOION_RELEASE_TAG_ALLOWED_SIGNERS" >"$allowed_signers"
|
||||
chmod 0600 "$feed_key" "$commit_key" "$allowed_signers"
|
||||
echoion_configure_component_ssh \
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)
|
||||
# shellcheck source=scripts/lib/component-ssh.sh
|
||||
source "$root/scripts/lib/component-ssh.sh"
|
||||
|
||||
temporary=$(mktemp -d)
|
||||
trap 'rm -rf -- "$temporary"' EXIT
|
||||
|
||||
ssh-keygen -q -t ed25519 -N '' -f "$temporary/source-key"
|
||||
encoded=$(base64 -w0 "$temporary/source-key")
|
||||
|
||||
echoion_decode_base64_secret "$encoded" "$temporary/decoded-key"
|
||||
cmp -- "$temporary/source-key" "$temporary/decoded-key"
|
||||
|
||||
echoion_configure_component_ssh "$temporary/credentials" "$encoded"
|
||||
expected_public=$(ssh-keygen -y -f "$temporary/source-key")
|
||||
actual_public=$(ssh-keygen -y -f "$temporary/credentials/component-ssh-key")
|
||||
test "$actual_public" = "$expected_public"
|
||||
test -n "$GIT_SSH_COMMAND"
|
||||
|
||||
if echoion_decode_base64_secret \
|
||||
"$(<"$temporary/source-key")" "$temporary/rejected-raw-key" \
|
||||
>/dev/null 2>&1
|
||||
then
|
||||
echo "raw multiline private key was accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
if echoion_decode_base64_secret 'not-base64!' "$temporary/rejected-invalid-key" \
|
||||
>/dev/null 2>&1
|
||||
then
|
||||
echo "invalid base64 private key was accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "component SSH secret encoding tests passed"
|
||||
Reference in new issue
Block a user