Skip to content

Commit

Permalink
Merge pull request #718 from oda-hub/set-token-uploaded-file-url
Browse files Browse the repository at this point in the history
passing token to the funciotn that builds the uploaded file url
  • Loading branch information
burnout87 authored Nov 19, 2024
2 parents f3e0e20 + 5b33fe9 commit 04a1d31
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 8 additions & 5 deletions cdci_data_analysis/analysis/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def parse_inputs_files(self,
bind_port,
request_files_dir,
decoded_token,
token=None,
sentry_dsn=None):
error_message = 'Error while {step} {temp_dir_content_msg}{additional}'
# TODO probably exception handling can be further improved and/or optmized
Expand Down Expand Up @@ -281,7 +282,8 @@ def parse_inputs_files(self,
uploaded_files_obj=uploaded_files_obj,
products_url=products_url,
bind_host=bind_host,
bind_port=bind_port)
bind_port=bind_port,
token=token)
step = 'updating ownership files'
self.update_ownership_files(uploaded_files_obj,
request_files_dir=request_files_dir,
Expand Down Expand Up @@ -708,16 +710,17 @@ def set_input_products_from_fronted(self, input_file_path, par_dic, verbose=Fals
else:
raise RuntimeError

def update_par_dic_with_uploaded_files(self, par_dic, uploaded_files_obj, products_url, bind_host, bind_port):
def update_par_dic_with_uploaded_files(self, par_dic, uploaded_files_obj, products_url, bind_host, bind_port, token=None):
if validators.url(products_url, simple_host=True):
# TODO remove the dispatch-data part, better to have it extracted from the configuration file
basepath = os.path.join(products_url, 'dispatch-data/download_file')
else:
basepath = os.path.join(f"http://{bind_host}:{bind_port}", 'download_file')
for f in uploaded_files_obj:
dpars = urlencode(dict(file_list=uploaded_files_obj[f],
_is_mmoda_url=True,
return_archive=False))
dict_args = dict(file_list=uploaded_files_obj[f], _is_mmoda_url=True, return_archive=False)
if token is not None:
dict_args['token'] = token
dpars = urlencode(dict_args)
download_file_url = f"{basepath}?{dpars}"
par_dic[f] = download_file_url

Expand Down
1 change: 1 addition & 0 deletions cdci_data_analysis/flask_app/dispatcher_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def __init__(self, app,
bind_host=bind_host,
bind_port=bind_port,
request_files_dir=self.request_files_dir,
token=self.token,
decoded_token=self.decoded_token,
sentry_dsn=self.sentry_dsn
)
Expand Down
18 changes: 13 additions & 5 deletions tests/test_server_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dateutil import parser, tz
from functools import reduce
from urllib import parse
from urllib.parse import urlencode
from urllib.parse import urlencode, urlparse, parse_qs, urlunparse
import nbformat as nbf
import yaml
import gzip
Expand Down Expand Up @@ -1714,18 +1714,24 @@ def test_arg_file(dispatcher_live_fixture, dispatcher_test_conf, public_download
file_hash = make_hash_file(p_file_path)
dpars = urlencode(dict(file_list=file_hash,
_is_mmoda_url=True,
return_archive=False))
return_archive=False,
token=encoded_token))
local_download_url = f"{os.path.join(products_host_port, 'download_file')}?{dpars}"

assert arg_download_url == local_download_url

if public_download_request:
url_parts = urlparse(arg_download_url)
url_args = parse_qs(url_parts.query)
del url_args['token']
new_url_parts = url_parts._replace(query=urlencode(url_args, doseq=True))
arg_download_url = urlunparse(new_url_parts)
c = requests.get(arg_download_url)
assert c.status_code == 403
jdata = c.json()
assert jdata['exit_status']['message'] == "User cannot access the file"
else:
arg_download_url += f'&token={encoded_token}'
# arg_download_url += f'&token={encoded_token}'
c = requests.get(arg_download_url)
assert c.status_code == 200
with open(p_file_path) as p_file:
Expand Down Expand Up @@ -1787,7 +1793,8 @@ def test_arg_file_external_product_url(dispatcher_live_fixture_with_external_pro
file_hash = make_hash_file(p_file_path)
dpars = urlencode(dict(file_list=file_hash,
_is_mmoda_url=True,
return_archive=False))
return_archive=False,
token=encoded_token))
local_download_url = f"{os.path.join(dispatcher_test_conf_with_external_products_url['products_url'], 'dispatch-data/download_file')}?{dpars}"

assert jdata['products']['analysis_parameters']['dummy_file'] == local_download_url
Expand Down Expand Up @@ -1847,7 +1854,8 @@ def test_arg_file_default_product_url(dispatcher_live_fixture_with_default_route
file_hash = make_hash_file(p_file_path)
dpars = urlencode(dict(file_list=file_hash,
_is_mmoda_url=True,
return_archive=False))
return_archive=False,
token=encoded_token))
local_download_url = f"{os.path.join(dispatcher_test_conf_with_default_route_products_url['products_url'], 'dispatch-data/download_file')}?{dpars}"

assert jdata['products']['analysis_parameters']['dummy_file'] == local_download_url
Expand Down

0 comments on commit 04a1d31

Please sign in to comment.