fix(cloudfront): generate IAM routes and request resources (#7991)

CloudFront's REST routes were not in the vendored route table and its namespace's resources were not read, so under IAM enforcement no CloudFront request was checked: a caller with no permissions could create, change and delete distributions, cache policies, functions and the rest. Its botocore model is now in regen_rest_routes.MODELS and its namespace in regen_iam_request_resources.NAMESPACES, so each request is checked as the action of the operation its route names, on the resources the request names.

regen_iam_request_resources read requests of the JSON, Query, CBOR and REST-JSON protocols only, so a REST-XML request named no resource. It now reads a REST-XML request's path labels and query parameters, which RequestResources reads whatever the protocol, and not its body, which is not one it reads.

Creation tags stay unread: CloudFront wraps them in a Tags structure holding Items, which regen_iam_tag_on_create does not read yet.

Three CloudFront operation shapes were still checked on `*` or as no
IAM action:

- `stem` drops a trailing `config`, so a bare `Id` names the resource
  whose config an operation reads (GetDistributionConfig and the other
  ...Config reads, and GetFieldLevelEncryption, whose type is
  field-level-encryption-config).
- A member whose pattern takes only an ARN counts as an ARN when it is
  named `Resource`, as well as when it is an identifier, so the tagging
  calls are checked on the resource their ?Resource names. Members
  naming another resource (a role, a monitor, a target) still do not.
- An operation whose own name is no action, and which several actions
  authorize, is the one its name starts with as a word:
  CreateDistributionWithTags is checked as cloudfront:CreateDistribution.
  The same rule maps Keyspaces' CreateKeyspace, CreateTable, CreateType
  and RestoreTable to cassandra:Create and cassandra:Restore, and
  QuickSight's CreateTopicV2 and UpdateTopicV2 to quicksight:CreateTopic
  and quicksight:UpdateTopic. A plural is no such word: S3's
  DeleteObjects is checked for each object it deletes.
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-11 22:10:09 -04:00
1 parent c9d9ba24f4
commit e13bb4e1ec
11 files changed
+698 -19

No files matched your search

@@ -0,0 +1,156 @@
package io.floci.aws.services.cloudfront;
import io.floci.aws.testing.SignedIamEnforcementProfile;
import io.floci.aws.testing.SignedIamUsers;
import io.floci.aws.testutil.AwsRequestSigner;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* CloudFront requests are checked against the caller's policies through its generated routes and request
* resources: as the action of the operation each route names, on the resource its path names
* ({@code arn:aws:cloudfront::<account>:cache-policy/<id>} for a cache policy), before the handler changes anything.
*/
@QuarkusTest
@TestProfile(SignedIamEnforcementProfile.class)
class CloudFrontIamIntegrationTest {
private static final String API = "/2020-05-31";
private static final String CACHE_POLICY = """
<CachePolicyConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
<Name>%s</Name>
<MinTTL>1</MinTTL>
<ParametersInCacheKeyAndForwardedToOrigin>
<EnableAcceptEncodingGzip>false</EnableAcceptEncodingGzip>
<HeadersConfig><HeaderBehavior>none</HeaderBehavior></HeadersConfig>
<CookiesConfig><CookieBehavior>none</CookieBehavior></CookiesConfig>
<QueryStringsConfig><QueryStringBehavior>none</QueryStringBehavior></QueryStringsConfig>
</ParametersInCacheKeyAndForwardedToOrigin>
</CachePolicyConfig>""";
private final AwsRequestSigner admin = AwsRequestSigner.signedAs("test", "test", "cloudfront");
@TestHTTPResource("/")
URI endpoint;
@Test
void callerWithoutPermissionsIsRefusedBeforeAnythingIsCreated() throws Exception {
String name = "iam-none-" + UUID.randomUUID().toString().substring(0, 8);
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "cf-iam-")) {
denied(send(user.signer("cloudfront"), "POST", API + "/cache-policy", CACHE_POLICY.formatted(name)),
"cloudfront:CreateCachePolicy");
denied(send(user.signer("cloudfront"), "GET", API + "/distribution", null), "cloudfront:ListDistributions");
// CreateDistributionWithTags is no IAM action: AWS checks it as CreateDistribution.
denied(send(user.signer("cloudfront"), "POST", API + "/distribution?WithTags", "<DistributionConfigWithTags/>"),
"cloudfront:CreateDistribution");
}
send(admin, "GET", API + "/cache-policy?Type=custom", null).then().statusCode(200)
.body(not(containsString(name)));
}
@Test
void cachePolicyScopedAllowReadsThatPolicyAndRefusesAnother() throws Exception {
String allowed = createCachePolicy();
String other = createCachePolicy();
try (SignedIamUsers.User user = user("Allow", "[\"cloudfront:GetCachePolicy\",\"cloudfront:GetCachePolicyConfig\"]",
cachePolicyArn(allowed))) {
send(user.signer("cloudfront"), "GET", API + "/cache-policy/" + allowed, null).then().statusCode(200)
.body(containsString("<Id>" + allowed + "</Id>"));
denied(send(user.signer("cloudfront"), "GET", API + "/cache-policy/" + other, null),
"cloudfront:GetCachePolicy");
// A config read is checked on the policy its path names, as the policy's read is.
send(user.signer("cloudfront"), "GET", API + "/cache-policy/" + allowed + "/config", null).then()
.statusCode(200).body(containsString("<CachePolicyConfig"));
denied(send(user.signer("cloudfront"), "GET", API + "/cache-policy/" + other + "/config", null),
"cloudfront:GetCachePolicyConfig");
} finally {
deleteCachePolicy(allowed);
deleteCachePolicy(other);
}
}
@Test
void explicitDenyKeepsACachePolicyFromBeingDeleted() throws Exception {
String id = createCachePolicy();
try (SignedIamUsers.User user = user("Deny", "cloudfront:DeleteCachePolicy", cachePolicyArn(id))) {
String etag = send(admin, "GET", API + "/cache-policy/" + id, null).then().statusCode(200)
.extract().header("ETag");
denied(send(user.signer("cloudfront"), "DELETE", API + "/cache-policy/" + id, null, Map.of("if-match", etag)),
"cloudfront:DeleteCachePolicy");
send(admin, "GET", API + "/cache-policy/" + id, null).then().statusCode(200);
} finally {
deleteCachePolicy(id);
}
}
@Test
void listingNeedsTheListActionNotTheGetAction() throws Exception {
try (SignedIamUsers.User user = user("Allow", "cloudfront:GetCachePolicy", "*")) {
denied(send(user.signer("cloudfront"), "GET", API + "/cache-policy", null), "cloudfront:ListCachePolicies");
}
try (SignedIamUsers.User user = user("Allow", "cloudfront:ListCachePolicies", "*")) {
send(user.signer("cloudfront"), "GET", API + "/cache-policy", null).then().statusCode(200)
.body(containsString("<CachePolicyList"));
}
}
private String createCachePolicy() throws Exception {
String name = "iam-" + UUID.randomUUID().toString().substring(0, 8);
return send(admin, "POST", API + "/cache-policy", CACHE_POLICY.formatted(name)).then().statusCode(201)
.extract().xmlPath().getString("CachePolicy.Id");
}
private void deleteCachePolicy(String id) throws Exception {
Response current = send(admin, "GET", API + "/cache-policy/" + id, null);
if (current.statusCode() == 200) {
send(admin, "DELETE", API + "/cache-policy/" + id, null, Map.of("if-match", current.header("ETag")))
.then().statusCode(204);
}
}
private static String cachePolicyArn(String id) {
return "arn:aws:cloudfront::000000000000:cache-policy/" + id;
}
private SignedIamUsers.User user(String effect, String action, String resource) throws Exception {
String allow = "Deny".equals(effect) ? "{\"Effect\":\"Allow\",\"Action\":\"cloudfront:*\",\"Resource\":\"*\"}," : "";
return SignedIamUsers.createUser(endpoint, "cf-iam-", "access",
"{\"Version\":\"2012-10-17\",\"Statement\":[" + allow + "{\"Effect\":\"" + effect
+ "\",\"Action\":" + (action.startsWith("[") ? action : "\"" + action + "\"")
+ ",\"Resource\":\"" + resource + "\"}]}");
}
private Response send(AwsRequestSigner signer, String method, String path, String body) throws Exception {
return send(signer, method, path, body, Map.of());
}
private Response send(AwsRequestSigner signer, String method, String path, String body, Map<String, String> headers)
throws Exception {
URI uri = endpoint.resolve(path);
byte[] bytes = body == null ? new byte[0] : body.getBytes(StandardCharsets.UTF_8);
// The signer signs the extra headers but returns only its own, so the request carries both.
RequestSpecification request = given().urlEncodingEnabled(false).headers(headers)
.headers(signer.headersFor(method, uri, headers, bytes));
if (body != null) {
request = request.contentType("application/xml").body(bytes);
}
return request.request(method, uri);
}
private static void denied(Response response, String action) {
response.then().statusCode(403).body(containsString("not authorized to perform: " + action));
}
}