Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VADC-904 #134

Merged
merged 9 commits into from
Apr 10, 2024
34 changes: 27 additions & 7 deletions src/argowrapper/engine/helpers/argo_engine_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Callable, Dict, List

import jwt
import urllib

from argowrapper import logger
from argowrapper.auth import Auth
Expand Down Expand Up @@ -59,12 +58,7 @@ def parse_common_details(
)

if user_name_str:
if user_name_str.startswith("user-"):
user_name_str = user_name_str[5:]
# argo engine encode % as -
user_name_str = urllib.parse.unquote(user_name_str.replace("-", "%"))
# put actual - back after decoding
user_name_str = user_name_str.replace("%", "-")
user_name_str = convert_username_label_to_gen3username(user_name_str)

return {
"name": workflow_details["metadata"].get("name"),
Expand Down Expand Up @@ -228,3 +222,29 @@ def get_username_from_token(header_and_or_token: str) -> str:
username = decoded.get("context", {}).get("user", {}).get("name")
logger.info(f"{username} is submitting a workflow")
return username


def convert_username_label_to_gen3username(label: str) -> str:
"""this function will reverse the conversion of a username to label as
defined by the convert_gen3username_to_pod_label function. eg "user--21" -> "!"

Args:
label (str): _description_

Returns:
: _description_
"""
label = label.replace("user-", "", 1)
regex = r"-[0-9A-Za-z]{2}"
return re.sub(regex, _convert_to_label, label)


def _convert_to_label(special_character_match: str) -> str:
if match := special_character_match.group():
match = match.strip("-")
try:
byte_array = bytearray.fromhex(match)
return byte_array.decode()
except:
logger.info("match is not hex value, return original")
return "-" + match
26 changes: 2 additions & 24 deletions test/test_argo_engine_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@
from test.constants import EXAMPLE_AUTH_HEADER, EXAMPLE_JUST_TOKEN


def _convert_to_label(special_character_match: str) -> str:
if match := special_character_match.group():
match = match.strip("-")
byte_array = bytearray.fromhex(match)
return byte_array.decode()


def convert_username_label_to_gen3username(label: str) -> str:
"""this function will reverse the conversion of a username to label as
defined by the convert_gen3username_to_pod_label function. eg "user--21" -> "!"

Args:
label (str): _description_

Returns:
: _description_
"""
label = label.replace("user-", "", 1)
regex = r"-[0-9A-Za-z]{2}"
return re.sub(regex, _convert_to_label, label)


@pytest.fixture(scope="module")
def setup():
print("*****SETUP*****")
Expand All @@ -48,7 +26,7 @@ def setup():

UsernameLabelPair = namedtuple("UsernameLabelPair", "username label")
user_label_data = [
UsernameLabelPair("abc123", "user-abc123"),
UsernameLabelPair("abc123-test", "user-abc123-test"),
UsernameLabelPair("48@!(CEab***", "user-48-40-21-28CEab-2a-2a-2a"),
UsernameLabelPair("-scott.VA@gmail.com", "user--2dscott-2eVA-40gmail-2ecom"),
]
Expand All @@ -61,7 +39,7 @@ def test_convert_username_label_to_gen3username(username, label):

@pytest.mark.parametrize("username,label", user_label_data)
def test_convert_gen3username_to_pod_label(username, label):
assert convert_username_label_to_gen3username(label) == username
assert argo_engine_helper.convert_username_label_to_gen3username(label) == username


def test_convert_gen3teamproject_to_pod_label_and_back():
Expand Down
Loading