Skip to content

Events create connection test fix #11904

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

Merged
merged 2 commits into from
Nov 26, 2024
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
80 changes: 47 additions & 33 deletions localstack-core/localstack/services/events/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,34 +229,45 @@ def on_before_stop(self):
# Helper Methods for connections and api destinations
##########

def _validate_api_destination_name(self, name: str) -> None:
"""Validate the API destination name according to AWS rules."""
def _validate_api_destination_name(self, name: str) -> list[str]:
"""Validate the API destination name according to AWS rules. Returns a list of validation errors."""
errors = []
if not re.match(r"^[\.\-_A-Za-z0-9]+$", name):
raise ValidationException(
f"1 validation error detected: Value '{name}' at 'name' failed to satisfy constraint: "
errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must satisfy regular expression pattern: [\\.\\-_A-Za-z0-9]+"
)
if not (1 <= len(name) <= 64):
raise ValidationException(
f"1 validation error detected: Value '{name}' at 'name' failed to satisfy constraint: "
"Member must have length between 1 and 64"
errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must have length less than or equal to 64"
)
return errors

def _validate_connection_name(self, name: str) -> None:
"""Validate the connection name according to AWS rules."""
def _validate_connection_name(self, name: str) -> list[str]:
"""Validate the connection name according to AWS rules. Returns a list of validation errors."""
errors = []
if not re.match("^[\\.\\-_A-Za-z0-9]+$", name):
raise ValidationException(
f"1 validation error detected: Value '{name}' at 'name' failed to satisfy constraint: "
errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must satisfy regular expression pattern: [\\.\\-_A-Za-z0-9]+"
)
if not (1 <= len(name) <= 64):
errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must have length less than or equal to 64"
)
return errors

def _validate_auth_type(self, auth_type: str) -> None:
"""Validate the authorization type is one of the allowed values."""
def _validate_auth_type(self, auth_type: str) -> list[str]:
"""Validate the authorization type. Returns a list of validation errors."""
errors = []
if auth_type not in VALID_AUTH_TYPES:
raise ValidationException(
f"1 validation error detected: Value '{auth_type}' at 'authorizationType' failed to satisfy constraint: "
errors.append(
f"Value '{auth_type}' at 'authorizationType' failed to satisfy constraint: "
f"Member must satisfy enum value set: [{', '.join(VALID_AUTH_TYPES)}]"
)
return errors

def _get_connection_by_arn(self, connection_arn: str) -> Optional[Dict]:
"""Retrieve a connection by its ARN."""
Expand Down Expand Up @@ -510,12 +521,23 @@ def create_connection(
description: ConnectionDescription = None,
**kwargs,
) -> CreateConnectionResponse:
"""Create a new connection."""
auth_type = authorization_type
if hasattr(authorization_type, "value"):
auth_type = authorization_type.value

errors = []
errors.extend(self._validate_connection_name(name))
errors.extend(self._validate_auth_type(auth_type))

if errors:
error_message = (
f"{len(errors)} validation error{'s' if len(errors) > 1 else ''} detected: "
)
error_message += "; ".join(errors)
raise ValidationException(error_message)

def create():
auth_type = authorization_type
if hasattr(authorization_type, "value"):
auth_type = authorization_type.value
self._validate_auth_type(auth_type)
self._validate_connection_name(name)
store = self.get_store(context.region, context.account_id)

if name in store.connections:
Expand Down Expand Up @@ -684,19 +706,11 @@ def create_api_destination(

def create():
validation_errors = []
if not re.match(r"^[\.\-_A-Za-z0-9]+$", name):
validation_errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must satisfy regular expression pattern: [\\.\\-_A-Za-z0-9]+"
)
if not (1 <= len(name) <= 64):
validation_errors.append(
f"Value '{name}' at 'name' failed to satisfy constraint: "
"Member must have length between 1 and 64"
)

connection_arn_pattern = r"^arn:aws([a-z]|\-)*:events:[a-z0-9\-]+:\d{12}:connection/[\.\-_A-Za-z0-9]+/[\-A-Za-z0-9]+$"
if not re.match(connection_arn_pattern, connection_arn):
validation_errors.extend(self._validate_api_destination_name(name))
if not re.match(
r"^arn:aws([a-z]|\-)*:events:[a-z0-9\-]+:\d{12}:connection/[\.\-_A-Za-z0-9]+/[\-A-Za-z0-9]+$",
connection_arn,
):
validation_errors.append(
f"Value '{connection_arn}' at 'connectionArn' failed to satisfy constraint: "
"Member must satisfy regular expression pattern: "
Expand Down
4 changes: 1 addition & 3 deletions tests/aws/services/events/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,7 @@ def _handler(_request: Request):
assert oauth_request.args["oauthquery"] == "value3"

@markers.aws.validated
@pytest.mark.skip(
reason="V2 provider does not support this feature yet and it also fails in V1 now"
)
@pytest.mark.skipif(is_old_provider(), reason="V1 provider does not support this feature")
def test_create_connection_validations(self, aws_client, snapshot):
connection_name = "This should fail with two errors 123467890123412341234123412341234"

Expand Down
Loading