mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+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