feat(emr): keep persistent application UIs and answer their presigned URLs (#8711)

EMR's application UI operations, CreatePersistentAppUI,
DescribePersistentAppUI, GetPersistentAppUIPresignedURL and
GetOnClusterAppUIPresignedURL, were not served.

Floci now keeps a cluster's persistent application UIs, with the types
its release and applications give it (the Spark History Server from
5.25.0, the Tez UI and YARN timeline server from 5.30.1), refusing a
cluster with more than one primary node and an account past 200, and
reports whether the cluster's security configuration turns on runtime
roles. It serves none of those UIs: a presigned URL, persistent or
on-cluster, opens a signed one-hour page of Floci's about the cluster
and the interface asked for.

The link signing of EMR Serverless's dashboards and these pages is one
helper, core.common.LinkSigner, and their HTML page and table rows come
from core.common.Html.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 02:32:58 -04:00
1 parent 7199c8580e
commit 751c97dde6
20 files changed
+959 -88

No files matched your search

@@ -2,7 +2,11 @@ package io.floci.aws.core.common;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class HtmlTest {
@@ -12,6 +16,19 @@ class HtmlTest {
Html.escape("<a href=\"x\" title='y'>R&D</a>"));
}
@Test
void aPageEscapesItsTitleAndARowEscapesItsCellsAndIsLeftOutWithoutAValue() {
List<String> lines = new ArrayList<>();
Html.row(lines, "Name", "<b>");
Html.row(lines, "Empty", "");
Html.row(lines, "Absent", null);
assertEquals(List.of("<tr><th>Name</th><td>&lt;b&gt;</td></tr>"), lines);
String page = Html.page("A & B", lines);
assertTrue(page.contains("<title>A &amp; B</title>"), page);
assertTrue(page.contains("<h1>A &amp; B</h1>\n<tr><th>Name</th>"), page);
}
@Test
void anEntityAlreadyWrittenIsEscapedAgainRatherThanReadAsOne() {
assertEquals("&amp;lt;", Html.escape("&lt;"));
@@ -0,0 +1,27 @@
package io.floci.aws.core.common;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LinkSignerTest {
private static final byte[] KEY = "a key of this process, 32 bytes!".getBytes(StandardCharsets.US_ASCII);
@Test
void aSignatureVerifiesForTheSameFieldsAndKeyAndForNothingElse() {
LinkSigner signer = new LinkSigner(KEY);
String signature = signer.sign("123456789012", "us-east-1", "j-1", "SHS");
assertEquals(signature, new LinkSigner(KEY).sign("123456789012", "us-east-1", "j-1", "SHS"));
assertTrue(signer.verifies(signature, "123456789012", "us-east-1", "j-1", "SHS"));
assertFalse(signer.verifies(signature, "123456789012", "us-east-1", "j-1", "TEZ"));
assertFalse(signer.verifies(signature, "123456789012", "us-east-1", "j-1"));
assertFalse(signer.verifies(null, "123456789012", "us-east-1", "j-1", "SHS"));
assertFalse(LinkSigner.withNewKey().verifies(signature, "123456789012", "us-east-1", "j-1", "SHS"));
}
}
@@ -0,0 +1,183 @@
package io.floci.aws.services.emr;
import io.floci.aws.core.common.Html;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import static io.floci.aws.testing.EmrRequests.emr;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
/**
* A cluster's application UIs over the AWS JSON 1.1 wire protocol: CreatePersistentAppUI, DescribePersistentAppUI and
* the two presigned URL operations, and the page Floci serves for their URLs, as it serves no application UI itself.
*/
@QuarkusTest
class EmrAppUiIntegrationTest {
private static final String ARN = "arn:aws:elasticmapreduce:us-east-1:000000000000:cluster/";
@BeforeAll
static void configure() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void aPersistentAppUiIsMadeForAClusterAndItsPresignedUrlOpensFlocisPage() {
String clusterId = cluster("emr-6.15.0", "[{\"Name\":\"Spark\"},{\"Name\":\"Tez\"},{\"Name\":\"Hadoop\"}]", "");
String appUiId = emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + clusterId + "\","
+ "\"Tags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}")
.then().statusCode(200)
.body("PersistentAppUIId", matchesPattern("p-[A-Z0-9]{13}"))
.body("RuntimeRoleEnabledCluster", equalTo(false))
.extract().path("PersistentAppUIId");
emr("DescribePersistentAppUI", "{\"PersistentAppUIId\":\"" + appUiId + "\"}")
.then().statusCode(200)
.body("PersistentAppUI.PersistentAppUIId", equalTo(appUiId))
.body("PersistentAppUI.PersistentAppUITypeList", equalTo(List.of("SHS", "TEZ", "YTS")))
.body("PersistentAppUI.PersistentAppUIStatus", equalTo("ACTIVE"))
.body("PersistentAppUI.AuthorId", equalTo("000000000000"))
.body("PersistentAppUI.CreationTime", notNullValue())
.body("PersistentAppUI.Tags[0].Key", equalTo("team"));
String url = emr("GetPersistentAppUIPresignedURL", "{\"PersistentAppUIId\":\"" + appUiId + "\","
+ "\"PersistentAppUIType\":\"TEZ\",\"ApplicationId\":\"application_1_0001\"}")
.then().statusCode(200)
.body("PresignedURLReady", equalTo(true))
.extract().path("PresignedURL");
page(url).then().statusCode(200).contentType(containsString("text/html"))
.body(containsString("<h1>TEZ of cluster " + clusterId + "</h1>"))
.body(containsString("<tr><th>Application</th><td>application_1_0001</td></tr>"))
.body(containsString(Html.escape(EmrAppUiController.NO_UI)));
page(url.replace("type=TEZ", "type=SHS")).then().statusCode(403)
.body(containsString(Html.escape(EmrAppUiController.INVALID)));
String withoutApplication = emr("GetPersistentAppUIPresignedURL", "{\"PersistentAppUIId\":\"" + appUiId
+ "\"}")
.then().statusCode(200).extract().path("PresignedURL");
page(withoutApplication).then().statusCode(200);
// A link with no application is not one for an application named "null".
page(withoutApplication.replace("&expires=", "&application=null&expires=")).then().statusCode(403);
terminate(clusterId);
}
@Test
void theProfilerTypeAskedForIsTheUisOneInterfaceAndATagNeedsAKey() {
String clusterId = cluster("emr-6.15.0", "[{\"Name\":\"Spark\"},{\"Name\":\"Tez\"}]", "");
String appUiId = emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + clusterId + "\","
+ "\"ProfilerType\":\"TEZUI\"}")
.then().statusCode(200).extract().path("PersistentAppUIId");
emr("DescribePersistentAppUI", "{\"PersistentAppUIId\":\"" + appUiId + "\"}")
.then().statusCode(200)
.body("PersistentAppUI.PersistentAppUITypeList", equalTo(List.of("TEZ")));
emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + clusterId + "\",\"ProfilerType\":\"YTS\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + clusterId + "\","
+ "\"Tags\":[{\"Value\":\"no-key\"}]}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
terminate(clusterId);
}
@Test
void anOnClusterUrlIsThereWhileTheClusterRunsAndADryRunGivesNone() {
String clusterId = cluster("emr-6.15.0", "[{\"Name\":\"Spark\"},{\"Name\":\"Hadoop\"}]", "");
String url = emr("GetOnClusterAppUIPresignedURL", "{\"ClusterId\":\"" + clusterId + "\","
+ "\"OnClusterAppUIType\":\"SparkHistoryServer\"}")
.then().statusCode(200)
.body("PresignedURLReady", equalTo(true))
.extract().path("PresignedURL");
page(url).then().statusCode(200)
.body(containsString("<h1>SparkHistoryServer of cluster " + clusterId + "</h1>"));
emr("GetOnClusterAppUIPresignedURL", "{\"ClusterId\":\"" + clusterId + "\",\"DryRun\":true}")
.then().statusCode(200)
.body("PresignedURLReady", equalTo(true))
.body("PresignedURL", nullValue());
emr("GetOnClusterAppUIPresignedURL", "{\"ClusterId\":\"" + clusterId + "\","
+ "\"OnClusterAppUIType\":\"Ganglia\"}")
.then().statusCode(400)
.body("__type", equalTo("ValidationException"));
terminate(clusterId);
emr("GetOnClusterAppUIPresignedURL", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
@Test
void aPersistentAppUiIsRefusedForAnOldReleaseAnotherResourceOrAnInterfaceTheClusterHasNot() {
String old = cluster("emr-5.24.0", "[{\"Name\":\"Spark\"}]", "");
emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + old + "\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"arn:aws:emr-containers:us-east-1:000000000000:"
+ "/virtualclusters/abc\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
emr("CreatePersistentAppUI", "{}")
.then().statusCode(400)
.body("__type", equalTo("ValidationException"));
String sparkOnly = cluster("emr-5.29.0", "[{\"Name\":\"Spark\"}]", "");
String appUiId = emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + sparkOnly + "\"}")
.then().statusCode(200).extract().path("PersistentAppUIId");
emr("DescribePersistentAppUI", "{\"PersistentAppUIId\":\"" + appUiId + "\"}")
.then().statusCode(200)
.body("PersistentAppUI.PersistentAppUITypeList", equalTo(List.of("SHS")));
emr("GetPersistentAppUIPresignedURL", "{\"PersistentAppUIId\":\"" + appUiId + "\","
+ "\"PersistentAppUIType\":\"TEZ\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
terminate(old);
terminate(sparkOnly);
}
@Test
void aClusterWhoseSecurityConfigurationTurnsOnRuntimeRolesIsReportedAsOne() {
String name = "runtime-roles-" + UUID.randomUUID();
emr("CreateSecurityConfiguration", "{\"Name\":\"" + name + "\",\"SecurityConfiguration\":"
+ "\"{\\\"AuthorizationConfiguration\\\":{\\\"IAMConfiguration\\\":"
+ "{\\\"EnableApplicationScopedIAMRole\\\":true}}}\"}")
.then().statusCode(200);
String clusterId = cluster("emr-6.15.0", "[{\"Name\":\"Spark\"}]",
"\"SecurityConfiguration\": \"" + name + "\",");
emr("CreatePersistentAppUI", "{\"TargetResourceArn\":\"" + ARN + clusterId + "\"}")
.then().statusCode(200)
.body("RuntimeRoleEnabledCluster", equalTo(true));
terminate(clusterId);
}
private static String cluster(String releaseLabel, String applications, String extra) {
return emr("RunJobFlow", """
{"Name": "app-uis", "ReleaseLabel": "%s", "Applications": %s, %s
"Instances": {"KeepJobFlowAliveWhenNoSteps": true,
"InstanceGroups": [{"Name": "primary", "InstanceRole": "MASTER", "InstanceType": "m5.xlarge",
"InstanceCount": 1}]}}
""".formatted(releaseLabel, applications, extra))
.then().statusCode(200).extract().path("JobFlowId");
}
private static void terminate(String clusterId) {
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}").then().statusCode(200);
}
// The page a link opens, asked for on the address this test's Floci listens on, which the link's base URL is not.
private static Response page(String url) {
URI link = URI.create(url);
return given().urlEncodingEnabled(false).when().get(link.getRawPath() + "?" + link.getRawQuery());
}
}
@@ -0,0 +1,57 @@
package io.floci.aws.services.emr;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.LinkSigner;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.services.emr.model.EmrCluster;
import io.floci.aws.services.emr.model.EmrPersistentAppUi;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.List;
import static io.floci.aws.services.emr.EmrServiceFixture.ACCOUNT;
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
import static io.floci.aws.services.emr.EmrServiceFixture.cluster;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** The account's cap on persistent application UIs, which ended clusters' UIs leave, via {@link EmrAppUiService}. */
class EmrAppUiServiceTest {
private final EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 0);
private final EmrAppUiService appUis = new EmrAppUiService(
AccountAwareStorageBackend.<EmrPersistentAppUi>inMemory(ACCOUNT), fixture.service,
new RegionResolver(REGION, ACCOUNT), fixture.clock, "http://localhost:4566", LinkSigner.withNewKey());
@Test
void aUiOfAClusterThatEndedMoreThanThirtyDaysAgoNoLongerCountsAgainstTheCap() {
String ended = sparkCluster();
for (int i = 0; i < EmrAppUiService.MAX_PERSISTENT_APP_UIS; i++) {
appUis.create(arn(ended), null, null);
}
String next = sparkCluster();
AwsException full = assertThrows(AwsException.class, () -> appUis.create(arn(next), null, null));
assertTrue(full.getMessage().contains("200 persistent application UIs"), full.getMessage());
fixture.service.terminateJobFlows(List.of(ended));
fixture.clock.advance(EmrAppUiService.LOGS_KEPT.minus(Duration.ofMinutes(1)));
assertThrows(AwsException.class, () -> appUis.create(arn(next), null, null));
fixture.clock.advance(Duration.ofMinutes(2));
assertEquals("ACTIVE", appUis.create(arn(next), null, null).appUi().getStatus());
}
private String sparkCluster() {
EmrCluster cluster = cluster("app-uis", true);
cluster.setReleaseLabel("emr-6.15.0");
cluster.setApplications("[{\"Name\":\"Spark\"}]");
return fixture.service.runJobFlow(cluster, REGION).getId();
}
private static String arn(String clusterId) {
return "arn:aws:elasticmapreduce:us-east-1:000000000000:cluster/" + clusterId;
}
}
@@ -136,8 +136,8 @@ class EmrNotebookExecutionServiceTest {
EmrCluster cluster = clusterSpec("emr-6.15.0", NOTEBOOK_APPS);
cluster.setId("j-FAILING");
cluster.setState("WAITING");
when(failing.describeCluster("j-FAILING")).thenReturn(cluster)
.thenThrow(new IllegalStateException("storage unavailable"));
when(failing.describeCluster("j-FAILING")).thenReturn(cluster);
when(failing.findDescribedCluster("j-FAILING")).thenThrow(new IllegalStateException("storage unavailable"));
EmrNotebookExecutionService service = new EmrNotebookExecutionService(store, failing,
new RegionResolver(REGION, ACCOUNT), fixture.clock, RUN);
String id = service.start(request("j-FAILING")).getId();
@@ -1113,7 +1113,7 @@ class EmrServiceContainerStepsTest {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode answer = (JsonNode) new EmrHandler(service, mock(EmrStudioService.class),
mock(EmrNotebookExecutionService.class), objectMapper)
mock(EmrNotebookExecutionService.class), mock(EmrAppUiService.class), objectMapper)
.handle("DescribeStep", objectMapper.createObjectNode().put("ClusterId", cluster.getId())
.put("StepId", cluster.getSteps().get(0).getId()), REGION).getEntity();
@@ -2042,7 +2042,7 @@ class EmrServiceContainerStepsTest {
String waitingId = bootstrapping.service.runJobFlow(cluster("waiting", true), REGION).getId();
ObjectMapper objectMapper = new ObjectMapper();
EmrHandler handler = new EmrHandler(bootstrapping.service, mock(EmrStudioService.class),
mock(EmrNotebookExecutionService.class), objectMapper);
mock(EmrNotebookExecutionService.class), mock(EmrAppUiService.class), objectMapper);
String page = ERROR_CODE_PAGES + "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE.html";
JsonNode expected = objectMapper.readTree("""
[{"ErrorCode": "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE",
@@ -3,6 +3,7 @@ package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.emr.EmrAppUiService;
import io.floci.aws.services.emr.EmrHandler;
import io.floci.aws.services.emr.EmrNotebookExecutionService;
import io.floci.aws.services.emr.EmrService;
@@ -75,7 +76,8 @@ class AslExecutorEmrSyncTest {
.customResourceLiveness(null)
.emrTaskInvoker(new EmrTaskInvoker(
new EmrHandler(emrService, mock(EmrStudioService.class),
mock(EmrNotebookExecutionService.class), objectMapper), objectMapper))
mock(EmrNotebookExecutionService.class), mock(EmrAppUiService.class), objectMapper),
objectMapper))
.nanoTime(testNanos::get)
.sleeper(testNanos::addAndGet)
.build();