fix(route53): check Route 53's requests under IAM enforcement (#8090)

Under IAM enforcement no Route 53 request but the delegation set calls
was checked: Route 53's model was not among the vendored REST routes,
and its namespace's request resources were not read, so no other
request was mapped to an action and the filter let it through. A
caller with no permissions could create, change and delete hosted
zones, record sets, health checks and tags.

Route 53's routes are now vendored and its requests read, now that
REST-XML path and query members are. Two generator changes name what
the rules did not:

- a path segment in ies names the type in y, so a bare Id after it is
  that type's: ListTrafficPolicyVersions's, in
  /trafficpolicies/{Id}/versions, is the traffic policy's, as the rule
  for path labels already reads UpdateHostedZoneComment's hosted zone
  and the other traffic policy calls;
- a member whose enum lists two or more of the action's types picks
  the type: Route 53's tag calls, by ResourceType.

RequestResources drops a leading /<type>/ from an id, the path Route 53
returns its ids under, so a delegation set or hosted zone named that
way is the same resource. The delegation set calls go through the
vendored routes like the rest, and their hand-built action map and ARN
builder are gone.

Co-authored-by: mrfelek <17274927+mrfelek@users.noreply.github.com>
This commit is contained in:
mrfelekandmrfelek authored and GitHub committed 2026-11-12 20:56:16 -04:00
1 parent 282b53cacc
commit 6b1ba4387a
12 files changed
+451 -59

No files matched your search

@@ -3,9 +3,7 @@ package io.floci.aws.services.iam;
import io.floci.aws.core.common.AwsProtocolClaimFilter;
import io.floci.aws.core.common.ProtocolClaim;
import io.floci.aws.core.common.WireProtocol;
import io.floci.aws.services.route53.Route53Controller;
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;
@@ -23,7 +21,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@@ -224,18 +221,16 @@ class IamActionRegistryTest {
assertNull(registry.resolveRoute("access-analyzer", ctx));
}
/** Route 53's delegation set calls resolve from their vendored routes, an id given with its type's path too. */
@ParameterizedTest
@CsvSource({"createReusableDelegationSet,CreateReusableDelegationSet",
"getReusableDelegationSet,GetReusableDelegationSet",
"listReusableDelegationSets,ListReusableDelegationSets",
"deleteReusableDelegationSet,DeleteReusableDelegationSet"})
void matchedDelegationSetMethodsResolveTheirOwnActions(String method, String operation) {
ResourceInfo info = Mockito.mock(ResourceInfo.class);
Mockito.doReturn(Route53Controller.class).when(info).getResourceClass();
when(info.getResourceMethod()).thenReturn(Arrays.stream(Route53Controller.class.getMethods())
.filter(candidate -> candidate.getName().equals(method)).findFirst().orElseThrow());
assertEquals("route53:" + operation, registry.resolveMatchedRoute("route53", info));
assertNull(registry.resolveMatchedRoute("s3", info));
@CsvSource({"POST,/2013-04-01/delegationset,CreateReusableDelegationSet",
"GET,/2013-04-01/delegationset,ListReusableDelegationSets",
"GET,/2013-04-01/delegationset/N1,GetReusableDelegationSet",
"GET,/2013-04-01/delegationset/%2Fdelegationset%2FN1,GetReusableDelegationSet",
"DELETE,/2013-04-01/delegationset/N1,DeleteReusableDelegationSet"})
void delegationSetRoutesResolveTheirOwnActions(String method, String path, String operation) {
assertEquals("route53:" + operation, registry.resolveRoute("route53",
mockCtx(method, path, new MultivaluedHashMap<>(), MediaType.APPLICATION_XML_TYPE, "")));
}
@ParameterizedTest
@@ -74,10 +74,9 @@ class ResourceArnBuilderTest {
for (String id : List.of("N1", "%2Fdelegationset%2FN1")) {
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost/2013-04-01/delegationset/" + id));
when(uriInfo.getBaseUri()).thenReturn(URI.create("http://localhost/"));
MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
parameters.putSingle("Id", id.equals("N1") ? "N1" : "/delegationset/N1");
when(uriInfo.getPathParameters()).thenReturn(parameters);
for (String operation : List.of("GetReusableDelegationSet", "DeleteReusableDelegationSet")) {
// The id is read from the route the request matched, the type's path Route 53 returns it under dropped.
when(ctx.getMethod()).thenReturn(operation.startsWith("Get") ? "GET" : "DELETE");
assertEquals(List.of("arn:" + partition + ":route53:::delegationset/N1"),
builder.buildResources("route53", "route53:" + operation, ctx, region, ACCOUNT, partition));
}
@@ -0,0 +1,162 @@
package io.floci.aws.services.route53;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.EnforcementFixtures;
import io.floci.aws.testing.IamEnforcementProfile;
import io.floci.aws.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.RegisterExtension;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.Route53Exception;
import software.amazon.awssdk.services.route53.model.Tag;
import software.amazon.awssdk.services.route53.model.TagResourceType;
import java.net.URI;
import java.util.UUID;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Hosted zone requests are checked on the hosted zone their path names
* ({@code arn:aws:route53:::hostedzone/<id>}), and a tag request on the resource its
* {@code ResourceType} and {@code ResourceId} labels name, before the handler changes anything.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Route53HostedZonesIamEnforcementIntegrationTest {
private static final String ACCOUNT = "741686000031";
@TestHTTPResource("/")
URI baseUri;
private final PartitionCleanup CLEANUP = new PartitionCleanup();
@RegisterExtension
final AfterAllCallback classCleanup = CLEANUP::afterEach;
private Route53Client root;
private String selectedZone;
private String otherZone;
@BeforeAll
void createSharedFixtures() {
root = client(ACCOUNT);
CLEANUP.register(root::close);
selectedZone = createZone(root);
otherZone = createZone(root);
}
@Test
void aCallerWithoutPermissionsIsRefusedBeforeAZoneIsCreated() {
String name = "none-" + UUID.randomUUID().toString().substring(0, 8) + ".example.com.";
try (Route53Client client = client(userWithPolicy(null))) {
assertDenied(() -> client.createHostedZone(r -> r.name(name).callerReference(UUID.randomUUID().toString())));
assertDenied(() -> client.getHostedZone(r -> r.id(selectedZone)));
assertDenied(client::listHostedZones);
}
assertFalse(root.listHostedZones().hostedZones().stream().anyMatch(zone -> zone.name().equals(name)));
}
@Test
void aZoneScopedAllowGrantsOnlyThatZone() {
String key = userWithPolicy(allow("[\"route53:GetHostedZone\",\"route53:UpdateHostedZoneComment\","
+ "\"route53:ListResourceRecordSets\"]", zoneArn(selectedZone)));
try (Route53Client client = client(key)) {
assertEquals(selectedZone, bare(client.getHostedZone(r -> r.id(selectedZone)).hostedZone().id()));
client.updateHostedZoneComment(r -> r.id(selectedZone).comment("scoped"));
client.listResourceRecordSets(r -> r.hostedZoneId(selectedZone));
assertDenied(() -> client.getHostedZone(r -> r.id(otherZone)));
assertDenied(() -> client.updateHostedZoneComment(r -> r.id(otherZone).comment("scoped")));
assertDenied(() -> client.listResourceRecordSets(r -> r.hostedZoneId(otherZone)));
}
}
@Test
void anExplicitDenyOnAZoneKeepsItFromBeingDeleted() {
String zone = createZone(root);
String key = userWithPolicy("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"route53:*","Resource":"*"},
{"Effect":"Deny","Action":"route53:DeleteHostedZone","Resource":"%s"}]}
""".formatted(zoneArn(zone)));
try (Route53Client client = client(key)) {
assertDenied(() -> client.deleteHostedZone(r -> r.id(zone)));
// Named by the path Route 53 returns its id under, the zone is the same one, and so is the deny.
given().urlEncodingEnabled(false).header("Authorization", auth(key, "route53"))
.delete("/2013-04-01/hostedzone/%2Fhostedzone%2F" + zone)
.then().statusCode(403).body(containsString("<Code>AccessDenied</Code>"));
assertEquals(zone, bare(root.getHostedZone(r -> r.id(zone)).hostedZone().id()));
}
}
@Test
void aTagRequestIsCheckedOnTheResourceItsTypeAndIdName() {
String key = userWithPolicy(allow("[\"route53:ChangeTagsForResource\",\"route53:ListTagsForResource\"]",
zoneArn(selectedZone)));
Tag tag = Tag.builder().key("team").value("dns").build();
try (Route53Client client = client(key)) {
client.changeTagsForResource(r -> r.resourceType(TagResourceType.HOSTEDZONE).resourceId(selectedZone)
.addTags(tag));
assertEquals(1, client.listTagsForResource(r -> r.resourceType(TagResourceType.HOSTEDZONE)
.resourceId(selectedZone)).resourceTagSet().tags().size());
assertDenied(() -> client.changeTagsForResource(r -> r.resourceType(TagResourceType.HOSTEDZONE)
.resourceId(otherZone).addTags(tag)));
// The same id under another type is another resource.
assertDenied(() -> client.listTagsForResource(r -> r.resourceType(TagResourceType.HEALTHCHECK)
.resourceId(selectedZone)));
}
}
private String createZone(Route53Client client) {
String name = "iam-" + UUID.randomUUID().toString().substring(0, 8) + ".example.com";
String id = bare(client.createHostedZone(r -> r.name(name).callerReference(UUID.randomUUID().toString()))
.hostedZone().id());
CLEANUP.register(() -> root.deleteHostedZone(r -> r.id(id)));
return id;
}
/** A hosted zone's id without the {@code /hostedzone/} path Route 53 returns it under. */
private static String bare(String id) {
return id.replace("/hostedzone/", "");
}
private static String zoneArn(String id) {
return "arn:aws:route53:::hostedzone/" + id;
}
private static String allow(String actions, String resource) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":%s,"Resource":"%s"}]}
""".formatted(actions, resource);
}
private String userWithPolicy(String policy) {
String user = "r53-zones-" + UUID.randomUUID();
String key = EnforcementFixtures.createUser(CLEANUP, baseUri.getPort(), auth(ACCOUNT, "iam"), user).accessKeyId();
if (policy != null) {
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "zones", policy);
}
return key;
}
private Route53Client client(String key) {
return AwsTestClients.create(Route53Client.builder(), key, Region.US_EAST_1, baseUri);
}
private void assertDenied(Runnable call) {
Route53Exception error = assertThrows(Route53Exception.class, call::run);
assertEquals(403, error.statusCode());
assertEquals("AccessDenied", error.awsErrorDetails().errorCode());
}
}