Skip to content

Commit

Permalink
refactor: use constant volume path
Browse files Browse the repository at this point in the history
  • Loading branch information
athith-g committed Sep 3, 2024
1 parent d0391e0 commit b96a12a
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions crypt4gh_middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import flask

VOLUME_PATH = "/vol/crypt/"
# mypy: disable-error-code="index"

class PathNotAllowedException(ValueError):
Expand All @@ -26,30 +27,30 @@ def _add_decryption_executor(self, request: flask.Request) -> flask.Request:
"decrypt.py"
] + self.original_input_paths + [
"--output-dir",
"/vol/crypt/"
VOLUME_PATH
]
}
request.json["executors"].insert(0, executor)
return request

def _add_volume(self, request: flask.Request) -> flask.Request:
"""Check volumes to ensure none start with /vol/crypt/ and add /vol/crypt/.
"""Check volumes to ensure none start with VOLUME_PATH and add VOLUME_PATH.
Raises:
PathNotAllowedError if volumes start with /vol/crypt/.
PathNotAllowedError if volumes start with VOLUME_PATH.
"""
for volume in request.json["volumes"]:
if volume.startswith("/vol/crypt/"):
raise PathNotAllowedException("/vol/crypt/ is not allowed in volumes.")
request.json["volumes"].append("/vol/crypt/")
if volume.startswith(VOLUME_PATH):
raise PathNotAllowedException(f"{VOLUME_PATH} is not allowed in volumes.")
request.json["volumes"].append(VOLUME_PATH)
return request

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 request.json["executors"]:
for i, path in enumerate(executor_body["command"]):
if path in self.original_input_paths:
executor_body["command"][i] = str(Path("/vol/crypt")/Path(path).name)
executor_body["command"][i] = str(Path(VOLUME_PATH)/Path(path).name)
return request

def _check_output_paths(self, request: flask.Request) -> None:
Expand All @@ -68,11 +69,11 @@ def _set_original_input_paths(self, request: flask.Request) -> None:
"""Retrieve and store the original input file paths.
Raises:
PathNotAllowedError if any path starts with /vol/crypt/.
PathNotAllowedError if any path starts with VOLUME_PATH.
"""
for input_body in request.json["inputs"]:
if input_body["path"].startswith("/vol/crypt/"):
raise PathNotAllowedException("/vol/crypt/ is not allowed in input path.")
if input_body["path"].startswith(VOLUME_PATH):
raise PathNotAllowedException(f"{VOLUME_PATH} is not allowed in input path.")
self.original_input_paths.append(input_body["path"])

def apply_middleware(self, request: flask.Request) -> flask.Request:
Expand Down

0 comments on commit b96a12a

Please sign in to comment.