ECR: add CreateRepository name validation (#6479)
This commit is contained in:
parent
b66272717f
commit
4011a68f06
@ -30,6 +30,9 @@ from moto.moto_api._internal import mock_random as random
|
|||||||
from moto.utilities.tagging_service import TaggingService
|
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_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 = namedtuple(
|
||||||
"EcrRepositoryArn", ["partition", "region", "account_id", "repo_name"]
|
"EcrRepositoryArn", ["partition", "region", "account_id", "repo_name"]
|
||||||
@ -466,6 +469,12 @@ class ECRBackend(BaseBackend):
|
|||||||
if self.repositories.get(repository_name):
|
if self.repositories.get(repository_name):
|
||||||
raise RepositoryAlreadyExistsException(repository_name, self.account_id)
|
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(
|
repository = Repository(
|
||||||
account_id=self.account_id,
|
account_id=self.account_id,
|
||||||
region_name=self.region_name,
|
region_name=self.region_name,
|
||||||
|
@ -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
|
@mock_ecr
|
||||||
def test_describe_repositories():
|
def test_describe_repositories():
|
||||||
client = boto3.client("ecr", region_name="us-east-1")
|
client = boto3.client("ecr", region_name="us-east-1")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user