Skip to content

Commit

Permalink
cleanup test scripts
Browse files Browse the repository at this point in the history
- use neic crypt4gh
- do not use pip
- silent curls
  • Loading branch information
aaperis committed Dec 5, 2024
1 parent b0aa964 commit 73baacd
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

# Build containers
docker build -t neicnordic/sda-download:latest . || exit 1
Expand Down
1 change: 1 addition & 0 deletions sda-download/.github/integration/setup/common/1_keys.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

cd dev_utils || exit 1

Expand Down
6 changes: 5 additions & 1 deletion sda-download/.github/integration/setup/common/20_tools.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash
set -e

C4GH_VERSION="$(curl --retry 100 -sL https://api.github.com/repos/neicnordic/crypt4gh/releases/latest | jq -r '.name')"
curl --retry 100 -sL https://github.com/neicnordic/crypt4gh/releases/download/"${C4GH_VERSION}"/crypt4gh_linux_x86_64.tar.gz | sudo tar -xz -C /usr/bin/ &&
sudo chmod +x /usr/bin/crypt4gh

pip3 install crypt4gh
sudo apt install -y jq s3cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

cd dev_utils || exit 1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash

pip3 install s3cmd
set -e

cd dev_utils || exit 1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
set -e

pip3 install s3cmd
sudo apt install -y s3cmd

cd dev_utils || exit 1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

cd dev_utils || exit 1

Expand Down
19 changes: 9 additions & 10 deletions sda-download/.github/integration/tests/common/50_check_endpoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ echo "got correct response when POST method used"
# ------------------
# Test good token

token=$(curl --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')
token=$(curl -s --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')

## Test datasets endpoint

check_dataset=$(curl --cacert certs/ca.pem -H "Authorization: Bearer $token" https://localhost:8443/metadata/datasets | jq -r '.[0]')
check_dataset=$(curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" https://localhost:8443/metadata/datasets | jq -r '.[0]')

if [ "$check_dataset" != "https://doi.example/ty009.sfrrss/600.45asasga" ]; then
echo "dataset https://doi.example/ty009.sfrrss/600.45asasga not found"
Expand All @@ -72,7 +72,7 @@ echo "expected dataset found"

## Test datasets/files endpoint

check_files=$(curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/metadata/datasets/https://doi.example/ty009.sfrrss/600.45asasga/files" | jq -r '.[0].fileId')
check_files=$(curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/metadata/datasets/https://doi.example/ty009.sfrrss/600.45asasga/files" | jq -r '.[0].fileId')

if [ "$check_files" != "urn:neic:001-002" ]; then
echo "file with id urn:neic:001-002 not found"
Expand All @@ -88,10 +88,9 @@ echo "expected file found"
C4GH_PASSPHRASE=$(grep -F passphrase config.yaml | sed -e 's/.* //' -e 's/"//g')
export C4GH_PASSPHRASE

crypt4gh decrypt --sk c4gh.sec.pem < dummy_data.c4gh > old-file.txt

curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002" --output test-download.txt
crypt4gh decrypt -s c4gh.sec.pem -f dummy_data.c4gh && mv dummy_data old-file.txt

curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002" --output test-download.txt

cmp --silent old-file.txt test-download.txt
status=$?
Expand All @@ -102,7 +101,7 @@ else
exit 1
fi

curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=0&endCoordinate=2" --output test-part.txt
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=0&endCoordinate=2" --output test-part.txt

dd if=old-file.txt ibs=1 skip=0 count=2 > old-part.txt

Expand All @@ -115,7 +114,7 @@ else
exit 1
fi

curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=7&endCoordinate=14" --output test-part2.txt
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=7&endCoordinate=14" --output test-part2.txt

dd if=old-file.txt ibs=1 skip=7 count=7 > old-part2.txt

Expand All @@ -128,7 +127,7 @@ else
exit 1
fi

curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=70000&endCoordinate=140000" --output test-part3.txt
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/files/urn:neic:001-002?startCoordinate=70000&endCoordinate=140000" --output test-part3.txt

dd if=old-file.txt ibs=1 skip=70000 count=70000 > old-part3.txt

Expand Down Expand Up @@ -162,7 +161,7 @@ echo "got correct response when token has no permissions"
# Test token with untrusted sources
# for this test we attach a list of trusted sources

token=$(curl --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[2]')
token=$(curl -s --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[2]')

## Test datasets endpoint

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash


TLS="True"
[[ "$STORAGETYPE" == "s3notls" ]] && TLS="False"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ fi
cd dev_utils || exit 1

# get a token, set up variables
token=$(curl --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')
token=$(curl -s --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')
dataset="https://doi.example/ty009.sfrrss/600.45asasga"
file="dummy_data"
expected_size=1048605
C4GH_PASSPHRASE=$(grep -F passphrase config.yaml | sed -e 's/.* //' -e 's/"//g')
export C4GH_PASSPHRASE

# download decrypted full file, check file size
curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file" --output full1.bam
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file" --output full1.bam
file_size=$(stat -c %s full1.bam) # Get the size of the file

if [ "$file_size" -ne "$expected_size" ]; then
Expand All @@ -24,9 +24,11 @@ if [ "$file_size" -ne "$expected_size" ]; then
fi

# test that start, end=0 returns the whole file
curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file?startCoordinate=0&endCoordinate=0" --output full2.bam
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file?startCoordinate=0&endCoordinate=0" --output full2.bam

if ! cmp --silent full1.bam full2.bam; then
echo "Full decrypted files, with and without coordinates, are different"
exit 1
fi

echo "OK"
28 changes: 12 additions & 16 deletions sda-download/.github/integration/tests/common/80_check_reencrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fi
cd dev_utils || exit 1

# Get a token, set up variables
token=$(curl --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')
token=$(curl -s --cacert certs/ca.pem "https://localhost:8000/tokens" | jq -r '.[0]')

if [ -z "$token" ]; then
echo "Failed to obtain token"
Expand All @@ -19,15 +19,9 @@ file="dummy_data"
expected_size=1048605

# Download unencrypted full file, check file size
curl --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file" --output full1.bam

if [ ! -f "full1.bam" ]; then
echo "Failed to download full1.bam"
exit 1
fi
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" "https://localhost:8443/s3/$dataset/$file" --output full1.bam

file_size=$(stat -c %s full1.bam) # Get the size of the file

if [ "$file_size" -ne "$expected_size" ]; then
echo "Incorrect file size for downloaded file"
exit 1
Expand All @@ -36,11 +30,7 @@ fi
# Test reencrypt the file header with the client public key
clientkey=$(base64 -w0 client.pub.pem)
reencryptedFile=reencrypted.bam.c4gh
curl --cacert certs/ca.pem -H "Authorization: Bearer $token" -H "Client-Public-Key: $clientkey" "https://localhost:8443/s3-encrypted/$dataset/$file" --output $reencryptedFile
if [ ! -f "$reencryptedFile" ]; then
echo "Failed to download re-encrypted file"
exit 1
fi
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" -H "Client-Public-Key: $clientkey" "https://localhost:8443/s3-encrypted/$dataset/$file" --output $reencryptedFile

expected_encrypted_size=1049205
file_size=$(stat -c %s $reencryptedFile)
Expand All @@ -51,10 +41,13 @@ fi

# Decrypt the reencrypted file and compare it with the original unencrypted file
export C4GH_PASSPHRASE="strongpass" # passphrase for the client crypt4gh key
if ! crypt4gh decrypt --sk client.sec.pem < $reencryptedFile > full2.bam; then
crypt4gh decrypt -s client.sec.pem -f $reencryptedFile
if [ ! -f "${reencryptedFile%.c4gh}" ] ; then
echo "Failed to decrypt re-encrypted file with the client's private key"
exit 1
fi
mv "${reencryptedFile%.c4gh}" full2.bam


if ! cmp --silent full1.bam full2.bam; then
echo "Decrypted version of $reencryptedFile and the original unencrypted file, are different"
Expand All @@ -63,7 +56,7 @@ fi

# download reencrypted partial file, check file size
partReencryptedFile=part1.bam.c4gh
curl --cacert certs/ca.pem -H "Authorization: Bearer $token" -H "Client-Public-Key: $clientkey" "https://localhost:8443/s3-encrypted/$dataset/$file?startCoordinate=0&endCoordinate=1000" --output $partReencryptedFile
curl -s --cacert certs/ca.pem -H "Authorization: Bearer $token" -H "Client-Public-Key: $clientkey" "https://localhost:8443/s3-encrypted/$dataset/$file?startCoordinate=0&endCoordinate=1000" --output $partReencryptedFile
file_size=$(stat -c %s $partReencryptedFile) # Get the size of the file
part_expected_size=65688

Expand All @@ -72,7 +65,8 @@ if [ "$file_size" -ne "$part_expected_size" ]; then
exit 1
fi

if ! crypt4gh decrypt --sk client.sec.pem < $partReencryptedFile > part1.bam; then
crypt4gh decrypt -s client.sec.pem -f $partReencryptedFile
if [ ! -f "${partReencryptedFile%.c4gh}" ] ; then
echo "Re-encrypted partial file could not be decrypted"
exit 1
fi
Expand Down Expand Up @@ -106,3 +100,5 @@ resp=$(curl --cacert certs/ca.pem -H "Authorization: Bearer $token" -H "Client-P
if [ "$resp" -ne 500 ]; then
echo "Incorrect response with missing public key, expected 500 got $resp"
fi

echo "OK"
20 changes: 10 additions & 10 deletions sda-download/.github/integration/tests/s3notls/52_check_endpoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ echo "got correct response when POST method used"
# ------------------
# Test good token

token=$(curl "http://localhost:8000/tokens" | jq -r '.[0]')
token=$(curl -s "http://localhost:8000/tokens" | jq -r '.[0]')

## Test datasets endpoint

check_dataset=$(curl -H "Authorization: Bearer $token" http://localhost:8080/metadata/datasets | jq -r '.[0]')
check_dataset=$(curl -s -H "Authorization: Bearer $token" http://localhost:8080/metadata/datasets | jq -r '.[0]')

if [ "$check_dataset" != "https://doi.example/ty009.sfrrss/600.45asasga" ]; then
echo "dataset https://doi.example/ty009.sfrrss/600.45asasga not found"
Expand All @@ -57,7 +57,7 @@ echo "expected dataset found"

## Test datasets/files endpoint

check_files=$(curl -H "Authorization: Bearer $token" "http://localhost:8080/metadata/datasets/https://doi.example/ty009.sfrrss/600.45asasga/files" | jq -r '.[0].fileId')
check_files=$(curl -s -H "Authorization: Bearer $token" "http://localhost:8080/metadata/datasets/https://doi.example/ty009.sfrrss/600.45asasga/files" | jq -r '.[0].fileId')

if [ "$check_files" != "urn:neic:001-002" ]; then
echo "file with id urn:neic:001-002 not found"
Expand All @@ -73,9 +73,9 @@ echo "expected file found"
C4GH_PASSPHRASE=$(grep -F passphrase config.yaml | sed -e 's/.* //' -e 's/"//g')
export C4GH_PASSPHRASE

crypt4gh decrypt --sk c4gh.sec.pem < dummy_data.c4gh > old-file.txt
crypt4gh decrypt -s c4gh.sec.pem -f dummy_data.c4gh && mv dummy_data old-file.txt

curl -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002" --output test-download.txt
curl -s -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002" --output test-download.txt


cmp --silent old-file.txt test-download.txt
Expand All @@ -86,7 +86,7 @@ else
echo "Files are different"
fi

curl -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002?startCoordinate=0&endCoordinate=2" --output test-part.txt
curl -s -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002?startCoordinate=0&endCoordinate=2" --output test-part.txt

dd if=old-file.txt ibs=1 skip=0 count=2 > old-part.txt

Expand All @@ -99,7 +99,7 @@ else
exit 1
fi

curl -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002?startCoordinate=7&endCoordinate=14" --output test-part2.txt
curl -s -H "Authorization: Bearer $token" "http://localhost:8080/files/urn:neic:001-002?startCoordinate=7&endCoordinate=14" --output test-part2.txt

dd if=old-file.txt ibs=1 skip=7 count=7 > old-part2.txt

Expand All @@ -115,7 +115,7 @@ fi
# ------------------
# Test get visas failed

token=$(curl "http://localhost:8000/tokens" | jq -r '.[1]')
token=$(curl -s "http://localhost:8000/tokens" | jq -r '.[1]')

## Test datasets endpoint

Expand All @@ -133,11 +133,11 @@ echo "got correct response when token has no permissions"
# Test token with untrusted sources
# for this test we don't attach a list of trusted sources

token=$(curl "http://localhost:8000/tokens" | jq -r '.[2]')
token=$(curl -s "http://localhost:8000/tokens" | jq -r '.[2]')

## Test datasets endpoint

check_dataset=$(curl -H "Authorization: Bearer $token" http://localhost:8080/metadata/datasets | jq -r '.[0]')
check_dataset=$(curl -s -H "Authorization: Bearer $token" http://localhost:8080/metadata/datasets | jq -r '.[0]')

if [ "$check_dataset" != "https://doi.example/ty009.sfrrss/600.45asasga" ]; then
echo "dataset https://doi.example/ty009.sfrrss/600.45asasga not found"
Expand Down

0 comments on commit 73baacd

Please sign in to comment.