diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index fb3d16f6e9..e33a0496e1 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -712,6 +712,20 @@ Gateway v1 (`AWS`/Lambda mapping templates) and the AppSync resolver templates d ## API Gateway v2 (HTTP and WebSocket APIs) {#v2} +### HTTP and WebSocket integration credentials + +V2 management APIs retain `credentialsArn` for integrations and +`authorizerCredentialsArn` for REQUEST authorizers through create, update, get and +list. Omitting either member on update preserves its stored value. HTTP API quick +create retains integration credentials for Lambda ARN targets and ignores them for +HTTP targets. OpenAPI import and CloudFormation API `Body` map the integration +`credentials` and authorizer `authorizerCredentials` extensions to these fields. +Integration copies and authorizer restore paths retain them too. + +This models credential selection; execution-role assumption and credential-based +Lambda invocation authorization are not implemented by this change. Dedicated +CloudFormation credential properties remain outside this support. + **Protocol:** REST JSON **Endpoint:** `http://localhost:4566/v2/apis/...` diff --git a/pom.xml b/pom.xml index 8f1cf5d973..216d9d02fe 100644 --- a/pom.xml +++ b/pom.xml @@ -330,6 +330,12 @@ + + software.amazon.awssdk + apigatewayv2 + ${aws.sdk.version} + test + software.amazon.awssdk dynamodb diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java index f398dd989f..afffede9cc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java @@ -2800,6 +2800,9 @@ public class ApiGatewayController { if (i.getConnectionType() != null) node.put("connectionType", i.getConnectionType()); if (i.getConnectionId() != null) node.put("connectionId", i.getConnectionId()); node.put("payloadFormatVersion", i.getPayloadFormatVersion()); + if (i.getCredentialsArn() != null) { + node.put("credentialsArn", i.getCredentialsArn()); + } if (i.getIntegrationUri() != null) node.put("integrationUri", i.getIntegrationUri()); if (i.getRequestTemplates() != null) { ObjectNode requestTemplates = node.putObject("requestTemplates"); @@ -2933,6 +2936,9 @@ public class ApiGatewayController { jwt.put("issuer", a.getJwtConfiguration().issuer()); } } + if (a.getAuthorizerCredentialsArn() != null) { + node.put("authorizerCredentialsArn", a.getAuthorizerCredentialsArn()); + } if (a.getAuthorizerUri() != null) { node.put("authorizerUri", a.getAuthorizerUri()); } diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java index 920f74aa10..04a545d05f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java @@ -589,6 +589,9 @@ public class ApiGatewayV2JsonHandler { auth.getJwtConfiguration().audience().forEach(aud::add); } } + if (auth.getAuthorizerCredentialsArn() != null) { + node.put("AuthorizerCredentialsArn", auth.getAuthorizerCredentialsArn()); + } if (auth.getAuthorizerUri() != null) { node.put("AuthorizerUri", auth.getAuthorizerUri()); } @@ -628,6 +631,9 @@ public class ApiGatewayV2JsonHandler { if (i.getConnectionType() != null) node.put("ConnectionType", i.getConnectionType()); if (i.getConnectionId() != null) node.put("ConnectionId", i.getConnectionId()); node.put("PayloadFormatVersion", i.getPayloadFormatVersion()); + if (i.getCredentialsArn() != null) { + node.put("CredentialsArn", i.getCredentialsArn()); + } if (i.getIntegrationUri() != null) node.put("IntegrationUri", i.getIntegrationUri()); if (i.getRequestTemplates() != null) { ObjectNode requestTemplates = node.putObject("RequestTemplates"); diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java index 82e6387e0c..8fb6fdb0f1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java @@ -526,6 +526,7 @@ public class ApiGatewayV2OpenApiImporter { if (!identitySource.isEmpty()) { request.put("identitySource", identitySource); } + putIfPresent(request, "authorizerCredentialsArn", stringValue(authDef.get("authorizerCredentials"))); putIfPresent(request, "authorizerUri", stringValue(authDef.get("authorizerUri"))); putIfPresent(request, "authorizerPayloadFormatVersion", stringValue(authDef.get("authorizerPayloadFormatVersion"))); @@ -665,6 +666,7 @@ public class ApiGatewayV2OpenApiImporter { if (type != null) { request.put("integrationType", type.toUpperCase(Locale.ROOT)); } + putIfPresent(request, "credentialsArn", stringValue(definition.get("credentials"))); putIfPresent(request, "integrationUri", stringValue(definition.get("uri"))); putIfPresent(request, "integrationMethod", stringValue(definition.get("httpMethod"))); putIfPresent(request, "connectionType", upper(stringValue(definition.get("connectionType")))); diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index 100270dc70..aad4866f98 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -143,11 +143,15 @@ public class ApiGatewayV2Service { String target = targetValue != null ? String.valueOf(targetValue) : null; if ("HTTP".equals(protocolType) && target != null && !target.isBlank()) { boolean isLambdaTarget = AwsArnUtils.looksLikeArn(target); - Integration integration = createIntegration(region, apiId, Map.of( + Map integrationRequest = new HashMap<>(Map.of( "integrationType", isLambdaTarget ? "AWS_PROXY" : "HTTP_PROXY", "integrationUri", target, "integrationMethod", isLambdaTarget ? "POST" : "ANY", "payloadFormatVersion", "2.0")); + if (isLambdaTarget) { + integrationRequest.put("credentialsArn", request.get("credentialsArn")); + } + Integration integration = createIntegration(region, apiId, integrationRequest); createRoute(region, apiId, Map.of( "routeKey", "$default", "target", "integrations/" + integration.getIntegrationId())); @@ -381,6 +385,7 @@ public class ApiGatewayV2Service { auth.setJwtConfiguration(new Authorizer.JwtConfiguration(audience, issuer)); } + auth.setAuthorizerCredentialsArn((String) request.get("authorizerCredentialsArn")); auth.setAuthorizerUri((String) request.get("authorizerUri")); auth.setAuthorizerPayloadFormatVersion((String) request.get("authorizerPayloadFormatVersion")); auth.setAuthorizerResultTtlInSeconds(authorizerResultTtl(request.get("authorizerResultTtlInSeconds"))); @@ -418,6 +423,7 @@ public class ApiGatewayV2Service { restored.setName(authorizer.getName()); restored.setJwtConfiguration(authorizer.getJwtConfiguration()); restored.setIdentitySource(authorizer.getIdentitySource()); + restored.setAuthorizerCredentialsArn(authorizer.getAuthorizerCredentialsArn()); restored.setAuthorizerUri(authorizer.getAuthorizerUri()); restored.setAuthorizerPayloadFormatVersion(authorizer.getAuthorizerPayloadFormatVersion()); restored.setAuthorizerResultTtlInSeconds(authorizer.getAuthorizerResultTtlInSeconds()); @@ -434,6 +440,9 @@ public class ApiGatewayV2Service { if (request.containsKey("name") && request.get("name") != null) { auth.setName((String) request.get("name")); } + if (request.get("authorizerCredentialsArn") != null) { + auth.setAuthorizerCredentialsArn((String) request.get("authorizerCredentialsArn")); + } if (request.containsKey("authorizerType") && request.get("authorizerType") != null) { auth.setAuthorizerType((String) request.get("authorizerType")); } @@ -691,6 +700,7 @@ public class ApiGatewayV2Service { Integration integration = new Integration(); integration.setIntegrationId(shortId(8)); integration.setIntegrationType((String) request.get("integrationType")); + integration.setCredentialsArn((String) request.get("credentialsArn")); integration.setIntegrationUri((String) request.get("integrationUri")); integration.setConnectionType((String) request.get("connectionType")); integration.setPayloadFormatVersion((String) request.getOrDefault("payloadFormatVersion", "2.0")); @@ -746,6 +756,9 @@ public class ApiGatewayV2Service { Map request) { Integration integration = getIntegration(region, apiId, integrationId); + if (request.get("credentialsArn") != null) { + integration.setCredentialsArn((String) request.get("credentialsArn")); + } if (request.containsKey("integrationType") && request.get("integrationType") != null) { integration.setIntegrationType((String) request.get("integrationType")); } diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Authorizer.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Authorizer.java index e523e41782..0607fdbd60 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Authorizer.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Authorizer.java @@ -18,8 +18,13 @@ public class Authorizer { private Integer authorizerResultTtlInSeconds; private Boolean enableSimpleResponses; + private String authorizerCredentialsArn; + public Authorizer() {} + public String getAuthorizerCredentialsArn() { return authorizerCredentialsArn; } + public void setAuthorizerCredentialsArn(String authorizerCredentialsArn) { this.authorizerCredentialsArn = authorizerCredentialsArn; } + public String getAuthorizerId() { return authorizerId; } public void setAuthorizerId(String authorizerId) { this.authorizerId = authorizerId; } diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Integration.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Integration.java index 49d09d2964..72f55b01ba 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Integration.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/model/Integration.java @@ -21,10 +21,16 @@ public class Integration { private Map requestParameters; private String connectionId; + private String credentialsArn; + public Integration() {} + public String getCredentialsArn() { return credentialsArn; } + public void setCredentialsArn(String credentialsArn) { this.credentialsArn = credentialsArn; } + /** Shallow copy. Add new fields here so callers like {@code withResolvedUri} keep them. */ public Integration(Integration src) { + this.credentialsArn = src.credentialsArn; this.integrationId = src.integrationId; this.integrationType = src.integrationType; this.connectionType = src.connectionType; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java index 6053fb2399..7b3609cd2e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java @@ -313,6 +313,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner { Map integrationRequest = new HashMap<>(); integrationRequest.put("integrationType", integrationType.toUpperCase(Locale.ROOT)); putOpenApiIntegrationValue(integrationRequest, "integrationUri", integration, "uri"); + putOpenApiIntegrationValue(integrationRequest, "credentialsArn", integration, "credentials"); putOpenApiIntegrationValue(integrationRequest, "integrationMethod", integration, "httpMethod"); putOpenApiIntegrationValue(integrationRequest, "payloadFormatVersion", integration, @@ -378,6 +379,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner { request.put("authorizerType", authorizerType); putOpenApiAuthorizerIdentitySource(request, definition); putOpenApiAuthorizerValue(request, "authorizerUri", definition, "authorizerUri"); + putOpenApiAuthorizerValue(request, "authorizerCredentialsArn", definition, "authorizerCredentials"); putOpenApiAuthorizerValue(request, "authorizerPayloadFormatVersion", definition, "authorizerPayloadFormatVersion"); putOpenApiAuthorizerValue(request, "authorizerResultTtlInSeconds", definition, diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2CredentialsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2CredentialsIntegrationTest.java new file mode 100644 index 0000000000..7647135753 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2CredentialsIntegrationTest.java @@ -0,0 +1,199 @@ +package io.github.hectorvent.floci.services.apigatewayv2; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.apigatewayv2.model.Authorizer; +import io.github.hectorvent.floci.services.apigatewayv2.model.Integration; +import io.github.hectorvent.floci.testing.AwsTestClients; +import io.github.hectorvent.floci.testing.CfnStackWaits; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.http.apache5.Apache5HttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.apigatewayv2.ApiGatewayV2Client; +import software.amazon.awssdk.services.apigatewayv2.model.CreateAuthorizerResponse; +import software.amazon.awssdk.services.apigatewayv2.model.CreateIntegrationResponse; +import software.amazon.awssdk.services.apigatewayv2.model.GetIntegrationResponse; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class ApiGatewayV2CredentialsIntegrationTest { + private static final String ROLE = "arn:aws:iam::000000000000:role/invoke"; + private static final String OTHER = "arn:aws:iam::000000000000:role/updated"; + private static final String FUNCTION = "arn:aws:lambda:us-east-1:000000000000:function:target"; + + @Inject + ApiGatewayV2Service service; + + @Inject + ObjectMapper mapper; + + private ApiGatewayV2Client client() { + return AwsTestClients.create(ApiGatewayV2Client.builder(), "test", Region.US_EAST_1, + Apache5HttpClient.builder()); + } + + @ParameterizedTest + @ValueSource(strings = {"HTTP", "WEBSOCKET"}) + void sdkCredentialsSurviveCreateUpdateListAndOmittedUpdate(String protocol) { + try (ApiGatewayV2Client client = client()) { + String api = client.createApi(b -> b.name("credentials").protocolType(protocol) + .routeSelectionExpression("$request.body.action")).apiId(); + try { + CreateIntegrationResponse integration = client.createIntegration(b -> b.apiId(api) + .integrationType("AWS_PROXY").integrationUri(FUNCTION).integrationMethod("POST") + .payloadFormatVersion("2.0").credentialsArn(ROLE)); + assertEquals(ROLE, integration.credentialsArn()); + String id = integration.integrationId(); + assertEquals(ROLE, client.getIntegration(b -> b.apiId(api).integrationId(id)).credentialsArn()); + assertEquals(ROLE, client.getIntegrations(b -> b.apiId(api)).items().getFirst().credentialsArn()); + assertEquals(OTHER, client.updateIntegration(b -> b.apiId(api).integrationId(id) + .credentialsArn(OTHER)).credentialsArn()); + assertEquals(OTHER, client.updateIntegration(b -> b.apiId(api).integrationId(id) + .integrationMethod("POST")).credentialsArn()); + assertEquals("arn:aws:iam::*:user/*", client.updateIntegration(b -> b.apiId(api) + .integrationId(id).credentialsArn("arn:aws:iam::*:user/*")).credentialsArn()); + CreateAuthorizerResponse authorizer = client.createAuthorizer(b -> b.apiId(api).name("request") + .authorizerType("REQUEST").authorizerUri(FUNCTION).authorizerCredentialsArn(ROLE) + .authorizerPayloadFormatVersion("2.0").authorizerResultTtlInSeconds(0) + .identitySource(List.of("$request.header.Authorization"))); + assertEquals(ROLE, authorizer.authorizerCredentialsArn()); + String auth = authorizer.authorizerId(); + assertEquals(ROLE, client.getAuthorizer(b -> b.apiId(api).authorizerId(auth)) + .authorizerCredentialsArn()); + assertEquals(ROLE, client.getAuthorizers(b -> b.apiId(api)).items().getFirst() + .authorizerCredentialsArn()); + assertEquals(OTHER, client.updateAuthorizer(b -> b.apiId(api).authorizerId(auth) + .authorizerCredentialsArn(OTHER)).authorizerCredentialsArn()); + assertEquals(OTHER, client.updateAuthorizer(b -> b.apiId(api).authorizerId(auth) + .name("renamed")).authorizerCredentialsArn()); + } finally { + client.deleteApi(b -> b.apiId(api)); + } + } + } + + @ParameterizedTest + @ValueSource(strings = {FUNCTION, "https://backend.example/items"}) + void sdkQuickCreateUsesCredentialsOnlyForLambdaTarget(String target) throws Exception { + try (ApiGatewayV2Client client = client()) { + String api = client.createApi(b -> b.name("quick-credentials-sdk").protocolType("HTTP") + .target(target).credentialsArn(ROLE)).apiId(); + try (AutoCloseable cleanup = () -> client.deleteApi(b -> b.apiId(api))) { + String id = client.getIntegrations(b -> b.apiId(api)).items().getFirst().integrationId(); + GetIntegrationResponse integration = client.getIntegration(b -> b.apiId(api).integrationId(id)); + assertEquals(target, integration.integrationUri()); + assertEquals(FUNCTION.equals(target) ? "AWS_PROXY" : "HTTP_PROXY", + integration.integrationTypeAsString()); + assertEquals(FUNCTION.equals(target) ? ROLE : null, integration.credentialsArn()); + } + } + } + + @Test + void quickCreateAndRestoreKeepCredentialsAndOldStoredJsonDefaultsToNull() throws Exception { + String api = service.createApi("us-east-1", Map.of("name", "quick-credentials", "protocolType", "HTTP", + "target", FUNCTION, "credentialsArn", ROLE)).getApiId(); + try { + Integration integration = service.getIntegrations("us-east-1", api).getFirst(); + assertEquals(ROLE, integration.getCredentialsArn()); + Integration copy = new Integration(integration); + service.deleteIntegration("us-east-1", api, integration.getIntegrationId()); + service.restoreIntegration("us-east-1", api, copy); + assertEquals(ROLE, service.getIntegration("us-east-1", api, copy.getIntegrationId()).getCredentialsArn()); + Authorizer auth = service.createAuthorizer("us-east-1", api, Map.of("name", "request", + "authorizerType", "REQUEST", "authorizerCredentialsArn", ROLE)); + service.deleteAuthorizer("us-east-1", api, auth.getAuthorizerId()); + service.restoreAuthorizer("us-east-1", api, auth); + assertEquals(ROLE, service.getAuthorizer("us-east-1", api, auth.getAuthorizerId()) + .getAuthorizerCredentialsArn()); + assertEquals(ROLE, mapper.readValue(mapper.writeValueAsString(copy), Integration.class).getCredentialsArn()); + assertEquals(ROLE, mapper.readValue(mapper.writeValueAsString(auth), Authorizer.class) + .getAuthorizerCredentialsArn()); + assertNull(mapper.readValue("{}", Integration.class).getCredentialsArn()); + assertNull(mapper.readValue("{}", Authorizer.class).getAuthorizerCredentialsArn()); + } finally { + service.deleteApi("us-east-1", api); + } + } + + @Test + void sdkCloudFormationBodyKeepsIntegrationAndAuthorizerCredentials() throws Exception { + String stack = "body-credentials-" + UUID.randomUUID(); + String template = """ + {"Resources":{"HttpApi":{"Type":"AWS::ApiGatewayV2::Api","Properties":{ + "Name":"body-credentials","ProtocolType":"HTTP","Body":{ + "openapi":"3.0.1","info":{"title":"credentials","version":"1"}, + "components":{"securitySchemes":{"RequestAuth":{ + "type":"apiKey","name":"Authorization","in":"header", + "x-amazon-apigateway-authorizer":{"type":"request", + "authorizerUri":"%s","authorizerCredentials":"%s", + "identitySource":"$request.header.Authorization", + "authorizerPayloadFormatVersion":"2.0"}}}}, + "paths":{"/items":{"get":{"security":[{"RequestAuth":[]}], + "x-amazon-apigateway-integration":{"type":"aws_proxy","uri":"%s", + "credentials":"%s","httpMethod":"POST","payloadFormatVersion":"2.0"}}}}}}}}} + """.formatted(FUNCTION, OTHER, FUNCTION, ROLE); + try (CloudFormationClient cfn = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1); + ApiGatewayV2Client client = client()) { + cfn.createStack(b -> b.stackName(stack).templateBody(template)); + try (AutoCloseable cleanup = () -> { + cfn.deleteStack(b -> b.stackName(stack)); + CfnStackWaits.awaitStackDeleted(stack); + }) { + CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE"); + String api = cfn.describeStackResource(b -> b.stackName(stack).logicalResourceId("HttpApi")) + .stackResourceDetail().physicalResourceId(); + assertEquals(ROLE, client.getIntegrations(b -> b.apiId(api)).items().getFirst().credentialsArn()); + assertEquals(OTHER, client.getAuthorizers(b -> b.apiId(api)).items().getFirst() + .authorizerCredentialsArn()); + cfn.updateStack(b -> b.stackName(stack).templateBody(template + .replace(ROLE, ROLE + "-updated").replace(OTHER, OTHER + "-updated"))); + CfnStackWaits.awaitStatus(stack, "UPDATE_COMPLETE"); + assertEquals(ROLE + "-updated", client.getIntegrations(b -> b.apiId(api)) + .items().getFirst().credentialsArn()); + assertEquals(OTHER + "-updated", client.getAuthorizers(b -> b.apiId(api)) + .items().getFirst().authorizerCredentialsArn()); + } + } + } + + @Test + void jsonCompatibilityPathKeepsCredentials() throws Exception { + String api = service.createApi("us-east-1", Map.of("name", "json-credentials", "protocolType", "HTTP")).getApiId(); + try { + JsonNode integration = json("CreateIntegration", Map.of("ApiId", api, "IntegrationType", "AWS_PROXY", + "IntegrationUri", FUNCTION, "CredentialsArn", ROLE), 201); + assertEquals(ROLE, integration.path("CredentialsArn").asText()); + String id = integration.path("IntegrationId").asText(); + assertEquals(OTHER, json("UpdateIntegration", Map.of("ApiId", api, "IntegrationId", id, + "CredentialsArn", OTHER), 200).path("CredentialsArn").asText()); + JsonNode auth = json("CreateAuthorizer", Map.of("ApiId", api, "Name", "request", "AuthorizerType", + "REQUEST", "AuthorizerCredentialsArn", ROLE), 201); + assertEquals(ROLE, auth.path("AuthorizerCredentialsArn").asText()); + assertEquals(OTHER, json("UpdateAuthorizer", Map.of("ApiId", api, "AuthorizerId", + auth.path("AuthorizerId").asText(), "AuthorizerCredentialsArn", OTHER), 200) + .path("AuthorizerCredentialsArn").asText()); + } finally { + service.deleteApi("us-east-1", api); + } + } + + private JsonNode json(String action, Map body, int status) throws Exception { + String response = given().contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "AmazonApiGatewayV2." + action) + .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261026/us-east-1/apigatewayv2/aws4_request") + .body(mapper.writeValueAsBytes(body)).post("/").then().statusCode(status).extract().asString(); + return mapper.readTree(response); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java index d84cbcd972..2c256ca866 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java @@ -40,6 +40,7 @@ class ApiGatewayV2OpenApiImportTest { "x-amazon-apigateway-authorizer": { "type": "request", "authorizerUri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations", + "authorizerCredentials": "arn:aws:iam::000000000000:role/authorizer", "authorizerPayloadFormatVersion": "2.0", "enableSimpleResponses": true, "identitySource": "$request.header.Authorization,$context.routeKey", @@ -53,6 +54,7 @@ class ApiGatewayV2OpenApiImportTest { "get": { "x-amazon-apigateway-integration": { "type": "aws_proxy", + "credentials": "arn:aws:iam::000000000000:role/integration", "httpMethod": "POST", "uri": "arn:aws:lambda:us-east-1:000000000000:function:items", "payloadFormatVersion": "2.0", @@ -65,6 +67,7 @@ class ApiGatewayV2OpenApiImportTest { "security": [], "x-amazon-apigateway-integration": { "type": "aws_proxy", + "credentials": "arn:aws:iam::000000000000:role/integration", "httpMethod": "POST", "uri": "arn:aws:lambda:us-east-1:000000000000:function:public", "payloadFormatVersion": "2.0" @@ -113,6 +116,8 @@ class ApiGatewayV2OpenApiImportTest { JsonNode authorizer = authorizers.get("items").get(0); assertEquals("LambdaAuth", authorizer.get("name").asText()); assertEquals("REQUEST", authorizer.get("authorizerType").asText()); + assertEquals("arn:aws:iam::000000000000:role/authorizer", + authorizer.path("authorizerCredentialsArn").asText()); assertEquals("2.0", authorizer.get("authorizerPayloadFormatVersion").asText()); assertTrue(authorizer.get("enableSimpleResponses").asBoolean()); assertEquals(300, authorizer.get("authorizerResultTtlInSeconds").asInt()); @@ -125,6 +130,8 @@ class ApiGatewayV2OpenApiImportTest { assertEquals(2, integrations.get("items").size()); JsonNode integration = integrations.get("items").get(0); assertEquals("AWS_PROXY", integration.get("integrationType").asText()); + assertEquals("arn:aws:iam::000000000000:role/integration", + integration.path("credentialsArn").asText()); assertEquals("POST", integration.get("integrationMethod").asText()); assertEquals("2.0", integration.get("payloadFormatVersion").asText()); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java index b636015487..3fcae65426 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java @@ -17,6 +17,8 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudForm import io.github.hectorvent.floci.services.s3.S3Service; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InOrder; import java.util.HashMap; @@ -324,6 +326,43 @@ class ApiGatewayV2CfnProvisionerTest { "the orphan the attempt could not remove is owed on the restored resource: " + record); } + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void bodyMapsIntegrationAndRequestAuthorizerCredentials(boolean integrationCredentials) throws Exception { + Authorizer authorizer = new Authorizer(); + authorizer.setAuthorizerId("body-authorizer"); + when(apiGatewayV2Service.createAuthorizer(eq(REGION), eq(API_ID), anyMap())).thenReturn(authorizer); + Integration integration = new Integration(); + integration.setIntegrationId("body-integration"); + when(apiGatewayV2Service.createIntegration(eq(REGION), eq(API_ID), anyMap())).thenReturn(integration); + when(apiGatewayV2Service.createRoute(eq(REGION), eq(API_ID), anyMap())) + .thenReturn(route("body-route", "GET /items")); + + StackResource resource = provision(body(""" + {"components":{"securitySchemes":{"RequestAuth":{ + "type":"apiKey","name":"Authorization","in":"header", + "x-amazon-apigateway-authorizer":{"type":"request", + "authorizerUri":"arn:aws:lambda:us-east-1:000000000000:function:auth", + "authorizerCredentials":"arn:aws:iam::000000000000:role/authorizer", + "identitySource":"$request.header.Authorization", + "authorizerPayloadFormatVersion":"2.0"}}}}, + "paths":{"/items":{"get":{"security":[{"RequestAuth":[]}], + "x-amazon-apigateway-integration":{"type":"aws_proxy", + "uri":"arn:aws:lambda:us-east-1:000000000000:function:target", + "credentials":"arn:aws:iam::000000000000:role/integration", + "httpMethod":"POST","payloadFormatVersion":"2.0"}}}}} + """), null, Map.of()); + + assertEquals("CREATE_COMPLETE", resource.getStatus()); + if (integrationCredentials) { + verify(apiGatewayV2Service).createIntegration(eq(REGION), eq(API_ID), argThat(request -> + "arn:aws:iam::000000000000:role/integration".equals(request.get("credentialsArn")))); + } else { + verify(apiGatewayV2Service).createAuthorizer(eq(REGION), eq(API_ID), argThat(request -> + "arn:aws:iam::000000000000:role/authorizer".equals(request.get("authorizerCredentialsArn")))); + } + } + @Test void materializesInheritedJwtSecurityAndHonorsOperationOptOut() throws Exception { Authorizer authorizer = new Authorizer(); diff --git a/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java b/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java index 570c8df8f6..253497f05c 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java +++ b/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java @@ -5,6 +5,7 @@ import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.awscore.client.builder.AwsSyncClientBuilder; +import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; import software.amazon.awssdk.regions.Region; @@ -18,8 +19,14 @@ public final class AwsTestClients { public static & AwsSyncClientBuilder, C> C create( B builder, String account, Region region) { + return create(builder, account, region, UrlConnectionHttpClient.builder()); + } + + /** Selects a transport explicitly for operations requiring methods such as PATCH. */ + public static & AwsSyncClientBuilder, C> C create( + B builder, String account, Region region, SdkHttpClient.Builder httpClientBuilder) { return builder.endpointOverride(URI.create("http://localhost:" + RestAssured.port)) - .httpClientBuilder(UrlConnectionHttpClient.builder()).region(region) + .httpClientBuilder(httpClientBuilder).region(region) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"))) .build(); }