diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java index c3dcb20b25..3514305f25 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecr; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsPartitions; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -40,6 +41,13 @@ import java.util.regex.Pattern; @ApplicationScoped public class EcrService implements ResourceProvider { + /** + * A private ECR registry host in any partition, {@code .dkr.ecr..}, + * matched the same way {@code EcrRegistryManager} recognises an ECR image URI. + */ + private static final Pattern ECR_UPSTREAM_HOST = Pattern.compile( + "[0-9]{12}\\.dkr\\.ecr\\.[a-z0-9-]+\\." + AwsPartitions.dnsSuffixRegex()); + private static final Logger LOG = Logger.getLogger(EcrService.class); private static final Pattern REPO_NAME = Pattern.compile( "(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*"); @@ -1023,7 +1031,7 @@ public class EcrService implements ResourceProvider { if (host.equals("cgr.dev")) { return "chainguard"; } - if (host.matches("[0-9]{12}\\.dkr\\.ecr\\.[a-z0-9-]+\\.amazonaws\\.com(?:\\.cn)?")) { + if (ECR_UPSTREAM_HOST.matcher(host).matches()) { return "ecr"; } throw unsupportedUpstreamRegistry(upstreamRegistryUrl); diff --git a/src/main/java/io/github/hectorvent/floci/services/signin/SigninService.java b/src/main/java/io/github/hectorvent/floci/services/signin/SigninService.java index 190c583ec3..ffb181eddd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/signin/SigninService.java +++ b/src/main/java/io/github/hectorvent/floci/services/signin/SigninService.java @@ -50,7 +50,7 @@ public class SigninService { private static final int MAX_STATE_LENGTH = 128; private static final Pattern PKCE_VALUE_PATTERN = Pattern.compile("[A-Za-z0-9._~-]{43,128}"); private static final Pattern AWS_SIGNIN_HOST_PATTERN = - Pattern.compile("[a-z]{2}-[a-z-]+-\\d+\\.signin\\.aws\\.amazon\\.com"); + Pattern.compile("[a-z]{2}-[a-z-]+-\\d+\\.signin\\.aws\\.amazon\\.com"); // partition-literal: console sign-in host, non-commercial host has no published source private final IamService iamService; private final RegionResolver regionResolver; diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java index ff3a8d4c89..eef815a1ff 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java @@ -227,6 +227,29 @@ class EcrServiceTest { "missing/cache", null, null, null, REGION)).getErrorCode()); } + /** + * An ECR upstream is recognised in every partition, as Floci's own registry recognises an ECR + * image URI: the host pattern once accepted only the commercial and China suffixes. + */ + @Test + void pullThroughCacheRule_acceptsAnEcrUpstreamInEveryPartition() { + String[] hosts = { + "123456789012.dkr.ecr.us-east-1.amazonaws.com", + "123456789012.dkr.ecr.cn-north-1.amazonaws.com.cn", + "123456789012.dkr.ecr.us-gov-west-1.amazonaws.com", + "123456789012.dkr.ecr.us-iso-east-1.c2s.ic.gov", + "123456789012.dkr.ecr.eusc-de-east-1.amazonaws.eu", + }; + for (int i = 0; i < hosts.length; i++) { + PullThroughCacheRule rule = service.createPullThroughCacheRule("ecr-up-" + i, hosts[i], null, + null, null, null, null, REGION); + assertEquals("ecr", rule.getUpstreamRegistry(), hosts[i]); + } + assertEquals("UnsupportedUpstreamRegistryException", assertThrows(AwsException.class, + () -> service.createPullThroughCacheRule("ecr-up-bad", "123456789012.dkr.ecr.us-east-1.example.com", + null, null, null, null, null, REGION)).getErrorCode()); + } + @Test void pullThroughCacheRule_rejectsInvalidInputs() { assertEquals("InvalidParameterException", assertThrows(AwsException.class, diff --git a/tools/partition/partition_literals.py b/tools/partition/partition_literals.py index c10121b677..848e547060 100644 --- a/tools/partition/partition_literals.py +++ b/tools/partition/partition_literals.py @@ -81,7 +81,9 @@ CATEGORIES: tuple[Category, ...] = ( ), Category( "dns-suffix", - re.compile(r"amazonaws\.com(?!\.cn)|cloudfront\.net|signin\.aws|public\.ecr\.aws"), + # A dot may be escaped (amazonaws\\.com in a Java regex), so a hand-written host pattern + # is held to the same rule as a host. + re.compile(r"amazonaws\\*\.com(?!\\*\.cn)|cloudfront\\*\.net|signin\\*\.aws|public\\*\.ecr\\*\.aws"), True, "a commercial DNS suffix or host; derive from the partition's dnsSuffix", ), diff --git a/tools/partition/test_partition_literals.py b/tools/partition/test_partition_literals.py index a3e5a52285..f30cd4c5ab 100644 --- a/tools/partition/test_partition_literals.py +++ b/tools/partition/test_partition_literals.py @@ -116,6 +116,10 @@ def test_arn_regex_dialect_does_not_match_a_plain_literal(): ("public.ecr.aws/lambda/java", True), ("ec2.cn-north-1.amazonaws.com.cn", False), ("c2s.ic.gov", False), + # A host written as a Java regex, its dots escaped, is held to the same rule. + ("[0-9]{12}\\\\.dkr\\\\.ecr\\\\.[a-z0-9-]+\\\\.amazonaws\\\\.com", True), + ("[a-z]+\\\\.cloudfront\\\\.net", True), + ("ecr\\\\.[a-z0-9-]+\\\\.amazonaws\\\\.com\\\\.cn", False), ]) def test_dns_suffix_matches_commercial_hosts_only(text, expected): assert ("dns-suffix" in categories_of(text)) is expected