mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(iot): verify device certificates on the MQTT TLS listener (#3180)
* feat(iot): registered-certificate lookup and iot:Connect evaluation * feat(iot): verify device certificates and iot:Connect on the MQTT TLS listener * docs(iot): device verification on the MQTT TLS listener * test(iot): device evaluation over plain stores and under racing policy and thing changes * fix(iot): remember where a device certificate was found and log refusals at debug * fix(iot): treat any address literal in SNI as no domain name
This commit is contained in:
1 parent
a1d0597ffe
commit
19a85360e1
10 files changed
+1729
-67
No files matched your search
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iot;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage;
|
||||
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.quarkus.runtime.ShutdownEvent;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
@@ -27,9 +28,14 @@ import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import javax.net.ssl.ExtendedSSLSession;
|
||||
import javax.net.ssl.SNIHostName;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.X509ExtendedTrustManager;
|
||||
import java.net.Socket;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
@@ -40,6 +46,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class IotMqttBrokerService {
|
||||
@@ -47,10 +54,11 @@ public class IotMqttBrokerService {
|
||||
private static final Logger LOG = Logger.getLogger(IotMqttBrokerService.class);
|
||||
|
||||
/**
|
||||
* The TLS listener asks for a client certificate and accepts whichever one is presented: AWS
|
||||
* IoT trusts a device certificate because it is registered, not because of its issuer, and
|
||||
* that lookup belongs to the broker, not to the handshake. Until it exists, 8883 admits every
|
||||
* client exactly as 1883 does.
|
||||
* The TLS listener asks for a client certificate and lets the handshake complete with whichever
|
||||
* one is presented: AWS IoT trusts a device certificate because it is registered, not because
|
||||
* of its issuer, and that lookup happens on the CONNECT in {@link #handleEndpoint}, where a
|
||||
* certificate that is missing, unregistered, inactive or not allowed to connect is answered
|
||||
* with CONNACK not authorized.
|
||||
*/
|
||||
static final X509ExtendedTrustManager ACCEPT_ANY_CLIENT_CERTIFICATE = new X509ExtendedTrustManager() {
|
||||
@Override
|
||||
@@ -86,6 +94,8 @@ public class IotMqttBrokerService {
|
||||
}
|
||||
};
|
||||
|
||||
private static final Pattern IPV4_LITERAL = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3}");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final Vertx vertx;
|
||||
private final Instance<IotService> iotService;
|
||||
@@ -131,7 +141,7 @@ public class IotMqttBrokerService {
|
||||
|
||||
MqttServer mqttServer = listen(new MqttServerOptions()
|
||||
.setHost(config.services().iot().mqtt().host())
|
||||
.setPort(config.services().iot().mqtt().port()), "IoT MQTT broker");
|
||||
.setPort(config.services().iot().mqtt().port()), "IoT MQTT broker", false);
|
||||
try {
|
||||
startTlsListener();
|
||||
} catch (RuntimeException e) {
|
||||
@@ -142,9 +152,10 @@ public class IotMqttBrokerService {
|
||||
}
|
||||
|
||||
/**
|
||||
* The MQTT over TLS listener, AWS IoT's 8883, sharing every handler with the plaintext one.
|
||||
* Its key material is the TLS registry's default configuration, the same certificate the HTTPS
|
||||
* endpoint presents. Not opened when the port is 0 or TLS is off.
|
||||
* The MQTT over TLS listener, AWS IoT's 8883, sharing every handler with the plaintext one but
|
||||
* admitting only registered devices whose policies allow the connect. Its key material is the
|
||||
* TLS registry's default configuration, the same certificate the HTTPS endpoint presents. Not
|
||||
* opened when the port is 0 or TLS is off.
|
||||
*/
|
||||
private void startTlsListener() {
|
||||
int tlsPort = config.services().iot().mqtt().tlsPort();
|
||||
@@ -168,13 +179,13 @@ public class IotMqttBrokerService {
|
||||
.setSsl(true)
|
||||
.setKeyCertOptions(KeyCertOptions.wrap(manager))
|
||||
.setTrustOptions(TrustOptions.wrap(ACCEPT_ANY_CLIENT_CERTIFICATE))
|
||||
.setClientAuth(ClientAuth.REQUEST), "IoT MQTT TLS broker");
|
||||
.setClientAuth(ClientAuth.REQUEST), "IoT MQTT TLS broker", true);
|
||||
keyManager = manager;
|
||||
}
|
||||
|
||||
private MqttServer listen(MqttServerOptions options, String name) {
|
||||
private MqttServer listen(MqttServerOptions options, String name, boolean verifyDevice) {
|
||||
MqttServer mqttServer = MqttServer.create(vertx, options);
|
||||
mqttServer.endpointHandler(this::handleEndpoint);
|
||||
mqttServer.endpointHandler(endpoint -> handleEndpoint(endpoint, verifyDevice));
|
||||
mqttServer.exceptionHandler(error -> LOG.warnv("{0} error: {1}", name, error.getMessage()));
|
||||
try {
|
||||
mqttServer.listen().toCompletionStage().toCompletableFuture().join();
|
||||
@@ -266,13 +277,32 @@ public class IotMqttBrokerService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void handleEndpoint(MqttEndpoint endpoint) {
|
||||
/**
|
||||
* On the TLS listener the CONNECT is admitted only for a presented certificate that IoT Core
|
||||
* has registered and whose attached policies allow {@code iot:Connect} for the client id;
|
||||
* anything else is answered with CONNACK not authorized (return code 5, reason code 0x87 on
|
||||
* MQTT 5) and never becomes a session, so a client already holding that client id keeps its
|
||||
* connection. The plaintext listener, which the WebSocket bridge also lands on, stays open to
|
||||
* every client.
|
||||
*/
|
||||
private void handleEndpoint(MqttEndpoint endpoint, boolean verifyDevice) {
|
||||
String clientId = endpoint.clientIdentifier();
|
||||
SocketAddress remoteAddress = endpoint.remoteAddress();
|
||||
String sourceIp = remoteAddress == null ? null : remoteAddress.host();
|
||||
String principal = null;
|
||||
if (verifyDevice) {
|
||||
principal = admittedDevice(endpoint, clientId, sourceIp);
|
||||
if (principal == null) {
|
||||
endpoint.reject(endpoint.protocolVersion() == 5
|
||||
? MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED_5
|
||||
: MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
ClientSession session = new ClientSession(
|
||||
clientId,
|
||||
endpoint,
|
||||
remoteAddress == null ? null : remoteAddress.host(),
|
||||
sourceIp,
|
||||
remoteAddress == null ? -1 : remoteAddress.port(),
|
||||
endpoint.isCleanSession());
|
||||
|
||||
@@ -291,6 +321,75 @@ public class IotMqttBrokerService {
|
||||
}
|
||||
|
||||
endpoint.accept();
|
||||
if (principal != null) {
|
||||
LOG.debugv("IoT MQTT TLS client {0} admitted as {1}", clientId, principal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The certificate ARN of the device behind the connection when it may connect, otherwise null.
|
||||
* A failure while deciding refuses the client rather than leaving the CONNECT unanswered. A
|
||||
* refusal is the client's own doing and reaches it as the CONNACK, so it is logged at debug
|
||||
* only: the CONNECT is reachable by anyone.
|
||||
*/
|
||||
private String admittedDevice(MqttEndpoint endpoint, String clientId, String sourceIp) {
|
||||
try {
|
||||
Optional<IotService.RegisteredDevice> device = presentedDevice(endpoint);
|
||||
if (device.isEmpty()) {
|
||||
LOG.debugv("IoT MQTT TLS client {0} refused: no registered certificate presented", clientId);
|
||||
return null;
|
||||
}
|
||||
String certificateArn = device.get().certificate().getCertificateArn();
|
||||
if (!iotService.get().isConnectAllowed(device.get(), clientId, sourceIp, requestedServerName(endpoint))) {
|
||||
LOG.debugv("IoT MQTT TLS client {0} refused: iot:Connect is not allowed for {1}", clientId, certificateArn);
|
||||
return null;
|
||||
}
|
||||
return certificateArn;
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "IoT MQTT TLS client {0} refused: device verification failed: {1}", clientId, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The registered certificate behind the peer's leaf, if the client presented one at all. */
|
||||
private Optional<IotService.RegisteredDevice> presentedDevice(MqttEndpoint endpoint) {
|
||||
SSLSession session = endpoint.isSsl() ? endpoint.sslSession() : null;
|
||||
if (session == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Certificate[] chain;
|
||||
try {
|
||||
chain = session.getPeerCertificates();
|
||||
} catch (SSLPeerUnverifiedException e) {
|
||||
LOG.debugv("IoT MQTT TLS client {0} presented no certificate", endpoint.clientIdentifier());
|
||||
return Optional.empty();
|
||||
}
|
||||
if (chain.length == 0 || !(chain[0] instanceof X509Certificate leaf)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return iotService.get().findRegisteredCertificate(leaf);
|
||||
}
|
||||
|
||||
/**
|
||||
* The server name the client sent in the handshake, which AWS IoT exposes as
|
||||
* {@code iot:DomainName}. An address literal is not a domain name: some clients send the
|
||||
* host they dialled as SNI even when it is an IP.
|
||||
*/
|
||||
private static String requestedServerName(MqttEndpoint endpoint) {
|
||||
if (!(endpoint.sslSession() instanceof ExtendedSSLSession session)) {
|
||||
return null;
|
||||
}
|
||||
return session.getRequestedServerNames().stream()
|
||||
.filter(SNIHostName.class::isInstance)
|
||||
.map(name -> ((SNIHostName) name).getAsciiName())
|
||||
.filter(name -> !isAddressLiteral(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/** An IPv4 dotted quad, or anything carrying a colon, which only an IPv6 literal does. */
|
||||
static boolean isAddressLiteral(String name) {
|
||||
return name.indexOf(':') >= 0 || IPV4_LITERAL.matcher(name).matches();
|
||||
}
|
||||
|
||||
private void handleSubscribe(ClientSession session, MqttSubscribeMessage message) {
|
||||
|
||||
@@ -6,13 +6,17 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotCertificate;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotJob;
|
||||
@@ -46,6 +50,8 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateEncodingException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
@@ -55,13 +61,17 @@ import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -74,11 +84,16 @@ public class IotService {
|
||||
private static final Pattern THING_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9:_-]{1,128}");
|
||||
/** The {@code FirehoseSeparator} shape of the IoT API model. */
|
||||
private static final Pattern FIREHOSE_SEPARATOR = Pattern.compile("([\\n\\t])|(\\r\\n)|(,)");
|
||||
/** An AWS policy variable such as {@code ${iot:ClientId}}. */
|
||||
private static final Pattern POLICY_VARIABLE = Pattern.compile("\\$\\{[^}]*\\}");
|
||||
public static final int MAX_POLICY_VERSIONS = 5;
|
||||
|
||||
/** One lock for every policy version write and the policy delete, so a cap check, an append and a delete cannot interleave. */
|
||||
private final Object policyWriteLock = new Object();
|
||||
|
||||
/** certificateId to the partition and key it was last found under; see {@link #findRegisteredCertificate}. */
|
||||
private final Map<String, CertificateLocation> certificateLocations = new ConcurrentHashMap<>();
|
||||
|
||||
private final StorageBackend<String, Thing> thingStore;
|
||||
private final StorageBackend<String, IotCertificate> certificateStore;
|
||||
private final StorageBackend<String, IotPolicy> policyStore;
|
||||
@@ -106,6 +121,7 @@ public class IotService {
|
||||
private final FirehoseService firehoseService;
|
||||
private final CloudWatchLogsService cloudWatchLogsService;
|
||||
private final FlociCertificateAuthority certificateAuthority;
|
||||
private final IamPolicyEvaluator policyEvaluator;
|
||||
private final RuleSqlEvaluator ruleSqlEvaluator;
|
||||
|
||||
@Inject
|
||||
@@ -123,7 +139,8 @@ public class IotService {
|
||||
LambdaService lambdaService,
|
||||
FirehoseService firehoseService,
|
||||
CloudWatchLogsService cloudWatchLogsService,
|
||||
FlociCertificateAuthority certificateAuthority) {
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
IamPolicyEvaluator policyEvaluator) {
|
||||
this(storageFactory.create("iot", "iot-things.json", new TypeReference<Map<String, Thing>>() {}),
|
||||
storageFactory.create("iot", "iot-certificates.json", new TypeReference<Map<String, IotCertificate>>() {}),
|
||||
storageFactory.create("iot", "iot-policies.json", new TypeReference<Map<String, IotPolicy>>() {}),
|
||||
@@ -138,7 +155,8 @@ public class IotService {
|
||||
storageFactory.create("iot", "iot-thing-groups.json", new TypeReference<Map<String, IotThingGroup>>() {}),
|
||||
storageFactory.create("iot", "iot-thing-group-memberships.json", new TypeReference<Map<String, Set<String>>>() {}),
|
||||
config, regionResolver, objectMapper, publishEventRecorder, mqttBrokerService, sqsService, snsService,
|
||||
s3Service, kinesisService, dynamoDbService, lambdaService, firehoseService, cloudWatchLogsService, certificateAuthority);
|
||||
s3Service, kinesisService, dynamoDbService, lambdaService, firehoseService, cloudWatchLogsService, certificateAuthority,
|
||||
policyEvaluator);
|
||||
}
|
||||
|
||||
IotService(StorageBackend<String, Thing> thingStore,
|
||||
@@ -167,7 +185,8 @@ public class IotService {
|
||||
LambdaService lambdaService,
|
||||
FirehoseService firehoseService,
|
||||
CloudWatchLogsService cloudWatchLogsService,
|
||||
FlociCertificateAuthority certificateAuthority) {
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
IamPolicyEvaluator policyEvaluator) {
|
||||
this.thingStore = thingStore;
|
||||
this.certificateStore = certificateStore;
|
||||
this.policyStore = policyStore;
|
||||
@@ -195,6 +214,7 @@ public class IotService {
|
||||
this.firehoseService = firehoseService;
|
||||
this.cloudWatchLogsService = cloudWatchLogsService;
|
||||
this.certificateAuthority = certificateAuthority;
|
||||
this.policyEvaluator = policyEvaluator;
|
||||
this.ruleSqlEvaluator = new RuleSqlEvaluator(objectMapper);
|
||||
}
|
||||
|
||||
@@ -370,16 +390,20 @@ public class IotService {
|
||||
}
|
||||
}
|
||||
|
||||
/** AWS IoT's certificateId is the lowercase hex SHA-256 of the certificate's DER encoding. */
|
||||
private static void fill(IotCertificate certificate, X509Certificate x509) throws Exception {
|
||||
CertificateGenerator pem = new CertificateGenerator();
|
||||
certificate.setCertificateId(HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(x509.getEncoded())));
|
||||
certificate.setCertificateId(certificateIdOf(x509));
|
||||
certificate.setCertificatePem(pem.toPem(x509));
|
||||
certificate.setPublicKey(pem.toPem(x509.getPublicKey()));
|
||||
certificate.setNotBefore(x509.getNotBefore().toInstant());
|
||||
certificate.setNotAfter(x509.getNotAfter().toInstant());
|
||||
}
|
||||
|
||||
/** AWS IoT's certificateId is the lowercase hex SHA-256 of the certificate's DER encoding. */
|
||||
private static String certificateIdOf(X509Certificate x509) throws CertificateEncodingException, NoSuchAlgorithmException {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(x509.getEncoded()));
|
||||
}
|
||||
|
||||
public IotCertificate describeCertificate(String certificateId, String region) {
|
||||
return certificateStore.get(certificateKey(region, certificateId))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Certificate not found: " + certificateId, 404));
|
||||
@@ -408,6 +432,205 @@ public class IotService {
|
||||
throw new AwsException("InvalidRequestException", "Cannot delete attached certificate", 400);
|
||||
}
|
||||
certificateStore.delete(certificateKey(region, certificateId));
|
||||
certificateLocations.remove(certificateId);
|
||||
}
|
||||
|
||||
/** A device certificate the registry holds, with the account partition and region it was registered in. */
|
||||
public record RegisteredDevice(String accountId, String region, IotCertificate certificate) {
|
||||
}
|
||||
|
||||
private record CertificateLocation(String accountId, String key) {
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS IoT trusts a device certificate because it is registered, not because of who signed it.
|
||||
* The lookup key is the certificateId, which is the SHA-256 of the DER encoding, searched
|
||||
* across every account partition: a connecting device carries no request context. The
|
||||
* partition and key a certificate was found under are remembered, so a device's next connect
|
||||
* is one keyed read; a remembered location that no longer resolves is dropped and the scan
|
||||
* runs again.
|
||||
*/
|
||||
public Optional<RegisteredDevice> findRegisteredCertificate(X509Certificate presented) {
|
||||
String certificateId;
|
||||
try {
|
||||
certificateId = certificateIdOf(presented);
|
||||
} catch (CertificateEncodingException | NoSuchAlgorithmException e) {
|
||||
LOG.debugv("Could not fingerprint the presented device certificate: {0}", e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
CertificateLocation known = certificateLocations.get(certificateId);
|
||||
if (known != null) {
|
||||
Optional<IotCertificate> current = getForAccount(certificateStore, known.accountId(), known.key());
|
||||
if (current.isPresent()) {
|
||||
return Optional.of(new RegisteredDevice(known.accountId(), regionOfCertificateKey(known.key()), current.get()));
|
||||
}
|
||||
certificateLocations.remove(certificateId, known);
|
||||
}
|
||||
String suffix = ":" + certificateId;
|
||||
Optional<AccountAwareStorageBackend.AccountEntry<IotCertificate>> found =
|
||||
registeredCertificates(key -> key.startsWith("cert:") && key.endsWith(suffix)).stream()
|
||||
.sorted(Comparator.comparing((AccountAwareStorageBackend.AccountEntry<IotCertificate> entry) -> entry.accountId())
|
||||
.thenComparing(AccountAwareStorageBackend.AccountEntry::key))
|
||||
.findFirst();
|
||||
found.ifPresent(entry -> certificateLocations.put(certificateId, new CertificateLocation(entry.accountId(), entry.key())));
|
||||
return found.map(entry -> new RegisteredDevice(entry.accountId(), regionOfCertificateKey(entry.key()), entry.value()));
|
||||
}
|
||||
|
||||
private static String regionOfCertificateKey(String key) {
|
||||
return key.split(":", 3)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides {@code iot:Connect} for a client id the way AWS IoT does on a CONNECT: the certificate
|
||||
* must be ACTIVE and within its validity dates, the policies are the default versions attached
|
||||
* to the certificate in its own account and region, and the connection's policy variables are
|
||||
* substituted and offered as condition keys. No policy means deny; an explicit deny wins.
|
||||
*/
|
||||
public boolean isConnectAllowed(RegisteredDevice device, String clientId, String sourceIp, String domainName) {
|
||||
IotCertificate certificate = device.certificate();
|
||||
if (clientId == null || clientId.isBlank() || !"ACTIVE".equals(certificate.getStatus()) || !isWithinValidity(certificate)) {
|
||||
return false;
|
||||
}
|
||||
List<String> documents = attachedPolicyDocuments(certificate.getCertificateArn(), device.region(), device.accountId());
|
||||
if (documents.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Map<String, String> variables = connectionVariables(device, clientId, sourceIp, domainName);
|
||||
List<String> resolved = documents.stream().map(document -> resolvePolicyVariables(document, variables)).toList();
|
||||
Map<String, List<String>> conditionContext = new LinkedHashMap<>();
|
||||
variables.forEach((key, value) -> conditionContext.put(key, List.of(value)));
|
||||
String clientArn = AwsArnUtils.Arn.of("iot", device.region(), device.accountId(), "client/" + clientId).toString();
|
||||
return policyEvaluator.evaluate(CallerContext.of(resolved), null, "iot:Connect", clientArn, conditionContext)
|
||||
== IamPolicyEvaluator.Decision.ALLOW;
|
||||
}
|
||||
|
||||
private static boolean isWithinValidity(IotCertificate certificate) {
|
||||
Instant now = Instant.now();
|
||||
return (certificate.getNotBefore() == null || !now.isBefore(certificate.getNotBefore()))
|
||||
&& (certificate.getNotAfter() == null || now.isBefore(certificate.getNotAfter()));
|
||||
}
|
||||
|
||||
private List<String> attachedPolicyDocuments(String certificateArn, String region, String accountId) {
|
||||
String prefix = "policy-attachment:" + region + ":";
|
||||
return keysForAccount(policyAttachmentStore, accountId).stream()
|
||||
.filter(key -> key.startsWith(prefix))
|
||||
.filter(key -> getForAccount(policyAttachmentStore, accountId, key).orElse(Set.of()).contains(certificateArn))
|
||||
.map(key -> key.substring(prefix.length()))
|
||||
.sorted()
|
||||
.map(policyName -> getForAccount(policyStore, accountId, policyKey(region, policyName)))
|
||||
.flatMap(Optional::stream)
|
||||
.map(IotPolicy::getPolicyDocument)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The policy variables AWS IoT defines for a connection: the basic ones (client id, source
|
||||
* address, the domain name the client connected to) and the thing ones, which resolve only when
|
||||
* the client id names a thing attached to the certificate, as on AWS.
|
||||
*/
|
||||
private Map<String, String> connectionVariables(RegisteredDevice device, String clientId, String sourceIp, String domainName) {
|
||||
Map<String, String> variables = new LinkedHashMap<>();
|
||||
variables.put("iot:ClientId", clientId);
|
||||
if (sourceIp != null) {
|
||||
variables.put("aws:SourceIp", sourceIp);
|
||||
}
|
||||
if (domainName != null) {
|
||||
variables.put("iot:DomainName", domainName);
|
||||
}
|
||||
Optional<Thing> thing = getForAccount(thingStore, device.accountId(), thingKey(device.region(), clientId));
|
||||
boolean attached = thing.isPresent()
|
||||
&& getForAccount(thingPrincipalStore, device.accountId(), thingPrincipalKey(device.region(), clientId))
|
||||
.orElse(Set.of()).contains(device.certificate().getCertificateArn());
|
||||
variables.put("iot:Connection.Thing.IsAttached", Boolean.toString(attached));
|
||||
if (attached) {
|
||||
variables.put("iot:Connection.Thing.ThingName", clientId);
|
||||
if (thing.get().getThingTypeName() != null) {
|
||||
variables.put("iot:Connection.Thing.ThingTypeName", thing.get().getThingTypeName());
|
||||
}
|
||||
thing.get().getAttributes().forEach((name, value) -> variables.put("iot:Connection.Thing.Attributes[" + name + "]", value));
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes the connection's variables into every string of the document on the parsed tree,
|
||||
* so a client id is never read as JSON. A statement still holding a variable Floci cannot
|
||||
* resolve matches nothing, as an unresolvable variable does on AWS, and is dropped. A document
|
||||
* that does not parse is returned as is: the evaluator logs it and skips it.
|
||||
*/
|
||||
private String resolvePolicyVariables(String document, Map<String, String> variables) {
|
||||
JsonNode root;
|
||||
try {
|
||||
root = objectMapper.readTree(document);
|
||||
} catch (JsonProcessingException e) {
|
||||
return document;
|
||||
}
|
||||
if (!(root instanceof ObjectNode policy)) {
|
||||
return document;
|
||||
}
|
||||
JsonNode statements = policy.get("Statement");
|
||||
Iterable<JsonNode> each = statements == null ? List.of() : statements.isArray() ? statements : List.of(statements);
|
||||
ArrayNode resolved = objectMapper.createArrayNode();
|
||||
for (JsonNode statement : each) {
|
||||
JsonNode substituted = substituteVariables(statement.deepCopy(), variables);
|
||||
if (!hasUnresolvedVariable(substituted)) {
|
||||
resolved.add(substituted);
|
||||
}
|
||||
}
|
||||
policy.set("Statement", resolved);
|
||||
return policy.toString();
|
||||
}
|
||||
|
||||
private static JsonNode substituteVariables(JsonNode node, Map<String, String> variables) {
|
||||
if (node.isTextual()) {
|
||||
String text = node.asText();
|
||||
for (Map.Entry<String, String> variable : variables.entrySet()) {
|
||||
text = text.replace("${" + variable.getKey() + "}", variable.getValue());
|
||||
}
|
||||
return TextNode.valueOf(text);
|
||||
}
|
||||
if (node instanceof ObjectNode object) {
|
||||
for (Map.Entry<String, JsonNode> property : object.properties()) {
|
||||
property.setValue(substituteVariables(property.getValue(), variables));
|
||||
}
|
||||
} else if (node instanceof ArrayNode array) {
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
array.set(i, substituteVariables(array.get(i), variables));
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static boolean hasUnresolvedVariable(JsonNode node) {
|
||||
if (node.isTextual()) {
|
||||
return POLICY_VARIABLE.matcher(node.asText()).find();
|
||||
}
|
||||
for (JsonNode child : node) {
|
||||
if (hasUnresolvedVariable(child)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<AccountAwareStorageBackend.AccountEntry<IotCertificate>> registeredCertificates(Predicate<String> keyFilter) {
|
||||
if (certificateStore instanceof AccountAwareStorageBackend<IotCertificate> aware) {
|
||||
return aware.scanAllAccountEntries(keyFilter);
|
||||
}
|
||||
return certificateStore.keys().stream()
|
||||
.filter(keyFilter)
|
||||
.flatMap(key -> certificateStore.get(key).stream()
|
||||
.map(value -> new AccountAwareStorageBackend.AccountEntry<>(regionResolver.getDefaultAccountId(), key, value)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static <V> Optional<V> getForAccount(StorageBackend<String, V> store, String accountId, String key) {
|
||||
return store instanceof AccountAwareStorageBackend<V> aware ? aware.getForAccount(accountId, key) : store.get(key);
|
||||
}
|
||||
|
||||
private static <V> Set<String> keysForAccount(StorageBackend<String, V> store, String accountId) {
|
||||
return store instanceof AccountAwareStorageBackend<V> aware ? aware.keysForAccount(accountId) : store.keys();
|
||||
}
|
||||
|
||||
public IotPolicy createPolicy(String policyName, String policyDocument, String region) {
|
||||
|
||||
Reference in new issue
Block a user