fix(autoscaling): reject launch templates without image ids (#1451)

This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-06-21 09:40:33 -05:00
1 parent 3f314dcb4e
commit e8b54e45e9
10 files changed
+562 -16

No files matched your search

@@ -95,6 +95,7 @@ public class AutoScalingQueryHandler {
private Response handleCreateLaunchConfiguration(MultivaluedMap<String, String> p, String region) {
service.createLaunchConfiguration(region,
p.getFirst("LaunchConfigurationName"),
p.getFirst("InstanceId"),
p.getFirst("ImageId"),
p.getFirst("InstanceType"),
p.getFirst("KeyName"),
@@ -121,10 +122,10 @@ public class AutoScalingQueryHandler {
xml.start("member")
.elem("LaunchConfigurationName", lc.getLaunchConfigurationName())
.elem("LaunchConfigurationARN", lc.getLaunchConfigurationArn())
.elem("ImageId", lc.getImageId() != null ? lc.getImageId() : "")
.elem("InstanceType", lc.getInstanceType() != null ? lc.getInstanceType() : "t3.micro")
.elem("CreatedTime", ISO_FMT.format(lc.getCreatedTime()))
.elem("AssociatePublicIpAddress", String.valueOf(lc.isAssociatePublicIpAddress()));
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()); }
if (lc.getUserData() != null) { xml.elem("UserData", lc.getUserData()); }
if (lc.getIamInstanceProfile() != null) { xml.elem("IamInstanceProfile", lc.getIamInstanceProfile()); }
@@ -8,6 +8,8 @@ import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.autoscaling.model.*;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -20,6 +22,14 @@ import java.util.stream.Collectors;
@ApplicationScoped
public class AutoScalingService {
static final String MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE =
"You must use a valid fully-formed launch template. The request must contain the parameter ImageId";
static final String INVALID_LAUNCH_TEMPLATE_MESSAGE =
"The specified launch template does not exist.";
static final String INVALID_LAUNCH_CONFIGURATION_PARAMETERS_MESSAGE =
"Valid requests must contain either the InstanceID parameter "
+ "or both the ImageId and InstanceType parameters.";
@Inject
RegionResolver regionResolver;
@@ -58,8 +68,8 @@ public class AutoScalingService {
// ── Launch Configurations ──────────────────────────────────────────────────
public LaunchConfiguration createLaunchConfiguration(String region, String name, String imageId,
String instanceType, String keyName,
public LaunchConfiguration createLaunchConfiguration(String region, String name, String instanceId,
String imageId, String instanceType, String keyName,
List<String> securityGroups, String userData,
String iamInstanceProfile,
boolean associatePublicIpAddress) {
@@ -68,13 +78,47 @@ public class AutoScalingService {
throw new AwsException("AlreadyExists",
"Launch configuration '" + name + "' already exists.", 400);
}
if (isBlank(instanceId) && (isBlank(imageId) || isBlank(instanceType))) {
throw new AwsException("ValidationError", INVALID_LAUNCH_CONFIGURATION_PARAMETERS_MESSAGE, 400);
}
if (notBlank(instanceId) && ec2Service != null) {
List<Instance> sourceInstances = ec2Service.describeInstances(region, List.of(instanceId), Map.of())
.stream()
.flatMap(reservation -> reservation.getInstances().stream())
.collect(Collectors.toList());
if (!sourceInstances.isEmpty()) {
Instance source = sourceInstances.getFirst();
if (isBlank(imageId)) {
imageId = source.getImageId();
}
if (isBlank(instanceType)) {
instanceType = source.getInstanceType();
}
if (isBlank(keyName)) {
keyName = source.getKeyName();
}
if ((securityGroups == null || securityGroups.isEmpty())
&& source.getSecurityGroups() != null) {
securityGroups = source.getSecurityGroups().stream()
.map(group -> group.getGroupId() != null ? group.getGroupId() : group.getGroupName())
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
if (isBlank(userData)) {
userData = source.getUserData();
}
if (isBlank(iamInstanceProfile)) {
iamInstanceProfile = source.getIamInstanceProfileArn();
}
}
}
LaunchConfiguration lc = new LaunchConfiguration();
lc.setLaunchConfigurationName(name);
lc.setLaunchConfigurationArn(
AwsArnUtils.Arn.of("autoscaling", region, regionResolver.getAccountId(),
"launchConfiguration:" + name).toString());
lc.setImageId(imageId);
lc.setInstanceType(instanceType != null ? instanceType : "t3.micro");
lc.setInstanceType(instanceType);
lc.setKeyName(keyName);
lc.setSecurityGroups(securityGroups != null ? new ArrayList<>(securityGroups) : new ArrayList<>());
lc.setUserData(userData);
@@ -131,6 +175,8 @@ public class AutoScalingService {
"Valid requests must contain either LaunchTemplate, LaunchConfigurationName, "
+ "InstanceId or MixedInstancesPolicy parameter.", 400);
}
validateEffectiveLaunchImage(region, launchConfigName, launchTemplateId, launchTemplateName,
launchTemplateVersion, mixedInstancesPolicy);
AutoScalingGroup asg = new AutoScalingGroup();
asg.setAutoScalingGroupName(name);
@@ -174,6 +220,14 @@ public class AutoScalingService {
List<String> terminationPolicies) {
AutoScalingGroup asg = requireGroup(region, name);
validateLaunchSource(launchConfigName, launchTemplateId, launchTemplateName, mixedInstancesPolicy);
LaunchIdentity effectiveIdentity = effectiveLaunchIdentity(asg, launchConfigName,
launchTemplateId, launchTemplateName, launchTemplateVersion, mixedInstancesPolicy);
validateEffectiveLaunchImage(region,
effectiveIdentity.launchConfigurationName(),
effectiveIdentity.launchTemplateId(),
effectiveIdentity.launchTemplateName(),
effectiveIdentity.launchTemplateVersion(),
effectiveIdentity.mixedInstancesPolicy());
if (launchConfigName != null) {
asg.setLaunchConfigurationName(launchConfigName);
asg.setLaunchTemplateId(null);
@@ -668,6 +722,107 @@ public class AutoScalingService {
return value != null && !value.isBlank();
}
private LaunchIdentity effectiveLaunchIdentity(AutoScalingGroup asg,
String launchConfigName,
String launchTemplateId,
String launchTemplateName,
String launchTemplateVersion,
MixedInstancesPolicy mixedInstancesPolicy) {
LaunchIdentity identity = new LaunchIdentity(
asg.getLaunchConfigurationName(),
asg.getLaunchTemplateId(),
asg.getLaunchTemplateName(),
asg.getLaunchTemplateVersion(),
asg.getMixedInstancesPolicy());
if (launchConfigName != null) {
return new LaunchIdentity(launchConfigName, null, null, null, null);
}
if (launchTemplateId != null || launchTemplateName != null) {
return new LaunchIdentity(null, launchTemplateId, launchTemplateName, launchTemplateVersion, null);
}
if (mixedInstancesPolicy != null) {
return new LaunchIdentity(null, null, null, null, mixedInstancesPolicy);
}
return identity;
}
private void validateEffectiveLaunchImage(String region,
String launchConfigName,
String launchTemplateId,
String launchTemplateName,
String launchTemplateVersion,
MixedInstancesPolicy mixedInstancesPolicy) {
if (launchConfigName != null) {
return;
}
if (launchTemplateId != null || launchTemplateName != null) {
validateLaunchTemplateImage(region, launchTemplateId, launchTemplateName, launchTemplateVersion);
return;
}
MixedInstancesPolicy.LaunchTemplateSpecification specification =
mixedInstancesLaunchTemplateSpecification(mixedInstancesPolicy);
if (specification != null) {
validateLaunchTemplateImage(region,
specification.getLaunchTemplateId(),
specification.getLaunchTemplateName(),
specification.getVersion());
}
}
private void validateLaunchTemplateImage(String region, String launchTemplateId,
String launchTemplateName, String launchTemplateVersion) {
if (ec2Service == null) {
return;
}
List<String> versions = isBlank(launchTemplateVersion) ? List.of() : List.of(launchTemplateVersion);
List<LaunchTemplate> launchTemplateVersions = ec2Service.describeLaunchTemplateVersions(
region,
launchTemplateId,
launchTemplateName,
versions);
if (launchTemplateVersions.isEmpty()) {
throw invalidLaunchTemplate();
}
if (isBlank(launchTemplateVersions.getFirst().getImageId())) {
throw missingLaunchTemplateImageId();
}
}
private static MixedInstancesPolicy.LaunchTemplateSpecification mixedInstancesLaunchTemplateSpecification(
MixedInstancesPolicy policy) {
if (policy == null || policy.getLaunchTemplate() == null) {
return null;
}
MixedInstancesPolicy.LaunchTemplateSpecification specification =
policy.getLaunchTemplate().getLaunchTemplateSpecification();
if (specification == null) {
return null;
}
if (isBlank(specification.getLaunchTemplateId()) && isBlank(specification.getLaunchTemplateName())) {
return null;
}
return specification;
}
private static AwsException missingLaunchTemplateImageId() {
return new AwsException("ValidationError", MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE, 400);
}
private static AwsException invalidLaunchTemplate() {
return new AwsException("ValidationError", INVALID_LAUNCH_TEMPLATE_MESSAGE, 400);
}
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
private record LaunchIdentity(
String launchConfigurationName,
String launchTemplateId,
String launchTemplateName,
String launchTemplateVersion,
MixedInstancesPolicy mixedInstancesPolicy) {}
private static String instanceRefreshKey(String region, String asgName, String refreshId) {
return region + "::" + asgName + "::" + refreshId;
}
@@ -746,6 +746,7 @@ public class CloudFormationResourceProvisioner {
}
String associatePublicIp = resolveOptional(props, "AssociatePublicIpAddress", engine);
var lc = autoScalingService.createLaunchConfiguration(region, name,
resolveOptional(props, "InstanceId", engine),
resolveOptional(props, "ImageId", engine),
resolveOptional(props, "InstanceType", engine),
resolveOptional(props, "KeyName", engine),
@@ -318,6 +318,9 @@ public class Ec2Service {
List<String> securityGroupIds, String subnetId,
String clientToken, List<Tag> instanceTags,
String userData, String iamInstanceProfileArn) {
if (imageId == null || imageId.isBlank()) {
throw new AwsException("MissingParameter", "The request must contain the parameter ImageId", 400);
}
ensureDefaultResources(region);
// Resolve subnet
@@ -363,7 +366,7 @@ public class Ec2Service {
Instance inst = new Instance();
inst.setInstanceId(instanceId);
inst.setImageId(imageId != null ? imageId : "ami-default");
inst.setImageId(imageId);
inst.setState(InstanceState.pending());
inst.setInstanceType(instanceType != null ? instanceType : "t2.micro");
inst.setPlacement(new Placement(az));