Skip to content

Commit

Permalink
ECR: add CreateRepository name validation (#6479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ackdav authored Jul 4, 2023
1 parent b662727 commit 4011a68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions moto/ecr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from moto.utilities.tagging_service import TaggingService

ECR_REPOSITORY_ARN_PATTERN = "^arn:(?P<partition>[^:]+):ecr:(?P<region>[^:]+):(?P<account_id>[^:]+):repository/(?P<repo_name>.*)$"
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"]
Expand Down Expand Up @@ -466,6 +469,12 @@ def create_repository(
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,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ecr/test_ecr_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4011a68

Please sign in to comment.