From 1721f8a737a891a7c3955fdf57395eb325e8bfcd Mon Sep 17 00:00:00 2001 From: chrystinne Date: Tue, 14 Jan 2025 16:25:47 -0500 Subject: [PATCH] Remove middleware, prevent display of AWS sync command when S3 URI is unavailable,and skip DUA signature check for open projects. --- physionet-django/physionet/settings/base.py | 1 - .../project/modelcomponents/middleware.py | 19 ------------------- .../project/modelcomponents/storage.py | 18 +++++++----------- .../templates/project/published_project.html | 12 +++++++----- physionet-django/project/views.py | 7 ++++--- 5 files changed, 18 insertions(+), 39 deletions(-) delete mode 100644 physionet-django/project/modelcomponents/middleware.py diff --git a/physionet-django/physionet/settings/base.py b/physionet-django/physionet/settings/base.py index 6584dd96c..5348b482d 100644 --- a/physionet-django/physionet/settings/base.py +++ b/physionet-django/physionet/settings/base.py @@ -90,7 +90,6 @@ # RedirectFallbackMiddleware should go at end of list, according # to the docs: https://docs.djangoproject.com/en/4.1/ref/contrib/redirects/ 'django.contrib.redirects.middleware.RedirectFallbackMiddleware', - 'project.modelcomponents.middleware.CurrentRequestMiddleware', ] REST_FRAMEWORK = { diff --git a/physionet-django/project/modelcomponents/middleware.py b/physionet-django/project/modelcomponents/middleware.py deleted file mode 100644 index 4dfb5dd65..000000000 --- a/physionet-django/project/modelcomponents/middleware.py +++ /dev/null @@ -1,19 +0,0 @@ -import threading - -_request_local = threading.local() - - -def get_current_request(): - """Retrieve the current request from thread-local storage.""" - return getattr(_request_local, 'request', None) - - -class CurrentRequestMiddleware: - """Middleware to store the current request in thread-local storage.""" - def __init__(self, get_response): - self.get_response = get_response - - def __call__(self, request): - _request_local.request = request - response = self.get_response(request) - return response diff --git a/physionet-django/project/modelcomponents/storage.py b/physionet-django/project/modelcomponents/storage.py index 150fa8a49..2220fcc21 100644 --- a/physionet-django/project/modelcomponents/storage.py +++ b/physionet-django/project/modelcomponents/storage.py @@ -2,8 +2,6 @@ from django.db import models from django.conf import settings from project.modelcomponents.generic import BaseInvitation -from project.modelcomponents.middleware import get_current_request - class StorageRequest(BaseInvitation): """ @@ -64,22 +62,20 @@ class AWS(models.Model): class Meta: default_permissions = () - def s3_uri(self): + def s3_uri(self, user=None): """ Construct the S3 URI for the project. + Parameters: + user (User): The user requesting the S3 URI """ from project.cloud.s3 import get_access_point_name_for_user_and_project if self.is_private: - # Retrieve the current request - request = get_current_request() - if not request or not hasattr(request, 'user') or not request.user.is_authenticated: - print("Error: No valid user in the current request.") + if not user or not user.is_authenticated: + print("Error: No valid user provided") return None - - # Get the current user from the request - current_user = request.user + # Fetch access point name - access_point_name = get_access_point_name_for_user_and_project(current_user, self) + access_point_name = get_access_point_name_for_user_and_project(user, self) if access_point_name and "No " not in access_point_name: return ( f's3://arn:aws:s3:us-east-1:{settings.AWS_ACCOUNT_ID}:accesspoint/' diff --git a/physionet-django/project/templates/project/published_project.html b/physionet-django/project/templates/project/published_project.html index cb4f290e9..2a9a09646 100644 --- a/physionet-django/project/templates/project/published_project.html +++ b/physionet-django/project/templates/project/published_project.html @@ -430,11 +430,13 @@
Access the files
wget -r -N -c -np{% if project.access_policy %} --user {{ user }} --ask-password{% endif %} {{ bulk_url_prefix }}{% url 'serve_published_project_file' project.slug project.version '' %}
{% endif %} - {% if has_s3_credentials and project.aws.sent_files and has_signed_dua and s3_uri%} -
  • - Download the files using AWS command line tools: -
    aws s3 sync {{ s3_uri }} DESTINATION
    -
  • + {% if has_s3_credentials and project.aws.sent_files and s3_uri != None %} + {% if not project.aws.is_private or has_signed_dua %} +
  • + Download the files using AWS command line tools: +
    aws s3 sync {{ s3_uri }} DESTINATION
    +
  • + {% endif %} {% endif %} diff --git a/physionet-django/project/views.py b/physionet-django/project/views.py index e7a25b868..fbb986339 100644 --- a/physionet-django/project/views.py +++ b/physionet-django/project/views.py @@ -1934,11 +1934,12 @@ def published_project(request, project_slug, version, subdir=''): # Check if AWS instance exists for the project s3_uri = None - if has_signed_dua and hasattr(project, 'aws'): + if hasattr(project, 'aws'): if project.aws.is_private: - s3_uri = project.aws.s3_uri() + if has_signed_dua: + s3_uri = project.aws.s3_uri(user=request.user) else: - s3_uri = '--no-sign-request ' + project.aws.s3_uri() + s3_uri = '--no-sign-request ' + project.aws.s3_uri(user=None) context = { 'project': project,