mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): assign a public IP only when the subnet opts in via MapPublicIpOnLaunch (#2023)
* fix(ec2): assign a public IP only when the subnet opts in via MapPublicIpOnLaunch (#1984) Every instance was given PublicIpAddress 127.0.0.1 unconditionally in Ec2ContainerManager, so a private-subnet instance wrongly reported an internet-facing address (breaks the ssh-reachability / public-vs-private distinction). runInstances now flags the instance (associatePublicIp) from its subnet's MapPublicIpOnLaunch, and the container manager exposes a public address only when that flag is set. Private-subnet instances get no PublicIpAddress — DescribeInstances omits the element (XmlBuilder skips nulls), matching real EC2. The value for public instances stays the host-reachable 127.0.0.1 (unchanged). Unit test covers the public-vs-private flag. Supports the aws-bench project: un-excludes the ssh-reachability scenario. * fix(ec2): honor the launch-time AssociatePublicIpAddress override * fix(ec2): honor the top-level AssociatePublicIpAddress and an explicit-false launch configuration Two gaps left by the previous commit, both flagged in review. RunInstances only read the override off the primary network-interface spec (NetworkInterface.1.AssociatePublicIpAddress), so the legacy top-level AssociatePublicIpAddress parameter — what the CLI and older SDKs send — was silently dropped and the subnet default decided instead. It is now read as a fallback; AWS rejects both shapes at once, so the interface spec wins when present. The launch-configuration path was worse than before. LaunchConfiguration stored a primitive boolean, so an absent flag and an explicit false were indistinguishable, and the reconciler could only forward an explicit true. An LC that set AssociatePublicIpAddress=false in a MapPublicIpOnLaunch subnet still got a public address. The field is now a nullable Boolean the whole way through (service, query handler, CFN provisioner), so false overrides the subnet and only null falls back to it. DescribeLaunchConfigurations omits the element when it was never set, as AWS does. Tests: a handler-level test pins all four parse cases (top-level, interface spec, both present, absent), and the reconciler test pins true/false/null reaching runInstances unchanged.
This commit is contained in:
1 parent
188588b4eb
commit
2ab795c6dc
12 files changed
+404
-23
No files matched your search
+7
-3
@@ -102,7 +102,7 @@ public class AutoScalingQueryHandler {
|
||||
memberList(p, "SecurityGroups"),
|
||||
p.getFirst("UserData"),
|
||||
p.getFirst("IamInstanceProfile"),
|
||||
"true".equalsIgnoreCase(p.getFirst("AssociatePublicIpAddress")));
|
||||
nullableBoolParam(p, "AssociatePublicIpAddress"));
|
||||
String xml = new XmlBuilder()
|
||||
.start("CreateLaunchConfigurationResponse", NS)
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
@@ -122,8 +122,12 @@ public class AutoScalingQueryHandler {
|
||||
xml.start("member")
|
||||
.elem("LaunchConfigurationName", lc.getLaunchConfigurationName())
|
||||
.elem("LaunchConfigurationARN", lc.getLaunchConfigurationArn())
|
||||
.elem("CreatedTime", ISO_FMT.format(lc.getCreatedTime()))
|
||||
.elem("AssociatePublicIpAddress", String.valueOf(lc.isAssociatePublicIpAddress()));
|
||||
.elem("CreatedTime", ISO_FMT.format(lc.getCreatedTime()));
|
||||
// AWS omits the flag entirely when the LC never set it, which is
|
||||
// what tells a caller the subnet default still applies.
|
||||
if (lc.getAssociatePublicIpAddress() != null) {
|
||||
xml.elem("AssociatePublicIpAddress", String.valueOf(lc.getAssociatePublicIpAddress()));
|
||||
}
|
||||
if (lc.getImageId() != null) { xml.elem("ImageId", lc.getImageId()); }
|
||||
if (lc.getInstanceType() != null) { xml.elem("InstanceType", lc.getInstanceType()); }
|
||||
if (lc.getKeyName() != null) { xml.elem("KeyName", lc.getKeyName()); }
|
||||
|
||||
+13
-5
@@ -296,7 +296,8 @@ public class AutoScalingReconciler {
|
||||
null,
|
||||
propagatedInstanceTags(asg, launchSource),
|
||||
launchSource.userData(),
|
||||
launchSource.iamInstanceProfile());
|
||||
launchSource.iamInstanceProfile(),
|
||||
launchSource.associatePublicIpAddress());
|
||||
|
||||
for (Instance ec2Inst : reservation.getInstances()) {
|
||||
AsgInstance asgInst = new AsgInstance();
|
||||
@@ -413,7 +414,11 @@ public class AutoScalingReconciler {
|
||||
lc.getIamInstanceProfile(),
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
null,
|
||||
// Forwarded as-is: an explicit false has to beat a public
|
||||
// subnet's MapPublicIpOnLaunch, and only null means
|
||||
// "fall back to the subnet default".
|
||||
lc.getAssociatePublicIpAddress());
|
||||
}
|
||||
|
||||
LaunchTemplate launchTemplate = resolveLaunchTemplate(asg);
|
||||
@@ -438,7 +443,8 @@ public class AutoScalingReconciler {
|
||||
version.getIamInstanceProfileArn(),
|
||||
asg.getLaunchTemplateId(),
|
||||
asg.getLaunchTemplateName(),
|
||||
resolvedVersion);
|
||||
resolvedVersion,
|
||||
null);
|
||||
}
|
||||
|
||||
MixedInstancesPolicy.LaunchTemplateSpecification specification =
|
||||
@@ -469,7 +475,8 @@ public class AutoScalingReconciler {
|
||||
? mixedLaunchTemplate.getLaunchTemplateId()
|
||||
: specification.getLaunchTemplateId(),
|
||||
specification.getLaunchTemplateName(),
|
||||
resolvedVersion);
|
||||
resolvedVersion,
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,7 +565,8 @@ public class AutoScalingReconciler {
|
||||
String iamInstanceProfile,
|
||||
String launchTemplateId,
|
||||
String launchTemplateName,
|
||||
String launchTemplateVersion) {}
|
||||
String launchTemplateVersion,
|
||||
Boolean associatePublicIpAddress) {}
|
||||
|
||||
// Override for describeAutoScalingGroups with null region (all regions)
|
||||
// The service only filters by region when non-null; null means all.
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ public class AutoScalingService {
|
||||
String imageId, String instanceType, String keyName,
|
||||
List<String> securityGroups, String userData,
|
||||
String iamInstanceProfile,
|
||||
boolean associatePublicIpAddress) {
|
||||
Boolean associatePublicIpAddress) {
|
||||
String key = lcKey(region, name);
|
||||
if (launchConfigs.containsKey(key)) {
|
||||
throw new AwsException("AlreadyExists",
|
||||
|
||||
+5
-3
@@ -19,7 +19,9 @@ public class LaunchConfiguration {
|
||||
private List<String> securityGroups = new ArrayList<>();
|
||||
private String userData;
|
||||
private String iamInstanceProfile;
|
||||
private boolean associatePublicIpAddress;
|
||||
// Nullable on purpose: AWS treats an absent flag as "fall back to the
|
||||
// subnet's MapPublicIpOnLaunch" and an explicit false as an override.
|
||||
private Boolean associatePublicIpAddress;
|
||||
private Instant createdTime;
|
||||
private String region;
|
||||
|
||||
@@ -49,8 +51,8 @@ public class LaunchConfiguration {
|
||||
public String getIamInstanceProfile() { return iamInstanceProfile; }
|
||||
public void setIamInstanceProfile(String v) { this.iamInstanceProfile = v; }
|
||||
|
||||
public boolean isAssociatePublicIpAddress() { return associatePublicIpAddress; }
|
||||
public void setAssociatePublicIpAddress(boolean v) { this.associatePublicIpAddress = v; }
|
||||
public Boolean getAssociatePublicIpAddress() { return associatePublicIpAddress; }
|
||||
public void setAssociatePublicIpAddress(Boolean v) { this.associatePublicIpAddress = v; }
|
||||
|
||||
public Instant getCreatedTime() { return createdTime; }
|
||||
public void setCreatedTime(Instant v) { this.createdTime = v; }
|
||||
|
||||
+18
-2
@@ -1051,7 +1051,11 @@ public class CloudFormationResourceProvisioner {
|
||||
resolveStringList(props, "SecurityGroups", engine),
|
||||
resolveOptional(props, "UserData", engine),
|
||||
resolveOptional(props, "IamInstanceProfile", engine),
|
||||
Boolean.parseBoolean(associatePublicIp));
|
||||
// Absent in the template means the subnet default applies, so
|
||||
// it stays null rather than collapsing to false.
|
||||
associatePublicIp == null || associatePublicIp.isBlank()
|
||||
? null
|
||||
: Boolean.parseBoolean(associatePublicIp));
|
||||
// Ref returns the launch configuration name.
|
||||
r.setPhysicalId(name);
|
||||
r.getAttributes().put("Arn", lc.getLaunchConfigurationArn());
|
||||
@@ -1192,8 +1196,20 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
// The launch-time public-IP override rides on the primary network
|
||||
// interface spec; absent means the subnet's MapPublicIpOnLaunch default.
|
||||
Boolean associatePublicIp = null;
|
||||
var networkInterfaces = props.path("NetworkInterfaces");
|
||||
if (networkInterfaces.isArray() && !networkInterfaces.isEmpty()) {
|
||||
String assocRaw = engine.resolve(networkInterfaces.get(0).path("AssociatePublicIpAddress"));
|
||||
if (assocRaw != null && !assocRaw.isBlank()) {
|
||||
associatePublicIp = Boolean.parseBoolean(assocRaw);
|
||||
}
|
||||
}
|
||||
|
||||
var reservation = ec2Service.runInstances(region, imageId, instanceType, 1, 1, keyName,
|
||||
securityGroupIds, subnetId, null, tags, userData, iamInstanceProfile);
|
||||
securityGroupIds, subnetId, null, tags, userData, iamInstanceProfile,
|
||||
associatePublicIp);
|
||||
var instance = reservation.getInstances().get(0);
|
||||
r.setPhysicalId(instance.getInstanceId());
|
||||
r.getAttributes().put("InstanceId", instance.getInstanceId());
|
||||
|
||||
@@ -204,9 +204,14 @@ public class Ec2ContainerManager {
|
||||
|
||||
configureLinkLocalMetadataEndpoint(containerId, instanceId, flociHost, imdsPort);
|
||||
|
||||
// Set public-facing addresses
|
||||
instance.setPublicIpAddress("127.0.0.1");
|
||||
instance.setPublicDnsName("localhost");
|
||||
// Set public-facing addresses only for instances whose subnet
|
||||
// opts in via MapPublicIpOnLaunch (#1984). Private-subnet
|
||||
// instances have no public IP/DNS, matching real EC2. The value
|
||||
// stays the host-reachable 127.0.0.1/localhost for public ones.
|
||||
if (instance.isAssociatePublicIp()) {
|
||||
instance.setPublicIpAddress("127.0.0.1");
|
||||
instance.setPublicDnsName("localhost");
|
||||
}
|
||||
|
||||
instance.setState(InstanceState.running());
|
||||
LOG.infov("EC2 instance {0} running in container {1} (SSH host port {2})",
|
||||
|
||||
@@ -425,6 +425,19 @@ public class Ec2QueryHandler {
|
||||
int maxCount = Integer.parseInt(p.getOrDefault("MaxCount", List.of("1")).get(0));
|
||||
String keyName = p.getFirst("KeyName");
|
||||
String subnetId = p.getFirst("SubnetId");
|
||||
// The launch-time public-IP override arrives either on the primary
|
||||
// network interface spec (the shape Terraform's
|
||||
// associate_public_ip_address sends) or as the legacy top-level
|
||||
// parameter. AWS rejects both at once, so the interface spec wins when
|
||||
// present. Absent means "use the subnet's MapPublicIpOnLaunch default".
|
||||
String assocParam = p.getFirst("NetworkInterface.1.AssociatePublicIpAddress");
|
||||
if (assocParam == null) {
|
||||
assocParam = p.getFirst("AssociatePublicIpAddress");
|
||||
}
|
||||
Boolean associatePublicIp = assocParam == null ? null : Boolean.parseBoolean(assocParam);
|
||||
if (subnetId == null) {
|
||||
subnetId = p.getFirst("NetworkInterface.1.SubnetId");
|
||||
}
|
||||
String clientToken = p.getFirst("ClientToken");
|
||||
List<String> sgIds = getList(p, "SecurityGroupId");
|
||||
|
||||
@@ -471,7 +484,8 @@ public class Ec2QueryHandler {
|
||||
}
|
||||
|
||||
Reservation res = service.runInstances(region, imageId, instanceType, minCount, maxCount,
|
||||
keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn);
|
||||
keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn,
|
||||
associatePublicIp);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("RunInstancesResponse", AwsNamespaces.EC2)
|
||||
|
||||
@@ -819,6 +819,17 @@ public class Ec2Service implements ContainerTeardown {
|
||||
List<String> securityGroupIds, String subnetId,
|
||||
String clientToken, List<Tag> instanceTags,
|
||||
String userData, String iamInstanceProfileArn) {
|
||||
return runInstances(region, imageId, instanceType, minCount, maxCount, keyName,
|
||||
securityGroupIds, subnetId, clientToken, instanceTags, userData,
|
||||
iamInstanceProfileArn, null);
|
||||
}
|
||||
|
||||
public Reservation runInstances(String region, String imageId, String instanceType,
|
||||
int minCount, int maxCount, String keyName,
|
||||
List<String> securityGroupIds, String subnetId,
|
||||
String clientToken, List<Tag> instanceTags,
|
||||
String userData, String iamInstanceProfileArn,
|
||||
Boolean associatePublicIp) {
|
||||
if (imageId == null || imageId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter ImageId", 400);
|
||||
}
|
||||
@@ -876,6 +887,12 @@ public class Ec2Service implements ContainerTeardown {
|
||||
inst.setPlacement(new Placement(az));
|
||||
inst.setSubnetId(finalSubnetId);
|
||||
inst.setVpcId(vpcId);
|
||||
// AWS precedence (#1984): the launch-time AssociatePublicIpAddress
|
||||
// override wins in both directions; the subnet's MapPublicIpOnLaunch
|
||||
// attribute is only the default when the launch does not specify it.
|
||||
inst.setAssociatePublicIp(associatePublicIp != null
|
||||
? associatePublicIp
|
||||
: subnet != null && subnet.isMapPublicIpOnLaunch());
|
||||
inst.setPrivateIpAddress(privateIp);
|
||||
inst.setPrivateDnsName("ip-" + privateIp.replace('.', '-') + ".ec2.internal");
|
||||
inst.setKeyName(keyName);
|
||||
|
||||
@@ -25,6 +25,9 @@ public class Instance {
|
||||
private String publicIpAddress;
|
||||
private String privateDnsName;
|
||||
private String publicDnsName;
|
||||
// Whether this instance should be assigned a public IP — true when its subnet
|
||||
// has MapPublicIpOnLaunch (#1984). Instances in private subnets get none.
|
||||
private boolean associatePublicIp = false;
|
||||
private String keyName;
|
||||
private List<GroupIdentifier> securityGroups = new ArrayList<>();
|
||||
private List<InstanceNetworkInterface> networkInterfaces = new ArrayList<>();
|
||||
@@ -102,6 +105,8 @@ public class Instance {
|
||||
|
||||
public String getPublicDnsName() { return publicDnsName; }
|
||||
public void setPublicDnsName(String publicDnsName) { this.publicDnsName = publicDnsName; }
|
||||
public boolean isAssociatePublicIp() { return associatePublicIp; }
|
||||
public void setAssociatePublicIp(boolean associatePublicIp) { this.associatePublicIp = associatePublicIp; }
|
||||
|
||||
public String getKeyName() { return keyName; }
|
||||
public void setKeyName(String keyName) { this.keyName = keyName; }
|
||||
|
||||
Reference in new issue
Block a user