mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): fail an EC2 instance whose LaunchTemplate cannot be resolved (#4058)
Ec2InstanceCfnProvisioner swallowed a failed resolveLaunchTemplateData at debug and then launched the instance with default config (t3.micro, no template AMI/KeyName). A LaunchTemplate a stack references but that does not resolve is a real error, as on AWS, so let the AwsException propagate and fail the resource. The unused LOG field is removed with the swallow. The lookup only runs when the resolved LaunchTemplate is an object naming a template: an Fn::If that selects AWS::NoValue resolves to an empty node (CloudFormationTemplateEngine maps AWS::NoValue to ""), which on AWS means the property is absent, so it must launch without a template rather than raise InvalidLaunchTemplateId.NotFound. Refs #3810
This commit is contained in:
1 parent
12cad79616
commit
e1334cb512
2 files changed
+50
-22
No files matched your search
+19
-22
@@ -12,7 +12,6 @@ import io.github.hectorvent.floci.services.ec2.model.Reservation;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -28,8 +27,6 @@ import java.util.Set;
|
||||
@ApplicationScoped
|
||||
public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Ec2InstanceCfnProvisioner.class);
|
||||
|
||||
private static final String INSTANCE = "AWS::EC2::Instance";
|
||||
|
||||
private final Ec2Service ec2Service;
|
||||
@@ -55,25 +52,25 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
// An instance may reference a LaunchTemplate for its config; fields the
|
||||
// properties don't set resolve from the template's data, as on AWS.
|
||||
if (props != null && props.has("LaunchTemplate")) {
|
||||
JsonNode ltRef = engine.resolveNode(props.get("LaunchTemplate"));
|
||||
try {
|
||||
LaunchTemplateData ltData = ec2Service.resolveLaunchTemplateData(region,
|
||||
ltRef.path("LaunchTemplateId").asText(null),
|
||||
ltRef.path("LaunchTemplateName").asText(null),
|
||||
ltRef.path("Version").asText(null));
|
||||
if (imageId == null || imageId.isBlank()) {
|
||||
imageId = ltData.getImageId();
|
||||
}
|
||||
if (instanceType == null || instanceType.isBlank()) {
|
||||
instanceType = ltData.getInstanceType();
|
||||
}
|
||||
if (keyName == null || keyName.isBlank()) {
|
||||
keyName = ltData.getKeyName();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not resolve launch template for instance {0}: {1}",
|
||||
r.getLogicalId(), e.getMessage());
|
||||
JsonNode ltRef = props != null ? engine.resolveNode(props.get("LaunchTemplate")) : null;
|
||||
String ltId = ltRef != null ? ltRef.path("LaunchTemplateId").asText(null) : null;
|
||||
String ltName = ltRef != null ? ltRef.path("LaunchTemplateName").asText(null) : null;
|
||||
// Only look one up when the property actually names a template. An Fn::If that selects
|
||||
// AWS::NoValue resolves to an empty node here, which on AWS means the property is absent
|
||||
// (launch without a template), so it must not turn into an InvalidLaunchTemplateId.NotFound.
|
||||
if (ltRef != null && ltRef.isObject() && (ltId != null || ltName != null)) {
|
||||
// A LaunchTemplate the stack references but that does not resolve is a real error, as on
|
||||
// AWS: let it fail the resource rather than silently launching with default config.
|
||||
LaunchTemplateData ltData = ec2Service.resolveLaunchTemplateData(region, ltId, ltName,
|
||||
ltRef.path("Version").asText(null));
|
||||
if (imageId == null || imageId.isBlank()) {
|
||||
imageId = ltData.getImageId();
|
||||
}
|
||||
if (instanceType == null || instanceType.isBlank()) {
|
||||
instanceType = ltData.getInstanceType();
|
||||
}
|
||||
if (keyName == null || keyName.isBlank()) {
|
||||
keyName = ltData.getKeyName();
|
||||
}
|
||||
}
|
||||
if (instanceType == null || instanceType.isBlank()) {
|
||||
|
||||
+31
@@ -91,6 +91,37 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
eq("template-key"), anyList(), any(), any(), anyList(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLaunchTemplateThatDoesNotResolveFailsTheInstance() throws Exception {
|
||||
when(ec2.resolveLaunchTemplateData("us-east-1", "lt-missing", null, null))
|
||||
.thenThrow(new AwsException("InvalidLaunchTemplateId.NotFound",
|
||||
"The specified launch template does not exist.", 400));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(resource("Server"), props("""
|
||||
{"LaunchTemplate": {"LaunchTemplateId": "lt-missing"}}
|
||||
"""), ctx()));
|
||||
|
||||
assertEquals("InvalidLaunchTemplateId.NotFound", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLaunchTemplateResolvingToNoValueLaunchesWithoutIt() throws Exception {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-nolt");
|
||||
stubLaunch(instance);
|
||||
|
||||
// An Fn::If selecting AWS::NoValue resolves to an empty node, which on AWS means the property
|
||||
// is absent: the instance launches without a template rather than failing to resolve one.
|
||||
StackResource r = resource("Server");
|
||||
provisioner.provision(r, props("""
|
||||
{"LaunchTemplate": ""}
|
||||
"""), ctx());
|
||||
|
||||
assertEquals("i-nolt", r.getPhysicalId());
|
||||
verify(ec2).runInstances(eq("us-east-1"), any(), eq("t3.micro"), eq(1), eq(1), any(),
|
||||
anyList(), any(), any(), anyList(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsAnInstanceDeclaredWithNoProperties() {
|
||||
Instance instance = new Instance();
|
||||
|
||||
Reference in new issue
Block a user