Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix #35

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/test-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ jobs:
echo "Testing removing subscription"
# test deleting subscriptions
docker exec subscriber bash -c "source /home/wis2downloader/.venv/bin/activate && wis2downloader remove-subscription --topic cache/a/wis2/+/services/#"
# clean up, remove test download
docker exec subscriber bash -c "rm \"./app/data/downloads/$(date +'%Y')/$(date +'%m')/$(date +'%d')/cache/a/wis2/my-centre/services/downloader/openapi.bin\""
- name: Run API tests
working-directory: docker/tests
run: |
Expand All @@ -71,12 +73,14 @@ jobs:
# publish a test message
docker exec publisher pywis-pubsub publish --topic cache/a/wis2/my-centre/services/downloader \
--config /pywis-pubsub/config/config.yml \
-i test -u "http://subscriber:5000/metrics"
-i test -u "http://subscriber:5000/openapi"
sleep 1s
# cat file contents (check the published file has been downloaded)
cat "./data/$(date +'%Y')/$(date +'%m')/$(date +'%d')/cache/a/wis2/my-centre/services/downloader/metrics.bin"
cat "./data/$(date +'%Y')/$(date +'%m')/$(date +'%d')/cache/a/wis2/my-centre/services/downloader/openapi.bin"
# test deleting subscriptions
curl -X DELETE http://localhost:5000/subscriptions/cache/a/wis2/%2B/services/%23
# clean up, remove test download
docker exec subscriber bash -c "rm \"./app/data/downloads/$(date +'%Y')/$(date +'%m')/$(date +'%d')/cache/a/wis2/my-centre/services/downloader/openapi.bin\""
- name: Shutdown
working-directory: docker/tests
run: |
Expand Down
22 changes: 15 additions & 7 deletions wis2downloader/downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ def process_job(self, job) -> None:

# Get information about the job for verification later
expected_hash, hash_function = self.get_hash_info(job)
expected_size = job.get('payload', {}).get('content', {}).get('size')

# Get the download url, update status, and file type from the job links
_url, update, media_type = self.get_download_url(job)
_url, update, media_type, expected_size = self.get_download_url(job)

if _url is None:
LOGGER.warning(f"No download link found in job {job}")
Expand Down Expand Up @@ -236,16 +235,18 @@ def get_topic_and_centre(self, job) -> tuple:

def get_hash_info(self, job):
expected_hash = job.get('payload', {}).get(
'properties', {}).get('integrity', {}).get('hash')
'properties', {}).get('integrity', {}).get('value')
hash_method = job.get('payload', {}).get(
'properties', {}).get('integrity', {}).get('method')

hash_function = None

# Check if hash method is known using our enumumeration of hash methods
if hash_method in VerificationMethods._member_names_:
# get method
method = VerificationMethods[hash_method].value
hash_function = hashlib.new(method)
# load and return from the hashlib library
hash_function = getattr(hashlib, method, None)

return expected_hash, hash_function

Expand All @@ -254,18 +255,21 @@ def get_download_url(self, job) -> tuple:
_url = None
update = False
media_type = None
expected_size = None
for link in links:
if link.get('rel') == 'update':
_url = link.get('href')
media_type = link.get('type')
expected_size = link.get('length')
update = True
break
elif link.get('rel') == 'canonical':
_url = link.get('href')
media_type = link.get('type')
expected_size = link.get('length')
break

return _url, update, media_type
return _url, update, media_type, expected_size

def extract_filename(self, _url) -> tuple:
path = urlsplit(_url).path
Expand All @@ -279,8 +283,12 @@ def validate_data(self, data, expected_hash,
hash_function):
return True

hash_value = hash_function(data).digest()
hash_value = base64.b64encode(hash_value).decode()
try:
hash_value = hash_function(data).digest()
hash_value = base64.b64encode(hash_value).decode()
except Exception as e:
LOGGER.error(e)
return False
if (hash_value != expected_hash) or (len(data) != expected_size):
return False

Expand Down
Loading