From 4011a68f064b5a8b91cb8c134cdf6d716ab8aa0c Mon Sep 17 00:00:00 2001 From: David <6107913+ackdav@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:49:57 +0200 Subject: [PATCH] ECR: add CreateRepository name validation (#6479) --- moto/ecr/models.py | 9 +++++++++ tests/test_ecr/test_ecr_boto3.py | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/moto/ecr/models.py b/moto/ecr/models.py index ced3be688..f2d2961bf 100644 --- a/moto/ecr/models.py +++ b/moto/ecr/models.py @@ -30,6 +30,9 @@ from moto.moto_api._internal import mock_random as random from moto.utilities.tagging_service import TaggingService ECR_REPOSITORY_ARN_PATTERN = "^arn:(?P[^:]+):ecr:(?P[^:]+):(?P[^:]+):repository/(?P.*)$" +ECR_REPOSITORY_NAME_PATTERN = ( + "(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*" +) EcrRepositoryArn = namedtuple( "EcrRepositoryArn", ["partition", "region", "account_id", "repo_name"] @@ -466,6 +469,12 @@ class ECRBackend(BaseBackend): if self.repositories.get(repository_name): raise RepositoryAlreadyExistsException(repository_name, self.account_id) + match = re.fullmatch(ECR_REPOSITORY_NAME_PATTERN, repository_name) + if not match: + raise InvalidParameterException( + f"Invalid parameter at 'repositoryName' failed to satisfy constraint: 'must satisfy regular expression '{ECR_REPOSITORY_NAME_PATTERN}'" + ) + repository = Repository( account_id=self.account_id, region_name=self.region_name, diff --git a/tests/test_ecr/test_ecr_boto3.py b/tests/test_ecr/test_ecr_boto3.py index ca1479ebe..f3856b455 100644 --- a/tests/test_ecr/test_ecr_boto3.py +++ b/tests/test_ecr/test_ecr_boto3.py @@ -156,6 +156,19 @@ def test_create_repository_error_already_exists(): ) +@mock_ecr +def test_create_repository_error_name_validation(): + client = boto3.client("ecr", region_name="eu-central-1") + repo_name = "tesT" + + with pytest.raises(ClientError) as e: + client.create_repository(repositoryName=repo_name) + + ex = e.value + ex.operation_name.should.equal("CreateRepository") + ex.response["Error"]["Code"].should.contain("InvalidParameterException") + + @mock_ecr def test_describe_repositories(): client = boto3.client("ecr", region_name="us-east-1")