Skip to content

Commit

Permalink
fix the bucket names in case of special characters in username
Browse files Browse the repository at this point in the history
  • Loading branch information
sblack-usu committed Jan 10, 2024
1 parent b80a210 commit 5b678d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
5 changes: 5 additions & 0 deletions app/api/subsetter/app/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from enum import Enum
from functools import lru_cache
from typing import List, Optional, Tuple
Expand Down Expand Up @@ -48,6 +49,10 @@ class User(BeanieBaseUser, Document):
given_name: Optional[str] = None
family_name: Optional[str] = None

@property
def bucket_name(self):
return re.sub("[^A-Za-z0-9\.-]", "", re.sub("[@]", ".at.", self.username.lower()))

async def update_profile(self):
async def get_profile(token: str) -> Tuple[str, str]:
async with httpx.AsyncClient() as client:
Expand Down
20 changes: 10 additions & 10 deletions app/api/subsetter/app/routers/argo/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,31 @@
api_instance = workflow_service_api.WorkflowServiceApi(api_client)


def parflow_submission_body(hucs: list, username: str, workflow_name: str):
def parflow_submission_body(hucs: list, bucket_name: str, workflow_name: str):
return {
"resourceKind": "WorkflowTemplate",
"resourceName": "parflow-subset-v1-by-huc-minio",
"submitOptions": {
"name": workflow_name,
"parameters": [
f"output-path=argo_workflows/parflow/{workflow_name}",
f"output-bucket={username}",
f"output-bucket={bucket_name}",
"hucs=" + ",".join(hucs),
],
},
}


def nwm1_submission_body(
y_south: float, x_west: float, y_north: float, x_east: float, username: str, workflow_name: str
y_south: float, x_west: float, y_north: float, x_east: float, bucket_name: str, workflow_name: str
):
return {
"resourceKind": "WorkflowTemplate",
"resourceName": "nwm1-subset-minio",
"submitOptions": {
"name": workflow_name,
"parameters": [
f"output-bucket={username}",
f"output-bucket={bucket_name}",
f"output-path=argo_workflows/nwm1/{workflow_name}",
f"y_south={y_south}",
f"x_west={x_west}",
Expand All @@ -72,15 +72,15 @@ def nwm1_submission_body(


def nwm2_submission_body(
y_south: float, x_west: float, y_north: float, x_east: float, username: str, workflow_name: str
y_south: float, x_west: float, y_north: float, x_east: float, bucket_name: str, workflow_name: str
):
return {
"resourceKind": "WorkflowTemplate",
"resourceName": "nwm2-subset-minio",
"submitOptions": {
"name": workflow_name,
"parameters": [
f"output-bucket={username}",
f"output-bucket={bucket_name}",
f"output-path=argo_workflows/nwm2/{workflow_name}",
f"y_south={y_south}",
f"x_west={x_west}",
Expand Down Expand Up @@ -113,7 +113,7 @@ async def submit_parflow(
workflow_id = str(uuid.uuid4())
api_response = api_instance.submit_workflow(
namespace=get_settings().argo_namespace,
body=parflow_submission_body(hucs, user.username, workflow_id),
body=parflow_submission_body(hucs, user.bucket_name, workflow_id),
_preload_content=False,
)
log.info(api_response.json())
Expand All @@ -129,7 +129,7 @@ async def submit_nwm1(
workflow_id = str(uuid.uuid4())
api_response = api_instance.submit_workflow(
namespace=get_settings().argo_namespace,
body=nwm1_submission_body(y_south, x_west, y_north, x_east, user.username, workflow_id),
body=nwm1_submission_body(y_south, x_west, y_north, x_east, user.bucket_name, workflow_id),
_preload_content=False,
)
log.info(api_response.json())
Expand All @@ -145,7 +145,7 @@ async def submit_nwm2(
workflow_id = str(uuid.uuid4())
api_response = api_instance.submit_workflow(
namespace=get_settings().argo_namespace,
body=nwm2_submission_body(y_south, x_west, y_north, x_east, user.username, workflow_id),
body=nwm2_submission_body(y_south, x_west, y_north, x_east, user.bucket_name, workflow_id),
_preload_content=False,
)
log.info(api_response.json())
Expand Down Expand Up @@ -223,7 +223,7 @@ async def signed_url_minio(workflow_params: WorkflowDep) -> UrlResponseModel:
submission = workflow_params.user.get_submission(workflow_params.workflow_id)
url = get_minio_client().presigned_get_object(
"subsetter-outputs",
f"{workflow_params.user.username}/{submission.workflow_name}/{submission.workflow_id}/all.gz",
f"{workflow_params.user.bucket_name}/{submission.workflow_name}/{submission.workflow_id}/all.gz",
)
return {'url': url}

Expand Down
5 changes: 3 additions & 2 deletions app/api/subsetter/app/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class UserManager(ObjectIDIDMixin, BaseUserManager[User, PydanticObjectId]):

async def on_after_register(self, user: User, request: Optional[Request] = None):
await user.update_profile()
if not get_minio_client().bucket_exists(user.username):
get_minio_client().make_bucket(user.username)
if not get_minio_client().bucket_exists(user.bucket_name):
get_minio_client().make_bucket(user.bucket_name)
print(f"created bucket: {user.bucket_name}")
print(f"User {user.id} has registered.")

async def on_after_forgot_password(self, user: User, token: str, request: Optional[Request] = None):
Expand Down

0 comments on commit 5b678d3

Please sign in to comment.