fix(ecs): look up a task definition without failing on a stored record with no ARN (#5296)

* fix(ecs): look up a task definition without failing on a stored record with no ARN

resolveTaskDefinitionOrThrow compared each stored definition's ARN with
d.getTaskDefinitionArn().equals(ref), so a family or family:revision lookup
threw a NullPointerException when the region held a definition with no ARN.
The comparison is now null-safe, and EcsTaskDefinitionLegacyKeyTest covers a
family lookup next to such a record.

* fix(ecs): answer a missing task definition reference with ClientException

A null or blank reference reached the lookup's comparisons and threw a
NullPointerException, a 500, once the region held any task definition:
the family fallback's family.equals(...) failed even before the
null-safe ARN comparison. It is now refused up front with the same
ClientException an unknown reference gets.

* test(ecs): cover the family lookup in the existing no-ARN test
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 11:15:40 -04:00
1 parent d39c7e1381
commit 4927f71ce1
2 files changed
+20 -1

No files matched your search

@@ -5265,10 +5265,13 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
}
private TaskDefinition resolveTaskDefinitionOrThrow(String ref, String region) {
if (ref == null || ref.isBlank()) {
throw new AwsException("ClientException", "Unable to describe task definition: " + ref, 400);
}
TaskDefinition td = taskDefinitions.get(familyKey(region, ref));
if (td != null) { return td; }
td = taskDefinitionsIn(region)
.filter(d -> d.getTaskDefinitionArn().equals(ref)
.filter(d -> ref.equals(d.getTaskDefinitionArn())
|| (d.getFamily() + ":" + d.getRevision()).equals(ref))
.findFirst().orElse(null);
if (td != null) { return td; }
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ecs;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
@@ -16,6 +17,7 @@ import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -74,6 +76,8 @@ class EcsTaskDefinitionLegacyKeyTest {
assertEquals(List.of(registered.getTaskDefinitionArn()), arns(service, REGION));
assertEquals(registered.getTaskDefinitionArn(),
service.describeTaskDefinition(FAMILY + ":1", REGION).getTaskDefinitionArn());
assertEquals(registered.getTaskDefinitionArn(),
service.describeTaskDefinition(FAMILY, REGION).getTaskDefinitionArn());
}
@Test
@@ -93,6 +97,18 @@ class EcsTaskDefinitionLegacyKeyTest {
assertEquals(2, register(service, REGION).getRevision());
}
// Catches: a missing reference dereferenced while searching a populated region, a 500
// NullPointerException instead of the ClientException any unknown reference gets.
@Test
void aMissingReferenceInAPopulatedRegionIsAClientException() {
EcsService service = newService(new InMemoryStorageFactory());
register(service, REGION);
AwsException e = assertThrows(AwsException.class, () -> service.describeTaskDefinition(null, REGION));
assertEquals("ClientException", e.getErrorCode());
}
private static TaskDefinition legacyDefinition() {
TaskDefinition td = new TaskDefinition();
td.setFamily(FAMILY);