mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): report actual deleteOnTermination for instance network interfaces (#7940)
DescribeInstances reported deleteOnTermination=true for every network interface attachment. It now reports what the interface carries: an interface RunInstances creates defaults to true, a caller-supplied one to false, and an explicit NetworkInterface.1.DeleteOnTermination is honoured. Asking for true on a supplied interface is refused with InvalidParameterCombination before anything launches, as AWS does, and a launch-created interface sent false is kept as a standalone record, so it returns to available when its instance terminates instead of being deleted with it. DescribeInstances and DescribeNetworkInterfaces read the same value, and both build an implicit interface through one helper. Closes #7838
This commit is contained in:
1 parent
65e737ff2c
commit
1fea61f88b
4 files changed
+225
-49
No files matched your search
@@ -823,6 +823,7 @@ public class Ec2QueryHandler {
|
||||
// the instance's primary interface (network_interface { network_interface_id = ... }).
|
||||
String networkInterfaceId = p.getFirst("NetworkInterface.1.NetworkInterfaceId");
|
||||
int networkInterfaceDeviceIndex = parseIntParam(p, "NetworkInterface.1.DeviceIndex", 0);
|
||||
Boolean explicitDeleteOnTermination = parseOptionalBoolean(p.getFirst("NetworkInterface.1.DeleteOnTermination"), "NetworkInterface.1.DeleteOnTermination");
|
||||
String clientToken = p.getFirst("ClientToken");
|
||||
List<String> sgIds = getList(p, "SecurityGroupId");
|
||||
// With a primary interface spec the groups travel on it: Terraform's aws_instance sends that
|
||||
@@ -950,6 +951,12 @@ public class Ec2QueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// AWS only allows DeleteOnTermination=true for a newly created interface.
|
||||
if (Boolean.TRUE.equals(explicitDeleteOnTermination) && networkInterfaceId != null && !networkInterfaceId.isBlank()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"You can specify true for DeleteOnTermination only when creating a new network interface.", 400);
|
||||
}
|
||||
|
||||
Reservation res = service.runInstances(region, imageId, instanceType, minCount, maxCount,
|
||||
keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn,
|
||||
associatePublicIp, networkInterfaceId, networkInterfaceDeviceIndex, null, metadataOptions,
|
||||
@@ -957,6 +964,14 @@ public class Ec2QueryHandler {
|
||||
p.getFirst("Placement.HostId"), p.getFirst("Placement.Tenancy"), blockDeviceMappings,
|
||||
"spot".equals(marketType), privateIpAddress, capacitySpecification);
|
||||
|
||||
// Apply an explicit NetworkInterface.1.DeleteOnTermination to the launched instance.
|
||||
if (explicitDeleteOnTermination != null) {
|
||||
for (Instance inst : res.getInstances()) {
|
||||
service.setNetworkInterfaceDeleteOnTermination(region, inst.getInstanceId(),
|
||||
explicitDeleteOnTermination);
|
||||
}
|
||||
}
|
||||
|
||||
// The request's EbsOptimized wins over a launch template's. Applied after the launch, on this
|
||||
// API path only: CloudFormation, Auto Scaling and Fleet launches still read false. Passing it
|
||||
// into Ec2Service.runInstances would cover all of them.
|
||||
@@ -5649,7 +5664,12 @@ public class Ec2QueryHandler {
|
||||
if (eni.getAttachTime() != null) {
|
||||
xml.elem("attachTime", eni.getAttachTime());
|
||||
}
|
||||
xml.elem("deleteOnTermination", "true")
|
||||
boolean deleteOnTermination = eni.isDeleteOnTermination();
|
||||
NetworkInterface standalone = service.getNetworkInterface(inst.getRegion(), eni.getNetworkInterfaceId());
|
||||
if (standalone != null && standalone.getAttachment() != null) {
|
||||
deleteOnTermination = standalone.getAttachment().isDeleteOnTermination();
|
||||
}
|
||||
xml.elem("deleteOnTermination", String.valueOf(deleteOnTermination))
|
||||
.end("attachment")
|
||||
.start("privateIpAddressesSet")
|
||||
.start("item")
|
||||
|
||||
@@ -4272,6 +4272,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (inst.getLaunchTime() != null) {
|
||||
eni.setAttachTime(ISO_FMT.format(inst.getLaunchTime()));
|
||||
}
|
||||
// Caller-supplied ENI defaults to false; an implicit interface defaults to true.
|
||||
eni.setDeleteOnTermination(suppliedEni == null);
|
||||
inst.getNetworkInterfaces().add(eni);
|
||||
if (suppliedEni != null) {
|
||||
// The standalone record stays authoritative rather than being folded into the
|
||||
@@ -5110,6 +5112,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/** The standalone record for an interface id, or null when it is implicit/launch-created. */
|
||||
public NetworkInterface getNetworkInterface(String region, String networkInterfaceId) {
|
||||
if (networkInterfaceId == null || networkInterfaceId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return networkInterfaces.get(key(region, networkInterfaceId)).orElse(null);
|
||||
}
|
||||
|
||||
/** The instance a standalone interface is attached to, or null. */
|
||||
private String instanceAttachedTo(String region, String networkInterfaceId) {
|
||||
return networkInterfaceId == null ? null : networkInterfaces.get(key(region, networkInterfaceId))
|
||||
@@ -15848,56 +15858,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
continue;
|
||||
}
|
||||
foundIds.add(eni.getNetworkInterfaceId());
|
||||
NetworkInterface ni = new NetworkInterface();
|
||||
ni.setNetworkInterfaceId(eni.getNetworkInterfaceId());
|
||||
ni.setSubnetId(eni.getSubnetId());
|
||||
ni.setVpcId(eni.getVpcId());
|
||||
ni.setDescription(eni.getDescription());
|
||||
ni.setOwnerId(eni.getOwnerId());
|
||||
ni.setStatus(eni.getStatus());
|
||||
ni.setMacAddress(eni.getMacAddress());
|
||||
ni.setPrivateIpAddress(eni.getPrivateIpAddress());
|
||||
ni.setPrivateDnsName(eni.getPrivateDnsName());
|
||||
ni.setSourceDestCheck(eni.isSourceDestCheck());
|
||||
ni.setGroups(new ArrayList<>(eni.getGroups()));
|
||||
// Phase 3: availability zone, tags, interface type
|
||||
if (inst.getPlacement() != null) {
|
||||
ni.setAvailabilityZone(inst.getPlacement().getAvailabilityZone());
|
||||
}
|
||||
// A network interface's tags are its OWN, never the instance's. AWS tags
|
||||
// exactly the resource types a RunInstances TagSpecification names, so an
|
||||
// interface created for an instance whose specification said
|
||||
// ResourceType=instance carries no tags until something tags the eni- id
|
||||
// itself - and DescribeTags never listed these copied tags either, so
|
||||
// DescribeNetworkInterfaces disagreed with DescribeTags about the same
|
||||
// resource. Read the interface's own entry in the tag store instead:
|
||||
// CreateTags on the eni- id writes it, and so does a RunInstances
|
||||
// TagSpecification with ResourceType=network-interface.
|
||||
Optional<List<Tag>> tagList = tagsOf(safeAccount, region, eni.getNetworkInterfaceId());
|
||||
ni.getTagSet().addAll(tagList.orElse(List.of()));
|
||||
|
||||
NetworkInterfaceAttachment att = new NetworkInterfaceAttachment();
|
||||
att.setAttachmentId(eni.getAttachmentId());
|
||||
att.setDeviceIndex(eni.getDeviceIndex());
|
||||
att.setStatus("attached");
|
||||
att.setInstanceId(inst.getInstanceId());
|
||||
att.setInstanceOwnerId(eni.getOwnerId());
|
||||
// Phase 3: attachTime from instance launchTime, deleteOnTermination
|
||||
if (inst.getLaunchTime() != null) {
|
||||
att.setAttachTime(ISO_FMT.format(inst.getLaunchTime()));
|
||||
}
|
||||
att.setDeleteOnTermination(true);
|
||||
ni.setAttachment(att);
|
||||
|
||||
// Phase 3: privateIpAddressesSet - primary IP
|
||||
NetworkInterfacePrivateIpAddress primaryIp = new NetworkInterfacePrivateIpAddress();
|
||||
primaryIp.setPrivateIpAddress(eni.getPrivateIpAddress());
|
||||
primaryIp.setPrivateDnsName(eni.getPrivateDnsName());
|
||||
primaryIp.setPrimary(true);
|
||||
NetworkInterface ni = synthesizeImplicitNetworkInterface(inst, eni, region, safeAccount);
|
||||
// Look up EIP association for this instance
|
||||
addressForInstance(inst.getInstanceId()).ifPresent(addr ->
|
||||
primaryIp.setAssociation(networkInterfaceAssociation(addr, eni.getOwnerId())));
|
||||
ni.getPrivateIpAddresses().add(primaryIp);
|
||||
ni.getPrivateIpAddresses().getFirst().setAssociation(
|
||||
networkInterfaceAssociation(addr, eni.getOwnerId())));
|
||||
|
||||
// Phase 4: apply filters
|
||||
if (!matchesFilters(ni, filters, region)) {
|
||||
@@ -16137,6 +16102,85 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/** Builds the standalone record describeNetworkInterfaces would otherwise synthesize for an
|
||||
* implicit (launch-created) interface, so a kept (DeleteOnTermination=false) interface is
|
||||
* reported identically whether or not it has been promoted to the standalone store. */
|
||||
private NetworkInterface synthesizeImplicitNetworkInterface(Instance inst, InstanceNetworkInterface eni,
|
||||
String region, String accountId) {
|
||||
NetworkInterface ni = new NetworkInterface();
|
||||
ni.setNetworkInterfaceId(eni.getNetworkInterfaceId());
|
||||
ni.setSubnetId(eni.getSubnetId());
|
||||
ni.setVpcId(eni.getVpcId());
|
||||
ni.setDescription(eni.getDescription());
|
||||
ni.setOwnerId(eni.getOwnerId());
|
||||
ni.setStatus(eni.getStatus());
|
||||
ni.setMacAddress(eni.getMacAddress());
|
||||
ni.setPrivateIpAddress(eni.getPrivateIpAddress());
|
||||
ni.setPrivateDnsName(eni.getPrivateDnsName());
|
||||
ni.setSourceDestCheck(eni.isSourceDestCheck());
|
||||
ni.setGroups(new ArrayList<>(eni.getGroups()));
|
||||
if (inst.getPlacement() != null) {
|
||||
ni.setAvailabilityZone(inst.getPlacement().getAvailabilityZone());
|
||||
}
|
||||
// A network interface's tags are its OWN, never the instance's. AWS tags
|
||||
// exactly the resource types a RunInstances TagSpecification names, so an
|
||||
// interface created for an instance whose specification said
|
||||
// ResourceType=instance carries no tags until something tags the eni- id
|
||||
// itself - and DescribeTags never listed these copied tags either, so
|
||||
// DescribeNetworkInterfaces disagreed with DescribeTags about the same
|
||||
// resource. Read the interface's own entry in the tag store instead:
|
||||
// CreateTags on the eni- id writes it, and so does a RunInstances
|
||||
// TagSpecification with ResourceType=network-interface.
|
||||
Optional<List<Tag>> tagList = tagsOf(accountId, region, eni.getNetworkInterfaceId());
|
||||
ni.getTagSet().addAll(tagList.orElse(List.of()));
|
||||
NetworkInterfaceAttachment att = new NetworkInterfaceAttachment();
|
||||
att.setAttachmentId(eni.getAttachmentId());
|
||||
att.setDeviceIndex(eni.getDeviceIndex());
|
||||
att.setStatus("attached");
|
||||
att.setInstanceId(inst.getInstanceId());
|
||||
att.setInstanceOwnerId(eni.getOwnerId());
|
||||
if (inst.getLaunchTime() != null) {
|
||||
att.setAttachTime(ISO_FMT.format(inst.getLaunchTime()));
|
||||
}
|
||||
att.setDeleteOnTermination(eni.isDeleteOnTermination());
|
||||
ni.setAttachment(att);
|
||||
NetworkInterfacePrivateIpAddress primaryIp = new NetworkInterfacePrivateIpAddress();
|
||||
primaryIp.setPrivateIpAddress(eni.getPrivateIpAddress());
|
||||
primaryIp.setPrivateDnsName(eni.getPrivateDnsName());
|
||||
primaryIp.setPrimary(true);
|
||||
ni.getPrivateIpAddresses().add(primaryIp);
|
||||
return ni;
|
||||
}
|
||||
|
||||
/** Sets DeleteOnTermination on the launched instance's network interfaces for an explicit request param. */
|
||||
public void setNetworkInterfaceDeleteOnTermination(String region, String instanceId, boolean value) {
|
||||
Instance inst = instances.get(key(region, instanceId)).orElse(null);
|
||||
if (inst == null) return;
|
||||
for (InstanceNetworkInterface eni : inst.getNetworkInterfaces()) {
|
||||
NetworkInterface ni = networkInterfaces.get(key(region, eni.getNetworkInterfaceId())).orElse(null);
|
||||
if (ni == null) {
|
||||
// Implicit (auto-created) interface: honor the flag. When false, create a standalone
|
||||
// record so the interface is returned to "available" on termination instead of deleted.
|
||||
eni.setDeleteOnTermination(value);
|
||||
if (!value) {
|
||||
NetworkInterface standalone = synthesizeImplicitNetworkInterface(inst, eni, region, callerAccountId());
|
||||
NetworkInterfaceAttachment att = standalone.getAttachment();
|
||||
att.setDeleteOnTermination(false);
|
||||
standalone.setStatus("in-use");
|
||||
networkInterfaces.put(key(region, eni.getNetworkInterfaceId()), standalone);
|
||||
}
|
||||
} else {
|
||||
// Supplied (caller-created) interface: honor the flag on both copies.
|
||||
eni.setDeleteOnTermination(value);
|
||||
if (ni.getAttachment() != null) {
|
||||
ni.getAttachment().setDeleteOnTermination(value);
|
||||
networkInterfaces.put(key(region, ni.getNetworkInterfaceId()), ni);
|
||||
}
|
||||
}
|
||||
}
|
||||
instances.put(key(region, instanceId), inst);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a standalone ENI and gives the lease its primary address took back to the VPC
|
||||
* network. A requested primary took no lease, so any it carries is a failed launch's, given
|
||||
|
||||
@@ -24,6 +24,7 @@ public class InstanceNetworkInterface {
|
||||
private String attachmentId;
|
||||
private int deviceIndex;
|
||||
private String attachTime;
|
||||
private boolean deleteOnTermination = true;
|
||||
|
||||
public InstanceNetworkInterface() {}
|
||||
|
||||
@@ -68,4 +69,7 @@ public class InstanceNetworkInterface {
|
||||
|
||||
public String getAttachTime() { return attachTime; }
|
||||
public void setAttachTime(String attachTime) { this.attachTime = attachTime; }
|
||||
|
||||
public boolean isDeleteOnTermination() { return deleteOnTermination; }
|
||||
public void setDeleteOnTermination(boolean deleteOnTermination) { this.deleteOnTermination = deleteOnTermination; }
|
||||
}
|
||||
@@ -586,6 +586,114 @@ class Ec2NetworkInterfaceIntegrationTest {
|
||||
Assertions.assertTrue(replacement.startsWith("i-"));
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteOnTermination=true on a supplied interface is refused: AWS allows true only when
|
||||
* RunInstances is creating the network interface itself, so a request naming an existing
|
||||
* interface with it fails before anything launches, and the interface stays unattached.
|
||||
*/
|
||||
@Test
|
||||
@Order(29)
|
||||
void aSuppliedInterfaceSentTrueIsRefused() {
|
||||
String eni = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("SecurityGroupId.1", securityGroupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("NetworkInterface.1.NetworkInterfaceId", eni)
|
||||
.formParam("NetworkInterface.1.DeviceIndex", "0")
|
||||
.formParam("NetworkInterface.1.DeleteOnTermination", "true")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
|
||||
// The refused launch left nothing behind: the interface has no attachment and would be
|
||||
// accepted by a launch without the flag.
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eni)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status",
|
||||
equalTo("available"))
|
||||
.body(not(containsString("<attachment")));
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface RunInstances creates itself honors an explicit DeleteOnTermination=false: the
|
||||
* instance's interface is then kept past termination, so it comes back to "available" for
|
||||
* reuse instead of being destroyed with the instance, which is what DeleteOnTermination
|
||||
* means on AWS.
|
||||
*/
|
||||
@Test
|
||||
@Order(30)
|
||||
void aLaunchCreatedInterfaceSentFalseSurvivesTermination() {
|
||||
String instanceId = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("NetworkInterface.1.DeleteOnTermination", "false")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
String eni = given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("DescribeInstancesResponse.reservationSet.item.instancesSet.item"
|
||||
+ ".networkInterfaceSet.item.networkInterfaceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "TerminateInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// The interface the launch created outlives its instance and is back to "available",
|
||||
// rather than gone with it.
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eni)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status",
|
||||
equalTo("available"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void anExplicitAddressAnotherInterfaceHoldsIsRefused() {
|
||||
|
||||
Reference in new issue
Block a user