mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): decode CloudFormation launch template user data (#4711)
* fix(ec2): decode CloudFormation launch template user data * fix(ec2): bound and recover launch template user data * fix(ec2): honor explicit user data over legacy templates
This commit is contained in:
1 parent
a338a430cc
commit
3df7fb17ee
14 files changed
+449
-59
No files matched your search
@@ -122,12 +122,25 @@ public final class UserDataPipeline {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
USER_DATA_DECOMPRESSION_BUDGET.acquire();
|
||||
byte[] decompressed = decompressGzipWithLimit(payload);
|
||||
if (decompressed == null) {
|
||||
LOG.warnv("UserData decompressed past {0} bytes; discarding as oversized",
|
||||
MAX_DECOMPRESSED_USER_DATA_BYTES);
|
||||
}
|
||||
return decompressed;
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
LOG.warnv("Interrupted while waiting for UserData decompression budget; discarding payload");
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("UserData starts with the gzip magic bytes but could not be decompressed: {0}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns null for oversized content; both API decoding and guest execution share this budget. */
|
||||
public static byte[] decompressGzipWithLimit(byte[] payload) throws IOException, InterruptedException {
|
||||
USER_DATA_DECOMPRESSION_BUDGET.acquire();
|
||||
try {
|
||||
Runnable hook = userDataDecompressionTestHook;
|
||||
if (hook != null) {
|
||||
@@ -145,16 +158,11 @@ public final class UserDataPipeline {
|
||||
while ((read = gzip.read(buffer)) != -1) {
|
||||
totalRead += read;
|
||||
if (totalRead > MAX_DECOMPRESSED_USER_DATA_BYTES) {
|
||||
LOG.warnv("UserData decompressed past {0} bytes; discarding as oversized",
|
||||
MAX_DECOMPRESSED_USER_DATA_BYTES);
|
||||
return null;
|
||||
}
|
||||
decompressed.write(buffer, 0, read);
|
||||
}
|
||||
return decompressed.toByteArray();
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("UserData starts with the gzip magic bytes but could not be decompressed: {0}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
USER_DATA_DECOMPRESSION_BUDGET.release();
|
||||
|
||||
+5
-2
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.ScalingActivity;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2UserDataDecoder;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Reservation;
|
||||
@@ -592,7 +593,8 @@ public class AutoScalingReconciler {
|
||||
version.getData().getKeyName(),
|
||||
version.getData().effectiveSecurityGroupIds(),
|
||||
version.getData().getInstanceTags(),
|
||||
version.getData().getUserData(),
|
||||
Ec2UserDataDecoder.decodeIfMissing(
|
||||
version.getData().getUserData(), version.getData().getEncodedUserData()),
|
||||
ec2Service.iamInstanceProfileArn(version.getData()),
|
||||
asg.getLaunchTemplateId(),
|
||||
asg.getLaunchTemplateName(),
|
||||
@@ -622,7 +624,8 @@ public class AutoScalingReconciler {
|
||||
version.getData().getKeyName(),
|
||||
version.getData().effectiveSecurityGroupIds(),
|
||||
version.getData().getInstanceTags(),
|
||||
version.getData().getUserData(),
|
||||
Ec2UserDataDecoder.decodeIfMissing(
|
||||
version.getData().getUserData(), version.getData().getEncodedUserData()),
|
||||
ec2Service.iamInstanceProfileArn(version.getData()),
|
||||
specification.getLaunchTemplateId() == null
|
||||
? mixedLaunchTemplate.getLaunchTemplateId()
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
// 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));
|
||||
ltRef.path("Version").asText(null), false);
|
||||
if (imageId == null || imageId.isBlank()) {
|
||||
imageId = ltData.getImageId();
|
||||
}
|
||||
|
||||
+4
-1
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2UserDataDecoder;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -53,7 +54,9 @@ public class Ec2LaunchTemplateCfnProvisioner implements CfnResourceProvisioner {
|
||||
data.setInstanceType(node.path("InstanceType").asText(null));
|
||||
data.setKeyName(node.path("KeyName").asText(null));
|
||||
// CFN carries UserData already base64-encoded.
|
||||
data.setEncodedUserData(node.path("UserData").asText(null));
|
||||
String encodedUserData = node.path("UserData").asText(null);
|
||||
data.setEncodedUserData(encodedUserData);
|
||||
data.setUserData(Ec2UserDataDecoder.decode(encodedUserData));
|
||||
data.setIamInstanceProfile(iamInstanceProfile(node.path("IamInstanceProfile")));
|
||||
if (node.has("SecurityGroupIds")) {
|
||||
List<String> securityGroupIds = new ArrayList<>();
|
||||
|
||||
@@ -16,14 +16,11 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@ApplicationScoped
|
||||
public class Ec2QueryHandler {
|
||||
@@ -728,7 +725,7 @@ public class Ec2QueryHandler {
|
||||
String userDataEncoded = p.getFirst("UserData");
|
||||
String userData = null;
|
||||
if (userDataEncoded != null && !userDataEncoded.isBlank()) {
|
||||
userData = decodeUserData(userDataEncoded);
|
||||
userData = Ec2UserDataDecoder.decode(userDataEncoded);
|
||||
}
|
||||
|
||||
String iamInstanceProfileArn = resolveIamInstanceProfileArn(p);
|
||||
@@ -760,7 +757,8 @@ public class Ec2QueryHandler {
|
||||
LaunchTemplateData.MetadataOptions metadataOptions = parseMetadataOptions(p, "MetadataOptions.");
|
||||
String creditSpecificationCpuCredits = p.getFirst("CreditSpecification.CpuCredits");
|
||||
|
||||
LaunchTemplateData launchTemplateData = resolveRunInstancesLaunchTemplateData(p, region);
|
||||
LaunchTemplateData launchTemplateData = resolveRunInstancesLaunchTemplateData(
|
||||
p, region, userDataEncoded == null || userDataEncoded.isBlank());
|
||||
if (launchTemplateData != null) {
|
||||
if (launchTemplateData.getMetadataOptions() != null) {
|
||||
metadataOptions = LaunchTemplateData.MetadataOptions.merge(
|
||||
@@ -837,14 +835,15 @@ public class Ec2QueryHandler {
|
||||
throw new AwsException("InvalidParameterValue", name + " must be a positive integer", 400);
|
||||
}
|
||||
|
||||
private LaunchTemplateData resolveRunInstancesLaunchTemplateData(MultivaluedMap<String, String> p, String region) {
|
||||
private LaunchTemplateData resolveRunInstancesLaunchTemplateData(MultivaluedMap<String, String> p, String region,
|
||||
boolean decodeUserData) {
|
||||
String id = p.getFirst("LaunchTemplate.LaunchTemplateId");
|
||||
String name = p.getFirst("LaunchTemplate.LaunchTemplateName");
|
||||
String version = p.getFirst("LaunchTemplate.Version");
|
||||
if ((id == null || id.isBlank()) && (name == null || name.isBlank())) {
|
||||
return null;
|
||||
}
|
||||
return service.resolveLaunchTemplateData(region, id, name, version);
|
||||
return service.resolveLaunchTemplateData(region, id, name, version, decodeUserData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1447,7 +1446,7 @@ public class Ec2QueryHandler {
|
||||
// describes back exactly as sent.
|
||||
String userDataEncoded = p.getFirst("UserData.Value");
|
||||
if (userDataEncoded != null) {
|
||||
service.modifyInstanceUserData(region, instanceId, decodeUserData(userDataEncoded), userDataEncoded);
|
||||
service.modifyInstanceUserData(region, instanceId, Ec2UserDataDecoder.decode(userDataEncoded), userDataEncoded);
|
||||
}
|
||||
// Find which attribute is being modified
|
||||
for (String attr : List.of("InstanceType.Value", "SourceDestCheck.Value", "EbsOptimized.Value")) {
|
||||
@@ -5219,7 +5218,7 @@ public class Ec2QueryHandler {
|
||||
|
||||
String encodedUserData = p.getFirst(prefix + ".UserData");
|
||||
data.setEncodedUserData(encodedUserData);
|
||||
data.setUserData(decodeUserData(encodedUserData));
|
||||
data.setUserData(Ec2UserDataDecoder.decode(encodedUserData));
|
||||
|
||||
String profileArn = p.getFirst(prefix + ".IamInstanceProfile.Arn");
|
||||
String profileName = p.getFirst(prefix + ".IamInstanceProfile.Name");
|
||||
@@ -5564,27 +5563,6 @@ public class Ec2QueryHandler {
|
||||
return value != null ? String.valueOf(value) : null;
|
||||
}
|
||||
|
||||
private String decodeUserData(String userDataEncoded) {
|
||||
if (userDataEncoded == null || userDataEncoded.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
byte[] decoded;
|
||||
try {
|
||||
decoded = Base64.getDecoder().decode(userDataEncoded);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData is not valid base64 content.", 400);
|
||||
}
|
||||
if (decoded.length >= 2 && (decoded[0] & 0xff) == 0x1f && (decoded[1] & 0xff) == 0x8b) {
|
||||
try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(decoded))) {
|
||||
decoded = gzip.readAllBytes();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData is not valid gzip content.", 400);
|
||||
}
|
||||
}
|
||||
return new String(decoded, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private String vpcEndpointXml(VpcEndpoint endpoint) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("vpcEndpointId", endpoint.getVpcEndpointId())
|
||||
|
||||
@@ -6679,13 +6679,22 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
|
||||
public LaunchTemplateData resolveLaunchTemplateData(String region, String id, String name, String version) {
|
||||
return resolveLaunchTemplateData(region, id, name, version, true);
|
||||
}
|
||||
|
||||
public LaunchTemplateData resolveLaunchTemplateData(String region, String id, String name, String version,
|
||||
boolean decodeUserData) {
|
||||
ensureDefaultResources(region);
|
||||
LaunchTemplate launchTemplate = findLaunchTemplate(region, id, name);
|
||||
String resolvedVersion = resolveLaunchTemplateVersion(
|
||||
launchTemplate,
|
||||
version,
|
||||
launchTemplate.getDefaultVersionNumber());
|
||||
return new LaunchTemplateData(versionData(launchTemplate, resolvedVersion));
|
||||
LaunchTemplateData data = new LaunchTemplateData(versionData(launchTemplate, resolvedVersion));
|
||||
if (decodeUserData) {
|
||||
data.setUserData(Ec2UserDataDecoder.decodeIfMissing(data.getUserData(), data.getEncodedUserData()));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public LaunchTemplate deleteLaunchTemplate(String region, String id, String name) {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class Ec2UserDataDecoder {
|
||||
|
||||
private static final int MAX_USER_DATA_BYTES = 16 * 1024;
|
||||
private static final int MAX_ENCODED_USER_DATA_LENGTH = ((MAX_USER_DATA_BYTES + 2) / 3) * 4;
|
||||
|
||||
private Ec2UserDataDecoder() {
|
||||
}
|
||||
|
||||
public static String decodeIfMissing(String userData, String encodedUserData) {
|
||||
return userData != null ? userData : decode(encodedUserData);
|
||||
}
|
||||
|
||||
public static String decode(String encoded) {
|
||||
if (encoded == null || encoded.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
if (encoded.length() > MAX_ENCODED_USER_DATA_LENGTH) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData exceeds the 16 KiB limit.", 400);
|
||||
}
|
||||
byte[] decoded;
|
||||
try {
|
||||
decoded = Base64.getDecoder().decode(encoded);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData is not valid base64 content.", 400);
|
||||
}
|
||||
if (decoded.length > MAX_USER_DATA_BYTES) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData exceeds the 16 KiB limit.", 400);
|
||||
}
|
||||
if (decoded.length >= 2 && (decoded[0] & 0xff) == 0x1f && (decoded[1] & 0xff) == 0x8b) {
|
||||
try {
|
||||
decoded = UserDataPipeline.decompressGzipWithLimit(decoded);
|
||||
if (decoded == null) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData exceeds the decompressed size limit.", 400);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new AwsException("InternalError", "Interrupted while decoding UserData.", 500);
|
||||
} catch (IOException e) {
|
||||
throw new AwsException("InvalidParameterValue", "UserData is not valid gzip content.", 400);
|
||||
}
|
||||
}
|
||||
return new String(decoded, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -27,6 +27,7 @@ import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.anyList;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
@@ -110,6 +111,7 @@ class AutoScalingReconcilerTest {
|
||||
version.setLatestVersionNumber("1");
|
||||
version.getData().setImageId("ami-version-1");
|
||||
version.getData().setInstanceType("t3.micro");
|
||||
version.getData().setEncodedUserData("IyEvYmluL2Jhc2gKZWNobyBoaQo=");
|
||||
version.getData().setIamInstanceProfile(new LaunchTemplateData.IamInstanceProfile(
|
||||
"arn:aws:iam::000000000000:instance-profile/app-profile", null));
|
||||
when(ec2Service.iamInstanceProfileArn(version.getData()))
|
||||
@@ -128,7 +130,8 @@ class AutoScalingReconcilerTest {
|
||||
reservation.setInstances(List.of(ec2Instance));
|
||||
when(ec2Service.runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),
|
||||
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
|
||||
anyList(), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"), eq(null))).thenReturn(reservation);
|
||||
anyList(), eq("#!/bin/bash\necho hi\n"),
|
||||
eq("arn:aws:iam::000000000000:instance-profile/app-profile"), eq(null))).thenReturn(reservation);
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
@@ -139,7 +142,9 @@ class AutoScalingReconcilerTest {
|
||||
ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.captor();
|
||||
verify(ec2Service).runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),
|
||||
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
|
||||
tags.capture(), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"), eq(null));
|
||||
tags.capture(), eq("#!/bin/bash\necho hi\n"),
|
||||
eq("arn:aws:iam::000000000000:instance-profile/app-profile"), eq(null));
|
||||
assertNull(version.getData().getUserData());
|
||||
assertEquals(propagatedTags.size(), tags.getValue().size());
|
||||
assertEquals("app.ClusterId", tags.getValue().get(0).getKey());
|
||||
assertEquals("development", tags.getValue().get(0).getValue());
|
||||
@@ -308,6 +313,7 @@ class AutoScalingReconcilerTest {
|
||||
version.setLatestVersionNumber("3");
|
||||
version.getData().setImageId("ami-version-3");
|
||||
version.getData().setInstanceType("t3.micro");
|
||||
version.getData().setEncodedUserData("IyEvYmluL2Jhc2gKZWNobyBoaQo=");
|
||||
when(ec2Service.describeLaunchTemplates("us-east-1", List.of("lt-123"), List.of(), Map.of()))
|
||||
.thenReturn(List.of(launchTemplate));
|
||||
when(ec2Service.describeLaunchTemplateVersions("us-east-1", "lt-123", null, List.of("3")))
|
||||
@@ -318,7 +324,7 @@ class AutoScalingReconcilerTest {
|
||||
reservation.setInstances(List.of(ec2Instance));
|
||||
when(ec2Service.runInstances(eq("us-east-1"), eq("ami-version-3"), eq("t3.small"),
|
||||
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
|
||||
eq(List.of()), eq(null), eq(null), eq(null))).thenReturn(reservation);
|
||||
eq(List.of()), eq("#!/bin/bash\necho hi\n"), eq(null), eq(null))).thenReturn(reservation);
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
@@ -329,7 +335,8 @@ class AutoScalingReconcilerTest {
|
||||
assertEquals("t3.small", asg.getInstances().getFirst().getInstanceType());
|
||||
verify(ec2Service).runInstances(eq("us-east-1"), eq("ami-version-3"), eq("t3.small"),
|
||||
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
|
||||
eq(List.of()), eq(null), eq(null), eq(null));
|
||||
eq(List.of()), eq("#!/bin/bash\necho hi\n"), eq(null), eq(null));
|
||||
assertNull(version.getData().getUserData());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+174
-2
@@ -1,11 +1,22 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
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.LaunchTemplateData;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* End-to-end check that CloudFormation provisions AWS::EC2::LaunchTemplate for real
|
||||
@@ -16,6 +27,9 @@ import static org.hamcrest.Matchers.not;
|
||||
@QuarkusTest
|
||||
class CloudFormationLaunchTemplateIntegrationTest {
|
||||
|
||||
@Inject
|
||||
Ec2Service ec2Service;
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String EC2_AUTH =
|
||||
@@ -95,6 +109,7 @@ class CloudFormationLaunchTemplateIntegrationTest {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String templateName = "cfn-lt-" + suffix;
|
||||
String stackName = "cfn-lt-stack-" + suffix;
|
||||
String encodedUserData = "IyEvYmluL2Jhc2gKZWNobyBoaQo=";
|
||||
|
||||
String template = """
|
||||
{
|
||||
@@ -106,7 +121,7 @@ class CloudFormationLaunchTemplateIntegrationTest {
|
||||
"LaunchTemplateData": {
|
||||
"ImageId": "ami-12345678",
|
||||
"InstanceType": "t3.micro",
|
||||
"UserData": "IyEvYmluL2Jhc2gKZWNobyBoaQo="
|
||||
"UserData": "%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +131,7 @@ class CloudFormationLaunchTemplateIntegrationTest {
|
||||
"LatestVersion": {"Value": {"Fn::GetAtt": ["AppLaunchTemplate", "LatestVersionNumber"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(templateName);
|
||||
""".formatted(templateName, encodedUserData);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
@@ -156,6 +171,51 @@ class CloudFormationLaunchTemplateIntegrationTest {
|
||||
.statusCode(200)
|
||||
.body(containsString(templateName));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeLaunchTemplateVersions")
|
||||
.formParam("LaunchTemplateName", templateName)
|
||||
.formParam("Versions.1", "$Latest")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.userData",
|
||||
equalTo(encodedUserData));
|
||||
|
||||
LaunchTemplateData launchData = ec2Service.resolveLaunchTemplateData(
|
||||
"us-east-1", null, templateName, null);
|
||||
assertEquals("#!/bin/bash\necho hi\n", launchData.getUserData());
|
||||
assertEquals(encodedUserData, launchData.getEncodedUserData());
|
||||
|
||||
String instanceId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("LaunchTemplate.LaunchTemplateName", templateName)
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
Instance instance = ec2Service.describeInstances("us-east-1", List.of(instanceId), Map.of())
|
||||
.getFirst().getInstances().getFirst();
|
||||
assertEquals(launchData.getUserData(), instance.getUserData());
|
||||
assertEquals(encodedUserData, instance.getEncodedUserData());
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "TerminateInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
@@ -178,4 +238,116 @@ class CloudFormationLaunchTemplateIntegrationTest {
|
||||
.statusCode(200)
|
||||
.body(not(containsString(templateName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitUserDataOverridesOversizedLegacyTemplate() {
|
||||
String templateName = "legacy-user-data-" + Long.toString(System.nanoTime(), 36);
|
||||
String oversized = Base64.getEncoder().encodeToString(
|
||||
"A".repeat(16 * 1024 + 1).getBytes(StandardCharsets.UTF_8));
|
||||
LaunchTemplateData legacyData = new LaunchTemplateData();
|
||||
legacyData.setImageId("ami-12345678");
|
||||
legacyData.setInstanceType("t3.micro");
|
||||
legacyData.setEncodedUserData(oversized);
|
||||
ec2Service.createLaunchTemplate("us-east-1", templateName, legacyData, List.of());
|
||||
|
||||
String userData = "#!/bin/sh\necho override\n";
|
||||
String replacement = Base64.getEncoder().encodeToString(userData.getBytes(StandardCharsets.UTF_8));
|
||||
String instanceId = null;
|
||||
try {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("LaunchTemplate.LaunchTemplateName", templateName)
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("UserData exceeds the 16 KiB limit"));
|
||||
|
||||
instanceId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("LaunchTemplate.LaunchTemplateName", templateName)
|
||||
.formParam("UserData", replacement)
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
Instance instance = ec2Service.describeInstances("us-east-1", List.of(instanceId), Map.of())
|
||||
.getFirst().getInstances().getFirst();
|
||||
assertEquals(userData, instance.getUserData());
|
||||
assertEquals(replacement, instance.getEncodedUserData());
|
||||
} finally {
|
||||
if (instanceId != null) {
|
||||
ec2Service.terminateInstances("us-east-1", List.of(instanceId));
|
||||
}
|
||||
ec2Service.deleteLaunchTemplate("us-east-1", null, templateName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloudFormationInstanceIgnoresUnusedLegacyTemplateUserData() throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String templateName = "legacy-cfn-user-data-" + suffix;
|
||||
String stackName = "legacy-cfn-user-data-stack-" + suffix;
|
||||
String oversized = Base64.getEncoder().encodeToString(
|
||||
"A".repeat(16 * 1024 + 1).getBytes(StandardCharsets.UTF_8));
|
||||
LaunchTemplateData legacyData = new LaunchTemplateData();
|
||||
legacyData.setImageId("ami-12345678");
|
||||
legacyData.setInstanceType("t3.micro");
|
||||
legacyData.setEncodedUserData(oversized);
|
||||
ec2Service.createLaunchTemplate("us-east-1", templateName, legacyData, List.of());
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Inst": {
|
||||
"Type": "AWS::EC2::Instance",
|
||||
"Properties": {
|
||||
"LaunchTemplate": {"LaunchTemplateName": "%s", "Version": "1"},
|
||||
"UserData": "IyEvYmluL3NoCmVjaG8gY2ZuCg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(templateName);
|
||||
try (AutoCloseable cleanup = () -> {
|
||||
try {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.awaitStackDeleted(stackName);
|
||||
} finally {
|
||||
ec2Service.deleteLaunchTemplate("us-east-1", null, templateName);
|
||||
}
|
||||
}) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
|
||||
assertEquals("CREATE_COMPLETE", state.status(), state.reason());
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -123,7 +123,7 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
data.setImageId("ami-from-template");
|
||||
data.setInstanceType("m5.large");
|
||||
data.setKeyName("template-key");
|
||||
when(ec2.resolveLaunchTemplateData("us-east-1", "lt-1", null, "1")).thenReturn(data);
|
||||
when(ec2.resolveLaunchTemplateData("us-east-1", "lt-1", null, "1", false)).thenReturn(data);
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-2");
|
||||
stubLaunch(instance);
|
||||
@@ -138,7 +138,7 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void aLaunchTemplateThatDoesNotResolveFailsTheInstance() throws Exception {
|
||||
when(ec2.resolveLaunchTemplateData("us-east-1", "lt-missing", null, null))
|
||||
when(ec2.resolveLaunchTemplateData("us-east-1", "lt-missing", null, null, false))
|
||||
.thenThrow(new AwsException("InvalidLaunchTemplateId.NotFound",
|
||||
"The specified launch template does not exist.", 400));
|
||||
|
||||
|
||||
+43
-1
@@ -3,21 +3,29 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -196,4 +204,38 @@ class Ec2LaunchTemplateCfnProvisionerTest {
|
||||
assertNull(captor.getValue().getIamInstanceProfile().getName());
|
||||
assertEquals(List.of("sg-1", "sg-2"), captor.getValue().getSecurityGroupIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionedUserDataIsDecodedForInstanceLaunch() {
|
||||
when(ec2.createLaunchTemplate(any(), any(), any(), any())).thenReturn(template("lt-userdata"));
|
||||
String script = "#!/bin/bash\necho from-cfn\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
ObjectNode data = mapper.createObjectNode().put("UserData", encoded);
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.set("LaunchTemplateData", data);
|
||||
|
||||
provisioner.provision(resource("Lt"), props, ctx());
|
||||
|
||||
ArgumentCaptor<LaunchTemplateData> captor = ArgumentCaptor.forClass(LaunchTemplateData.class);
|
||||
verify(ec2).createLaunchTemplate(eq("us-east-1"), anyString(), captor.capture(), isNull());
|
||||
assertEquals(encoded, captor.getValue().getEncodedUserData());
|
||||
assertEquals(script, captor.getValue().getUserData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedGzippedUserDataIsRejectedBeforeTemplateCreation() throws IOException {
|
||||
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(compressed)) {
|
||||
gzip.write("A".repeat(UserDataPipeline.MAX_DECOMPRESSED_USER_DATA_BYTES + 1)
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.set("LaunchTemplateData", mapper.createObjectNode()
|
||||
.put("UserData", Base64.getEncoder().encodeToString(compressed.toByteArray())));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource("Lt"), props, ctx()));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
verify(ec2, never()).createLaunchTemplate(any(), any(), any(), any());
|
||||
}
|
||||
}
|
||||
@@ -566,18 +566,15 @@ class Ec2ContainerManagerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void userDataShellScriptsBoundsAggregateConcurrentDecompression() throws Exception {
|
||||
// Each launch decodes its UserData independently on Ec2ContainerManager's unbounded
|
||||
// cached launch executor, so the per-payload 10 MB cap alone does not stop many
|
||||
// concurrent launches from each decompressing near that limit at once (Greptile's
|
||||
// follow-up on the gzip-bomb fix). Drive real concurrent decompressions through the
|
||||
// public entry point and use the test hook to prove no more than
|
||||
// MAX_CONCURRENT_USER_DATA_DECOMPRESSIONS (4) ever run at the same instant, while still
|
||||
// exercising genuine concurrency rather than serialized calls.
|
||||
void ec2UserDataPathsShareAggregateConcurrentDecompressionBudget() throws Exception {
|
||||
// API decoding and guest execution share the same budget. The hook must see every
|
||||
// decompression, and no more than four may run at once across both entry points.
|
||||
int concurrentLaunches = 12;
|
||||
AtomicInteger totalDecompressions = new AtomicInteger(0);
|
||||
AtomicInteger active = new AtomicInteger(0);
|
||||
AtomicInteger peakActive = new AtomicInteger(0);
|
||||
UserDataPipeline.userDataDecompressionTestHook = () -> {
|
||||
totalDecompressions.incrementAndGet();
|
||||
int now = active.incrementAndGet();
|
||||
peakActive.accumulateAndGet(now, Math::max);
|
||||
try {
|
||||
@@ -598,7 +595,11 @@ class Ec2ContainerManagerTest {
|
||||
|
||||
List<Future<List<String>>> futures = new ArrayList<>();
|
||||
for (int i = 0; i < concurrentLaunches; i++) {
|
||||
futures.add(pool.submit(() -> Ec2ContainerManager.userDataShellScripts(gzipped)));
|
||||
if (i % 2 == 0) {
|
||||
futures.add(pool.submit(() -> Ec2ContainerManager.userDataShellScripts(gzipped)));
|
||||
} else {
|
||||
futures.add(pool.submit(() -> List.of(Ec2UserDataDecoder.decode(gzipped))));
|
||||
}
|
||||
}
|
||||
for (Future<List<String>> future : futures) {
|
||||
results.add(future.get(30, TimeUnit.SECONDS));
|
||||
@@ -609,6 +610,7 @@ class Ec2ContainerManagerTest {
|
||||
}
|
||||
|
||||
assertEquals(concurrentLaunches, results.size());
|
||||
assertEquals(concurrentLaunches, totalDecompressions.get());
|
||||
assertTrue(peakActive.get() <= 4,
|
||||
"peak concurrent UserData decompressions was " + peakActive.get() + ", expected <= 4");
|
||||
assertTrue(peakActive.get() >= 2,
|
||||
|
||||
@@ -41,6 +41,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -359,6 +360,40 @@ class Ec2ServicePersistenceTest {
|
||||
"instance tags on a legacy template with no versions map must survive restart");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyLaunchTemplateUserDataDecodesOnRead(@TempDir Path dir) throws IOException {
|
||||
String encoded = "IyEvYmluL2Jhc2gKZWNobyBoaQo=";
|
||||
String legacyJson = """
|
||||
{
|
||||
"us-east-1::lt-legacy-user-data": {
|
||||
"launchTemplateId": "lt-legacy-user-data",
|
||||
"launchTemplateName": "legacy-user-data",
|
||||
"defaultVersionNumber": "1",
|
||||
"latestVersionNumber": "1",
|
||||
"region": "us-east-1",
|
||||
"data": { "encodedUserData": "%s" },
|
||||
"versions": {
|
||||
"1": { "encodedUserData": "%s" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(encoded, encoded);
|
||||
Files.writeString(dir.resolve("ec2-launch-templates.json"), legacyJson);
|
||||
|
||||
Ec2Service restarted = newService(dir);
|
||||
LaunchTemplateData resolved = restarted.resolveLaunchTemplateData(
|
||||
REGION, "lt-legacy-user-data", null, "1");
|
||||
assertEquals(encoded, resolved.getEncodedUserData());
|
||||
assertEquals("#!/bin/bash\necho hi\n", resolved.getUserData());
|
||||
|
||||
LaunchTemplateData described = restarted.describeLaunchTemplateVersions(
|
||||
REGION, "lt-legacy-user-data", null, List.of("1")).getFirst().getData();
|
||||
assertEquals(encoded, described.getEncodedUserData());
|
||||
assertNull(described.getUserData());
|
||||
assertNull(restarted.describeLaunchTemplates(REGION, List.of("lt-legacy-user-data"),
|
||||
List.of(), Map.of()).getFirst().getData().getUserData());
|
||||
}
|
||||
|
||||
private BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
|
||||
EbsBlockDevice ebs = new EbsBlockDevice();
|
||||
ebs.setSnapshotId(snapshotId);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class Ec2UserDataDecoderTest {
|
||||
|
||||
@Test
|
||||
void decodesGzippedUserData() throws IOException {
|
||||
String userData = "#!/bin/bash\necho hi\n";
|
||||
|
||||
assertEquals(userData, Ec2UserDataDecoder.decode(gzipBase64(userData)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsGzipExpansionAtExecutionLimit() throws IOException {
|
||||
String userData = "A".repeat(UserDataPipeline.MAX_DECOMPRESSED_USER_DATA_BYTES);
|
||||
|
||||
assertEquals(userData, Ec2UserDataDecoder.decode(gzipBase64(userData)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsAwsRawUserDataLimit() {
|
||||
String raw = "A".repeat(16 * 1024);
|
||||
|
||||
assertEquals(raw, Ec2UserDataDecoder.decode(
|
||||
Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsRawUserDataBeyondAwsLimit() {
|
||||
String raw = "A".repeat(16 * 1024 + 1);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> Ec2UserDataDecoder.decode(
|
||||
Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8))));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsGzipExpansionBeyondExecutionLimit() throws IOException {
|
||||
String oversized = "A".repeat(UserDataPipeline.MAX_DECOMPRESSED_USER_DATA_BYTES + 1);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> Ec2UserDataDecoder.decode(gzipBase64(oversized)));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preservesInvalidBase64AndGzipErrors() {
|
||||
AwsException invalidBase64 = assertThrows(AwsException.class,
|
||||
() -> Ec2UserDataDecoder.decode("not-base64"));
|
||||
assertEquals("InvalidParameterValue", invalidBase64.getErrorCode());
|
||||
|
||||
String malformedGzip = Base64.getEncoder().encodeToString(new byte[]{0x1f, (byte) 0x8b, 0x00});
|
||||
AwsException invalidGzip = assertThrows(AwsException.class,
|
||||
() -> Ec2UserDataDecoder.decode(malformedGzip));
|
||||
assertEquals("InvalidParameterValue", invalidGzip.getErrorCode());
|
||||
}
|
||||
|
||||
private static String gzipBase64(String content) throws IOException {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(bytes)) {
|
||||
gzip.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(bytes.toByteArray());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user