fix(codebuild): give resource-tag conditions the tags of the project or report group a request names (#8596)

The generated request resources give CodeBuild's project and report
group types aws:ResourceTag/ keys, but CodeBuildStoredResourceArns did
not answer storedTags, so a condition on their tags saw none. It now
answers from CodeBuildService.storedTags, which reads the project or
the report group stored under the ARN in the account and Region it
names, a pre-multi-account one of the default account in place,
matched on the whole ARN, and gives its key and value pairs as tags.
The tag lists are volatile, as CodeBuild replaces a resource's list
whole and never changes one in place.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 11:53:55 -04:00
1 parent 48be04cc21
commit d58d9ecac9
8 files changed
+181 -11

No files matched your search

@@ -0,0 +1,66 @@
package io.floci.aws.services.codebuild;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.codebuild.model.Project;
import io.floci.aws.services.codebuild.model.ProjectArtifacts;
import io.floci.aws.services.codebuild.model.ProjectEnvironment;
import io.floci.aws.services.codebuild.model.ProjectSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class CodeBuildStoredTagsTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private CodeBuildService service;
@BeforeEach
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
when(storageFactory.create(anyString(), anyString(), any()))
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory(ACCOUNT));
service = new CodeBuildService(mock(CodeBuildRunner.class), mock(EmulatorConfig.class), storageFactory);
service.initializeStorage();
}
@Test
void storedTagsReadTheProjectOrTheReportGroupTheArnNamesInItsAccountAndRegion() {
String project = createProject("web", List.of(tag("team", "data"))).getArn();
String reportGroup = service.createReportGroup(REGION, ACCOUNT, "tests", "TEST",
Map.of("exportConfigType", "NO_EXPORT"), List.of(tag("team", "qa"))).getArn();
assertEquals(Map.of("team", "data"), service.storedTags(project));
assertEquals(Map.of("team", "qa"), service.storedTags(reportGroup));
assertEquals(Map.of(), service.storedTags(project.replace("web", "api")));
assertEquals(Map.of(), service.storedTags(project.replace(ACCOUNT, "111111111111")));
assertEquals(Map.of(), service.storedTags(project.replace(REGION, "us-west-2")));
assertEquals(Map.of(), service.storedTags(reportGroup.replace(ACCOUNT, "111111111111")));
assertEquals(Map.of(), service.storedTags(createProject("untagged", null).getArn()));
}
private Project createProject(String name, List<Map<String, String>> tags) {
ProjectSource source = new ProjectSource();
source.setType("NO_SOURCE");
ProjectArtifacts artifacts = new ProjectArtifacts();
artifacts.setType("NO_ARTIFACTS");
return service.createProject(REGION, ACCOUNT, name, null, source, null, null, artifacts, null,
new ProjectEnvironment(), "arn:aws:iam::" + ACCOUNT + ":role/codebuild", null, null, null, tags,
null, null, null);
}
private static Map<String, String> tag(String key, String value) {
return Map.of("key", key, "value", value);
}
}
@@ -901,6 +901,51 @@ class IamRequestResourceEnforcementIntegrationTest {
.body(containsString("ResourceNotFoundException"));
}
/**
* An {@code aws:ResourceTag} condition reads the tags CodeBuild holds on a project and a report group, so an allow
* on one team's tag lets the caller read that team's and not another's.
*/
@Test
void aCodeBuildResourceTagConditionReadsTheTagsOfTheProjectAndTheReportGroup() {
String tagged = codeBuildProject("res-tag-project-" + suffix(), "data");
String other = codeBuildProject("res-tag-project-" + suffix(), "web");
String taggedGroup = codeBuildReportGroup("res-tag-group-" + suffix(), "data");
String otherGroup = codeBuildReportGroup("res-tag-group-" + suffix(), "web");
User user = user("res-codebuild-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":[\"codebuild:BatchGetProjects\","
+ "\"codebuild:BatchGetReportGroups\"],\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
codeBuild(user, "BatchGetProjects", "{\"names\":[\"" + tagged + "\"]}").statusCode(200);
refusedJson(codeBuild(user, "BatchGetProjects", "{\"names\":[\"" + other + "\"]}"),
"codebuild:BatchGetProjects");
codeBuild(user, "BatchGetReportGroups", "{\"reportGroupArns\":[\"" + taggedGroup + "\"]}").statusCode(200);
refusedJson(codeBuild(user, "BatchGetReportGroups", "{\"reportGroupArns\":[\"" + otherGroup + "\"]}"),
"codebuild:BatchGetReportGroups");
}
private String codeBuildProject(String name, String team) {
adminJson("codebuild", "CodeBuild_20161006.CreateProject").body("{\"name\":\"" + name
+ "\",\"source\":{\"type\":\"NO_SOURCE\",\"buildspec\":\"version: 0.2\"},"
+ "\"artifacts\":{\"type\":\"NO_ARTIFACTS\"},\"environment\":{\"type\":\"LINUX_CONTAINER\","
+ "\"image\":\"aws/codebuild/standard:7.0\",\"computeType\":\"BUILD_GENERAL1_SMALL\"},"
+ "\"serviceRole\":\"arn:aws:iam::" + ACCOUNT + ":role/codebuild-role\","
+ "\"tags\":[{\"key\":\"team\",\"value\":\"" + team + "\"}]}").post("/").then().statusCode(200);
cleanup.register(() -> adminJson("codebuild", "CodeBuild_20161006.DeleteProject")
.body("{\"name\":\"" + name + "\"}").post("/"));
return name;
}
private String codeBuildReportGroup(String name, String team) {
String arn = adminJson("codebuild", "CodeBuild_20161006.CreateReportGroup").body("{\"name\":\"" + name
+ "\",\"type\":\"TEST\",\"exportConfig\":{\"exportConfigType\":\"NO_EXPORT\"},"
+ "\"tags\":[{\"key\":\"team\",\"value\":\"" + team + "\"}]}").post("/").then().statusCode(200)
.extract().path("reportGroup.arn");
cleanup.register(() -> adminJson("codebuild", "CodeBuild_20161006.DeleteReportGroup")
.body("{\"arn\":\"" + arn + "\"}").post("/"));
return arn;
}
/** A Managed Service for Apache Flink application is checked on the ARN its name gives. */
@Test
void anApplicationIsCheckedOnTheArnItsNameGives() {