From 8bd88bfb402d7688a31e8819624f50c1c74256d2 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Tue, 13 Oct 2026 22:02:27 -0500 Subject: [PATCH] fix(codepipeline): link approval notifications to the partition's console host (#5595) * fix(codepipeline): link approval notifications to the partition's console host The approval-needed notification built consoleLink and approvalReviewLink on the commercial console host for every pipeline, so a pipeline in China or GovCloud sent its approver to a console where the pipeline does not exist. The console host becomes a vendored per-partition fact, consoleHost in aws/region-facts.json, read by the generator from the user-guide snapshots under local/aws/aws-docs/partition-guides: cn/console.md for aws-cn (console.amazonaws.cn), gov/configure-account.md for aws-us-gov (console.amazonaws-us-gov.com) and gov/govcloud-organizations.md for the commercial host. The China guide also spells the host console.www.amazonaws.cn once, in cn/managing-aws.md; the console page's own statement is the one used. A guide page that lost its sentence, or states two hosts with it, fails the generation instead of leaving a stale value. CodePipelineEventPublisher resolves the host from the execution's region, since it runs on a worker thread without a request context. No source states a console host for the ISO or EUSC partitions, which keep the commercial one and are listed as an open question. The path after the host stays the commercial console's: no source gives the per-service path per partition. Signed-off-by: Hector Ventura * fix(codepipeline): parse the China console host from the Markdown guide page, require sourced hosts AWS serves each user-guide page as Markdown at the same URL with .md in place of .html, and that rendering writes the China console host as a link, "[https://console.amazonaws.cn](https://console.amazonaws.cn).". The aws-cn pattern only took the page-text form, so a snapshot saved from the Markdown page failed the generator, and with it the rest of "make aws-data-sync". The pattern now takes both forms. The docstring names the three guide page URLs and the path each is saved under. self_check now requires consoleHost for the three partitions that have a source (aws, aws-cn, aws-us-gov). The shape-only check used when the sources are not checked out accepted a file without them, which would link approval notifications to the commercial console. Signed-off-by: Hector Ventura --------- Signed-off-by: Hector Ventura --- docs/configuration/partitions.md | 7 +- .../floci/core/common/AwsRegionFacts.java | 14 ++- .../CodePipelineEventPublisher.java | 20 ++++- src/main/resources/aws/region-facts.json | 10 ++- .../floci/core/common/AwsRegionFactsTest.java | 11 +++ .../CodePipelineEventPublisherTest.java | 43 ++++++++- tools/aws/regen_region_facts.py | 72 +++++++++++++-- tools/aws/test_regen_region_facts.py | 88 ++++++++++++++++--- 8 files changed, 233 insertions(+), 32 deletions(-) diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.md index 6cb77cd1c2..a050ce5f7f 100644 --- a/docs/configuration/partitions.md +++ b/docs/configuration/partitions.md @@ -116,7 +116,10 @@ back to `aws` for a label no partition publishes. 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 + and `aws-cn`; the SAML sign-on URL for five partitions. The console link in a CodePipeline + approval notification uses the console host of the pipeline's partition (`console.amazonaws.cn` + in China, `console.amazonaws-us-gov.com` in GovCloud), as the China and GovCloud user guides + state it; the path after the host is the commercial console's. 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 REST API or custom domain is refused outside the commercial partition (`BadRequestException`): GovCloud, EUSC and ISO have no @@ -241,6 +244,8 @@ the commercial value or Floci's own base host until sourced: (`us-iso-east-1-az1`), where the other partitions get AWS's short prefix (`use1-az1`, `usgw1-az1`, `cnn1-az1`); - S3 `LocationConstraint` enum values and Route 53 hosted zones for the ISO and EUSC regions; +- the management console host in the ISO and EUSC partitions, where a CodePipeline approval + notification keeps linking to the commercial `console.aws.amazon.com`; - 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`; - the exact error AWS WAF returns for the `CLOUDFRONT` scope in a partition without CloudFront 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 index 389f4665c4..5cde2fd447 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegionFacts.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegionFacts.java @@ -13,9 +13,9 @@ 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 + * provider, the CDK and the China and GovCloud user guides: load balancer and S3 website hosted + * zone ids, the CloudFront hosted zone per partition, the SAML sign-on URL and the management + * console host 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 { @@ -58,6 +58,14 @@ public final class AwsRegionFacts { return partitionText(partition, "samlSignOnUrl"); } + /** + * The management console host, without a scheme. Stated by the user guides for {@code aws}, + * {@code aws-cn} and {@code aws-us-gov}; absent for the ISO partitions and {@code aws-eusc}. + */ + public static Optional consoleHost(String partition) { + return partitionText(partition, "consoleHost"); + } + /** {@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"); diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisher.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisher.java index 07151ff063..19767d4280 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisher.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisher.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsRegionFacts; +import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.services.codepipeline.model.CodePipelineExecution; import io.github.hectorvent.floci.services.codepipeline.model.CodePipelineExecution.ActionExecution; @@ -31,7 +33,7 @@ public class CodePipelineEventPublisher { private static final Logger LOG = Logger.getLogger(CodePipelineEventPublisher.class); private static final int MAX_SNS_SUBJECT_LENGTH = 100; - private static final String CONSOLE_URL = "https://console.aws.amazon.com/codepipeline/home"; + private static final String CONSOLE_PATH = "/codepipeline/home"; /** The approval message's {@code expires} is to the minute, e.g. 2016-07-07T20:22Z. */ private static final DateTimeFormatter MINUTE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'").withZone(ZoneOffset.UTC); @@ -107,7 +109,7 @@ public class CodePipelineEventPublisher { String externalEntityLink, double expires) { try { ObjectNode message = mapper.createObjectNode(); - String consoleLink = CONSOLE_URL + "?region=" + execution.getRegion() + String consoleLink = consoleUrl(execution.getRegion()) + "?region=" + execution.getRegion() + "#/view/" + execution.getPipelineName(); message.put("region", execution.getRegion()); message.put("consoleLink", consoleLink); @@ -138,6 +140,20 @@ public class CodePipelineEventPublisher { } } + /** + * The CodePipeline console page on the console host of the pipeline's partition, derived + * from the execution's region because this runs with no request context. The user guides + * state the host for the commercial, China and GovCloud partitions; the ISO and EUSC + * partitions have no published one and keep the commercial host. The path is the commercial + * console's in every partition: no source gives the per-service path per partition. + */ + private static String consoleUrl(String region) { + String host = AwsRegionFacts.consoleHost(AwsRegions.partitionFor(region)) + .or(() -> AwsRegionFacts.consoleHost(AwsRegions.DEFAULT_PARTITION)) + .orElseThrow(); + return "https://" + host + CONSOLE_PATH; + } + /** Epoch seconds (with fraction) as the ISO-8601 UTC timestamp CodePipeline emits, e.g. 2023-10-26T13:31:39.604Z. */ private static String isoTimestamp(double epochSeconds) { return Instant.ofEpochMilli(Math.round(epochSeconds * 1000)).toString(); diff --git a/src/main/resources/aws/region-facts.json b/src/main/resources/aws/region-facts.json index 3f2fb155ed..7903b1a653 100644 --- a/src/main/resources/aws/region-facts.json +++ b/src/main/resources/aws/region-facts.json @@ -3,18 +3,21 @@ "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", + "docs": "aws-docs/partition-guides (snapshots of the China and GovCloud user guides) cn/console.md, gov/configure-account.md, gov/govcloud-organizations.md", "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" + "samlSignOnUrl": "https://signin.aws.amazon.com/saml", + "consoleHost": "console.aws.amazon.com" }, "aws-cn": { "vpcEndpointServiceNamePrefix": "cn.com.amazonaws.vpce", "cloudfrontHostedZoneId": "Z3RFFRIM2A3IF5", - "samlSignOnUrl": "https://signin.amazonaws.cn/saml" + "samlSignOnUrl": "https://signin.amazonaws.cn/saml", + "consoleHost": "console.amazonaws.cn" }, "aws-eusc": { "vpcEndpointServiceNamePrefix": "eu.amazonaws.vpce" @@ -35,7 +38,8 @@ }, "aws-us-gov": { "vpcEndpointServiceNamePrefix": "com.amazonaws.vpce", - "samlSignOnUrl": "https://signin.amazonaws-us-gov.com/saml" + "samlSignOnUrl": "https://signin.amazonaws-us-gov.com/saml", + "consoleHost": "console.amazonaws-us-gov.com" } }, "regions": { 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 index bb340c2562..068e3eca12 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionFactsTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionFactsTest.java @@ -50,6 +50,17 @@ class AwsRegionFactsTest { assertTrue(AwsRegionFacts.samlSignOnUrl(null).isEmpty()); } + /** The hosts the China and GovCloud user guides state; no guide covers the ISO or EUSC partitions. */ + @Test + void consoleHostIsPerPartitionWhereAGuideStatesIt() { + assertEquals(Optional.of("console.aws.amazon.com"), AwsRegionFacts.consoleHost("aws")); + assertEquals(Optional.of("console.amazonaws.cn"), AwsRegionFacts.consoleHost("aws-cn")); + assertEquals(Optional.of("console.amazonaws-us-gov.com"), AwsRegionFacts.consoleHost("aws-us-gov")); + assertTrue(AwsRegionFacts.consoleHost("aws-iso").isEmpty()); + assertTrue(AwsRegionFacts.consoleHost("aws-eusc").isEmpty()); + assertTrue(AwsRegionFacts.consoleHost(null).isEmpty()); + } + @ParameterizedTest @CsvSource({ "us-east-1, s3, com.amazonaws.us-east-1.s3", diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisherTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisherTest.java index d28311dc51..092fa30d1f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineEventPublisherTest.java @@ -8,6 +8,8 @@ import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; import io.github.hectorvent.floci.services.sns.SnsService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -18,12 +20,16 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.verify; /** * Unit tests for {@link CodePipelineEventPublisher#actionStateChange}'s execution-result * shape, pinned at the entry handed to {@code eventBridgeService.putEvents} so a regression - * in the optional-field guards is caught even though the event delivery itself is best-effort. + * in the optional-field guards is caught even though the event delivery itself is best-effort, + * and for the approval notification's console links, pinned at the message handed to + * {@code snsService.publish}. */ @ExtendWith(MockitoExtension.class) class CodePipelineEventPublisherTest { @@ -83,6 +89,41 @@ class CodePipelineEventPublisherTest { assertFalse(detail.path("execution-result").has("error-code")); } + /** + * The console host follows the pipeline's partition where a user guide states one; EUSC and + * ISO have no published host and keep the commercial one. + */ + @ParameterizedTest + @CsvSource({ + "us-east-1, console.aws.amazon.com", + "cn-north-1, console.amazonaws.cn", + "us-gov-west-1, console.amazonaws-us-gov.com", + "eusc-de-east-1, console.aws.amazon.com", + "us-iso-east-1, console.aws.amazon.com"}) + void approvalLinksUseTheConsoleHostOfThePipelinesPartition(String region, String host) throws Exception { + CodePipelineEventPublisher publisher = + new CodePipelineEventPublisher(eventBridgeService, snsService, MAPPER); + + CodePipelineExecution execution = new CodePipelineExecution(); + execution.setRegion(region); + execution.setAccountId("000000000000"); + execution.setPipelineName("release"); + ActionExecution action = new ActionExecution(); + action.setStageName("Gate"); + action.setActionName("Approve"); + action.setToken("token-1"); + + publisher.approvalNeeded(execution, action, "topic-arn", null, null, 1700000000); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(snsService).publish(eq("topic-arn"), isNull(), captor.capture(), any(), eq(region)); + JsonNode message = MAPPER.readTree(captor.getValue()); + String consoleLink = "https://" + host + "/codepipeline/home?region=" + region + "#/view/release"; + assertEquals(consoleLink, message.path("consoleLink").asText()); + assertEquals(consoleLink + "/Gate/Approve/approve/token-1", + message.path("approval").path("approvalReviewLink").asText()); + } + @Test @SuppressWarnings("unchecked") void startTimeKeepsTheMillisecondsOfTheStoredStartTime() throws Exception { diff --git a/tools/aws/regen_region_facts.py b/tools/aws/regen_region_facts.py index 8806aad52e..5381dd5d69 100644 --- a/tools/aws/regen_region_facts.py +++ b/tools/aws/regen_region_facts.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 -"""Regenerate src/main/resources/aws/region-facts.json from the AWS CDK and Terraform sources. +"""Regenerate src/main/resources/aws/region-facts.json from the AWS CDK, Terraform and user-guide 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: +General Reference), never as SDK data. The two open-source projects that transcribe them, and the +China and GovCloud user guides for what neither transcribes, 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 @@ -20,6 +21,16 @@ sources here, read from the `local/aws` checkouts: (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`. +- The management console host per partition, for `aws`, `aws-cn` and `aws-us-gov`: one sentence + each from the user-guide snapshots under `aws-docs/partition-guides` (`CONSOLE_HOST_SENTENCES`). + No source states it for the ISO or EUSC partitions, which get no field. The snapshots are these + three pages, saved under the path after the arrow: + https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-organizations.html -> gov/govcloud-organizations.md + https://docs.amazonaws.cn/en_us/aws/latest/userguide/console.html -> cn/console.md + https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/configure-account.html -> gov/configure-account.md + Either rendering parses: the page's text (what the `aws-refs` skill's docs driver extracts from + the `.html`), or the Markdown AWS serves at the same URL with `.md` in place of `.html`, which + writes the China host as a link. 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`. @@ -41,6 +52,7 @@ 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" +LOCAL_GUIDES = REPO_ROOT / "local/aws/aws-docs/partition-guides" TF_ELB = "internal/service/elb/hosted_zone_id_data_source.go" TF_ELBV2 = "internal/service/elbv2/hosted_zone_id_data_source.go" @@ -59,6 +71,21 @@ GO_CLOUDFRONT_DEFAULT_RE = re.compile(r'\}\s*\n\s*return "([A-Z0-9]+)" // See ht 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.-]+)';") +# partition -> (guide page, the sentence that states the console host). The China guide spells the +# host two ways: `console.amazonaws.cn` on its console page (cn/console.md, and again in +# cn/accounts-and-credentials.md) and `console.www.amazonaws.cn` once (cn/managing-aws.md). The +# console page's own statement is the one vendored. The commercial host is taken from the GovCloud +# guide, which names it where it sends the reader back to the commercial Region. AWS's Markdown +# rendering of the China page writes the host as a link, `[https://host](https://host).`, where the +# page text has `https://host .`; the pattern takes both. +CONSOLE_HOST_SENTENCES = { + "aws": ("gov/govcloud-organizations.md", re.compile( + r"in the commercial Region, sign in to the Organizations console at https://([a-z0-9.-]+)/organizations")), + "aws-cn": ("cn/console.md", re.compile( + r"Management Console is available in both Chinese and English and can be accessed at \[?https://([a-z0-9.-]+)[\] ]")), + "aws-us-gov": ("gov/configure-account.md", re.compile( + r"Sign in to the AWS GovCloud \(US\) console and open the IAM console at https://([a-z0-9.-]+)/iam")), +} 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"} @@ -149,10 +176,28 @@ def parse_vpc_endpoint_exceptions(text: str) -> tuple[dict[str, list[str]], dict return services, prefixes, cn_suffix +def parse_console_host(text: str, sentence: re.Pattern[str], page: str) -> str: + """The one host `sentence` states in a guide page; a page that lost the sentence, or states two + different hosts with it, is an error rather than a guess.""" + hosts = sorted({match.group(1).rstrip(".") for match in sentence.finditer(text)}) + if len(hosts) != 1: + raise ValueError(f"{page}: expected one console host from /{sentence.pattern}/, found {hosts}") + return hosts[0] + + +def parse_console_hosts(guides: Path) -> dict[str, str]: + return {partition: parse_console_host((guides / page).read_text(encoding="utf-8"), sentence, page) + for partition, (page, sentence) in CONSOLE_HOST_SENTENCES.items()} + + +def guides_present(guides: Path) -> bool: + return all((guides / page).exists() for page, _ in CONSOLE_HOST_SENTENCES.values()) + + # --------------------------------------------------------------------------- # # Generation # --------------------------------------------------------------------------- # -def build(terraform: Path, cdk: Path, partitions_doc: dict) -> dict: +def build(terraform: Path, cdk: Path, guides: 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") @@ -170,6 +215,7 @@ def build(terraform: Path, cdk: Path, partitions_doc: dict) -> dict: (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") + console_hosts = parse_console_hosts(guides) 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"]} @@ -204,6 +250,8 @@ def build(terraform: Path, cdk: Path, partitions_doc: dict) -> dict: entry["cloudfrontHostedZoneId"] = cloudfront_tf[partition_id] if partition_id in saml: entry["samlSignOnUrl"] = saml[partition_id] + if partition_id in console_hosts: + entry["consoleHost"] = console_hosts[partition_id] partitions[partition_id] = entry return { @@ -211,6 +259,8 @@ def build(terraform: Path, cdk: Path, partitions_doc: dict) -> dict: "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}", + "docs": "aws-docs/partition-guides (snapshots of the China and GovCloud user guides) " + + ", ".join(sorted(page for page, _ in CONSOLE_HOST_SENTENCES.values())), "s3WebsiteConflicts": "terraform wins over the CDK where they disagree (it cites the S3 website-endpoint table)", }, "partitions": partitions, @@ -234,6 +284,7 @@ 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-]+)+") +CONSOLE_HOST_RE = re.compile(r"console(\.[a-z0-9-]+){2,}") def self_check(path: Path, partitions_path: Path = PARTITIONS) -> list[str]: @@ -269,6 +320,8 @@ def self_check(path: Path, partitions_path: Path = PARTITIONS) -> list[str]: expected = ".".join(reversed(partition["dnsSuffix"].split("."))) + ".vpce" if entry.get("vpcEndpointServiceNamePrefix") != expected: problems.append(f"{path}: {partition['id']}.vpcEndpointServiceNamePrefix should be {expected!r}") + if partition["id"] in CONSOLE_HOST_SENTENCES and "consoleHost" not in entry: + problems.append(f"{path}: {partition['id']}.consoleHost is missing") known = {region["id"] for partition in partitions_doc for region in partition["regions"]} for region in document["regions"]: if region not in known: @@ -277,7 +330,7 @@ def self_check(path: Path, partitions_path: Path = PARTITIONS) -> list[str]: 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) + "vpcEndpointServiceNamePrefix": VPCE_PREFIX_RE, "consoleHost": CONSOLE_HOST_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): @@ -303,12 +356,14 @@ def main(argv: list[str] | None = None) -> int: 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("--guides", type=Path, default=LOCAL_GUIDES, help="aws-docs/partition-guides snapshots") 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() + sources_present = ((args.terraform / TF_ELB).exists() and (args.cdk / CDK_FACT_TABLES).exists() + and guides_present(args.guides)) if not sources_present: if args.check: problems = self_check(args.output, args.partitions) @@ -318,9 +373,10 @@ def main(argv: list[str] | None = None) -> int: 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") + raise SystemExit("error: regenerating needs the terraform-provider-aws and aws-cdk checkouts and the " + "aws-docs/partition-guides snapshots under local/aws") - document = build(args.terraform, args.cdk, json.loads(args.partitions.read_text(encoding="utf-8"))) + document = build(args.terraform, args.cdk, args.guides, 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 "" diff --git a/tools/aws/test_regen_region_facts.py b/tools/aws/test_regen_region_facts.py index bdfeea1cc7..2893b4d38a 100644 --- a/tools/aws/test_regen_region_facts.py +++ b/tools/aws/test_regen_region_facts.py @@ -102,6 +102,24 @@ TS_VPCE = ''' return VPC_ENDPOINT_SERVICE_EXCEPTIONS[region]?.includes(name) ? '.cn' : ''; } ''' +GUIDE_COMMERCIAL = """ +From the management account of your organization in the commercial Region, sign in to the Organizations console at https://console.aws.amazon.com/organizations +""" +GUIDE_CHINA = """ +The Amazon Web Services Management Console is available in both Chinese and English and can be accessed at https://console.amazonaws.cn . The console is dedicated to the China Regions. +""" +GUIDE_GOVCLOUD = """ +Sign in to the AWS GovCloud (US) console and open the IAM console at https://console.amazonaws-us-gov.com/iam. + +IAM users can now use either the account alias or account number. + +Sign in to the AWS GovCloud (US) console and open the IAM console at https://console.amazonaws-us-gov.com/iam. +""" +# the same sentence in the Markdown AWS serves at .../userguide/console.md +GUIDE_CHINA_MARKDOWN = """ +The Amazon Web Services Management Console is available in both Chinese and English and can be accessed at [https://console.amazonaws.cn](https://console.amazonaws.cn). The Amazon Web Services Management Console is dedicated to the China Regions. +""" +GUIDES = {"aws": GUIDE_COMMERCIAL, "aws-cn": GUIDE_CHINA, "aws-us-gov": GUIDE_GOVCLOUD} 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"}]}, @@ -110,17 +128,18 @@ PARTITIONS = {"partitions": [ ]} -def write_sources(tmp_path: Path) -> tuple[Path, Path]: +def write_sources(tmp_path: Path) -> tuple[Path, Path, Path]: tf = tmp_path / "terraform" cdk = tmp_path / "cdk" + guides = tmp_path / "guides" 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), - ]: + ] + [(guides, r.CONSOLE_HOST_SENTENCES[partition][0], body) for partition, body in GUIDES.items()]: path = root / rel path.parent.mkdir(parents=True, exist_ok=True) path.write_text(body) - return tf, cdk + return tf, cdk, guides @pytest.mark.parametrize("camel,region", [ @@ -159,9 +178,36 @@ def test_vpc_endpoint_exceptions_are_per_region_and_service(): assert cn_suffix == {"cn-north-1": ["transcribe"], "cn-northwest-1": ["transcribe"]} +def test_console_hosts_come_from_the_guide_sentences(tmp_path): + _, _, guides = write_sources(tmp_path) + assert r.parse_console_hosts(guides) == {"aws": "console.aws.amazon.com", "aws-cn": "console.amazonaws.cn", + "aws-us-gov": "console.amazonaws-us-gov.com"} + assert r.guides_present(guides) + assert not r.guides_present(tmp_path / "nowhere") + + +def test_the_china_console_host_parses_from_the_markdown_link_form(): + page, sentence = r.CONSOLE_HOST_SENTENCES["aws-cn"] + assert r.parse_console_host(GUIDE_CHINA_MARKDOWN, sentence, page) == "console.amazonaws.cn" + + +def test_a_guide_page_without_its_sentence_or_with_two_hosts_is_an_error(): + page, sentence = r.CONSOLE_HOST_SENTENCES["aws-us-gov"] + with pytest.raises(ValueError, match="configure-account"): + r.parse_console_host("Sign in to the AWS GovCloud (US) console.", sentence, page) + with pytest.raises(ValueError, match="console.example.com"): + r.parse_console_host(GUIDE_GOVCLOUD + GUIDE_GOVCLOUD.replace("console.amazonaws-us-gov.com", "console.example.com"), + sentence, page) + # the China guide's other spelling sits in a different sentence and is not picked up + page, sentence = r.CONSOLE_HOST_SENTENCES["aws-cn"] + with pytest.raises(ValueError, match="console.md"): + r.parse_console_host("a graphical user interface that you access online at https://console.www.amazonaws.cn .", + sentence, page) + + 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) + tf, cdk, guides = write_sources(tmp_path) + document = r.build(tf, cdk, guides, PARTITIONS) regions = document["regions"] assert regions["us-east-1"] == {"albHostedZoneId": "Z35SXDOTRQ7X7K", "classicElbHostedZoneId": "Z35SXDOTRQ7X7K", "nlbHostedZoneId": "Z26RNL4JYFTOTI", "s3WebsiteHostedZoneId": "Z3AQBSTGFYJSTF"} @@ -174,23 +220,26 @@ def test_build_merges_sources_with_terraform_winning_the_s3_conflicts(tmp_path): partitions = document["partitions"] assert partitions["aws-cn"] == {"vpcEndpointServiceNamePrefix": "cn.com.amazonaws.vpce", "cloudfrontHostedZoneId": "Z3RFFRIM2A3IF5", - "samlSignOnUrl": "https://signin.amazonaws.cn/saml"} + "samlSignOnUrl": "https://signin.amazonaws.cn/saml", + "consoleHost": "console.amazonaws.cn"} + assert partitions["aws-us-gov"]["consoleHost"] == "console.amazonaws-us-gov.com" 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) + tf, cdk, guides = 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"] + r.build(tf, cdk, guides, {"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) + tf, cdk, guides = 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)] + common = ["--terraform", str(tf), "--cdk", str(cdk), "--guides", str(guides), + "--partitions", str(partitions), "--output", str(output)] assert r.main(["--check"] + common) == 1 assert r.main(common) == 0 assert r.main(["--check"] + common) == 0 @@ -199,23 +248,28 @@ def test_cli_write_check_and_self_check(tmp_path, capsys): 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 + # nor with the checkouts but without the guide snapshots + assert r.main(["--check", "--terraform", str(tf), "--cdk", str(cdk), "--guides", 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(): + if (not (r.LOCAL_TERRAFORM / r.TF_ELB).exists() or not (r.LOCAL_CDK / r.CDK_FACT_TABLES).exists() + or not r.guides_present(r.LOCAL_GUIDES)): 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"))) + document = r.build(r.LOCAL_TERRAFORM, r.LOCAL_CDK, r.LOCAL_GUIDES, 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) + tf, cdk, guides = write_sources(tmp_path) + good = r.build(tf, cdk, guides, PARTITIONS) output = tmp_path / "region-facts.json" def problems(mutate): @@ -238,6 +292,12 @@ def test_self_check_holds_the_values_to_what_partitions_json_publishes(tmp_path) vpcEndpointServiceNamePrefix="com.example.vpce"), "plain-http SAML URL": lambda d: d["partitions"][partition].update( samlSignOnUrl="http://signin.example/saml"), + "console host with a scheme": lambda d: d["partitions"][partition].update( + consoleHost="https://console.aws.amazon.com"), + "console host that is not a console": lambda d: d["partitions"][partition].update(consoleHost="example.com"), + "missing China console host": lambda d: d["partitions"]["aws-cn"].pop("consoleHost"), + "missing GovCloud console host": lambda d: d["partitions"]["aws-us-gov"].pop("consoleHost"), + "missing commercial console host": lambda d: d["partitions"]["aws"].pop("consoleHost"), "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"]}),