From 800033b1ac455dc11ac33a91e9756ca830135426 Mon Sep 17 00:00:00 2001 From: Marie Backman Date: Wed, 8 Jan 2025 12:17:44 -0500 Subject: [PATCH] Revert "Merge pull request #169 from neutrons/live_plot_Authorization" (#206) * Revert "Merge pull request #169 from neutrons/live_plot_Authorization" This reverts commit 157d1ba2e00497d5fb57d829910ac881305b8dda, reversing changes made to 636c61e20f1007ced922a22d84a016da758e98d6. * fix failing tests --- src/webmon_app/reporting/report/view_util.py | 25 +++++++++++++++---- .../reporting/templates/report/detail.html | 3 --- .../tests/test_report/test_view_util.py | 17 ++++++++++++- tests/test_livedata.py | 10 +++----- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/webmon_app/reporting/report/view_util.py b/src/webmon_app/reporting/report/view_util.py index 642a9f4e..3a4c4816 100644 --- a/src/webmon_app/reporting/report/view_util.py +++ b/src/webmon_app/reporting/report/view_util.py @@ -52,6 +52,22 @@ def generate_key(instrument: str, run_id: int): return hashlib.sha1(f"{instrument.upper()}{secret_key}{run_id}".encode("utf-8")).hexdigest() +def append_key(input_url, instrument, run_id): + """ + Append a live data secret key to a url + + :param input_url: url to modify + :param instrument: instrument name + :param run_id: run number + """ + client_key = generate_key(instrument, run_id) + if client_key is None: + return input_url + # Determine whether this is the first query string argument of the url + delimiter = "&" if "/?" in input_url else "?" + return "%s%skey=%s" % (input_url, delimiter, client_key) + + def fill_template_values(request, **template_args): """ Fill the template argument items needed to populate @@ -509,7 +525,7 @@ def get_plot_template_dict(run_object=None, instrument=None, run_id=None): html_data = get_plot_data_from_server(instrument, run_id, "html") if html_data is not None: plot_dict["html_data"] = html_data - plot_dict["update_url"] = live_data_url + "/html/" + plot_dict["update_url"] = append_key(live_data_url + "/html/", instrument, run_id) if extract_ascii_from_div(html_data) is not None: plot_dict["data_url"] = reverse("report:download_reduced_data", args=[instrument, run_id]) return plot_dict @@ -519,7 +535,7 @@ def get_plot_template_dict(run_object=None, instrument=None, run_id=None): # Third, local json data for the d3 plots if json_data: - plot_dict["update_url"] = live_data_url + "/json/" + plot_dict["update_url"] = append_key(live_data_url + "/json/", instrument, run_id) plot_data, x_label, y_label = extract_d3_data_from_json(json_data) if plot_data is not None: @@ -549,9 +565,8 @@ def get_plot_data_from_server(instrument, run_id, data_type="json"): live_data_url, data_type, ) - key = generate_key(instrument, run_id) - headers = {} if key is None else {"Authorization": key} - data_request = requests.get(live_data_url, timeout=1.5, headers=headers) + live_data_url = append_key(live_data_url, instrument, run_id) + data_request = requests.get(live_data_url, timeout=1.5) if data_request.status_code == 200: json_data = data_request.text except: # noqa: E722 diff --git a/src/webmon_app/reporting/templates/report/detail.html b/src/webmon_app/reporting/templates/report/detail.html index 145ed61f..888746d6 100644 --- a/src/webmon_app/reporting/templates/report/detail.html +++ b/src/webmon_app/reporting/templates/report/detail.html @@ -46,9 +46,6 @@ function get_plot_update() { $.ajax({type: "GET", url: "{{update_url}}", - headers: { - "Authorization": "{{key}}" - }, success: function(data) { if (div_data.localeCompare(data) != 0) { div_data = data; diff --git a/src/webmon_app/reporting/tests/test_report/test_view_util.py b/src/webmon_app/reporting/tests/test_report/test_view_util.py index 8f6ef27d..4ada513e 100644 --- a/src/webmon_app/reporting/tests/test_report/test_view_util.py +++ b/src/webmon_app/reporting/tests/test_report/test_view_util.py @@ -78,6 +78,21 @@ def test_generate_key(self): refval = "d556af22f61bf04e6eb79d88f5c9031230b29b33" self.assertEqual(rst, refval) + def test_append_key(self): + from reporting.report.view_util import append_key + + # case: client_key is None + input_url = "www.test.xyz" + inst = "test_instrument" + run_id = 1 + rst = append_key(input_url, inst, run_id) + self.assertEqual(rst, input_url) + # case: client_key is not None + with self.settings(LIVE_PLOT_SECRET_KEY="test_key"): + rst = append_key(input_url, inst, run_id) + refval = f"{input_url}?key=d556af22f61bf04e6eb79d88f5c9031230b29b33" + self.assertEqual(rst, refval) + @mock.patch(("reporting.dasmon.view_util.fill_template_values"), return_value="passed") def test_fill_template_values(self, mockDasmonTemplateFiller): from reporting.report.view_util import fill_template_values @@ -311,7 +326,7 @@ def test_get_plot_data_from_server(self, mockRequestsGet): # test inst = Instrument.objects.get(name="test_instrument") run = DataRun.objects.get(run_number=1, instrument_id=inst) - rst = get_plot_data_from_server(inst, run) + rst = get_plot_data_from_server(inst.name, run) self.assertEqual(rst, "test") def test_extract_d3_data_from_json(self): diff --git a/tests/test_livedata.py b/tests/test_livedata.py index 41538475..dbff088b 100644 --- a/tests/test_livedata.py +++ b/tests/test_livedata.py @@ -61,15 +61,13 @@ def send_request(self, task, run_number, requestType): return response.text def test_reduction_request_livedata(self): - key = generate_key(self.instrument, self.run_number) ssl_crt_filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../nginx/nginx.crt") key = generate_key(self.instrument, self.run_number) # first check that the there isn't an existing plot, should 404 response = requests.get( - f"{LIVEDATA_TEST_URL}/plots/{self.instrument}/{self.run_number}/update/html/", + f"{LIVEDATA_TEST_URL}/plots/{self.instrument}/{self.run_number}/update/html/?key={key}", verify=ssl_crt_filename, - headers={"Authorization": key}, ) assert response.status_code == 404 @@ -78,9 +76,8 @@ def test_reduction_request_livedata(self): # the data should now be on livedata response = requests.get( - f"{LIVEDATA_TEST_URL}/plots/{self.instrument}/{self.run_number}/update/html/", + f"{LIVEDATA_TEST_URL}/plots/{self.instrument}/{self.run_number}/update/html/?key={key}", verify=ssl_crt_filename, - headers={"Authorization": key}, ) assert response.status_code == 200 assert "Example Plot Data" in response.text @@ -90,8 +87,7 @@ def test_reduction_request_livedata(self): # now verify that the run report page is templated correctly client = self.get_session() page = client.get(f"{WEBMON_TEST_URL}/report/{self.instrument}/{self.run_number}/") - assert 'url: "https://172.16.238.222:443/plots/arcs/214583/update/html/"' in page.text - assert f'"Authorization": "{key}"' in page.text + assert f"https://172.16.238.222:443/plots/arcs/214583/update/html/?key={key}" in page.text def generate_key(instrument, run_id):