fix(ec2): honour DryRun on the volume and tag calls (#8244)

CreateVolume, DeleteVolume, AttachVolume, DetachVolume, CreateTags and
DeleteTags ignored DryRun=true and carried out the request. The AWS EBS
CSI driver's controller sends a dry run CreateVolume, tagged
CSIVolumeName=IopsLimitDryRun, every time it starts in order to probe
the IOPS limits. AWS answers it with DryRunOperation and creates nothing,
while Floci created a real 4 GiB volume, with its backing file, on every
restart. A dry run DeleteVolume really deleted the volume.

Each of these calls now runs the checks its real path makes and then
answers DryRunOperation (HTTP 412) without changing anything, the
convention ModifyVolume already follows. CreateVolume answers once its
parameters and tag specification are read. DeleteVolume first requires
the volume to exist and be detached. AttachVolume requires the volume,
the instance, its state, the Availability Zone and the device to pass.
DetachVolume requires the attachment to match and the root device rules
to allow it. CreateTags checks the ids, the keys, the lengths, that each
resource exists and the per-resource limit. DeleteTags checks the ids
and the keys. No backing device is created, deleted, attached or
detached by a dry run.

Every handler read of DryRun goes through dryRun(p), which refuses a blank
or non-boolean value with InvalidParameterValue, as DeleteSecurityGroup and
DeleteLaunchTemplateVersions already did by hand; both now use it, and
checkDryRun sits on it. Every DryRunOperation comes from
Ec2Service.dryRunOperation().

Tests: a DryRun value that is not a boolean is refused on the six volume
and tag calls; under IAM enforcement a dry run needs the caller's current
permission. AwsSdkErrors.assertAwsError replaces the copies the route and
S3 enforcement tests kept.
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-13 17:22:19 -04:00
1 parent 39c29de635
commit 3ae36eb7be
8 files changed
+436 -69

No files matched your search

@@ -947,7 +947,7 @@ public class Ec2QueryHandler {
Reservation res = service.runInstances(region, imageId, instanceType, minCount, maxCount,
keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn,
associatePublicIp, networkInterfaceId, networkInterfaceDeviceIndex, null, metadataOptions,
creditSpecificationCpuCredits, userDataEncoded, Boolean.parseBoolean(p.getFirst("DryRun")),
creditSpecificationCpuCredits, userDataEncoded, dryRun(p),
p.getFirst("Placement.HostId"), p.getFirst("Placement.Tenancy"), blockDeviceMappings,
"spot".equals(marketType), privateIpAddress, capacitySpecification);
@@ -2361,11 +2361,20 @@ public class Ec2QueryHandler {
}
private void checkDryRun(MultivaluedMap<String, String> params) {
if (Boolean.parseBoolean(params.getFirst("DryRun"))) {
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
if (dryRun(params)) {
throw Ec2Service.dryRunOperation();
}
}
/** The request's DryRun flag. A blank or non-boolean value is refused rather than read as false. */
private boolean dryRun(MultivaluedMap<String, String> params) {
String value = params.getFirst("DryRun");
if (value != null && value.isBlank()) {
throw new AwsException("InvalidParameterValue", "DryRun is not a valid boolean.", 400);
}
return Boolean.TRUE.equals(parseOptionalBoolean(value, "DryRun"));
}
private Response handleModifyIpam(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
List<String> addOperatingRegions = new ArrayList<>();
@@ -3649,12 +3658,8 @@ public class Ec2QueryHandler {
}
private Response handleDeleteSecurityGroup(MultivaluedMap<String, String> p, String region) {
String dryRun = p.getFirst("DryRun");
if (dryRun != null && dryRun.isBlank()) {
throw new AwsException("InvalidParameterValue", "DryRun is not a valid boolean.", 400);
}
String groupId = service.deleteSecurityGroup(region, p.getFirst("GroupId"), p.getFirst("GroupName"),
Boolean.TRUE.equals(parseOptionalBoolean(dryRun, "DryRun")));
dryRun(p));
return xmlResponse(new XmlBuilder().start("DeleteSecurityGroupResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString()).elem("return", "true")
.elem("groupId", groupId).end("DeleteSecurityGroupResponse").build());
@@ -4206,7 +4211,7 @@ public class Ec2QueryHandler {
String v = p.getFirst("Tag." + i + ".Value");
tagList.add(creationTag(k, v));
}
service.createTags(region, resourceIds, tagList);
service.createTags(region, resourceIds, tagList, dryRun(p));
return booleanResponse("CreateTags");
}
@@ -4219,7 +4224,7 @@ public class Ec2QueryHandler {
String v = p.getFirst("Tag." + i + ".Value");
tagList.add(new Tag(k, v));
}
service.deleteTags(region, resourceIds, tagList);
service.deleteTags(region, resourceIds, tagList, dryRun(p));
return booleanResponse("DeleteTags");
}
@@ -4403,7 +4408,7 @@ public class Ec2QueryHandler {
}
service.replaceRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, pcxId,
instanceId, networkInterfaceId,
Boolean.parseBoolean(p.getFirst("DryRun")));
dryRun(p));
return booleanResponse("ReplaceRoute");
}
@@ -4652,7 +4657,7 @@ public class Ec2QueryHandler {
InstanceConnectEndpoint endpoint = service.createInstanceConnectEndpoint(region, p.getFirst("SubnetId"),
getList(p, "SecurityGroupId"), preserve == null ? null : Boolean.parseBoolean(preserve),
p.getFirst("IpAddressType"), parseTagsForResource(p, "instance-connect-endpoint"),
p.getFirst("ClientToken"), Boolean.parseBoolean(p.getFirst("DryRun")));
p.getFirst("ClientToken"), dryRun(p));
XmlBuilder xml = new XmlBuilder()
.start("CreateInstanceConnectEndpointResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -5271,7 +5276,7 @@ public class Ec2QueryHandler {
p.getFirst("LaunchTemplateId"),
p.getFirst("LaunchTemplateName"),
firstPresent(p, "SetDefaultVersion", "DefaultVersion"),
Boolean.parseBoolean(p.getFirst("DryRun")));
dryRun(p));
XmlBuilder xml = new XmlBuilder()
.start("ModifyLaunchTemplateResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -5292,13 +5297,9 @@ public class Ec2QueryHandler {
}
private Response handleDeleteLaunchTemplateVersions(MultivaluedMap<String, String> p, String region) {
String dryRun = p.getFirst("DryRun");
if (dryRun != null && dryRun.isBlank()) {
throw new AwsException("InvalidParameterValue", "DryRun is not a valid boolean.", 400);
}
List<Ec2Service.LaunchTemplateVersionDeletion> results = service.deleteLaunchTemplateVersions(
region, p.getFirst("LaunchTemplateId"), p.getFirst("LaunchTemplateName"),
getList(p, "LaunchTemplateVersion"), Boolean.TRUE.equals(parseOptionalBoolean(dryRun, "DryRun")));
getList(p, "LaunchTemplateVersion"), dryRun(p));
XmlBuilder xml = new XmlBuilder().start("DeleteLaunchTemplateVersionsResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString());
for (boolean success : List.of(true, false)) {
@@ -6769,6 +6770,9 @@ public class Ec2QueryHandler {
String kmsKeyId = p.getFirst("KmsKeyId");
List<Tag> volumeTags = parseTagsForResource(p, "volume");
// Answered once the parameters and tags are read, everything a real create checks, so a
// dry run stores no volume and creates no backing file.
checkDryRun(p);
Volume vol = service.createVolume(region, availabilityZone, volumeType, size,
encrypted, kmsKeyId, iops, throughput, snapshotId, volumeTags);
@@ -6798,7 +6802,7 @@ public class Ec2QueryHandler {
}
private Response handleDeleteVolume(MultivaluedMap<String, String> p, String region) {
service.deleteVolume(region, p.getFirst("VolumeId"));
service.deleteVolume(region, p.getFirst("VolumeId"), dryRun(p));
return booleanResponse("DeleteVolume");
}
@@ -6814,9 +6818,8 @@ public class Ec2QueryHandler {
Boolean multiAttachEnabled = p.getFirst("MultiAttachEnabled") != null
? Boolean.parseBoolean(p.getFirst("MultiAttachEnabled"))
: null;
boolean dryRun = Boolean.parseBoolean(p.getFirst("DryRun"));
VolumeModification mod = service.modifyVolume(region, volumeId, size, volumeType,
iops, throughput, multiAttachEnabled, dryRun);
iops, throughput, multiAttachEnabled, dryRun(p));
XmlBuilder xml = new XmlBuilder()
.start("ModifyVolumeResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -6895,7 +6898,8 @@ public class Ec2QueryHandler {
String volumeId = p.getFirst("VolumeId");
String instanceId = p.getFirst("InstanceId");
String device = p.getFirst("Device");
VolumeAttachment attachment = service.attachVolume(region, volumeId, instanceId, device);
VolumeAttachment attachment = service.attachVolume(region, volumeId, instanceId, device,
dryRun(p));
return volumeAttachmentResponse("AttachVolume", attachment, "attaching");
}
@@ -6904,7 +6908,8 @@ public class Ec2QueryHandler {
String instanceId = p.getFirst("InstanceId");
String device = p.getFirst("Device");
boolean force = "true".equalsIgnoreCase(p.getFirst("Force"));
VolumeAttachment attachment = service.detachVolume(region, volumeId, instanceId, device, force);
VolumeAttachment attachment = service.detachVolume(region, volumeId, instanceId, device, force,
dryRun(p));
return volumeAttachmentResponse("DetachVolume", attachment, "detaching");
}
@@ -4157,7 +4157,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
if (dryRun) {
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
synchronized (attachmentTopologyLock(region)) {
if (finalSubnetId != null) {
@@ -7988,8 +7988,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
"The security group '" + selectedId + "' has a dependent object", 400);
}
if (dryRun) {
throw new AwsException("DryRunOperation",
"Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
securityGroups.delete(key(region, selectedId));
removeTags(region, selectedId);
@@ -9910,7 +9909,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
nextDefault = String.valueOf(requested);
}
if (dryRun) {
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
launchTemplate.setDefaultVersionNumber(nextDefault);
launchTemplates.put(key(region, launchTemplate.getLaunchTemplateId()), launchTemplate);
@@ -10076,8 +10075,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
synchronized (template) {
ensureLaunchTemplatePresent(region, template, id);
if (dryRun) {
throw new AwsException("DryRunOperation",
"Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
ensureLaunchTemplateVersions(template);
int lastIssued = lastIssuedLaunchTemplateVersion(template);
@@ -10468,9 +10466,25 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value);
}
/**
* What EC2 answers a dry run with once the request has passed every check the real call makes:
* DryRunOperation, HTTP 412, nothing changed.
*/
static AwsException dryRunOperation() {
return new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
}
// ─── Tags ──────────────────────────────────────────────────────────────────
public void createTags(String region, List<String> resourceIds, List<Tag> tagList) {
createTags(region, resourceIds, tagList, false);
}
/**
* As above; with {@code dryRun} the request passes every check a write makes, the
* per-resource limit included, and is then answered with DryRunOperation, nothing written.
*/
public void createTags(String region, List<String> resourceIds, List<Tag> tagList, boolean dryRun) {
validateTagTargets(region, resourceIds);
validateUserTagKeys(tagList);
validateTagLengths(tagList);
@@ -10484,6 +10498,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
for (String resourceId : resourceIds) {
requireWithinTagLimit(region, resourceId, tagList);
}
if (dryRun) {
throw dryRunOperation();
}
createSystemTags(region, resourceIds, tagList);
});
}
@@ -10712,8 +10729,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteTags(String region, List<String> resourceIds, List<Tag> tagList) {
deleteTags(region, resourceIds, tagList, false);
}
/** As above; with {@code dryRun} the request is validated and then answered with DryRunOperation. */
public void deleteTags(String region, List<String> resourceIds, List<Tag> tagList, boolean dryRun) {
validateTagTargets(region, resourceIds);
validateUserTagKeys(tagList);
if (dryRun) {
throw dryRunOperation();
}
deleteResourceTags(region, resourceIds, tagList);
}
@@ -12251,8 +12276,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
: null;
if (dryRun) {
throw new AwsException("DryRunOperation",
"Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
// The target the request does not name is cleared rather than carried over from the
@@ -13119,7 +13143,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
if (dryRun) {
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
String token = clientToken != null && !clientToken.isBlank() ? clientToken : null;
if (token != null) {
@@ -14850,6 +14874,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteVolume(String region, String volumeId) {
deleteVolume(region, volumeId, false);
}
/**
* As above; with {@code dryRun} the volume must still exist and be detached, and the request
* is then answered with DryRunOperation, the volume and its backing storage left in place.
*/
public void deleteVolume(String region, String volumeId, boolean dryRun) {
if (volumeId == null || volumeId.isBlank()) {
throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
}
@@ -14862,6 +14894,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
throw new AwsException("VolumeInUse",
"Volume " + volumeId + " is currently attached to an instance", 400);
}
if (dryRun) {
throw dryRunOperation();
}
if (volumeBlockDeviceManager != null && volumeBlockDeviceManager.isAvailable()) {
if (!volumeBlockDeviceManager.deleteVolume(volumeId)) {
throw new AwsException("InternalError",
@@ -15008,7 +15043,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
if (dryRun) {
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
throw dryRunOperation();
}
Integer originalSize = volume.getSize();
@@ -15147,6 +15182,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public VolumeAttachment attachVolume(String region, String volumeId, String instanceId, String device) {
return attachVolume(region, volumeId, instanceId, device, false);
}
/**
* As above; with {@code dryRun} the volume, the instance and the device pass every check an
* attachment makes, and the request is then answered with DryRunOperation, nothing attached.
*/
public VolumeAttachment attachVolume(String region, String volumeId, String instanceId, String device,
boolean dryRun) {
ensureDefaultResources(region);
if (volumeId == null || volumeId.isEmpty()) {
throw new AwsException("MissingParameter",
@@ -15172,14 +15216,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
"The device '" + device + "' is already in use by an attachment in progress", 400);
}
try {
return attachVolumeToInstance(region, volumeId, instanceId, device);
return attachVolumeToInstance(region, volumeId, instanceId, device, dryRun);
} finally {
pendingVolumeAttachments.remove(target);
}
});
}
private VolumeAttachment attachVolumeToInstance(String region, String volumeId, String instanceId, String device) {
private VolumeAttachment attachVolumeToInstance(String region, String volumeId, String instanceId, String device,
boolean dryRun) {
Volume volume = getRequiredVolume(region, volumeId);
Instance inst = getRequiredInstance(region, instanceId);
if (!List.of("running", "stopped").contains(inst.getState().getName())) {
@@ -15215,6 +15260,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
}
if (dryRun) {
throw dryRunOperation();
}
if (volumeBlockDeviceManager != null) {
volumeBlockDeviceManager.attachVolume(volume, inst, device);
@@ -15236,6 +15284,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public VolumeAttachment detachVolume(String region, String volumeId, String instanceId, String device, boolean force) {
return detachVolume(region, volumeId, instanceId, device, force, false);
}
/**
* As above; with {@code dryRun} the attachment passes every check a detach makes, and the
* request is then answered with DryRunOperation, the volume left attached.
*/
public VolumeAttachment detachVolume(String region, String volumeId, String instanceId, String device,
boolean force, boolean dryRun) {
if (volumeId == null || volumeId.isEmpty()) {
throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
}
@@ -15265,6 +15322,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
"Device " + inst.getRootDeviceName() + " has the root partition on it. Detaching it will damage the " +
"filesystem/partition tables. To force detachment, use the force parameter", 400);
}
if (dryRun) {
throw dryRunOperation();
}
target.setState("detached");
volume.getAttachments().clear();
volume.setState("available");