mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): CreateSnapshot, DeleteSnapshot and CopySnapshot for EBS volumes (#8360)
EC2 now serves CreateSnapshot, DeleteSnapshot and CopySnapshot for EBS volumes. CreateSnapshot takes the volume's size and encryption, is owned by the caller and accepts snapshot tags from TagSpecification. DeleteSnapshot removes the snapshot and its tags, and refuses with InvalidSnapshot.InUse while a registered AMI uses it. CopySnapshot copies from SourceRegion into the request's Region as a new snapshot, encrypted when the source is, when Encrypted=true or when the destination's encryption by default says so, without the source's tags. All three honor DryRun, keeping the error the real call would give. An unsigned, unversioned CopySnapshot now routes to EC2, since Floci's ElastiCache does not serve it. RegisterImage builds its snapshot records through the same helper. Closes #7943
This commit is contained in:
1 parent
2c0e4e5ff8
commit
ea594d8d2a
6 files changed
+487
-13
No files matched your search
@@ -513,6 +513,8 @@ public class AwsQueryController {
|
||||
Map.entry("DescribeEventCategories", "redshift"),
|
||||
Map.entry("DescribeAccountAttributes", "ec2"),
|
||||
Map.entry("DescribeSnapshots", "ec2"),
|
||||
// ElastiCache defines it too, but Floci's ElastiCache does not serve it.
|
||||
Map.entry("CopySnapshot", "ec2"),
|
||||
Map.entry("DescribeAccountLimits", "elasticloadbalancing"),
|
||||
Map.entry("CreateIntegration", "redshift"),
|
||||
Map.entry("DeleteIntegration", "redshift"),
|
||||
|
||||
@@ -264,6 +264,9 @@ public class Ec2QueryHandler {
|
||||
case "DeregisterImage" -> handleDeregisterImage(params, region);
|
||||
case "CopyImage" -> handleCopyImage(params, region);
|
||||
case "DescribeSnapshots" -> handleDescribeSnapshots(params, region);
|
||||
case "CreateSnapshot" -> handleCreateSnapshot(params, region);
|
||||
case "DeleteSnapshot" -> handleDeleteSnapshot(params, region);
|
||||
case "CopySnapshot" -> handleCopySnapshot(params, region);
|
||||
// Tags
|
||||
case "CreateTags" -> handleCreateTags(params, region);
|
||||
case "DeleteTags" -> handleDeleteTags(params, region);
|
||||
@@ -4217,6 +4220,42 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateSnapshot. The response reports {@code pending} with no progress yet, as AWS does for
|
||||
* a snapshot it has just started; DescribeSnapshots then reports it {@code completed}.
|
||||
*/
|
||||
private Response handleCreateSnapshot(MultivaluedMap<String, String> p, String region) {
|
||||
List<Tag> snapshotTags = parseTagsForResource(p, "snapshot", Set.of("snapshot"));
|
||||
Snapshot snapshot = service.createSnapshot(region, p.getFirst("VolumeId"),
|
||||
p.getFirst("Description"), snapshotTags, dryRun(p));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateSnapshotResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.raw(snapshotXml(snapshot, "pending", ""))
|
||||
.end("CreateSnapshotResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDeleteSnapshot(MultivaluedMap<String, String> p, String region) {
|
||||
service.deleteSnapshot(region, p.getFirst("SnapshotId"), dryRun(p));
|
||||
return booleanResponse("DeleteSnapshot");
|
||||
}
|
||||
|
||||
private Response handleCopySnapshot(MultivaluedMap<String, String> p, String region) {
|
||||
List<Tag> snapshotTags = parseTagsForResource(p, "snapshot", Set.of("snapshot"));
|
||||
boolean encrypted = Boolean.TRUE.equals(parseOptionalBoolean(p.getFirst("Encrypted"), "Encrypted"));
|
||||
Snapshot copy = service.copySnapshot(region, p.getFirst("SourceRegion"),
|
||||
p.getFirst("SourceSnapshotId"), p.getFirst("Description"), encrypted, snapshotTags,
|
||||
dryRun(p));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CopySnapshotResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("snapshotId", copy.getSnapshotId())
|
||||
.raw(tagSetXml(copy.getTags()))
|
||||
.end("CopySnapshotResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
// ─── Tag handlers ─────────────────────────────────────────────────────────
|
||||
|
||||
// AWS stores a tag created without a Value as an empty string. DeleteTags is the exception:
|
||||
@@ -6748,11 +6787,15 @@ public class Ec2QueryHandler {
|
||||
}
|
||||
|
||||
private String snapshotXml(Snapshot snapshot) {
|
||||
return snapshotXml(snapshot, snapshot.getState(), snapshot.getProgress());
|
||||
}
|
||||
|
||||
private String snapshotXml(Snapshot snapshot, String status, String progress) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("snapshotId", snapshot.getSnapshotId())
|
||||
.elem("ownerId", snapshot.getOwnerId())
|
||||
.elem("status", snapshot.getState())
|
||||
.elem("progress", snapshot.getProgress())
|
||||
.elem("status", status)
|
||||
.elem("progress", progress)
|
||||
.elem("encrypted", String.valueOf(snapshot.isEncrypted()))
|
||||
.elem("description", snapshot.getDescription());
|
||||
if (snapshot.getVolumeId() != null) {
|
||||
|
||||
@@ -2253,6 +2253,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
*/
|
||||
private final Object imageRegistryLock = new Object();
|
||||
|
||||
/** The volume id AWS reports on a snapshot made by CopySnapshot, which has no source volume. */
|
||||
private static final String COPIED_SNAPSHOT_VOLUME_ID = "vol-ffffffff";
|
||||
|
||||
/**
|
||||
* The monitor guarding the AMI registry against a launch and a deregistration interleaving.
|
||||
* Exposed package-private so a test can hold it and land a tombstone at a chosen point in a
|
||||
@@ -9819,6 +9822,139 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateSnapshot of an EBS volume. The snapshot takes the volume's size and encryption and is
|
||||
* owned by the caller. AWS reports it as {@code pending} until the data is copied; Floci's
|
||||
* copy completes within the call, so the stored snapshot is {@code completed} and a caller
|
||||
* that waits for that state returns on its first poll. With {@code dryRun} the volume must
|
||||
* still exist, and the request is then answered with DryRunOperation, nothing created.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateSnapshot.html">CreateSnapshot</a>
|
||||
*/
|
||||
public Snapshot createSnapshot(String region, String volumeId, String description, List<Tag> snapshotTags,
|
||||
boolean dryRun) {
|
||||
if (volumeId == null || volumeId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter VolumeId", 400);
|
||||
}
|
||||
return withVolumeLock(volumeId, () -> {
|
||||
Volume volume = volumes.get(key(region, volumeId)).flatMap(v -> settleEndedHolders(region, v))
|
||||
.orElseThrow(() -> new AwsException("InvalidVolume.NotFound",
|
||||
"The volume '" + volumeId + "' does not exist.", 400));
|
||||
if (dryRun) {
|
||||
throw dryRunOperation();
|
||||
}
|
||||
Snapshot snapshot = newSnapshot(region, "snap-" + randomHex(17), volumeId, volume.getSize(),
|
||||
description, volume.isEncrypted());
|
||||
storeSnapshot(region, snapshot, snapshotTags);
|
||||
return snapshot;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteSnapshot. A snapshot backing a registered AMI is refused with InvalidSnapshot.InUse:
|
||||
* "You cannot delete a snapshot of the root device of an EBS volume used by a registered AMI.
|
||||
* You must first deregister the AMI before you can delete the snapshot." With {@code dryRun}
|
||||
* the snapshot must still exist and be unused, and the request is then answered with
|
||||
* DryRunOperation, the snapshot and its tags left in place.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DeleteSnapshot.html">DeleteSnapshot</a>
|
||||
*/
|
||||
public void deleteSnapshot(String region, String snapshotId, boolean dryRun) {
|
||||
if (snapshotId == null || snapshotId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter SnapshotId", 400);
|
||||
}
|
||||
// Under the lock registration publishes snapshot references under, so an AMI being
|
||||
// registered on this snapshot is either seen here or sees the snapshot gone, and under the
|
||||
// snapshot's own lock, which a tag write holds, so CreateTags cannot tag it after it is gone.
|
||||
synchronized (imageRegistryLock) {
|
||||
synchronized (lockFor(key(region, snapshotId))) {
|
||||
deleteSnapshotLocked(region, snapshotId, dryRun);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteSnapshotLocked(String region, String snapshotId, boolean dryRun) {
|
||||
if (snapshots.get(key(region, snapshotId)).isEmpty()) {
|
||||
throw new AwsException("InvalidSnapshot.NotFound",
|
||||
"The snapshot '" + snapshotId + "' does not exist.", 400);
|
||||
}
|
||||
Optional<Image> user = registeredImages.scan(k -> true).stream()
|
||||
.filter(image -> region.equals(image.getRegion()))
|
||||
.filter(image -> !DEREGISTERED_STATE.equals(image.getState()))
|
||||
.filter(image -> image.getBlockDeviceMappings().stream()
|
||||
.map(BlockDeviceMapping::getEbs)
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(ebs -> snapshotId.equals(ebs.getSnapshotId())))
|
||||
.findFirst();
|
||||
if (user.isPresent()) {
|
||||
throw new AwsException("InvalidSnapshot.InUse",
|
||||
"The snapshot " + snapshotId + " is currently in use by " + user.get().getImageId(), 400);
|
||||
}
|
||||
if (dryRun) {
|
||||
throw dryRunOperation();
|
||||
}
|
||||
snapshots.delete(key(region, snapshotId));
|
||||
removeTags(region, snapshotId);
|
||||
}
|
||||
|
||||
/**
|
||||
* CopySnapshot. As with CopyImage the request's own region is the destination and
|
||||
* {@code SourceRegion} names where the source lives. The copy is an independent snapshot with
|
||||
* its own id, owned by the caller; it stays encrypted when the source is, and becomes
|
||||
* encrypted when the request or the destination's encryption by default says so. Tags are not
|
||||
* copied: "User-defined tags are not copied from the source snapshot to the snapshot copy."
|
||||
* With {@code dryRun} the source must still exist, and the request is then answered with
|
||||
* DryRunOperation, nothing copied.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopySnapshot.html">CopySnapshot</a>
|
||||
*/
|
||||
public Snapshot copySnapshot(String destinationRegion, String sourceRegion, String sourceSnapshotId,
|
||||
String description, boolean encrypted, List<Tag> snapshotTags,
|
||||
boolean dryRun) {
|
||||
if (sourceRegion == null || sourceRegion.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter SourceRegion", 400);
|
||||
}
|
||||
if (sourceSnapshotId == null || sourceSnapshotId.isBlank()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter SourceSnapshotId", 400);
|
||||
}
|
||||
Snapshot source = snapshots.get(key(sourceRegion, sourceSnapshotId))
|
||||
.orElseThrow(() -> new AwsException("InvalidSnapshot.NotFound",
|
||||
"The snapshot '" + sourceSnapshotId + "' does not exist.", 400));
|
||||
if (dryRun) {
|
||||
throw dryRunOperation();
|
||||
}
|
||||
Snapshot copy = newSnapshot(destinationRegion, "snap-" + randomHex(17), COPIED_SNAPSHOT_VOLUME_ID,
|
||||
source.getVolumeSize(), description,
|
||||
source.isEncrypted() || encrypted || ebsEncryptionByDefault(destinationRegion));
|
||||
storeSnapshot(destinationRegion, copy, snapshotTags);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** A completed snapshot record owned by the caller, not yet stored. */
|
||||
private Snapshot newSnapshot(String region, String snapshotId, String volumeId, Integer volumeSize,
|
||||
String description, boolean encrypted) {
|
||||
Snapshot snapshot = new Snapshot();
|
||||
snapshot.setSnapshotId(snapshotId);
|
||||
snapshot.setVolumeId(volumeId);
|
||||
snapshot.setVolumeSize(volumeSize);
|
||||
snapshot.setOwnerId(callerAccountId());
|
||||
snapshot.setState("completed");
|
||||
snapshot.setDescription(description != null ? description : "");
|
||||
snapshot.setStartTime(Instant.now());
|
||||
snapshot.setEncrypted(encrypted);
|
||||
snapshot.setRegion(region);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private void storeSnapshot(String region, Snapshot snapshot, List<Tag> snapshotTags) {
|
||||
if (snapshotTags != null && !snapshotTags.isEmpty()) {
|
||||
snapshot.setTags(new ArrayList<>(snapshotTags));
|
||||
putTags(region, snapshot.getSnapshotId(), new ArrayList<>(snapshotTags));
|
||||
}
|
||||
snapshots.put(key(region, snapshot.getSnapshotId()), snapshot);
|
||||
}
|
||||
|
||||
// ─── Launch Templates ─────────────────────────────────────────────────────
|
||||
|
||||
public LaunchTemplate createLaunchTemplate(String region, String name, LaunchTemplateData data,
|
||||
@@ -10580,16 +10716,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
private Snapshot snapshotFrom(String region, String snapshotId, Image image, BlockDeviceMapping mapping) {
|
||||
EbsBlockDevice ebs = mapping.getEbs();
|
||||
Snapshot snapshot = new Snapshot();
|
||||
snapshot.setSnapshotId(snapshotId);
|
||||
snapshot.setOwnerId(callerAccountId());
|
||||
snapshot.setState("completed");
|
||||
snapshot.setDescription("Created by RegisterImage for " + image.getName());
|
||||
snapshot.setStartTime(Instant.now());
|
||||
snapshot.setVolumeSize(ebs.getVolumeSize());
|
||||
snapshot.setEncrypted(Boolean.TRUE.equals(ebs.getEncrypted()));
|
||||
snapshot.setRegion(region);
|
||||
return snapshot;
|
||||
return newSnapshot(region, snapshotId, null, ebs.getVolumeSize(),
|
||||
"Created by RegisterImage for " + image.getName(), Boolean.TRUE.equals(ebs.getEncrypted()));
|
||||
}
|
||||
|
||||
private boolean matchesSnapshotFilters(Snapshot snapshot, List<Map.Entry<String, List<String>>> filters) {
|
||||
@@ -10679,6 +10807,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// it and then together exceed it, and a rejected request writes nothing.
|
||||
withTagWriteLocks(region, resourceIds, () -> {
|
||||
for (String resourceId : resourceIds) {
|
||||
// Checked again under the lock DeleteSnapshot deletes under, so a snapshot deleted
|
||||
// since the check above is not left with tags and no resource.
|
||||
if (resourceId.startsWith("snap-")) {
|
||||
requireFound(snapshots.get(key(region, resourceId)).isPresent(),
|
||||
"InvalidSnapshot.NotFound", "The snapshot '" + resourceId + "' does not exist.");
|
||||
}
|
||||
requireWithinTagLimit(region, resourceId, tagList);
|
||||
}
|
||||
if (dryRun) {
|
||||
|
||||
Reference in new issue
Block a user