feat(iot): configurable DescribeEndpoint address for published AWS IoT ports (#3202)

* feat(iot): configurable DescribeEndpoint address for published AWS IoT ports

AWS IoT's DescribeEndpoint returns a bare hostname and each client adds its
own port (8883, 443 or 8443). Floci returns host:4566 because its HTTP data
plane lives there and IoT Data clients build https://<endpoint> from it. Both
are right for their callers, and Floci inside a container cannot tell whether
the operator published the AWS ports.

floci.services.iot.endpoint-address (FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS)
is returned as is for every endpoint type when set; unset or blank keeps the
host and port of the base URL. EmulatorConfig.iotEndpointAddress() resolves
it once for DescribeEndpoint and for the AWS-managed domain configurations,
which now follow a changed address instead of keeping the value they were
seeded with. The name joins the generated server certificate's SANs the way
FLOCI_HOSTNAME does, so devices verify it on 8883 and 443.

* docs(iot): DescribeEndpoint address when the AWS IoT ports are published

Describe FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS on the IoT page with the
compose port layout it is meant for, note it on the TLS page next to the
other names the server certificate covers, and list the IoT Core variables
in the environment variable reference.

* fix(tls): accept only host or host:port as the IoT endpoint certificate name

A value with a query or fragment, such as host?x=1, still parsed to a host
and was added to the server certificate. The name now has to be exactly the
authority java.net.URI reads back, so anything beyond host[:port] is skipped
with the same warning as a URL or a path.

* test(iot): pin the address the concurrent seeding test asserts at the end

Every caller had already been joined, so the final describe could not race,
but setting the address explicitly before it makes that plain to read.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-09-07 21:42:38 -05:00
1 parent 65db87d627
commit 53a84c8890
14 files changed
+421 -17

No files matched your search

@@ -0,0 +1,61 @@
package io.github.hectorvent.floci.config;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* {@link EmulatorConfig#iotEndpointAddress()}: the configured address when there is one,
* otherwise the host and port of the effective base URL.
*/
class EmulatorConfigIotEndpointAddressTest {
private static EmulatorConfig config(String effectiveBaseUrl, Optional<String> endpointAddress) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.effectiveBaseUrl()).thenReturn(effectiveBaseUrl);
when(config.services().iot().endpointAddress()).thenReturn(endpointAddress);
when(config.iotEndpointAddress()).thenCallRealMethod();
return config;
}
@Test
void unsetFallsBackToTheHostAndPortOfTheBaseUrl() {
assertEquals("localhost:4566", config("http://localhost:4566", Optional.empty()).iotEndpointAddress());
}
@Test
void fallbackDropsSchemeAndPath() {
assertEquals("floci:4566", config("https://floci:4566/", Optional.empty()).iotEndpointAddress());
}
@Test
void configuredAddressIsReturnedVerbatim() {
assertEquals("iot.example.localhost.floci.io",
config("http://localhost:4566", Optional.of("iot.example.localhost.floci.io")).iotEndpointAddress());
}
@Test
void configuredAddressKeepsAPortTheOperatorAdded() {
assertEquals("iot.example.localhost.floci.io:8443",
config("http://localhost:4566", Optional.of("iot.example.localhost.floci.io:8443")).iotEndpointAddress());
}
@Test
void surroundingWhitespaceIsDropped() {
assertEquals("iot.example.localhost.floci.io",
config("http://localhost:4566", Optional.of(" iot.example.localhost.floci.io\n")).iotEndpointAddress());
}
@ParameterizedTest
@ValueSource(strings = {"", " ", "\t\n"})
void blankAddressCountsAsUnset(String blank) {
assertEquals("localhost:4566", config("http://localhost:4566", Optional.of(blank)).iotEndpointAddress());
}
}
@@ -45,6 +45,7 @@ class TlsConfigSourceCertificateGenerationTest {
System.clearProperty("floci.storage.persistent-path");
System.clearProperty("floci.hostname");
System.clearProperty("floci.base-url");
System.clearProperty("floci.services.iot.endpoint-address");
}
/**
@@ -217,6 +218,36 @@ class TlsConfigSourceCertificateGenerationTest {
"Metadata should include default 'localhost' hostname");
}
/**
* The IoT endpoint address is what devices verify on 8883 and 443, so the boot certificate
* covers it even when it has more labels than the wildcard SAN matches.
*/
@Test
void testCertificateIncludesIotEndpointAddress() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "iot.example.localhost.floci.io:8883");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertTrue(sans.contains("iot.example.localhost.floci.io"), sans.toString());
assertFalse(sans.contains("iot.example.localhost.floci.io:8883"), "the port is not part of the name");
assertTrue(Files.readString(tempDir.resolve("tls/floci-server.metadata.json")).contains("iot.example.localhost.floci.io"),
"the metadata records it as a configured name, so a later change is detected");
}
@Test
void testIotEndpointAddressAddedAfterBootRegeneratesTheCertificate() throws Exception {
new TlsConfigSource();
Path certFile = tempDir.resolve("tls/floci-server.crt");
assertFalse(extractSansFromCertificate(certFile).contains("iot.example.localhost.floci.io"));
System.setProperty("floci.services.iot.endpoint-address", "iot.example.localhost.floci.io");
new TlsConfigSource();
assertTrue(extractSansFromCertificate(certFile).contains("iot.example.localhost.floci.io"),
"a changed endpoint address is a hostname configuration change");
}
// ==================== Helper Methods ====================
/**
@@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.*;
* Tests the extractCustomHostnames() method to verify:
* - Extraction from FLOCI_HOSTNAME
* - Extraction from FLOCI_BASE_URL (DNS names and IP addresses)
* - Extraction from FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS (the port, when given, is dropped)
* - Filtering of default values (localhost, 127.0.0.1, 0.0.0.0)
* - Deduplication of hostnames
* - Edge case handling (malformed URLs, missing host, IPv6 addresses)
@@ -24,6 +25,7 @@ class TlsConfigSourceHostnameExtractionTest {
void cleanupSystemProperties() {
System.clearProperty("floci.hostname");
System.clearProperty("floci.base-url");
System.clearProperty("floci.services.iot.endpoint-address");
}
/**
@@ -243,6 +245,79 @@ class TlsConfigSourceHostnameExtractionTest {
"Should extract IPv6 address '[::1]' from FLOCI_BASE_URL");
}
/**
* The IoT endpoint address is a name devices verify on 8883 and 443, so it joins the SANs.
*/
@Test
void testExtractFromIotEndpointAddress() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "iot.example.localhost.floci.io");
List<String> hostnames = invokeExtractCustomHostnames();
assertEquals(List.of("iot.example.localhost.floci.io"), hostnames);
}
@Test
void testIotEndpointAddressPortIsDropped() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "iot.example.localhost.floci.io:8443");
List<String> hostnames = invokeExtractCustomHostnames();
assertEquals(List.of("iot.example.localhost.floci.io"), hostnames);
}
@Test
void testIotEndpointAddressKeepsAnIpv6Literal() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "[fd00::10]:8883");
List<String> hostnames = invokeExtractCustomHostnames();
assertEquals(List.of("[fd00::10]"), hostnames);
}
@Test
void testIotEndpointAddressDefaultHostIsFiltered() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "localhost:4566");
List<String> hostnames = invokeExtractCustomHostnames();
assertTrue(hostnames.isEmpty(), "localhost is covered by the default SANs");
}
@Test
void testIotEndpointAddressIsDeduplicatedAgainstHostname() throws Exception {
System.setProperty("floci.hostname", "floci");
System.setProperty("floci.services.iot.endpoint-address", "floci:8883");
List<String> hostnames = invokeExtractCustomHostnames();
assertEquals(List.of("floci"), hostnames);
}
@Test
void testUnparsableIotEndpointAddressIsSkipped() throws Exception {
System.setProperty("floci.services.iot.endpoint-address", "not a host");
List<String> hostnames = invokeExtractCustomHostnames();
assertTrue(hostnames.isEmpty(), "an address that is not a host is logged and skipped, not fatal");
}
/**
* A URL instead of a host is an operator typo: java.net.URI would read "https" as the host,
* so the value is skipped rather than adding a wrong name to the certificate.
*/
@Test
void testIotEndpointAddressWithASchemeOrPathIsSkipped() throws Exception {
for (String typo : List.of("https://iot.example.localhost.floci.io", "iot.example.localhost.floci.io:8443/",
"user@iot.example.localhost.floci.io", "iot.example.localhost.floci.io:abc",
"iot.example.localhost.floci.io?x=1", "iot.example.localhost.floci.io#fragment")) {
System.setProperty("floci.services.iot.endpoint-address", typo);
assertTrue(invokeExtractCustomHostnames().isEmpty(), typo);
}
}
// ==================== Helper Methods ====================
/**
@@ -0,0 +1,78 @@
package io.github.hectorvent.floci.services.iot;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
/**
* With {@code floci.services.iot.endpoint-address} set, DescribeEndpoint answers with that value
* for every endpoint type, the way AWS returns a bare hostname that devices complete with their
* own port. {@link IotIntegrationTest} and {@link IotEndpointHostnameIntegrationTest} keep
* guarding the unset case, the host and port of the base URL.
*/
@QuarkusTest
@TestProfile(IotDescribeEndpointOverrideIntegrationTest.Profile.class)
class IotDescribeEndpointOverrideIntegrationTest {
static final String ENDPOINT_ADDRESS = "iot.example.localhost.floci.io";
@Test
void omittedEndpointTypeReturnsTheConfiguredAddress() {
given()
.when()
.get("/endpoint")
.then()
.statusCode(200)
.body("endpointAddress", equalTo(ENDPOINT_ADDRESS));
}
@ParameterizedTest
@ValueSource(strings = {"iot:Data-ATS", "iot:Data", "iot:Jobs", "iot:CredentialProvider"})
void everyEndpointTypeReturnsTheConfiguredAddressVerbatim(String endpointType) {
given()
.queryParam("endpointType", endpointType)
.when()
.get("/endpoint")
.then()
.statusCode(200)
.body("endpointAddress", equalTo(ENDPOINT_ADDRESS));
}
@Test
void unsupportedEndpointTypeIsStillRejected() {
given()
.queryParam("endpointType", "iot:Nonsense")
.when()
.get("/endpoint")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
@ParameterizedTest
@ValueSource(strings = {"iot:Data-ATS", "iot:Data", "iot:Jobs", "iot:CredentialProvider"})
void awsManagedDomainConfigurationsReportTheConfiguredAddress(String name) {
given()
.when()
.get("/domainConfigurations/" + name)
.then()
.statusCode(200)
.body("domainType", equalTo("AWS_MANAGED"))
.body("domainName", equalTo(ENDPOINT_ADDRESS));
}
public static final class Profile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.iot.endpoint-address", ENDPOINT_ADDRESS);
}
}
}
@@ -13,13 +13,16 @@ import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -39,13 +42,15 @@ class IotDomainConfigurationServiceTest {
"arn:aws:acm:us-east-1:000000000000:certificate/22222222-2222-2222-2222-222222222222";
private final ObjectMapper mapper = new ObjectMapper();
/** What DescribeEndpoint answers; tests change it to model a restart with another address. */
private final AtomicReference<String> endpointAddress = new AtomicReference<>("localhost:4566");
private final IotDomainConfigurationService service = new IotDomainConfigurationService(
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), endpointConfig(),
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), endpointConfig(endpointAddress),
mock(TlsCertificateManager.class));
private static EmulatorConfig endpointConfig() {
private static EmulatorConfig endpointConfig(AtomicReference<String> endpointAddress) {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
when(config.iotEndpointAddress()).thenAnswer(invocation -> endpointAddress.get());
return config;
}
@@ -203,6 +208,45 @@ class IotDomainConfigurationServiceTest {
.startsWith("arn:aws:iot:eu-west-1:"));
}
@Test
void awsManagedConfigurationsCarryTheDescribeEndpointAddress() {
endpointAddress.set("iot.example.localhost.floci.io");
for (String name : List.of("iot:Data-ATS", "iot:Data", "iot:CredentialProvider", "iot:Jobs")) {
assertEquals("iot.example.localhost.floci.io",
service.describeDomainConfiguration(name, REGION).getDomainName(), name);
}
}
@Test
void awsManagedDomainNameFollowsTheAddressAfterSeeding() {
IotDomainConfiguration seeded = service.describeDomainConfiguration("iot:Data-ATS", REGION);
String seededArn = seeded.getDomainConfigurationArn();
assertEquals("localhost:4566", seeded.getDomainName());
service.updateDomainConfiguration("iot:Data-ATS",
mapper.createObjectNode().put("domainConfigurationStatus", "DISABLED"), REGION);
Instant statusChanged = service.describeDomainConfiguration("iot:Data-ATS", REGION).getLastStatusChangeDate();
service.tagResource(seededArn, Map.of("team", "devices"));
service.createDomainConfiguration("iot-domain", customDomainRequest(), REGION);
endpointAddress.set("iot.example.localhost.floci.io");
IotDomainConfiguration refreshed = service.describeDomainConfiguration("iot:Data-ATS", REGION);
assertEquals("iot.example.localhost.floci.io", refreshed.getDomainName());
assertEquals(seededArn, refreshed.getDomainConfigurationArn(), "refreshed in place, not seeded again");
assertEquals("DISABLED", refreshed.getDomainConfigurationStatus(), "an earlier update is kept");
assertEquals(statusChanged, refreshed.getLastStatusChangeDate(), "a new address is not a status change");
assertEquals(Map.of("team", "devices"), service.listTagsForResource(seededArn), "tags survive the refresh");
assertEquals("DATA", refreshed.getServiceType());
List<IotDomainConfiguration> listed = service.listDomainConfigurations(REGION, null, null, null).items();
assertEquals(List.of("iot.example.localhost.floci.io", "iot.example.localhost.floci.io",
"iot.example.localhost.floci.io", "iot.example.localhost.floci.io"),
listed.stream().filter(item -> "AWS_MANAGED".equals(item.getDomainType()))
.map(IotDomainConfiguration::getDomainName).toList(),
"every AWS-managed configuration follows");
assertEquals("iot.example.com", service.describeDomainConfiguration("iot-domain", REGION).getDomainName(),
"a customer-managed domain keeps its own name");
}
@Test
void awsManagedConfigurationsCanBeUpdatedAndTaggedButNotDeleted() {
IotDomainConfiguration legacy = service.updateDomainConfiguration("iot:Data",
@@ -276,6 +320,38 @@ class IotDomainConfigurationServiceTest {
}
}
@Test
void concurrentFirstDescribesSeedOneManagedConfigurationPerRegion() throws Exception {
int callers = 16;
CountDownLatch start = new CountDownLatch(1);
ExecutorService pool = Executors.newFixedThreadPool(callers);
try {
List<Future<IotDomainConfiguration>> results = new ArrayList<>();
for (int i = 0; i < callers; i++) {
String address = i % 2 == 0 ? "localhost:4566" : "iot.example.localhost.floci.io";
results.add(pool.submit(() -> {
start.await();
endpointAddress.set(address);
return service.describeDomainConfiguration("iot:Jobs", "eu-north-1");
}));
}
start.countDown();
Set<String> arns = new HashSet<>();
for (Future<IotDomainConfiguration> result : results) {
IotDomainConfiguration seen = result.get();
assertNotNull(seen.getDomainName());
arns.add(seen.getDomainConfigurationArn());
}
assertEquals(1, arns.size(), "seeded once, whichever address the callers raced with: " + arns);
endpointAddress.set("iot.example.localhost.floci.io");
assertEquals("iot.example.localhost.floci.io",
service.describeDomainConfiguration("iot:Jobs", "eu-north-1").getDomainName(),
"the next describe brings the seeded record to the current address");
} finally {
pool.shutdownNow();
}
}
@Test
void createRejectsADuplicateName() {
service.createDomainConfiguration("iot-domain", customDomainRequest(), REGION);
@@ -42,7 +42,7 @@ class IotDomainConfigurationTlsServiceTest {
private static EmulatorConfig endpointConfig() {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
when(config.iotEndpointAddress()).thenReturn("localhost:4566");
return config;
}