mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codepipeline): preserve and manage custom action type tags (#8049)
* fix(codepipeline): preserve and manage custom action type tags CreateCustomActionType copied its whole request into the stored action type, so its tags sat inside actionType instead of on the resource, and the response had no top-level tags. TagResource, UntagResource and ListTagsForResource only looked up pipeline ARNs, so an actiontype ARN answered ResourceNotFoundException and a custom action type could not be tagged. Keep the tags on the action type's stored item and return them at the top level of the CreateCustomActionType response, as the API model does. The three tag operations now resolve an actiontype ARN (actiontype:Owner/Category/Provider/Version) before falling back to a pipeline ARN. Action types saved before this change load with no tags. Fixes #7950 * fix(codepipeline): guard custom action type writes and cap merged tags CreateCustomActionType checked for an existing type before storing it, and the tag operations read, edited and stored a type's tag map, all without a lock. Two creates of one type could both succeed, a tag write could restore a type deleted meanwhile, and the map was edited in place while ListTagsForResource and the store serializer iterated it. Create, delete and tag writes of an action type now share a per-type lock, pipeline tag writes take the lock pipeline writes already use, and a tag write stores a new map instead of editing the stored one. TagResource checked only the size of the incoming tag list, so a resource with 50 tags accepted a 51st key. It now checks the merged set and answers TooManyTagsException, for pipelines too; replacing the value of an existing key at 50 tags still works.
This commit is contained in:
1 parent
3b4cae7387
commit
455aa4b80d
3 files changed
+219
-22
No files matched your search
@@ -22,6 +22,7 @@ import io.floci.aws.services.s3.model.S3Object;
|
||||
import io.floci.aws.testing.ThreadStates;
|
||||
import io.smallrye.config.SmallRyeConfigBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
@@ -32,9 +33,11 @@ import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
@@ -696,6 +699,122 @@ class CodePipelineServiceTest {
|
||||
assertEquals("Failed", summaries.get(0).path("status").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customActionTypeTagsAreResourceTagsOfItsArn() throws Exception {
|
||||
String arn = "arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/TagProvider/1";
|
||||
JsonNode created = handle("CreateCustomActionType", """
|
||||
{
|
||||
"category": "Build",
|
||||
"provider": "TagProvider",
|
||||
"version": "1",
|
||||
"inputArtifactDetails": {"minimumCount": 0, "maximumCount": 1},
|
||||
"outputArtifactDetails": {"minimumCount": 0, "maximumCount": 1},
|
||||
"tags": [{"key": "team", "value": "red"}]
|
||||
}
|
||||
""");
|
||||
|
||||
assertEquals(mapper.readTree("[{\"key\": \"team\", \"value\": \"red\"}]"), created.path("tags"));
|
||||
assertFalse(created.path("actionType").has("tags"));
|
||||
assertEquals(created.path("tags"),
|
||||
handle("ListTagsForResource", "{\"resourceArn\": \"" + arn + "\"}").path("tags"));
|
||||
|
||||
handle("TagResource", """
|
||||
{"resourceArn": "%s", "tags": [{"key": "env", "value": "dev"}]}
|
||||
""".formatted(arn));
|
||||
handle("UntagResource", """
|
||||
{"resourceArn": "%s", "tagKeys": ["team"]}
|
||||
""".formatted(arn));
|
||||
|
||||
assertEquals(mapper.readTree("[{\"key\": \"env\", \"value\": \"dev\"}]"),
|
||||
handle("ListTagsForResource", "{\"resourceArn\": \"" + arn + "\"}").path("tags"));
|
||||
AwsException missing = assertThrows(AwsException.class, () -> handle("ListTagsForResource",
|
||||
"{\"resourceArn\": \"" + arn.replace("TagProvider", "OtherProvider") + "\"}"));
|
||||
assertEquals("ResourceNotFoundException", missing.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceCountsTheTagsTheResourceAlreadyHas() throws Exception {
|
||||
String arn = "arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/FullProvider/1";
|
||||
handle("CreateCustomActionType", customActionTypeRequest("FullProvider", 50));
|
||||
|
||||
AwsException tooMany = assertThrows(AwsException.class, () -> handle("TagResource", """
|
||||
{"resourceArn": "%s", "tags": [{"key": "k50", "value": "v"}]}
|
||||
""".formatted(arn)));
|
||||
assertEquals("TooManyTagsException", tooMany.getErrorCode());
|
||||
handle("TagResource", """
|
||||
{"resourceArn": "%s", "tags": [{"key": "k0", "value": "changed"}]}
|
||||
""".formatted(arn));
|
||||
|
||||
JsonNode tags = handle("ListTagsForResource", "{\"resourceArn\": \"" + arn + "\"}").path("tags");
|
||||
assertEquals(50, tags.size());
|
||||
assertEquals("changed", tags.get(0).path("value").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondCreateOfACustomActionTypeWaitsForTheFirstAndIsRefused() throws Exception {
|
||||
String request = customActionTypeRequest("RaceProvider", 0);
|
||||
Future<JsonNode> second = handleWhileHoldingActionTypeLock("RaceProvider", "CreateCustomActionType",
|
||||
request, () -> handle("CreateCustomActionType", request));
|
||||
|
||||
ExecutionException refused = assertThrows(ExecutionException.class, second::get);
|
||||
assertEquals("ActionTypeAlreadyExistsException", ((AwsException) refused.getCause()).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagWriteWaitsForACustomActionTypeDeleteAndDoesNotRestoreIt() throws Exception {
|
||||
String arn = "arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/DeletedProvider/1";
|
||||
handle("CreateCustomActionType", customActionTypeRequest("DeletedProvider", 0));
|
||||
Future<JsonNode> tagged = handleWhileHoldingActionTypeLock("DeletedProvider", "TagResource", """
|
||||
{"resourceArn": "%s", "tags": [{"key": "env", "value": "dev"}]}
|
||||
""".formatted(arn), () -> handle("DeleteCustomActionType", """
|
||||
{"category": "Build", "provider": "DeletedProvider", "version": "1"}
|
||||
"""));
|
||||
|
||||
ExecutionException missing = assertThrows(ExecutionException.class, tagged::get);
|
||||
assertEquals("ResourceNotFoundException", ((AwsException) missing.getCause()).getErrorCode());
|
||||
AwsException deleted = assertThrows(AwsException.class,
|
||||
() -> handle("ListTagsForResource", "{\"resourceArn\": \"" + arn + "\"}"));
|
||||
assertEquals("ResourceNotFoundException", deleted.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends {@code operation} from another thread while this one holds the lock of the custom action
|
||||
* type Build/{@code provider}/1, and runs {@code meanwhile} once the request waits on it.
|
||||
*/
|
||||
private Future<JsonNode> handleWhileHoldingActionTypeLock(String provider, String operation, String body,
|
||||
Executable meanwhile) {
|
||||
String lockKey = ACCOUNT + ":" + REGION + ":action:Build:Custom:" + provider + ":1";
|
||||
try (ExecutorService requests = Executors.newSingleThreadExecutor()) {
|
||||
AtomicReference<Thread> requestThread = new AtomicReference<>();
|
||||
return service.actionTypeLocks.withLock(lockKey, () -> {
|
||||
Future<JsonNode> response = requests.submit(() -> {
|
||||
requestThread.set(Thread.currentThread());
|
||||
return handle(operation, body);
|
||||
});
|
||||
assertDoesNotThrow(() -> ThreadStates.awaitBlockedOrDone(requestThread, response));
|
||||
assertDoesNotThrow(meanwhile);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static String customActionTypeRequest(String provider, int tagCount) {
|
||||
StringJoiner tags = new StringJoiner(", ");
|
||||
for (int i = 0; i < tagCount; i++) {
|
||||
tags.add("{\"key\": \"k" + i + "\", \"value\": \"v\"}");
|
||||
}
|
||||
return """
|
||||
{
|
||||
"category": "Build",
|
||||
"provider": "%s",
|
||||
"version": "1",
|
||||
"inputArtifactDetails": {"minimumCount": 0, "maximumCount": 1},
|
||||
"outputArtifactDetails": {"minimumCount": 0, "maximumCount": 1},
|
||||
"tags": [%s]
|
||||
}
|
||||
""".formatted(provider, tags);
|
||||
}
|
||||
|
||||
private JsonNode handle(String action, String body) throws Exception {
|
||||
return service.handle(action, mapper.readTree(body), REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user