feat(emr-serverless): answer GetDashboardForJobRun with a link to a page of Floci's about the run (#8687)

GetDashboardForJobRun gives "a URL that you can use to access the
application UIs for a job run", which "is valid for one hour after you
generate it". Floci did not serve it, so a client asking for a run's
application UI link, as Airflow's EMR Serverless operator can, failed.

It now checks the run and its attempt as GetJobRun does and answers
with a link to a page Floci serves at /_aws/emr-serverless/dashboard on
its own address: the attempt's state, its times, and where its
driver's logs go in S3 and CloudWatch Logs, which each attempt now
keeps as it was admitted, so a later change to its application's
monitoring configuration moves neither the logs nor what the page says
of them. EMR Serverless opens the
run's live Spark or Tez UI there, or its Spark History Server once it
has ended, neither of which Floci keeps. The link names the run and is
signed with a key Floci makes as it starts, so a changed link, one past
its hour, or one given before a restart, opens a refusal.

The three private HTML escapes (SES's unsubscribe page, Lambda web's
redirect, CodeArtifact's pypi index) become one, core.common.Html,
which the page uses too.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 21:04:27 -04:00
1 parent ef07bfd4b3
commit 081fd97c05
16 files changed
+618 -25

No files matched your search

@@ -0,0 +1,20 @@
package io.floci.aws.core.common;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HtmlTest {
@Test
void markupInTextOrInAnAttributeQuotedEitherWayIsShownAsItIs() {
assertEquals("<a href="x" title='y'>R&D</a>",
Html.escape("<a href=\"x\" title='y'>R&D</a>"));
}
@Test
void anEntityAlreadyWrittenIsEscapedAgainRatherThanReadAsOne() {
assertEquals("&amp;lt;", Html.escape("&lt;"));
assertEquals("plain text", Html.escape("plain text"));
}
}
@@ -0,0 +1,144 @@
package io.floci.aws.services.emrserverless;
import io.floci.aws.core.common.Html;
import io.floci.aws.testing.EmrServerlessRequests;
import io.floci.aws.testing.PartitionCleanup;
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 org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.UUID;
import static io.floci.aws.testing.EmrServerlessRequests.emrServerless;
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.assertTrue;
/**
* GetDashboardForJobRun through the REST API, and the page its link opens on Floci: the run's attempt, its state and
* where its driver's logs go, as Floci keeps no Spark UI or Spark History Server to open.
*/
@QuarkusTest
class EmrServerlessDashboardIntegrationTest {
private static final String REGION = "eu-west-1";
private static final String ROLE = "arn:aws:iam::000000000000:role/emr-serverless-job";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void aRunsDashboardLinkOpensAPageWithItsStateAndWhereItsDriverLogsGo() {
String applicationId = application();
String jobRunId = run(applicationId, """
, "configurationOverrides": {"monitoringConfiguration": {
"s3MonitoringConfiguration": {"logUri": "s3://bucket/logs/"},
"cloudWatchLoggingConfiguration": {"enabled": true, "logGroupName": "/floci/runs"}}}""");
String url = emrServerless(REGION).when()
.get("/applications/" + applicationId + "/jobruns/" + jobRunId + "/dashboard")
.then().statusCode(200).extract().path("url");
assertTrue(url.contains("/_aws/emr-serverless/dashboard?"), url);
page(url).then().statusCode(200).contentType(containsString("text/html"))
.body(containsString("<h1>Job run " + jobRunId + "</h1>"))
.body(containsString("<tr><th>State</th><td>SUCCESS</td></tr>"))
.body(containsString("<tr><th>Attempt</th><td>1</td></tr>"))
.body(containsString("<tr><th>Driver logs in S3</th><td>s3://bucket/logs/applications/"
+ applicationId + "/jobs/" + jobRunId + "/SPARK_DRIVER/</td></tr>"))
.body(containsString("<tr><th>Driver logs in CloudWatch Logs</th><td>/floci/runs</td></tr>"))
.body(containsString(Html.escape(EmrServerlessDashboardController.NO_UI)));
}
@Test
void aRunsPageShowsWhereItsLogsWentEvenOnceItsApplicationSendsThemElsewhere() {
String applicationId = application();
emrServerless(REGION).body("""
{"clientToken": "%s", "monitoringConfiguration": {
"s3MonitoringConfiguration": {"logUri": "s3://bucket/before/"}}}
""".formatted(UUID.randomUUID()))
.when().patch("/applications/" + applicationId).then().statusCode(200);
String jobRunId = run(applicationId, "");
String url = emrServerless(REGION).when()
.get("/applications/" + applicationId + "/jobruns/" + jobRunId + "/dashboard")
.then().statusCode(200).extract().path("url");
emrServerless(REGION).when().post("/applications/" + applicationId + "/stop").then().statusCode(200);
emrServerless(REGION).body("""
{"clientToken": "%s", "monitoringConfiguration": {
"s3MonitoringConfiguration": {"logUri": "s3://bucket/after/"}}}
""".formatted(UUID.randomUUID()))
.when().patch("/applications/" + applicationId).then().statusCode(200);
page(url).then().statusCode(200)
.body(containsString("<td>s3://bucket/before/applications/" + applicationId + "/jobs/" + jobRunId
+ "/SPARK_DRIVER/</td>"))
.body(not(containsString("s3://bucket/after/")));
}
@Test
void aLinkWhoseSignatureIsChangedOpensARefusal() {
String applicationId = application();
String jobRunId = run(applicationId, "");
String url = emrServerless(REGION).when()
.get("/applications/" + applicationId + "/jobruns/" + jobRunId + "/dashboard")
.then().statusCode(200).extract().path("url");
int signature = url.indexOf("signature=") + "signature=".length();
String changed = url.substring(0, signature) + (url.charAt(signature) == '0' ? '1' : '0')
+ url.substring(signature + 1);
page(changed).then().statusCode(403)
.body(containsString(Html.escape(EmrServerlessDashboardController.INVALID)))
.body(not(containsString(jobRunId + "</td>")));
}
@Test
void aRunOrAttemptThatIsNotThereIsRefusedAsGetJobRunRefusesIt() {
String applicationId = application();
String jobRunId = run(applicationId, "");
String dashboard = "/applications/" + applicationId + "/jobruns/" + jobRunId + "/dashboard";
emrServerless(REGION).when().get("/applications/" + applicationId + "/jobruns/00fmissing/dashboard")
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
emrServerless(REGION).queryParam("attempt", 2).when().get(dashboard)
.then().statusCode(404).body("message", equalTo("Job run " + jobRunId + " has no attempt 2"));
emrServerless(REGION).queryParam("attempt", 0).when().get(dashboard)
.then().statusCode(400).body("__type", equalTo("ValidationException"));
// A run is in its application's Region alone, as GetJobRun finds it.
emrServerless("us-east-1").when().get(dashboard)
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
}
private String application() {
String applicationId = EmrServerlessRequests.createSparkApplication(REGION, "emr-7.5.0");
cleanup.register(() -> EmrServerlessRequests.deleteApplication(REGION, applicationId));
return applicationId;
}
// A run, ended before it is returned, so its application stops when the test is done.
private static String run(String applicationId, String extra) {
String jobRunId = EmrServerlessRequests.startJobRun(REGION, applicationId, """
{"clientToken": "%s", "executionRoleArn": "%s",
"jobDriver": {"sparkSubmit": {"entryPoint": "s3://bucket/jobs/app.py"}}%s}
""".formatted(UUID.randomUUID(), ROLE, extra));
EmrServerlessRequests.awaitJobRun(REGION, applicationId, jobRunId);
return jobRunId;
}
// 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,97 @@
package io.floci.aws.services.emrserverless;
import io.floci.aws.testing.MutableClock;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class EmrServerlessDashboardsTest {
// Where MutableClock starts; each read moves it a millisecond, within the second a link's expiry is counted in.
private static final Instant NOW = Instant.parse("2026-01-01T00:00:00Z");
private static final byte[] KEY = "a key of this process, 32 bytes!".getBytes(StandardCharsets.US_ASCII);
private final MutableClock clock = new MutableClock();
private final EmrServerlessDashboards dashboards = new EmrServerlessDashboards("http://localhost:4566/", clock,
KEY);
@Test
void aLinkNamesTheRunsAttemptOnFlocisAddressAndExpiresInAnHour() {
String url = dashboards.url("123456789012", "eu-west-1", "00fabcd", "00frun", 2);
assertTrue(url.startsWith("http://localhost:4566/_aws/emr-serverless/dashboard?"), url);
Map<String, String> query = query(url);
assertEquals("123456789012", query.get("account"));
assertEquals("eu-west-1", query.get("region"));
assertEquals("00fabcd", query.get("application"));
assertEquals("00frun", query.get("jobRun"));
assertEquals("2", query.get("attempt"));
// "The URL is valid for one hour after you generate it."
assertEquals(String.valueOf(NOW.plus(Duration.ofHours(1)).getEpochSecond()), query.get("expires"));
assertEquals(new EmrServerlessDashboards.Link("123456789012", "eu-west-1", "00fabcd", "00frun", 2,
NOW.plus(Duration.ofHours(1)).getEpochSecond()), verified(query));
}
@Test
void aLinkWhoseHourHasPassedOpensNothing() {
Map<String, String> query = query(dashboards.url("123456789012", "us-east-1", "app", "run", 1));
clock.advance(Duration.ofMinutes(59));
assertNotNull(verified(query));
clock.advance(Duration.ofMinutes(1));
assertNull(verified(query));
}
@Test
void aLinkWithAnyOfItsPartsChangedOpensNothing() {
Map<String, String> query = query(dashboards.url("123456789012", "us-east-1", "app", "run", 1));
for (String part : new String[]{"account", "region", "application", "jobRun", "attempt", "expires"}) {
Map<String, String> changed = new LinkedHashMap<>(query);
changed.put(part, part.equals("attempt") || part.equals("expires")
? String.valueOf(Long.parseLong(query.get(part)) + 1) : query.get(part) + "x");
assertNull(verified(changed), part);
}
Map<String, String> unsigned = new LinkedHashMap<>(query);
unsigned.remove("signature");
assertNull(verified(unsigned));
Map<String, String> malformed = new LinkedHashMap<>(query);
malformed.put("attempt", "one");
assertNull(verified(malformed));
}
@Test
void aLinkFromAnotherProcessesKeyOpensNothing() {
EmrServerlessDashboards restarted = new EmrServerlessDashboards("http://localhost:4566", clock,
"another process's key, 32 bytes".getBytes(StandardCharsets.US_ASCII));
Map<String, String> query = query(restarted.url("123456789012", "us-east-1", "app", "run", 1));
assertNull(verified(query));
}
private EmrServerlessDashboards.Link verified(Map<String, String> query) {
return dashboards.verified(query.get("account"), query.get("region"), query.get("application"),
query.get("jobRun"), query.get("attempt"), query.get("expires"), query.get("signature"));
}
private static Map<String, String> query(String url) {
Map<String, String> query = new LinkedHashMap<>();
for (String pair : URI.create(url).getRawQuery().split("&")) {
int equals = pair.indexOf('=');
query.put(pair.substring(0, equals),
URLDecoder.decode(pair.substring(equals + 1), StandardCharsets.UTF_8));
}
return query;
}
}
@@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.emrserverless.EmrServerlessController;
import io.floci.aws.services.emrserverless.EmrServerlessDashboards;
import io.floci.aws.services.emrserverless.EmrServerlessJobRunService;
import io.floci.aws.services.emrserverless.EmrServerlessService;
import io.floci.aws.services.emrserverless.model.Application;
@@ -77,7 +79,8 @@ class AslExecutorEmrServerlessSyncTest {
.sfnService(instance)
.customResourceLiveness(null)
.emrServerlessTaskInvoker(new EmrServerlessTaskInvoker(
new EmrServerlessController(applications, jobRuns), objectMapper))
new EmrServerlessController(applications, jobRuns, mock(EmrServerlessDashboards.class),
mock(RegionResolver.class)), objectMapper))
.nanoTime(testNanos::get)
.sleeper(testNanos::addAndGet)
.build();