diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationKmsKeyPropertiesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationKmsKeyPropertiesIntegrationTest.java index f2611625af..dc82769104 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationKmsKeyPropertiesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationKmsKeyPropertiesIntegrationTest.java @@ -35,7 +35,7 @@ class CloudFormationKmsKeyPropertiesIntegrationTest { """)); CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE", EnforcementFixtures.auth(account, "cloudformation")); - String keyId = physicalId(account, stack); + String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, EnforcementFixtures.auth(account, "cloudformation")); JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId)); assertAll( () -> assertEquals(keyId, key.getString("KeyMetadata.KeyId")), @@ -50,7 +50,7 @@ class CloudFormationKmsKeyPropertiesIntegrationTest { createStack(account, stack, template("")); CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE", EnforcementFixtures.auth(account, "cloudformation")); - String keyId = physicalId(account, stack); + String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, EnforcementFixtures.auth(account, "cloudformation")); JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId)); assertAll( @@ -85,17 +85,6 @@ class CloudFormationKmsKeyPropertiesIntegrationTest { .statusCode(200); } - private static String physicalId(String account, String stack) { - return CfnRequests.cfn("DescribeStackResource", stack) - .header("Authorization", EnforcementFixtures.auth(account, "cloudformation")) - .formParam("LogicalResourceId", KEY_RESOURCE) - .when() - .post("/") - .then() - .statusCode(200) - .extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }"); - } - private static JsonPath kms(String account, String action, String body) { return given() .header("Authorization", EnforcementFixtures.auth(account, "kms")) diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java index 35acd9eb33..9a3db1a02f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.cloudformation; +import io.github.hectorvent.floci.testing.CfnRequests; import io.github.hectorvent.floci.testing.CfnStackWaits; import io.github.hectorvent.floci.testing.PartitionCleanup; import io.quarkus.test.junit.QuarkusTest; @@ -316,17 +317,7 @@ class CloudFormationLambdaObjectPropertyIntegrationTest { """.formatted(bucket, key)); assertEquals("String".equals(type) ? "ROLLBACK_COMPLETE" : "CREATE_COMPLETE", CfnStackWaits.awaitTerminal(parent, CFN_AUTH).status(), type); - String childStack = given() - .contentType("application/x-www-form-urlencoded") - .header("Authorization", CFN_AUTH) - .formParam("Action", "DescribeStackResource") - .formParam("StackName", parent) - .formParam("LogicalResourceId", "Child") - .when() - .post("/") - .then() - .statusCode(200) - .extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }"); + String childStack = CfnRequests.physicalId(parent, "Child", CFN_AUTH); if ("String".equals(type)) { assertEventReason(childStack, "Property [Architectures] expected type: JSONArray, found: String"); } else { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java index 4022e8e25f..c8524d9188 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.route53.Route53HostedZoneTestSupport; import io.github.hectorvent.floci.services.route53.Route53Service; +import io.github.hectorvent.floci.testing.CfnRequests; import io.github.hectorvent.floci.testing.CfnStackWaits; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; @@ -398,12 +399,12 @@ class CloudFormationRoute53RecordSetIntegrationTest { String stackName = "r53-hc-replace-" + Long.toString(System.nanoTime(), 36); createStack(stackName, healthCheckTemplate(30, false, false)); assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - String oldId = physicalId(stackName, "Check"); + String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH); try { updateStack(stackName, healthCheckTemplate(10, false, false)); assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - String newId = physicalId(stackName, "Check"); + String newId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH); assertNotEquals(oldId, newId); assertTrue(healthCheck(newId).contains("10")); healthCheckRequest(oldId).then().statusCode(404); @@ -418,12 +419,12 @@ class CloudFormationRoute53RecordSetIntegrationTest { String stackName = "r53-hc-retain-" + Long.toString(System.nanoTime(), 36); createStack(stackName, healthCheckTemplate(30, true, false)); assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - String oldId = physicalId(stackName, "Check"); + String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH); try { updateStack(stackName, healthCheckTemplate(10, true, false)); assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - assertNotEquals(oldId, physicalId(stackName, "Check")); + assertNotEquals(oldId, CfnRequests.physicalId(stackName, "Check", CFN_AUTH)); assertTrue(healthCheck(oldId).contains("30")); } finally { deleteStack(stackName); @@ -438,12 +439,12 @@ class CloudFormationRoute53RecordSetIntegrationTest { String stackName = "r53-hc-rollback-" + Long.toString(System.nanoTime(), 36); createStack(stackName, healthCheckTemplate(30, false, false)); assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - String oldId = physicalId(stackName, "Check"); + String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH); try { updateStack(stackName, healthCheckTemplate(10, false, true)); assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); - assertEquals(oldId, physicalId(stackName, "Check")); + assertEquals(oldId, CfnRequests.physicalId(stackName, "Check", CFN_AUTH)); assertTrue(healthCheck(oldId).contains("30")); String checks = given().header("Authorization", ROUTE53_AUTH) .when().get("/2013-04-01/healthcheck") @@ -473,7 +474,7 @@ class CloudFormationRoute53RecordSetIntegrationTest { // AWS documents Ref as the group's name, for example MyRecordSetGroup, and Fn::GetAtt Id as // the ID GetChange takes. assertEquals("Group", stackOutput(stackName, "GroupRef")); - assertEquals("Group", physicalId(stackName, "Group")); + assertEquals("Group", CfnRequests.physicalId(stackName, "Group", CFN_AUTH)); firstChange = stackOutput(stackName, "GroupChange"); assertChangeInSync(firstChange); String created = listResourceRecordSets(zoneId); @@ -540,7 +541,7 @@ class CloudFormationRoute53RecordSetIntegrationTest { parameterizedStackRequest(stackName, "CreateStack", template, "BlueWeight", "70"); assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); try { - String checkId = physicalId(stackName, "Check"); + String checkId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH); String created = listResourceRecordSets(zoneId); assertTrue(created.contains("blue70"), created); assertTrue(created.contains("green30"), created); @@ -1059,17 +1060,6 @@ class CloudFormationRoute53RecordSetIntegrationTest { .when().post("/").then().statusCode(200); } - private String physicalId(String stackName, String logicalId) { - return given() - .contentType("application/x-www-form-urlencoded") - .header("Authorization", CFN_AUTH) - .formParam("Action", "DescribeStackResource") - .formParam("StackName", stackName) - .formParam("LogicalResourceId", logicalId) - .when().post("/").then().statusCode(200) - .extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }"); - } - private Response healthCheckRequest(String id) { return given().header("Authorization", ROUTE53_AUTH) .when().get("/2013-04-01/healthcheck/" + id); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSamEventInvokeConfigIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSamEventInvokeConfigIntegrationTest.java index 13cebc2b29..95951a1d35 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSamEventInvokeConfigIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSamEventInvokeConfigIntegrationTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; +import static io.github.hectorvent.floci.testing.CfnRequests.physicalIdUnauthenticated; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -113,7 +114,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest { .body("MaximumEventAgeInSeconds", equalTo(21600)) .body("DestinationConfig.OnFailure.Destination", equalTo(queueArn)); - getRolePolicy(physicalId(name, "FnRole"), "FnEventInvokeConfigOnFailureSQSPolicy") + getRolePolicy(physicalIdUnauthenticated(name, "FnRole"), "FnEventInvokeConfigOnFailureSQSPolicy") .body(containsString("sqs:SendMessage")) .body(containsString(queueArn)) .body(not(containsString("Fn::GetAtt"))); @@ -177,13 +178,13 @@ class CloudFormationSamEventInvokeConfigIntegrationTest { """.formatted(name)); CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE"); - String topicArn = physicalId(name, "FnEventInvokeConfigOnSuccessTopic"); + String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnSuccessTopic"); given().queryParam("Qualifier", "$LATEST") .when().get("/2019-09-25/functions/" + name + "/event-invoke-config") .then() .statusCode(200) .body("DestinationConfig.OnSuccess.Destination", equalTo(topicArn)); - getRolePolicy(physicalId(name, "FnRole"), "FnEventInvokeConfigOnSuccessSNSPolicy") + getRolePolicy(physicalIdUnauthenticated(name, "FnRole"), "FnEventInvokeConfigOnSuccessSNSPolicy") .body(containsString("sns:publish")) .body(containsString(topicArn)); } @@ -300,7 +301,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest { .statusCode(200); CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE"); - String topicArn = physicalId(name, "FnEventInvokeConfigOnFailureTopic"); + String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnFailureTopic"); given().queryParam("Qualifier", "$LATEST") .when().get("/2019-09-25/functions/" + name + "/event-invoke-config") .then() @@ -345,7 +346,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest { """.formatted(name, parameterName)); CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE"); - String topicArn = physicalId(name, "FnEventInvokeConfigOnFailureTopic"); + String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnFailureTopic"); given().queryParam("Qualifier", "$LATEST") .when().get("/2019-09-25/functions/" + name + "/event-invoke-config") .then() @@ -401,16 +402,6 @@ class CloudFormationSamEventInvokeConfigIntegrationTest { .statusCode(200); } - private static String physicalId(String stackName, String logicalId) { - XmlPath xml = given() - .contentType("application/x-www-form-urlencoded") - .formParam("Action", "DescribeStackResource") - .formParam("StackName", stackName) - .formParam("LogicalResourceId", logicalId) - .when().post("/").then().statusCode(200).extract().xmlPath(); - return xml.getString("**.find { it.name() == 'PhysicalResourceId' }"); - } - private static ValidatableResponse getRolePolicy(String roleName, String policyName) { return given() .contentType("application/x-www-form-urlencoded") diff --git a/src/test/java/io/github/hectorvent/floci/testing/CfnRequests.java b/src/test/java/io/github/hectorvent/floci/testing/CfnRequests.java index 6113d17767..a206ec1191 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/CfnRequests.java +++ b/src/test/java/io/github/hectorvent/floci/testing/CfnRequests.java @@ -17,6 +17,20 @@ public final class CfnRequests { return stackName == null ? request : request.formParam("StackName", stackName); } + public static String physicalIdUnauthenticated(String stackName, String logicalId) { + return physicalId(stackName, logicalId, null); + } + + public static String physicalId(String stackName, String logicalId, String authorization) { + RequestSpecification request = cfn("DescribeStackResource", stackName) + .formParam("LogicalResourceId", logicalId); + if (authorization != null) { + request.header("Authorization", authorization); + } + return request.when().post("/").then().statusCode(200) + .extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }"); + } + /** Returns the stack-level tags reported by DescribeStacks. */ public static Map tags(String stackName) { String xml = cfn("DescribeStacks", stackName).when().post("/")