mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): Volume throughput, DescribeAddressesAttribute, and IAM instance profile parity (#1103)
* fix(ec2): add Volume throughput, DescribeAddressesAttribute, and IAM instance profile parity Three EC2 read-path gaps broke Terraform/OpenTofu apply cycles: throughput to the Volume model, parsed it in CreateVolume, and emitted it in CreateVolume/DescribeVolumes. gp3 defaults to 125 MiB/s (matching AWS); other volume types leave it null. the VPC module's aws_eip read cycle. Implemented the operation (returns allocationId + publicIp per AddressAttribute) and added it to the EC2 action allowlist. instance was launched with one, and DescribeIamInstanceProfileAssociations returned UnsupportedOperation — causing perpetual Terraform drift then a hard failure. instanceXml now emits <iamInstanceProfile> (arn + id), and DescribeIamInstanceProfileAssociations is implemented with instance-id filter and AssociationId lookup. Profile and association ids are derived deterministically from the instance id so repeated describes are stable. Verified against AWS SDK v2 shapes (Volume.Throughput, AddressAttribute, IamInstanceProfileAssociation). * fix: incorrect exception throw
This commit is contained in:
1 parent
0467eebf69
commit
a13a221a88
5 files changed
+323
-25
No files matched your search
@@ -157,6 +157,8 @@ public class AwsQueryController {
|
||||
"CreateRouteTable", "DescribeRouteTables", "DeleteRouteTable",
|
||||
"AssociateRouteTable", "DisassociateRouteTable", "CreateRoute", "DeleteRoute",
|
||||
"AllocateAddress", "AssociateAddress", "DisassociateAddress", "ReleaseAddress", "DescribeAddresses",
|
||||
"DescribeAddressesAttribute",
|
||||
"DescribeIamInstanceProfileAssociations",
|
||||
"DescribeAvailabilityZones", "DescribeRegions", "DescribeAccountAttributes",
|
||||
"DescribeInstanceTypes",
|
||||
"DescribeNetworkInterfaces"
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.Base64;
|
||||
|
||||
@ApplicationScoped
|
||||
public class Ec2QueryHandler {
|
||||
@@ -38,6 +37,8 @@ public class Ec2QueryHandler {
|
||||
// Instances
|
||||
case "RunInstances" -> handleRunInstances(params, region);
|
||||
case "DescribeInstances" -> handleDescribeInstances(params, region);
|
||||
case "DescribeIamInstanceProfileAssociations" ->
|
||||
handleDescribeIamInstanceProfileAssociations(params, region);
|
||||
case "TerminateInstances" -> handleTerminateInstances(params, region);
|
||||
case "StartInstances" -> handleStartInstances(params, region);
|
||||
case "StopInstances" -> handleStopInstances(params, region);
|
||||
@@ -70,8 +71,10 @@ public class Ec2QueryHandler {
|
||||
case "RevokeSecurityGroupEgress" -> handleRevokeSecurityGroupEgress(params, region);
|
||||
case "DescribeSecurityGroupRules" -> handleDescribeSecurityGroupRules(params, region);
|
||||
case "ModifySecurityGroupRules" -> handleModifySecurityGroupRules(params, region);
|
||||
case "UpdateSecurityGroupRuleDescriptionsIngress" -> handleUpdateSgRuleDescriptionsIngress(params, region);
|
||||
case "UpdateSecurityGroupRuleDescriptionsEgress" -> handleUpdateSgRuleDescriptionsEgress(params, region);
|
||||
case "UpdateSecurityGroupRuleDescriptionsIngress" ->
|
||||
handleUpdateSgRuleDescriptionsIngress(params, region);
|
||||
case "UpdateSecurityGroupRuleDescriptionsEgress" ->
|
||||
handleUpdateSgRuleDescriptionsEgress(params, region);
|
||||
// Key Pairs
|
||||
case "CreateKeyPair" -> handleCreateKeyPair(params, region);
|
||||
case "DescribeKeyPairs" -> handleDescribeKeyPairs(params, region);
|
||||
@@ -103,6 +106,7 @@ public class Ec2QueryHandler {
|
||||
case "DisassociateAddress" -> handleDisassociateAddress(params, region);
|
||||
case "ReleaseAddress" -> handleReleaseAddress(params, region);
|
||||
case "DescribeAddresses" -> handleDescribeAddresses(params, region);
|
||||
case "DescribeAddressesAttribute" -> handleDescribeAddressesAttribute(params, region);
|
||||
// Regions & Account
|
||||
case "DescribeAvailabilityZones" -> handleDescribeAvailabilityZones(params, region);
|
||||
case "DescribeRegions" -> handleDescribeRegions(params, region);
|
||||
@@ -131,13 +135,13 @@ public class Ec2QueryHandler {
|
||||
private Response ec2Error(String code, String message, int status) {
|
||||
String xml = new XmlBuilder()
|
||||
.start("Response")
|
||||
.start("Errors")
|
||||
.start("Error")
|
||||
.elem("Code", code)
|
||||
.elem("Message", message)
|
||||
.end("Error")
|
||||
.end("Errors")
|
||||
.elem("RequestID", UUID.randomUUID().toString())
|
||||
.start("Errors")
|
||||
.start("Error")
|
||||
.elem("Code", code)
|
||||
.elem("Message", message)
|
||||
.end("Error")
|
||||
.end("Errors")
|
||||
.elem("RequestID", UUID.randomUUID().toString())
|
||||
.end("Response")
|
||||
.build();
|
||||
return Response.status(status).entity(xml).type(MediaType.APPLICATION_XML).build();
|
||||
@@ -274,6 +278,71 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDescribeIamInstanceProfileAssociations(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> associationIds = getList(p, "AssociationId");
|
||||
Map<String, List<String>> filters = getFilters(p);
|
||||
List<String> instanceFilter = filters.get("instance-id");
|
||||
|
||||
List<Reservation> reservations = service.describeInstances(region, List.of(), Map.of());
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeIamInstanceProfileAssociationsResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.start("iamInstanceProfileAssociationSet");
|
||||
for (Reservation res : reservations) {
|
||||
for (Instance inst : res.getInstances()) {
|
||||
if (inst.getIamInstanceProfileArn() == null) {
|
||||
continue;
|
||||
}
|
||||
String assocId = iamInstanceProfileAssociationId(inst.getInstanceId());
|
||||
if (instanceFilter != null && !instanceFilter.contains(inst.getInstanceId())) {
|
||||
continue;
|
||||
}
|
||||
if (!associationIds.isEmpty() && !associationIds.contains(assocId)) {
|
||||
continue;
|
||||
}
|
||||
xml.start("item")
|
||||
.elem("associationId", assocId)
|
||||
.elem("instanceId", inst.getInstanceId())
|
||||
.start("iamInstanceProfile")
|
||||
.elem("arn", inst.getIamInstanceProfileArn())
|
||||
.elem("id", iamInstanceProfileId(inst.getInstanceId()))
|
||||
.end("iamInstanceProfile")
|
||||
.elem("state", "associated")
|
||||
.end("item");
|
||||
}
|
||||
}
|
||||
xml.end("iamInstanceProfileAssociationSet")
|
||||
.end("DescribeIamInstanceProfileAssociationsResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic instance-profile id derived from the instance id so repeated describes are stable.
|
||||
*/
|
||||
private static String iamInstanceProfileId(String instanceId) {
|
||||
return "AIPA" + stableSuffix(instanceId, 17).toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic association id derived from the instance id so repeated describes are stable.
|
||||
*/
|
||||
private static String iamInstanceProfileAssociationId(String instanceId) {
|
||||
return "iip-assoc-" + stableSuffix(instanceId, 17);
|
||||
}
|
||||
|
||||
private static String stableSuffix(String seed, int length) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int h = seed.hashCode();
|
||||
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
long v = ((long) h) & 0xFFFFFFFFL;
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(alphabet.charAt((int) (v % alphabet.length())));
|
||||
v = v * 1103515245L + 12345L + i;
|
||||
v &= 0xFFFFFFFFL;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Response handleDescribeInstances(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> ids = getList(p, "InstanceId");
|
||||
Map<String, List<String>> filters = getFilters(p);
|
||||
@@ -973,7 +1042,7 @@ public class Ec2QueryHandler {
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("associationId", assoc.getRouteTableAssociationId())
|
||||
.start("associationState")
|
||||
.elem("state", assoc.getAssociationState())
|
||||
.elem("state", assoc.getAssociationState())
|
||||
.end("associationState")
|
||||
.end("AssociateRouteTableResponse");
|
||||
return xmlResponse(xml.build());
|
||||
@@ -1050,6 +1119,26 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDescribeAddressesAttribute(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> allocationIds = getList(p, "AllocationId");
|
||||
List<Address> addrs = service.describeAddresses(region, allocationIds, Map.of());
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeAddressesAttributeResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.start("addressSet");
|
||||
for (Address addr : addrs) {
|
||||
// AddressAttribute carries allocationId, publicIp and (optionally) ptrRecord.
|
||||
// Floci does not model reverse DNS, so ptrRecord is omitted (null), matching
|
||||
// real EC2 behaviour for EIPs without a configured PTR record.
|
||||
xml.start("item")
|
||||
.elem("allocationId", addr.getAllocationId())
|
||||
.elem("publicIp", addr.getPublicIp())
|
||||
.end("item");
|
||||
}
|
||||
xml.end("addressSet").end("DescribeAddressesAttributeResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
// ─── Region / AZ / Account handlers ──────────────────────────────────────
|
||||
|
||||
private Response handleDescribeAvailabilityZones(MultivaluedMap<String, String> p, String region) {
|
||||
@@ -1335,16 +1424,22 @@ public class Ec2QueryHandler {
|
||||
.end("capacityReservationSpecification");
|
||||
if (inst.getRootVolumeId() != null) {
|
||||
xml.start("blockDeviceMapping")
|
||||
.start("item")
|
||||
.elem("deviceName", inst.getRootDeviceName())
|
||||
.start("ebs")
|
||||
.elem("volumeId", inst.getRootVolumeId())
|
||||
.elem("status", "attached")
|
||||
.elem("deleteOnTermination", "true")
|
||||
.elem("attachTime", inst.getLaunchTime() != null ? ISO_FMT.format(inst.getLaunchTime()) : "")
|
||||
.end("ebs")
|
||||
.end("item")
|
||||
.end("blockDeviceMapping");
|
||||
.start("item")
|
||||
.elem("deviceName", inst.getRootDeviceName())
|
||||
.start("ebs")
|
||||
.elem("volumeId", inst.getRootVolumeId())
|
||||
.elem("status", "attached")
|
||||
.elem("deleteOnTermination", "true")
|
||||
.elem("attachTime", inst.getLaunchTime() != null ? ISO_FMT.format(inst.getLaunchTime()) : "")
|
||||
.end("ebs")
|
||||
.end("item")
|
||||
.end("blockDeviceMapping");
|
||||
}
|
||||
if (inst.getIamInstanceProfileArn() != null) {
|
||||
xml.start("iamInstanceProfile")
|
||||
.elem("arn", inst.getIamInstanceProfileArn())
|
||||
.elem("id", iamInstanceProfileId(inst.getInstanceId()))
|
||||
.end("iamInstanceProfile");
|
||||
}
|
||||
xml.raw(tagSetXml(inst.getTags()));
|
||||
return xml.build();
|
||||
@@ -1505,6 +1600,16 @@ public class Ec2QueryHandler {
|
||||
boolean encrypted = "true".equalsIgnoreCase(encryptedStr);
|
||||
String iopsStr = p.getFirst("Iops");
|
||||
int iops = iopsStr != null ? Integer.parseInt(iopsStr) : 0;
|
||||
String throughputStr = p.getFirst("Throughput");
|
||||
Integer throughput = null;
|
||||
if (throughputStr != null) {
|
||||
try {
|
||||
throughput = Integer.parseInt(throughputStr);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException", "Invalid Throughput value: " + throughputStr, 400);
|
||||
}
|
||||
}
|
||||
|
||||
String snapshotId = p.getFirst("SnapshotId");
|
||||
|
||||
List<Tag> volumeTags = new ArrayList<>();
|
||||
@@ -1522,7 +1627,7 @@ public class Ec2QueryHandler {
|
||||
}
|
||||
|
||||
Volume vol = service.createVolume(region, availabilityZone, volumeType, size,
|
||||
encrypted, iops, snapshotId, volumeTags);
|
||||
encrypted, iops, throughput, snapshotId, volumeTags);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateVolumeResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
@@ -1564,6 +1669,9 @@ public class Ec2QueryHandler {
|
||||
if (vol.getIops() > 0) {
|
||||
xml.elem("iops", String.valueOf(vol.getIops()));
|
||||
}
|
||||
if (vol.getThroughput() != null) {
|
||||
xml.elem("throughput", String.valueOf(vol.getThroughput()));
|
||||
}
|
||||
if (vol.getSnapshotId() != null) {
|
||||
xml.elem("snapshotId", vol.getSnapshotId());
|
||||
}
|
||||
|
||||
@@ -1483,17 +1483,24 @@ public class Ec2Service {
|
||||
// ─── Volumes ───────────────────────────────────────────────────────────────
|
||||
|
||||
public Volume createVolume(String region, String availabilityZone, String volumeType,
|
||||
int size, boolean encrypted, int iops, String snapshotId,
|
||||
List<Tag> volumeTags) {
|
||||
int size, boolean encrypted, int iops, Integer throughput,
|
||||
String snapshotId, List<Tag> volumeTags) {
|
||||
ensureDefaultResources(region);
|
||||
String volumeId = "vol-" + randomHex(17);
|
||||
String effectiveType = volumeType != null ? volumeType : "gp2";
|
||||
Volume vol = new Volume();
|
||||
vol.setVolumeId(volumeId);
|
||||
vol.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a");
|
||||
vol.setVolumeType(volumeType != null ? volumeType : "gp2");
|
||||
vol.setVolumeType(effectiveType);
|
||||
vol.setSize(size > 0 ? size : 8);
|
||||
vol.setEncrypted(encrypted);
|
||||
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)) {
|
||||
vol.setThroughput(throughput != null && throughput > 0 ? throughput : 125);
|
||||
} else {
|
||||
vol.setThroughput(throughput);
|
||||
}
|
||||
vol.setSnapshotId(snapshotId);
|
||||
vol.setCreateTime(Instant.now());
|
||||
vol.setState("available");
|
||||
|
||||
@@ -18,6 +18,7 @@ public class Volume {
|
||||
private String availabilityZone;
|
||||
private boolean encrypted;
|
||||
private int iops;
|
||||
private Integer throughput; // MiB/s; only meaningful for gp3 (null otherwise)
|
||||
private String snapshotId;
|
||||
private Instant createTime;
|
||||
private String region;
|
||||
@@ -47,6 +48,9 @@ public class Volume {
|
||||
public int getIops() { return iops; }
|
||||
public void setIops(int iops) { this.iops = iops; }
|
||||
|
||||
public Integer getThroughput() { return throughput; }
|
||||
public void setThroughput(Integer throughput) { this.throughput = throughput; }
|
||||
|
||||
public String getSnapshotId() { return snapshotId; }
|
||||
public void setSnapshotId(String snapshotId) { this.snapshotId = snapshotId; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user