Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): store omitted tag values as empty strings (#3880)

Priamo Germosen052da935151f
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java
index 3420e07ef6..3e39f637f9 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java
@@ -3,6 +3,7 @@ package com.floci.test;
import org.junit.jupiter.api.*;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.*;
+import software.amazon.awssdk.services.ec2.model.Tag;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@@ -391,12 +392,30 @@ class Ec2Tests {
.vpcId(vpcId)
.cidrBlock("10.0.1.0/24")
.availabilityZone("us-east-1a")
+ .tagSpecifications(
+ TagSpecification.builder()
+ .resourceType(ResourceType.SUBNET)
+ .tags(Tag.builder().key("example.io:managed-by").value("sdk-test").build())
+ .build(),
+ TagSpecification.builder()
+ .resourceType(ResourceType.SUBNET)
+ .tags(
+ Tag.builder().key("Name").value("sdk-test-subnet").build(),
+ Tag.builder().key("omitted-value").build(),
+ Tag.builder().key("explicit-empty-value").value("").build())
+ .build())
.build());
subnetId = resp.subnet().subnetId();
assertThat(subnetId).isNotNull().startsWith("subnet-");
assertThat(resp.subnet().vpcId()).isEqualTo(vpcId);
assertThat(resp.subnet().cidrBlock()).isEqualTo("10.0.1.0/24");
+ assertThat(resp.subnet().tags()).extracting(Tag::key, Tag::value)
+ .containsExactlyInAnyOrder(
+ tuple("example.io:managed-by", "sdk-test"),
+ tuple("Name", "sdk-test-subnet"),
+ tuple("omitted-value", ""),
+ tuple("explicit-empty-value", ""));
}
@Test
@@ -408,6 +427,21 @@ class Ec2Tests {
assertThat(resp.subnets()).hasSize(1);
assertThat(resp.subnets().get(0).subnetId()).isEqualTo(subnetId);
+ assertThat(resp.subnets().get(0).tags()).extracting(Tag::key, Tag::value)
+ .containsExactlyInAnyOrder(
+ tuple("example.io:managed-by", "sdk-test"),
+ tuple("Name", "sdk-test-subnet"),
+ tuple("omitted-value", ""),
+ tuple("explicit-empty-value", ""));
+ assertThat(ec2.describeTags(DescribeTagsRequest.builder()
+ .filters(Filter.builder().name("resource-id").values(subnetId).build())
+ .build()).tags())
+ .extracting(TagDescription::key, TagDescription::value)
+ .containsExactlyInAnyOrder(
+ tuple("example.io:managed-by", "sdk-test"),
+ tuple("Name", "sdk-test-subnet"),
+ tuple("omitted-value", ""),
+ tuple("explicit-empty-value", ""));
}
@Test
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
index e345b6a8cd..4324d3be7e 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
@@ -518,7 +518,7 @@ public class Ec2QueryHandler {
String key = p.getFirst(prefix + "." + i + ".Tag." + j + ".Key");
if (key == null) break;
String value = p.getFirst(prefix + "." + i + ".Tag." + j + ".Value");
- tags.add(new Tag(key, value));
+ tags.add(creationTag(key, value));
}
}
}
@@ -728,7 +728,7 @@ public class Ec2QueryHandler {
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
if (k == null) break;
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
- target.add(new Tag(k, v));
+ target.add(creationTag(k, v));
}
}
}
@@ -1465,7 +1465,7 @@ public class Ec2QueryHandler {
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
if (k == null) break;
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
- vpcTags.add(new Tag(k, v));
+ vpcTags.add(creationTag(k, v));
}
}
}
@@ -3366,6 +3366,12 @@ public class Ec2QueryHandler {
// ─── Tag handlers ─────────────────────────────────────────────────────────
+ // AWS stores a tag created without a Value as an empty string. DeleteTags is the exception:
+ // there an omitted value means "any value", so it builds its Tag objects directly.
+ private static Tag creationTag(String key, String value) {
+ return new Tag(key, value == null ? "" : value);
+ }
+
private Response handleCreateTags(MultivaluedMap<String, String> p, String region) {
List<String> resourceIds = getList(p, "ResourceId");
List<Tag> tagList = new ArrayList<>();
@@ -3373,7 +3379,7 @@ public class Ec2QueryHandler {
String k = p.getFirst("Tag." + i + ".Key");
if (k == null) break;
String v = p.getFirst("Tag." + i + ".Value");
- tagList.add(new Tag(k, v));
+ tagList.add(creationTag(k, v));
}
service.createTags(region, resourceIds, tagList);
return booleanResponse("CreateTags");
@@ -5351,7 +5357,7 @@ public class Ec2QueryHandler {
if (key == null) {
break;
}
- tagList.add(new Tag(key, p.getFirst(base + ".Tag." + j + ".Value")));
+ tagList.add(creationTag(key, p.getFirst(base + ".Tag." + j + ".Value")));
}
specs.add(new LaunchTemplateData.TagSpecification(resourceType, tagList));
}
@@ -5573,7 +5579,7 @@ public class Ec2QueryHandler {
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
if (k == null) break;
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
- volumeTags.add(new Tag(k, v));
+ volumeTags.add(creationTag(k, v));
}
}
}
@@ -5752,14 +5758,14 @@ public class Ec2QueryHandler {
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
if (k == null) break;
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
- spotRequestTags.add(new Tag(k, v));
+ spotRequestTags.add(creationTag(k, v));
}
} else if ("instance".equals(resType)) {
for (int j = 1; ; j++) {
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
if (k == null) break;
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
- instanceTags.add(new Tag(k, v));
+ instanceTags.add(creationTag(k, v));
}
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
index 7ea4d19f7a..4001e58466 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
@@ -4510,9 +4510,20 @@ class Ec2IntegrationTest {
.formParam("TagSpecification.1.ResourceType", "subnet")
.formParam("TagSpecification.1.Tag.1.Key", "Name")
.formParam("TagSpecification.1.Tag.1.Value", "tagged-subnet")
+ .formParam("TagSpecification.2.ResourceType", "subnet")
+ .formParam("TagSpecification.2.Tag.1.Key", "omitted-value")
+ .formParam("TagSpecification.2.Tag.2.Key", "explicit-empty-value")
+ .formParam("TagSpecification.2.Tag.2.Value", "")
.header("Authorization", AUTH_HEADER)
.when().post("/")
- .then().statusCode(200)
+ .then()
+ .statusCode(200)
+ .body("CreateSubnetResponse.subnet.tagSet.item.find { it.key == 'Name' }.value",
+ equalTo("tagged-subnet"))
+ .body("CreateSubnetResponse.subnet.tagSet.item.find { it.key == 'omitted-value' }.value",
+ equalTo(""))
+ .body("CreateSubnetResponse.subnet.tagSet.item.find { it.key == 'explicit-empty-value' }.value",
+ equalTo(""))
.extract().path("CreateSubnetResponse.subnet.subnetId");
given()
@@ -4522,8 +4533,27 @@ class Ec2IntegrationTest {
.when().post("/")
.then()
.statusCode(200)
- .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.key", equalTo("Name"))
- .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.value", equalTo("tagged-subnet"));
+ .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.find { it.key == 'Name' }.value",
+ equalTo("tagged-subnet"))
+ .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.find { it.key == 'omitted-value' }.value",
+ equalTo(""))
+ .body("DescribeSubnetsResponse.subnetSet.item.tagSet.item.find { it.key == 'explicit-empty-value' }.value",
+ equalTo(""));
+
+ given()
+ .formParam("Action", "DescribeTags")
+ .formParam("Filter.1.Name", "resource-id")
+ .formParam("Filter.1.Value.1", subnet)
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/")
+ .then()
+ .statusCode(200)
+ .body("DescribeTagsResponse.tagSet.item.find { it.key == 'Name' }.value",
+ equalTo("tagged-subnet"))
+ .body("DescribeTagsResponse.tagSet.item.find { it.key == 'omitted-value' }.value",
+ equalTo(""))
+ .body("DescribeTagsResponse.tagSet.item.find { it.key == 'explicit-empty-value' }.value",
+ equalTo(""));
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java
new file mode 100644
index 0000000000..56720db4c2
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java
@@ -0,0 +1,119 @@
+package io.github.hectorvent.floci.services.ec2;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.ec2.model.Subnet;
+import io.github.hectorvent.floci.services.ec2.model.Tag;
+import io.github.hectorvent.floci.services.ec2.model.Vpc;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.MultivaluedMap;
+import jakarta.ws.rs.core.Response;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.util.List;
+
+class Ec2QueryHandlerTest {
+
+ @Test
+ void normalizesValuelessCreateSubnetTagsBeforeMutation() {
+ Ec2Service service = mock(Ec2Service.class);
+ Subnet subnet = new Subnet();
+ subnet.setSubnetId("subnet-test");
+ when(service.createSubnet("us-east-1", "vpc-test", "10.38.1.0/24", null, null, null))
+ .thenReturn(subnet);
+ MultivaluedMap<String, String> params = createSubnetParams("10.38.1.0/24");
+ params.putSingle("TagSpecification.1.ResourceType", "subnet");
+ params.putSingle("TagSpecification.1.Tag.1.Key", "omitted-value");
+ params.putSingle("TagSpecification.1.Tag.2.Key", "explicit-empty-value");
+ params.putSingle("TagSpecification.1.Tag.2.Value", "");
+ params.putSingle("TagSpecification.1.Tag.3.Key", "ordinary-value");
+ params.putSingle("TagSpecification.1.Tag.3.Value", "present");
+
+ Response response = handler(service).handle("CreateSubnet", params, "us-east-1");
+
+ assertEquals(200, response.getStatus());
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.forClass(List.class);
+ verify(service).createTags(eq("us-east-1"), eq(List.of("subnet-test")), tags.capture());
+ assertEquals(List.of("", "", "present"),
+ tags.getValue().stream().map(Tag::getValue).toList());
+ }
+
+ @Test
+ void createTagsStoresOmittedValueAsEmptyString() {
+ Ec2Service service = mock(Ec2Service.class);
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("ResourceId.1", "vpc-test");
+ params.putSingle("Tag.1.Key", "omitted-value");
+ params.putSingle("Tag.2.Key", "explicit-empty-value");
+ params.putSingle("Tag.2.Value", "");
+ params.putSingle("Tag.3.Key", "ordinary-value");
+ params.putSingle("Tag.3.Value", "present");
+
+ Response response = handler(service).handle("CreateTags", params, "us-east-1");
+
+ assertEquals(200, response.getStatus());
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.forClass(List.class);
+ verify(service).createTags(eq("us-east-1"), eq(List.of("vpc-test")), tags.capture());
+ assertEquals(List.of("", "", "present"),
+ tags.getValue().stream().map(Tag::getValue).toList());
+ }
+
+ @Test
+ void deleteTagsKeepsOmittedValueAsNullSoItMatchesAnyValue() {
+ Ec2Service service = mock(Ec2Service.class);
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("ResourceId.1", "vpc-test");
+ params.putSingle("Tag.1.Key", "any-value");
+
+ Response response = handler(service).handle("DeleteTags", params, "us-east-1");
+
+ assertEquals(200, response.getStatus());
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.forClass(List.class);
+ verify(service).deleteTags(eq("us-east-1"), eq(List.of("vpc-test")), tags.capture());
+ assertNull(tags.getValue().getFirst().getValue());
+ }
+
+ @Test
+ void normalizesValuelessCreateVpcTagsBeforeMutation() {
+ Ec2Service service = mock(Ec2Service.class);
+ Vpc vpc = new Vpc();
+ vpc.setVpcId("vpc-test");
+ when(service.createVpc("us-east-1", "10.38.0.0/16", false, false)).thenReturn(vpc);
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("CidrBlock", "10.38.0.0/16");
+ params.putSingle("TagSpecification.1.ResourceType", "vpc");
+ params.putSingle("TagSpecification.1.Tag.1.Key", "omitted-value");
+
+ Response response = handler(service).handle("CreateVpc", params, "us-east-1");
+
+ assertEquals(200, response.getStatus());
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.forClass(List.class);
+ verify(service).createTags(eq("us-east-1"), eq(List.of("vpc-test")), tags.capture());
+ assertEquals(List.of(""), tags.getValue().stream().map(Tag::getValue).toList());
+ }
+
+ private Ec2QueryHandler handler(Ec2Service service) {
+ return new Ec2QueryHandler(
+ service, mock(EmulatorConfig.class), mock(FlowLogService.class),
+ mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
+ mock(Ec2IpamService.class));
+ }
+
+ private MultivaluedMap<String, String> createSubnetParams(String cidrBlock) {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("VpcId", "vpc-test");
+ params.putSingle("CidrBlock", cidrBlock);
+ return params;
+ }
+}