Blame tests/test-signed-pull-summary.sh

Packit Service 2a3f3d
#!/bin/bash
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# Copyright (C) 2019 Collabora Ltd.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# SPDX-License-Identifier: LGPL-2.0+
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# This library is free software; you can redistribute it and/or
Packit Service 2a3f3d
# modify it under the terms of the GNU Lesser General Public
Packit Service 2a3f3d
# License as published by the Free Software Foundation; either
Packit Service 2a3f3d
# version 2 of the License, or (at your option) any later version.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# This library is distributed in the hope that it will be useful,
Packit Service 2a3f3d
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2a3f3d
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2a3f3d
# Lesser General Public License for more details.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# You should have received a copy of the GNU Lesser General Public
Packit Service 2a3f3d
# License along with this library; if not, write to the
Packit Service 2a3f3d
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service 2a3f3d
# Boston, MA 02111-1307, USA.
Packit Service 2a3f3d
Packit Service 2a3f3d
# Based on test-pull-summary-sigs.sh test.
Packit Service 2a3f3d
Packit Service 2a3f3d
set -euo pipefail
Packit Service 2a3f3d
Packit Service 2a3f3d
. $(dirname $0)/libtest.sh
Packit Service 2a3f3d
Packit Service 2a3f3d
echo "1..14"
Packit Service 2a3f3d
Packit Service 2a3f3d
# This is explicitly opt in for testing
Packit Service 2a3f3d
export OSTREE_DUMMY_SIGN_ENABLED=1
Packit Service 2a3f3d
Packit Service 2a3f3d
repo_reinit () {
Packit Service 2a3f3d
    ARGS="$*"
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    rm -rf repo
Packit Service 2a3f3d
    mkdir repo
Packit Service 2a3f3d
    ostree_repo_init repo --mode=archive
Packit Service 2a3f3d
    ${OSTREE} --repo=repo remote add \
Packit Service 2a3f3d
        --set=gpg-verify=false --set=gpg-verify-summary=false \
Packit Service 2a3f3d
        --set=sign-verify=false --set=sign-verify-summary=true \
Packit Service 2a3f3d
        ${ARGS} origin $(cat httpd-address)/ostree/gnomerepo
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
for engine in dummy ed25519
Packit Service 2a3f3d
do
Packit Service 2a3f3d
    case "${engine}" in
Packit Service 2a3f3d
        dummy)
Packit Service 2a3f3d
            # Tests with dummy engine
Packit Service 2a3f3d
            SIGN_KEY="dummysign"
Packit Service 2a3f3d
            PUBLIC_KEY="dummysign"
Packit Service 2a3f3d
            ;;
Packit Service 2a3f3d
        ed25519)
Packit Service 2a3f3d
            if ! has_sign_ed25519; then
Packit Service 2a3f3d
                echo "ok ${engine} pull mirror summary # SKIP due libsodium unavailability"
Packit Service 2a3f3d
                echo "ok ${engine} pull with signed summary # SKIP due libsodium unavailability"
Packit Service 2a3f3d
                echo "ok ${engine} prune summary cache # SKIP due libsodium unavailability"
Packit Service 2a3f3d
                echo "ok ${engine} pull with signed summary and cachedir # SKIP due libsodium unavailability"
Packit Service 2a3f3d
                echo "ok ${engine} pull with invalid ${engine} summary signature fails # SKIP due libsodium unavailability"
Packit Service 2a3f3d
                echo "ok ${engine} pull delta with signed summary # SKIP due libsodium unavailability"
Packit Service 2a3f3d
Packit Service 2a3f3d
                continue
Packit Service 2a3f3d
            fi
Packit Service 2a3f3d
            gen_ed25519_keys
Packit Service 2a3f3d
            SIGN_KEY="${ED25519SECRET}"
Packit Service 2a3f3d
            PUBLIC_KEY="${ED25519PUBLIC}"
Packit Service 2a3f3d
            ;;
Packit Service 2a3f3d
        *)
Packit Service 2a3f3d
            fatal "Unsupported engine ${engine}"
Packit Service 2a3f3d
            ;;
Packit Service 2a3f3d
    esac
Packit Service 2a3f3d
Packit Service 2a3f3d
    COMMIT_SIGN="--sign-type=${engine} --sign=${SIGN_KEY}"
Packit Service 2a3f3d
Packit Service 2a3f3d
    # clenup the testdir prior the next engine
Packit Service 2a3f3d
    rm -rf ${test_tmpdir}/ostree-srv ${test_tmpdir}/gnomerepo ${test_tmpdir}/httpd ${test_tmpdir}/repo ${test_tmpdir}/cachedir\
Packit Service 2a3f3d
        ${test_tmpdir}/main-copy ${test_tmpdir}/other-copy ${test_tmpdir}/yet-another-copy 
Packit Service 2a3f3d
Packit Service 2a3f3d
    setup_fake_remote_repo1 "archive" "${COMMIT_SIGN}"
Packit Service 2a3f3d
Packit Service 2a3f3d
    # Now, setup multiple branches
Packit Service 2a3f3d
    mkdir ${test_tmpdir}/ostree-srv/other-files
Packit Service 2a3f3d
    cd ${test_tmpdir}/ostree-srv/other-files
Packit Service 2a3f3d
    echo 'hello world another object' > hello-world
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree  --repo=${test_tmpdir}/ostree-srv/gnomerepo commit ${COMMIT_SIGN} -b other -s "A commit" -m "Another Commit body"
Packit Service 2a3f3d
Packit Service 2a3f3d
    mkdir ${test_tmpdir}/ostree-srv/yet-other-files
Packit Service 2a3f3d
    cd ${test_tmpdir}/ostree-srv/yet-other-files
Packit Service 2a3f3d
    echo 'hello world yet another object' > yet-another-hello-world
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree  --repo=${test_tmpdir}/ostree-srv/gnomerepo commit ${COMMIT_SIGN} -b yet-another -s "A commit" -m "Another Commit body"
Packit Service 2a3f3d
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u
Packit Service 2a3f3d
Packit Service 2a3f3d
    prev_dir=`pwd`
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    ostree_repo_init repo --mode=archive
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo remote add \
Packit Service 2a3f3d
        --set=gpg-verify=false --set=gpg-verify-summary=false \
Packit Service 2a3f3d
        --set=sign-verify=false --set=sign-verify-summary=false \
Packit Service 2a3f3d
        origin $(cat httpd-address)/ostree/gnomerepo
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo pull --mirror origin
Packit Service 2a3f3d
    assert_has_file repo/summary
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo checkout -U main main-copy
Packit Service 2a3f3d
    assert_file_has_content main-copy/baz/cow "moo"
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo checkout -U other other-copy
Packit Service 2a3f3d
    assert_file_has_content other-copy/hello-world "hello world another object"
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo checkout -U yet-another yet-another-copy
Packit Service 2a3f3d
    assert_file_has_content yet-another-copy/yet-another-hello-world "hello world yet another object"
Packit Service 2a3f3d
    ${CMD_PREFIX} ostree --repo=repo fsck
Packit Service 2a3f3d
    echo "ok ${engine} pull mirror summary"
Packit Service 2a3f3d
Packit Service 2a3f3d
Packit Service 2a3f3d
    cd $prev_dir
Packit Service 2a3f3d
Packit Service 2a3f3d
    ${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u ${COMMIT_SIGN}
Packit Service 2a3f3d
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    repo_reinit --set=verification-${engine}-key=${PUBLIC_KEY}
Packit Service 2a3f3d
    ${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
    assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
    assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
Packit Service 2a3f3d
    rm repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
    ${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
    assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
Packit Service 2a3f3d
    echo "ok ${engine} pull with signed summary"
Packit Service 2a3f3d
Packit Service 2a3f3d
    touch repo/tmp/cache/summaries/foo
Packit Service 2a3f3d
    touch repo/tmp/cache/summaries/foo.sig
Packit Service 2a3f3d
    ${OSTREE} --repo=repo prune
Packit Service 2a3f3d
    assert_not_has_file repo/tmp/cache/summaries/foo
Packit Service 2a3f3d
    assert_not_has_file repo/tmp/cache/summaries/foo.sig
Packit Service 2a3f3d
    assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
    assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
    echo "ok ${engine} prune summary cache"
Packit Service 2a3f3d
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    repo_reinit --set=verification-${engine}-key=${PUBLIC_KEY}
Packit Service 2a3f3d
    mkdir cachedir
Packit Service 2a3f3d
    ${OSTREE} --repo=repo pull --cache-dir=cachedir origin main
Packit Service 2a3f3d
    assert_not_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
    assert_not_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
    assert_has_file cachedir/summaries/origin
Packit Service 2a3f3d
    assert_has_file cachedir/summaries/origin.sig
Packit Service 2a3f3d
Packit Service 2a3f3d
    rm cachedir/summaries/origin
Packit Service 2a3f3d
    ${OSTREE} --repo=repo pull --cache-dir=cachedir origin main
Packit Service 2a3f3d
    assert_not_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
    assert_has_file cachedir/summaries/origin
Packit Service 2a3f3d
Packit Service 2a3f3d
    echo "ok ${engine} pull with signed summary and cachedir"
Packit Service 2a3f3d
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    repo_reinit --set=verification-${engine}-key=${PUBLIC_KEY}
Packit Service 2a3f3d
    mv ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{,.good}
Packit Service 2a3f3d
    echo invalid > ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig
Packit Service 2a3f3d
    if ${OSTREE} --repo=repo pull origin main 2>err.txt; then
Packit Service 2a3f3d
        assert_not_reached "Successful pull with invalid ${engine} signature"
Packit Service 2a3f3d
    fi
Packit Service 2a3f3d
    assert_file_has_content err.txt "No signatures found"
Packit Service 2a3f3d
    mv ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{.good,}
Packit Service 2a3f3d
    echo "ok ${engine} pull with invalid ${engine} summary signature fails"
Packit Service 2a3f3d
Packit Service 2a3f3d
    # Generate a delta
Packit Service 2a3f3d
    ${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo static-delta generate --empty main
Packit Service 2a3f3d
    ${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u ${COMMIT_SIGN}
Packit Service 2a3f3d
Packit Service 2a3f3d
    cd ${test_tmpdir}
Packit Service 2a3f3d
    repo_reinit --set=verification-${engine}-key=${PUBLIC_KEY}
Packit Service 2a3f3d
    ${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
    echo "ok ${engine} pull delta with signed summary"
Packit Service 2a3f3d
Packit Service 2a3f3d
done
Packit Service 2a3f3d
Packit Service 2a3f3d
if ! has_sign_ed25519; then
Packit Service 2a3f3d
    echo "ok ${engine} pull with signed summary remote old summary # SKIP due libsodium unavailability"
Packit Service 2a3f3d
    echo "ok ${engine} pull with signed summary broken cache # SKIP due libsodium unavailability"
Packit Service 2a3f3d
    exit 0
Packit Service 2a3f3d
fi
Packit Service 2a3f3d
Packit Service 2a3f3d
gen_ed25519_keys
Packit Service 2a3f3d
SIGN_KEY="${ED25519SECRET}"
Packit Service 2a3f3d
PUBLIC_KEY="${ED25519PUBLIC}"
Packit Service 2a3f3d
COMMIT_SIGN="--sign-type=${engine} --sign=${SIGN_KEY}"
Packit Service 2a3f3d
Packit Service 2a3f3d
Packit Service 2a3f3d
# Verify 'ostree remote summary' output.
Packit Service 2a3f3d
${OSTREE} --repo=repo remote summary origin > summary.txt
Packit Service 2a3f3d
assert_file_has_content summary.txt "* main"
Packit Service 2a3f3d
assert_file_has_content summary.txt "* other"
Packit Service 2a3f3d
assert_file_has_content summary.txt "* yet-another"
Packit Service 2a3f3d
grep static-deltas summary.txt > static-deltas.txt
Packit Service 2a3f3d
assert_file_has_content static-deltas.txt \
Packit Service 2a3f3d
    $(${OSTREE} --repo=repo rev-parse origin:main)
Packit Service 2a3f3d
Packit Service 2a3f3d
## Tests for handling of cached summaries while racing with remote summary updates
Packit Service 2a3f3d
Packit Service 2a3f3d
# Make 2 different but valid summary/signature pairs to test races with
Packit Service 2a3f3d
${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u ${COMMIT_SIGN}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{,.1}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{,.1}
Packit Service 2a3f3d
mkdir ${test_tmpdir}/ostree-srv/even-another-files
Packit Service 2a3f3d
cd ${test_tmpdir}/ostree-srv/even-another-files
Packit Service 2a3f3d
echo 'hello world even another object' > even-another-hello-world
Packit Service 2a3f3d
${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo commit ${COMMIT_SIGN} -b even-another -s "A commit" -m "Another Commit body"
Packit Service 2a3f3d
${OSTREE} --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u ${COMMIT_SIGN}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{,.2}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{,.2}
Packit Service 2a3f3d
cd ${test_tmpdir}
Packit Service 2a3f3d
Packit Service 2a3f3d
# Reset to the old valid summary and pull to cache it
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{.1,}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{.1,}
Packit Service 2a3f3d
repo_reinit --set=verification-ed25519-key=${PUBLIC_KEY}
Packit Service 2a3f3d
${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.1 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.1 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
# Simulate a pull race where the client gets the old summary and the new
Packit Service 2a3f3d
# summary signature since it was generated on the server between the
Packit Service 2a3f3d
# requests
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{.2,}
Packit Service 2a3f3d
if ${OSTREE} --repo=repo pull origin main 2>err.txt; then
Packit Service 2a3f3d
    assert_not_reached "Successful pull with old summary"
Packit Service 2a3f3d
fi
Packit Service 2a3f3d
assert_file_has_content err.txt "ed25519: Signature couldn't be verified with: key"
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.1 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.1 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
# Publish correct summary and check that subsequent pull succeeds
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{.2,}
Packit Service 2a3f3d
${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.2 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.2 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
echo "ok ${engine} pull with signed summary remote old summary"
Packit Service 2a3f3d
Packit Service 2a3f3d
Packit Service 2a3f3d
# Reset to the old valid summary and pull to cache it
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{.1,}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{.1,}
Packit Service 2a3f3d
repo_reinit --set=verification-ed25519-key=${PUBLIC_KEY}
Packit Service 2a3f3d
${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.1 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.1 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
# Simulate a broken summary cache to see if it can be recovered from.
Packit Service 2a3f3d
# Prior to commit c4c2b5eb the client would save the summary to the
Packit Service 2a3f3d
# cache before validating the signature. That would mean the cache would
Packit Service 2a3f3d
# have mismatched summary and signature and ostree would remain
Packit Service 2a3f3d
# deadlocked there until the remote published a new signature.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# First pull with OSTREE_REPO_TEST_ERROR=invalid-cache to see the
Packit Service 2a3f3d
# invalid cache is detected. Then pull again to check if it can be
Packit Service 2a3f3d
# recovered from.
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.2 repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
if OSTREE_REPO_TEST_ERROR=invalid-cache ${OSTREE} --repo=repo pull origin main 2>err.txt; then
Packit Service 2a3f3d
    assert_not_reached "Should have hit OSTREE_REPO_TEST_ERROR_INVALID_CACHE"
Packit Service 2a3f3d
fi
Packit Service 2a3f3d
assert_file_has_content err.txt "OSTREE_REPO_TEST_ERROR_INVALID_CACHE"
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.2 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.1 >&2
Packit Service 2a3f3d
${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.1 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.1 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
# Publish new signature and check that subsequent pull succeeds
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary{.2,}
Packit Service 2a3f3d
cp ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig{.2,}
Packit Service 2a3f3d
${OSTREE} --repo=repo pull origin main
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin
Packit Service 2a3f3d
assert_has_file repo/tmp/cache/summaries/origin.sig
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin ${test_tmpdir}/ostree-srv/gnomerepo/summary.2 >&2
Packit Service 2a3f3d
cmp repo/tmp/cache/summaries/origin.sig ${test_tmpdir}/ostree-srv/gnomerepo/summary.sig.2 >&2
Packit Service 2a3f3d
Packit Service 2a3f3d
echo "ok ${engine} pull with signed summary broken cache"
Packit Service 2a3f3d