mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
e9c37666c7
commit
9b36aa6980
4 files changed
+111
-15
No files matched your search
@@ -774,9 +774,10 @@ adds the corresponding security schemes and operation requirements. API Gateway
|
||||
these as `authorizationType: AWS_IAM` and `apiKeyRequired: true`: invocation requires both
|
||||
a valid SigV4 signature and an enabled API key associated with the invoked stage when both
|
||||
controls are selected. Existing operation requirements are preserved. When a default applies
|
||||
to OpenAPI 3, inline custom authorizer schemes are removed unless they also appear in the
|
||||
SAM `securityDefinitions` map. This matches SAM's removal of undeclared OpenAPI authorizers
|
||||
before import. Swagger 2 keeps its custom schemes. Defaults are selected by the SAM names
|
||||
to OpenAPI 3, SAM replaces the entire `components.securitySchemes` map with its
|
||||
`securityDefinitions` plus generated defaults, then removes `securityDefinitions`. Inline
|
||||
schemes absent from that map are dropped, including schemes without an authorizer. Other
|
||||
components are preserved. Swagger 2 keeps its custom schemes. Defaults are selected by the SAM names
|
||||
`AWS_IAM` and `api_key`; an existing scheme with another name does not suppress either default.
|
||||
As in SAM, the defaults are added to each operation's own `security`, inside each branch of
|
||||
an `Fn::If` written around a path or an operation, skipping an `AWS::NoValue` branch. A
|
||||
|
||||
+11
-11
@@ -108,21 +108,21 @@ final class SamRestApiAuth {
|
||||
}
|
||||
}
|
||||
if (!swagger) {
|
||||
removeUndeclaredCustomSchemes(document, definitions);
|
||||
replaceOpenApiSchemes(document, iam, keyRequired);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeUndeclaredCustomSchemes(ObjectNode document, ObjectNode definitions) {
|
||||
// SAM builds securityDefinitions first, then replaces OpenAPI securitySchemes with it.
|
||||
// Inline OpenAPI authorizers absent from that generated map do not reach the importer.
|
||||
Iterator<Map.Entry<String, JsonNode>> schemes = definitions.fields();
|
||||
while (schemes.hasNext()) {
|
||||
Map.Entry<String, JsonNode> scheme = schemes.next();
|
||||
if (scheme.getValue().has("x-amazon-apigateway-authorizer")
|
||||
&& !document.path("securityDefinitions").has(scheme.getKey())) {
|
||||
schemes.remove();
|
||||
}
|
||||
private void replaceOpenApiSchemes(ObjectNode document, boolean iam, boolean keyRequired) {
|
||||
// SAM replaces the whole OpenAPI map with its generated Swagger definitions.
|
||||
ObjectNode definitions = object(document, "securityDefinitions");
|
||||
if (iam) {
|
||||
addScheme(definitions, "AWS_IAM", true);
|
||||
}
|
||||
if (keyRequired) {
|
||||
addScheme(definitions, "api_key", false);
|
||||
}
|
||||
object(document, "components").set("securitySchemes", definitions);
|
||||
document.remove("securityDefinitions");
|
||||
}
|
||||
|
||||
private void applyDefaults(ObjectNode operation, ObjectNode definitions, boolean iam, boolean keyRequired) {
|
||||
|
||||
+38
@@ -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"})
|
||||
|
||||
+58
-1
@@ -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"));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user