fix(organizations): refuse incomplete tags and duplicate tag keys (#8098)

This commit is contained in:
soreavis authored and GitHub committed 2026-11-12 09:05:31 -04:00
1 parent 8a817ec5e9
commit 8b31e7727c
2 files changed
+120 -2

No files matched your search

@@ -797,13 +797,27 @@ public class OrganizationsJsonHandler {
return value.isMissingNode() || value.isNull() ? null : value.asText();
}
/** Organizations sends tags as a list of {@code {Key, Value}} objects, not a map. */
/**
* Organizations sends tags as a list of {@code {Key, Value}} objects, not a map. A repeated key
* is gone once the list is a map, so it is refused while reading it.
*/
private static Map<String, String> parseTags(JsonNode node) {
if (node == null || !node.isArray()) {
return null;
}
Map<String, String> tags = new LinkedHashMap<>();
node.forEach(entry -> tags.put(entry.path("Key").asText(), entry.path("Value").asText("")));
for (JsonNode entry : node) {
String key = text(entry, "Key");
String value = text(entry, "Value");
if (key == null || value == null) {
throw new AwsException("InvalidInputException",
"Each tag needs a Key and a Value, and neither can be null.", 400);
}
if (tags.putIfAbsent(key, value) != null) {
throw new AwsException("InvalidInputException", "Duplicate tag key: " + key + ".", 400,
Map.of("Reason", "DUPLICATE_TAG_KEY"));
}
}
return tags;
}
@@ -0,0 +1,104 @@
package io.floci.aws.services.organizations;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsErrorResponse;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
class OrganizationsJsonHandlerTest {
private static final String OWNER = "100623600101";
private static final String INVITEE = "100623600109";
private static final String TAGS = "\"Tags\":[{\"Key\":\"team\",\"Value\":\"red\"},{\"Key\":\"note\",\"Value\":\"\"}]";
private static final Map<String, String> EXPECTED = Map.of("team", "red", "note", "");
private final ObjectMapper mapper = new ObjectMapper();
private OrganizationsService service;
private OrganizationsJsonHandler handler;
private String root;
@BeforeEach
void setUp() {
service = new OrganizationsService(mapper, new RegionResolver("us-east-1", OWNER),
AccountAwareStorageBackend.inMemory(OWNER), AccountAwareStorageBackend.inMemory(OWNER),
AccountAwareStorageBackend.inMemory(OWNER), AccountAwareStorageBackend.inMemory(OWNER),
AccountAwareStorageBackend.inMemory(OWNER), AccountAwareStorageBackend.inMemory(OWNER));
handler = new OrganizationsJsonHandler(service, mapper);
root = service.createOrganization(OWNER, "ALL").getRoot().getId();
}
@Test
void validTagsReachEveryTaggedOperation() throws Exception {
String unit = ok("CreateOrganizationalUnit", OWNER,
"{\"ParentId\":\"" + root + "\",\"Name\":\"Unit\"," + TAGS + "}")
.path("OrganizationalUnit").path("Id").asText();
assertEquals(EXPECTED, service.listTagsForResource(OWNER, unit));
String account = ok("CreateAccount", OWNER,
"{\"Email\":\"tagged@example.test\",\"AccountName\":\"Tagged\"," + TAGS + "}")
.path("CreateAccountStatus").path("AccountId").asText();
assertEquals(EXPECTED, service.listTagsForResource(OWNER, account));
service.enablePolicyType(OWNER, root, "SERVICE_CONTROL_POLICY");
String policy = ok("CreatePolicy", OWNER, "{\"Content\":\"{}\",\"Description\":\"\",\"Name\":\"Policy\","
+ "\"Type\":\"SERVICE_CONTROL_POLICY\"," + TAGS + "}")
.path("Policy").path("PolicySummary").path("Id").asText();
assertEquals(EXPECTED, service.listTagsForResource(OWNER, policy));
ok("TagResource", OWNER, "{\"ResourceId\":\"" + root + "\"," + TAGS + "}");
assertEquals(EXPECTED, service.listTagsForResource(OWNER, root));
ok("PutResourcePolicy", OWNER, "{\"Content\":\"{}\"," + TAGS + "}");
assertEquals(EXPECTED, service.describeResourcePolicy(OWNER).tags());
String handshake = ok("InviteAccountToOrganization", OWNER,
"{\"Target\":{\"Id\":\"" + INVITEE + "\",\"Type\":\"ACCOUNT\"}," + TAGS + "}")
.path("Handshake").path("Id").asText();
ok("AcceptHandshake", INVITEE, "{\"HandshakeId\":\"" + handshake + "\"}");
assertEquals(EXPECTED, service.listTagsForResource(OWNER, INVITEE));
}
@ParameterizedTest
@ValueSource(strings = {
"{\"Key\":\"team\",\"Value\":null}",
"{\"Key\":\"team\"}",
"{\"Key\":null,\"Value\":\"red\"}",
"{\"Value\":\"red\"}"})
void aTagWithANullOrMissingKeyOrValueFailsTheRequest(String tag) throws Exception {
try (Response response = handler.handle("TagResource",
mapper.readTree("{\"ResourceId\":\"" + root + "\",\"Tags\":[" + tag + "]}"), OWNER)) {
assertEquals(400, response.getStatus());
assertEquals("InvalidInputException", ((AwsErrorResponse) response.getEntity()).type());
}
assertEquals(Map.of(), service.listTagsForResource(OWNER, root));
}
@Test
void aRepeatedTagKeyFailsTheRequestWithItsReason() throws Exception {
try (Response response = handler.handle("TagResource", mapper.readTree("{\"ResourceId\":\"" + root
+ "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"red\"},{\"Key\":\"team\",\"Value\":\"blue\"}]}"), OWNER)) {
assertEquals(400, response.getStatus());
JsonNode body = (JsonNode) response.getEntity();
assertEquals("InvalidInputException", body.path("__type").asText());
assertEquals("DUPLICATE_TAG_KEY", body.path("Reason").asText());
}
assertEquals(Map.of(), service.listTagsForResource(OWNER, root));
}
private JsonNode ok(String action, String caller, String request) throws Exception {
try (Response response = handler.handle(action, mapper.readTree(request), caller)) {
assertEquals(200, response.getStatus(), String.valueOf(response.getEntity()));
return (JsonNode) response.getEntity();
}
}
}