Skip to content

Commit

Permalink
refactor: take request in as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
athith-g committed Sep 1, 2024
1 parent 5224faf commit 7eb9f7d
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions crypt4gh_middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class CryptMiddleware:
"""Middleware class to handle Crypt4GH file inputs."""

def __init__(self):
self.request = None
self.original_input_paths = []
self.output_dir = Path("/vol/crypt/")

def _add_decryption_executor(self) -> None:
def _add_decryption_executor(self, request: flask.Request) -> flask.Request:
"""Add the decryption executor to the executor list."""
executor = {
"image": "athitheyag/crypt4gh:1.0",
Expand All @@ -24,57 +22,58 @@ def _add_decryption_executor(self) -> None:
"decrypt.py"
] + self.original_input_paths + [
"--output-dir",
str(self.output_dir)
"/vol/crypt/"
]
}
self.request.json["executors"].insert(0, executor)
request.json["executors"].insert(0, executor)
return request

def _change_executor_paths(self) -> None:
def _change_executor_paths(self, request: flask.Request) -> flask.Request:
"""Change original input file paths in executors to the output directory."""
for executor_body in self.request.json["executors"]:
for executor_body in request.json["executors"]:
for i, path in enumerate(executor_body["command"]):
if path in self.original_input_paths:
executor_body["command"][i] = str(self.output_dir/Path(path).name)
executor_body["command"][i] = str(Path("/vol/crypt")/Path(path).name)
return request

def _change_output_paths(self) -> None:
def _change_output_paths(self, request: flask.Request) -> flask.Request:
"""Change original output file paths to the output directory if the output path is
the same as an input path.
Accounts for case where input file is modified in place in a TES request.
"""
for output_body in self.request.json["outputs"]:
for output_body in request.json["outputs"]:
path = output_body["path"]
if path in self.original_input_paths:
output_body["path"] = str(self.output_dir/Path(path).name)
output_body["path"] = str(Path("/vol/crypt")/Path(path).name)
return request

def _check_volumes(self) -> None:
def _check_volumes(self, request: flask.Request) -> None:
"""Check volumes to ensure none start with /vol/crypt.
Raises:
ValueError if volumes start with /vol/crypt.
"""
for volume in self.request.json["volumes"]:
for volume in request.json["volumes"]:
if volume.startswith("/vol/crypt"):
raise PathNotAllowedError("/vol/crypt/ is not allowed in volumes.")
raise PathNotAllowedError("/vol/crypt is not allowed in volumes.")

def _set_original_input_paths(self) -> None:
def _set_original_input_paths(self, request: flask.Request) -> None:
"""Retrieve and store the original input file paths.
Raises:
ValueError if any path starts with /vol/crypt.
"""
for input_body in self.request.json["inputs"]:
for input_body in request.json["inputs"]:
if input_body["path"].startswith("/vol/crypt"):
raise PathNotAllowedError("/vol/crypt/ is not allowed in input path.")
self.original_input_paths.append(input_body["path"])

def apply_middleware(self, request: flask.Request) -> flask.Request:
"""Apply middleware to request."""
self.request = request
self._set_original_input_paths()
self._check_volumes()
self._change_executor_paths()
self._change_output_paths()
self._add_decryption_executor()

return self.request
self._set_original_input_paths(request)
self._check_volumes(request)
request = self._change_executor_paths(request)
request = self._change_output_paths(request)
request = self._add_decryption_executor(request)
return request

0 comments on commit 7eb9f7d

Please sign in to comment.