From da063a82f80848275b1ac26e886495df9f71cc1e Mon Sep 17 00:00:00 2001 From: Nazimuddin Tajuddin Basha Date: Sun, 11 Oct 2026 21:13:26 -0500 Subject: [PATCH] fix(ec2): apply EBS encryption by default to new volumes and root volumes (#5371) * fix(ec2): apply EBS encryption by default to new volumes and root volumes EnableEbsEncryptionByDefault stored the flag but nothing on the volume creation path read it, so CreateVolume and the RunInstances root volume came back Encrypted=false even with the default on. CreateVolume now encrypts when the region default is on, and an explicit Encrypted=false cannot opt out, as on AWS. The RunInstances root volume follows the same rule. The setting is per region. Fixes #5370 * test(ec2): use an explicit RequestSpecification type instead of var * refactor(ec2): take Ec2EbsEncryptionService on the CDI constructor Constructor injection per AGENTS.md: Ec2EbsEncryptionService joins the other CDI-only dependencies on Ec2Service's @Inject constructor instead of being a field. The test uses a static import for assertEquals in place of the private wrapper. --- .../floci/services/ec2/Ec2Service.java | 16 +- ...yptionByDefaultAppliedIntegrationTest.java | 156 ++++++++++++++++++ 2 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/ec2/Ec2EbsEncryptionByDefaultAppliedIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 1461eed9c5..ef7b96ebe4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -294,6 +294,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab private jakarta.enterprise.inject.Instance clusterNodeInstanceProviders; private ClusterNodeInstanceProvider testClusterNodeInstanceProvider; private final Ec2VolumeBlockDeviceManager volumeBlockDeviceManager; + // Only the CDI constructor receives it; null on the hand-built constructors, which reads as + // "encryption by default is off". + private Ec2EbsEncryptionService ebsEncryptionService; private jakarta.enterprise.inject.Instance routeTableListenersInstance; private final List routeTableListeners = new CopyOnWriteArrayList<>(); private jakarta.enterprise.inject.Instance instanceLaunchListenersInstance; @@ -428,13 +431,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab jakarta.enterprise.inject.Instance clusterNodeInstanceProviders, Ec2VolumeBlockDeviceManager volumeBlockDeviceManager, jakarta.enterprise.inject.Instance routeTableListenersInstance, - jakarta.enterprise.inject.Instance instanceLaunchListenersInstance) { + jakarta.enterprise.inject.Instance instanceLaunchListenersInstance, + Ec2EbsEncryptionService ebsEncryptionService) { this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog, instanceTypeCatalog, storageFactory, requestContextInstance, iamService, volumeBlockDeviceManager); this.vpcNetworkManager = vpcNetworkManager; this.clusterNodeInstanceProviders = clusterNodeInstanceProviders; this.routeTableListenersInstance = routeTableListenersInstance; this.instanceLaunchListenersInstance = instanceLaunchListenersInstance; + this.ebsEncryptionService = ebsEncryptionService; } public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager, @@ -3482,6 +3487,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab rootVol.setSize(DEFAULT_ROOT_VOLUME_SIZE_GIB); rootVol.setState("in-use"); rootVol.setRegion(region); + rootVol.setEncrypted(ebsEncryptionByDefault(region)); rootVol.setCreateTime(Instant.now()); VolumeAttachment att = new VolumeAttachment(); att.setVolumeId(rootVolId); @@ -9785,6 +9791,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab // ─── Volumes ─────────────────────────────────────────────────────────────── + private boolean ebsEncryptionByDefault(String region) { + return ebsEncryptionService != null && ebsEncryptionService.getEbsEncryptionByDefault(region); + } + public Volume createVolume(String region, String availabilityZone, String volumeType, int size, boolean encrypted, int iops, Integer throughput, String snapshotId, List volumeTags) { @@ -9796,7 +9806,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab vol.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a"); vol.setVolumeType(effectiveType); vol.setSize(size > 0 ? size : 8); - vol.setEncrypted(encrypted); + // With encryption by default on, AWS encrypts every new volume and the Encrypted parameter + // cannot opt out of it. + vol.setEncrypted(encrypted || ebsEncryptionByDefault(region)); vol.setIops(iops > 0 ? iops : (volumeType != null && volumeType.startsWith("io") ? iops : 0)); // Throughput is a gp3-only attribute; AWS reports 125 MiB/s by default for gp3. if ("gp3".equals(effectiveType)) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2EbsEncryptionByDefaultAppliedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2EbsEncryptionByDefaultAppliedIntegrationTest.java new file mode 100644 index 0000000000..9158445bf3 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2EbsEncryptionByDefaultAppliedIntegrationTest.java @@ -0,0 +1,156 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.hasItem; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * EBS encryption by default has to be applied, not only stored: a volume created, or an instance + * root volume launched, while the setting is on is encrypted, as on AWS. + * + *

Runs in its own region. The setting is region-scoped and persists across test classes in the same + * application, so using us-east-1 would leak encrypted volumes into every other EC2 test. The last test + * turns it off again; a static @AfterAll cannot, because the application is already stopped by then.

+ */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class Ec2EbsEncryptionByDefaultAppliedIntegrationTest { + + private static final String REGION = "ap-southeast-3"; + private static final String AZ = REGION + "a"; + + private static String authHeader(String region) { + return "AWS4-HMAC-SHA256 Credential=test/20260205/" + region + "/ec2/aws4_request"; + } + + private static final String AUTH_HEADER = authHeader(REGION); + + private static String createVolume(String... extraParams) { + RequestSpecification request = given() + .formParam("Action", "CreateVolume") + .formParam("AvailabilityZone", AZ) + .formParam("Size", "1") + .header("Authorization", AUTH_HEADER); + for (int i = 0; i + 1 < extraParams.length; i += 2) { + request = request.formParam(extraParams[i], extraParams[i + 1]); + } + return request.when().post("/").then().statusCode(200) + .extract().path("CreateVolumeResponse.encrypted"); + } + + private static void setDefault(boolean enabled) { + given().formParam("Action", enabled ? "EnableEbsEncryptionByDefault" : "DisableEbsEncryptionByDefault") + .header("Authorization", AUTH_HEADER) + .when().post("/").then().statusCode(200); + } + + private static String runInstanceInRegion(String region) { + return given() + .formParam("Action", "RunInstances") + .formParam("ImageId", "ami-0abcdef1234567890") + .formParam("InstanceType", "t3.micro") + .formParam("MinCount", "1") + .formParam("MaxCount", "1") + .header("Authorization", authHeader(region)) + .when().post("/").then().statusCode(200) + .extract().path("RunInstancesResponse.instancesSet.item.instanceId"); + } + + private static String rootVolumeEncrypted(String region, String instanceId) { + String volumeId = given() + .formParam("Action", "DescribeInstances") + .formParam("InstanceId.1", instanceId) + .header("Authorization", authHeader(region)) + .when().post("/").then().statusCode(200) + .extract().path("DescribeInstancesResponse.reservationSet.item.instancesSet.item" + + ".blockDeviceMapping.item.ebs.volumeId"); + return given() + .formParam("Action", "DescribeVolumes") + .formParam("VolumeId.1", volumeId) + .header("Authorization", authHeader(region)) + .when().post("/").then().statusCode(200) + .extract().path("DescribeVolumesResponse.volumeSet.item.encrypted"); + } + + @Test + @Order(1) + void volumesAreUnencryptedWhileTheDefaultIsOff() { + assertEquals("false", createVolume()); + } + + @Test + @Order(2) + void aNewVolumeIsEncryptedWhileTheDefaultIsOn() { + setDefault(true); + assertEquals("true", createVolume()); + } + + @Test + @Order(3) + void theEncryptedFlagIsPersistedAndReportedByDescribeVolumes() { + String volumeId = given() + .formParam("Action", "CreateVolume") + .formParam("AvailabilityZone", AZ) + .formParam("Size", "1") + .header("Authorization", AUTH_HEADER) + .when().post("/").then().statusCode(200) + .extract().path("CreateVolumeResponse.volumeId"); + given() + .formParam("Action", "DescribeVolumes") + .formParam("Filter.1.Name", "encrypted") + .formParam("Filter.1.Value.1", "true") + .header("Authorization", AUTH_HEADER) + .when().post("/").then().statusCode(200) + .body("DescribeVolumesResponse.volumeSet.item.volumeId", hasItem(volumeId)); + } + + @Test + @Order(4) + void encryptedFalseCannotOptOutOfTheDefault() { + assertEquals("true", createVolume("Encrypted", "false")); + } + + @Test + @Order(5) + void anInstanceRootVolumeIsEncryptedWhileTheDefaultIsOn() { + String instanceId = runInstanceInRegion(REGION); + assertEquals("true", rootVolumeEncrypted(REGION, instanceId)); + } + + @Test + @Order(6) + void theDefaultIsRegionScoped() { + // The default is on for REGION only; another region keeps creating unencrypted volumes. + String other = "ap-southeast-4"; + String encrypted = given() + .formParam("Action", "CreateVolume") + .formParam("AvailabilityZone", other + "a") + .formParam("Size", "1") + .header("Authorization", authHeader(other)) + .when().post("/").then().statusCode(200) + .extract().path("CreateVolumeResponse.encrypted"); + assertEquals("false", encrypted); + } + + @Test + @Order(7) + void anExplicitlyEncryptedVolumeStaysEncrypted() { + setDefault(false); + assertEquals("true", createVolume("Encrypted", "true")); + } + + @Test + @Order(8) + void disablingTheDefaultStopsEncryptingNewVolumesAndRootVolumes() { + assertEquals("false", createVolume()); + String instanceId = runInstanceInRegion(REGION); + assertEquals("false", rootVolumeEncrypted(REGION, instanceId)); + } +}