Skip to content

Commit

Permalink
Update test to use agent with multiple API version support
Browse files Browse the repository at this point in the history
Replace the re-compilation of the agent using an older version with a
simple reconfigurion of the agent enabling the desired API version.

Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
  • Loading branch information
ansasaki committed Dec 19, 2024
1 parent 7451be0 commit 60903b9
Showing 1 changed file with 12 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ rlJournalStart

rlPhaseStartSetup "Do the keylime setup"
rlRun 'rlImport "./test-helpers"' || rlDie "cannot import keylime-tests/test-helpers library"
# install recommend devel packages from CRB if missing
rpm -q tpm2-tss-devel 2> /dev/null || INSTALL_PKGS="$INSTALL_PKGS tpm2-tss-devel"
rpm -q libarchive-devel 2> /dev/null || INSTALL_PKGS="$INSTALL_PKGS libarchive-devel"
if ! rpm -q zeromq-devel 2> /dev/null; then
rlIsRHEL '<10' && INSTALL_PKGS="$INSTALL_PKGS zeromq-devel"
fi
rlIsRHEL '<10' && EPEL_ARG="--enablerepo epel" || EPEL_ARG=""
[ -n "$INSTALL_PKGS" ] && rlRun "dnf --enablerepo \*CRB $EPEL_ARG -y install $INSTALL_PKGS"
rlAssertRpm keylime

# update /etc/keylime.conf
limeBackupConfig
# verifier
Expand Down Expand Up @@ -51,50 +42,22 @@ rlJournalStart
WORKDIR=$( mktemp -d -p "/var/tmp" )
rlPhaseEnd

rlPhaseStartTest "Compile keylime agent with old API version"
# Store a backup of the installed binary
rlRun "rlFileBackup --namespace agent /usr/bin/keylime_agent"
# check if I am running agent from RPM file, i.e. not the upstream one
# in this case I am going to use sources from RPM file because
# I need to use the right version and extra patches from SRPM may
# be necessary
if rpm -q keylime-agent-rust && rpm -q --qf '%{VENDOR}' keylime-agent-rust | grep -qv 'Fedora Copr - user packit'; then
rlLogInfo "Will use agent sources from SRPM"
rlFetchSrcForInstalled keylime-agent-rust
rlRun "rpm -i keylime-agent-rust*.src.rpm"
rlRun "dnf -y builddep ~/rpmbuild/SPECS/keylime-agent-rust.spec"
rlRun "rpmbuild -bp ~/rpmbuild/SPECS/keylime-agent-rust.spec --nodeps --define '_builddir $PWD'" 0,1
if ls -d keylime-agent-rust*build; then
rlRun "pushd keylime-agent-rust*build/rust-keylime*"
else
rlRun "rm -rf rust-keylime-*SPECPARTS"
rlRun "pushd rust-keylime*"
fi
else
rlLogInfo "Will use agent sources from upstream repo"
rlRun "git clone ${RUST_KEYLIME_UPSTREAM_URL} ${WORKDIR}/rust-keylime"
rlRun "pushd ${WORKDIR}/rust-keylime"
rlPhaseStartTest "Get agent supported versions"
rlRun "limeStartAgent"
rlRun "limeWaitForAgentRegistration ${AGENT_ID}"
mapfile -t SUPPORTED_VERSIONS< <(grep -h 'Starting server with API version' "$(limeAgentLogfile)" | grep -ohE '[0-9]*\.[0-9]*' | sort -V)
if [[ "${#SUPPORTED_VERSIONS[@]}" -lt 2 ]]; then
rlDie "Agent supports only one API version"
fi
# Get a supported version older than the current
CURRENT_VERSION="$(grep -E '(^.*API_VERSION.*v)([0-9]+\.[0-9]+)' keylime-agent/src/common.rs | grep -o -E '[0-9]+\.[0-9]+')"
OLD_VERSION="$(grep -o -E "Supported older API versions: .*" "$(limeVerifierLogfile)" | grep -o -E '[0-9]+\.[0-9]+' | sed -n "1,/^$CURRENT_VERSION\$/ p" | grep -v "^$CURRENT_VERSION\$" | tail -1)"

# Replace the API version to fake an older version
rlRun "cp keylime-agent/src/common.rs keylime-agent/src/common.rs.backup"
rlRun "sed -i -E \"s/(^.*API_VERSION.*v)([0-9]+\.[0-9]+)/\1$OLD_VERSION/\" keylime-agent/src/common.rs"
rlRun "diff keylime-agent/src/common.rs.backup keylime-agent/src/common.rs" 1
# Replace agent binary
rlRun "cargo build"
rlLog "Agent support versions: ${SUPPORTED_VERSIONS[*]}"
OLD_VERSION=${SUPPORTED_VERSIONS[0]}
LATEST_VERSION=${SUPPORTED_VERSIONS[${#SUPPORTED_VERSIONS[@]} -1]}
rlRun "limeStopAgent"
BUILDDIR=$PWD
rlRun "cp ${BUILDDIR}/target/debug/keylime_agent /usr/bin/keylime_agent"
rlRun "popd"
rlPhaseEnd

rlPhaseStartTest "Add keylime agent with old API version"
rlRun "limeUpdateConf agent api_versions \"\\\"${OLD_VERSION}\\\""
rlRun "limeStartAgent"
rlRun "limeWaitForAgentRegistration ${AGENT_ID}"
rlAssertGrep "Starting server with API version v${OLD_VERSION}" "$(limeAgentLogfile)" -E
rlRun "cat > script.expect <<_EOF
set timeout 20
spawn keylime_tenant -v 127.0.0.1 -t 127.0.0.1 -u $AGENT_ID --verify --runtime-policy policy.json --cert default -c add
Expand All @@ -111,7 +74,7 @@ _EOF"

rlPhaseStartTest "Verify that API version is automatically bumped"
rlRun "limeStopAgent"
rlRun "rlFileRestore --namespace agent"
rlRun "limeUpdateConf agent api_versions \"\\\"${LATEST_VERSION}\\\""
rlRun "limeStartAgent"
rlRun "rlWaitForCmd 'tail \$(limeVerifierLogfile) | grep -q \"Agent $AGENT_ID API version updated\"' -m 10 -d 1 -t 10"
rlRun "limeWaitForAgentStatus $AGENT_ID 'Get Quote'"
Expand All @@ -121,7 +84,7 @@ _EOF"

rlPhaseStartTest "Verify that API version downgrade is not allowed"
rlRun "limeStopAgent"
rlRun "cp ${BUILDDIR}/target/debug/keylime_agent /usr/bin/keylime_agent"
rlRun "limeUpdateConf agent api_versions \"\\\"${OLD_VERSION}\\\""
rlRun "limeStartAgent"
rlRun "limeWaitForAgentStatus $AGENT_ID '(Failed|Invalid Quote)'"
rlAssertGrep "WARNING - Agent $AGENT_ID API version $OLD_VERSION is lower or equal to previous version" "$(limeVerifierLogfile)"
Expand All @@ -130,7 +93,6 @@ _EOF"

rlPhaseStartCleanup "Do the keylime cleanup"
rlRun "limeStopAgent"
rlRun "rlFileRestore --namespace agent"
rlRun "limeStopRegistrar"
rlRun "limeStopVerifier"
if limeTPMEmulated; then
Expand Down

0 comments on commit 60903b9

Please sign in to comment.