fix(codepipeline): give resource-tag conditions the tags of the pipeline a request names (#8839)

The generated request resources give CodePipeline's pipeline, webhook
and actiontype types the aws:ResourceTag/ key, but no service answered
the codepipeline namespace's stored tags, so a condition on their tags
saw none. CodePipelineService now answers it, in the request's account
and in place: a pipeline under its name and a webhook under its name,
each matched on the ARN it recorded, and a custom action type under the
key its ARN gives, as the tag handlers find it. A stage or an action ARN
reads nothing. Tag writes already replace the map under the resource's
lock; the tags fields are now volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 04:19:15 -04:00
1 parent 29d4555896
commit 933b617961
7 files changed
+204 -27

No files matched your search

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.codebuild.CodeBuildService;
import io.floci.aws.services.codebuild.model.Build;
@@ -55,7 +56,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -798,6 +801,81 @@ class CodePipelineServiceTest {
}
}
/**
* IAM's lookup reads the tags of the pipeline, the webhook or the custom action type the ARN names, as the tag
* calls and PutWebhook leave them, and nothing for a stage, another account's, another Region's or an unknown one.
*/
@Test
void storedTagsAreTheTagsOfThePipelineWebhookOrActionTypeTheArnNames() throws Exception {
String pipelineArn = "arn:aws:codepipeline:us-east-1:000000000000:stored";
handle("CreatePipeline", pipelineRequest("stored", "[{\"key\": \"team\", \"value\": \"web\"}]"));
handle("TagResource", "{\"resourceArn\": \"" + pipelineArn
+ "\", \"tags\": [{\"key\": \"team\", \"value\": \"data\"}, {\"key\": \"env\", \"value\": \"dev\"}]}");
handle("UntagResource", "{\"resourceArn\": \"" + pipelineArn + "\", \"tagKeys\": [\"env\"]}");
String actionTypeArn = "arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/StoredProvider/1";
handle("CreateCustomActionType", customActionTypeRequest("StoredProvider", 1));
handle("PutWebhook", """
{"webhook": {"name": "stored-hook", "targetPipeline": "stored", "targetAction": "ManualApproval"},
"tags": [{"key": "team", "value": "ops"}]}
""");
String webhookArn = "arn:aws:codepipeline:us-east-1:000000000000:webhook:stored-hook";
assertEquals("codepipeline", service.namespace());
assertEquals(Map.of("team", "data"), service.storedTags(pipelineArn));
assertEquals(Map.of("k0", "v"), service.storedTags(actionTypeArn));
assertEquals(Map.of("team", "ops"), service.storedTags(webhookArn));
assertTrue(service.storedTags(pipelineArn + "/Approve").isEmpty());
assertTrue(service.storedTags(pipelineArn.replace(ACCOUNT, "111122223333")).isEmpty());
assertTrue(service.storedTags(pipelineArn.replace(REGION, "us-west-2")).isEmpty());
assertTrue(service.storedTags(actionTypeArn.replace(REGION, "us-west-2")).isEmpty());
assertTrue(service.storedTags(webhookArn.replace("stored-hook", "missing")).isEmpty());
assertTrue(service.storedTags(pipelineArn.replace(":stored", ":missing")).isEmpty());
assertTrue(service.storedTags("arn:aws:codebuild:us-east-1:000000000000:project/stored").isEmpty());
}
/** A pipeline stored before storage was account-aware is read in place: the lookup migrates nothing. */
@Test
void storedTagsReadAPreAccountAwarePipelineInPlace() {
InMemoryStorage<String, CodePipelinePipeline> raw = new InMemoryStorage<>();
StorageFactory factory = mock(StorageFactory.class);
doAnswer(invocation -> AccountAwareStorageBackend.inMemory(ACCOUNT))
.when(factory).create(anyString(), anyString(), any());
doReturn(new AccountAwareStorageBackend<>(raw, null, ACCOUNT))
.when(factory).create(anyString(), eq("codepipeline-pipelines.json"), any());
CodePipelineService legacyState = new CodePipelineService(factory, mapper, mock(CodeBuildService.class),
mock(CodeDeployService.class), mock(LambdaService.class), mock(S3Service.class),
configWithPollInterval(500L));
CodePipelinePipeline legacy = new CodePipelinePipeline();
legacy.setName("legacy");
legacy.setRegion(REGION);
legacy.setArn("arn:aws:codepipeline:us-east-1:000000000000:legacy");
legacy.setTags(Map.of("team", "data"));
raw.put(REGION + ":legacy", legacy);
assertEquals(Map.of("team", "data"), legacyState.storedTags(legacy.getArn()));
assertEquals(Set.of(REGION + ":legacy"), raw.keys());
}
/** A two-stage pipeline of manual approvals named {@code name}, with the {@code tags} list. */
private static String pipelineRequest(String name, String tags) {
return """
{
"pipeline": {
"name": "%s",
"roleArn": "arn:aws:iam::000000000000:role/codepipeline-role",
"artifactStore": {"type": "S3", "location": "codepipeline-artifacts"},
"stages": [
{"name": "Approve", "actions": [{"name": "ManualApproval", "actionTypeId":
{"category": "Approval", "owner": "AWS", "provider": "Manual", "version": "1"}}]},
{"name": "Complete", "actions": [{"name": "ManualApprovalComplete", "actionTypeId":
{"category": "Approval", "owner": "AWS", "provider": "Manual", "version": "1"}}]}
]
},
"tags": %s
}
""".formatted(name, tags);
}
private static String customActionTypeRequest(String provider, int tagCount) {
StringJoiner tags = new StringJoiner(", ");
for (int i = 0; i < tagCount; i++) {
@@ -988,10 +988,7 @@ class IamRequestResourceEnforcementIntegrationTest {
@Test
void aPipelineAndAWebhookAreCheckedOnTheArnsTheirNamesGive() {
String name = "res-pipeline-" + suffix();
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
createPipeline(name, null);
String pipelineArn = adminJson("codepipeline", "CodePipeline_20150709.GetPipeline")
.body("{\"name\":\"" + name + "\"}").post("/").then().statusCode(200).extract().path("metadata.pipelineArn");
assertEquals("arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + name, pipelineArn);
@@ -1015,6 +1012,38 @@ class IamRequestResourceEnforcementIntegrationTest {
codePipeline(user, "DeleteWebhook", "{\"name\":\"" + hook + "\"}").statusCode(200);
}
/**
* An {@code aws:ResourceTag} condition reads the tags CodePipeline holds on the pipeline a request names, as
* TagResource leaves them, so an allow on one team's tag lets the caller read that team's pipeline and not
* another's.
*/
@Test
void aCodePipelineResourceTagConditionReadsTheTagsOfThePipeline() {
String web = "[{\"key\":\"team\",\"value\":\"web\"}]";
String tagged = "res-pipeline-tagged-" + suffix();
String other = "res-pipeline-other-" + suffix();
createPipeline(tagged, web);
createPipeline(other, web);
adminJson("codepipeline", "CodePipeline_20150709.TagResource")
.body("{\"resourceArn\":\"arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + tagged
+ "\",\"tags\":[{\"key\":\"team\",\"value\":\"data\"}]}")
.post("/").then().statusCode(200);
User user = user("res-pipeline-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"codepipeline:GetPipeline\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
codePipeline(user, "GetPipeline", "{\"name\":\"" + tagged + "\"}").statusCode(200);
refusedJson(codePipeline(user, "GetPipeline", "{\"name\":\"" + other + "\"}"), "codepipeline:GetPipeline");
}
/** Creates the pipeline {@code name} as an administrator, with the {@code tags} list if any, deleted afterwards. */
private void createPipeline(String name, String tags) {
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name, tags)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
}
/** {@code StartPipelineExecution} names its pipeline by a bare {@code name}, on which it is checked. */
@Test
void aPipelineExecutionIsCheckedOnThePipelineItsNameGives() {
@@ -1037,12 +1066,8 @@ class IamRequestResourceEnforcementIntegrationTest {
void anUpdateIsCheckedOnThePipelineItNamesNotOneItsRoleHolds() {
String allowed = "res-pipeline-" + suffix();
String other = allowed + "-other";
for (String name : List.of(allowed, other)) {
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
}
createPipeline(allowed, null);
createPipeline(other, null);
String allowedArn = "arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + allowed;
User user = user("res-pipeline-update");
user.policy(allowOn("codepipeline:UpdatePipeline", allowedArn) + ","
@@ -1056,10 +1081,7 @@ class IamRequestResourceEnforcementIntegrationTest {
@Test
void aWebhooksTagsAreCheckedOnTheWebhookNotItsPipeline() {
String name = "res-pipeline-" + suffix();
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
createPipeline(name, null);
String hook = "res-hook-" + suffix();
String codePipeline = "arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":";
User user = user("res-webhook-tags");
@@ -2799,12 +2821,18 @@ class IamRequestResourceEnforcementIntegrationTest {
/** A pipeline with a source and a build stage, the two stages CodePipeline requires. */
private static String pipeline(String name) {
return pipeline(name, null);
}
/** A CreatePipeline or UpdatePipeline body for the pipeline {@code name}, with the {@code tags} list if any. */
private static String pipeline(String name, String tags) {
return "{\"pipeline\":{\"name\":\"" + name + "\",\"roleArn\":\"arn:aws:iam::" + ACCOUNT + ":role/codepipeline-role\","
+ "\"artifactStore\":{\"type\":\"S3\",\"location\":\"codepipeline-artifacts\"},\"stages\":["
+ "{\"name\":\"Source\",\"actions\":[{\"name\":\"SourceAction\",\"actionTypeId\":{\"category\":\"Source\","
+ "\"owner\":\"AWS\",\"provider\":\"S3\",\"version\":\"1\"},\"runOrder\":1}]},"
+ "{\"name\":\"Build\",\"actions\":[{\"name\":\"BuildAction\",\"actionTypeId\":{\"category\":\"Build\","
+ "\"owner\":\"AWS\",\"provider\":\"CodeBuild\",\"version\":\"1\"},\"runOrder\":1}]}]}}";
+ "\"owner\":\"AWS\",\"provider\":\"CodeBuild\",\"version\":\"1\"},\"runOrder\":1}]}]}"
+ (tags == null ? "" : ",\"tags\":" + tags) + "}";
}
private static RequestSpecification tagged(RequestSpecification request) {