mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
65db87d627
commit
53a84c8890
14 files changed
+421
-17
No files matched your search
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.smallrye.config.ConfigMapping;
|
||||
import io.smallrye.config.WithDefault;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
@@ -42,6 +43,18 @@ public interface EmulatorConfig {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* The address IoT Core's DescribeEndpoint returns for every endpoint type, also the domain
|
||||
* name of the AWS-managed domain configurations: {@code floci.services.iot.endpoint-address}
|
||||
* when set, otherwise the host and port of {@link #effectiveBaseUrl()}.
|
||||
*/
|
||||
default String iotEndpointAddress() {
|
||||
return services().iot().endpointAddress()
|
||||
.map(String::strip)
|
||||
.filter(address -> !address.isEmpty())
|
||||
.orElseGet(() -> URI.create(effectiveBaseUrl()).getAuthority());
|
||||
}
|
||||
|
||||
@WithDefault("us-east-1")
|
||||
String defaultRegion();
|
||||
|
||||
@@ -797,6 +810,15 @@ public interface EmulatorConfig {
|
||||
@WithDefault("false")
|
||||
boolean ruleSqlStrict();
|
||||
|
||||
/**
|
||||
* Returned as is by DescribeEndpoint for every endpoint type when set. AWS returns a bare
|
||||
* hostname and clients add their own port: 8883 for MQTT, 443 for HTTPS and MQTT over
|
||||
* WebSocket, 8443 for HTTPS with a client certificate. Set it when those ports reach Floci
|
||||
* (8883 to the MQTT TLS listener, 443 and 8443 to the HTTPS listener). Unset, DescribeEndpoint
|
||||
* returns the host and port of the base URL. Env: FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS
|
||||
*/
|
||||
Optional<String> endpointAddress();
|
||||
|
||||
MqttConfig mqtt();
|
||||
}
|
||||
|
||||
|
||||
@@ -298,7 +298,8 @@ public class TlsConfigSource implements ConfigSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts custom hostnames from FLOCI_HOSTNAME and FLOCI_BASE_URL configuration.
|
||||
* Extracts custom hostnames from FLOCI_HOSTNAME, FLOCI_BASE_URL and
|
||||
* FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS configuration.
|
||||
* Filters out default values like "localhost" and "127.0.0.1".
|
||||
* Returns a deduplicated list of custom hostnames.
|
||||
*
|
||||
@@ -327,6 +328,26 @@ public class TlsConfigSource implements ConfigSource {
|
||||
LOG.warnv("TLS: failed to parse base URL for hostname extraction: {0}", baseUrl);
|
||||
}
|
||||
|
||||
// Extract from FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS: devices verify that name on 8883 and 443
|
||||
String iotEndpoint = resolveProperty("floci.services.iot.endpoint-address", "").strip();
|
||||
if (!iotEndpoint.isEmpty()) {
|
||||
try {
|
||||
// Anything beyond host[:port] is a typo: java.net.URI would read "https" as the host of a URL.
|
||||
URI uri = new URI("//" + iotEndpoint);
|
||||
String host = uri.getHost();
|
||||
if (host == null || uri.getUserInfo() != null || !iotEndpoint.equals(uri.getRawAuthority())) {
|
||||
LOG.warnv("TLS: floci.services.iot.endpoint-address is not a host or host:port, not added to the certificate: {0}",
|
||||
iotEndpoint);
|
||||
} else if (!isDefaultHostname(host)) {
|
||||
hostnames.add(host);
|
||||
LOG.debugv("TLS: extracted hostname from floci.services.iot.endpoint-address: {0}", host);
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
LOG.warnv("TLS: failed to parse floci.services.iot.endpoint-address for hostname extraction: {0}",
|
||||
iotEndpoint);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> result = new ArrayList<>(hostnames);
|
||||
if (!result.isEmpty()) {
|
||||
LOG.infov("TLS: detected custom hostnames: {0}", result);
|
||||
|
||||
+12
-6
@@ -19,7 +19,6 @@ import io.github.hectorvent.floci.services.iot.model.IotDomainConfiguration.TlsC
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
@@ -278,24 +277,31 @@ public class IotDomainConfigurationService {
|
||||
/**
|
||||
* The default-endpoint configurations AWS creates for every account: AWS_MANAGED, ENABLED, no
|
||||
* server certificate, and the address DescribeEndpoint returns as their domain name. They can
|
||||
* be updated and tagged like any other, but not deleted.
|
||||
* be updated and tagged like any other, but not deleted. A stored one whose domain name no
|
||||
* longer matches that address (the emulator restarted with another base URL or
|
||||
* {@code floci.services.iot.endpoint-address}) takes the current one and keeps everything else.
|
||||
*/
|
||||
private void seedAwsManaged(String region) {
|
||||
String domainName = config.iotEndpointAddress();
|
||||
synchronized (lock) {
|
||||
AWS_MANAGED.forEach((name, serviceType) -> {
|
||||
String key = key(region, name);
|
||||
if (store.get(key).isEmpty()) {
|
||||
store.put(key, awsManaged(name, serviceType, region));
|
||||
IotDomainConfiguration existing = store.get(key).orElse(null);
|
||||
if (existing == null) {
|
||||
store.put(key, awsManaged(name, serviceType, domainName, region));
|
||||
} else if (!domainName.equals(existing.getDomainName())) {
|
||||
existing.setDomainName(domainName);
|
||||
store.put(key, existing);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private IotDomainConfiguration awsManaged(String name, String serviceType, String region) {
|
||||
private IotDomainConfiguration awsManaged(String name, String serviceType, String domainName, String region) {
|
||||
IotDomainConfiguration configuration = new IotDomainConfiguration();
|
||||
configuration.setDomainConfigurationName(name);
|
||||
configuration.setDomainConfigurationArn(arn(name, region));
|
||||
configuration.setDomainName(URI.create(config.effectiveBaseUrl()).getAuthority());
|
||||
configuration.setDomainName(domainName);
|
||||
configuration.setServiceType(serviceType);
|
||||
configuration.setDomainConfigurationStatus("ENABLED");
|
||||
configuration.setDomainType("AWS_MANAGED");
|
||||
|
||||
@@ -47,7 +47,6 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -224,8 +223,7 @@ public class IotService {
|
||||
throw new AwsException("InvalidRequestException", "Unsupported endpoint type: " + effectiveType, 400);
|
||||
}
|
||||
startMqttIfEnabled();
|
||||
URI baseUri = URI.create(config.effectiveBaseUrl());
|
||||
return baseUri.getAuthority();
|
||||
return config.iotEndpointAddress();
|
||||
}
|
||||
|
||||
public Thing createThing(String thingName, Map<String, String> attributes, String region) {
|
||||
|
||||
Reference in new issue
Block a user