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

* fix(ec2): store omitted inline tag values as empty strings

* fix(ec2): normalize omitted tag values on CreateTags and every tag-spec path

---------

Co-authored-by: Jason van Zyl <jason@vanzyl.ca>
This commit is contained in:
Priamo GermosenandJason van Zyl authored and GitHub committed 2026-09-18 23:49:02 -05:00
1 parent 75fee00adc
commit 052da93515
4 files changed
+200 -11

No files matched your search

@@ -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
@@ -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;
}
}