refactor(iam): move request operation resolution out of the enforcement filter (#8785)

IamEnforcementFilter works out which service a request reaches, the
operation it calls there and the IAM action that operation is authorized
as, for Query, Smithy RPC v2, JSON and REST requests alike. That lives in
private methods of the filter and runs only while IAM enforcement is on.

The fault-injection half of #4623 needs the same answer for every request,
enforcement on or off, to match FIS inject-api actions against the
operations they name. Move it, unchanged, into RequestOperationResolver:

- ResolvedAuthorization becomes the public ResolvedOperation record.
- The resolver is an application-scoped bean rather than a JAX-RS
  provider, so it takes the matched ResourceInfo as a parameter.
- The filter builds its own instance from the four collaborators it
  already holds, so none of its constructors change.

RequestOperationResolverTest covers each protocol against the real service
catalog and action rules, with no Quarkus boot.

Refs #4623
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 18:35:09 -04:00
1 parent aa2a9f959e
commit f76de9c463
3 files changed
+365 -193

No files matched your search

@@ -0,0 +1,122 @@
package io.floci.aws.core.common;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RequestOperationResolver.ResolvedOperation;
import io.floci.aws.services.iam.IamActionRegistry;
import io.floci.aws.services.iam.ResourceArnBuilder;
import io.floci.aws.services.s3.S3Controller;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ResourceInfo;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* {@link RequestOperationResolver} on its own, outside {@link IamEnforcementFilter}, with the real service catalog
* and action rules: each protocol resolves to the service that serves the request and the operation it calls there.
*/
class RequestOperationResolverTest {
private static final String SIGNED_FOR = "AWS4-HMAC-SHA256 "
+ "Credential=AKIAEXAMPLE/20261117/us-east-1/%s/aws4_request, "
+ "SignedHeaders=host;x-amz-date, Signature=deadbeef";
private final ResolvedServiceCatalog catalog =
new ResolvedServiceCatalog(mock(EmulatorConfig.class, RETURNS_DEEP_STUBS));
private final RequestOperationResolver resolver = new RequestOperationResolver(new IamActionRegistry(),
new AwsQueryServiceResolver(catalog), new ResourceArnBuilder(new ObjectMapper()), catalog);
@Test
void aQueryCallResolvesToTheOperationItsActionNames() {
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
query.add("Action", "DescribeInstances");
query.add("Version", "2016-11-15");
ContainerRequestContext ctx = request("GET", "/", query, Map.of(),
new ProtocolClaim(WireProtocol.AWS_QUERY, catalog.byExternalKey("ec2").orElseThrow(), null, null));
ResolvedOperation resolved = resolver.resolve(SIGNED_FOR.formatted("ec2"), "ec2", ctx, null);
assertEquals(new ResolvedOperation("ec2", "ec2:DescribeInstances", "ec2:DescribeInstances"), resolved);
}
@Test
void aJsonCallResolvesToTheOperationItsTargetNames() {
ContainerRequestContext ctx = request("POST", "/", new MultivaluedHashMap<>(),
Map.of("X-Amz-Target", "Kinesis_20131202.PutRecord"), jsonClaim("kinesis", "PutRecord"));
ResolvedOperation resolved = resolver.resolve(SIGNED_FOR.formatted("kinesis"), "kinesis", ctx, null);
assertEquals(new ResolvedOperation("kinesis", "kinesis:PutRecord", "kinesis:PutRecord"), resolved);
}
@Test
void aCallIsResolvedAsTheServiceThatServesItRatherThanTheScopeItIsSignedFor() {
ContainerRequestContext ctx = request("POST", "/", new MultivaluedHashMap<>(),
Map.of("X-Amz-Target", "DynamoDB_20120810.PutItem"), jsonClaim("dynamodb", "PutItem"));
ResolvedOperation resolved = resolver.resolve(SIGNED_FOR.formatted("sqs"), "sqs", ctx, null);
assertEquals("dynamodb", resolved.credentialScope());
assertEquals("dynamodb:PutItem", resolved.action());
}
@Test
void aRestRequestIsResolvedByItsRouteAndNotByAnXAmzTargetItCarries() {
ContainerRequestContext ctx = request("GET", "/bucket/key", new MultivaluedHashMap<>(),
Map.of("X-Amz-Target", "Kinesis_20131202.DeleteStream"), ProtocolClaim.rest());
ResourceInfo route = mock(ResourceInfo.class);
doReturn(S3Controller.class).when(route).getResourceClass();
ResolvedOperation resolved = resolver.resolve(SIGNED_FOR.formatted("s3"), "s3", ctx, route);
assertEquals(new ResolvedOperation("s3", "s3:GetObject", "s3:GetObject"), resolved);
}
@Test
void aRequestNoRuleMapsKeepsTheScopeItIsSignedFor() {
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
query.add("Version", "2016-11-15");
ContainerRequestContext ctx = request("GET", "/", query, Map.of(),
new ProtocolClaim(WireProtocol.AWS_QUERY, catalog.byExternalKey("ec2").orElseThrow(), null, null));
ResolvedOperation resolved = resolver.resolve(SIGNED_FOR.formatted("ec2"), "ec2", ctx, null);
assertEquals(new ResolvedOperation("ec2", null, null), resolved);
}
private ProtocolClaim jsonClaim(String service, String operation) {
ServiceDescriptor descriptor = catalog.byExternalKey(service).orElseThrow();
return new ProtocolClaim(WireProtocol.AWS_JSON_1_1, descriptor, operation, null);
}
private static ContainerRequestContext request(String method, String path, MultivaluedMap<String, String> query,
Map<String, String> headers, ProtocolClaim claim) {
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getMethod()).thenReturn(method);
when(ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY)).thenReturn(claim);
when(ctx.getHeaderString(anyString())).thenAnswer(invocation -> headers.get(invocation.<String>getArgument(0)));
when(ctx.getMediaType()).thenReturn("POST".equals(method)
? MediaType.valueOf("application/x-amz-json-1.1") : null);
UriInfo uri = mock(UriInfo.class);
when(uri.getPath()).thenReturn(path);
when(uri.getRequestUri()).thenReturn(URI.create("http://localhost:4566" + path));
when(uri.getBaseUri()).thenReturn(URI.create("http://localhost:4566/"));
when(uri.getQueryParameters()).thenReturn(query);
when(uri.getPathParameters()).thenReturn(new MultivaluedHashMap<>());
when(ctx.getUriInfo()).thenReturn(uri);
return ctx;
}
}