Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions executor/python_executor/credential.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from typing import Any, Dict
from oocana import InputHandleDef
class CredentialInput:
def __init__(self, type: str, value: str):
def __init__(self, type: str, id: str):
self.type = type
self.value = value
self.id = id

def generate_credential_input(credential_path: str) -> CredentialInput | None:
"""Generate a CredentialInput from a credential path string.

The credential path should be in the format `${{OO_CREDENTIAL:type,id}}`. If the format is incorrect,
The credential path should be in the format `${{OO_CREDENTIAL:type,name,id}}`. If the format is incorrect,
the function returns None.
"""

if not (credential_path.startswith("${{OO_CREDENTIAL:") and credential_path.endswith("}}")):
# logger warning("Credential path format is incorrect. Expected to start with '${{OO_CREDENTIAL:' and end with '}}'.")
return None

if credential_path.count(",") != 2:
# logger warning("Credential path format is incorrect. Expected exactly two commas.")
return None

credential_path = credential_path.removeprefix("${{OO_CREDENTIAL:").removesuffix("}}")
if credential_path:
try:
type, id = credential_path.split(",", maxsplit=1)
type, _name, id = credential_path.split(",", maxsplit=2)
return CredentialInput(type, id)
except ValueError:
return None
Expand Down
16 changes: 10 additions & 6 deletions executor/tests/test_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from oocana import InputHandleDef
from typing import cast

ORIGIN_VALUE = "Custom,credential_id"
ORIGIN_VALUE = "Custom,credential_name,credential_id"

class TestCredential(unittest.TestCase):

Expand Down Expand Up @@ -33,7 +33,7 @@ def test_replace_credential(self):
cred_input = v.get("c")
self.assertIsInstance(cred_input, CredentialInput)
self.assertEqual(cred_input.type, "Custom")
self.assertEqual(cred_input.value, "credential_id")
self.assertEqual(cred_input.id, "credential_id")


def test_credential_without_content_media(self):
Expand Down Expand Up @@ -70,20 +70,20 @@ def test_credential_in_other_string(self):

def test_generate_credential_input_valid(self):
"""Test valid credential input generation"""
result = generate_credential_input("${{OO_CREDENTIAL:AWS,my_credential_id}}")
result = generate_credential_input("${{OO_CREDENTIAL:AWS,my_credential_name,my_credential_id}}")
self.assertIsInstance(result, CredentialInput)
result = cast(CredentialInput, result)
self.assertEqual(result.type, "AWS")
self.assertEqual(result.value, "my_credential_id")
self.assertEqual(result.id, "my_credential_id")

def test_generate_credential_input_invalid_format(self):
"""Test invalid credential input format"""
# Missing prefix
result = generate_credential_input("AWS,my_credential_id")
result = generate_credential_input("AWS,my_credential_name,my_credential_id")
self.assertIsNone(result)

# Missing suffix
result = generate_credential_input("${{OO_CREDENTIAL:AWS,my_credential_id")
result = generate_credential_input("${{OO_CREDENTIAL:AWS,my_credential_name,my_credential_id")
self.assertIsNone(result)

# Wrong prefix
Expand All @@ -98,5 +98,9 @@ def test_generate_credential_input_invalid_format(self):
result = generate_credential_input("${{OO_CREDENTIAL:AWS}}")
self.assertIsNone(result)

# Only two parameters (missing third)
result = generate_credential_input("${{OO_CREDENTIAL:AWS,my_credential}}")
self.assertIsNone(result)

if __name__ == '__main__':
unittest.main()