diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java index ea95d8df3d..d3a9339a74 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java @@ -34,22 +34,18 @@ import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; +import java.util.List; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Generic dispatcher for all AWS services that use the Query Protocol (form-encoded POST, XML response). * Routes requests to the appropriate service handler based on the service name extracted from the - * Authorization header's credential scope, falling back to action-name matching when no auth header - * is present. - * - *

Currently supported services: - *

+ * Authorization header's credential scope. When there is no such header, or its scope names no + * service that speaks the Query protocol, the action name and the {@code Version} the request + * declares decide instead, as {@link #inferServiceFromAction} describes. * * @see AwsJsonController */ @@ -162,7 +158,7 @@ public class AwsQueryController { "PutScalingPolicy", "DeletePolicy", "DescribePolicies", "DescribeScalingActivities", "DescribeAutoScalingNotificationTypes", "DescribeTerminationPolicyTypes", - "DescribeAdjustmentTypes", "DescribeAccountLimits", + "DescribeAdjustmentTypes", "DescribeAccountLimits", "DescribeLoadBalancers", "DescribeLifecycleHookTypes", "DescribeMetricCollectionTypes", "EnableMetricsCollection", "DisableMetricsCollection" ); @@ -311,7 +307,7 @@ public class AwsQueryController { "The request must contain the parameter Action", 400); } - String service = serviceResolver.resolve(authorization, action); + String service = serviceResolver.resolve(authorization, action, formParams.getFirst("Version")); LOG.debugv("Query protocol service={0} action={1}", service, action); String region = regionResolver.resolveRegion(httpHeaders); @@ -530,17 +526,13 @@ public class AwsQueryController { return Response.status(400).entity(xml).type(MediaType.APPLICATION_XML).build(); } - /** - * Determines the target AWS service. Prefers the service name embedded in the - * Authorization header credential scope. Falls back to action-name lookup when - * the header is absent (e.g. raw HTTP testing without AWS SDK auth). - */ private static final Set ELASTICACHE_ACTIONS = Set.of( "ValidateIamAuthToken", "CreateReplicationGroup", "DescribeReplicationGroups", "ModifyReplicationGroup", "DeleteReplicationGroup", "CreateUser", "DescribeUsers", "ModifyUser", "DeleteUser", "CreateUserGroup", "DescribeUserGroups", "ModifyUserGroup", "DeleteUserGroup", - "CreateServerlessCache", "DescribeServerlessCaches", "ModifyServerlessCache", "DeleteServerlessCache" + "CreateServerlessCache", "DescribeServerlessCaches", "ModifyServerlessCache", "DeleteServerlessCache", + "ListTagsForResource" ); private static final Set CLOUDWATCH_ACTIONS = Set.of( @@ -654,53 +646,87 @@ public class AwsQueryController { "GetClusterCredentials", "GetClusterCredentialsWithIAM" ); - static String inferServiceFromAction(String action) { - if (STS_ACTIONS.contains(action)) { - return "sts"; + /** + * Tried last, so by name alone SQS takes only what no other service defines, as it takes a name + * nobody defines. Its {@code Version} is what wins back the two names SNS also defines. + */ + private static final Set SQS_ACTIONS = Set.of( + "CreateQueue", "DeleteQueue", "ListQueues", "GetQueueUrl", + "GetQueueAttributes", "SetQueueAttributes", "PurgeQueue", + "SendMessage", "SendMessageBatch", "ReceiveMessage", "DeleteMessage", "DeleteMessageBatch", + "ChangeMessageVisibility", "ChangeMessageVisibilityBatch", + "TagQueue", "UntagQueue", "ListQueueTags", "ListDeadLetterSourceQueues", + "StartMessageMoveTask", "ListMessageMoveTasks", "CancelMessageMoveTask", + "AddPermission", "RemovePermission" + ); + + /** ELBv2's names and Classic's own. A name both APIs define is split by {@link #isElbClassicRequest}. */ + private static final Set ELB_ACTIONS = Stream.concat( + ELB_V2_ACTIONS.stream(), ElbClassicQueryHandler.CLASSIC_ONLY_ACTIONS.stream()) + .collect(Collectors.toUnmodifiableSet()); + + /** A Query service, the API versions its clients declare, and the action names it serves. */ + private record QueryService(String name, Set apiVersions, Set actions) { + } + + /** + * Every Query service, in the order they are tried when the action name alone has to decide: + * where two services define the same name, the earlier one takes it. + * + *

An API version is the {@code apiVersion} of the service's model. SES and Elastic Beanstalk + * publish the same one and share no action name, so the action settles which of the two a + * request is for. Both Elastic Load Balancing APIs lead to one service here and + * {@link #isElbClassicRequest} splits them on the same field, and DocumentDB and Neptune publish + * RDS's version and are split from it in the {@code rds} case. Cognito's model is JSON only, so + * no Query client declares a version for it. + */ + private static final List QUERY_SERVICES = List.of( + new QueryService("sts", Set.of("2011-06-15"), STS_ACTIONS), + new QueryService("iam", Set.of("2010-05-08"), IAM_ACTIONS), + new QueryService("sns", Set.of("2010-03-31"), SNS_ACTIONS), + new QueryService("elasticache", Set.of("2015-02-02"), ELASTICACHE_ACTIONS), + new QueryService("rds", Set.of("2014-10-31"), RDS_ACTIONS), + new QueryService("monitoring", Set.of("2010-08-01"), CLOUDWATCH_ACTIONS), + new QueryService("cloudformation", Set.of("2010-05-15"), CLOUDFORMATION_ACTIONS), + new QueryService("email", Set.of("2010-12-01"), SES_ACTIONS), + new QueryService("cognito-idp", Set.of(), COGNITO_ACTIONS), + new QueryService("ec2", Set.of("2016-11-15"), EC2_ACTIONS), + new QueryService("elasticloadbalancing", + Set.of(ELB_V2_API_VERSION, ELB_CLASSIC_API_VERSION), ELB_ACTIONS), + new QueryService("autoscaling", Set.of("2011-01-01"), AUTOSCALING_ACTIONS), + new QueryService("elasticbeanstalk", Set.of("2010-12-01"), ELASTIC_BEANSTALK_ACTIONS), + new QueryService("redshift", Set.of("2012-12-01"), REDSHIFT_ACTIONS), + new QueryService("sqs", Set.of("2012-11-05"), SQS_ACTIONS)); + + /** + * The service a Query request goes to when its credential scope does not name one. + * + *

The action name alone is ambiguous wherever two services define it, and then the earlier + * one in {@link #QUERY_SERVICES} wins. The {@code Version} every Query client sends is what + * tells them apart, as {@link #isElbClassicRequest} already uses it for the two Elastic Load + * Balancing APIs: {@code CreateUser} under IAM's version is IAM's, and under ElastiCache's it + * is ElastiCache's. + * + *

{@code Version} only chooses among the services that serve the name. One that names a + * service without it, or none at all, leaves the order to decide, so a hand-rolled request that + * omits the field goes exactly where it went before. + */ + static String inferServiceFromAction(String action, String version) { + String byName = null; + for (QueryService service : QUERY_SERVICES) { + if (!service.actions().contains(action)) { + continue; + } + if (version != null && service.apiVersions().contains(version)) { + return service.name(); + } + if (byName == null) { + byName = service.name(); + } } - if (IAM_ACTIONS.contains(action)) { - return "iam"; - } - if (SNS_ACTIONS.contains(action)) { - return "sns"; - } - if (ELASTICACHE_ACTIONS.contains(action)) { - return "elasticache"; - } - if (RDS_ACTIONS.contains(action)) { - return "rds"; - } - if (CLOUDWATCH_ACTIONS.contains(action)) { - return "monitoring"; - } - if (CLOUDFORMATION_ACTIONS.contains(action)) { - return "cloudformation"; - } - if (SES_ACTIONS.contains(action)) { - return "email"; - } - if (COGNITO_ACTIONS.contains(action)) { - return "cognito-idp"; - } - if (EC2_ACTIONS.contains(action)) { - return "ec2"; - } - if (ELB_V2_ACTIONS.contains(action) - || ElbClassicQueryHandler.CLASSIC_ONLY_ACTIONS.contains(action)) { - return "elasticloadbalancing"; - } - if (AUTOSCALING_ACTIONS.contains(action)) { - return "autoscaling"; - } - if (ELASTIC_BEANSTALK_ACTIONS.contains(action)) { - return "elasticbeanstalk"; - } - if (REDSHIFT_ACTIONS.contains(action)) { - return "redshift"; - } - // SQS actions are numerous and not enumerated — fall back to sqs only for - // requests that arrived without an Authorization header (raw/test clients) - return "sqs"; + // A name no service defines goes to SQS, which is how a raw SQS request without an + // Authorization header gets there. + return byName != null ? byName : "sqs"; } private Response xmlErrorResponse(String code, String message, int status) { diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolver.java index 69c58d81a1..9431585f7e 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolver.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolver.java @@ -19,14 +19,19 @@ public class AwsQueryServiceResolver { this.catalog = catalog; } - public String resolve(String authorization, String action) { + /** + * The service named by a Query-capable signing scope, else the one the action and the request's + * declared {@code Version} lead to. Both callers read {@code Version} from the form body, as they + * do the action, so they cannot disagree about which service a request is for. + */ + public String resolve(String authorization, String action, String version) { ServiceDescriptor descriptor = SigV4CredentialScope.serviceName(authorization) .flatMap(catalog::byCredentialScope) .orElse(null); if (descriptor != null && descriptor.supportsProtocol(ServiceProtocol.QUERY)) { return descriptor.externalKey(); } - return AwsQueryController.inferServiceFromAction(action); + return AwsQueryController.inferServiceFromAction(action, version); } /** Returns the operation the Query controller will dispatch from a form request. */ diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index 94f0c799c1..3236f70381 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java @@ -533,7 +533,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter { if (queryAction == null || queryAction.isBlank()) { return new ResolvedAuthorization(claimedScope, null); } - String service = queryServiceResolver.resolve(auth, queryAction); + String service = queryServiceResolver.resolve(auth, queryAction, actionRegistry.queryVersion(ctx)); String servingScope = catalog.byExternalKey(service) .map(descriptor -> iamServiceScope(descriptor, claimedScope)) .orElseGet(() -> catalog.canonicalCredentialScope(service)); diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java index d84f082f2c..1372f3821a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java @@ -328,6 +328,14 @@ public class IamActionRegistry { RequestBodyReader.formField(ctx, "Operation")); } + /** + * Returns the API version a Query request declares, from the form body for the same reason as + * {@link #queryAction}: it is the value the controller routes on. + */ + public String queryVersion(ContainerRequestContext ctx) { + return RequestBodyReader.formField(ctx, "Version"); + } + /** The same form POST at the root that the Query controller dispatches. */ public static boolean isQueryRequest(ContainerRequestContext ctx) { if (!"POST".equalsIgnoreCase(ctx.getMethod()) || ctx.getUriInfo() == null) { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java index 43317726e0..aac60c8cb0 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java @@ -6,8 +6,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.regex.Matcher; @@ -23,15 +25,22 @@ import static org.junit.jupiter.api.Assertions.assertTrue; * services, which is what this test makes visible and stops growing. * *

Routing only decides anything when a request arrives without a usable credential scope, - * because otherwise the service comes from the SigV4 scope. On that path the action name is all - * there is, and {@code inferServiceFromAction}'s chain ends in {@code return "sqs"}, so a missing - * name answers {@code UnsupportedOperation} in the SQS namespace rather than failing visibly. - * {@code AwsQueryServiceResolver} backs {@code IamEnforcementFilter} through the same code, so the - * same operation is also authorized under the wrong namespace, which is the part worth a test. + * because otherwise the service comes from the SigV4 scope. On that path there is the action name + * and the {@code Version} the request declares, and {@code inferServiceFromAction} falls back to + * SQS, so a missing name answers {@code UnsupportedOperation} in the SQS namespace rather than + * failing visibly. {@code AwsQueryServiceResolver} backs {@code IamEnforcementFilter} + * through the same code, so the same operation is also authorized under the wrong namespace, which + * is the part worth a test. + * + *

Each action is routed with its own service's {@code Version}, as any Query client sends it, + * and again without one, as a hand-rolled request may leave it out. Without it a name two services + * share goes to whichever is tried first, so that path is only held to reaching a service that + * dispatches the name. * *

The known gaps live in {@code src/test/resources/query-routing-known-gaps.tsv} rather than * here, so that fixing one is a line removed from a data file. The list is checked both ways: a new - * gap fails {@link #everyDispatchedActionRoutesToItsOwnService}, and a gap that has been fixed + * gap fails {@link #everyDispatchedActionRoutesToItsOwnService} or + * {@link #withoutAVersionEveryActionReachesAServiceThatDispatchesIt}, and a gap that has been fixed * without delisting fails {@link #theKnownGapListHasNoStaleEntries}. */ class AwsQueryActionRoutingParityTest { @@ -76,7 +85,7 @@ class AwsQueryActionRoutingParityTest { * their sets are checked as one and why {@code isElbClassicRequest} exists to split them on * {@code Version} afterwards. * - *

SQS is listed even though it is the fallback the chain ends in, because being the default + *

SQS is listed even though it is the fallback routing ends in, because being the default * does not make it safe: a name an earlier service claims never reaches it. */ private static final List HANDLERS = List.of( @@ -106,6 +115,28 @@ class AwsQueryActionRoutingParityTest { new QueryHandler("redshift", HANDLERS_ROOT + "redshift/RedshiftQueryHandler.java"), new QueryHandler("sqs", HANDLERS_ROOT + "sqs/SqsQueryHandler.java")); + /** + * The {@code Version} each service's clients send, which is the {@code apiVersion} in its + * botocore model. Stated here rather than read back from the controller, so a wrong version + * there fails this test instead of agreeing with it. ELBv2's stands for both load balancing + * APIs, since they route to one service by either version and are only split afterwards. + */ + private static final Map API_VERSIONS = Map.ofEntries( + Map.entry("sts", "2011-06-15"), + Map.entry("iam", "2010-05-08"), + Map.entry("sns", "2010-03-31"), + Map.entry("sqs", "2012-11-05"), + Map.entry("elasticache", "2015-02-02"), + Map.entry("rds", "2014-10-31"), + Map.entry("monitoring", "2010-08-01"), + Map.entry("cloudformation", "2010-05-15"), + Map.entry("email", "2010-12-01"), + Map.entry("ec2", "2016-11-15"), + Map.entry("elasticloadbalancing", "2015-12-01"), + Map.entry("autoscaling", "2011-01-01"), + Map.entry("elasticbeanstalk", "2010-12-01"), + Map.entry("redshift", "2012-12-01")); + private static final Path KNOWN_GAPS = Path.of("src/test/resources/query-routing-known-gaps.tsv"); /** @@ -130,11 +161,11 @@ class AwsQueryActionRoutingParityTest { for (QueryHandler handler : HANDLERS) { for (String action : dispatchedActions(handler)) { - String reached = AwsQueryController.inferServiceFromAction(action); + String reached = reached(handler, action); if (!handler.service().equals(reached) && !known.contains(gapKey(handler.service(), action, reached))) { unexpected.add(handler.service() + " dispatches " + action - + " but the name reaches " + reached); + + " but a request for it reaches " + reached); } } } @@ -142,11 +173,40 @@ class AwsQueryActionRoutingParityTest { assertTrue(unexpected.isEmpty(), "An action a handler dispatches has to route to that handler, or a request without " + "a credential scope answers in the wrong namespace and is authorized " - + "under it too. Add the name to its service's set in AwsQueryController, " - + "having checked that no service earlier in inferServiceFromAction's " - + "chain already claims it. If an earlier service does claim it, the name " - + "needs the Version discriminator instead, and belongs in " + KNOWN_GAPS - + " with that noted. New: " + unexpected); + + "under it too. Add the name to its service's set in AwsQueryController; " + + "a name another service also defines is then told apart by the request's " + + "Version. New: " + unexpected); + } + + /** + * Without a {@code Version} the name decides alone, and a name two services define goes to the + * one tried first, which is right for either of them. What is wrong is reaching a service that + * does not dispatch the name at all, and the check above cannot see it, because a + * {@code Version} goes straight to the service it names: a set claiming a name its handler + * lacks, tried ahead of the service that has it, takes every such request. + */ + @Test + void withoutAVersionEveryActionReachesAServiceThatDispatchesIt() throws IOException { + Set known = knownGaps(); + Map> dispatched = dispatchedByService(); + List unexpected = new ArrayList<>(); + + for (QueryHandler handler : HANDLERS) { + for (String action : dispatchedActions(handler)) { + String reached = reachedWithoutVersion(action); + if (!dispatched.getOrDefault(reached, Set.of()).contains(action) + && !known.contains(gapKey(handler.service(), action, reached))) { + unexpected.add(handler.service() + " dispatches " + action + + " but without a Version a request for it reaches " + reached); + } + } + } + + assertTrue(unexpected.isEmpty(), + "Without a Version an action has to reach a service that dispatches it, or the " + + "request answers UnsupportedOperation in a namespace it was never for. " + + "A set in AwsQueryController claims a name its handler does not " + + "dispatch, ahead of the service that does: " + unexpected); } /** @@ -156,13 +216,18 @@ class AwsQueryActionRoutingParityTest { */ @Test void theKnownGapListHasNoStaleEntries() throws IOException { + Map> dispatched = dispatchedByService(); Set live = new LinkedHashSet<>(); for (QueryHandler handler : HANDLERS) { for (String action : dispatchedActions(handler)) { - String reached = AwsQueryController.inferServiceFromAction(action); + String reached = reached(handler, action); if (!handler.service().equals(reached)) { live.add(gapKey(handler.service(), action, reached)); } + String reachedWithoutVersion = reachedWithoutVersion(action); + if (!dispatched.getOrDefault(reachedWithoutVersion, Set.of()).contains(action)) { + live.add(gapKey(handler.service(), action, reachedWithoutVersion)); + } } } @@ -270,6 +335,34 @@ class AwsQueryActionRoutingParityTest { + "that counted it, including the total. " + wrong); } + /** + * Where an unsigned request for the action goes, carrying the handler's own {@code Version} the + * way every Query client sends it. + */ + private static String reached(QueryHandler handler, String action) { + String version = API_VERSIONS.get(handler.service()); + assertTrue(version != null, "No API version listed for " + handler.service()); + return AwsQueryController.inferServiceFromAction(action, version); + } + + /** Where an unsigned request for the action goes when it carries no {@code Version} at all. */ + private static String reachedWithoutVersion(String action) { + return AwsQueryController.inferServiceFromAction(action, null); + } + + /** + * Every name each service dispatches, across all of its handlers. A service with no Query + * handler, as Cognito has none, dispatches nothing. + */ + private static Map> dispatchedByService() throws IOException { + Map> dispatched = new HashMap<>(); + for (QueryHandler handler : HANDLERS) { + dispatched.computeIfAbsent(handler.service(), service -> new TreeSet<>()) + .addAll(dispatchedActions(handler)); + } + return dispatched; + } + /** One line of the known-gap file: the service, the action, and what it reaches instead. */ private static String gapKey(String service, String action, String reached) { return service + "\t" + action + "\t" + reached; diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryControllerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryControllerIntegrationTest.java index 958cd309fd..9c3f261876 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryControllerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryControllerIntegrationTest.java @@ -1,10 +1,15 @@ package io.github.hectorvent.floci.core.common; +import io.github.hectorvent.floci.testing.PartitionCleanup; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import java.util.UUID; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -14,6 +19,9 @@ import static org.hamcrest.Matchers.not; @QuarkusTest class AwsQueryControllerIntegrationTest { + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + @Test void missingActionParameterReturns400MissingAction() { given() @@ -76,4 +84,94 @@ class AwsQueryControllerIntegrationTest { .contentType("application/xml") .body("DescribeRegionsResponse.regionInfo.item.size()", greaterThan(0)); } + + @Test + void sharedActionNameReachesTheServiceItsVersionDeclares() { + // IAM and ElastiCache both define CreateUser and IAM takes it by name. The Version an + // ElastiCache client sends is what brings the request to ElastiCache instead. + String userId = "version-routed-" + UUID.randomUUID().toString().substring(0, 8); + // Registered first, so the user goes even when an assertion below fails. It asserts + // nothing, so it stays quiet once the test has deleted the user itself. + cleanup.register(() -> deleteElastiCacheUser(userId)); + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateUser") + .formParam("Version", "2015-02-02") + .formParam("UserId", userId) + .formParam("UserName", userId) + .formParam("Engine", "redis") + .formParam("AccessString", "on ~* +@all") + .formParam("AuthenticationMode.Type", "no-password-required") + .when() + .post("/") + .then() + .statusCode(200) + .body("CreateUserResponse.CreateUserResult.UserId", equalTo(userId)); + + // DeleteUser is shared the same way. + deleteElastiCacheUser(userId) + .statusCode(200) + .body("DeleteUserResponse.DeleteUserResult.UserId", equalTo(userId)); + } + + @Test + void sqsPermissionActionReachesSqsUnderItsVersion() { + // SNS also defines AddPermission and takes it by name, so it is SQS's Version that brings + // this request to the queue rather than to SNS. + String queueUrl = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("Version", "2012-11-05") + .formParam("QueueName", "version-routed-" + UUID.randomUUID().toString().substring(0, 8)) + .when() + .post("/") + .then() + .statusCode(200) + .extract().path("CreateQueueResponse.CreateQueueResult.QueueUrl"); + cleanup.register(() -> given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("Version", "2012-11-05") + .formParam("QueueUrl", queueUrl) + .when() + .post("/") + .then() + .statusCode(200)); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "AddPermission") + .formParam("Version", "2012-11-05") + .formParam("QueueUrl", queueUrl) + .formParam("Label", "granted-under-sqs-version") + .formParam("AWSAccountId.1", "111122223333") + .formParam("ActionName.1", "SendMessage") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "GetQueueAttributes") + .formParam("Version", "2012-11-05") + .formParam("QueueUrl", queueUrl) + .formParam("AttributeName.1", "Policy") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("granted-under-sqs-version")); + } + + private static ValidatableResponse deleteElastiCacheUser(String userId) { + return given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteUser") + .formParam("Version", "2015-02-02") + .formParam("UserId", userId) + .when() + .post("/") + .then(); + } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java index dc7aa1a7d5..19a770715f 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java @@ -2,6 +2,8 @@ package io.github.hectorvent.floci.core.common; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.util.Optional; import java.util.Set; @@ -26,7 +28,7 @@ class AwsQueryServiceResolverTest { when(catalog.byCredentialScope("lambda")).thenReturn(Optional.of( descriptor("lambda", ServiceProtocol.REST_JSON))); - assertEquals("iam", resolver.resolve(authorization("lambda"), "CreateUser")); + assertEquals("iam", resolver.resolve(authorization("lambda"), "CreateUser", null)); } @Test @@ -34,7 +36,7 @@ class AwsQueryServiceResolverTest { when(catalog.byCredentialScope("sqs")).thenReturn(Optional.of( descriptor("sqs", ServiceProtocol.QUERY))); - assertEquals("sqs", resolver.resolve(authorization("sqs"), "CreateUser")); + assertEquals("sqs", resolver.resolve(authorization("sqs"), "CreateUser", null)); } @Test @@ -42,12 +44,64 @@ class AwsQueryServiceResolverTest { when(catalog.byCredentialScope("iam")).thenReturn(Optional.of( descriptor("iam", ServiceProtocol.QUERY))); - assertEquals("iam", resolver.resolve(authorization("iam"), "CreateUser")); + assertEquals("iam", resolver.resolve(authorization("iam"), "CreateUser", null)); } @Test void missingAuthorizationKeepsActionFallback() { - assertEquals("sqs", resolver.resolve(null, "UnknownQueryAction")); + assertEquals("sqs", resolver.resolve(null, "UnknownQueryAction", null)); + } + + /** + * A name two services define goes to the one whose {@code Version} the request declares. With + * no version, or one that does not settle it, the routing order decides as it always has. + */ + @ParameterizedTest + @CsvSource(nullValues = "none", value = { + "CreateUser, 2015-02-02, elasticache", + "CreateUser, 2010-05-08, iam", + "CreateUser, none, iam", + "AddPermission, 2012-11-05, sqs", + "AddPermission, 2010-03-31, sns", + "AddPermission, none, sns", + "ListTagsForResource, 2014-10-31, rds", + "ListTagsForResource, none, sns", + "DeleteTags, 2012-12-01, redshift", + "DeleteTags, 2011-01-01, autoscaling", + "DeleteTags, none, ec2", + "DescribeTags, 2012-06-01, elasticloadbalancing", + "GetTemplate, 2010-12-01, email", + "GetTemplate, none, cloudformation", + "DescribeEnvironments, 2010-12-01, elasticbeanstalk", + }) + void versionPicksAmongTheServicesThatShareAName(String action, String version, String expected) { + assertEquals(expected, resolver.resolve(null, action, version)); + } + + @Test + void versionOfAServiceWithoutTheActionLeavesTheOrderToDecide() { + assertEquals("sns", resolver.resolve(null, "Publish", "2010-05-08")); + } + + @Test + void unrecognisedVersionLeavesTheOrderToDecide() { + assertEquals("iam", resolver.resolve(null, "CreateUser", "2099-01-01")); + } + + @Test + void queryCapableSignedScopeOutranksVersion() { + when(catalog.byCredentialScope("iam")).thenReturn(Optional.of( + descriptor("iam", ServiceProtocol.QUERY))); + + assertEquals("iam", resolver.resolve(authorization("iam"), "CreateUser", "2015-02-02")); + } + + @Test + void unsupportedSignedScopeFallsBackToVersion() { + when(catalog.byCredentialScope("lambda")).thenReturn(Optional.of( + descriptor("lambda", ServiceProtocol.REST_JSON))); + + assertEquals("elasticache", resolver.resolve(authorization("lambda"), "CreateUser", "2015-02-02")); } private static String authorization(String scope) { diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamServingServiceEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamServingServiceEnforcementIntegrationTest.java index 6f2d054573..810cbb37a4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamServingServiceEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamServingServiceEnforcementIntegrationTest.java @@ -18,6 +18,7 @@ import java.util.UUID; import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; @QuarkusTest @@ -92,6 +93,40 @@ class IamServingServiceEnforcementIntegrationTest { adminIam("GetUser", Map.of("UserName", attempted)).statusCode(404); } + @Test + void queryVersionMovesAuthorizationWithTheServiceItSelects() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String attempted = "version-denied-" + suffix; + String accessKeyId = createUserWithPolicy("version-iam-only-" + suffix, "IamCreateUserOnly", """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Action":"iam:CreateUser","Resource":"*"} + ]}"""); + + elastiCacheCreateUser(accessKeyId, attempted) + .statusCode(403) + .body(containsString("elasticache:CreateUser")); + + adminIam("GetUser", Map.of("UserName", attempted)).statusCode(404); + adminElastiCache("DescribeUsers", Map.of("UserId", attempted)).statusCode(404); + } + + @Test + void queryVersionReachesTheServiceItsPolicyAllows() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String attempted = "version-allowed-" + suffix; + String accessKeyId = createUserWithPolicy("version-cache-only-" + suffix, "CacheCreateUserOnly", """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Action":"elasticache:CreateUser","Resource":"*"} + ]}"""); + + cleanup.register(() -> adminElastiCache("DeleteUser", Map.of("UserId", attempted)).statusCode(200)); + elastiCacheCreateUser(accessKeyId, attempted) + .statusCode(200) + .body("CreateUserResponse.CreateUserResult.UserId", equalTo(attempted)); + + adminIam("GetUser", Map.of("UserName", attempted)).statusCode(404); + } + @Test void restRequestCannotBypassApiGatewayPolicyWithIamScope() { String suffix = UUID.randomUUID().toString().substring(0, 8); @@ -175,6 +210,34 @@ class IamServingServiceEnforcementIntegrationTest { return iamCall(accessKeyId, scope, "Action", action, params); } + /** + * An ElastiCache {@code CreateUser}, a name IAM also defines and takes by name. Signed under a + * scope that is not a Query service, so the scope cannot decide and the {@code Version} has to. + */ + private static ValidatableResponse elastiCacheCreateUser(String accessKeyId, String userId) { + return given() + .header("Authorization", auth(accessKeyId, "lambda")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateUser") + .formParam("Version", "2015-02-02") + .formParam("UserId", userId) + .formParam("UserName", userId) + .formParam("Engine", "redis") + .formParam("AccessString", "on ~* +@all") + .formParam("AuthenticationMode.Type", "no-password-required") + .when().post("/").then(); + } + + private static ValidatableResponse adminElastiCache(String action, Map params) { + RequestSpecification spec = given() + .header("Authorization", auth(ACCOUNT_ID, "elasticache")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", action) + .formParam("Version", "2015-02-02"); + params.forEach(spec::formParam); + return spec.when().post("/").then(); + } + private static ValidatableResponse iamOperationCall(String accessKeyId, String scope, String operation, Map params) { return iamCall(accessKeyId, scope, "Operation", operation, params); diff --git a/src/test/resources/query-routing-known-gaps.tsv b/src/test/resources/query-routing-known-gaps.tsv index dff27f0df2..e5e9a0b385 100644 --- a/src/test/resources/query-routing-known-gaps.tsv +++ b/src/test/resources/query-routing-known-gaps.tsv @@ -3,35 +3,32 @@ # Columns: service, action, service it reaches instead. # # Routing only matters when a request arrives without a usable credential scope, since -# otherwise the service comes from the SigV4 scope. On that path the action name is all -# there is, and AwsQueryServiceResolver backs IamEnforcementFilter too, so an entry here -# is also authorized under the wrong namespace. +# otherwise the service comes from the SigV4 scope. On that path there is the action name +# and the Version the request declares, and AwsQueryServiceResolver backs +# IamEnforcementFilter too, so an entry here is also authorized under the wrong namespace. # -# Two kinds of entry, told apart by the third column: +# Nearly every entry is a name missing from its own service's routing set, and adding it +# there is the fix, as #5032 did for IAM. The third column only says where the request +# goes meanwhile: sqs when no set holds the name, since that is the default, or the +# service whose set does. A name two services define needs nothing more than being in +# both sets, because the Version a request carries decides between them. # -# -> sqs The name is in no routing set, so it falls through to the sqs default. -# Fixable by adding the name to its service's set, as #5032 did for IAM, -# once checked that no service earlier in the chain claims it. +# The exception is a request without a Version, which only a hand-rolled client sends. +# A name two services define then goes to the one tried first, which is a gap only when +# that one does not handle it. SNS defines AddPermission and RemovePermission, but its +# handler does not implement them, so SQS's reach SNS that way. Implementing them in SNS +# is the fix. # -# -> other A service earlier in inferServiceFromAction's chain already owns the -# name, so adding it to the right set changes nothing. These need the -# Version form field to disambiguate, the way isElbClassicRequest already -# reads it, which is a cross-service routing change of its own. -# -# 189 entries: 171 unrouted, 18 claimed by another service. +# 173 entries: 171 unrouted, 2 claimed by another service. # # Shrink this file, never grow it. Removing a line is the point; adding one needs a # reason in the PR that adds it. AwsQueryActionRoutingParityTest fails on an entry that # is no longer a gap, so a fix cannot leave the list stale. -# autoscaling: 17 (13 unrouted, 4 claimed elsewhere) +# autoscaling: 13 (13 unrouted, 0 claimed elsewhere) autoscaling AttachTrafficSources sqs -autoscaling DeletePolicy iam autoscaling DeleteScheduledAction sqs -autoscaling DeleteTags ec2 autoscaling DeleteWarmPool sqs -autoscaling DescribeAccountLimits elasticloadbalancing -autoscaling DescribeLoadBalancers elasticloadbalancing autoscaling DescribeScheduledActions sqs autoscaling DescribeTrafficSources sqs autoscaling DescribeWarmPool sqs @@ -133,39 +130,29 @@ ec2 ResetEbsDefaultKmsKeyId sqs ec2 SearchTransitGatewayRoutes sqs ec2 UnmonitorInstances sqs -# elasticache: 15 (12 unrouted, 3 claimed elsewhere) +# elasticache: 12 (12 unrouted, 0 claimed elsewhere) elasticache CreateCacheCluster sqs elasticache CreateCacheParameterGroup sqs elasticache CreateCacheSubnetGroup sqs -elasticache CreateUser iam elasticache DeleteCacheCluster sqs elasticache DeleteCacheParameterGroup sqs elasticache DeleteCacheSubnetGroup sqs -elasticache DeleteUser iam elasticache DescribeCacheClusters sqs elasticache DescribeCacheParameterGroups sqs elasticache DescribeCacheParameters sqs elasticache DescribeCacheSubnetGroups sqs -elasticache ListTagsForResource sns elasticache ModifyCacheParameterGroup sqs elasticache ModifyCacheSubnetGroup sqs -# elasticloadbalancing: 4 (3 unrouted, 1 claimed elsewhere) +# elasticloadbalancing: 3 (3 unrouted, 0 claimed elsewhere) elasticloadbalancing DescribeCapacityReservation sqs elasticloadbalancing DescribeListenerAttributes sqs -elasticloadbalancing DescribeTags ec2 elasticloadbalancing ModifyListenerAttributes sqs -# email: 2 (1 unrouted, 1 claimed elsewhere) -email GetTemplate cloudformation +# email: 1 (1 unrouted, 0 claimed elsewhere) email UpdateAccountSendingEnabled sqs -# monitoring: 3 (0 unrouted, 3 claimed elsewhere) -monitoring ListTagsForResource sns -monitoring TagResource sns -monitoring UntagResource sns - -# rds: 48 (47 unrouted, 1 claimed elsewhere) +# rds: 47 (47 unrouted, 0 claimed elsewhere) rds AddSourceIdentifierToSubscription sqs rds CopyDBClusterParameterGroup sqs rds CopyDBClusterSnapshot sqs @@ -190,7 +177,6 @@ rds DescribeEventSubscriptions sqs rds DescribeEvents sqs rds FailoverDBCluster sqs rds FailoverGlobalCluster sqs -rds ListTagsForResource sns rds ModifyDBClusterEndpoint sqs rds ModifyDBClusterSnapshotAttribute sqs rds ModifyDBSnapshot sqs @@ -215,16 +201,13 @@ rds StopDBInstance sqs rds SwitchoverGlobalCluster sqs rds SwitchoverReadReplica sqs -# redshift: 12 (9 unrouted, 3 claimed elsewhere) +# redshift: 9 (9 unrouted, 0 claimed elsewhere) redshift CreateIntegration sqs -redshift CreateTags ec2 redshift DeleteIntegration sqs -redshift DeleteTags ec2 redshift DescribeClusterVersions sqs redshift DescribeIntegrations sqs redshift DescribeLoggingStatus sqs redshift DescribeOrderableClusterOptions sqs -redshift DescribeTags ec2 redshift DisableLogging sqs redshift EnableLogging sqs redshift ModifyClusterIamRoles sqs