diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java index d7e1e9055e..75fc9d18b3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java @@ -1381,7 +1381,7 @@ public class EcsJsonHandler { for (JsonNode item : node) { result.add(new VolumeFrom( item.path("sourceContainer").asText(), - item.path("readOnly").asBoolean(false))); + item.hasNonNull("readOnly") ? item.get("readOnly").asBoolean() : null)); } return result; } @@ -1559,7 +1559,7 @@ public class EcsJsonHandler { result.add(new MountPoint( item.path("sourceVolume").asText(), item.path("containerPath").asText(), - item.path("readOnly").asBoolean(false))); + item.hasNonNull("readOnly") ? item.get("readOnly").asBoolean() : null)); } return result; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java index c83d4f5c0c..25f3629688 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java @@ -300,7 +300,7 @@ public class EcsResponseWriter { ObjectNode mpNode = objectMapper.createObjectNode(); mpNode.put("sourceVolume", mp.sourceVolume()); mpNode.put("containerPath", mp.containerPath()); - mpNode.put("readOnly", mp.readOnly()); + if (mp.readOnly() != null) { mpNode.put("readOnly", mp.readOnly()); } mps.add(mpNode); } n.set("mountPoints", mps); @@ -310,7 +310,7 @@ public class EcsResponseWriter { for (VolumeFrom volumeFrom : def.getVolumesFrom()) { ObjectNode volumeFromNode = objectMapper.createObjectNode(); volumeFromNode.put("sourceContainer", volumeFrom.sourceContainer()); - volumeFromNode.put("readOnly", volumeFrom.readOnly()); + if (volumeFrom.readOnly() != null) { volumeFromNode.put("readOnly", volumeFrom.readOnly()); } volumesFrom.add(volumeFromNode); } n.set("volumesFrom", volumesFrom); diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java index e9e3aaa2a9..13c9c39880 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java @@ -525,7 +525,7 @@ public class EcsContainerManager { // rejection is not swallowed, it propagates out of startTask and is // surfaced by EcsService as the task's stoppedReason. hostVolumePolicy.validate(sourcePath); - if (mp.readOnly()) { + if (Boolean.TRUE.equals(mp.readOnly())) { specBuilder.withReadOnlyBind(sourcePath, mp.containerPath()); } else { specBuilder.withBind(sourcePath, mp.containerPath()); @@ -538,7 +538,8 @@ public class EcsContainerManager { labels.put(ContainerStorageHelper.SERVICE_LABEL, "ecs"); lifecycleManager.ensureVolume(ephemeralVolume, labels); } - specBuilder.withNamedVolume(ephemeralVolume, mp.containerPath(), mp.readOnly()); + specBuilder.withNamedVolume(ephemeralVolume, mp.containerPath(), + Boolean.TRUE.equals(mp.readOnly())); } else { LOG.warnv("Skipping mountPoint with unresolved volume {0} on container {1}", mp.sourceVolume(), def.getName()); @@ -553,7 +554,8 @@ public class EcsContainerManager { throw new IllegalStateException("ECS volumesFrom source container " + volumeFrom.sourceContainer() + " has not started"); } - specBuilder.withVolumesFrom(sourceContainerId, volumeFrom.readOnly()); + specBuilder.withVolumesFrom(sourceContainerId, + Boolean.TRUE.equals(volumeFrom.readOnly())); } } @@ -2369,7 +2371,7 @@ public class EcsContainerManager { lifecycleManager.ensureSharedVolume(efsVolumeName, efsCfg.ownerUid(), efsCfg.ownerGid(), efsCfg.rootPermissions(), efsCfg.initImage()); } - specBuilder.withNamedVolume(efsVolumeName, mp.containerPath(), mp.readOnly()); + specBuilder.withNamedVolume(efsVolumeName, mp.containerPath(), Boolean.TRUE.equals(mp.readOnly())); PosixUser posixUser = accessPoint != null ? accessPoint.getPosixUser() : null; if (posixUser != null) { if (posixUser.getUid() == null || posixUser.getGid() == null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/model/MountPoint.java b/src/main/java/io/github/hectorvent/floci/services/ecs/model/MountPoint.java index ba442c664b..f1b8fcc49d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/model/MountPoint.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/model/MountPoint.java @@ -9,5 +9,5 @@ import io.quarkus.runtime.annotations.RegisterForReflection; * host source path is bind-mounted into the container at {@code containerPath}. */ @RegisterForReflection -public record MountPoint(String sourceVolume, String containerPath, boolean readOnly) { +public record MountPoint(String sourceVolume, String containerPath, Boolean readOnly) { } diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/model/VolumeFrom.java b/src/main/java/io/github/hectorvent/floci/services/ecs/model/VolumeFrom.java index 7f8f700458..7325d41873 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/model/VolumeFrom.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/model/VolumeFrom.java @@ -7,5 +7,5 @@ import io.quarkus.runtime.annotations.RegisterForReflection; * {@code {"sourceContainer": ..., "readOnly": ...}}. */ @RegisterForReflection -public record VolumeFrom(String sourceContainer, boolean readOnly) { +public record VolumeFrom(String sourceContainer, Boolean readOnly) { } diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsIntegrationTest.java index 4c4f531edf..84a2a728f8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsIntegrationTest.java @@ -1347,4 +1347,108 @@ class EcsIntegrationTest { .body("__type", containsString("ClientException")) .body("message", containsString("HealthCheck command is required.")); } + + @Test + @Order(72) + void registerAndDescribeTaskDefinitionKeepsReadOnlyAbsentWhenItWasNeverSent() { + String arn = ecs("RegisterTaskDefinition") + .body(""" + { + "family": "readonly-absent-roundtrip", + "volumes": [{"name": "v"}], + "containerDefinitions": [ + { + "name": "a", + "image": "nginx:latest", + "memory": 128, + "essential": true, + "mountPoints": [{"sourceVolume": "v", "containerPath": "/a"}] + }, + { + "name": "b", + "image": "nginx:latest", + "memory": 128, + "essential": false, + "volumesFrom": [{"sourceContainer": "a"}] + } + ] + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("taskDefinition.containerDefinitions[0].mountPoints[0]" + + ".sourceVolume", equalTo("v")) + .body("taskDefinition.containerDefinitions[0].mountPoints[0]" + + ".containerPath", equalTo("/a")) + .body("taskDefinition.containerDefinitions[1].volumesFrom[0]" + + ".sourceContainer", equalTo("a")) + .extract() + .path("taskDefinition.taskDefinitionArn"); + + ecs("DescribeTaskDefinition") + .body(""" + {"taskDefinition": "%s"} + """.formatted(arn)) + .when() + .post("/") + .then() + .statusCode(200) + .body("taskDefinition.containerDefinitions[0].mountPoints[0]", not(hasKey("readOnly"))) + .body("taskDefinition.containerDefinitions[1].volumesFrom[0]", not(hasKey("readOnly"))); + } + + @Test + @Order(73) + void registerAndDescribeTaskDefinitionEchoesAnExplicitReadOnly() { + String arn = ecs("RegisterTaskDefinition") + .body(""" + { + "family": "readonly-explicit-roundtrip", + "volumes": [{"name": "v"}], + "containerDefinitions": [ + { + "name": "a", + "image": "nginx:latest", + "memory": 128, + "essential": true, + "mountPoints": [ + {"sourceVolume": "v", "containerPath": "/ro", "readOnly": true}, + {"sourceVolume": "v", "containerPath": "/rw", "readOnly": false} + ], + "volumesFrom": [ + {"sourceContainer": "b", "readOnly": true}, + {"sourceContainer": "b", "readOnly": false} + ] + }, + { + "name": "b", + "image": "nginx:latest", + "memory": 128, + "essential": false + } + ] + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .extract() + .path("taskDefinition.taskDefinitionArn"); + + ecs("DescribeTaskDefinition") + .body(""" + {"taskDefinition": "%s"} + """.formatted(arn)) + .when() + .post("/") + .then() + .statusCode(200) + .body("taskDefinition.containerDefinitions[0].mountPoints[0].readOnly", equalTo(true)) + .body("taskDefinition.containerDefinitions[0].mountPoints[1].readOnly", equalTo(false)) + .body("taskDefinition.containerDefinitions[0].volumesFrom[0].readOnly", equalTo(true)) + .body("taskDefinition.containerDefinitions[0].volumesFrom[1].readOnly", equalTo(false)); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandlerVolumesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandlerVolumesTest.java index d012bf9b20..69211682f3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandlerVolumesTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandlerVolumesTest.java @@ -159,6 +159,87 @@ class EcsJsonHandlerVolumesTest { assertFalse(mps.get(1).path("readOnly").asBoolean(), "aws mount is read-write"); } + @Test + void registerTaskDefinitionOmitsReadOnlyFromMountPointsAndVolumesFromThatNeverSentIt() throws Exception { + String requestJson = """ + { + "family": "td", + "containerDefinitions": [ + { + "name": "a", + "image": "nginx", + "memory": 128, + "essential": true, + "mountPoints": [ + {"sourceVolume": "v", "containerPath": "/a"} + ] + }, + { + "name": "b", + "image": "nginx", + "memory": 128, + "essential": false, + "volumesFrom": [ + {"sourceContainer": "a"} + ] + } + ], + "volumes": [{"name": "v"}] + } + """; + JsonNode request = objectMapper.readTree(requestJson); + + Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1"); + JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition"); + JsonNode containerA = td.path("containerDefinitions").get(0); + JsonNode containerB = td.path("containerDefinitions").get(1); + + assertTrue(containerA.path("mountPoints").get(0).path("readOnly").isMissingNode(), + "a mountPoint registered without readOnly must not gain one"); + assertTrue(containerB.path("volumesFrom").get(0).path("readOnly").isMissingNode(), + "a volumesFrom entry registered without readOnly must not gain one"); + } + + @Test + void registerTaskDefinitionEchoesAnExplicitReadOnlyOnMountPointsAndVolumesFrom() throws Exception { + String requestJson = """ + { + "family": "td-explicit-readonly", + "containerDefinitions": [ + { + "name": "app", + "image": "nginx", + "memory": 128, + "essential": true, + "mountPoints": [ + {"sourceVolume": "v", "containerPath": "/ro", "readOnly": true}, + {"sourceVolume": "v", "containerPath": "/rw", "readOnly": false} + ], + "volumesFrom": [ + {"sourceContainer": "other", "readOnly": true}, + {"sourceContainer": "other2", "readOnly": false} + ] + } + ], + "volumes": [{"name": "v"}] + } + """; + JsonNode request = objectMapper.readTree(requestJson); + + Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1"); + JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition"); + JsonNode definition = td.path("containerDefinitions").get(0); + JsonNode mps = definition.path("mountPoints"); + JsonNode volumesFrom = definition.path("volumesFrom"); + + assertTrue(mps.get(0).path("readOnly").asBoolean(), "explicit readOnly true must round-trip"); + assertFalse(mps.get(1).path("readOnly").asBoolean(), "explicit readOnly false must round-trip"); + assertTrue(volumesFrom.get(0).path("readOnly").asBoolean(), + "explicit volumesFrom readOnly true must round-trip"); + assertFalse(volumesFrom.get(1).path("readOnly").asBoolean(), + "explicit volumesFrom readOnly false must round-trip"); + } + @Test void registerTaskDefinitionRoundTripsEfsVolumeConfiguration() throws Exception { String requestJson = """