diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 560a3b4c7a..04d62a7aff 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -1278,8 +1278,8 @@ DELETE /execute-api/{apiId}/{stageName}/@connections/{connectionId} — Disconn The `@connections` management API is likewise reachable on the execute-api host (`http://{apiId}.execute-api.{region}.localhost:4566/{stage}/@connections/{connectionId}`). - **Mapping templates**: in a request template of an `AWS` or `HTTP` integration, `$input` is the message as the client sent it (base64 for a binary frame, empty on `$connect` and `$disconnect`). In a response template it is the integration's reply. In both, `$context` holds the route's request context: `connectionId`, `routeKey`, `eventType`, `apiId`, `stage`, `requestId`, `extendedRequestId`, `connectedAt`, `requestTime`, `requestTimeEpoch`, `domainName`, `identity.sourceIp` and `identity.userAgent`. `$context.messageId` is not set, and `$context.authorizer` is set on `$connect` only. - **Lambda authorizers**: a `$connect` route with `authorizationType` `CUSTOM` and an `authorizerId` naming a `REQUEST` authorizer invokes that authorizer's Lambda function, and the connection proceeds only when the policy it returns allows. With IAM enforcement on, that invoke is checked as an integration is, as the `credentials` row above describes: without `authorizerCredentialsArn` the function policy alone decides, so it has to allow `apigateway.amazonaws.com` to call `lambda:InvokeFunction`; with one the call is made as that role, which has to exist in the API's account, trust `apigateway.amazonaws.com` and be allowed the invocation, whatever the function policy grants. `arn:aws:iam::*:user/*`, the caller's identity, names no role an authorizer can call as, since the caller of `$connect` has none yet, so an authorizer with it is decided by the function policy alone. Either way a statement naming an `aws:SourceArn` is matched against the authorizer's own ARN (`arn:aws:execute-api:::/authorizers/`), which is the ARN the guide has callers grant a Lambda authorizer, and not the `$connect` ARN the event reports as its `methodArn`; the policy the authorizer returns is still evaluated against that `$connect` ARN. `` is the function's Region when the authorizer URI names one, and the API's otherwise. A refused invoke fails the authorizer, which answers `500` and refuses the upgrade, and the function is not invoked. See [Function policies under IAM enforcement](lambda.md#function-policies-under-iam-enforcement). -- **Lambda integrations**: an `AWS_PROXY` or `AWS` route whose URI names a Lambda function invokes it, with `${stageVariables.}` resolved in the URI. With IAM enforcement on, an integration without `credentialsArn` needs the function policy to allow `apigateway.amazonaws.com` to call `lambda:InvokeFunction`, with the route's execute-api ARN (`arn:aws:execute-api::://`, where `` is `$connect`, `$disconnect` or the matched route key) as `aws:SourceArn` when the statement names one. `` is the function's Region when the URI's function ARN names one, and the API's otherwise. One with `credentialsArn` invokes as that role, which must exist in the API's account, trust `apigateway.amazonaws.com` and be allowed `lambda:InvokeFunction` by its own policies together with the function policy; `${stageVariables.}` in the role ARN is resolved from the stage, and the role's policies see that Region as `aws:RequestedRegion`. `arn:aws:iam::*:user/*` is not checked. A refused call fails the route, which answers `Internal server error`, and a refusal on `$connect` refuses the upgrade; either way the function is not invoked. See [Function policies under IAM enforcement](lambda.md#function-policies-under-iam-enforcement). -- **SQS integrations**: an `AWS` integration whose URI is `arn:aws:apigateway::sqs:path//` sends its rendered request template to SQS as a form-encoded query request, such as `Action=SendMessage&MessageBody=$util.urlEncode($input.body)`. The URI's region and account select the queue, unless the template sets `QueueUrl`. The response template's `$input` is the SQS XML reply. A request SQS refuses is logged and refuses the upgrade on `$connect`. With IAM enforcement on, an integration without `credentialsArn` needs the queue policy to allow `apigateway.amazonaws.com`, with the route's execute-api ARN as `aws:SourceArn`. One with `credentialsArn` calls as that role, which must exist in the API's account, trust `apigateway.amazonaws.com` and be allowed the action by its own policies together with the queue policy; `${stageVariables.}` in the role ARN is resolved from the stage, and the role's policies see the queue's Region as `aws:RequestedRegion`. Either way, the queues checked are the ones the same request made directly acts on, such as the queue `QueueName` names for `GetQueueUrl`, and a refused call fails the route with `Internal server error`. `arn:aws:iam::*:user/*` is not checked. `requestParameters` are stored but not applied. Other AWS services fail the route with `Internal server error`. +- **Lambda integrations**: an `AWS_PROXY` or `AWS` route whose URI names a Lambda function invokes it, with `${stageVariables.}` resolved in the URI. With IAM enforcement on, an integration without `credentialsArn` needs the function policy to allow `apigateway.amazonaws.com` to call `lambda:InvokeFunction`, with the route's execute-api ARN (`arn:aws:execute-api::://`, where `` is `$connect`, `$disconnect` or the matched route key) as `aws:SourceArn` when the statement names one. `` is the function's Region when the URI's function ARN names one, and the API's otherwise. One with `credentialsArn` invokes as that role, which must exist in the API's account, trust `apigateway.amazonaws.com` and be allowed `lambda:InvokeFunction` by its own policies together with the function policy; `${stageVariables.}` in the role ARN is resolved from the stage, and the role's policies see that Region as `aws:RequestedRegion`. `arn:aws:iam::*:user/*`, the caller's identity, names no role an integration can call as, since no AWS principal signs a WebSocket frame, so an integration with it is decided by the function policy alone. A refused call fails the route, which answers `Internal server error`, and a refusal on `$connect` refuses the upgrade; either way the function is not invoked. See [Function policies under IAM enforcement](lambda.md#function-policies-under-iam-enforcement). +- **SQS integrations**: an `AWS` integration whose URI is `arn:aws:apigateway::sqs:path//` sends its rendered request template to SQS as a form-encoded query request, such as `Action=SendMessage&MessageBody=$util.urlEncode($input.body)`. The URI's region and account select the queue, unless the template sets `QueueUrl`. The response template's `$input` is the SQS XML reply. A request SQS refuses is logged and refuses the upgrade on `$connect`. With IAM enforcement on, an integration without `credentialsArn` needs the queue policy to allow `apigateway.amazonaws.com`, with the route's execute-api ARN as `aws:SourceArn`. One with `credentialsArn` calls as that role, which must exist in the API's account, trust `apigateway.amazonaws.com` and be allowed the action by its own policies together with the queue policy; `${stageVariables.}` in the role ARN is resolved from the stage, and the role's policies see the queue's Region as `aws:RequestedRegion`. Either way, the queues checked are the ones the same request made directly acts on, such as the queue `QueueName` names for `GetQueueUrl`, and a refused call fails the route with `Internal server error`. `arn:aws:iam::*:user/*`, the caller's identity, names no role an integration can call as, since no AWS principal signs a WebSocket frame, so an integration with it is decided by the queue policy alone. `requestParameters` are stored but not applied. Other AWS services fail the route with `Internal server error`. - **Idle timeout**: 10 minutes (matching AWS default). Not configurable per-API. - **Max connection duration**: 2 hours (matching AWS). Connections are closed automatically. - **Payload size limit**: 128 KB per frame (matching AWS). Oversized messages receive an error frame. diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationAuthorizer.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationAuthorizer.java index fe09623272..6729c2bf6a 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationAuthorizer.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationAuthorizer.java @@ -76,12 +76,14 @@ public class ApiGatewayIntegrationAuthorizer { } /** - * Whether an authorizer may make its call. {@code credentials} is the authorizer's - * {@code authorizerCredentialsArn}, the role API Gateway calls as. The caller-passthrough value names no such - * role: an authorizer runs before the caller has an identity, so it decides nothing here, and an authorizer - * carrying it is left to the target's resource policy as one without credentials is. + * Whether a call made on a request that carries no caller identity may proceed. Neither a {@code $connect} + * authorizer nor a WebSocket route's integration has such an identity: an authorizer runs before the caller is + * known, and a WebSocket frame arrives over a connection no AWS principal signed. The caller-passthrough value + * names no role to call as there, so it decides nothing, and a resource naming it is left to the target's + * resource policy as one with no credentials is. A real role is still decided the way {@link #mayCall} decides + * it, and a REST or HTTP request, which can be SigV4-signed, keeps the passthrough meaning. */ - public boolean mayCallAuthorizer(String credentials, String action, String resourceArn, String region, + public boolean mayCallWithoutCallerIdentity(String credentials, String action, String resourceArn, String region, String sourceArn, String accountId) { return mayCall(passesCallerThrough(credentials) ? null : credentials, action, resourceArn, region, sourceArn, accountId); diff --git a/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketAuthorizerService.java b/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketAuthorizerService.java index 0fe95e587f..4d37c5030b 100644 --- a/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketAuthorizerService.java +++ b/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketAuthorizerService.java @@ -257,7 +257,7 @@ public class WebSocketAuthorizerService { String sourceArn = AwsArnUtils.Arn.of("execute-api", region, route.accountId(), apiId + "/authorizers/" + authorizerId).toString(); String functionArn = LambdaArnUtils.functionArn(functionName, route.partition(), region, route.accountId()); - return integrationAuthorizer.mayCallAuthorizer(credentialsArn, "lambda:InvokeFunction", functionArn, + return integrationAuthorizer.mayCallWithoutCallerIdentity(credentialsArn, "lambda:InvokeFunction", functionArn, region, sourceArn, route.accountId()); } diff --git a/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java b/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java index 79a9ab31c8..2c8dcc0e98 100644 --- a/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java +++ b/src/main/java/io/floci/aws/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java @@ -307,7 +307,9 @@ public class WebSocketIntegrationInvoker { * role, which has to exist in the API's account, trust API Gateway and be allowed the invocation: a role * without the grant is refused whatever the function policy says, and a role in another account is refused. * An integration without it is decided by the function policy alone, whose {@code aws:SourceArn} condition is - * that same ARN. The Region is the function's when its reference names one, since Lambda authorizes the call + * that same ARN. The caller-passthrough {@code credentials} value names no role either, because a WebSocket + * frame arrives over a connection no AWS principal signed, so such an integration is decided by that policy too. + * The Region is the function's when its reference names one, since Lambda authorizes the call * in the Region it is invoked in. A refusal fails the route, which answers {@code Internal server error}. */ private void requireLambdaAccess(Integration integration, String apiRegion, String routeKey, String eventJson, @@ -321,8 +323,8 @@ public class WebSocketIntegrationInvoker { String functionArn = LambdaArnUtils.functionArn(functionName, AwsArnUtils.parse(sourceArn).partition(), region, account); String credentials = substituteStageVariables(integration.getCredentialsArn(), stageVariables); - if (!integrationAuthorizer.mayCall(credentials, "lambda:InvokeFunction", functionArn, region, sourceArn, - account)) { + if (!integrationAuthorizer.mayCallWithoutCallerIdentity(credentials, "lambda:InvokeFunction", functionArn, + region, sourceArn, account)) { throw new AwsException("AccessDeniedException", "API Gateway is not allowed to invoke " + functionArn + " for integration " + integration.getIntegrationId(), 403); } @@ -385,8 +387,9 @@ public class WebSocketIntegrationInvoker { * Whether API Gateway may make this call. The source ARN names the API, the stage and the route. What the * call acts on comes from the request-resource table, as for the same request made directly: each action an * operation's parts take on what they name, or the operation's action on each resource it names, or on - * {@code *} when it names none. An integration with {@code credentialsArn} calls as that role; one without - * is decided by the queue policy alone. A request without an {@code Action} is left for the dispatch to + * {@code *} when it names none. An integration with {@code credentialsArn} calls as that role; one without, and + * one naming the caller-passthrough value, which no signed caller stands behind on a WebSocket route, is + * decided by the queue policy alone. A request without an {@code Action} is left for the dispatch to * refuse. */ private boolean apiGatewayMayCallSqs(Integration integration, MultivaluedMap params, @@ -400,8 +403,8 @@ public class WebSocketIntegrationInvoker { String sourceArn = routeSourceArn(eventJson, apiRegion, routeKey); String credentials = substituteStageVariables(integration.getCredentialsArn(), stageVariables); for (Map.Entry check : sqsChecks(action, params, queueRegion, account)) { - if (!integrationAuthorizer.mayCall(credentials, check.getKey(), check.getValue(), queueRegion, sourceArn, - account)) { + if (!integrationAuthorizer.mayCallWithoutCallerIdentity(credentials, check.getKey(), check.getValue(), + queueRegion, sourceArn, account)) { return false; } } diff --git a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketLambdaPermissionIntegrationTest.java b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketLambdaPermissionIntegrationTest.java index 1a10136551..cafadd4821 100644 --- a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketLambdaPermissionIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketLambdaPermissionIntegrationTest.java @@ -333,8 +333,21 @@ class WebSocketLambdaPermissionIntegrationTest { assertRefused(function); } + /** + * {@code arn:aws:iam::*:user/*} is the caller's identity, and no AWS principal signs a WebSocket frame, so a + * route naming it reaches no function that has not been opened to API Gateway. + */ @Test - void callerCredentialsAreNotChecked() throws Exception { + void callerPassThroughWithoutAFunctionPolicyIsRefused() throws Exception { + String function = createFunction(REGION, "caller-no-grant-" + suffix()); + route("$default", "AWS_PROXY", function, "arn:aws:iam::*:user/*"); + + assertRefused(function); + } + + /** The same route invokes once its function grants API Gateway, as one with no credentials ARN does. */ + @Test + void callerPassThroughIsDecidedByTheFunctionPolicy() throws Exception { String function = createFunction(REGION, "caller-" + suffix()); route("$default", "AWS_PROXY", function, "arn:aws:iam::*:user/*"); addPermission(function, sourceArn(apiId, "dev/$default")); diff --git a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketSqsPermissionIntegrationTest.java b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketSqsPermissionIntegrationTest.java index 067f3b1e3f..72947041fe 100644 --- a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketSqsPermissionIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketSqsPermissionIntegrationTest.java @@ -204,6 +204,29 @@ class WebSocketSqsPermissionIntegrationTest { assertNull(receiveBody()); } + /** + * {@code arn:aws:iam::*:user/*} is the caller's identity, and no AWS principal signs a WebSocket frame, so a + * route naming it sends to no queue that has not been opened to API Gateway. + */ + @Test + void callerPassThroughWithoutAQueuePolicyRefusesTheSend() throws Exception { + route("$default", SEND, "arn:aws:iam::*:user/*"); + + assertInternalServerError(); + assertNull(receiveBody()); + } + + /** The same route sends once the queue grants API Gateway, as one with no credentials ARN does. */ + @Test + void callerPassThroughIsDecidedByTheQueuePolicy() throws Exception { + route("$default", SEND, "arn:aws:iam::*:user/*"); + allow("sqs:SendMessage", sourceArn(apiId, "dev/$default")); + + send(); + + assertEquals("MESSAGE", awaitBody()); + } + @Test void explicitDenyInTheQueuePolicyRefusesAnAllowedRole() throws Exception { String role = createRole("apigateway.amazonaws.com", rolePolicy("sqs:SendMessage", queueArn()));