fix(cloudformation): replace SAM OpenAPI security schemes (#6638)

Follow the separate whole-map replacement requested in upstream #6632. Preserve Swagger, no-default behavior, operation requirements and unrelated OpenAPI components.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-26 09:44:15 -04:00
1 parent e9c37666c7
commit 9b36aa6980
4 files changed
+111 -15

No files matched your search

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testutil.ExecuteApiRequestSigner;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
@@ -13,7 +14,14 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
import software.amazon.awssdk.services.apigateway.model.GetMethodResponse;
import java.net.URI;
import java.time.Instant;
import java.util.Map;
import java.util.UUID;
@@ -22,6 +30,7 @@ import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** Exercises transformed auth through the REST importer and the real execute-api HTTP verifier. */
@@ -30,6 +39,9 @@ class SamRestApiAuthIntegrationTest {
private final ObjectMapper mapper = new ObjectMapper();
@TestHTTPResource("/")
URI endpoint;
@ParameterizedTest
@CsvSource({"false,true", "true,false", "true,true"})
void conflictingAuthorizersCannotSilentlyReplaceTheSamIamDefault(boolean swagger, boolean repeated)
@@ -85,6 +97,32 @@ class SamRestApiAuthIntegrationTest {
});
}
@Test
void undeclaredInlineApiKeyDoesNotSurviveSamOpenApiPostProcessing() throws Exception {
ObjectNode template = template(false);
ObjectNode properties = (ObjectNode) template.at("/Resources/Api/Properties");
properties.set("Auth", mapper.readTree("{\"DefaultAuthorizer\":\"AWS_IAM\"}"));
ObjectNode body = (ObjectNode) properties.get("DefinitionBody");
body.set("components", mapper.readTree("""
{"securitySchemes":{"inlineKey":{"type":"apiKey","name":"x-api-key","in":"header"}}}
"""));
((ObjectNode) body.at("/paths/~1items/post")).set("security",
mapper.readTree("[{\"inlineKey\":[]}]"));
try (ApiGatewayClient client = ApiGatewayClient.builder().endpointOverride(endpoint)
.region(Region.US_EAST_1).httpClientBuilder(UrlConnectionHttpClient.builder())
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build()) {
withDeployedApi(template, "sam-inline-key-", apiId -> {
String resourceId = client.getResources(request -> request.restApiId(apiId)).items().stream()
.filter(resource -> "/items".equals(resource.path())).findFirst().orElseThrow().id();
GetMethodResponse method = client.getMethod(request -> request.restApiId(apiId)
.resourceId(resourceId).httpMethod("POST"));
assertEquals("AWS_IAM", method.authorizationType());
assertFalse(method.apiKeyRequired());
});
}
}
@ParameterizedTest
@CsvSource({"false,none", "false,custom-authentication", "false,awsSigv4",
"true,none", "true,custom-authentication", "true,awsSigv4"})
@@ -154,6 +154,63 @@ class SamRestApiAuthTest {
body.set("securityDefinitions", definitions.deepCopy());
JsonNode expanded = processor.expandSamTemplate(template).at("/Resources/Api/Properties/Body");
assertEquals(definitions.get("custom"), expanded.at("/components/securitySchemes/custom"));
assertFalse(expanded.has("securityDefinitions"));
}
@ParameterizedTest
@CsvSource({"false,iam,false", "false,key,false", "false,none,true",
"true,iam,true", "true,key,true", "true,none,true"})
void replacesInlineSchemesOnlyForOpenApiDefaults(boolean swagger, String defaults, boolean retained)
throws Exception {
ObjectNode template = template(swagger);
ObjectNode properties = (ObjectNode) template.at("/Resources/Api/Properties");
properties.set("Auth", mapper.readTree(switch (defaults) {
case "iam" -> "{\"DefaultAuthorizer\":\"AWS_IAM\"}";
case "key" -> "{\"ApiKeyRequired\":true}";
default -> "{\"ApiKeyRequired\":false}";
}));
ObjectNode body = (ObjectNode) properties.get("DefinitionBody");
JsonNode schemes = mapper.readTree("""
{"inlineKey":{"type":"apiKey","name":"x-inline-key","in":"header"},
"inlineOAuth":{"type":"oauth2","flows":{}}}
""");
setSchemes(body, swagger, (ObjectNode) schemes.deepCopy());
body.set("security", mapper.readTree("[{\"inlineOAuth\":[\"read\"]}]"));
((ObjectNode) body.at("/paths/~1items/get")).set("security",
mapper.readTree("[{\"inlineKey\":[]}]"));
JsonNode original = template.deepCopy();
JsonNode expanded = processor.expandSamTemplate(template).at("/Resources/Api/Properties/Body");
JsonNode result = swagger ? expanded.path("securityDefinitions")
: expanded.at("/components/securitySchemes");
assertEquals(retained, result.has("inlineKey"));
assertEquals(retained, result.has("inlineOAuth"));
assertEquals(body.get("security"), expanded.get("security"));
assertEquals(body.at("/paths/~1items/get/security/0"), expanded.at("/paths/~1items/get/security/0"));
assertEquals(original, template);
}
@Test
void replacesInlineMetadataWithDeclaredDefinitionsAndPreservesOtherComponents() throws Exception {
ObjectNode template = template(false);
ObjectNode body = (ObjectNode) template.at("/Resources/Api/Properties/DefinitionBody");
body.set("components", mapper.readTree("""
{"schemas":{"Example":{"type":"string"}},
"securitySchemes":{"declared":{"type":"apiKey","name":"old","in":"header"}}}
"""));
JsonNode declared = mapper.readTree("""
{"declared":{"type":"apiKey","name":"new","in":"header"},
"onlyDeclared":{"type":"oauth2","flows":{}}}
""");
body.set("securityDefinitions", declared.deepCopy());
JsonNode original = template.deepCopy();
JsonNode expanded = processor.expandSamTemplate(template).at("/Resources/Api/Properties/Body");
assertEquals(declared.get("declared"), expanded.at("/components/securitySchemes/declared"));
assertEquals(declared.get("onlyDeclared"), expanded.at("/components/securitySchemes/onlyDeclared"));
assertEquals(body.at("/components/schemas"), expanded.at("/components/schemas"));
assertFalse(expanded.has("securityDefinitions"));
assertTrue(expanded.at("/components/securitySchemes/AWS_IAM").isObject());
assertTrue(expanded.at("/components/securitySchemes/api_key").isObject());
assertEquals(original, template);
}
@ParameterizedTest
@@ -171,7 +228,7 @@ class SamRestApiAuthTest {
JsonNode original = template.deepCopy();
JsonNode expanded = processor.expandSamTemplate(template).at("/Resources/Api/Properties/Body");
assertEquals(original, template);
assertEquals(scheme, expanded.at("/components/securitySchemes/existing"));
assertTrue(expanded.at("/components/securitySchemes/existing").isMissingNode());
assertEquals(mapper.readTree("[{\"existing\":[]},{\"AWS_IAM\":[]},{\"api_key\":[]}]"),
expanded.at("/paths/~1items/get/security"));
}