fix(cloudfront): give resource-tag conditions the tags of the resource a request names (#8700)

The generated request resources give ten CloudFront types the
aws:ResourceTag/ key, but CloudFrontKeyValueStoreService, the
namespace's StoredResourceArns, answered no tags, so a condition on a
CloudFront resource's tags saw none. It now reads the tags CloudFront
holds under the ARN, in place, from the tag store CloudFrontService
writes, where the tag handlers keep them: in the request's account, by
the whole ARN, since CloudFront ARNs name the default account whichever
account owns the resource. DeleteFunction now removes the tags kept
under the function's ARN, its name, and CreateFunction drops any left
there, so a function created again under a deleted one's name starts
with none. CloudFront's tag writes already put a new map in place of
the held one.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 23:54:23 -04:00
1 parent bcf5e58886
commit 15519216c3
7 files changed
+156 -20

No files matched your search

@@ -348,6 +348,61 @@ class CloudFrontServiceTest {
assertEquals(Map.of("team", "infra"), service.listTagsForResource(dist.getArn()));
}
/**
* IAM's tag lookup, which the key value store service answers for the namespace, reads the tags CloudFront holds
* under the whole ARN where its tag handlers keep them, in the account the request runs in, from the store this
* service writes them to; the ARN's account does not move the read, since CloudFront ARNs name the default one.
*/
@Test
void storedTagsAreTheTagsHeldUnderTheWholeArnWhereTheRequestRuns() {
CloudFrontService service = serviceWithDomainSuffix(DEFAULT_DOMAIN_SUFFIX);
CloudFrontKeyValueStoreService lookup = keyValueStoresOnTheSameFiles();
Distribution dist = service.createDistribution(new Distribution(), Map.of("team", "data"));
service.tagResource(dist.getArn(), Map.of("env", "prod"));
String otherAccountArn = dist.getArn().replace(ACCOUNT, "111111111111");
service.tagResource(otherAccountArn, Map.of("owner", "them"));
assertEquals(Map.of("team", "data", "env", "prod"), lookup.storedTags(dist.getArn()));
assertEquals(Map.of("owner", "them"), lookup.storedTags(otherAccountArn));
assertEquals(Map.of(), lookup.storedTags(dist.getArn() + "X"));
assertEquals(Map.of(), lookup.storedTags("arn:aws:s3:::" + dist.getId().toLowerCase()));
}
/** A function's ARN is its name, so one deleted and created again starts with no tags, for IAM as for the API. */
@Test
void aFunctionCreatedAgainUnderADeletedOnesNameHasNoneOfItsTags() {
CloudFrontService service = serviceWithDomainSuffix(DEFAULT_DOMAIN_SUFFIX);
CloudFrontKeyValueStoreService lookup = keyValueStoresOnTheSameFiles();
String arn = service.functionArn("tagged");
service.tagResource(arn, Map.of("seeded", "before"));
CloudFrontFunction created = service.createFunction(function("tagged"));
assertEquals(Map.of(), service.listTagsForResource(arn));
service.tagResource(arn, Map.of("team", "data"));
service.deleteFunction("tagged", created.getEtag());
service.createFunction(function("tagged"));
assertEquals(Map.of(), service.listTagsForResource(arn));
assertEquals(Map.of(), lookup.storedTags(arn));
}
/** A key value store service on the same per-file stores as the service {@link #serviceWithDomainSuffix} built. */
private CloudFrontKeyValueStoreService keyValueStoresOnTheSameFiles() {
StorageFactory sameFiles = Mockito.mock(StorageFactory.class);
when(sameFiles.create(Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenAnswer(invocation -> stores.computeIfAbsent(invocation.getArgument(1, String.class),
ignored -> AccountAwareStorageBackend.inMemory(ACCOUNT)));
return new CloudFrontKeyValueStoreService(sameFiles, new RegionResolver("us-east-1", ACCOUNT));
}
private static CloudFrontFunction function(String name) {
CloudFrontFunction function = new CloudFrontFunction();
function.setName(name);
function.setFunctionCode("function handler(event) { return event.request; }");
function.setRuntime("cloudfront-js-2.0");
return function;
}
@Test
void deletingDistributionRemovesTagsStoredUnderItsArn() {
CloudFrontService service = serviceWithDomainSuffix(DEFAULT_DOMAIN_SUFFIX);
@@ -53,6 +53,8 @@ class IamRestRouteEnforcementIntegrationTest {
private static final String MANAGEMENT = "731000000154";
/** Another account, owning the sinks and shares this class's own account reaches across accounts. */
private static final String PEER = "731000000162";
/** The XML namespace of CloudFront's 2020-05-31 control plane requests. */
private static final String CLOUDFRONT_XMLNS = "http://cloudfront.amazonaws.com/doc/2020-05-31/";
@TestHTTPResource("/")
static URI baseUri;
@@ -115,18 +117,10 @@ class IamRestRouteEnforcementIntegrationTest {
@Test
void cloudFrontKeyValueStoreControlPlaneIsCheckedAgainstTheStoresReturnedArn() {
String user = user("route-kvs-control");
String name = "kvs-" + suffix();
String namespace = "http://cloudfront.amazonaws.com/doc/2020-05-31/";
Response created = given().header("Authorization", auth(ADMIN, "cloudfront"))
.contentType("application/xml")
.body("<CreateKeyValueStoreRequest xmlns=\"" + namespace + "\"><Name>" + name
+ "</Name></CreateKeyValueStoreRequest>")
.post("/2020-05-31/key-value-store");
created.then().statusCode(201);
String arn = XmlParser.extractFirst(created.asString(), "ARN", null);
String etag = created.header("ETag");
cleanup.register(() -> given().header("Authorization", auth(ADMIN, "cloudfront")).header("If-Match", etag)
.delete("/2020-05-31/key-value-store/" + name).then().statusCode(204));
KeyValueStoreFixture created = keyValueStore();
String name = created.name();
String arn = created.arn();
String etag = created.etag();
policy(user, allow("cloudfront:*", "*") + "," +
"{\"Effect\":\"Deny\",\"Action\":\"cloudfront:DeleteKeyValueStore\",\"Resource\":\"" + arn + "\"}");
@@ -141,6 +135,53 @@ class IamRestRouteEnforcementIntegrationTest {
policy(user, allow("cloudfront:DescribeKeyValueStore", arn));
as(user, "cloudfront").get("/2020-05-31/key-value-store/" + name).then().statusCode(200);
}
/**
* An {@code aws:ResourceTag} condition reads the tags CloudFront holds on the key value store a request names, so
* an allow on one team's tag lets the caller describe that team's store and not another's.
*/
@Test
void aCloudFrontResourceTagConditionReadsTheTagsOfTheKeyValueStore() {
String user = user("route-kvs-tags");
KeyValueStoreFixture tagged = keyValueStore();
KeyValueStoreFixture other = keyValueStore();
tagCloudFront(tagged.arn(), "data");
tagCloudFront(other.arn(), "web");
policy(user, "{\"Effect\":\"Allow\",\"Action\":\"cloudfront:DescribeKeyValueStore\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
as(user, "cloudfront").get("/2020-05-31/key-value-store/" + tagged.name()).then().statusCode(200);
refused(as(user, "cloudfront").get("/2020-05-31/key-value-store/" + other.name()).then(),
"cloudfront:DescribeKeyValueStore");
}
/** A CloudFront key value store an administrator created, by its name, ARN and control plane ETag. */
private record KeyValueStoreFixture(String name, String arn, String etag) {}
/** A key value store an administrator creates, deleted after the test. */
private KeyValueStoreFixture keyValueStore() {
String name = "kvs-" + suffix();
Response created = given().header("Authorization", auth(ADMIN, "cloudfront"))
.contentType("application/xml")
.body("<CreateKeyValueStoreRequest xmlns=\"" + CLOUDFRONT_XMLNS + "\"><Name>" + name
+ "</Name></CreateKeyValueStoreRequest>")
.post("/2020-05-31/key-value-store");
created.then().statusCode(201);
String etag = created.header("ETag");
cleanup.register(() -> given().header("Authorization", auth(ADMIN, "cloudfront")).header("If-Match", etag)
.delete("/2020-05-31/key-value-store/" + name).then().statusCode(204));
return new KeyValueStoreFixture(name, XmlParser.extractFirst(created.asString(), "ARN", null), etag);
}
/** Tags the CloudFront resource {@code arn} with the team, as an administrator. */
private static void tagCloudFront(String arn, String team) {
given().header("Authorization", auth(ADMIN, "cloudfront")).contentType("application/xml")
.queryParam("Operation", "Tag").queryParam("Resource", arn)
.body("<Tags xmlns=\"" + CLOUDFRONT_XMLNS + "\"><Items><Tag><Key>team</Key><Value>" + team
+ "</Value></Tag></Items></Tags>")
.post("/2020-05-31/tagging").then().statusCode(204);
}
@Test
void aScheduleRequestSignedForAnotherServiceIsStillCheckedAsScheduler() {
// Checked by the route it reached, not by the credential scope it was signed with: an S3