Skip to content

Commit

Permalink
fix: correct validation for Task ID format
Browse files Browse the repository at this point in the history
Signed-off-by: Gahyun Suh <[email protected]>
  • Loading branch information
Gahyun Suh committed Nov 26, 2023
1 parent fdbbe09 commit b04494e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
21 changes: 14 additions & 7 deletions src/deadline/client/cli/_deadline_web_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

DEADLINE_URL_SCHEME_NAME = "deadline"
DEADLINE_ID_PATTERN = re.compile(r"^[0-9a-f]{32}$")
DEADLINE_TASK_ID_PATTERN = re.compile(r"^[0-9a-f]{32}-(0|([1-9][0-9]{0,9}))$")
VALID_RESOURCE_NAMES_IN_ID = ["farm", "queue", "job", "step", "task"]


Expand Down Expand Up @@ -73,6 +74,7 @@ def validate_resource_ids(ids: Dict[str, str]) -> None:
"""
Validates that the resource IDs are all valid.
i.e. "<name of resource>-<a hexadecimal string of length 32>"
for Task ID: "task-<a hexadecimal string of length 32>-<0 or a number up to 10 digits long>"
Args:
ids (Dict[str, str]): The resource IDs to validate.
Expand All @@ -82,16 +84,17 @@ def validate_resource_ids(ids: Dict[str, str]) -> None:
resource_type = id_str.split("-")[0]
if not id_name.startswith(resource_type) or not validate_id_format(resource_type, id_str):
raise DeadlineOperationError(
f'The ID "{id_name}": "{id_str}" is not valid. '
"The ID format must be a valid resource name followed by a hyphen, then a hexadecimal string of 32 characters."
f'The given resource ID "{id_name}": "{id_str}" has invalid format.'
)


def validate_id_format(resource_type: str, full_id_str: str) -> bool:
"""
Validates if the ID is in correct format. The ID must
- start with one of the resource names, and immediately followed by "-", and
- the rest of the string that follows must be a hexadecimal string of length 32.
- start with one of the resource names, and followed by a hyphen ("-"), and
- the string that follows must be a hexadecimal string of length 32.
- Additional for "task": followed by another hyphen ("-"), and ends with
a string of either 0 or a number up to 10 digits long.
Args:
full_id_str (str): The ID to validate.
Expand All @@ -105,9 +108,13 @@ def validate_id_format(resource_type: str, full_id_str: str) -> bool:
return False

id_str = full_id_str[len(prefix) :]
if len(id_str) != 32:
return False
return bool(DEADLINE_ID_PATTERN.fullmatch(id_str))

if resource_type == "task":
return bool(DEADLINE_TASK_ID_PATTERN.fullmatch(id_str))
else:
if len(id_str) != 32:
return False
return bool(DEADLINE_ID_PATTERN.fullmatch(id_str))


def install_deadline_web_url_handler(all_users: bool) -> None:
Expand Down
37 changes: 25 additions & 12 deletions test/unit/deadline_client/cli/test_cli_handle_web_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,54 @@ def test_validate_resource_ids_successful(ids: Dict[str, str]):
@pytest.mark.parametrize(
("ids", "exception_message"),
[
({"": ""}, 'The ID "": "" is not valid.'),
({"farm_id": "farm-123"}, 'The ID "farm_id": "farm-123" is not valid.'),
({"": ""}, 'The given resource ID "": "" has invalid format.'),
(
{"farm_id": "farm-123"},
'The given resource ID "farm_id": "farm-123" has invalid format.',
),
(
{"farm_id": "farm-0123456789abcdefabcdefabcdefabc"},
'The ID "farm_id": "farm-0123456789abcdefabcdefabcdefabc" is not valid.',
'The given resource ID "farm_id": "farm-0123456789abcdefabcdefabcdefabc" has invalid format.',
),
(
{"farm_id": "farm-0123456789abcdefabcdefabcdefabcdef"},
'The ID "farm_id": "farm-0123456789abcdefabcdefabcdefabcdef" is not valid.',
'The given resource ID "farm_id": "farm-0123456789abcdefabcdefabcdefabcdef" has invalid format.',
),
(
{"farm_id": "0123456789abcdefabcdefabcdefabcd"},
'The ID "farm_id": "0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "farm_id": "0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"farm_id": "far-0123456789abcdefabcdefabcdefabcd"},
'The ID "farm_id": "far-0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "farm_id": "far-0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"farm_id": "-farm-0123456789abcdefabcdefabcdefabcd"},
'The ID "farm_id": "-farm-0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "farm_id": "-farm-0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"farm_id": "farm--0123456789abcdefabcdefabcdefabcd"},
'The ID "farm_id": "farm--0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "farm_id": "farm--0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"farm_id": "farm-farm-0123456789abcdefabcdefabcdefabcd"},
'The ID "farm_id": "farm-farm-0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "farm_id": "farm-farm-0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"mission_id": "mission-0123456789abcdefabcdefabcdefabcd"},
'The ID "mission_id": "mission-0123456789abcdefabcdefabcdefabcd" is not valid.',
'The given resource ID "mission_id": "mission-0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
(
{"farm_id": MOCK_QUEUE_ID},
f'The given resource ID "farm_id": "{MOCK_QUEUE_ID}" has invalid format.',
),
({"farm_id": MOCK_QUEUE_ID}, f'The ID "farm_id": "{MOCK_QUEUE_ID}" is not valid.'),
(
{"farm_id": MOCK_FARM_ID, "queue_id": "queue-123"},
'The ID "queue_id": "queue-123" is not valid.',
'The given resource ID "queue_id": "queue-123" has invalid format.',
),
(
{"task_id": "task-0123456789abcdefabcdefabcdefabcd"},
'The given resource ID "task_id": "task-0123456789abcdefabcdefabcdefabcd" has invalid format.',
),
],
)
Expand Down Expand Up @@ -228,6 +238,9 @@ def test_validate_id_format_successful(resource_type: str, full_id_str: str):
("farm", "queue-0123456789abcdefabcdefabcdefabcd"),
("farmfarm", "farmfarm-0123456789abcdefabcdefabcdefabcd"),
("mission", "mission-0123456789abcdefabcdefabcdefabcd"),
("task", "task-0123456789abcdefabcdefabcdefabcd"),
("task", "task-0123456789abcdefabcdefabcdefabcd-00"),
("task", "task-0123456789abcdefabcdefabcdefabcd-12345678912345"),
],
)
def test_validate_id_format_failed(resource_type: str, full_id_str: str):
Expand Down
2 changes: 1 addition & 1 deletion test/unit/deadline_client/shared_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MOCK_STORAGE_PROFILE_ID = "sp-0123456789abcdefabcdefabcdefabcd"
MOCK_JOB_ID = "job-0123456789abcdefabcdefabcdefabcd"
MOCK_STEP_ID = "step-0123456789abcdefabcdefabcdefabcd"
MOCK_TASK_ID = "task-0123456789abcdefabcdefabcdefabcd"
MOCK_TASK_ID = "task-0123456789abcdefabcdefabcdefabcd-99"
MOCK_PROFILE_NAME = "my-studio-profile"
MOCK_QUEUES_LIST = [
{
Expand Down

0 comments on commit b04494e

Please sign in to comment.