diff --git a/src/main/resources/aws/payment-cryptography-request-shapes.json b/src/main/resources/aws/payment-cryptography-request-shapes.json index 5a8c400b5d..973325d26c 100644 --- a/src/main/resources/aws/payment-cryptography-request-shapes.json +++ b/src/main/resources/aws/payment-cryptography-request-shapes.json @@ -143,13 +143,13 @@ "type": "string", "min": 70, "max": 150, - "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2}-[a-z]{1,16}-[0-9]+:[0-9]{12}:key/[0-9a-zA-Z]{16,64}" + "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2,4}(-[a-z]{1,16})+-[0-9]+:[0-9]{12}:key/[0-9a-zA-Z]{16,64}" }, "KeyArnOrKeyAliasType": { "type": "string", "min": 7, "max": 322, - "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2}-[a-z]{1,16}-[0-9]+:[0-9]{12}:(key/[0-9a-zA-Z]{16,64}|alias/[a-zA-Z0-9/_-]+)$|^alias/[a-zA-Z0-9/_-]+" + "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2,4}(-[a-z]{1,16})+-[0-9]+:[0-9]{12}:(key/[0-9a-zA-Z]{16,64}|alias/[a-zA-Z0-9/_-]+)$|^alias/[a-zA-Z0-9/_-]+" }, "KeyAttributes": { "type": "structure", @@ -278,7 +278,7 @@ }, "Region": { "type": "string", - "pattern": "[a-z]{2}-[a-z]{1,16}-[0-9]+" + "pattern": "[a-z]{2,4}(-[a-z]{1,16})+-[0-9]+" }, "Regions": { "type": "list", @@ -288,7 +288,7 @@ "type": "string", "min": 70, "max": 150, - "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2}-[a-z]{1,16}-[0-9]+:[0-9]{12}:key/[0-9a-zA-Z]{16,64}" + "pattern": "arn:aws[a-z0-9-]*:payment-cryptography:[a-z]{2,4}(-[a-z]{1,16})+-[0-9]+:[0-9]{12}:key/[0-9a-zA-Z]{16,64}" }, "Tag": { "type": "structure", diff --git a/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyIntegrationTest.java index fe274c9e88..b3813ea502 100644 --- a/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyIntegrationTest.java @@ -1,10 +1,16 @@ package io.github.hectorvent.floci.services.paymentcryptography; +import io.github.hectorvent.floci.testing.PartitionCleanup; +import io.github.hectorvent.floci.testing.PartitionMatrix; +import io.github.hectorvent.floci.testing.PartitionMatrix.PartitionCase; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.util.List; @@ -28,6 +34,9 @@ class PaymentCryptographyIntegrationTest { private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; private static final String TARGET = "PaymentCryptographyControlPlane."; + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -41,6 +50,15 @@ class PaymentCryptographyIntegrationTest { .when().post("/"); } + private static Response callIn(String region, String action, String body) { + return given() + .header("Authorization", PartitionMatrix.sigV4Auth(region, "payment-cryptography")) + .header("X-Amz-Target", TARGET + action) + .contentType(CONTENT_TYPE) + .body(body) + .when().post("/"); + } + private static final String CREATE_KEY = """ {"KeyAttributes":{"KeyAlgorithm":"AES_256","KeyClass":"SYMMETRIC_KEY", "KeyUsage":"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY", @@ -441,4 +459,60 @@ class PaymentCryptographyIntegrationTest { .then().statusCode(200) .body("Keys", hasSize(1)); } + + @ParameterizedTest + @MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES) + void aKeyOutsideTheCommercialPartitionIsNamedByTheArnItWasMintedWith(PartitionCase partitionCase) { + String region = partitionCase.region(); + String arn = callIn(region, "CreateKey", CREATE_KEY) + .then().statusCode(200) + .extract().path("Key.KeyArn"); + cleanup.register(() -> callIn(region, "DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}")); + PartitionMatrix.assertArnIn(partitionCase, arn); + + callIn(region, "GetKey", "{\"KeyIdentifier\":\"" + arn + "\"}") + .then().statusCode(200) + .body("Key.KeyArn", equalTo(arn)); + + callIn(region, "TagResource", "{\"ResourceArn\":\"" + arn + "\"," + + "\"Tags\":[{\"Key\":\"Origin\",\"Value\":\"PRIMARY\"}]}") + .then().statusCode(200); + callIn(region, "ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}") + .then().statusCode(200) + .body("Tags.Key", contains(equalTo("Origin"))); + + callIn(region, "CreateAlias", "{\"AliasName\":\"alias/team/partition\",\"KeyArn\":\"" + arn + "\"}") + .then().statusCode(200) + .body("Alias.KeyArn", equalTo(arn)); + cleanup.register(() -> callIn(region, "DeleteAlias", "{\"AliasName\":\"alias/team/partition\"}")); + callIn(region, "GetAlias", "{\"AliasName\":\"alias/team/partition\"}") + .then().statusCode(200) + .body("Alias.KeyArn", equalTo(arn)); + + String aliasArn = arn.substring(0, arn.lastIndexOf(":key/")) + ":alias/team/partition"; + callIn(region, "GetKey", "{\"KeyIdentifier\":\"" + aliasArn + "\"}") + .then().statusCode(200) + .body("Key.KeyArn", equalTo(arn)); + } + + @Test + void anArnWhoseRegionIsNotARegionIsRefusedByEveryMemberThatTakesOne() { + for (String region : List.of("us-gov", "us-gov-west-", "u-gov-west-1", "europe-de-east-1", "US-GOV-WEST-1")) { + String arn = "arn:aws-us-gov:payment-cryptography:" + region + ":000000000000:key/" + + "0123456789abcdef0123456789abcdef"; + + call("GetKey", "{\"KeyIdentifier\":\"" + arn + "\"}") + .then().statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("regular expression")); + call("CreateAlias", "{\"AliasName\":\"alias/team/refused\",\"KeyArn\":\"" + arn + "\"}") + .then().statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("regular expression")); + call("ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}") + .then().statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("regular expression")); + } + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyRequestShapesTest.java b/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyRequestShapesTest.java new file mode 100644 index 0000000000..7b42d2af8c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/paymentcryptography/PaymentCryptographyRequestShapesTest.java @@ -0,0 +1,46 @@ +package io.github.hectorvent.floci.services.paymentcryptography; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsPartition; +import io.github.hectorvent.floci.core.common.AwsPartitions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** The members that name a key or a region, checked against the vendored constraints in every published region. */ +class PaymentCryptographyRequestShapesTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String KEY_ID = "0123456789abcdef0123456789abcdef"; + + private static ObjectNode request(String member, String value) { + return MAPPER.createObjectNode().put(member, value); + } + + @Test + void everyPublishedRegionIsTaken() { + assertFalse(AwsPartitions.allRegionIds().isEmpty()); + for (String region : AwsPartitions.allRegionIds()) { + AwsPartition partition = AwsPartitions.forRegion(region).orElseThrow(); + String prefix = "arn:" + partition.id() + ":payment-cryptography:" + region + ":000000000000:"; + String key = prefix + "key/" + KEY_ID; + + assertDoesNotThrow(() -> PaymentCryptographyRequestShapes.check("GetKey", request("KeyIdentifier", key)), key); + assertDoesNotThrow(() -> PaymentCryptographyRequestShapes.check("GetKey", + request("KeyIdentifier", prefix + "alias/team/card-key")), region); + assertDoesNotThrow(() -> PaymentCryptographyRequestShapes.check("ListTagsForResource", + request("ResourceArn", key)), region); + ObjectNode create = MAPPER.createObjectNode(); + create.putObject("KeyAttributes") + .put("KeyAlgorithm", "AES_256") + .put("KeyClass", "SYMMETRIC_KEY") + .put("KeyUsage", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY") + .putObject("KeyModesOfUse").put("Encrypt", true); + create.put("Exportable", true); + create.putArray("ReplicationRegions").add(region); + assertDoesNotThrow(() -> PaymentCryptographyRequestShapes.check("CreateKey", create), region); + } + } +} diff --git a/tools/aws/regen_payment_cryptography_shapes.py b/tools/aws/regen_payment_cryptography_shapes.py index d0756a49cf..2bc6aee160 100644 --- a/tools/aws/regen_payment_cryptography_shapes.py +++ b/tools/aws/regen_payment_cryptography_shapes.py @@ -34,18 +34,24 @@ OPERATIONS = ( COMMERCIAL = "arn:aws:" EVERY_PARTITION = "arn:aws[a-z0-9-]*:" +# The region the model writes in its ARN patterns and on `Region`, which fits the commercial +# partition's two-part prefix alone, and the one that fits every published region: `us-gov-west-1` +# has a further part and `eusc-de-east-1` a longer prefix. +COMMERCIAL_REGION = "[a-z]{2}-[a-z]{1,16}-[0-9]+" +EVERY_REGION = "[a-z]{2,4}(-[a-z]{1,16})+-[0-9]+" -def widen_partition(doc: dict) -> dict: - """The model's patterns, with the partition they name widened to every partition. - The model writes the commercial partition, as the service's own model need not name the - others, but the service mints and takes the ARN of the partition a key is in, so the check - must not refuse the ARN of a key outside the commercial one. +def widen_partition_and_region(doc: dict) -> dict: + """The model's patterns, with the partition and the region they name widened to every partition's. + + The model writes the commercial partition and regions of its shape, as the service's own model + need not name the others, but the service mints and takes the ARN of the partition and the + region a key is in, so the check must not refuse the ARN of a key outside the commercial one. """ for shape in doc["shapes"].values(): pattern = shape.get("pattern") - if pattern and COMMERCIAL in pattern: - shape["pattern"] = pattern.replace(COMMERCIAL, EVERY_PARTITION) + if pattern: + shape["pattern"] = pattern.replace(COMMERCIAL, EVERY_PARTITION).replace(COMMERCIAL_REGION, EVERY_REGION) return doc @@ -55,8 +61,8 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("--botocore-data", type=Path, help="botocore data directory") args = parser.parse_args(argv) data, provenance = resolve_botocore_data(args.botocore_data) - doc = widen_partition(build_constraints(load_model(data, MODEL), OPERATIONS, provenance, - "tools/aws/regen_payment_cryptography_shapes.py")) + doc = widen_partition_and_region(build_constraints(load_model(data, MODEL), OPERATIONS, provenance, + "tools/aws/regen_payment_cryptography_shapes.py")) fresh = json.dumps(doc, indent=2) + "\n" if args.check: if not OUTPUT.exists() or strip_source(OUTPUT.read_text(encoding="utf-8")) != strip_source(fresh): diff --git a/tools/aws/test_request_shapes.py b/tools/aws/test_request_shapes.py index 96fa3313d5..7ae33548f6 100644 --- a/tools/aws/test_request_shapes.py +++ b/tools/aws/test_request_shapes.py @@ -12,6 +12,7 @@ from __future__ import annotations import gzip import json +import re from pathlib import Path import pytest @@ -393,6 +394,45 @@ def test_payment_cryptography_shapes_widen_the_partition_the_model_writes(): assert regen_payment_cryptography_shapes.EVERY_PARTITION in doc["shapes"]["ResourceArn"]["pattern"] +def test_payment_cryptography_shapes_widen_the_region_on_every_pattern_the_model_writes_it(): + doc = json.loads(regen_payment_cryptography_shapes.OUTPUT.read_text(encoding="utf-8")) + + patterns = [shape["pattern"] for shape in doc["shapes"].values() if "pattern" in shape] + assert not [p for p in patterns if regen_payment_cryptography_shapes.COMMERCIAL_REGION in p] + assert doc["shapes"]["Region"]["pattern"] == regen_payment_cryptography_shapes.EVERY_REGION + for name in ("KeyArn", "KeyArnOrKeyAliasType", "ResourceArn"): + assert regen_payment_cryptography_shapes.EVERY_REGION in doc["shapes"][name]["pattern"] + + +def test_payment_cryptography_shapes_take_every_published_region_of_its_partition(): + shapes = json.loads(regen_payment_cryptography_shapes.OUTPUT.read_text(encoding="utf-8"))["shapes"] + partitions = json.loads( + (regen_payment_cryptography_shapes.OUTPUT.parent / "partitions.json").read_text(encoding="utf-8"))["partitions"] + regions = [(partition["id"], region["id"]) for partition in partitions for region in partition["regions"]] + + assert len(regions) >= 46 + for partition, region in regions: + assert re.fullmatch(shapes["Region"]["pattern"], region), region + key = f"arn:{partition}:payment-cryptography:{region}:000000000000:key/0123456789abcdef" + alias = f"arn:{partition}:payment-cryptography:{region}:000000000000:alias/team/card-key" + assert re.fullmatch(shapes["KeyArn"]["pattern"], key), key + assert re.fullmatch(shapes["ResourceArn"]["pattern"], key), key + assert re.fullmatch(shapes["KeyArnOrKeyAliasType"]["pattern"], key), key + assert re.fullmatch(shapes["KeyArnOrKeyAliasType"]["pattern"], alias), alias + + +@pytest.mark.parametrize("region", [ + "us-gov", "us-gov-west-", "-gov-west-1", "u-gov-west-1", "europe-de-east-1", "US-GOV-WEST-1", "us_gov_west_1", +]) +def test_payment_cryptography_shapes_refuse_what_is_not_a_region(region): + shapes = json.loads(regen_payment_cryptography_shapes.OUTPUT.read_text(encoding="utf-8"))["shapes"] + + assert not re.fullmatch(shapes["Region"]["pattern"], region) + key = f"arn:aws:payment-cryptography:{region}:000000000000:key/0123456789abcdef" + for name in ("KeyArn", "KeyArnOrKeyAliasType", "ResourceArn"): + assert not re.fullmatch(shapes[name]["pattern"], key), name + + def test_payment_cryptography_shapes_take_the_key_an_arn_only_member_names(): doc = json.loads(regen_payment_cryptography_shapes.OUTPUT.read_text(encoding="utf-8"))