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

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

CreateVolume ignored KmsKeyId, so neither CreateVolume nor
DescribeVolumes reported kmsKeyId, even for a volume created encrypted
with a named key. A volume CreateVolume makes encrypted now keeps the
request's KmsKeyId as given, or gets the region's default EBS key when
it names none, and both responses report it. A volume that is not
encrypted keeps no key, and volumes stored before this change load as
they were, with no kmsKeyId.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): read volume responses with XmlParser

The volume KMS key tests read CreateVolume and DescribeVolumes
responses with a regular expression, substring offsets and plain
string matches. They now read them with XmlParser, as other EC2 tests
do, so element values are parsed and decoded as XML. Test-only; the
assertions are unchanged.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): keep the KMS key a launch's block device mapping names

A launch's encrypted volumes got the region's default EBS key even
when their block device mapping, or the launch template's, named
Ebs.KmsKeyId. The key now goes from the mapping to the volume, root
included, and a mapping that names none still gets the default.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): pin the launch volume key rules

Pin that a launch mapping naming a key without Encrypted leaves the
volume unencrypted and keyless, and that a request entry's key wins
over its launch template's. Delete the test's launch template, reuse
the launch helpers in the root-volume test, and document that a launch
volume encrypted only by its snapshot gets the region's default key,
since snapshots record no key.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-28 08:46:27 -04:00
1 parent db86fc7d72
commit a83fa9c9e4
7 files changed
+632 -9

No files matched your search

@@ -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()));
}
@@ -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)) {
@@ -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; }