fix(codedeploy): preserve deployment group creation tags (#8025)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-11 11:08:16 -04:00
1 parent 195a036b63
commit d0d0e0eef8
12 files changed
+645 -102

No files matched your search

@@ -73,38 +73,34 @@ class TagOnCreateTest {
+ "&TagSpecification.item.1.Tag.1.Key=team")).orElseThrow().otherSpellings());
}
/**
* CodeDeploy's handler reads a tag as {@code Key} and {@code Value}, or as {@code key} and {@code value}, so both
* are the tags it applies; under any other case a tag is checked on the tagging action only.
*/
/** CodeDeploy reads its modeled {@code Key}/{@code Value} tags; other spellings stay on the tagging check. */
@Test
void aBodysTagsAreReadInTheCasesTheirHandlerReadsAndCheckedOnTheTaggingActionInAnyOther() {
for (String tag : List.of("{\"key\":\"team\",\"value\":\"red\"}", "{\"Key\":\"team\",\"Value\":\"red\"}")) {
Tagging tagging = TagOnCreate.of("codedeploy:CreateApplication", json(
"{\"applicationName\":\"demo\",\"tags\":[" + tag + "]}")).orElseThrow();
assertEquals(List.of(set(Map.of("team", "red"))), tagging.sets(), tag);
assertEquals(List.of(), tagging.otherSpellings(), tag);
void aBodysTagsAreReadInTheModeledCaseAndCheckedOnTheTaggingActionInAnyOther() {
for (String operation : List.of("CreateApplication", "CreateDeploymentGroup")) {
Tagging tagging = TagOnCreate.of("codedeploy:" + operation, json(
"{\"applicationName\":\"demo\",\"tags\":[{\"Key\":\"team\",\"Value\":\"red\"}]}")).orElseThrow();
assertEquals(List.of(set(Map.of("team", "red"))), tagging.sets(), operation);
assertEquals(List.of(), tagging.otherSpellings(), operation);
}
// Under any other case the handler reads neither, and keeps an empty tag; the tagging action is checked for both.
Tagging lower = TagOnCreate.of("codedeploy:CreateApplication", json(
"{\"applicationName\":\"demo\",\"tags\":[{\"key\":\"team\",\"value\":\"red\"}]}")).orElseThrow();
assertEquals(List.of(), lower.sets());
assertEquals(List.of(set(Map.of("team", "red"))), lower.otherSpellings());
Tagging upper = TagOnCreate.of("codedeploy:CreateApplication", json(
"{\"applicationName\":\"demo\",\"tags\":[{\"KEY\":\"team\",\"VALUE\":\"red\"}]}")).orElseThrow();
assertEquals(List.of(set(Map.of("", ""))), upper.sets());
assertEquals(List.of(set(Map.of("", "")), set(Map.of("team", "red"))), upper.checked());
// Only CodeDeploy's handler reads a lowercase key where the SDK sends Key.
assertEquals(List.of(), upper.sets());
assertEquals(List.of(set(Map.of("team", "red"))), upper.otherSpellings());
assertEquals(List.of(), sets("dynamodb:CreateTable", json(
"{\"TableName\":\"t\",\"Tags\":[{\"key\":\"team\",\"value\":\"red\"}]}")));
}
/**
* CodeDeploy's handler iterates its tags member whatever it holds, so an object's values are tags too; a deny on
* one holds on the own action and the tagging action alike.
*/
/** A non-list CodeDeploy tags member is not applied, though its contents remain visible to the safety check. */
@Test
void anObjectsValuesAreTagsWhereTheHandlerIteratesIt() {
void anObjectsValuesAreOnlyOtherSpellingsForCodeDeploy() {
Tagging object = TagOnCreate.of("codedeploy:CreateApplication", json("{\"applicationName\":\"demo\","
+ "\"tags\":{\"0\":{\"key\":\"team\",\"value\":\"red\"}}}")).orElseThrow();
assertEquals(List.of(set(Map.of("team", "red"))), object.sets());
assertEquals(List.of("red"), TagOnCreate.requestTagContexts(object).getFirst().get("aws:RequestTag/team"));
assertEquals(List.of(), object.sets());
assertTrue(object.otherSpellings().contains(set(Map.of("team", "red"))));
// Elsewhere it is checked on the tagging action and with the own action, beside no tags.
Tagging elsewhere = TagOnCreate.of("dynamodb:CreateTable", json("{\"TableName\":\"t\","
+ "\"Tags\":{\"0\":{\"Key\":\"team\",\"Value\":\"red\"}}}")).orElseThrow();
@@ -133,16 +129,9 @@ class TagOnCreateTest {
"{\"Tags\":{\"team\":\"red\"}}")).orElseThrow().otherSpellings());
}
/**
* CodeDeploy's handler reads a tag's key and value as text whatever they hold, a missing key as an empty one,
* and so does the check; other handlers' keys are read as text when they are a number or a flag too.
*/
/** Some handlers read non-string tag fields as text, and the check follows their current behavior. */
@Test
void aTagKeyThatIsNoStringIsReadAsText() {
assertEquals(List.of(set(Map.of("123", "red"))), sets("codedeploy:CreateApplication", json(
"{\"applicationName\":\"demo\",\"tags\":[{\"key\":123,\"value\":\"red\"}]}")));
assertEquals(List.of(set(Map.of("", "red"))), sets("codedeploy:CreateApplication", json(
"{\"applicationName\":\"demo\",\"tags\":[{\"value\":\"red\"}]}")));
assertEquals(List.of(set(Map.of("true", "1"))), sets("dynamodb:CreateTable", json(
"{\"TableName\":\"t\",\"Tags\":[{\"Key\":true,\"Value\":1}]}")));
// Under any other spelling, every element of a tag list is checked on the tagging action.
@@ -86,7 +86,7 @@ class CodeDeployHookTimeoutTest {
ssm, mock(Ec2Service.class), new ObjectMapper(),
new RegionResolver(region, "000000000000"), null);
service.createApplication(region, "app", "Server", null);
service.createDeploymentGroup(region, "app", "group", null, "role", null);
service.createDeploymentGroup(region, "app", "group", null, "role", null, null);
return service;
}
}
@@ -2,6 +2,7 @@ package io.floci.aws.services.codedeploy;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
@@ -43,7 +44,8 @@ class CodeDeployServicePersistenceTest {
first.createApplication(REGION, "web-app", "Server",
List.of(Map.of("Key", "team", "Value", "platform")));
first.createDeploymentGroup(REGION, "web-app", "prod-group",
"CodeDeployDefault.OneAtATime", "arn:aws:iam::000000000000:role/cd", null);
"CodeDeployDefault.OneAtATime", "arn:aws:iam::000000000000:role/cd", null,
List.of(Map.of("Key", "owner", "Value", "release")));
first.createDeploymentConfig(REGION, "custom-cfg",
Map.of("type", "HOST_COUNT", "value", 1), "Server", null, null);
first.registerOnPremisesInstance(REGION, "onprem-1",
@@ -67,6 +69,10 @@ class CodeDeployServicePersistenceTest {
.stream().collect(Collectors.toMap(t -> t.get("Key"), t -> t.get("Value")));
assertEquals("prod", appTags.get("env")); // tagResource
assertEquals("platform", appTags.get("team")); // createApplication tags
Map<String, String> groupTags = reloaded.listTagsForResource(
first.deploymentGroupArn(REGION, "web-app", "prod-group")).stream()
.collect(Collectors.toMap(t -> t.get("Key"), t -> t.get("Value")));
assertEquals("release", groupTags.get("owner"));
}
@Test
@@ -76,8 +82,8 @@ class CodeDeployServicePersistenceTest {
CodeDeployService first = serviceWithStorage(storage);
first.createApplication(REGION, "keep-app", "Server", null);
first.createApplication(REGION, "drop-app", "Server", null);
first.createDeploymentGroup(REGION, "keep-app", "g1", null, "arn:r", null);
first.createDeploymentGroup(REGION, "keep-app", "g2", null, "arn:r", null);
first.createDeploymentGroup(REGION, "keep-app", "g1", null, "arn:r", null, null);
first.createDeploymentGroup(REGION, "keep-app", "g2", null, "arn:r", null, null);
first.deleteApplication(REGION, "drop-app");
first.deleteDeploymentGroup(REGION, "keep-app", "g2");
String arn = first.applicationArn(REGION, "keep-app");
@@ -96,6 +102,66 @@ class CodeDeployServicePersistenceTest {
assertTrue(!tags.containsKey("team"), "untagged key must not reappear after restart");
}
@Test
void taggedDeploymentGroupKeepsTagsWhenRenamed() {
SharedStorageFactory storage = new SharedStorageFactory();
CodeDeployService service = serviceWithStorage(storage);
service.createApplication(REGION, "rename-app", "Server", null);
service.createDeploymentGroup(REGION, "rename-app", "old-group", null, "arn:r", null, null);
String originalArn = service.deploymentGroupArn(REGION, "rename-app", "old-group");
service.tagResource(originalArn, List.of(Map.of("Key", "team", "Value", "red")));
service.updateDeploymentGroup(REGION, "rename-app", "old-group", "new-group", null, null, null);
CodeDeployService reloaded = serviceWithStorage(storage);
String renamedArn = reloaded.deploymentGroupArn(REGION, "rename-app", "new-group");
assertEquals(List.of(Map.of("Key", "team", "Value", "red")), reloaded.listTagsForResource(renamedArn));
assertEquals(List.of(), reloaded.listTagsForResource(originalArn));
}
@Test
void renamingDeploymentGroupToExistingNamePreservesBothGroupsAndTags() {
SharedStorageFactory storage = new SharedStorageFactory();
CodeDeployService service = serviceWithStorage(storage);
service.createApplication(REGION, "rename-conflict-app", "Server", null);
DeploymentGroup source = service.createDeploymentGroup(REGION, "rename-conflict-app", "source",
null, "arn:source-role", null, List.of(Map.of("Key", "team", "Value", "red")));
DeploymentGroup destination = service.createDeploymentGroup(REGION, "rename-conflict-app", "destination",
null, "arn:destination-role", null, List.of(Map.of("Key", "team", "Value", "blue")));
String sourceArn = service.deploymentGroupArn(REGION, "rename-conflict-app", "source");
String destinationArn = service.deploymentGroupArn(REGION, "rename-conflict-app", "destination");
AwsException error = assertThrows(AwsException.class, () -> service.updateDeploymentGroup(
REGION, "rename-conflict-app", "source", "destination", null, "arn:changed-role", null));
assertEquals("DeploymentGroupAlreadyExistsException", error.getErrorCode());
CodeDeployService reloaded = serviceWithStorage(storage);
assertEquals(source.getDeploymentGroupId(),
reloaded.getDeploymentGroup(REGION, "rename-conflict-app", "source").getDeploymentGroupId());
assertEquals(destination.getDeploymentGroupId(),
reloaded.getDeploymentGroup(REGION, "rename-conflict-app", "destination").getDeploymentGroupId());
assertEquals("arn:source-role",
reloaded.getDeploymentGroup(REGION, "rename-conflict-app", "source").getServiceRoleArn());
assertEquals(List.of(Map.of("Key", "team", "Value", "red")), reloaded.listTagsForResource(sourceArn));
assertEquals(List.of(Map.of("Key", "team", "Value", "blue")), reloaded.listTagsForResource(destinationArn));
}
@Test
void deletedDeploymentGroupDoesNotLeaveTagsForRecreatedName() {
SharedStorageFactory storage = new SharedStorageFactory();
CodeDeployService service = serviceWithStorage(storage);
service.createApplication(REGION, "recreate-app", "Server", null);
service.createDeploymentGroup(REGION, "recreate-app", "group", null, "arn:r", null, null);
String arn = service.deploymentGroupArn(REGION, "recreate-app", "group");
service.tagResource(arn, List.of(Map.of("Key", "team", "Value", "red")));
service.deleteDeploymentGroup(REGION, "recreate-app", "group");
service.createDeploymentGroup(REGION, "recreate-app", "group", null, "arn:r", null, null);
CodeDeployService reloaded = serviceWithStorage(storage);
assertEquals(List.of(), reloaded.listTagsForResource(arn));
}
private static CodeDeployService serviceWithStorage(StorageFactory storage) {
CodeDeployService service = new CodeDeployService(
mock(LambdaService.class), mock(EcsService.class), mock(ElbV2Service.class),
@@ -20,6 +20,7 @@ import java.time.Clock;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -66,6 +67,51 @@ class CodeDeployServiceTest {
return svc;
}
@Test
void onPremisesTagFilterTypesMatchOnlyRequestedTags() {
for (String name : List.of("red-team", "blue-team", "red-env", "untagged")) {
service.registerOnPremisesInstance(REGION, name, null, null);
}
service.addTagsToOnPremisesInstances(REGION, List.of("red-team"),
List.of(Map.of("Key", "team", "Value", "red")));
service.addTagsToOnPremisesInstances(REGION, List.of("blue-team"),
List.of(Map.of("Key", "team", "Value", "blue")));
service.addTagsToOnPremisesInstances(REGION, List.of("red-env"),
List.of(Map.of("Key", "env", "Value", "red")));
assertEquals(Set.of("red-team", "red-env"), Set.copyOf(service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Value", "red", "Type", "VALUE_ONLY")))));
assertEquals(Set.of("red-team", "red-env"), Set.copyOf(service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Key", "ignored", "Value", "red", "Type", "VALUE_ONLY")))));
assertEquals(Set.of("red-team", "blue-team"), Set.copyOf(service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Key", "team", "Value", "ignored", "Type", "KEY_ONLY")))));
assertEquals(List.of("red-team"), service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Key", "team", "Value", "red", "Type", "KEY_AND_VALUE"))));
assertEquals(List.of(), service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Key", "team", "Type", "KEY_AND_VALUE"))));
assertEquals(List.of(), service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Value", "red", "Type", "KEY_AND_VALUE"))));
assertEquals(List.of("red-team"), service.listOnPremisesInstances(REGION, null,
List.of(Map.of("Key", "team", "Value", "red"))));
}
@Test
void keylessUntypedOnPremisesFilterStillSelectsRegisteredDeploymentTargets() {
service.registerOnPremisesInstance(REGION, "matching-onprem", null, null);
service.registerOnPremisesInstance(REGION, "untagged-onprem", null, null);
service.addTagsToOnPremisesInstances(REGION, List.of("matching-onprem"),
List.of(Map.of("Key", "team", "Value", "red")));
service.createApplication(REGION, "keyless-filter-app", "Server", null);
service.createDeploymentGroup(REGION, "keyless-filter-app", "group", null, ROLE_ARN,
Map.of("onPremisesInstanceTagFilters", List.of(Map.of("Value", "red"))), null);
String deploymentId = service.createDeployment(REGION, "keyless-filter-app", "group", null,
Map.of("appSpecContent", Map.of("content", "os: linux")), null);
assertEquals(Set.of("matching-onprem", "untagged-onprem"),
Set.copyOf(service.listDeploymentTargets(REGION, deploymentId)));
}
// ---- Server platform (SSM) hooks ---------------------------------------------------
@Test
@@ -223,7 +269,7 @@ class CodeDeployServiceTest {
CodeDeployService shortTimeoutService = newService(Duration.ofSeconds(1));
shortTimeoutService.createApplication(REGION, "app-hook-timeout", "Lambda", null);
shortTimeoutService.createDeploymentGroup(REGION, "app-hook-timeout", "group-hook-timeout",
"CodeDeployDefault.LambdaAllAtOnce", ROLE_ARN, null);
"CodeDeployDefault.LambdaAllAtOnce", ROLE_ARN, null, null);
InvokeResult cleanInvoke = new InvokeResult(200, null, new byte[0], null, "req-3");
when(lambdaService.invoke(eq(REGION), eq("beforeHook"), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(cleanInvoke);
@@ -328,7 +374,7 @@ class CodeDeployServiceTest {
private String createServerDeployment(CodeDeployService target, String appName, String groupName,
int timeoutSeconds) {
target.createApplication(REGION, appName, "Server", null);
target.createDeploymentGroup(REGION, appName, groupName, "CodeDeployDefault.AllAtOnce", ROLE_ARN, null);
target.createDeploymentGroup(REGION, appName, groupName, "CodeDeployDefault.AllAtOnce", ROLE_ARN, null, null);
String appSpec = """
os: linux
hooks:
@@ -344,7 +390,7 @@ class CodeDeployServiceTest {
private void createLambdaAppAndGroup(String appName, String groupName) {
service.createApplication(REGION, appName, "Lambda", null);
service.createDeploymentGroup(REGION, appName, groupName,
"CodeDeployDefault.LambdaAllAtOnce", ROLE_ARN, null);
"CodeDeployDefault.LambdaAllAtOnce", ROLE_ARN, null, null);
}
private String createLambdaDeployment(String appName, String groupName, String beforeHook, String afterHook) {
@@ -0,0 +1,58 @@
package io.floci.aws.services.codedeploy;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class CodeDeployTagFilterHandlerTest {
@Test
void optionalTagFilterMembersRemainAbsent() throws Exception {
CodeDeployService service = mock(CodeDeployService.class);
when(service.listOnPremisesInstances(eq("us-east-1"), isNull(), anyList())).thenReturn(List.of());
ObjectMapper mapper = new ObjectMapper();
CodeDeployJsonHandler handler = new CodeDeployJsonHandler(service, mapper);
handler.handle("ListOnPremisesInstances", mapper.readTree("""
{"tagFilters":[{"Key":"team"},{"Value":"red"},{"Type":"KEY_ONLY"}]}
"""), "us-east-1");
verify(service).listOnPremisesInstances(eq("us-east-1"), isNull(),
eq(List.of(Map.of("Key", "team"), Map.of("Value", "red"), Map.of("Type", "KEY_ONLY"))));
}
@Test
void explicitNullTagFiltersAreTreatedAsAbsent() throws Exception {
CodeDeployService service = mock(CodeDeployService.class);
when(service.listOnPremisesInstances(eq("us-east-1"), isNull(), anyList())).thenReturn(List.of());
ObjectMapper mapper = new ObjectMapper();
CodeDeployJsonHandler handler = new CodeDeployJsonHandler(service, mapper);
handler.handle("ListOnPremisesInstances", mapper.readTree("{\"tagFilters\":null}"), "us-east-1");
verify(service).listOnPremisesInstances(eq("us-east-1"), isNull(), eq(List.of()));
}
@Test
void nullTagFilterMembersAreTreatedAsAbsent() throws Exception {
CodeDeployService service = mock(CodeDeployService.class);
when(service.listOnPremisesInstances(eq("us-east-1"), isNull(), anyList())).thenReturn(List.of());
ObjectMapper mapper = new ObjectMapper();
CodeDeployJsonHandler handler = new CodeDeployJsonHandler(service, mapper);
handler.handle("ListOnPremisesInstances", mapper.readTree("""
{"tagFilters":[{"Key":null,"Value":null,"Type":null}]}
"""), "us-east-1");
verify(service).listOnPremisesInstances(eq("us-east-1"), isNull(), eq(List.of(Map.of())));
}
}
@@ -0,0 +1,300 @@
package io.floci.aws.services.codedeploy;
import io.floci.aws.testing.PartitionCleanup;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
@QuarkusTest
class CodeDeployTagsIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void createDeploymentGroupStoresItsTags() {
String application = "tag-app-7949-" + UUID.randomUUID();
String group = "tag-dg-7949-" + UUID.randomUUID();
String groupArn = "arn:aws:codedeploy:us-east-1:000000000000:deploymentgroup:"
+ application + "/" + group;
deleteApplicationLater(application);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
deleteDeploymentGroupLater(application, group);
untagLater(groupArn);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateDeploymentGroup")
.contentType(CONTENT_TYPE)
.body("""
{
"applicationName": "%s",
"deploymentGroupName": "%s",
"serviceRoleArn": "arn:aws:iam::000000000000:role/codedeploy",
"tags": [{"Key": "team", "Value": "red"}]
}
""".formatted(application, group))
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("{\"ResourceArn\":\"" + groupArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(1))
.body("Tags[0].Key", equalTo("team"))
.body("Tags[0].Value", equalTo("red"));
}
@Test
void explicitNullTagsRemainOptional() {
String application = "null-tag-app-7949-" + UUID.randomUUID();
String group = "null-tag-group-7949-" + UUID.randomUUID();
deleteApplicationLater(application);
deleteDeploymentGroupLater(application, group);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\",\"tags\":null}")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateDeploymentGroup")
.contentType(CONTENT_TYPE)
.body("""
{
"applicationName": "%s",
"deploymentGroupName": "%s",
"serviceRoleArn": "arn:aws:iam::000000000000:role/codedeploy",
"tags": null
}
""".formatted(application, group))
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void createApplicationRejectsUnmodeledTagShapes() {
String[] invalidTags = {
"[{\"key\":\"team\",\"value\":\"red\"}]",
"[{\"Key\":123,\"Value\":\"red\"}]",
"[{\"Key\":\"team\",\"Value\":true}]",
"{\"0\":{\"Key\":\"team\",\"Value\":\"red\"}}"
};
for (int i = 0; i < invalidTags.length; i++) {
String application = "invalid-tag-app-7949-" + UUID.randomUUID();
String applicationArn = "arn:aws:codedeploy:us-east-1:000000000000:application:" + application;
deleteApplicationLater(application);
untagLater(applicationArn);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\",\"tags\":" + invalidTags[i] + "}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidTagsToAddException"));
given()
.header("X-Amz-Target", "CodeDeploy_20141006.GetApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ApplicationDoesNotExistException"));
given()
.header("X-Amz-Target", "CodeDeploy_20141006.ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("{\"ResourceArn\":\"" + applicationArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(0));
}
}
@Test
void createDeploymentGroupAndTagResourceRejectUnmodeledTagShapes() {
String application = "invalid-tag-group-app-7949-" + UUID.randomUUID();
String group = "invalid-tag-group-7949-" + UUID.randomUUID();
String applicationArn = "arn:aws:codedeploy:us-east-1:000000000000:application:" + application;
String groupArn = "arn:aws:codedeploy:us-east-1:000000000000:deploymentgroup:"
+ application + "/" + group;
deleteApplicationLater(application);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
deleteDeploymentGroupLater(application, group);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.CreateDeploymentGroup")
.contentType(CONTENT_TYPE)
.body("""
{
"applicationName": "%s",
"deploymentGroupName": "%s",
"serviceRoleArn": "arn:aws:iam::000000000000:role/codedeploy",
"tags": {"0": {"Key": "team", "Value": "red"}}
}
""".formatted(application, group))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidTagsToAddException"));
given()
.header("X-Amz-Target", "CodeDeploy_20141006.GetDeploymentGroup")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application
+ "\",\"deploymentGroupName\":\"" + group + "\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("DeploymentGroupDoesNotExistException"));
given()
.header("X-Amz-Target", "CodeDeploy_20141006.ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("{\"ResourceArn\":\"" + groupArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(0));
untagLater(applicationArn);
given()
.header("X-Amz-Target", "CodeDeploy_20141006.TagResource")
.contentType(CONTENT_TYPE)
.body("""
{
"ResourceArn": "%s",
"Tags": [{"key": "team", "value": "red"}]
}
""".formatted(applicationArn))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidTagsToAddException"));
given()
.header("X-Amz-Target", "CodeDeploy_20141006.ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("{\"ResourceArn\":\"" + applicationArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(0));
}
@Test
void listOnPremisesInstancesRejectsObjectTagFilters() {
assertMalformed("ListOnPremisesInstances",
"{\"tagFilters\":{\"0\":{\"Key\":\"team\",\"Value\":\"red\"}}}",
"InvalidTagFilterException");
}
@Test
void listOnPremisesInstancesRejectsUnknownFilterType() {
assertMalformed("ListOnPremisesInstances",
"{\"tagFilters\":[{\"Key\":\"team\",\"Type\":\"UNKNOWN\"}]}",
"InvalidTagFilterException");
}
@Test
void addTagsToOnPremisesInstancesRejectsObjectTags() {
assertMalformed("AddTagsToOnPremisesInstances",
"{\"instanceNames\":[],\"tags\":{\"0\":{\"Key\":\"team\",\"Value\":\"red\"}}}",
"InvalidTagException");
}
@Test
void removeTagsFromOnPremisesInstancesRejectsObjectTags() {
assertMalformed("RemoveTagsFromOnPremisesInstances",
"{\"instanceNames\":[],\"tags\":{\"0\":{\"Key\":\"team\",\"Value\":\"red\"}}}",
"InvalidTagException");
}
private void assertMalformed(String action, String body, String expectedType) {
given()
.header("X-Amz-Target", "CodeDeploy_20141006." + action)
.contentType(CONTENT_TYPE)
.body(body)
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo(expectedType));
}
private void deleteApplicationLater(String application) {
cleanup.register(() -> given()
.header("X-Amz-Target", "CodeDeploy_20141006.DeleteApplication")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\"}")
.post("/"));
}
private void deleteDeploymentGroupLater(String application, String group) {
cleanup.register(() -> given()
.header("X-Amz-Target", "CodeDeploy_20141006.DeleteDeploymentGroup")
.contentType(CONTENT_TYPE)
.body("{\"applicationName\":\"" + application + "\",\"deploymentGroupName\":\"" + group + "\"}")
.post("/"));
}
private void untagLater(String arn) {
cleanup.register(() -> given()
.header("X-Amz-Target", "CodeDeploy_20141006.UntagResource")
.contentType(CONTENT_TYPE)
.body("{\"ResourceArn\":\"" + arn + "\",\"TagKeys\":[\"team\",\"123\",\"\"]}")
.post("/"));
}
}
@@ -504,22 +504,15 @@ class IamTagOnCreateEnforcementIntegrationTest {
.body("TagList.size()", equalTo(2));
}
/**
* CodeDeploy's handler reads a tag's key and value in either case, so the tagging action is checked for both,
* and the create's own action sees them.
*/
/** CodeDeploy's modeled tags need TagResource and are visible to the create's own condition keys. */
@Test
void anApplicationsTagsInEitherCaseNeedTagResource() {
void anApplicationsTagsNeedTagResource() {
User user = user("tag-application");
String application = "tag-app-" + suffix();
String create = "{\"applicationName\":\"" + application + "\",\"tags\":[{\"key\":\"team\",\"value\":\"red\"}]}";
String create = "{\"applicationName\":\"" + application + "\",\"tags\":[{\"Key\":\"team\",\"Value\":\"red\"}]}";
user.policy(allow("codedeploy:CreateApplication"));
refusedJson(user.json("codedeploy", "CodeDeploy_20141006.CreateApplication", "1.1").body(create).post("/").then(),
"codedeploy:TagResource");
// The handler keeps a key that is a number as its text, so it needs the tagging action too.
refusedJson(user.json("codedeploy", "CodeDeploy_20141006.CreateApplication", "1.1")
.body("{\"applicationName\":\"" + application + "\",\"tags\":[{\"key\":123,\"value\":\"red\"}]}")
.post("/").then(), "codedeploy:TagResource");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"codedeploy:CreateApplication\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"}}}," + allow("codedeploy:TagResource"));
@@ -527,14 +520,50 @@ class IamTagOnCreateEnforcementIntegrationTest {
.statusCode(200);
cleanup.register(() -> admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.DeleteApplication")
.contentType("application/x-amz-json-1.1").body("{\"applicationName\":\"" + application + "\"}").post("/"));
cleanup.register(() -> admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.UntagResource")
.contentType("application/x-amz-json-1.1")
.body("{\"ResourceArn\":\"arn:aws:codedeploy:us-east-1:000000000000:application:" + application
+ "\",\"TagKeys\":[\"team\"]}").post("/"));
}
// The handler iterates an object's values as tags, so a deny on one holds however they are shaped.
user.policy(allow("codedeploy:CreateApplication") + "," + allow("codedeploy:TagResource")
+ ",{\"Effect\":\"Deny\",\"Action\":\"codedeploy:CreateApplication\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"}}}");
refusedJson(user.json("codedeploy", "CodeDeploy_20141006.CreateApplication", "1.1")
.body("{\"applicationName\":\"" + application + "-object\",\"tags\":{\"0\":{\"key\":\"team\",\"value\":\"red\"}}}")
.post("/").then(), "codedeploy:CreateApplication");
@Test
void aDeploymentGroupsTagsNeedTagResource() {
User user = user("tag-deployment-group");
String application = "tag-group-app-" + suffix();
String group = "tag-group-" + suffix();
String groupArn = "arn:aws:codedeploy:us-east-1:000000000000:deploymentgroup:"
+ application + "/" + group;
cleanup.register(() -> admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.DeleteApplication")
.contentType("application/x-amz-json-1.1").body("{\"applicationName\":\"" + application + "\"}").post("/"));
admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.CreateApplication")
.contentType("application/x-amz-json-1.1")
.body("{\"applicationName\":\"" + application + "\"}").post("/").then().statusCode(200);
cleanup.register(() -> admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.DeleteDeploymentGroup")
.contentType("application/x-amz-json-1.1")
.body("{\"applicationName\":\"" + application + "\",\"deploymentGroupName\":\"" + group + "\"}")
.post("/"));
cleanup.register(() -> admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.UntagResource")
.contentType("application/x-amz-json-1.1")
.body("{\"ResourceArn\":\"" + groupArn + "\",\"TagKeys\":[\"team\"]}").post("/"));
String create = """
{"applicationName":"%s","deploymentGroupName":"%s",
"serviceRoleArn":"arn:aws:iam::000000000000:role/codedeploy",
"tags":[{"Key":"team","Value":"red"}]}
""".formatted(application, group);
user.policy(allow("codedeploy:CreateDeploymentGroup") + "," + allow("iam:PassRole"));
refusedJson(user.json("codedeploy", "CodeDeploy_20141006.CreateDeploymentGroup", "1.1")
.body(create).post("/").then(), "codedeploy:TagResource");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"codedeploy:CreateDeploymentGroup\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"}}},"
+ allow("codedeploy:TagResource") + "," + allow("iam:PassRole"));
user.json("codedeploy", "CodeDeploy_20141006.CreateDeploymentGroup", "1.1")
.body(create).post("/").then().statusCode(200);
admin("codedeploy").header("X-Amz-Target", "CodeDeploy_20141006.ListTagsForResource")
.contentType("application/x-amz-json-1.1")
.body("{\"ResourceArn\":\"" + groupArn + "\"}").post("/").then().statusCode(200)
.body("Tags.find { it.Key == 'team' }.Value", equalTo("red"));
}
@Test