#!/usr/bin/env bash # Runs inside the apk image (build/Dockerfile.apk), started by scripts/release.sh. # Adds the .apk files of /dist to /site/apk///, keeps the newest KEEP versions of each # package, signs new packages and the index with the key in /keys (EC or RSA). # Mounts: /site (the gh-pages tree, read-write), /keys (r4ven-apk.pem, read-only), /dist (read-only). set -Eeuo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd -P) SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" # shellcheck source=scripts/lib.sh source "${SCRIPT_DIR}/lib.sh" SITE_DIR="${SITE_DIR:-/site}" DIST_DIR="${DIST_DIR:-/dist}" KEY_FILE="${KEY_FILE:-/keys/r4ven-apk.pem}" PUBLIC_KEY="${PUBLIC_KEY:-${SITE_DIR}/apk/r4ven-apk.pub}" APK_RELEASE="${APK_RELEASE:-25.12}" APK_ARCH="${APK_ARCH:-x86_64}" KEEP="${KEEP:-3}" TAB=$'\t' usage() { cat << EOF Usage: ${SCRIPT_NAME} Runs in the apk container. Reads ${KEY_FILE} and the .apk files of ${DIST_DIR}, updates ${SITE_DIR}/apk/${APK_RELEASE}/${APK_ARCH}/ (newest ${KEEP} versions per package) and signs the packages and packages.adb. Writes ${SITE_DIR}/apk/catalog.json for the landing page. EOF } if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 fi WORK_DIR="" cleanup() { trap - SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT # A subshell (process substitution, pipeline) inherits the traps; only the main shell may remove the work dir. [[ "$BASHPID" == "$$" ]] || return 0 [[ -n "$WORK_DIR" && -d "$WORK_DIR" ]] && rm -rf "$WORK_DIR" return 0 } # shellcheck disable=SC2154 # rc is assigned inside the trap string itself trap 'rc=$?; cleanup; exit $rc' SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT [[ -f "$KEY_FILE" ]] || log_error "Private key ${KEY_FILE} not found" [[ -f "$PUBLIC_KEY" ]] || log_error "Public key ${PUBLIC_KEY} not found" [[ "$APK_RELEASE" =~ ^[0-9A-Za-z._-]+$ && "$APK_ARCH" =~ ^[0-9A-Za-z_]+$ ]] \ || log_error "Invalid APK_RELEASE or APK_ARCH" WORK_DIR=$(mktemp -d) # The private key must belong to the published public key. derived=$(openssl pkey -in "$KEY_FILE" -pubout) || log_error "Cannot read the private key ${KEY_FILE}" published=$(openssl pkey -pubin -in "$PUBLIC_KEY" -pubout) || log_error "Cannot read the public key ${PUBLIC_KEY}" [[ -n "$derived" && "$derived" == "$published" ]] \ || log_error "The private key does not match the public key ${PUBLIC_KEY}" trusted="${WORK_DIR}/trusted" mkdir "$trusted" cp "$PUBLIC_KEY" "${trusted}/r4ven-apk.pub" repo_dir="${SITE_DIR}/apk/${APK_RELEASE}/${APK_ARCH}" mkdir -p "$repo_dir" shopt -s nullglob # 1. New packages: check the name, sign. for f in "$DIST_DIR"/*.apk; do name=$(basename "$f") [[ "$name" =~ ^[A-Za-z0-9][A-Za-z0-9+._-]*\.apk$ ]] || log_error "Invalid package file name: ${name}" install -m644 "$f" "${repo_dir}/${name}" apk adbsign --allow-untrusted --reset-signatures --sign-key "$KEY_FILE" "${repo_dir}/${name}" log_info "Added and signed ${name}" done # 2. Drop the old versions: the name is the part before the first "-", as in the OpenWrt feeds. for f in "$repo_dir"/*.apk; do base=$(basename "$f") printf '%s\t%s\n' "${base%%-[0-9]*}" "$base" done | sort -t "$TAB" -k1,1 -k2,2Vr \ | awk -F'\t' -v keep="$KEEP" '++n[$1] > keep {print $2}' \ | while IFS= read -r old; do log_info "Removed old version: ${old}" rm -f -- "${repo_dir}/${old}" "${repo_dir}/${old}.sha256" done # 3. The signed index, checked the way a router does: only the published public key. files=("$repo_dir"/*.apk) [[ "${#files[@]}" -gt 0 ]] || log_error "No packages in ${repo_dir}" # A checksum file next to every package (published bytes, after signing), for those who download by hand. for f in "${files[@]}"; do (cd "$repo_dir" && sha256sum "$(basename "$f")" > "$(basename "$f").sha256") done rm -f "${repo_dir}/packages.adb" (cd "$repo_dir" && apk mkndx --allow-untrusted --output packages.adb ./*.apk) apk adbsign --allow-untrusted --reset-signatures --sign-key "$KEY_FILE" "${repo_dir}/packages.adb" apk --keys-dir "$trusted" --no-network verify "${files[@]}" > /dev/null \ || log_error "A package does not verify with the published key" search=$(apk --keys-dir "$trusted" --no-network --repository "${repo_dir}/packages.adb" search -v) \ || log_error "The index does not verify with the published key" log_info "apk ${APK_RELEASE}/${APK_ARCH}: $(wc -l <<< "$search") package(s), signed and verified" # 4. The catalog of the apk packages for the landing page (merged with catalog.json by gen_index.py). # The apk files do not hold the project address: the packages of this release get APK_PROJECT_URL, the others keep # the address the previous catalog gave them. prev_urls='{}' if [[ -f "${SITE_DIR}/apk/catalog.json" ]]; then prev_urls=$(jq -c '[.packages[] | select(.url != "") | {key: .file, value: .url}] | from_entries' "${SITE_DIR}/apk/catalog.json") fi while IFS= read -r line; do left="${line%% - *}" summary="${line#* - }" name="${left%%-[0-9]*}" version="${left#"${name}"-}" file="apk/${APK_RELEASE}/${APK_ARCH}/${left}.apk" size=$(stat -c %s "${SITE_DIR}/${file}") url="" [[ ! -f "/dist/${left}.apk" ]] || url="${APK_PROJECT_URL:-}" printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$name" "$version" "$APK_ARCH" "$file" "$size" "$summary" "$url" done <<< "$search" | jq -R -s --argjson prev "$prev_urls" ' {packages: [split("\n")[] | select(length > 0) | split("\t") | {format: "apk", name: .[0], version: .[1], arch: .[2], file: .[3], size: (.[4] | tonumber), summary: .[5], url: (if .[6] != "" then .[6] else ($prev[.[3]] // "") end)}]}' \ > "${SITE_DIR}/apk/catalog.json" log_info "Done"