diff --git a/Makefile b/Makefile index 188c3dcaf4..c03dee2005 100644 --- a/Makefile +++ b/Makefile @@ -172,8 +172,9 @@ partition-audit: ## Print the per-package table of remaining partition literals partition-test: ## Run the partition tooling's unit tests $(PYTHON) -m pytest tools/partition -q -aws-data-sync: ## Regenerate src/main/resources/aws/partitions.json from botocore (commit the result) +aws-data-sync: ## Regenerate src/main/resources/aws/*.json from botocore, the CDK and Terraform (commit the result) $(PYTHON) tools/aws/regen_partitions.py + $(PYTHON) tools/aws/regen_region_facts.py aws-data-check: ## CI gate: the vendored partition data must match a fresh generation @$(PYTHON) tools/aws/regen_partitions.py --check || { \ @@ -182,6 +183,12 @@ aws-data-check: ## CI gate: the vendored partition data must match a fresh gener echo " Run 'make aws-data-sync' and commit the result."; \ exit 1; \ } + @$(PYTHON) tools/aws/regen_region_facts.py --check || { \ + echo ""; \ + echo "error: src/main/resources/aws/region-facts.json is out of date or malformed."; \ + echo " Run 'make aws-data-sync' with local/aws checked out and commit the result."; \ + exit 1; \ + } aws-data-test: ## Run the partition-data generator's unit tests $(PYTHON) -m pytest tools/aws -q diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.md index 1b12a1dda2..1043a97a80 100644 --- a/docs/configuration/partitions.md +++ b/docs/configuration/partitions.md @@ -78,6 +78,23 @@ did before. (`IllegalLocationConstraintException` otherwise). A China or GovCloud client must send the constraint, which the AWS SDKs do; `GetBucketLocation` still answers an empty constraint only for `us-east-1`, in every partition. +- **Hosted zones and console URLs**: load balancer and S3 website hosted zone ids come from a + per-region table (`aws/region-facts.json`, generated from the Terraform provider and the CDK, + which transcribe the AWS General Reference); Network load balancers have their own zone, + distinct from the Application/Classic one. The CloudFront hosted zone is published for `aws` + and `aws-cn`; the SAML sign-on URL for five partitions. The ISO and EUSC regions have no load + balancer hosted zones, and only the two ISO-F regions have an S3 website zone; where a table has + no row the field is omitted rather than guessed. An `EDGE` API Gateway custom domain is refused + outside the commercial partition (`BadRequestException`): GovCloud and ISO have no CloudFront, + and China has no edge-optimized API Gateway + ([China API Gateway](https://docs.amazonaws.cn/en_us/aws/latest/userguide/api-gateway.html)). +- **VPC endpoint service names**: interface endpoints (as `DescribeVpcEndpointServices` lists them) + are `com.amazonaws..` everywhere, except the (region, service) pairs the CDK + lists for China, ISO and EUSC, which reverse the DNS suffix (`cn.com.amazonaws.cn-north-1.lambda`). + GovCloud keeps `com.amazonaws`. Gateway endpoints and their AWS-managed prefix lists (S3, + DynamoDB) are `com.amazonaws..` in every partition. +- **Lambda runtime images** pull from ECR Public (`public.ecr.aws`), which exists only in the + commercial partition; point `FLOCI_SERVICES_LAMBDA_ECR_BASE_URI` at a mirror elsewhere. - **WAF `CLOUDFRONT` scope**: available only where CloudFront exists (`aws`, `aws-cn`), and its resources live in the partition's implicit global region (`cn-northwest-1` in China). @@ -108,7 +125,9 @@ the commercial value or Floci's own base host until sourced: - the Lambda function-URL host outside the commercial partition; - whether AWS managed policy documents differ in content in China or GovCloud; - the API Gateway regional hosted zone per region; -- S3 `LocationConstraint` enum values and Route 53 hosted zones for the ISO and EUSC regions. +- S3 `LocationConstraint` enum values and Route 53 hosted zones for the ISO and EUSC regions; +- the SAML sign-on URL in `aws-iso-e`, `aws-iso-f` and `aws-eusc`, where assertions keep being + checked against the commercial `https://signin.aws.amazon.com/saml`. ## Related diff --git a/docs/services/s3.md b/docs/services/s3.md index ad83d3bd47..2cd95946ed 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -199,6 +199,23 @@ Floci's reserved storage directories (`.accounts`, `.versions`, `.annotations`) `InvalidBucketName`. On the persistent and hybrid backends such a name either climbs out of the owning account's directory or collides with object version and annotation data. +## Bucket Regions + +`CreateBucket` follows AWS's region rules, in every partition. The endpoint a request is signed for +decides what it accepts: + +- The `us-east-1` endpoint takes a `LocationConstraint` naming any other region, and no constraint + at all (the bucket then lives in `us-east-1`). A constraint of `us-east-1` itself is + `InvalidLocationConstraint`. +- Every other regional endpoint requires a constraint naming exactly its own region. No constraint, + or a different region (including `us-east-1`), is `IllegalLocationConstraintException`. + +The AWS SDKs send the constraint for any region but `us-east-1`, so SDK clients are unaffected. A +raw-HTTP client that signs for another region and sends an empty body has to add the +`CreateBucketConfiguration`; the signing region alone does not place a bucket. `GetBucketLocation` +answers an empty constraint only for `us-east-1`. See [AWS Partitions](../configuration/partitions.md) +for how this applies in China and GovCloud. + ## Global Bucket Namespace By default S3 buckets are isolated per account (see [Multi-Account Isolation](../configuration/multi-account.md)), diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegionFacts.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegionFacts.java new file mode 100644 index 0000000000..389f4665c4 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegionFacts.java @@ -0,0 +1,141 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.io.InputStream; +import java.util.LinkedHashSet; +import java.util.Locale; +import java.util.Optional; +import java.util.Set; + +/** + * The per-region and per-partition constants AWS publishes only as prose tables, vendored in + * {@code aws/region-facts.json} by {@code tools/aws/regen_region_facts.py} from the Terraform + * provider and the CDK: load balancer and S3 website hosted zone ids, the CloudFront hosted + * zone per partition, the SAML sign-on URL per partition and the VPC endpoint service-name + * prefixes. Every accessor is {@link Optional}: the ISO and EUSC partitions have no published + * hosted zones, and a service that needs one there has nothing faithful to answer. + */ +public final class AwsRegionFacts { + + static final String RESOURCE_NAME = "aws/region-facts.json"; + private static final String DEFAULT_VPC_ENDPOINT_PREFIX = "com.amazonaws"; + + private static final class Holder { + static final JsonNode FACTS = load(); + } + + private AwsRegionFacts() { + } + + /** Classic load balancers share the Application load balancer hosted zone. */ + public static Optional classicElbHostedZoneId(String region) { + return regionText(region, "classicElbHostedZoneId"); + } + + public static Optional albHostedZoneId(String region) { + return regionText(region, "albHostedZoneId"); + } + + /** Network load balancers have their own hosted zone in every region, distinct from the ALB one. */ + public static Optional nlbHostedZoneId(String region) { + return regionText(region, "nlbHostedZoneId"); + } + + public static Optional s3WebsiteHostedZoneId(String region) { + return regionText(region, "s3WebsiteHostedZoneId"); + } + + /** Published for {@code aws} and {@code aws-cn} only, where CloudFront exists. */ + public static Optional cloudFrontHostedZoneId(String partition) { + return partitionText(partition, "cloudfrontHostedZoneId"); + } + + /** The console's SAML endpoint; absent for {@code aws-iso-e}, {@code aws-iso-f} and {@code aws-eusc}. */ + public static Optional samlSignOnUrl(String partition) { + return partitionText(partition, "samlSignOnUrl"); + } + + /** {@code com.amazonaws.vpce}, {@code cn.com.amazonaws.vpce}, ...: the reversed DNS suffix plus {@code .vpce}. */ + public static Optional vpcEndpointServiceNamePrefix(String partition) { + return partitionText(partition, "vpcEndpointServiceNamePrefix"); + } + + /** + * The AWS-owned VPC endpoint service name for {@code service} in {@code region}, e.g. + * {@code com.amazonaws.us-east-1.s3} or {@code cn.com.amazonaws.cn-north-1.s3}. The reversed + * prefix applies only to the (region, service) pairs the CDK lists; every other pair keeps + * {@code com.amazonaws}, GovCloud included. China's {@code transcribe} also carries a + * trailing {@code .cn}. + */ + public static String vpcEndpointServiceName(String region, String service) { + JsonNode entry = regionNode(region); + String prefix = DEFAULT_VPC_ENDPOINT_PREFIX; + String suffix = ""; + if (entry != null) { + if (contains(entry.path("vpcEndpointPrefixServices"), service)) { + prefix = entry.path("vpcEndpointExceptionPrefix").asText(DEFAULT_VPC_ENDPOINT_PREFIX); + } + if (contains(entry.path("vpcEndpointCnSuffixServices"), service)) { + suffix = ".cn"; + } + } + return prefix + "." + region + "." + service + suffix; + } + + /** The services whose endpoint name in {@code region} uses the reversed-suffix prefix. */ + public static Set vpcEndpointPrefixServices(String region) { + Set services = new LinkedHashSet<>(); + JsonNode entry = regionNode(region); + if (entry != null) { + entry.path("vpcEndpointPrefixServices").forEach(node -> services.add(node.asText())); + } + return services; + } + + private static boolean contains(JsonNode array, String value) { + for (JsonNode node : array) { + if (node.asText().equals(value)) { + return true; + } + } + return false; + } + + private static Optional regionText(String region, String field) { + JsonNode entry = regionNode(region); + return entry == null || !entry.hasNonNull(field) ? Optional.empty() : Optional.of(entry.get(field).asText()); + } + + private static Optional partitionText(String partition, String field) { + if (partition == null) { + return Optional.empty(); + } + JsonNode entry = Holder.FACTS.path("partitions").get(partition.trim().toLowerCase(Locale.ROOT)); + return entry == null || !entry.hasNonNull(field) ? Optional.empty() : Optional.of(entry.get(field).asText()); + } + + private static JsonNode regionNode(String region) { + if (region == null || region.isBlank()) { + return null; + } + return Holder.FACTS.path("regions").get(region.trim().toLowerCase(Locale.ROOT)); + } + + private static JsonNode load() { + try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(RESOURCE_NAME)) { + if (in == null) { + throw new IllegalStateException("Region facts resource not found: " + RESOURCE_NAME); + } + JsonNode root = new ObjectMapper().readTree(in); + if (!root.path("regions").isObject() || !root.path("partitions").isObject()) { + throw new IllegalStateException(RESOURCE_NAME + " lacks the regions or partitions object"); + } + return root; + } catch (IOException e) { + throw new IllegalStateException("Failed to read the region facts " + RESOURCE_NAME, e); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java index 6c482a072d..557dd86fa8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.TlsCertificateManager; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsPartition; +import io.github.hectorvent.floci.core.common.AwsPartitions; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.core.common.ReservedTags; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -65,6 +68,9 @@ import java.util.regex.Pattern; @ApplicationScoped public class ApiGatewayService { + /** The only partition where API Gateway offers edge-optimized APIs and custom domain names. */ + private static final Set EDGE_OPTIMIZED_PARTITIONS = Set.of("aws"); + private static final ObjectMapper JSON = new ObjectMapper(); /** Documented default page size for GetUsage. */ @@ -1886,6 +1892,7 @@ public class ApiGatewayService { // neither regional nor edge-optimized, which nothing here could route or describe. throw new AwsException("BadRequestException", "Invalid value for endpoint type: " + endpointType, 400); } + requireEndpointTypeAvailable(endpointType, region); CustomDomain domain = new CustomDomain(); domain.setDomainName(domainName); @@ -1894,8 +1901,8 @@ public class ApiGatewayService { domain.setRegionalCertificateName((String) request.get("regionalCertificateName")); domain.setRegionalCertificateArn((String) request.get("regionalCertificateArn")); domain.setRegionalDomainName(domainName + ".regional.local"); - domain.setRegionalHostedZoneId("Z2FDTNDATAQYL2"); - applyEndpointType(domain, endpointType); + domain.setRegionalHostedZoneId("Z2FDTNDATAQYL2"); // partition-literal: no published per-region source for the regional zone + applyEndpointType(domain, endpointType, region); domain.setSecurityPolicy((String) request.getOrDefault("securityPolicy", "TLS_1_2")); // Nothing is provisioned behind the domain, so it is usable as soon as it exists. domain.setDomainNameStatus("AVAILABLE"); @@ -1922,12 +1929,26 @@ public class ApiGatewayService { } /** - * An edge-optimized domain fronts a CloudFront distribution, and a DNS alias points at the - * distribution's name in the fixed CloudFront hosted zone AWS documents for every region. A - * regional domain has none, so a move to {@code REGIONAL} drops the distribution again while a - * move to {@code EDGE} puts one in front of the domain, as the migration does on AWS. + * Edge-optimized APIs and domains exist only in the commercial partition: GovCloud and the + * ISO partitions have no CloudFront, and China has CloudFront but no edge-optimized API Gateway + * (https://docs.amazonaws.cn/en_us/aws/latest/userguide/api-gateway.html). No source shows + * AWS's message for the refusal, so the wording is Floci's own. */ - private void applyEndpointType(CustomDomain domain, String endpointType) { + private static void requireEndpointTypeAvailable(String endpointType, String region) { + AwsPartition partition = AwsPartitions.forRegionOrCommercial(region); + if ("EDGE".equals(endpointType) && !EDGE_OPTIMIZED_PARTITIONS.contains(partition.id())) { + throw new AwsException("BadRequestException", + "Endpoint type EDGE is not available in partition " + partition.id() + ".", 400); + } + } + + /** + * An edge-optimized domain fronts a CloudFront distribution, and a DNS alias points at the + * distribution's name in the CloudFront hosted zone of its partition. A regional domain has + * none, so a move to {@code REGIONAL} drops the distribution again while a move to {@code EDGE} + * puts one in front of the domain, as the migration does on AWS. + */ + private void applyEndpointType(CustomDomain domain, String endpointType, String region) { domain.setEndpointConfigurationType(endpointType); if (!"EDGE".equals(endpointType)) { domain.setDistributionDomainName(null); @@ -1936,7 +1957,8 @@ public class ApiGatewayService { domain.setDistributionDomainName( "d" + UUID.randomUUID().toString().replace("-", "").substring(0, 13) + "." + config.services().cloudfront().domainSuffix()); - domain.setDistributionHostedZoneId("Z2FDTNDATAQYW2"); + domain.setDistributionHostedZoneId(AwsRegionFacts.cloudFrontHostedZoneId( + AwsPartitions.forRegionOrCommercial(region).id()).orElse(null)); } } @@ -2050,12 +2072,17 @@ public class ApiGatewayService { throw new AwsException("BadRequestException", "Unsupported path: " + path, 400); } } + // Only a change of type is held to the partition: an EDGE domain stored before the rule + // existed must still take certificate or security-policy updates. + if (!Objects.equals(newEndpointConfigurationType, domain.getEndpointConfigurationType())) { + requireEndpointTypeAvailable(newEndpointConfigurationType, region); + } domain.setCertificateName(newCertificateName); domain.setCertificateArn(newCertificateArn); domain.setRegionalCertificateName(newRegionalCertificateName); domain.setRegionalCertificateArn(newRegionalCertificateArn); domain.setSecurityPolicy(newSecurityPolicy); - applyEndpointType(domain, newEndpointConfigurationType); + applyEndpointType(domain, newEndpointConfigurationType, region); domainStore.put(domainKey, domain); return domain; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index dbd28fbdb6..e346a5c865 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsNamespaces; import io.github.hectorvent.floci.core.common.AwsPartition; import io.github.hectorvent.floci.core.common.AwsPartitions; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.core.common.XmlBuilder; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.services.ec2.model.*; @@ -1584,9 +1585,11 @@ public class Ec2QueryHandler { return xmlResponse(xml.build()); } - // The common AWS interface-endpoint services, as short names. Rendered as - // com.amazonaws... CDK's InterfaceVpcEndpoint (lookupSupportedAzs) - // calls DescribeVpcEndpointServices at synth time; an empty set aborts synth. + // The common AWS interface-endpoint services, as short names. Rendered with the + // region's interface-endpoint prefix: com.amazonaws.., or the reversed + // suffix for the (region, service) pairs AwsRegionFacts lists (cn.com.amazonaws in + // China). CDK's InterfaceVpcEndpoint (lookupSupportedAzs) calls + // DescribeVpcEndpointServices at synth time; an empty set aborts synth. private static final List INTERFACE_ENDPOINT_SERVICES = List.of( "ec2", "ec2messages", "ssm", "ssmmessages", "logs", "monitoring", "sts", "secretsmanager", "kms", "ecr.api", "ecr.dkr", "ecs", "ecs-agent", "ecs-telemetry", @@ -1612,8 +1615,9 @@ public class Ec2QueryHandler { } else { // S3 has both a Gateway and an Interface offering; keep it in the set. for (String name : INTERFACE_ENDPOINT_SERVICES) { - fullNames.add("com.amazonaws." + region + "." + name); + fullNames.add(AwsRegionFacts.vpcEndpointServiceName(region, name)); } + // The S3 gateway service keeps com.amazonaws in every partition. fullNames.add("com.amazonaws." + region + ".s3"); } for (String full : fullNames) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 0667b55b5d..17c84cdc03 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -1224,6 +1224,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { } } + /** + * The managed lists belong to the S3 and DynamoDB gateway endpoints, which are named + * {@code com.amazonaws..} in every partition (the CDK's + * {@code GatewayVpcEndpointAwsService}); the reversed-suffix names apply to interface endpoints only. + */ private List awsManagedPrefixLists(String region) { return List.of( awsManagedPrefixList(region, "pl-63a5400a", "com.amazonaws." + region + ".s3", diff --git a/src/main/java/io/github/hectorvent/floci/services/elb/ElbClassicService.java b/src/main/java/io/github/hectorvent/floci/services/elb/ElbClassicService.java index 9fac65c070..d95356eec4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elb/ElbClassicService.java +++ b/src/main/java/io/github/hectorvent/floci/services/elb/ElbClassicService.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elb; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; import io.github.hectorvent.floci.core.storage.StorageBackedMap; @@ -46,9 +47,6 @@ import java.util.stream.Collectors; @ApplicationScoped public class ElbClassicService { - /** The hosted zone ID AWS reports for Classic load balancers; fixed per region on AWS too. */ - private static final String CANONICAL_HOSTED_ZONE_ID = "Z35SXDOTRQ7X7K"; - private static final SecureRandom RANDOM = new SecureRandom(); private final Ec2Service ec2Service; @@ -101,6 +99,18 @@ public class ElbClassicService { * nothing reachable from {@link #initializeStorage()} may touch an injected collaborator. */ public void restorePersistedRuntime() { + // A balancer stored before the zone was looked up per region still carries the one fixed + // zone every balancer used to report; take the region's zone again and store it back. + for (String region : List.copyOf(loadBalancers.keySet())) { + Map regionLbs = loadBalancers.get(region); + if (regionLbs == null) { + continue; + } + for (ClassicLoadBalancer lb : regionLbs.values()) { + lb.setCanonicalHostedZoneNameId(AwsRegionFacts.classicElbHostedZoneId(region).orElse(null)); + } + loadBalancers.put(region, regionLbs); + } for (Map regionLbs : loadBalancers.values()) { for (ClassicLoadBalancer lb : regionLbs.values()) { healthChecker.startMonitoring(lb); @@ -148,7 +158,7 @@ public class ElbClassicService { } lb.setDnsName(dnsName); lb.setCanonicalHostedZoneName(dnsName); - lb.setCanonicalHostedZoneNameId(CANONICAL_HOSTED_ZONE_ID); + lb.setCanonicalHostedZoneNameId(AwsRegionFacts.classicElbHostedZoneId(region).orElse(null)); lb.setScheme(scheme != null && !scheme.isBlank() ? scheme : "internet-facing"); lb.setCreatedTime(Instant.now()); lb.setListeners(new ArrayList<>(listeners)); diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java index 14796884a2..41c28906de 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; 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.AwsRegionFacts; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.SsrfProtection; import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; @@ -51,8 +52,6 @@ public class ElbV2Service implements ResourceProvider { @Inject EmulatorConfig config; - private static final String CANONICAL_HOSTED_ZONE_ID = "Z35SXDOTRQ7X7K"; - // region → ARN → resource private Map> loadBalancers = new ConcurrentHashMap<>(); private Map> targetGroups = new ConcurrentHashMap<>(); @@ -163,6 +162,7 @@ public class ElbV2Service implements ResourceProvider { */ public void restorePersistedRuntime() { + refreshCanonicalHostedZones(); if (healthChecker != null) { for (Map regionTargetGroups : targetGroups.values()) { for (TargetGroup targetGroup : regionTargetGroups.values()) { @@ -210,7 +210,7 @@ public class ElbV2Service implements ResourceProvider { LoadBalancer lb = new LoadBalancer(); lb.setLoadBalancerArn(arn); lb.setDnsName(dnsName); - lb.setCanonicalHostedZoneId(CANONICAL_HOSTED_ZONE_ID); + lb.setCanonicalHostedZoneId(canonicalHostedZoneId(lbType, region)); lb.setCreatedTime(Instant.now()); lb.setLoadBalancerName(name); lb.setScheme(lbScheme); @@ -1101,6 +1101,37 @@ public class ElbV2Service implements ResourceProvider { return UUID.randomUUID().toString().replace("-", "").substring(0, 16); } + /** + * A load balancer stored before the hosted zones were looked up per region and type still + * carries the one fixed zone every balancer used to report, so restored balancers take the + * zone of their region and type again, written back so the stored copy agrees. + */ + private void refreshCanonicalHostedZones() { + for (String region : List.copyOf(loadBalancers.keySet())) { + Map regionLoadBalancers = loadBalancers.get(region); + if (regionLoadBalancers == null) { + continue; + } + for (LoadBalancer lb : regionLoadBalancers.values()) { + String lbType = lb.getType() != null ? lb.getType() : "application"; + lb.setCanonicalHostedZoneId(canonicalHostedZoneId(lbType, region)); + } + loadBalancers.put(region, regionLoadBalancers); + } + } + + /** + * Network load balancers have their own hosted zone per region, distinct from the one + * Application (and Classic) load balancers share; a gateway load balancer has none. + */ + private static String canonicalHostedZoneId(String type, String region) { + return switch (type) { + case "network" -> AwsRegionFacts.nlbHostedZoneId(region).orElse(null); + case "gateway" -> null; + default -> AwsRegionFacts.albHostedZoneId(region).orElse(null); + }; + } + private static String lbTypePrefix(String type) { return switch (type) { case "network" -> "net"; diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifier.java b/src/main/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifier.java index e788802ee1..4cf880d1bb 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifier.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifier.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.iam; import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.services.iam.model.SAMLProvider; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -130,7 +131,7 @@ final class SAMLAssertionVerifier { } String recipient = confirmationData.getAttribute("Recipient"); Instant confirmationExpiry = instant(confirmationData.getAttribute("NotOnOrAfter")); - if ("https://signin.aws.amazon.com/saml".equals(recipient) + if (expectedRecipient(provider).equals(recipient) && confirmationExpiry != null && now.isBefore(confirmationExpiry)) { validBearer = true; break; @@ -238,4 +239,17 @@ final class SAMLAssertionVerifier { } } + /** + * The console sign-on URL a bearer assertion must name, in the provider's partition. The CDK + * publishes none for {@code aws-iso-e}, {@code aws-iso-f} and {@code aws-eusc}; there the + * commercial URL is kept, as every partition used before the per-partition table, rather than + * refusing every assertion. + */ + // Package-private for unit testing. + static String expectedRecipient(SAMLProvider provider) { + String partition = AwsArnUtils.parse(provider.getArn()).partition(); + return AwsRegionFacts.samlSignOnUrl(partition) + .or(() -> AwsRegionFacts.samlSignOnUrl("aws")) + .orElseThrow(); + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java index 4e46cd8cbb..7fade8fcd5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java @@ -25,7 +25,7 @@ import java.util.regex.Pattern; @ApplicationScoped public class MarketplaceAgreementService implements Resettable { - private static final String REGION = "us-east-1"; + private static final String REGION = "us-east-1"; // partition-literal: AWS Marketplace is commercial-only and AWS pins its APIs to these regions private static final String CATALOG = "AWSMarketplace"; private static final Pattern PROPOSAL_ID = Pattern.compile("(at-|ap-)[A-Za-z0-9]+"); private static final Pattern CLIENT_TOKEN = Pattern.compile("[a-zA-Z0-9-]{1,64}"); @@ -782,7 +782,7 @@ public class MarketplaceAgreementService implements Resettable { private static void validateRegion(String region) { if (!REGION.equals(region)) { - throw validation("AWS Marketplace Agreement API is available only in us-east-1."); + throw validation("AWS Marketplace Agreement API is available only in us-east-1."); // partition-literal: AWS's message text } } diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceCatalogController.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceCatalogController.java index f1818bd17b..03e3af8f86 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceCatalogController.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceCatalogController.java @@ -138,7 +138,7 @@ public class MarketplaceCatalogController { } private String region() { - return context.getRegion() == null ? "us-east-1" : context.getRegion(); + return context.getRegion() == null ? "us-east-1" : context.getRegion(); // partition-literal: fallback to Marketplace's own region when the request carries none } private static Response ok(JsonNode node) { diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentController.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentController.java index 4c121768b2..6dfab23da4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentController.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentController.java @@ -55,7 +55,7 @@ public class MarketplaceDeploymentController { } private String region() { - return requestContext.getRegion() == null ? "us-east-1" : requestContext.getRegion(); + return requestContext.getRegion() == null ? "us-east-1" : requestContext.getRegion(); // partition-literal: fallback to Marketplace's own region when the request carries none } private String accountId() { diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentService.java index 7fd7af465d..5bb9c9d0ce 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentService.java @@ -307,8 +307,8 @@ public class MarketplaceDeploymentService implements Resettable { } private static void validateRegion(String region) { - if (region != null && !region.isBlank() && !"us-east-1".equals(region)) { - throw validation("AWS Marketplace Deployment Service is available only in us-east-1."); + if (region != null && !region.isBlank() && !"us-east-1".equals(region)) { // partition-literal: AWS Marketplace is commercial-only and AWS pins its APIs to these regions + throw validation("AWS Marketplace Deployment Service is available only in us-east-1."); // partition-literal: AWS's message text } } diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryController.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryController.java index ea81d578fa..6eecd2adae 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryController.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryController.java @@ -74,7 +74,7 @@ public class MarketplaceDiscoveryController { } catch (AwsException e) { throw e; } catch (Exception e) { throw validation("Request body is not valid JSON."); } } - private String region() { return context.getRegion() == null ? "us-east-1" : context.getRegion(); } + private String region() { return context.getRegion() == null ? "us-east-1" : context.getRegion(); } // partition-literal: fallback to Marketplace's own region when the request carries none private Response ok(JsonNode node) { return Response.ok(node).build(); } private static AwsException validation(String message) { return new AwsException("ValidationException", message, 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryService.java index 0cd96b0de9..2b67b1eea8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryService.java @@ -23,7 +23,7 @@ import java.util.Set; @ApplicationScoped public class MarketplaceDiscoveryService implements Resettable { - private static final Set REGIONS = Set.of("us-east-1", "us-west-2", "eu-west-1"); + private static final Set REGIONS = Set.of("us-east-1", "us-west-2", "eu-west-1"); // partition-literal: AWS Marketplace is commercial-only and AWS pins its APIs to these regions private static final Set FACET_TYPES = Set.of("AVERAGE_CUSTOMER_RATING", "CATEGORY", "PUBLISHER", "FULFILLMENT_OPTION_TYPE", "PRICING_MODEL", "PRICING_UNIT", "DEPLOYED_ON_AWS", "NUMBER_OF_PRODUCTS"); private static final Set SEARCH_FILTER_TYPES = Set.of("MIN_AVERAGE_CUSTOMER_RATING", "MAX_AVERAGE_CUSTOMER_RATING", "CATEGORY", "PUBLISHER", "FULFILLMENT_OPTION_TYPE", "PRICING_MODEL", "PRICING_UNIT", "DEPLOYED_ON_AWS", "NUMBER_OF_PRODUCTS"); private final ObjectMapper mapper; @@ -412,7 +412,7 @@ public class MarketplaceDiscoveryService implements Resettable { throw validation(f+" is required and must match [\\w-]+."); }return v;} private static void validateRegion(String region){if(!REGIONS.contains(region)){ - throw validation("Marketplace Discovery is available only in us-east-1, us-west-2, and eu-west-1."); + throw validation("Marketplace Discovery is available only in us-east-1, us-west-2, and eu-west-1."); // partition-literal: AWS's message text }} private static AwsException validation(String m){return new AwsException("ValidationException",m,400);} private static AwsException notFound(String k,String id){return new AwsException("ResourceNotFoundException",k+" "+id+" was not found.",404);} diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceEntitlementService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceEntitlementService.java index dcf020d84d..d4a2c58d58 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceEntitlementService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceEntitlementService.java @@ -39,9 +39,9 @@ public class MarketplaceEntitlementService implements Resettable { } ObjectNode getEntitlements(JsonNode request, String region) { - if (region != null && !region.isBlank() && !"us-east-1".equals(region)) { + if (region != null && !region.isBlank() && !"us-east-1".equals(region)) { // partition-literal: AWS Marketplace is commercial-only and AWS pins its APIs to these regions throw new AwsException("InvalidParameterException", - "AWS Marketplace Entitlement Service is available only in us-east-1.", 400); + "AWS Marketplace Entitlement Service is available only in us-east-1.", 400); // partition-literal: AWS's message text } String productCode = requireText(request, "ProductCode", 1, 255); JsonNode filter = request == null ? null : request.get("Filter"); diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceReportingService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceReportingService.java index aa5c66ed76..e682e9cad9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceReportingService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceReportingService.java @@ -47,7 +47,7 @@ public class MarketplaceReportingService { } String token = UUID.randomUUID().toString().replace("-", ""); String dashboardName = dashboard.substring(dashboard.lastIndexOf('/') + 1); - String embedUrl = "https://us-east-1.quicksight.aws.amazon.com/sn/embed/share/accounts/" + String embedUrl = "https://us-east-1.quicksight.aws.amazon.com/sn/embed/share/accounts/" // partition-literal: Marketplace reporting dashboards embed from QuickSight in us-east-1 + accountId + "/dashboards/" + URLEncoder.encode(dashboardName, StandardCharsets.UTF_8) + "?code=" + token; MarketplaceBuyerDashboard result = new MarketplaceBuyerDashboard( diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceService.java index 769fc75f4f..82d34a1592 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceService.java @@ -32,7 +32,7 @@ import java.util.regex.Pattern; @ApplicationScoped public class MarketplaceService implements Resettable { private static final String CATALOG = "AWSMarketplace"; - private static final String REGION = "us-east-1"; + private static final String REGION = "us-east-1"; // partition-literal: AWS Marketplace is commercial-only and AWS pins its APIs to these regions private static final Pattern CLIENT_TOKEN = Pattern.compile("[!-~]{1,64}"); private static final Pattern CHANGE_SET_NAME = Pattern.compile("[\\w\\s+=.:@-]{1,100}"); private static final Set SUPPORTED_CHANGE_TYPES = Set.of( @@ -789,7 +789,7 @@ public class MarketplaceService implements Resettable { private static void validateRegion(String region) { if (!REGION.equals(region)) { - throw validation("AWS Marketplace Catalog API is available only in us-east-1."); + throw validation("AWS Marketplace Catalog API is available only in us-east-1."); // partition-literal: AWS's message text } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 276a9cb067..60317ba7b4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -2061,12 +2061,15 @@ public class S3Controller { } /** - * S3's CreateBucket region rules, the same in every partition (moto's {@code aws_verified} - * matrix): every regional endpoint but {@code us-east-1} requires a constraint naming exactly - * its region and answers {@code IllegalLocationConstraintException} otherwise, including for a - * {@code us-east-1} constraint, so in a China or GovCloud deployment the constraint is de facto - * required; the {@code us-east-1} endpoint takes any constraint but its own, which is - * {@code InvalidLocationConstraint}. + * S3's CreateBucket region rules, the same in every partition: every regional endpoint but + * {@code us-east-1} requires a constraint naming exactly its region and answers + * {@code IllegalLocationConstraintException} otherwise, including for a {@code us-east-1} + * constraint, so in a China or GovCloud deployment the constraint is de facto required; the + * {@code us-east-1} endpoint takes any constraint but its own, which is + * {@code InvalidLocationConstraint}. The S3 model backs the us-east-1 rules on its own: + * {@code us-east-1} is the one region missing from the {@code BucketLocationConstraint} enum, + * and {@code GetBucketLocationOutput} gives buckets in {@code us-east-1} a null constraint. The + * exact error codes and messages come from moto's {@code aws_verified} CreateBucket tests. */ static String bucketRegionForCreate(String locationConstraint, String endpointRegion) { boolean globalEndpoint = US_EAST_1.equals(endpointRegion); 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 89d53ec711..190c583ec3 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 @@ -278,7 +278,7 @@ public class SigninService { long now = issuedAt.getEpochSecond(); String header = encodeJson(Map.of("alg", "none", "typ", "JWT")); String payload = encodeJson(Map.of( - "iss", "https://signin.amazonaws.com", + "iss", "https://signin.amazonaws.com", // partition-literal: floci's own issuer; the console issuer has no published source "sub", principalArn, "aud", clientId, "aws_account_id", accountId, diff --git a/src/main/resources/aws/region-facts.json b/src/main/resources/aws/region-facts.json new file mode 100644 index 0000000000..3f2fb155ed --- /dev/null +++ b/src/main/resources/aws/region-facts.json @@ -0,0 +1,499 @@ +{ + "_source": { + "generator": "tools/aws/regen_region_facts.py", + "terraform": "terraform-provider-aws internal/service/elb/hosted_zone_id_data_source.go, internal/service/elbv2/hosted_zone_id_data_source.go, internal/service/s3/hosted_zones.go, internal/conns/awsclient.go", + "cdk": "aws-cdk packages/aws-cdk-lib/region-info/build-tools/fact-tables.ts, packages/aws-cdk-lib/aws-route53-targets/lib/cloudfront-target.ts, packages/aws-cdk-lib/aws-ec2/lib/vpc-endpoint.ts", + "s3WebsiteConflicts": "terraform wins over the CDK where they disagree (it cites the S3 website-endpoint table)" + }, + "partitions": { + "aws": { + "vpcEndpointServiceNamePrefix": "com.amazonaws.vpce", + "cloudfrontHostedZoneId": "Z2FDTNDATAQYW2", + "samlSignOnUrl": "https://signin.aws.amazon.com/saml" + }, + "aws-cn": { + "vpcEndpointServiceNamePrefix": "cn.com.amazonaws.vpce", + "cloudfrontHostedZoneId": "Z3RFFRIM2A3IF5", + "samlSignOnUrl": "https://signin.amazonaws.cn/saml" + }, + "aws-eusc": { + "vpcEndpointServiceNamePrefix": "eu.amazonaws.vpce" + }, + "aws-iso": { + "vpcEndpointServiceNamePrefix": "gov.ic.c2s.vpce", + "samlSignOnUrl": "https://signin.c2shome.ic.gov/saml" + }, + "aws-iso-b": { + "vpcEndpointServiceNamePrefix": "gov.sgov.sc2s.vpce", + "samlSignOnUrl": "https://signin.sc2shome.sgov.gov/saml" + }, + "aws-iso-e": { + "vpcEndpointServiceNamePrefix": "uk.adc-e.cloud.vpce" + }, + "aws-iso-f": { + "vpcEndpointServiceNamePrefix": "gov.ic.hci.csp.vpce" + }, + "aws-us-gov": { + "vpcEndpointServiceNamePrefix": "com.amazonaws.vpce", + "samlSignOnUrl": "https://signin.amazonaws-us-gov.com/saml" + } + }, + "regions": { + "af-south-1": { + "albHostedZoneId": "Z268VQBMOI5EKX", + "classicElbHostedZoneId": "Z268VQBMOI5EKX", + "nlbHostedZoneId": "Z203XCE67M25HM", + "s3WebsiteHostedZoneId": "Z83WF9RJE8B12" + }, + "ap-east-1": { + "albHostedZoneId": "Z3DQVH9N71FHZ0", + "classicElbHostedZoneId": "Z3DQVH9N71FHZ0", + "nlbHostedZoneId": "Z12Y7K3UBGUAD1", + "s3WebsiteHostedZoneId": "ZNB98KWMFR0R6" + }, + "ap-east-2": { + "albHostedZoneId": "Z02789141MW7T1WBU19PO", + "classicElbHostedZoneId": "Z02789141MW7T1WBU19PO", + "nlbHostedZoneId": "Z09176273OC2HWIAUNYW", + "s3WebsiteHostedZoneId": "Z064739330DAH7WJVOO93" + }, + "ap-northeast-1": { + "albHostedZoneId": "Z14GRHDCWA56QT", + "classicElbHostedZoneId": "Z14GRHDCWA56QT", + "nlbHostedZoneId": "Z31USIVHYNEOWT", + "s3WebsiteHostedZoneId": "Z2M4EHUR26P7ZW" + }, + "ap-northeast-2": { + "albHostedZoneId": "ZWKZPGTI48KDX", + "classicElbHostedZoneId": "ZWKZPGTI48KDX", + "nlbHostedZoneId": "ZIBE1TIR4HY56", + "s3WebsiteHostedZoneId": "Z3W03O7B5YMIYP" + }, + "ap-northeast-3": { + "albHostedZoneId": "Z5LXEXXYW11ES", + "classicElbHostedZoneId": "Z5LXEXXYW11ES", + "nlbHostedZoneId": "Z1GWIQ4HH19I5X", + "s3WebsiteHostedZoneId": "Z2YQB5RD63NC85" + }, + "ap-south-1": { + "albHostedZoneId": "ZP97RAFLXTNZK", + "classicElbHostedZoneId": "ZP97RAFLXTNZK", + "nlbHostedZoneId": "ZVDDRBQ08TROA", + "s3WebsiteHostedZoneId": "Z11RGJOFQNVJUP" + }, + "ap-south-2": { + "albHostedZoneId": "Z0173938T07WNTVAEPZN", + "classicElbHostedZoneId": "Z0173938T07WNTVAEPZN", + "nlbHostedZoneId": "Z0711778386UTO08407HT", + "s3WebsiteHostedZoneId": "Z02976202B4EZMXIPMXF7" + }, + "ap-southeast-1": { + "albHostedZoneId": "Z1LMS91P8CMLE5", + "classicElbHostedZoneId": "Z1LMS91P8CMLE5", + "nlbHostedZoneId": "ZKVM4W9LS7TM", + "s3WebsiteHostedZoneId": "Z3O0J2DXBE1FTB" + }, + "ap-southeast-2": { + "albHostedZoneId": "Z1GM3OXH4ZPM65", + "classicElbHostedZoneId": "Z1GM3OXH4ZPM65", + "nlbHostedZoneId": "ZCT6FZBF4DROD", + "s3WebsiteHostedZoneId": "Z1WCIGYICN2BYD" + }, + "ap-southeast-3": { + "albHostedZoneId": "Z08888821HLRG5A9ZRTER", + "classicElbHostedZoneId": "Z08888821HLRG5A9ZRTER", + "nlbHostedZoneId": "Z01971771FYVNCOVWJU1G", + "s3WebsiteHostedZoneId": "Z01846753K324LI26A3VV" + }, + "ap-southeast-4": { + "albHostedZoneId": "Z09517862IB2WZLPXG76F", + "classicElbHostedZoneId": "Z09517862IB2WZLPXG76F", + "nlbHostedZoneId": "Z01156963G8MIIL7X90IV", + "s3WebsiteHostedZoneId": "Z0312387243XT5FE14WFO" + }, + "ap-southeast-5": { + "albHostedZoneId": "Z06010284QMVVW7WO5J", + "classicElbHostedZoneId": "Z06010284QMVVW7WO5J", + "nlbHostedZoneId": "Z026317210H9ACVTRO6FB", + "s3WebsiteHostedZoneId": "Z08660063OXLMA7F1FJHU" + }, + "ap-southeast-6": { + "albHostedZoneId": "Z023301818UFJ50CIO0MV", + "classicElbHostedZoneId": "Z023301818UFJ50CIO0MV", + "nlbHostedZoneId": "Z01392953RKV2Q3RBP0KU", + "s3WebsiteHostedZoneId": "Z05686083R66JX5C163TC" + }, + "ap-southeast-7": { + "albHostedZoneId": "Z0390008CMBRTHFGWBCB", + "classicElbHostedZoneId": "Z0390008CMBRTHFGWBCB", + "nlbHostedZoneId": "Z054363131YWATEMWRG5L", + "s3WebsiteHostedZoneId": "Z0031014GXUMRZG6I14G" + }, + "ca-central-1": { + "albHostedZoneId": "ZQSVJUPU6J1EY", + "classicElbHostedZoneId": "ZQSVJUPU6J1EY", + "nlbHostedZoneId": "Z2EPGBW3API2WT", + "s3WebsiteHostedZoneId": "Z1QDHH18159H29" + }, + "ca-west-1": { + "albHostedZoneId": "Z06473681N0SF6OS049SD", + "classicElbHostedZoneId": "Z06473681N0SF6OS049SD", + "nlbHostedZoneId": "Z02754302KBB00W2LKWZ9", + "s3WebsiteHostedZoneId": "Z03565811Z33SLEZTHOUL" + }, + "cn-north-1": { + "albHostedZoneId": "Z1GDH35T77C1KE", + "classicElbHostedZoneId": "Z1GDH35T77C1KE", + "nlbHostedZoneId": "Z3QFB96KMJ7ED6", + "s3WebsiteHostedZoneId": "Z5CN8UMXT92WN", + "vpcEndpointExceptionPrefix": "cn.com.amazonaws", + "vpcEndpointPrefixServices": [ + "application-autoscaling", + "appmesh-envoy-management", + "athena", + "autoscaling", + "awsconnector", + "backup", + "batch", + "cassandra", + "cloudcontrolapi", + "cloudformation", + "codedeploy-commands-secure", + "databrew", + "dms", + "ebs", + "ec2", + "ecr.api", + "ecr.dkr", + "eks", + "elasticache", + "elasticbeanstalk", + "elasticfilesystem", + "elasticfilesystem-fips", + "emr-containers", + "execute-api", + "fsx", + "imagebuilder", + "iot.data", + "iotsitewise.api", + "iotsitewise.data", + "kinesis-streams", + "lambda", + "license-manager", + "monitoring", + "rds", + "redshift", + "redshift-data", + "s3", + "sagemaker.api", + "sagemaker.featurestore-runtime", + "sagemaker.runtime", + "securityhub", + "servicecatalog", + "sms", + "sqs", + "states", + "sts", + "sync-states", + "synthetics", + "transcribe", + "transcribestreaming", + "transfer", + "xray" + ], + "vpcEndpointCnSuffixServices": [ + "transcribe" + ] + }, + "cn-northwest-1": { + "albHostedZoneId": "ZM7IZAIOVVDZF", + "classicElbHostedZoneId": "ZM7IZAIOVVDZF", + "nlbHostedZoneId": "ZQEIKTCZ8352D", + "s3WebsiteHostedZoneId": "Z282HJ1KT0DH03", + "vpcEndpointExceptionPrefix": "cn.com.amazonaws", + "vpcEndpointPrefixServices": [ + "account", + "application-autoscaling", + "appmesh-envoy-management", + "athena", + "autoscaling", + "awsconnector", + "backup", + "batch", + "cassandra", + "cloudcontrolapi", + "cloudformation", + "codedeploy-commands-secure", + "databrew", + "dms", + "ebs", + "ec2", + "ecr.api", + "ecr.dkr", + "eks", + "elasticache", + "elasticbeanstalk", + "elasticfilesystem", + "elasticfilesystem-fips", + "emr-containers", + "execute-api", + "fsx", + "imagebuilder", + "iot.data", + "kinesis-streams", + "lambda", + "license-manager", + "monitoring", + "polly", + "rds", + "redshift", + "redshift-data", + "s3", + "sagemaker.api", + "sagemaker.featurestore-runtime", + "sagemaker.runtime", + "securityhub", + "servicecatalog", + "sms", + "sqs", + "states", + "sts", + "sync-states", + "synthetics", + "transcribe", + "transcribestreaming", + "transfer", + "workspaces", + "xray" + ], + "vpcEndpointCnSuffixServices": [ + "transcribe" + ] + }, + "eu-central-1": { + "albHostedZoneId": "Z215JYRZR1TBD5", + "classicElbHostedZoneId": "Z215JYRZR1TBD5", + "nlbHostedZoneId": "Z3F0SRJ5LGBH90", + "s3WebsiteHostedZoneId": "Z21DNDUVLTQW6Q" + }, + "eu-central-2": { + "albHostedZoneId": "Z06391101F2ZOEP8P5EB3", + "classicElbHostedZoneId": "Z06391101F2ZOEP8P5EB3", + "nlbHostedZoneId": "Z02239872DOALSIDCX66S", + "s3WebsiteHostedZoneId": "Z030506016YDQGETNASS" + }, + "eu-isoe-west-1": { + "vpcEndpointExceptionPrefix": "uk.adc-e.cloud", + "vpcEndpointPrefixServices": [ + "ebs", + "ecr.api", + "ecr.dkr", + "execute-api" + ] + }, + "eu-north-1": { + "albHostedZoneId": "Z23TAZ6LKFMNIO", + "classicElbHostedZoneId": "Z23TAZ6LKFMNIO", + "nlbHostedZoneId": "Z1UDT6IFJ4EJM", + "s3WebsiteHostedZoneId": "Z3BAZG2TWCNX0D" + }, + "eu-south-1": { + "albHostedZoneId": "Z3ULH7SSC9OV64", + "classicElbHostedZoneId": "Z3ULH7SSC9OV64", + "nlbHostedZoneId": "Z23146JA1KNAFP", + "s3WebsiteHostedZoneId": "Z30OZKI7KPW7MI" + }, + "eu-south-2": { + "albHostedZoneId": "Z0956581394HF5D5LXGAP", + "classicElbHostedZoneId": "Z0956581394HF5D5LXGAP", + "nlbHostedZoneId": "Z1011216NVTVYADP1SSV", + "s3WebsiteHostedZoneId": "Z0081959F7139GRJC19J" + }, + "eu-west-1": { + "albHostedZoneId": "Z32O12XQLNTSW2", + "classicElbHostedZoneId": "Z32O12XQLNTSW2", + "nlbHostedZoneId": "Z2IFOLAFXWLO4F", + "s3WebsiteHostedZoneId": "Z1BKCTXD74EZPE" + }, + "eu-west-2": { + "albHostedZoneId": "ZHURV8PSTC4K8", + "classicElbHostedZoneId": "ZHURV8PSTC4K8", + "nlbHostedZoneId": "ZD4D7Y8KGAS4G", + "s3WebsiteHostedZoneId": "Z3GKZC51ZF0DB4" + }, + "eu-west-3": { + "albHostedZoneId": "Z3Q77PNBQS71R4", + "classicElbHostedZoneId": "Z3Q77PNBQS71R4", + "nlbHostedZoneId": "Z1CMS0P5QUZ6D5", + "s3WebsiteHostedZoneId": "Z3R1K369G5AVDG" + }, + "eusc-de-east-1": { + "vpcEndpointExceptionPrefix": "eu.amazonaws", + "vpcEndpointPrefixServices": [ + "ecr.api", + "ecr.dkr", + "execute-api", + "securityhub" + ] + }, + "il-central-1": { + "albHostedZoneId": "Z09170902867EHPV2DABU", + "classicElbHostedZoneId": "Z09170902867EHPV2DABU", + "nlbHostedZoneId": "Z0313266YDI6ZRHTGQY4", + "s3WebsiteHostedZoneId": "Z09640613K4A3MN55U7GU" + }, + "me-central-1": { + "albHostedZoneId": "Z08230872XQRWHG2XF6I", + "classicElbHostedZoneId": "Z08230872XQRWHG2XF6I", + "nlbHostedZoneId": "Z00282643NTTLPANJJG2P", + "s3WebsiteHostedZoneId": "Z06143092I8HRXZRUZROF" + }, + "me-south-1": { + "albHostedZoneId": "ZS929ML54UICD", + "classicElbHostedZoneId": "ZS929ML54UICD", + "nlbHostedZoneId": "Z3QSRYVP46NYYV", + "s3WebsiteHostedZoneId": "Z1MPMWCPA7YB62" + }, + "mx-central-1": { + "albHostedZoneId": "Z023552324OKD1BB28BH5", + "classicElbHostedZoneId": "Z023552324OKD1BB28BH5", + "nlbHostedZoneId": "Z02031231H3ID6HYJ9A7U", + "s3WebsiteHostedZoneId": "Z057606446ZNVQJJ8WOP" + }, + "sa-east-1": { + "albHostedZoneId": "Z2P70J7HTTTPLU", + "classicElbHostedZoneId": "Z2P70J7HTTTPLU", + "nlbHostedZoneId": "ZTK26PT1VY4CU", + "s3WebsiteHostedZoneId": "Z7KQH4QJS55SO" + }, + "us-east-1": { + "albHostedZoneId": "Z35SXDOTRQ7X7K", + "classicElbHostedZoneId": "Z35SXDOTRQ7X7K", + "nlbHostedZoneId": "Z26RNL4JYFTOTI", + "s3WebsiteHostedZoneId": "Z3AQBSTGFYJSTF" + }, + "us-east-2": { + "albHostedZoneId": "Z3AADJGX6KTTL2", + "classicElbHostedZoneId": "Z3AADJGX6KTTL2", + "nlbHostedZoneId": "ZLMOA37VPKANP", + "s3WebsiteHostedZoneId": "Z2O1EMRO9K5GLX" + }, + "us-gov-east-1": { + "albHostedZoneId": "Z166TLBEWOO7G0", + "classicElbHostedZoneId": "Z166TLBEWOO7G0", + "nlbHostedZoneId": "Z1ZSMQQ6Q24QQ8", + "s3WebsiteHostedZoneId": "Z2NIFVYYW2VKV1" + }, + "us-gov-west-1": { + "albHostedZoneId": "Z33AYJ8TM3BH4J", + "classicElbHostedZoneId": "Z33AYJ8TM3BH4J", + "nlbHostedZoneId": "ZMG1MZ2THAWF1", + "s3WebsiteHostedZoneId": "Z31GFT0UA1I2HV" + }, + "us-iso-east-1": { + "vpcEndpointExceptionPrefix": "gov.ic.c2s", + "vpcEndpointPrefixServices": [ + "application-autoscaling", + "athena", + "autoscaling", + "comprehend", + "diode-messaging", + "diode-messaging-proxy", + "ebs", + "ec2", + "ecr.api", + "ecr.dkr", + "elasticfilesystem", + "elasticfilesystem-fips", + "execute-api", + "sagemaker.api", + "sagemaker.runtime", + "sns", + "sqs", + "textract", + "textract-fips", + "transcribe", + "workspaces" + ] + }, + "us-iso-west-1": { + "vpcEndpointExceptionPrefix": "gov.ic.c2s", + "vpcEndpointPrefixServices": [ + "autoscaling", + "ebs", + "ec2", + "ecr.api", + "ecr.dkr", + "elasticfilesystem", + "elasticfilesystem-fips", + "execute-api", + "monitoring", + "sns", + "sqs", + "workspaces" + ] + }, + "us-isob-east-1": { + "vpcEndpointExceptionPrefix": "gov.sgov.sc2s", + "vpcEndpointPrefixServices": [ + "application-autoscaling", + "autoscaling", + "diode-messaging", + "diode-messaging-proxy", + "ebs", + "ec2", + "ecr.api", + "ecr.dkr", + "elasticfilesystem", + "elasticfilesystem-fips", + "execute-api", + "sagemaker.api", + "sagemaker.runtime", + "sns", + "sqs", + "workspaces" + ] + }, + "us-isob-west-1": { + "vpcEndpointExceptionPrefix": "gov.sgov.sc2s", + "vpcEndpointPrefixServices": [ + "ecr.api", + "ecr.dkr", + "elasticfilesystem-fips", + "execute-api" + ] + }, + "us-isof-east-1": { + "s3WebsiteHostedZoneId": "Z03373031FTGQH4MNG6ST", + "vpcEndpointExceptionPrefix": "gov.ic.hci.csp", + "vpcEndpointPrefixServices": [ + "ebs", + "ecr.api", + "ecr.dkr", + "execute-api" + ] + }, + "us-isof-south-1": { + "s3WebsiteHostedZoneId": "Z03376072I8GXC2DXUFXI", + "vpcEndpointExceptionPrefix": "gov.ic.hci.csp", + "vpcEndpointPrefixServices": [ + "ebs", + "ecr.api", + "ecr.dkr", + "execute-api" + ] + }, + "us-west-1": { + "albHostedZoneId": "Z368ELLRRE2KJ0", + "classicElbHostedZoneId": "Z368ELLRRE2KJ0", + "nlbHostedZoneId": "Z24FKFUX50B4VW", + "s3WebsiteHostedZoneId": "Z2F56UZL2M1ACD" + }, + "us-west-2": { + "albHostedZoneId": "Z1H1FL5HABSF5", + "classicElbHostedZoneId": "Z1H1FL5HABSF5", + "nlbHostedZoneId": "Z18D5FSROUN65G", + "s3WebsiteHostedZoneId": "Z3BJ6K6RIION7M" + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionFactsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionFactsTest.java new file mode 100644 index 0000000000..bb340c2562 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionFactsTest.java @@ -0,0 +1,77 @@ +package io.github.hectorvent.floci.core.common; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** The vendored per-region tables, pinned against the Terraform and CDK rows they came from. */ +class AwsRegionFactsTest { + + @ParameterizedTest + @CsvSource({ + "us-east-1, Z35SXDOTRQ7X7K, Z26RNL4JYFTOTI, Z3AQBSTGFYJSTF", + "cn-north-1, Z1GDH35T77C1KE, Z3QFB96KMJ7ED6, Z5CN8UMXT92WN", + "us-gov-west-1, Z33AYJ8TM3BH4J, ZMG1MZ2THAWF1, Z31GFT0UA1I2HV", + "af-south-1, Z268VQBMOI5EKX, Z203XCE67M25HM, Z83WF9RJE8B12", + "mx-central-1, Z023552324OKD1BB28BH5, Z02031231H3ID6HYJ9A7U, Z057606446ZNVQJJ8WOP"}) + void loadBalancerAndWebsiteZonesFollowTheTerraformTables(String region, String alb, String nlb, String website) { + assertEquals(Optional.of(alb), AwsRegionFacts.albHostedZoneId(region)); + assertEquals(Optional.of(alb), AwsRegionFacts.classicElbHostedZoneId(region), "classic shares the ALB zone"); + assertEquals(Optional.of(nlb), AwsRegionFacts.nlbHostedZoneId(region)); + assertEquals(Optional.of(website), AwsRegionFacts.s3WebsiteHostedZoneId(region)); + } + + /** The ISO-F website zones exist only in the CDK table; the ISO load balancer zones nowhere. */ + @Test + void isoRegionsHaveOnlyWhatIsPublished() { + assertEquals(Optional.of("Z03376072I8GXC2DXUFXI"), AwsRegionFacts.s3WebsiteHostedZoneId("us-isof-south-1")); + assertTrue(AwsRegionFacts.albHostedZoneId("us-isof-south-1").isEmpty()); + assertTrue(AwsRegionFacts.nlbHostedZoneId("us-iso-east-1").isEmpty()); + assertTrue(AwsRegionFacts.s3WebsiteHostedZoneId("eusc-de-east-1").isEmpty()); + assertTrue(AwsRegionFacts.albHostedZoneId("xx-nowhere-9").isEmpty()); + assertTrue(AwsRegionFacts.albHostedZoneId(null).isEmpty()); + } + + @Test + void cloudFrontZoneAndSamlUrlArePerPartition() { + assertEquals(Optional.of("Z2FDTNDATAQYW2"), AwsRegionFacts.cloudFrontHostedZoneId("aws")); + assertEquals(Optional.of("Z3RFFRIM2A3IF5"), AwsRegionFacts.cloudFrontHostedZoneId("aws-cn")); + assertTrue(AwsRegionFacts.cloudFrontHostedZoneId("aws-us-gov").isEmpty()); + assertEquals(Optional.of("https://signin.aws.amazon.com/saml"), AwsRegionFacts.samlSignOnUrl("aws")); + assertEquals(Optional.of("https://signin.amazonaws.cn/saml"), AwsRegionFacts.samlSignOnUrl("aws-cn")); + assertEquals(Optional.of("https://signin.amazonaws-us-gov.com/saml"), AwsRegionFacts.samlSignOnUrl("aws-us-gov")); + assertEquals(Optional.of("https://signin.sc2shome.sgov.gov/saml"), AwsRegionFacts.samlSignOnUrl("aws-iso-b")); + assertTrue(AwsRegionFacts.samlSignOnUrl("aws-eusc").isEmpty()); + assertTrue(AwsRegionFacts.samlSignOnUrl(null).isEmpty()); + } + + @ParameterizedTest + @CsvSource({ + "us-east-1, s3, com.amazonaws.us-east-1.s3", + "us-gov-west-1, s3, com.amazonaws.us-gov-west-1.s3", + "cn-north-1, s3, cn.com.amazonaws.cn-north-1.s3", + "cn-north-1, dynamodb, com.amazonaws.cn-north-1.dynamodb", + "cn-northwest-1, transcribe, cn.com.amazonaws.cn-northwest-1.transcribe.cn", + "us-iso-east-1, ecr.api, gov.ic.c2s.us-iso-east-1.ecr.api", + "us-iso-east-1, s3, com.amazonaws.us-iso-east-1.s3", + "eusc-de-east-1, execute-api, eu.amazonaws.eusc-de-east-1.execute-api", + "us-isof-south-1, ebs, gov.ic.hci.csp.us-isof-south-1.ebs"}) + void vpcEndpointServiceNamesReverseTheSuffixOnlyForTheListedPairs(String region, String service, String expected) { + assertEquals(expected, AwsRegionFacts.vpcEndpointServiceName(region, service)); + } + + @Test + void vpcEndpointServiceNamePrefixIsTheReversedSuffix() { + assertEquals(Optional.of("com.amazonaws.vpce"), AwsRegionFacts.vpcEndpointServiceNamePrefix("aws")); + assertEquals(Optional.of("com.amazonaws.vpce"), AwsRegionFacts.vpcEndpointServiceNamePrefix("aws-us-gov")); + assertEquals(Optional.of("cn.com.amazonaws.vpce"), AwsRegionFacts.vpcEndpointServiceNamePrefix("aws-cn")); + assertEquals(Optional.of("gov.sgov.sc2s.vpce"), AwsRegionFacts.vpcEndpointServiceNamePrefix("aws-iso-b")); + assertEquals(52, AwsRegionFacts.vpcEndpointPrefixServices("cn-north-1").size()); + assertTrue(AwsRegionFacts.vpcEndpointPrefixServices("us-east-1").isEmpty()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java index d8bfbbe7fb..aa855e7b4c 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java @@ -9,6 +9,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import java.util.List; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasSize; @@ -64,6 +66,40 @@ class PartitionRegionGatesIntegrationTest { } } + /** + * Edge-optimized domains exist only in the commercial partition: GovCloud has no CloudFront, and + * China has CloudFront but no edge-optimized API Gateway. A regional domain needs neither. + */ + @Test + void edgeCustomDomainsAreRejectedOutsideTheCommercialPartition() { + String suffix = Long.toString(System.nanoTime(), 36); + for (String region : List.of("us-gov-west-1", "cn-north-1")) { + String partition = AwsRegions.partitionFor(region); + given() + .header("Authorization", PartitionMatrix.sigV4Auth(region, "apigateway")) + .contentType("application/json") + .body("{\"domainName\":\"edge-" + suffix + "." + region + ".example.com\"," + + "\"certificateArn\":\"arn:" + partition + ":acm:" + region + ":000000000000:certificate/edge\"," + + "\"endpointConfiguration\":{\"types\":[\"EDGE\"]}}") + .when().post("/domainnames") + .then().statusCode(400) + .body(containsString("not available in partition " + partition)); + } + + String regional = "regional-" + suffix + ".example.com"; + given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-gov-west-1", "apigateway")) + .contentType("application/json") + .body("{\"domainName\":\"" + regional + "\"," + + "\"regionalCertificateArn\":\"arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/regional\"," + + "\"endpointConfiguration\":{\"types\":[\"REGIONAL\"]}}") + .when().post("/domainnames") + .then().statusCode(201); + cleanup.register(() -> given() + .header("Authorization", PartitionMatrix.sigV4Auth("us-gov-west-1", "apigateway")) + .when().delete("/domainnames/" + regional)); + } + @Test void cloudFrontScopeLivesInThePartitionsImplicitGlobalRegion() { String name = "cn-" + Long.toString(System.nanoTime(), 36); @@ -81,6 +117,32 @@ class PartitionRegionGatesIntegrationTest { assertTrue(arn.startsWith("arn:aws-cn:wafv2:cn-northwest-1:000000000000:global/ipset/" + name + "/"), arn); } + /** + * The AWS-managed prefix lists belong to the S3 and DynamoDB gateway endpoints, which keep + * {@code com.amazonaws} in every partition; only interface endpoint names take the reversed + * suffix China lists. + */ + @Test + void gatewayPrefixListsKeepComAmazonawsWhileChinaInterfaceNamesReverseTheSuffix() { + List names = given() + .header("Authorization", PartitionMatrix.sigV4Auth("cn-north-1", "ec2")) + .formParam("Action", "DescribeManagedPrefixLists") + .formParam("Version", "2016-11-15") + .when().post("/").then().statusCode(200) + .extract().xmlPath().getList("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListName"); + assertTrue(names.contains("com.amazonaws.cn-north-1.s3"), names.toString()); + assertTrue(names.contains("com.amazonaws.cn-north-1.dynamodb"), names.toString()); + + List services = given() + .header("Authorization", PartitionMatrix.sigV4Auth("cn-north-1", "ec2")) + .formParam("Action", "DescribeVpcEndpointServices") + .formParam("Version", "2016-11-15") + .when().post("/").then().statusCode(200) + .extract().xmlPath().getList("DescribeVpcEndpointServicesResponse.serviceNameSet.item"); + assertTrue(services.contains("cn.com.amazonaws.cn-north-1.lambda"), services.toString()); + assertTrue(services.contains("com.amazonaws.cn-north-1.s3"), services.toString()); + } + @Test void lightsailRegionsAreEmptyOutsideTheCommercialPartition() { given() diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java new file mode 100644 index 0000000000..3ee22ac022 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java @@ -0,0 +1,91 @@ +package io.github.hectorvent.floci.services.apigateway; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.TlsCertificateManager; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.apigateway.model.CustomDomain; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * An edge-optimized custom domain exists only in the commercial partition, so it cannot be created + * or switched to elsewhere. A domain stored before that rule still has to take updates + * that leave its endpoint type alone. + */ +class ApiGatewayEdgeDomainPartitionTest { + + private static final String GOV_REGION = "us-gov-west-1"; + + private final Map> stores = new HashMap<>(); + private ApiGatewayService service; + + @BeforeEach + void setUp() { + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation -> + stores.computeIfAbsent(invocation.getArgument(1, String.class), + ignored -> AccountAwareStorageBackend.inMemory("000000000000"))); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().cloudfront().domainSuffix()).thenReturn("cloudfront.net"); + service = new ApiGatewayService(storageFactory, config, mock(TlsCertificateManager.class)); + } + + @SuppressWarnings("unchecked") + private AccountAwareStorageBackend domainStore() { + return (AccountAwareStorageBackend) stores.get("apigateway-domains.json"); + } + + @Test + void aStoredEdgeDomainTakesUpdatesThatLeaveItsTypeAlone() { + CustomDomain legacy = new CustomDomain(); + legacy.setDomainName("legacy.example.com"); + legacy.setEndpointConfigurationType("EDGE"); + legacy.setCertificateArn("arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/old"); + domainStore().put(GOV_REGION + "::legacy.example.com", legacy); + + CustomDomain updated = service.updateDomainName(GOV_REGION, "legacy.example.com", + List.of(Map.of("op", "replace", "path", "/certificateArn", + "value", "arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/new"))); + + assertEquals("arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/new", updated.getCertificateArn()); + assertEquals("EDGE", updated.getEndpointConfigurationType()); + } + + @Test + void switchingADomainToEdgeIsRefusedWhereThePartitionHasNoCloudFront() { + service.createDomainName(GOV_REGION, Map.of("domainName", "regional.example.com", + "regionalCertificateArn", "arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/r", + "endpointConfiguration", Map.of("types", List.of("REGIONAL")))); + + AwsException error = assertThrows(AwsException.class, () -> service.updateDomainName(GOV_REGION, + "regional.example.com", List.of(Map.of("op", "replace", + "path", "/endpointConfiguration/types/REGIONAL", "value", "EDGE")))); + + assertEquals("BadRequestException", error.getErrorCode()); + } + + /** China has CloudFront but no edge-optimized API Gateway. */ + @Test + void anEdgeDomainIsRefusedInChina() { + AwsException error = assertThrows(AwsException.class, () -> service.createDomainName("cn-north-1", + Map.of("domainName", "edge.example.cn", + "certificateArn", "arn:aws-cn:acm:cn-north-1:000000000000:certificate/e", + "endpointConfiguration", Map.of("types", List.of("EDGE"))))); + + assertEquals("BadRequestException", error.getErrorCode()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/elb/ElbClassicServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elb/ElbClassicServiceTest.java index ae66c8a737..17cae367f6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elb/ElbClassicServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elb/ElbClassicServiceTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.elb; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; import io.github.hectorvent.floci.services.ec2.model.Subnet; @@ -255,4 +256,17 @@ class ElbClassicServiceTest { service.createLoadBalancerListeners(REGION, "my-elb", List.of(httpListener())); assertEquals(1, lb.getListeners().size()); } + + /** A balancer stored before the zone was looked up per region carries the old fixed zone. */ + @Test + void aRestoredBalancerTakesTheHostedZoneOfItsRegion() { + create("stored-elb").setCanonicalHostedZoneNameId("Z35SXDOTRQ7X7K"); + assertEquals("Z35SXDOTRQ7X7K", + service.describeLoadBalancers(REGION, List.of("stored-elb")).getFirst().getCanonicalHostedZoneNameId()); + + service.restorePersistedRuntime(); + + assertEquals(AwsRegionFacts.classicElbHostedZoneId(REGION).orElseThrow(), + service.describeLoadBalancers(REGION, List.of("stored-elb")).getFirst().getCanonicalHostedZoneNameId()); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2DefaultAttributesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2DefaultAttributesIntegrationTest.java index 30cda7d5b2..e6824b6dd8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2DefaultAttributesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2DefaultAttributesIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.elbv2; import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.not; @@ -64,6 +65,31 @@ class ElbV2DefaultAttributesIntegrationTest { .extract().path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn"); } + /** + * Network load balancers have their own hosted zone per region (the ELB table in the AWS + * General Reference, as the Terraform provider transcribes it); floci reported the ALB zone + * for every type before the per-region facts landed. + */ + @Test + void eachLoadBalancerTypeReportsItsOwnHostedZone() { + String albArn = createLoadBalancer("zone-alb", "application", "10.101."); + String nlbArn = createLoadBalancer("zone-nlb", "network", "10.102."); + + assertEquals("Z35SXDOTRQ7X7K", canonicalHostedZoneId(albArn)); + assertEquals("Z26RNL4JYFTOTI", canonicalHostedZoneId(nlbArn)); + } + + private String canonicalHostedZoneId(String arn) { + return given() + .formParam("Action", "DescribeLoadBalancers") + .formParam("Version", "2015-12-01") + .formParam("LoadBalancerArns.member.1", arn) + .header("Authorization", AUTH) + .when().post("/") + .then().statusCode(200) + .extract().path("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.CanonicalHostedZoneId"); + } + @Test void aFreshApplicationLoadBalancerReportsTheDocumentedDefaults() { String arn = createLoadBalancer("attrs-alb", "application", "10.95."); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java index 0f469fee28..f8b9897108 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elbv2; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; @@ -480,4 +481,30 @@ class ElbV2ServiceTest { return (AccountAwareStorageBackend) stores.computeIfAbsent(fileName, ignored -> AccountAwareStorageBackend.inMemory("000000000000")); } } + + /** + * A balancer stored before the zone was looked up per region and type carries the one fixed + * zone every balancer used to report; a restart gives it the zone of its region and type back. + */ + @Test + void aRestoredNetworkLoadBalancerTakesTheHostedZoneOfItsRegionAndType() { + SharedStorageFactory storageFactory = new SharedStorageFactory(); + ElbV2Service first = serviceWithStorage(storageFactory, mock(ElbV2DataPlane.class), + mock(ElbV2HealthChecker.class)); + first.createLoadBalancer(REGION, "stored-nlb", "internal", "network", "ipv4", + ALB_SUBNETS, List.of(), Map.of()); + first.describeLoadBalancers(REGION, null, List.of("stored-nlb"), null, null).getFirst() + .setCanonicalHostedZoneId("Z35SXDOTRQ7X7K"); + + ElbV2Service reloaded = serviceWithStorage(storageFactory, mock(ElbV2DataPlane.class), + mock(ElbV2HealthChecker.class)); + assertEquals("Z35SXDOTRQ7X7K", reloaded.describeLoadBalancers(REGION, null, List.of("stored-nlb"), null, null) + .getFirst().getCanonicalHostedZoneId()); + + reloaded.restorePersistedRuntime(); + + assertEquals(AwsRegionFacts.nlbHostedZoneId(REGION).orElseThrow(), + reloaded.describeLoadBalancers(REGION, null, List.of("stored-nlb"), null, null) + .getFirst().getCanonicalHostedZoneId()); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifierRecipientTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifierRecipientTest.java new file mode 100644 index 0000000000..8772a32d74 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifierRecipientTest.java @@ -0,0 +1,31 @@ +package io.github.hectorvent.floci.services.iam; + +import io.github.hectorvent.floci.services.iam.model.SAMLProvider; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * A bearer assertion names the console sign-on URL of the provider's partition. Where the CDK + * publishes none (ISO-E, ISO-F, EUSC) the commercial URL is kept, as before the per-partition + * table, instead of refusing every assertion. + */ +class SAMLAssertionVerifierRecipientTest { + + @ParameterizedTest + @CsvSource({ + "aws, https://signin.aws.amazon.com/saml", + "aws-cn, https://signin.amazonaws.cn/saml", + "aws-iso, https://signin.c2shome.ic.gov/saml", + "aws-iso-e, https://signin.aws.amazon.com/saml", + "aws-iso-f, https://signin.aws.amazon.com/saml", + "aws-eusc, https://signin.aws.amazon.com/saml" + }) + void theRecipientIsThePartitionsSignOnUrlOrTheCommercialOne(String partition, String expected) { + SAMLProvider provider = new SAMLProvider(); + provider.setArn("arn:" + partition + ":iam::000000000000:saml-provider/idp"); + + assertEquals(expected, SAMLAssertionVerifier.expectedRecipient(provider)); + } +} diff --git a/tools/aws/regen_region_facts.py b/tools/aws/regen_region_facts.py new file mode 100644 index 0000000000..8806aad52e --- /dev/null +++ b/tools/aws/regen_region_facts.py @@ -0,0 +1,339 @@ +#!/usr/bin/env python3 +"""Regenerate src/main/resources/aws/region-facts.json from the AWS CDK and Terraform sources. + +AWS publishes a handful of per-region and per-partition constants only as prose tables (the +General Reference), never as SDK data. The two open-source projects that transcribe them are the +sources here, read from the `local/aws` checkouts: + +- Route 53 hosted zone ids for Classic and Application load balancers (one map, byte-identical + in `terraform-provider-aws`), for Network load balancers (a different map), and for S3 static + website endpoints: `internal/service/elb/hosted_zone_id_data_source.go`, + `internal/service/elbv2/hosted_zone_id_data_source.go`, `internal/service/s3/hosted_zones.go`. + The CDK's `ROUTE_53_BUCKET_WEBSITE_ZONE_IDS` (`region-info/build-tools/fact-tables.ts`) adds the + ISO-F regions Terraform lacks; where the two disagree (af-south-1, eu-south-1) Terraform wins, + because it cites the S3 website-endpoint table and the CDK values are the regular S3 zones. +- The CloudFront distribution hosted zone per partition: + `internal/conns/awsclient.go` (`CloudFrontDistributionHostedZoneID`), cross-checked against the + CDK's `aws-route53-targets/lib/cloudfront-target.ts` mapping. +- The SAML sign-on URL per partition: the CDK's `PARTITION_SAML_SIGN_ON_URL`. +- VPC endpoint service-name prefixes: the CDK's `aws-ec2/lib/vpc-endpoint.ts` exception table + (China and the ISO/EUSC partitions reverse their DNS suffix, but only for the listed services) + plus the `.cn` suffix exceptions; the per-partition `.vpce` prefix from + `region-info/build-tools/generate-static-data.ts`. + +Nothing is hand-typed. When the checkouts are absent (CI), `--check` only verifies the vendored +file parses and keeps its shape; a regeneration needs `local/aws`. + +Run from anywhere in the repo: + python3 tools/aws/regen_region_facts.py # rewrite the vendored file in place + python3 tools/aws/regen_region_facts.py --check # exit 1 when the vendored file is stale +""" +from __future__ import annotations + +import argparse +import json +import re +import sys +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] +OUTPUT = REPO_ROOT / "src/main/resources/aws/region-facts.json" +PARTITIONS = REPO_ROOT / "src/main/resources/aws/partitions.json" +LOCAL_TERRAFORM = REPO_ROOT / "local/aws/terraform-provider-aws" +LOCAL_CDK = REPO_ROOT / "local/aws/aws-cdk" + +TF_ELB = "internal/service/elb/hosted_zone_id_data_source.go" +TF_ELBV2 = "internal/service/elbv2/hosted_zone_id_data_source.go" +TF_S3 = "internal/service/s3/hosted_zones.go" +TF_CLIENT = "internal/conns/awsclient.go" +CDK_FACT_TABLES = "packages/aws-cdk-lib/region-info/build-tools/fact-tables.ts" +CDK_CLOUDFRONT_TARGET = "packages/aws-cdk-lib/aws-route53-targets/lib/cloudfront-target.ts" +CDK_VPC_ENDPOINT = "packages/aws-cdk-lib/aws-ec2/lib/vpc-endpoint.ts" + +GO_ROW_RE = re.compile(r'^\s*endpoints\.([A-Za-z0-9]+)RegionID:\s*"([A-Z0-9]+)",') +TS_ROW_RE = re.compile(r"^\s*'([a-z0-9-]+)':\s*'([A-Z0-9]+)',") +TS_ENUM_RE = re.compile(r"^\s*(\w+)\s*=\s*'([a-z-]+)',") +TS_PARTITION_ROW_RE = re.compile(r"^\s*\[Partition\.(\w+)\]:\s*'([^']+)',") +GO_CLOUDFRONT_RE = re.compile(r'endpoints\.(\w+)PartitionID\s*\{\s*\n\s*return "([A-Z0-9]+)"', re.MULTILINE) +GO_CLOUDFRONT_DEFAULT_RE = re.compile(r'\}\s*\n\s*return "([A-Z0-9]+)" // See https://docs\.aws\.amazon\.com') +CDK_CLOUDFRONT_RE = re.compile(r"\['([a-z-]+)'\]:\s*\{\s*zoneId:\s*'([A-Z0-9]+)'") +VPCE_LIST_RE = re.compile(r"'([a-z0-9-]+)':\s*\[([^\]]*)\]", re.DOTALL) +VPCE_CASE_RE = re.compile(r"((?:\s*case '[a-z0-9-]+':\n)+)\s*return '([a-z0-9.-]+)';") +GO_PARTITION_IDS = {"AwsCn": "aws-cn", "AwsUsGov": "aws-us-gov", "AwsIso": "aws-iso", "AwsIsoB": "aws-iso-b", + "AwsIsoE": "aws-iso-e", "AwsIsoF": "aws-iso-f", "Aws": "aws"} + + +# --------------------------------------------------------------------------- # +# Parsers +# --------------------------------------------------------------------------- # +def region_id_from_go_constant(camel: str) -> str: + """`UsGovEast1` -> `us-gov-east-1`; `ApSoutheast7` -> `ap-southeast-7`; `IlCentral1` -> `il-central-1`.""" + parts = re.findall(r"[A-Z][a-z]+|\d+", camel) + return "-".join(part.lower() for part in parts) + + +def parse_go_map(text: str, name: str) -> dict[str, str]: + """The rows of `var = map[string]string{ ... }`, commented-out rows skipped.""" + start = text.index(f"var {name} = map[string]string{{") + end = text.index("\n}", start) + rows: dict[str, str] = {} + for line in text[start:end].splitlines(): + match = GO_ROW_RE.match(line) + if match: + rows[region_id_from_go_constant(match.group(1))] = match.group(2) + return rows + + +def parse_ts_table(text: str, name: str) -> dict[str, str]: + """The `'region': 'value',` rows of `export const ... = { ... };`.""" + start = text.index(f"export const {name}") + end = text.index("\n};", start) + rows: dict[str, str] = {} + for line in text[start:end].splitlines(): + match = TS_ROW_RE.match(line) + if match: + rows[match.group(1)] = match.group(2) + return rows + + +def parse_ts_partition_enum(text: str) -> dict[str, str]: + start = text.index("enum Partition {") + end = text.index("}", start) + return {m.group(1): m.group(2) for m in (TS_ENUM_RE.match(l) for l in text[start:end].splitlines()) if m} + + +def parse_ts_partition_table(text: str, name: str, enum: dict[str, str]) -> dict[str, str]: + start = text.index(f"export const {name}") + end = text.index("\n};", start) + rows: dict[str, str] = {} + for line in text[start:end].splitlines(): + match = TS_PARTITION_ROW_RE.match(line) + if match: + rows[enum[match.group(1)]] = match.group(2) + return rows + + +def parse_go_cloudfront_zones(text: str) -> dict[str, str]: + zones = {GO_PARTITION_IDS[m.group(1)]: m.group(2) for m in GO_CLOUDFRONT_RE.finditer(text)} + default = GO_CLOUDFRONT_DEFAULT_RE.search(text) + if default is None: + raise ValueError("awsclient.go: CloudFrontDistributionHostedZoneID default not found") + zones["aws"] = default.group(1) + return zones + + +def parse_cdk_cloudfront_zones(text: str) -> dict[str, str]: + return {m.group(1): m.group(2) for m in CDK_CLOUDFRONT_RE.finditer(text)} + + +def parse_vpc_endpoint_exceptions(text: str) -> tuple[dict[str, list[str]], dict[str, str], dict[str, list[str]]]: + """(prefix services per region, exception prefix per region, `.cn` suffix services per region).""" + prefix_start = text.index("private getDefaultEndpointPrefix(") + suffix_start = text.index("private getDefaultEndpointSuffix(") + prefix_body = text[prefix_start:suffix_start] + suffix_body = text[suffix_start:text.index("\n }", suffix_start)] + table_start = prefix_body.index("VPC_ENDPOINT_SERVICE_EXCEPTIONS") + table_end = prefix_body.index("};", table_start) + services = {region: sorted(s.strip().strip("'") for s in body.split(",") if s.strip()) + for region, body in VPCE_LIST_RE.findall(prefix_body[table_start:table_end])} + prefixes: dict[str, str] = {} + for cases, prefix in VPCE_CASE_RE.findall(prefix_body[table_end:]): + for region in re.findall(r"case '([a-z0-9-]+)':", cases): + prefixes[region] = prefix + suffix_table_start = suffix_body.index("VPC_ENDPOINT_SERVICE_EXCEPTIONS") + cn_suffix = {region: sorted(s.strip().strip("'") for s in body.split(",") if s.strip()) + for region, body in VPCE_LIST_RE.findall(suffix_body[suffix_table_start:])} + missing = set(services) - set(prefixes) + if missing: + raise ValueError(f"vpc-endpoint.ts: regions with exception services but no prefix: {sorted(missing)}") + return services, prefixes, cn_suffix + + +# --------------------------------------------------------------------------- # +# Generation +# --------------------------------------------------------------------------- # +def build(terraform: Path, cdk: Path, partitions_doc: dict) -> dict: + elb = parse_go_map((terraform / TF_ELB).read_text(encoding="utf-8"), "hostedZoneIDPerRegionMap") + elbv2_text = (terraform / TF_ELBV2).read_text(encoding="utf-8") + alb = parse_go_map(elbv2_text, "hostedZoneIDPerRegionALBMap") + nlb = parse_go_map(elbv2_text, "hostedZoneIDPerRegionNLBMap") + s3_tf = parse_go_map((terraform / TF_S3).read_text(encoding="utf-8"), "hostedZoneIDsMap") + fact_tables = (cdk / CDK_FACT_TABLES).read_text(encoding="utf-8") + s3_cdk = parse_ts_table(fact_tables, "ROUTE_53_BUCKET_WEBSITE_ZONE_IDS") + enum = parse_ts_partition_enum(fact_tables) + saml = parse_ts_partition_table(fact_tables, "PARTITION_SAML_SIGN_ON_URL", enum) + cloudfront_tf = parse_go_cloudfront_zones((terraform / TF_CLIENT).read_text(encoding="utf-8")) + cloudfront_cdk = parse_cdk_cloudfront_zones((cdk / CDK_CLOUDFRONT_TARGET).read_text(encoding="utf-8")) + if cloudfront_tf != cloudfront_cdk: + raise ValueError(f"CloudFront hosted zones disagree: terraform {cloudfront_tf} vs cdk {cloudfront_cdk}") + vpce_services, vpce_prefixes, vpce_cn_suffix = parse_vpc_endpoint_exceptions( + (cdk / CDK_VPC_ENDPOINT).read_text(encoding="utf-8")) + if elb != alb: + raise ValueError("the Classic and ALB hosted-zone maps differ; the generator assumed they are one table") + + partition_ids = [p["id"] for p in partitions_doc["partitions"]] + region_partition = {r["id"]: p for p in partitions_doc["partitions"] for r in p["regions"]} + dns_suffix = {p["id"]: p["dnsSuffix"] for p in partitions_doc["partitions"]} + + s3_website = dict(s3_cdk) + s3_website.update(s3_tf) # Terraform cites the website-endpoint table; it wins on conflicts + region_ids = sorted(set(alb) | set(nlb) | set(s3_website) | set(vpce_services)) + regions = {} + for region in region_ids: + if region not in region_partition: + raise ValueError(f"{region}: named by a source table but not a published region in partitions.json") + entry = {} + if region in alb: + entry["albHostedZoneId"] = alb[region] + entry["classicElbHostedZoneId"] = elb[region] + if region in nlb: + entry["nlbHostedZoneId"] = nlb[region] + if region in s3_website: + entry["s3WebsiteHostedZoneId"] = s3_website[region] + if region in vpce_services: + entry["vpcEndpointExceptionPrefix"] = vpce_prefixes[region] + entry["vpcEndpointPrefixServices"] = vpce_services[region] + if region in vpce_cn_suffix: + entry["vpcEndpointCnSuffixServices"] = vpce_cn_suffix[region] + regions[region] = entry + + partitions = {} + for partition_id in partition_ids: + entry = {"vpcEndpointServiceNamePrefix": ".".join(reversed(dns_suffix[partition_id].split("."))) + ".vpce"} + if partition_id in cloudfront_tf: + entry["cloudfrontHostedZoneId"] = cloudfront_tf[partition_id] + if partition_id in saml: + entry["samlSignOnUrl"] = saml[partition_id] + partitions[partition_id] = entry + + return { + "_source": { + "generator": "tools/aws/regen_region_facts.py", + "terraform": f"terraform-provider-aws {TF_ELB}, {TF_ELBV2}, {TF_S3}, {TF_CLIENT}", + "cdk": f"aws-cdk {CDK_FACT_TABLES}, {CDK_CLOUDFRONT_TARGET}, {CDK_VPC_ENDPOINT}", + "s3WebsiteConflicts": "terraform wins over the CDK where they disagree (it cites the S3 website-endpoint table)", + }, + "partitions": partitions, + "regions": regions, + } + + +def render(document: dict) -> str: + return json.dumps(document, indent=2, ensure_ascii=False) + "\n" + + +def strip_source(text: str) -> str: + if not text.strip(): + return "" + document = json.loads(text) + document.pop("_source", None) + return json.dumps(document, sort_keys=True) + + +HOSTED_ZONE_ID_RE = re.compile(r"Z[0-9A-Z]{9,31}") +SAML_URL_RE = re.compile(r"https://[a-z0-9.-]+/saml") +VPCE_PREFIX_RE = re.compile(r"[a-z0-9-]+(\.[a-z0-9-]+)*\.vpce") +REVERSED_DNS_RE = re.compile(r"[a-z0-9-]+(\.[a-z0-9-]+)+") + + +def self_check(path: Path, partitions_path: Path = PARTITIONS) -> list[str]: + """Problems with the vendored file that need no sources: every partition partitions.json + publishes has an entry, its VPC endpoint prefix is exactly the reversed DNS suffix, every region + is a published one, and every other value has the shape of what it claims to be. It cannot tell + a stale value from a current one; only a regeneration against local/aws can.""" + problems: list[str] = [] + if not path.exists(): + return [f"{path} is missing"] + try: + document = json.loads(path.read_text(encoding="utf-8")) + except json.JSONDecodeError as e: + return [f"{path}: not valid JSON ({e})"] + for key in ("partitions", "regions"): + if not isinstance(document.get(key), dict) or not document[key]: + problems.append(f"{path}: '{key}' is missing or empty") + if problems: + return problems + for key in ("partitions", "regions"): + for name, entry in document[key].items(): + if not isinstance(entry, dict): + problems.append(f"{path}: {key}.{name} is not an object: {entry!r}") + if problems: + return problems + + partitions_doc = json.loads(partitions_path.read_text(encoding="utf-8"))["partitions"] + for partition in partitions_doc: + entry = document["partitions"].get(partition["id"]) + if entry is None: + problems.append(f"{path}: partition {partition['id']} has no entry") + continue + expected = ".".join(reversed(partition["dnsSuffix"].split("."))) + ".vpce" + if entry.get("vpcEndpointServiceNamePrefix") != expected: + problems.append(f"{path}: {partition['id']}.vpcEndpointServiceNamePrefix should be {expected!r}") + known = {region["id"] for partition in partitions_doc for region in partition["regions"]} + for region in document["regions"]: + if region not in known: + problems.append(f"{path}: region {region} is not published in partitions.json") + + for partition, entry in document["partitions"].items(): + for key, value in entry.items(): + pattern = {"cloudfrontHostedZoneId": HOSTED_ZONE_ID_RE, "samlSignOnUrl": SAML_URL_RE, + "vpcEndpointServiceNamePrefix": VPCE_PREFIX_RE}.get(key) + if pattern is None: + problems.append(f"{path}: {partition}.{key} is not a known field") + elif not isinstance(value, str) or not pattern.fullmatch(value): + problems.append(f"{path}: {partition}.{key} has an unexpected shape: {value!r}") + for region, entry in document["regions"].items(): + for key, value in entry.items(): + if key.endswith("HostedZoneId"): + if not isinstance(value, str) or not HOSTED_ZONE_ID_RE.fullmatch(value): + problems.append(f"{path}: {region}.{key} is not a hosted zone id: {value!r}") + elif key == "vpcEndpointExceptionPrefix": + if not isinstance(value, str) or not REVERSED_DNS_RE.fullmatch(value): + problems.append(f"{path}: {region}.{key} is not a reversed DNS prefix: {value!r}") + elif key in ("vpcEndpointPrefixServices", "vpcEndpointCnSuffixServices"): + if not isinstance(value, list) or not all(isinstance(v, str) and v for v in value): + problems.append(f"{path}: {region}.{key} is not a list of service names") + else: + problems.append(f"{path}: {region}.{key} is not a known field") + return problems + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--check", action="store_true", help="exit 1 when the vendored file differs from a fresh generation") + parser.add_argument("--terraform", type=Path, default=LOCAL_TERRAFORM, help="terraform-provider-aws checkout") + parser.add_argument("--cdk", type=Path, default=LOCAL_CDK, help="aws-cdk checkout") + parser.add_argument("--partitions", type=Path, default=PARTITIONS, help=argparse.SUPPRESS) + parser.add_argument("--output", type=Path, default=OUTPUT, help=argparse.SUPPRESS) + args = parser.parse_args(argv) + rel = args.output.relative_to(REPO_ROOT) if args.output.is_relative_to(REPO_ROOT) else args.output + + sources_present = (args.terraform / TF_ELB).exists() and (args.cdk / CDK_FACT_TABLES).exists() + if not sources_present: + if args.check: + problems = self_check(args.output, args.partitions) + for problem in problems: + print(f"error: {problem}") + if problems: + return 1 + print(f"{rel}: sources not checked out, shape verified only") + return 0 + raise SystemExit("error: regenerating needs the terraform-provider-aws and aws-cdk checkouts under local/aws") + + document = build(args.terraform, args.cdk, json.loads(args.partitions.read_text(encoding="utf-8"))) + rendered = render(document) + if args.check: + current = args.output.read_text(encoding="utf-8") if args.output.exists() else "" + if strip_source(current) != strip_source(rendered): + print(f"error: {rel} differs from a fresh generation. Run 'make aws-data-sync' and commit the result.") + return 1 + print(f"{rel} is up to date ({len(document['regions'])} regions, {len(document['partitions'])} partitions)") + return 0 + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(rendered, encoding="utf-8") + print(f"wrote {rel}: {len(document['regions'])} regions, {len(document['partitions'])} partitions") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/aws/test_regen_region_facts.py b/tools/aws/test_regen_region_facts.py new file mode 100644 index 0000000000..bdfeea1cc7 --- /dev/null +++ b/tools/aws/test_regen_region_facts.py @@ -0,0 +1,250 @@ +"""Tests for regen_region_facts. + +Run with: pytest tools/aws -q (or: make aws-data-test) +""" +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +import regen_region_facts as r + + +GO_ELB = ''' +// See https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region. +var hostedZoneIDPerRegionMap = map[string]string{ + endpoints.UsEast1RegionID: "Z35SXDOTRQ7X7K", + endpoints.UsGovEast1RegionID: "Z166TLBEWOO7G0", + endpoints.CnNorthwest1RegionID: "ZM7IZAIOVVDZF", + // endpoints.MxCentral1RegionID: "", +} +''' +GO_ELBV2 = GO_ELB.replace("hostedZoneIDPerRegionMap", "hostedZoneIDPerRegionALBMap") + ''' +var hostedZoneIDPerRegionNLBMap = map[string]string{ + endpoints.UsEast1RegionID: "Z26RNL4JYFTOTI", + endpoints.UsGovEast1RegionID: "Z1ZSMQQ6Q24QQ8", + endpoints.CnNorthwest1RegionID: "ZQEIKTCZ8352D", +} +''' +GO_S3 = ''' +var hostedZoneIDsMap = map[string]string{ + endpoints.UsEast1RegionID: "Z3AQBSTGFYJSTF", + endpoints.AfSouth1RegionID: "Z83WF9RJE8B12", +} +''' +GO_CLIENT = ''' +func (c *AWSClient) CloudFrontDistributionHostedZoneID(ctx context.Context) string { + if c.Partition(ctx) == endpoints.AwsCnPartitionID { + return "Z3RFFRIM2A3IF5" // See https://docs.amazonaws.cn/en_us/aws/latest/userguide/route53.html + } + return "Z2FDTNDATAQYW2" // See https://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html +} +''' +TS_FACTS = ''' +export const ROUTE_53_BUCKET_WEBSITE_ZONE_IDS: { [region: string]: string } = { + 'af-south-1': 'Z11KHD8FBVPUYU', + 'us-isof-south-1': 'Z03376072I8GXC2DXUFXI', +}; + +enum Partition { + Default = 'aws', + Cn = 'aws-cn', + UsGov = 'aws-us-gov', + UsIso = 'aws-iso', + UsIsoB = 'aws-iso-b', + UsIsoF = 'aws-iso-f', + EuIsoE = 'aws-iso-e', + Eusc = 'aws-eusc', +} + +export const PARTITION_SAML_SIGN_ON_URL: Partial> = { + [Partition.Default]: 'https://signin.aws.amazon.com/saml', + [Partition.Cn]: 'https://signin.amazonaws.cn/saml', +}; +''' +TS_CLOUDFRONT = ''' + mapping: { + ['aws']: { + zoneId: 'Z2FDTNDATAQYW2', // docs + }, + ['aws-cn']: { + zoneId: 'Z3RFFRIM2A3IF5', // docs + }, + }, +''' +TS_VPCE = ''' + private getDefaultEndpointPrefix(name: string, region: string) { + const VPC_ENDPOINT_SERVICE_EXCEPTIONS: { [region: string]: string[] } = { + 'cn-northwest-1': ['s3', 'ecr.api', + 'sts'], + 'us-isof-south-1': ['ebs', 'ecr.api'], + }; + if (VPC_ENDPOINT_SERVICE_EXCEPTIONS[region]?.includes(name)) { + switch (region) { + case 'us-isof-south-1': + case 'us-isof-east-1': + return 'gov.ic.hci.csp'; + case 'cn-north-1': + case 'cn-northwest-1': + return 'cn.com.amazonaws'; + } + } + return 'com.amazonaws'; + } + + private getDefaultEndpointSuffix(name: string, region: string) { + const VPC_ENDPOINT_SERVICE_EXCEPTIONS: { [region: string]: string[] } = { + 'cn-north-1': ['transcribe'], + 'cn-northwest-1': ['transcribe'], + }; + return VPC_ENDPOINT_SERVICE_EXCEPTIONS[region]?.includes(name) ? '.cn' : ''; + } +''' +PARTITIONS = {"partitions": [ + {"id": "aws", "dnsSuffix": "amazonaws.com", "regions": [{"id": "us-east-1"}, {"id": "af-south-1"}]}, + {"id": "aws-cn", "dnsSuffix": "amazonaws.com.cn", "regions": [{"id": "cn-north-1"}, {"id": "cn-northwest-1"}]}, + {"id": "aws-us-gov", "dnsSuffix": "amazonaws.com", "regions": [{"id": "us-gov-east-1"}]}, + {"id": "aws-iso-f", "dnsSuffix": "csp.hci.ic.gov", "regions": [{"id": "us-isof-south-1"}]}, +]} + + +def write_sources(tmp_path: Path) -> tuple[Path, Path]: + tf = tmp_path / "terraform" + cdk = tmp_path / "cdk" + for root, rel, body in [ + (tf, r.TF_ELB, GO_ELB), (tf, r.TF_ELBV2, GO_ELBV2), (tf, r.TF_S3, GO_S3), (tf, r.TF_CLIENT, GO_CLIENT), + (cdk, r.CDK_FACT_TABLES, TS_FACTS), (cdk, r.CDK_CLOUDFRONT_TARGET, TS_CLOUDFRONT), (cdk, r.CDK_VPC_ENDPOINT, TS_VPCE), + ]: + path = root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(body) + return tf, cdk + + +@pytest.mark.parametrize("camel,region", [ + ("UsEast1", "us-east-1"), ("UsGovEast1", "us-gov-east-1"), ("CnNorthwest1", "cn-northwest-1"), + ("IlCentral1", "il-central-1"), ("MxCentral1", "mx-central-1"), ("ApSoutheast7", "ap-southeast-7"), + ("CaWest1", "ca-west-1"), ("EuIsoeWest1", "eu-isoe-west-1"), +]) +def test_go_constants_become_region_ids(camel, region): + assert r.region_id_from_go_constant(camel) == region + + +def test_parse_go_map_skips_commented_rows(): + assert r.parse_go_map(GO_ELB, "hostedZoneIDPerRegionMap") == { + "us-east-1": "Z35SXDOTRQ7X7K", "us-gov-east-1": "Z166TLBEWOO7G0", "cn-northwest-1": "ZM7IZAIOVVDZF"} + + +def test_parse_ts_tables_and_partition_enum(): + assert r.parse_ts_table(TS_FACTS, "ROUTE_53_BUCKET_WEBSITE_ZONE_IDS") == { + "af-south-1": "Z11KHD8FBVPUYU", "us-isof-south-1": "Z03376072I8GXC2DXUFXI"} + enum = r.parse_ts_partition_enum(TS_FACTS) + assert enum["UsIsoB"] == "aws-iso-b" and enum["Eusc"] == "aws-eusc" + assert r.parse_ts_partition_table(TS_FACTS, "PARTITION_SAML_SIGN_ON_URL", enum) == { + "aws": "https://signin.aws.amazon.com/saml", "aws-cn": "https://signin.amazonaws.cn/saml"} + + +def test_cloudfront_zones_from_both_sources_agree(): + assert r.parse_go_cloudfront_zones(GO_CLIENT) == {"aws": "Z2FDTNDATAQYW2", "aws-cn": "Z3RFFRIM2A3IF5"} + assert r.parse_cdk_cloudfront_zones(TS_CLOUDFRONT) == r.parse_go_cloudfront_zones(GO_CLIENT) + + +def test_vpc_endpoint_exceptions_are_per_region_and_service(): + services, prefixes, cn_suffix = r.parse_vpc_endpoint_exceptions(TS_VPCE) + assert services == {"cn-northwest-1": ["ecr.api", "s3", "sts"], "us-isof-south-1": ["ebs", "ecr.api"]} + assert prefixes["cn-northwest-1"] == "cn.com.amazonaws" + assert prefixes["us-isof-east-1"] == "gov.ic.hci.csp" + assert cn_suffix == {"cn-north-1": ["transcribe"], "cn-northwest-1": ["transcribe"]} + + +def test_build_merges_sources_with_terraform_winning_the_s3_conflicts(tmp_path): + tf, cdk = write_sources(tmp_path) + document = r.build(tf, cdk, PARTITIONS) + regions = document["regions"] + assert regions["us-east-1"] == {"albHostedZoneId": "Z35SXDOTRQ7X7K", "classicElbHostedZoneId": "Z35SXDOTRQ7X7K", + "nlbHostedZoneId": "Z26RNL4JYFTOTI", "s3WebsiteHostedZoneId": "Z3AQBSTGFYJSTF"} + assert regions["af-south-1"]["s3WebsiteHostedZoneId"] == "Z83WF9RJE8B12" + assert regions["us-isof-south-1"] == {"s3WebsiteHostedZoneId": "Z03376072I8GXC2DXUFXI", + "vpcEndpointExceptionPrefix": "gov.ic.hci.csp", + "vpcEndpointPrefixServices": ["ebs", "ecr.api"]} + assert regions["cn-northwest-1"]["vpcEndpointCnSuffixServices"] == ["transcribe"] + assert "cn-north-1" not in regions or "albHostedZoneId" not in regions["cn-north-1"] + partitions = document["partitions"] + assert partitions["aws-cn"] == {"vpcEndpointServiceNamePrefix": "cn.com.amazonaws.vpce", + "cloudfrontHostedZoneId": "Z3RFFRIM2A3IF5", + "samlSignOnUrl": "https://signin.amazonaws.cn/saml"} + assert partitions["aws-iso-f"] == {"vpcEndpointServiceNamePrefix": "gov.ic.hci.csp.vpce"} + + +def test_build_refuses_a_region_the_partition_data_does_not_publish(tmp_path): + tf, cdk = write_sources(tmp_path) + with pytest.raises(ValueError, match="af-south-1"): + r.build(tf, cdk, {"partitions": [p for p in PARTITIONS["partitions"] if p["id"] != "aws"] + + [{"id": "aws", "dnsSuffix": "amazonaws.com", "regions": [{"id": "us-east-1"}]}]}) + + +def test_cli_write_check_and_self_check(tmp_path, capsys): + tf, cdk = write_sources(tmp_path) + partitions = tmp_path / "partitions.json" + partitions.write_text(json.dumps(PARTITIONS)) + output = tmp_path / "out" / "region-facts.json" + common = ["--terraform", str(tf), "--cdk", str(cdk), "--partitions", str(partitions), "--output", str(output)] + assert r.main(["--check"] + common) == 1 + assert r.main(common) == 0 + assert r.main(["--check"] + common) == 0 + assert "is up to date" in capsys.readouterr().out + # without the checkouts, --check verifies the shape only + assert r.main(["--check", "--terraform", str(tmp_path / "nowhere"), "--cdk", str(tmp_path / "nowhere"), + "--partitions", str(partitions), "--output", str(output)]) == 0 + assert "shape verified only" in capsys.readouterr().out + output.write_text('{"partitions": {}, "regions": {"us-east-1": {"albHostedZoneId": "nope"}}}') + assert r.main(["--check", "--terraform", str(tmp_path / "nowhere"), "--cdk", str(tmp_path / "nowhere"), + "--partitions", str(partitions), "--output", str(output)]) == 1 + + +def test_repo_file_matches_the_local_checkouts_when_present(): + if not (r.LOCAL_TERRAFORM / r.TF_ELB).exists() or not (r.LOCAL_CDK / r.CDK_FACT_TABLES).exists(): + pytest.skip("local/aws checkouts not present") + document = r.build(r.LOCAL_TERRAFORM, r.LOCAL_CDK, json.loads(r.PARTITIONS.read_text(encoding="utf-8"))) + assert r.strip_source(r.render(document)) == r.strip_source(r.OUTPUT.read_text(encoding="utf-8")) + + +def test_self_check_holds_the_values_to_what_partitions_json_publishes(tmp_path): + partitions = tmp_path / "partitions.json" + partitions.write_text(json.dumps(PARTITIONS)) + tf, cdk = write_sources(tmp_path) + good = r.build(tf, cdk, PARTITIONS) + output = tmp_path / "region-facts.json" + + def problems(mutate): + document = json.loads(json.dumps(good)) + mutate(document) + output.write_text(json.dumps(document)) + return r.self_check(output, partitions) + + region = next(iter(good["regions"])) + partition = next(iter(good["partitions"])) + accepted = { + "unchanged": lambda d: None, + # ap-southeast-1's NLB zone is twelve characters; real zone ids run from twelve to twenty-one + "twelve-character zone": lambda d: d["regions"][region].update(nlbHostedZoneId="ZKVM4W9LS7TM"), + } + rejected = { + "not a zone id": lambda d: d["regions"][region].update(nlbHostedZoneId="nope"), + "unpublished region": lambda d: d["regions"].update({"xx-nowhere-1": {}}), + "wrong endpoint prefix": lambda d: d["partitions"][partition].update( + vpcEndpointServiceNamePrefix="com.example.vpce"), + "plain-http SAML URL": lambda d: d["partitions"][partition].update( + samlSignOnUrl="http://signin.example/saml"), + "missing partition": lambda d: d["partitions"].pop(partition), + "partition entry not an object": lambda d: d["partitions"].update({partition: "invalid"}), + "region entry not an object": lambda d: d["regions"].update({region: ["invalid"]}), + } + for label, mutate in accepted.items(): + found = problems(mutate) + assert found == [], label + for label, mutate in rejected.items(): + found = problems(mutate) + assert found, label diff --git a/tools/partition/allowlist.yaml b/tools/partition/allowlist.yaml index 61de42c1ff..78005aa05e 100644 --- a/tools/partition/allowlist.yaml +++ b/tools/partition/allowlist.yaml @@ -7,25 +7,6 @@ files: - path: src/main/java/io/github/hectorvent/floci/services/route53/Route53VpcRegions.java reason: the VPCRegion enum transcribed verbatim from the Route 53 model. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java - reason: AWS Marketplace exists only in the commercial partition and AWS pins its APIs to - us-east-1 with the validation messages this service reproduces. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceCatalogController.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentController.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDeploymentService.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryController.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceDiscoveryService.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceEntitlementService.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceReportingService.java - reason: see MarketplaceAgreementService. - - path: src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceService.java - reason: see MarketplaceAgreementService. - path: src/main/java/io/github/hectorvent/floci/core/common/AwsNamespaces.java reason: XML namespace URIs (http://s3.amazonaws.com/doc/2006-03-01/ and the Query namespaces) are identifiers, not hosts, and are byte-identical in every partition; diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 3afc379157..8533184880 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -16,14 +16,9 @@ dns-suffix src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityC dns-suffix src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityWebhook.java 2 dns-suffix src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyService.java 1 dns-suffix src/main/java/io/github/hectorvent/floci/services/iam/IamService.java 6 -dns-suffix src/main/java/io/github/hectorvent/floci/services/iam/SAMLAssertionVerifier.java 1 dns-suffix src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java 2 dns-suffix src/main/java/io/github/hectorvent/floci/services/macie2/MacieController.java 1 dns-suffix src/main/java/io/github/hectorvent/floci/services/ram/RamController.java 2 dns-suffix src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftRoleAccess.java 2 dns-suffix src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java 1 -dns-suffix src/main/java/io/github/hectorvent/floci/services/signin/SigninService.java 1 dns-suffix src/main/java/io/github/hectorvent/floci/services/ssoportal/SsoPortalService.java 1 -hosted-zone-id src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java 2 -hosted-zone-id src/main/java/io/github/hectorvent/floci/services/elb/ElbClassicService.java 1 -hosted-zone-id src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java 1