Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): apply EBS encryption by default to new volumes and root volumes (#5371)
Nazimuddin Tajuddin Bashada063a82f808
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.javaindex 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, Resettabprivate jakarta.enterprise.inject.Instance<ClusterNodeInstanceProvider> 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<VpcRouteTableListener> routeTableListenersInstance;private final List<VpcRouteTableListener> routeTableListeners = new CopyOnWriteArrayList<>();private jakarta.enterprise.inject.Instance<Ec2InstanceLaunchListener> instanceLaunchListenersInstance;@@ -428,13 +431,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettabjakarta.enterprise.inject.Instance<ClusterNodeInstanceProvider> clusterNodeInstanceProviders,Ec2VolumeBlockDeviceManager volumeBlockDeviceManager,jakarta.enterprise.inject.Instance<VpcRouteTableListener> routeTableListenersInstance,- jakarta.enterprise.inject.Instance<Ec2InstanceLaunchListener> instanceLaunchListenersInstance) {+ jakarta.enterprise.inject.Instance<Ec2InstanceLaunchListener> 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, ResettabrootVol.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<Tag> volumeTags) {@@ -9796,7 +9806,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettabvol.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.javanew file mode 100644index 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.+ *+ * <p>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.</p>+ */+@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));+ }+}