fedora-createrepo-image/scripts/xlion-repo-rpm-sign-packages
XLion 964aa339d1
Set rpm's default backend to sq; sign rpm to both v4 and v6 format; import key to sq; (#525)
* Add sq

* repomd sq

* shellcheck

* fix: request both v4 and v6 RPM signatures

* fix: complete Sequoia signing contract

* fix: require RPM v4-compatible signing keys

* docs: streamline agent guidance and track downstream compatibility

* fix: reject invalid RPM verification and match explicit signing keys

* refactor: share RPM signing command across retries

* refactor: simplify key import and signing test fixtures
2026-10-28 10:32:32 +08:00

319 lines
8 KiB
Shell
Executable file

#!/bin/bash
#
# xlion-repo-rpm-sign-packages - Sign RPM packages and verify signatures.
#
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
DIRS=()
GPG_KEY=""
RETRY=3
RETRY_SLEEP=2
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
info() {
echo -e "${BLUE}$1${NC}" >&2
}
success() {
echo -e "${GREEN}$1${NC}" >&2
}
warn() {
echo -e "${YELLOW}Warning: $1${NC}" >&2
}
show_help() {
cat << EOF
xlion-repo-rpm-sign-packages - Sign RPM packages and verify signatures.
Usage:
$(basename "$0") --dir DIR [--dir DIR ...] --gpg-key KEY [options]
Options:
--dir DIR Directory containing .rpm packages. Can be repeated.
--gpg-key KEY Hex OpenPGP key ID (8/16 hex digits) or fingerprint (32/40/64 hex digits).
--retry COUNT Retry count when signature verification fails. Default: 3.
--retry-sleep SEC Seconds to sleep between retries. Default: 2.
-h, --help Show this help message.
Examples:
$(basename "$0") --dir wwwroot/latest --dir wwwroot/nightly --gpg-key "\$GPG_FINGERPRINT"
Notes:
- RPM packages are signed sequentially, not concurrently.
- Signing uses sq and requests both RPM v4 and v6 signature formats.
- rpmkeys -Kv must exit successfully, with no BAD or NOT OK result.
- A signature line must report : OK, a supported RPM v4 algorithm (RSA, DSA, ECDSA, or EdDSA), and the key in its explicit fingerprint/key ID field.
- v4 key IDs use the final 16 fingerprint digits; v6 key IDs use the first 16. An 8-digit ID matches the final 8 digits of a 16-digit ID; v3 fingerprints match only exactly.
- RPM v4 and v6 signature storage remains delegated to rpmsign.
- This tool expects the signing key to already be imported into sq.
- This tool expects the public key to already be imported into rpmkeys.
- Logs are printed to stderr.
EOF
}
check_dependencies() {
local missing=()
local cmd
for cmd in "$@"; do
if ! command -v "$cmd" > /dev/null 2>&1; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
error "Missing required CLI tool(s): ${missing[*]}"
fi
}
parse_non_negative_int() {
local name="$1"
local value="$2"
if [[ ! "$value" =~ ^[0-9]+$ ]]; then
error "Option $name must be a non-negative integer: $value"
fi
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dir)
[[ $# -ge 2 ]] || error "Option --dir requires a value."
DIRS+=("$2")
shift 2
;;
--gpg-key)
[[ $# -ge 2 ]] || error "Option --gpg-key requires a value."
GPG_KEY="$2"
shift 2
;;
--retry)
[[ $# -ge 2 ]] || error "Option --retry requires a value."
parse_non_negative_int --retry "$2"
RETRY="$2"
shift 2
;;
--retry-sleep)
[[ $# -ge 2 ]] || error "Option --retry-sleep requires a value."
parse_non_negative_int --retry-sleep "$2"
RETRY_SLEEP="$2"
shift 2
;;
-h | --help)
show_help
exit 0
;;
*)
error "Unknown option: $1"
;;
esac
done
}
resolve_existing_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
error "RPM package directory does not exist: $dir"
fi
(cd "$dir" && pwd -P) || error "Failed to resolve directory: $dir"
}
collect_rpms() {
local dir
local rpm_files=()
local dir_rpms=()
for dir in "${DIRS[@]}"; do
resolved_dir="$(resolve_existing_dir "$dir")"
mapfile -t dir_rpms < <(find "$resolved_dir" -maxdepth 1 -type f -name '*.rpm' | sort)
if [[ ${#dir_rpms[@]} -eq 0 ]]; then
error "No .rpm files found in directory: $resolved_dir"
fi
rpm_files+=("${dir_rpms[@]}")
done
printf '%s\n' "${rpm_files[@]}"
}
has_signature() {
local rpm_file="$1"
local output
local rpmkeys_status=0
local key_lc="${GPG_KEY,,}"
local line
local line_lc
local signature_key
local has_matching_signature=0
echo "== rpmkeys output for $rpm_file ==" >&2
if output="$(rpmkeys -Kv "$rpm_file" 2>&1)"; then
rpmkeys_status=0
else
rpmkeys_status=$?
fi
printf '%s\n' "$output" >&2
echo "== end ==" >&2
if (( rpmkeys_status != 0 )); then
warn "rpmkeys verification failed with exit status $rpmkeys_status."
return 1
fi
while IFS= read -r line; do
line_lc="${line,,}"
if [[ "$line_lc" =~ :[[:space:]]*(bad|not[[:space:]]+ok)([[:space:]]|$) ]]; then
warn "rpmkeys reported a failed verification result."
return 1
fi
if [[ "$line_lc" != *signature* ||
! "$line_lc" =~ (^|[[:space:]])(rsa|dsa|ecdsa|eddsa)(/|[[:space:]]) ||
! "$line_lc" =~ :[[:space:]]*ok([[:space:]]|$) ]]; then
continue
fi
signature_key=""
if [[ "$line_lc" =~ key[[:space:]]fingerprint:[[:space:]]*([[:xdigit:]]+) ]]; then
signature_key="${BASH_REMATCH[1]}"
elif [[ "$line_lc" =~ key[[:space:]]id[[:space:]]*:?[[:space:]]*([[:xdigit:]]+) ]]; then
signature_key="${BASH_REMATCH[1]}"
fi
[[ -n "$signature_key" ]] || continue
if [[ ! "$signature_key" =~ ^([[:xdigit:]]{8}|[[:xdigit:]]{16}|[[:xdigit:]]{32}|[[:xdigit:]]{40}|[[:xdigit:]]{64})$ ]]; then
continue
fi
if signature_key_matches "$key_lc" "$signature_key"; then
has_matching_signature=1
fi
done <<< "$output"
(( has_matching_signature ))
}
signature_key_matches() {
local requested="$1"
local reported="$2"
local requested_len=${#requested}
local reported_len=${#reported}
local requested_key_id
local reported_key_id
[[ "$requested" == "$reported" ]] && return 0
if [[ "$requested_len" =~ ^(32|40|64)$ && "$reported_len" =~ ^(32|40|64)$ ]]; then
return 1
fi
case "$requested_len" in
8 | 16) requested_key_id="$requested" ;;
40) requested_key_id="${requested: -16}" ;;
64) requested_key_id="${requested:0:16}" ;;
*) return 1 ;;
esac
case "$reported_len" in
8 | 16) reported_key_id="$reported" ;;
40) reported_key_id="${reported: -16}" ;;
64) reported_key_id="${reported:0:16}" ;;
*) return 1 ;;
esac
if [[ ${#requested_key_id} -eq 16 && ${#reported_key_id} -eq 8 ]]; then
[[ "$requested_key_id" == *"$reported_key_id" ]]
elif [[ ${#requested_key_id} -eq 8 && ${#reported_key_id} -eq 16 ]]; then
[[ "$reported_key_id" == *"$requested_key_id" ]]
else
[[ "$requested_key_id" == "$reported_key_id" ]]
fi
}
sign_rpm() {
local rpm_file="$1"
info "Signing: $rpm_file"
rpmsign --define '_openpgp_sign sq' --key-id "$GPG_KEY" --rpmv4 --rpmv6 --addsign "$rpm_file"
}
verify_or_retry() {
local rpm_file="$1"
local attempt=1
info "Checking: $rpm_file"
if has_signature "$rpm_file"; then
success "OK: $rpm_file"
return 0
fi
warn "Signature missing or invalid, starting retry: $rpm_file"
while ((attempt <= RETRY)); do
warn "Retry $attempt/$RETRY: $rpm_file"
if ! sign_rpm "$rpm_file"; then
warn "rpmsign returned non-zero, re-checking signature anyway."
fi
if has_signature "$rpm_file"; then
success "OK after retry: $rpm_file"
return 0
fi
((attempt++))
if ((attempt <= RETRY)); then
sleep "$RETRY_SLEEP"
fi
done
error "Signature invalid or missing after $RETRY retries: $rpm_file"
}
main() {
parse_args "$@"
check_dependencies find rpmkeys rpmsign sleep sort sq
if [[ ${#DIRS[@]} -eq 0 ]]; then
error "At least one --dir is required."
fi
if [[ -z "$GPG_KEY" ]]; then
error "Option --gpg-key is required."
fi
if [[ ! "${GPG_KEY,,}" =~ ^([[:xdigit:]]{8}|[[:xdigit:]]{16}|[[:xdigit:]]{32}|[[:xdigit:]]{40}|[[:xdigit:]]{64})$ ]]; then
error "Option --gpg-key must be an OpenPGP key ID or fingerprint in hexadecimal form."
fi
mapfile -t rpm_files < <(collect_rpms)
info "Signing ${#rpm_files[@]} RPM package(s)."
for rpm_file in "${rpm_files[@]}"; do
sign_rpm "$rpm_file"
done
info "Validating RPM signatures."
for rpm_file in "${rpm_files[@]}"; do
verify_or_retry "$rpm_file"
done
success "Signed and verified ${#rpm_files[@]} RPM package(s)."
}
main "$@"