Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): keep the KMS key a volume is created with (#6756)

Jackson Pricea83fa9c9e4f9
diff --git a/docs/services/ec2.md b/docs/services/ec2.md
index e0200113eb..b154a1cad7 100644
--- a/docs/services/ec2.md
+++ b/docs/services/ec2.md
@@ -1123,6 +1123,8 @@ A standalone ENI created via `CreateNetworkInterface` can also be handed to `Run
| AttachVolume | Attaches a volume to an instance at the requested device; returns the attachment in `attaching` state. |
| DetachVolume | Detaches a volume from an instance, optionally forced; returns the attachment in `detaching` state. |
+An encrypted volume reports the KMS key it was created with as `kmsKeyId` in `CreateVolume` and `DescribeVolumes`: the `KmsKeyId` of the `CreateVolume` request or of the launch's block device mapping (a launch template's included), or else the region's default EBS key as `GetEbsDefaultKmsKeyId` reported it then (`alias/aws/ebs` until `ModifyEbsDefaultKmsKeyId` sets another). Keys are kept as given (a key id, key ARN, alias name or alias ARN) and are not checked against KMS; AWS reports the key's ARN, and fails the volume later when the key cannot be used. A volume that is not encrypted has no key. AWS refuses a `KmsKeyId` without `Encrypted=true`, even with encryption by default on; Floci accepts it, and the volume keeps the key only when encryption by default, or for a launch volume an encrypted snapshot, makes it encrypted. A launch volume encrypted only by its snapshot and naming no key gets the region's default key, where AWS keeps the snapshot's: snapshots record no key. Volumes stored before Floci kept keys report none.
+
When `floci.services.ec2.volume-block-devices` is enabled (the default), attached EBS volumes are backed by real Linux loop devices inside running instance containers and synthesized EKS cluster node containers:
- **Storage mechanism**: Backing sparse raw image files are allocated in Floci storage under `floci-aws-ec2-volumes` (named volume or host path). A privileged helper container (`floci-aws-ec2-volume-helper`, running `floci.services.ec2.volume-helper-image`, default `alpine:3.21`) manages Linux loop devices via `losetup`. It starts on the first volume operation and is removed when Floci shuts down; the backing files stay in storage.
- **Target device nodes**: Inside privileged target instance containers, the corresponding device node is created at the requested path (for example `/dev/xvdf` or `/dev/sdf`) using `mknod` or a symlink to the loop device, matching the exact size specified during volume creation. Target containers can format filesystems (such as ext4 or xfs), mount them, and persist data across detach and reattach.
@@ -1182,7 +1184,7 @@ Spot prices returned by `DescribeSpotPriceHistory` are synthetic and derived det
| EnableEbsEncryptionByDefault | Turns on default encryption for new volumes in the region. |
| DisableEbsEncryptionByDefault | Turns default encryption back off. |
| GetEbsEncryptionByDefault | Reports whether default encryption is on. |
-| ModifyEbsDefaultKmsKeyId | Sets the KMS key used when a volume names none. |
+| ModifyEbsDefaultKmsKeyId | Sets the KMS key an encrypted volume gets when it names none. |
| GetEbsDefaultKmsKeyId | Reports the current default KMS key. |
| ResetEbsDefaultKmsKeyId | Restores the AWS-managed default key. |
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
index fae6e4ceb9..19d21ccad1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
@@ -6436,11 +6436,12 @@ public class Ec2QueryHandler {
}
String snapshotId = p.getFirst("SnapshotId");
+ String kmsKeyId = p.getFirst("KmsKeyId");
List<Tag> volumeTags = parseTagsForResource(p, "volume");
Volume vol = service.createVolume(region, availabilityZone, volumeType, size,
- encrypted, iops, throughput, snapshotId, volumeTags);
+ encrypted, kmsKeyId, iops, throughput, snapshotId, volumeTags);
XmlBuilder xml = new XmlBuilder()
.start("CreateVolumeResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -6600,7 +6601,8 @@ public class Ec2QueryHandler {
.elem("volumeType", vol.getVolumeType())
.elem("status", vol.getState())
.elem("availabilityZone", vol.getAvailabilityZone())
- .elem("encrypted", String.valueOf(vol.isEncrypted()));
+ .elem("encrypted", String.valueOf(vol.isEncrypted()))
+ .elem("kmsKeyId", vol.getKmsKeyId());
if (vol.getMultiAttachEnabled() != null) {
xml.elem("multiAttachEnabled", String.valueOf(vol.getMultiAttachEnabled()));
}
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 af5d2edaeb..bd002f4f20 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
@@ -4225,7 +4225,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
inst.setRootDeviceName(rootDeviceName);
for (LaunchVolume planned : launchVolumes) {
Volume volume = newVolume(region, az, planned.volumeType(), planned.size(),
- planned.encrypted(), planned.iops(), planned.throughput(), planned.snapshotId());
+ planned.encrypted(), planned.kmsKeyId(), planned.iops(), planned.throughput(),
+ planned.snapshotId());
volume.setState("in-use");
VolumeAttachment attachment = new VolumeAttachment();
attachment.setVolumeId(volume.getVolumeId());
@@ -8677,7 +8678,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
/** One EBS volume a launch creates, settled before anything of the launch is stored. */
private record LaunchVolume(String deviceName, String snapshotId, int size, String volumeType,
- int iops, Integer throughput, boolean encrypted, boolean deleteOnTermination) {}
+ int iops, Integer throughput, boolean encrypted, String kmsKeyId,
+ boolean deleteOnTermination) {}
/**
* The EBS volumes a launch creates, one per device: the AMI's block device mappings with the
@@ -8766,7 +8768,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
Integer throughput = "gp3".equals(volumeType) ? ebs.getThroughput() : null;
planned.add(new LaunchVolume(mapping.getDeviceName(), snapshotId, size, volumeType, iops, throughput,
Boolean.TRUE.equals(ebs.getEncrypted()) || snapshot != null && snapshot.isEncrypted(),
- !Boolean.FALSE.equals(ebs.getDeleteOnTermination())));
+ ebs.getKmsKeyId(), !Boolean.FALSE.equals(ebs.getDeleteOnTermination())));
}
return planned;
}
@@ -13534,11 +13536,36 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return ebsEncryptionService != null && ebsEncryptionService.getEbsEncryptionByDefault(region);
}
+ /**
+ * The KMS key a volume encrypted without one gets: the region's default EBS key, read when the
+ * volume is created, so a later ModifyEbsDefaultKmsKeyId leaves the volume's key alone, as on AWS.
+ */
+ private String ebsDefaultKmsKeyId(String region) {
+ return ebsEncryptionService != null ? ebsEncryptionService.getEbsDefaultKmsKeyId(region)
+ : Ec2EbsEncryptionService.AWS_MANAGED_KEY_ALIAS;
+ }
+
+ /** Creates a volume without naming a KMS key, so an encrypted one gets the region's default. */
public Volume createVolume(String region, String availabilityZone, String volumeType,
int size, boolean encrypted, int iops, Integer throughput,
String snapshotId, List<Tag> volumeTags) {
+ return createVolume(region, availabilityZone, volumeType, size, encrypted, null, iops, throughput,
+ snapshotId, volumeTags);
+ }
+
+ /**
+ * Creates a volume with the KMS key CreateVolume names. An encrypted volume keeps that key as
+ * given, a key id, key ARN, alias name or alias ARN, without checking it against KMS, as the
+ * region's default EBS key is not checked; AWS itself checks it only after answering. An
+ * encrypted volume that names no key gets the region's default, and a volume that is not
+ * encrypted has no key.
+ */
+ public Volume createVolume(String region, String availabilityZone, String volumeType,
+ int size, boolean encrypted, String kmsKeyId, int iops, Integer throughput,
+ String snapshotId, List<Tag> volumeTags) {
ensureDefaultResources(region);
- Volume vol = newVolume(region, availabilityZone, volumeType, size, encrypted, iops, throughput, snapshotId);
+ Volume vol = newVolume(region, availabilityZone, volumeType, size, encrypted, kmsKeyId, iops, throughput,
+ snapshotId);
String volumeId = vol.getVolumeId();
if (volumeTags != null) {
vol.setTags(new ArrayList<>(volumeTags));
@@ -13554,10 +13581,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
/**
* A new, unattached volume record, not yet stored, with the defaults CreateVolume and a
- * launch's block device mappings share.
+ * launch's block device mappings share. An encrypted volume keeps {@code kmsKeyId}, or gets
+ * the region's default key when it names none; a volume that is not encrypted has no key.
*/
private Volume newVolume(String region, String availabilityZone, String volumeType, int size,
- boolean encrypted, int iops, Integer throughput, String snapshotId) {
+ boolean encrypted, String kmsKeyId, int iops, Integer throughput,
+ String snapshotId) {
String effectiveType = volumeType != null ? volumeType : "gp2";
Volume vol = new Volume();
vol.setVolumeId("vol-" + randomHex(17));
@@ -13567,6 +13596,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
// With encryption by default on, AWS encrypts every new volume and the Encrypted parameter
// cannot opt out of it.
vol.setEncrypted(encrypted || ebsEncryptionByDefault(region));
+ if (vol.isEncrypted()) {
+ vol.setKmsKeyId(kmsKeyId != null && !kmsKeyId.isBlank() ? kmsKeyId : ebsDefaultKmsKeyId(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/main/java/io/github/hectorvent/floci/services/ec2/model/Volume.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Volume.java
index 71e6a0cf80..886feec128 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Volume.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Volume.java
@@ -17,6 +17,7 @@ public class Volume {
private String state; // creating, available, in-use, deleting, deleted, error
private String availabilityZone;
private boolean encrypted;
+ private String kmsKeyId; // the KMS key an encrypted volume was created with, as given; null when none
private int iops;
private Integer throughput; // MiB/s; only meaningful for gp3 (null otherwise)
private String snapshotId;
@@ -46,6 +47,9 @@ public class Volume {
public boolean isEncrypted() { return encrypted; }
public void setEncrypted(boolean encrypted) { this.encrypted = encrypted; }
+ public String getKmsKeyId() { return kmsKeyId; }
+ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
+
public int getIops() { return iops; }
public void setIops(int iops) { this.iops = iops; }
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
index c496e01006..5ec0a68ba6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
@@ -4,6 +4,7 @@ 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.common.XmlParser;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.services.ec2.model.Address;
@@ -35,6 +36,7 @@ import io.github.hectorvent.floci.services.ec2.model.Vpc;
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -694,6 +696,113 @@ class Ec2ServicePersistenceTest {
assertFalse(xml.contains("<createTime>"), xml);
}
+ @Test
+ void volumeKmsKeyIdSurvivesRestart(@TempDir Path dir) {
+ String keyArn = "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
+ String created = query(newService(dir), "CreateVolume", "AvailabilityZone", "us-east-1a", "Size", "1",
+ "Encrypted", "true", "KmsKeyId", keyArn);
+ String volumeId = XmlParser.extractFirst(created, "volumeId", null);
+
+ String described = query(newService(dir), "DescribeVolumes", "VolumeId.1", volumeId);
+
+ assertEquals(List.of("true"), XmlParser.extractAll(described, "encrypted"), described);
+ assertEquals(List.of(keyArn), XmlParser.extractAll(described, "kmsKeyId"), described);
+ }
+
+ /**
+ * A volume store as Floci wrote it before a volume kept its KMS key: an encrypted volume
+ * attached to an instance and an unencrypted one. Both load as they were stored, and
+ * DescribeVolumes leaves {@code kmsKeyId} out: the key the encrypted one was created with was
+ * never recorded, and the region's default may have changed since.
+ */
+ @Test
+ void volumesSavedBeforeTheirKmsKeyWasKeptLoadWithoutOne(@TempDir Path dir) throws IOException {
+ String legacyJson = """
+ {
+ "us-east-1::vol-0aaaaaaaaaaaaaaaa" : {
+ "volumeId" : "vol-0aaaaaaaaaaaaaaaa",
+ "volumeType" : "gp3",
+ "size" : 20,
+ "state" : "in-use",
+ "availabilityZone" : "us-east-1a",
+ "encrypted" : true,
+ "iops" : 0,
+ "throughput" : 125,
+ "snapshotId" : null,
+ "createTime" : "2026-10-20T01:38:17.105Z",
+ "multiAttachEnabled" : null,
+ "region" : "us-east-1",
+ "tags" : [ {
+ "key" : "Team",
+ "value" : "blue"
+ } ],
+ "attachments" : [ {
+ "instanceId" : "i-0aaaaaaaaaaaaaaaa",
+ "device" : "/dev/sdf",
+ "state" : "attached",
+ "attachTime" : "2026-10-20T01:39:02.411Z",
+ "deleteOnTermination" : false,
+ "volumeId" : "vol-0aaaaaaaaaaaaaaaa"
+ } ]
+ },
+ "us-east-1::vol-0bbbbbbbbbbbbbbbb" : {
+ "volumeId" : "vol-0bbbbbbbbbbbbbbbb",
+ "volumeType" : "gp2",
+ "size" : 8,
+ "state" : "available",
+ "availabilityZone" : "us-east-1b",
+ "encrypted" : false,
+ "iops" : 0,
+ "throughput" : null,
+ "snapshotId" : null,
+ "createTime" : "2026-10-20T01:38:17.112Z",
+ "multiAttachEnabled" : null,
+ "region" : "us-east-1",
+ "tags" : [ ],
+ "attachments" : [ ]
+ }
+ }
+ """;
+ Files.writeString(dir.resolve("ec2-volumes.json"), legacyJson);
+
+ Ec2Service restarted = newService(dir);
+
+ Map<String, Volume> restored = new HashMap<>();
+ for (Volume volume : restarted.describeVolumes(REGION, List.of(), Map.of())) {
+ restored.put(volume.getVolumeId(), volume);
+ }
+ assertEquals(2, restored.size());
+ Volume encrypted = restored.get("vol-0aaaaaaaaaaaaaaaa");
+ assertTrue(encrypted.isEncrypted());
+ assertEquals(20, encrypted.getSize());
+ assertEquals(125, encrypted.getThroughput());
+ assertEquals("i-0aaaaaaaaaaaaaaaa", encrypted.getAttachments().getFirst().getInstanceId());
+ assertEquals("blue", encrypted.getTags().getFirst().getValue());
+ assertFalse(restored.get("vol-0bbbbbbbbbbbbbbbb").isEncrypted());
+
+ String described = query(restarted, "DescribeVolumes");
+ List<String> volumeIds = XmlParser.extractAll(described, "volumeId");
+ assertTrue(volumeIds.contains("vol-0aaaaaaaaaaaaaaaa"), described);
+ assertTrue(volumeIds.contains("vol-0bbbbbbbbbbbbbbbb"), described);
+ assertTrue(XmlParser.extractAll(described, "encrypted").contains("true"), described);
+ assertEquals(List.of(), XmlParser.extractAll(described, "kmsKeyId"), described);
+ }
+
+ private static String query(Ec2Service service, String action, String... params) {
+ MultivaluedHashMap<String, String> form = new MultivaluedHashMap<>();
+ for (int i = 0; i < params.length; i += 2) {
+ form.add(params[i], params[i + 1]);
+ }
+ Response response = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
+ mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
+ mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
+ new RegionResolver(REGION, "000000000000"))
+ .handle(action, form, REGION);
+ String body = String.valueOf(response.getEntity());
+ assertEquals(200, response.getStatus(), action + ": " + body);
+ return body;
+ }
+
private static String jsonString(String value) {
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdIntegrationTest.java
new file mode 100644
index 0000000000..ffb23ab2b7
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdIntegrationTest.java
@@ -0,0 +1,318 @@
+package io.github.hectorvent.floci.services.ec2;
+
+import io.github.hectorvent.floci.core.common.XmlParser;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.ec2.Ec2Client;
+import software.amazon.awssdk.services.ec2.model.CreateVolumeResponse;
+import software.amazon.awssdk.services.ec2.model.Volume;
+
+import java.net.URI;
+import java.util.List;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * The KMS key a volume is created with, as the wire reports it: {@code kmsKeyId} on CreateVolume's
+ * response and on the volume DescribeVolumes lists, beside {@code encrypted}.
+ *
+ * <p>Runs in its own region: the EBS encryption defaults are region-scoped and persist across test
+ * classes in the same application, and the tests that change them put them back in a
+ * {@code finally}.</p>
+ */
+@QuarkusTest
+class Ec2VolumeKmsKeyIdIntegrationTest {
+
+ private static final String REGION = "ap-southeast-5";
+ private static final String AZ = REGION + "a";
+ private static final String AUTH_HEADER =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/" + REGION + "/ec2/aws4_request";
+ private static final String KEY_ARN =
+ "arn:aws:kms:" + REGION + ":000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
+ private static final String DEFAULT_KEY_ARN =
+ "arn:aws:kms:" + REGION + ":000000000000:key/0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0";
+
+ private static RequestSpecification ec2(String action, String... params) {
+ RequestSpecification request = given()
+ .formParam("Action", action)
+ .header("Authorization", AUTH_HEADER);
+ for (int i = 0; i + 1 < params.length; i += 2) {
+ request = request.formParam(params[i], params[i + 1]);
+ }
+ return request;
+ }
+
+ private static RequestSpecification createVolume(String... extraParams) {
+ String[] params = new String[extraParams.length + 4];
+ params[0] = "AvailabilityZone";
+ params[1] = AZ;
+ params[2] = "Size";
+ params[3] = "1";
+ System.arraycopy(extraParams, 0, params, 4, extraParams.length);
+ return ec2("CreateVolume", params);
+ }
+
+ /** The key an encrypted volume was created with, or an empty string when the element is missing. */
+ private static String createdVolumeKey(String... extraParams) {
+ return createVolume(extraParams).when().post("/").then().statusCode(200)
+ .body("CreateVolumeResponse.encrypted", equalTo("true"))
+ .extract().xmlPath().getString("CreateVolumeResponse.kmsKeyId");
+ }
+
+ private static String describedVolumeKey(String volumeId) {
+ return ec2("DescribeVolumes", "VolumeId.1", volumeId)
+ .when().post("/").then().statusCode(200)
+ .extract().xmlPath().getString("DescribeVolumesResponse.volumeSet.item.kmsKeyId");
+ }
+
+ private static void assertNoKmsKeyId(String xml) {
+ assertEquals(List.of(), XmlParser.extractAll(xml, "kmsKeyId"), xml);
+ }
+
+ private static void call(String action, String... params) {
+ ec2(action, params).when().post("/").then().statusCode(200);
+ }
+
+ @Test
+ void createVolumeAndDescribeVolumesReportTheKeyAnEncryptedVolumeWasCreatedWith() {
+ String volumeId = createVolume("Encrypted", "true", "KmsKeyId", KEY_ARN)
+ .when().post("/").then().statusCode(200)
+ .body("CreateVolumeResponse.volumeId", startsWith("vol-"))
+ .body("CreateVolumeResponse.encrypted", equalTo("true"))
+ .body("CreateVolumeResponse.kmsKeyId", equalTo(KEY_ARN))
+ .extract().path("CreateVolumeResponse.volumeId");
+
+ ec2("DescribeVolumes", "VolumeId.1", volumeId)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeVolumesResponse.volumeSet.item.volumeId", equalTo(volumeId))
+ .body("DescribeVolumesResponse.volumeSet.item.encrypted", equalTo("true"))
+ .body("DescribeVolumesResponse.volumeSet.item.kmsKeyId", equalTo(KEY_ARN));
+ }
+
+ @Test
+ void aVolumeThatIsNotEncryptedHasNoKmsKeyIdElement() {
+ String created = createVolume("KmsKeyId", KEY_ARN)
+ .when().post("/").then().statusCode(200)
+ .body("CreateVolumeResponse.encrypted", equalTo("false"))
+ .extract().asString();
+ assertNoKmsKeyId(created);
+
+ assertNoKmsKeyId(ec2("DescribeVolumes", "VolumeId.1", XmlParser.extractFirst(created, "volumeId", null))
+ .when().post("/").then().statusCode(200)
+ .body("DescribeVolumesResponse.volumeSet.item.encrypted", equalTo("false"))
+ .extract().asString());
+ }
+
+ /**
+ * An encrypted volume that names no key gets the region's default EBS key as it is when the
+ * volume is created; changing the default later leaves the volume's key alone.
+ */
+ @Test
+ void anEncryptedVolumeThatNamesNoKeyGetsTheRegionsDefaultKeyOfTheMoment() {
+ assertEquals("alias/aws/ebs", createdVolumeKey("Encrypted", "true"));
+ try {
+ call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
+ String volumeId = createVolume("Encrypted", "true").when().post("/").then().statusCode(200)
+ .body("CreateVolumeResponse.kmsKeyId", equalTo(DEFAULT_KEY_ARN))
+ .extract().path("CreateVolumeResponse.volumeId");
+
+ call("ResetEbsDefaultKmsKeyId");
+ assertEquals(DEFAULT_KEY_ARN, describedVolumeKey(volumeId));
+ } finally {
+ call("ResetEbsDefaultKmsKeyId");
+ }
+ }
+
+ /**
+ * With encryption by default on, a CreateVolume volume that names no key gets the default key,
+ * and one that names a key keeps it. AWS refuses a KmsKeyId without Encrypted=true even then;
+ * Floci accepts it, and the volume, encrypted by default, keeps the key it names.
+ */
+ @Test
+ void volumesEncryptedByDefaultGetTheDefaultKeyUnlessTheyNameOne() {
+ call("EnableEbsEncryptionByDefault");
+ try {
+ call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
+ assertEquals(DEFAULT_KEY_ARN, createdVolumeKey());
+ assertEquals(KEY_ARN, createdVolumeKey("Encrypted", "true", "KmsKeyId", KEY_ARN));
+ assertEquals(KEY_ARN, createdVolumeKey("KmsKeyId", KEY_ARN));
+ } finally {
+ // Encryption by default first: it is the setting the other tests here depend on.
+ try {
+ call("DisableEbsEncryptionByDefault");
+ } finally {
+ call("ResetEbsDefaultKmsKeyId");
+ }
+ }
+ }
+
+ @Test
+ void anInstanceRootVolumeThatIsNotEncryptedHasNoKey() {
+ String instanceId = launch();
+ try {
+ assertNoKmsKeyId(attachedVolume(instanceId, "/dev/xvda", "false"));
+ } finally {
+ call("TerminateInstances", "InstanceId.1", instanceId);
+ }
+ }
+
+ /**
+ * AWS refuses a launch mapping that names a key without Encrypted=true; Floci accepts it, and
+ * with encryption by default off the volume stays unencrypted and keeps no key.
+ */
+ @Test
+ void aLaunchMappingKeyWithoutEncryptedLeavesTheVolumeUnencrypted() {
+ String instanceId = launch(
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "1",
+ "BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN);
+ try {
+ assertNoKmsKeyId(attachedVolume(instanceId, "/dev/sdf", "false"));
+ } finally {
+ call("TerminateInstances", "InstanceId.1", instanceId);
+ }
+ }
+
+ /**
+ * A launch's encrypted volumes keep the key their block device mapping names, the root
+ * included, and one that names none gets the region's default.
+ */
+ @Test
+ void launchVolumesKeepTheKeyTheirMappingNames() {
+ String instanceId = launch(
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.Ebs.Encrypted", "true",
+ "BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN,
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "1",
+ "BlockDeviceMapping.2.Ebs.Encrypted", "true",
+ "BlockDeviceMapping.2.Ebs.KmsKeyId", DEFAULT_KEY_ARN,
+ "BlockDeviceMapping.3.DeviceName", "/dev/sdg",
+ "BlockDeviceMapping.3.Ebs.VolumeSize", "1",
+ "BlockDeviceMapping.3.Ebs.Encrypted", "true");
+ try {
+ assertEquals(List.of(KEY_ARN), XmlParser.extractAll(attachedVolume(instanceId, "/dev/xvda"), "kmsKeyId"));
+ assertEquals(List.of(DEFAULT_KEY_ARN),
+ XmlParser.extractAll(attachedVolume(instanceId, "/dev/sdf"), "kmsKeyId"));
+ assertEquals(List.of("alias/aws/ebs"),
+ XmlParser.extractAll(attachedVolume(instanceId, "/dev/sdg"), "kmsKeyId"));
+ } finally {
+ call("TerminateInstances", "InstanceId.1", instanceId);
+ }
+ }
+
+ /**
+ * A launch template's mapping key reaches the volume a launch from the template creates, and a
+ * request entry for the same device that names another key wins over it.
+ */
+ @Test
+ void aLaunchTemplateMappingKeyReachesTheVolume() {
+ String templateId = ec2("CreateLaunchTemplate", "LaunchTemplateName", "kms-key-" + System.nanoTime(),
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "1",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.Encrypted", "true",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN)
+ .when().post("/").then().statusCode(200)
+ .extract().path("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+ try {
+ String fromTemplate = launch("LaunchTemplate.LaunchTemplateId", templateId);
+ String overridden = launch("LaunchTemplate.LaunchTemplateId", templateId,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.Ebs.KmsKeyId", DEFAULT_KEY_ARN);
+ try {
+ assertEquals(List.of(KEY_ARN),
+ XmlParser.extractAll(attachedVolume(fromTemplate, "/dev/sdf"), "kmsKeyId"));
+ assertEquals(List.of(DEFAULT_KEY_ARN),
+ XmlParser.extractAll(attachedVolume(overridden, "/dev/sdf"), "kmsKeyId"));
+ } finally {
+ call("TerminateInstances", "InstanceId.1", fromTemplate, "InstanceId.2", overridden);
+ }
+ } finally {
+ call("DeleteLaunchTemplate", "LaunchTemplateId", templateId);
+ }
+ }
+
+ /** With encryption by default on, a launch's volumes that name no key get the default key. */
+ @Test
+ void launchVolumesEncryptedByDefaultGetTheDefaultKey() {
+ call("EnableEbsEncryptionByDefault");
+ try {
+ call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
+ String instanceId = launch();
+ try {
+ assertEquals(List.of(DEFAULT_KEY_ARN),
+ XmlParser.extractAll(attachedVolume(instanceId, "/dev/xvda"), "kmsKeyId"));
+ } finally {
+ call("TerminateInstances", "InstanceId.1", instanceId);
+ }
+ } finally {
+ try {
+ call("DisableEbsEncryptionByDefault");
+ } finally {
+ call("ResetEbsDefaultKmsKeyId");
+ }
+ }
+ }
+
+ private static String launch(String... extraParams) {
+ String[] params = new String[extraParams.length + 8];
+ params[0] = "ImageId";
+ params[1] = "ami-0abcdef1234567890";
+ params[2] = "InstanceType";
+ params[3] = "t3.micro";
+ params[4] = "MinCount";
+ params[5] = "1";
+ params[6] = "MaxCount";
+ params[7] = "1";
+ System.arraycopy(extraParams, 0, params, 8, extraParams.length);
+ return ec2("RunInstances", params).when().post("/").then().statusCode(200)
+ .extract().path("RunInstancesResponse.instancesSet.item.instanceId");
+ }
+
+ /** DescribeVolumes for the one encrypted volume attached to the instance at the device. */
+ private static String attachedVolume(String instanceId, String device) {
+ return attachedVolume(instanceId, device, "true");
+ }
+
+ /** DescribeVolumes for the one volume attached to the instance at the device. */
+ private static String attachedVolume(String instanceId, String device, String encrypted) {
+ String xml = ec2("DescribeVolumes",
+ "Filter.1.Name", "attachment.instance-id", "Filter.1.Value.1", instanceId,
+ "Filter.2.Name", "attachment.device", "Filter.2.Value.1", device)
+ .when().post("/").then().statusCode(200)
+ .extract().asString();
+ assertEquals(1, XmlParser.extractAll(xml, "size").size(), xml);
+ assertEquals(List.of(encrypted), XmlParser.extractAll(xml, "encrypted"), xml);
+ return xml;
+ }
+
+ @Test
+ void theAwsSdkReadsTheKeyFromCreateVolumeAndDescribeVolumes() {
+ try (Ec2Client client = Ec2Client.builder()
+ .endpointOverride(URI.create("http://localhost:" + RestAssured.port))
+ .region(Region.of(REGION))
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
+ .httpClientBuilder(UrlConnectionHttpClient.builder()).build()) {
+ CreateVolumeResponse created = client.createVolume(request -> request
+ .availabilityZone(AZ).size(1).encrypted(true).kmsKeyId(KEY_ARN));
+ assertEquals(KEY_ARN, created.kmsKeyId());
+ Volume described = client.describeVolumes(request -> request.volumeIds(created.volumeId()))
+ .volumes().getFirst();
+ assertEquals(KEY_ARN, described.kmsKeyId());
+
+ String plainId = client.createVolume(request -> request.availabilityZone(AZ).size(1)).volumeId();
+ assertNull(client.describeVolumes(request -> request.volumeIds(plainId))
+ .volumes().getFirst().kmsKeyId());
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdTest.java
new file mode 100644
index 0000000000..61c1acb538
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeKmsKeyIdTest.java
@@ -0,0 +1,156 @@
+package io.github.hectorvent.floci.services.ec2;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.common.XmlParser;
+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.MultivaluedMap;
+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.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * CreateVolume's KmsKeyId, sent through the Query handler to a real service. An encrypted volume
+ * keeps the key it was created with, or the region's default EBS key when it names none, and
+ * CreateVolume and DescribeVolumes report it as {@code kmsKeyId}; a volume that is not encrypted
+ * has no key, so the element is left out. The service here holds no EBS encryption settings, so
+ * the default is the AWS-managed {@code alias/aws/ebs} every account starts with.
+ */
+class Ec2VolumeKmsKeyIdTest {
+
+ private static final String REGION = "us-east-1";
+ private static final String AZ = REGION + "a";
+ private static final String KEY_ARN =
+ "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
+
+ private Ec2QueryHandler handler;
+
+ @BeforeEach
+ void setUp() {
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
+ EmulatorConfig.Ec2ServiceConfig ec2 = mock(EmulatorConfig.Ec2ServiceConfig.class);
+ when(config.defaultAccountId()).thenReturn("000000000000");
+ when(config.services()).thenReturn(services);
+ when(services.ec2()).thenReturn(ec2);
+ when(ec2.mock()).thenReturn(true);
+ when(ec2.mockPublicAddresses()).thenReturn(EmulatorConfig.Ec2ServiceConfig.MockPublicAddresses.NONE);
+ when(ec2.mockPublicAddress()).thenReturn(Optional.empty());
+ Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
+ Ec2Service service = new Ec2Service(config, mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), new AmiImageResolver(imageCatalog), imageCatalog,
+ new Ec2InstanceTypeCatalog(), new MemoryFactory());
+ handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
+ mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
+ mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"));
+ }
+
+ /** Each form CreateVolume documents for KmsKeyId is kept as the caller gave it. */
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "1234abcd-12ab-34cd-56ef-1234567890ab",
+ "alias/ExampleAlias",
+ KEY_ARN,
+ "arn:aws:kms:us-east-1:000000000000:alias/ExampleAlias"
+ })
+ void anEncryptedVolumeReportsTheKeyItWasCreatedWith(String kmsKeyId) {
+ String created = ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1",
+ "Encrypted", "true", "KmsKeyId", kmsKeyId);
+ assertEquals("true", first(created, "encrypted"));
+ assertEquals(kmsKeyId, first(created, "kmsKeyId"));
+
+ String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
+ assertEquals("true", first(described, "encrypted"));
+ assertEquals(kmsKeyId, first(described, "kmsKeyId"));
+ }
+
+ /** A blank KmsKeyId names no key, so the volume gets the default rather than an empty key. */
+ @ParameterizedTest
+ @ValueSource(strings = {"absent", "", " "})
+ void anEncryptedVolumeThatNamesNoKeyGetsTheDefaultKey(String kmsKeyId) {
+ String created = kmsKeyId.equals("absent")
+ ? ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", "true")
+ : ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", "true",
+ "KmsKeyId", kmsKeyId);
+ assertEquals("alias/aws/ebs", first(created, "kmsKeyId"));
+
+ String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
+ assertEquals("alias/aws/ebs", first(described, "kmsKeyId"));
+ }
+
+ /**
+ * AWS requires Encrypted=true beside a KmsKeyId. Floci still accepts the request, as it did
+ * before, but an unencrypted volume has no key to report.
+ */
+ @ParameterizedTest
+ @ValueSource(strings = {"", "false"})
+ void aKeyIsNotKeptOnAVolumeThatIsNotEncrypted(String encrypted) {
+ String created = encrypted.isEmpty()
+ ? ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "KmsKeyId", KEY_ARN)
+ : ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", encrypted,
+ "KmsKeyId", KEY_ARN);
+ assertEquals("false", first(created, "encrypted"));
+ assertEquals(List.of(), XmlParser.extractAll(created, "kmsKeyId"), created);
+
+ String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
+ assertEquals("false", first(described, "encrypted"));
+ assertEquals(List.of(), XmlParser.extractAll(described, "kmsKeyId"), described);
+ }
+
+ @Test
+ void theKeyStaysWithTheVolumeWhenItIsModifiedAndTagged() {
+ String volumeId = first(ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "VolumeType", "gp3",
+ "Encrypted", "true", "KmsKeyId", KEY_ARN), "volumeId");
+
+ ok("ModifyVolume", "VolumeId", volumeId, "Size", "2");
+ ok("CreateTags", "ResourceId.1", volumeId, "Tag.1.Key", "Team", "Tag.1.Value", "blue");
+
+ String described = ok("DescribeVolumes", "VolumeId.1", volumeId);
+ assertEquals("2", first(described, "size"));
+ assertEquals(KEY_ARN, first(described, "kmsKeyId"));
+ }
+
+ private String ok(String action, String... params) {
+ MultivaluedMap<String, String> form = new MultivaluedHashMap<>();
+ for (int i = 0; i < params.length; i += 2) {
+ form.add(params[i], params[i + 1]);
+ }
+ Response response = handler.handle(action, form, REGION);
+ String body = String.valueOf(response.getEntity());
+ assertEquals(200, response.getStatus(), action + ": " + body);
+ return body;
+ }
+
+ private static String first(String xml, String element) {
+ List<String> values = XmlParser.extractAll(xml, element);
+ assertFalse(values.isEmpty(), "no <" + element + "> in " + xml);
+ return values.getFirst();
+ }
+
+ private static final class MemoryFactory extends StorageFactory {
+ private MemoryFactory() {
+ super(null, null);
+ }
+
+ @Override
+ public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
+ TypeReference<Map<String, V>> typeReference) {
+ return AccountAwareStorageBackend.inMemory("000000000000");
+ }
+ }
+}