diff --git a/compatibility-tests/sdk-test-java/pom.xml b/compatibility-tests/sdk-test-java/pom.xml index 7f84ea85d8..d85e44997b 100644 --- a/compatibility-tests/sdk-test-java/pom.xml +++ b/compatibility-tests/sdk-test-java/pom.xml @@ -666,6 +666,20 @@ 1.84 test + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.2.5 + test + + + org.eclipse.paho + org.eclipse.paho.mqttv5.client + 1.2.5 + test + diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotCustomAuthorizerTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotCustomAuthorizerTest.java new file mode 100644 index 0000000000..5102da234b --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotCustomAuthorizerTest.java @@ -0,0 +1,1245 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; +import org.eclipse.paho.mqttv5.client.IMqttToken; +import org.eclipse.paho.mqttv5.client.MqttAsyncClient; +import org.eclipse.paho.mqttv5.client.MqttCallback; +import org.eclipse.paho.mqttv5.client.MqttConnectionOptions; +import org.eclipse.paho.mqttv5.client.MqttDisconnectResponse; +import org.eclipse.paho.mqttv5.common.MqttMessage; +import org.eclipse.paho.mqttv5.common.packet.MqttProperties; +import org.jboss.logging.Logger; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; +import software.amazon.awssdk.awscore.exception.AwsServiceException; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.iam.IamClient; +import software.amazon.awssdk.services.iot.IotClient; +import software.amazon.awssdk.services.iot.model.AuthorizerStatus; +import software.amazon.awssdk.services.iot.model.InvalidRequestException; +import software.amazon.awssdk.services.iot.model.InvalidResponseException; +import software.amazon.awssdk.services.iot.model.ResourceNotFoundException; +import software.amazon.awssdk.services.iot.model.TestInvokeAuthorizerRequest; +import software.amazon.awssdk.services.iot.model.TestInvokeAuthorizerResponse; +import software.amazon.awssdk.services.lambda.LambdaClient; +import software.amazon.awssdk.services.lambda.model.InvalidParameterValueException; +import software.amazon.awssdk.services.lambda.model.Runtime; +import software.amazon.awssdk.services.lambda.model.State; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.Signature; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.Assertions.fail; + +/** + * AWS IoT custom authorizers: {@code TestInvokeAuthorizer} and enforcement of a real MQTT CONNECT over + * WebSocket. Every expectation was measured against real AWS IoT Core (eu-west-1). + * + *

One Python Lambda serves every case. It decides by the decoded MQTT password, or by the token when + * there is no password, or by an {@code x-secret} header for an HTTP context. The {@code shape:} + * secrets make the Lambda check the received event against the AWS shape for that transport and answer + * with principal {@code shapeok} (allowed) or {@code shapebad} (denied), so the event shape is asserted + * through the authorizer response or the connection outcome. + * + *

Paho v3 and v5 share several class names ({@code MqttException}, {@code MemoryPersistence}); the v3 + * ones are imported and the v5 ones are written fully qualified. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("IoT custom authorizer invocation and enforcement") +class IotCustomAuthorizerTest { + + private static final Logger LOG = Logger.getLogger(IotCustomAuthorizerTest.class); + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static final String PRINCIPAL = "measuredprincipal"; + private static final String TOKEN_KEY_NAME = "tok"; + private static final String SHAPE_SERVER_NAME = "shape.example.com"; + private static final String NAME_PARAM = "x-amz-customauthorizer-name="; + private static final String SIGNATURE_PARAM = "x-amz-customauthorizer-signature="; + private static final String NAME_HEADER = "x-amz-customauthorizer-name"; + private static final int TIMEOUT_SECONDS = 20; + /** AWS IoT allows one CONNECT per second per client id and answers a faster one with CONNACK 151. */ + private static final long CONNECT_SPACING_NANOS = TimeUnit.MILLISECONDS.toNanos(1200); + /** + * MQTT 5 refusal: CONNACK reason 0x87. AWS also sends the reason string + * {@code CONNACK:Client is not authenticated/authorized to send the message:}, which Paho 1.2.5 drops. + */ + private static final int MQTT5_NOT_AUTHORIZED = 135; + /** MQTT 3.1.1 refusal: AWS sends no CONNACK and closes the WebSocket, which Paho reports as a lost connection. */ + private static final int MQTT3_REFUSAL_REASON_CODE = MqttException.REASON_CODE_CONNECTION_LOST; + + private static final String LAMBDA_SOURCE = """ + import base64 + import json + import os + import re + + ALLOW_POLICY = os.environ["ALLOW_POLICY"] + ARN_PREFIX = os.environ["IOT_ARN_PREFIX"] + CLIENT_ID = os.environ["ALLOWED_CLIENT_ID"] + NAME_PREFIX = os.environ["NAME_PREFIX"] + SERVER_NAME = os.environ["SHAPE_SERVER_NAME"] + NAME_PARAM = "x-amz-customauthorizer-name=" + SIGNATURE_PARAM = "x-amz-customauthorizer-signature=" + ID_PATTERN = re.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$") + SIGNED_KINDS = ("ti-token", "ti-signed-mqtt", "ws-signed") + + + def response(policies, principal="measuredprincipal", refresh=300, disconnect=3600): + out = {"isAuthenticated": True, "principalId": principal, "policyDocuments": policies} + if refresh is not None: + out["refreshAfterInSeconds"] = refresh + if disconnect is not None: + out["disconnectAfterInSeconds"] = disconnect + return out + + + def secret_of(event): + data = event.get("protocolData") or {} + mqtt = data.get("mqtt") or {} + if mqtt.get("password") is not None: + return base64.b64decode(mqtt["password"]).decode("utf-8") + if event.get("token") is not None: + return event["token"] + http = data.get("http") or {} + for key, value in (http.get("headers") or {}).items(): + if key.lower() == "x-secret": + return value + for part in (http.get("queryString") or "").lstrip("?").split("&"): + if part.startswith("x-secret="): + return part.split("=", 1)[1] + return None + + + def expect(problems, condition, what): + if not condition: + problems.append(what) + + + def named_username(username, signed, secret): + prefix = CLIENT_ID + "?" + NAME_PARAM + NAME_PREFIX + if not isinstance(username, str) or not username.startswith(prefix): + return False + if signed: + return ("&" + SIGNATURE_PARAM) in username and username.endswith("&tok=" + secret) + return "&" not in username + + + def shape_problems(kind, event, secret): + problems = [] + signed = kind in SIGNED_KINDS + expect(problems, event.get("signatureVerified") is signed, "signatureVerified %r" % event.get("signatureVerified")) + if signed: + expect(problems, event.get("token") == secret, "token %r" % event.get("token")) + else: + expect(problems, "token" not in event, "unexpected token") + meta = event.get("connectionMetadata") + expect(problems, isinstance(meta, dict) and set(meta) == {"id"} and ID_PATTERN.match(str(meta.get("id"))) is not None, "connectionMetadata %r" % meta) + if kind == "ti-token": + expect(problems, set(event) == {"token", "signatureVerified", "connectionMetadata"}, "keys %r" % sorted(event)) + return problems + top = {"protocolData", "protocols", "signatureVerified", "connectionMetadata"} + if signed: + top.add("token") + expect(problems, set(event) == top, "keys %r" % sorted(event)) + data = event.get("protocolData") + protocols = event.get("protocols") + if not isinstance(data, dict): + problems.append("protocolData %r" % data) + return problems + if kind == "ti-http": + expect(problems, protocols == ["http"], "protocols %r" % protocols) + expected = {"http": {"headers": {"X-Secret": secret, "X-Mixed-Case": "Value"}, "queryString": "?a=b&c=d"}} + expect(problems, data == expected, "protocolData %r" % data) + return problems + mqtt = data.get("mqtt") or {} + username = mqtt.get("username") + expect(problems, mqtt.get("clientId") == CLIENT_ID, "clientId %r" % mqtt.get("clientId")) + if signed: + expect(problems, set(mqtt) == {"username", "clientId"}, "mqtt keys %r" % sorted(mqtt)) + else: + expect(problems, set(mqtt) == {"username", "password", "clientId"}, "mqtt keys %r" % sorted(mqtt)) + expected_password = base64.b64encode(secret.encode("utf-8")).decode("ascii") + expect(problems, mqtt.get("password") == expected_password, "password %r" % mqtt.get("password")) + if kind in ("ti-mqtt", "ti-mqtt-tls", "ti-signed-mqtt"): + tls = kind == "ti-mqtt-tls" + expect(problems, protocols == (["tls", "mqtt"] if tls else ["mqtt"]), "protocols %r" % protocols) + expect(problems, set(data) == ({"tls", "mqtt"} if tls else {"mqtt"}), "protocolData keys %r" % sorted(data)) + if tls: + expect(problems, data.get("tls") == {"serverName": SERVER_NAME}, "tls %r" % data.get("tls")) + expect(problems, named_username(username, signed, secret), "username %r" % username) + return problems + expect(problems, isinstance(protocols, list) and "http" in protocols and "mqtt" in protocols + and set(protocols) <= {"tls", "http", "mqtt"} and len(set(protocols)) == len(protocols), "protocols %r" % protocols) + expect(problems, isinstance(protocols, list) and set(data) == set(protocols), "protocolData keys %r" % sorted(data)) + if "tls" in data: + tls = data["tls"] + expect(problems, isinstance(tls, dict) and set(tls) == {"serverName"} and bool(tls.get("serverName")), "tls %r" % tls) + http = data.get("http") or {} + expect(problems, set(http) == {"headers", "queryString"}, "http keys %r" % sorted(http)) + headers = http.get("headers") or {} + expect(problems, all(key == key.lower() for key in headers), "header names %r" % sorted(headers)) + expect(problems, "mqtt" in str(headers.get("sec-websocket-protocol")), "sec-websocket-protocol %r" % headers.get("sec-websocket-protocol")) + query = http.get("queryString") + if kind == "ws-query": + expect(problems, isinstance(query, str) and query.startswith(NAME_PARAM + NAME_PREFIX) and "&" not in query, "queryString %r" % query) + else: + expect(problems, query == "", "queryString %r" % query) + if kind == "ws-header": + expect(problems, str(headers.get("x-amz-customauthorizer-name")).startswith(NAME_PREFIX), "name header %r" % headers.get("x-amz-customauthorizer-name")) + if kind in ("ws-query", "ws-header"): + expect(problems, username == CLIENT_ID, "username %r" % username) + else: + expect(problems, named_username(username, signed, secret), "username %r" % username) + return problems + + + def handler(event, context): + print(json.dumps(event)) + secret = secret_of(event) + print("secret=%r" % (secret,)) + policy = ALLOW_POLICY + if secret is None: + return {"isAuthenticated": False} + if secret.startswith("shape:"): + problems = shape_problems(secret[len("shape:"):], event, secret) + print("shape problems: %r" % (problems,)) + if problems: + return {"isAuthenticated": False, "principalId": "shapebad", "policyDocuments": []} + return response([policy], principal="shapeok") + if secret.startswith("ttl:"): + _, refresh, disconnect = secret.split(":") + return response([policy], + refresh=None if refresh == "-" else int(refresh), + disconnect=None if disconnect == "-" else int(disconnect)) + if secret == "allow-me": + return response([policy]) + if secret == "raise": + raise Exception("compat-raise") + if secret == "garbage": + return {"foo": 1} + if secret == "noauthflag": + return {"principalId": "measuredprincipal", "policyDocuments": [policy]} + if secret == "noprincipal": + return {"isAuthenticated": True, "policyDocuments": [policy]} + if secret == "badprincipal": + return response([policy], principal="bad-principal!") + if secret == "nopolicy": + return {"isAuthenticated": True, "principalId": "measuredprincipal"} + if secret == "denynopolicy": + return {"isAuthenticated": False, "principalId": "measuredprincipal"} + if secret == "denypol": + return {"isAuthenticated": False, "principalId": "measuredprincipal", "policyDocuments": [policy]} + if secret == "emptypol": + return response([]) + if secret == "badjson": + return response(["this is not json"]) + if secret == "manypol": + return response([policy] * 11) + if secret == "bigpol": + big = json.loads(policy) + big["Statement"][1]["Resource"] = [ARN_PREFIX + ":topic/big/%040d" % i for i in range(60)] + return response([json.dumps(big)]) + if secret == "objpolicy": + return response([json.loads(policy)]) + return {"isAuthenticated": False} + """; + + private final String prefix = TestFixtures.uniqueName("compat-cauth"); + private final String roleName = prefix + "-role"; + private final String rolePolicyName = prefix + "-logs"; + private final String functionName = prefix + "-fn"; + private final String authorizer = prefix + "-a"; + private final String signedAuthorizer = prefix + "-s"; + private final String inactiveAuthorizer = prefix + "-i"; + private final String unknownAuthorizer = prefix + "-nope"; + private final String clientId = prefix + "-client"; + private final String otherClientId = prefix + "-other"; + private final List createdAuthorizers = new ArrayList<>(); + private final Map lastConnectNanos = new HashMap<>(); + + private IotClient iot; + private LambdaClient lambda; + private IamClient iam; + private CloudWatchLogsClient logs; + private boolean roleCreated; + private boolean functionCreated; + private boolean logGroupCreated; + private String allowPolicy; + private String mqttUri; + private KeyPair key1; + private KeyPair key2; + private KeyPair unregisteredKey; + + enum Mqtt { V3_1_1, V5 } + + @BeforeAll + void setUp() throws Exception { + iot = TestFixtures.iotClient(); + lambda = TestFixtures.lambdaClient(); + iam = TestFixtures.iamClient(); + logs = TestFixtures.cloudWatchLogsClient(); + + String region; + try (StsClient sts = TestFixtures.stsClient()) { + region = sts.serviceClientConfiguration().region().id(); + GetCallerIdentityResponse identity = sts.getCallerIdentity(); + String partition = identity.arn().split(":")[1]; + String arnPrefix = "arn:" + partition + ":iot:" + region + ":" + identity.account(); + allowPolicy = "{\"Version\": \"2012-10-17\", \"Statement\": [{\"Effect\": \"Allow\", " + + "\"Action\": \"iot:Connect\", \"Resource\": \"" + arnPrefix + ":client/" + clientId + "\"}, " + + "{\"Effect\": \"Allow\", \"Action\": [\"iot:Publish\", \"iot:Subscribe\", \"iot:Receive\"], " + + "\"Resource\": \"*\"}]}"; + + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + key1 = generator.generateKeyPair(); + key2 = generator.generateKeyPair(); + unregisteredKey = generator.generateKeyPair(); + + String roleArn = createRole(); + String functionArn = createFunction(roleArn, arnPrefix); + createAuthorizer(authorizer, functionArn, AuthorizerStatus.ACTIVE, false); + createAuthorizer(signedAuthorizer, functionArn, AuthorizerStatus.ACTIVE, true); + createAuthorizer(inactiveAuthorizer, functionArn, AuthorizerStatus.INACTIVE, false); + } + mqttUri = mqttBaseUri(); + LOG.infov("custom authorizer fixtures ready: prefix={0} mqtt={1}", prefix, mqttUri); + awaitAuthorizerReady(); + } + + @AfterAll + void tearDown() { + for (String name : createdAuthorizers) { + quietly("deactivate authorizer " + name, + () -> iot.updateAuthorizer(r -> r.authorizerName(name).status(AuthorizerStatus.INACTIVE))); + quietly("delete authorizer " + name, () -> iot.deleteAuthorizer(r -> r.authorizerName(name))); + } + if (functionCreated) { + quietly("delete function", () -> lambda.deleteFunction(r -> r.functionName(functionName))); + } + if (roleCreated) { + quietly("delete role policy", + () -> iam.deleteRolePolicy(r -> r.roleName(roleName).policyName(rolePolicyName))); + quietly("delete role", () -> iam.deleteRole(r -> r.roleName(roleName))); + } + if (logGroupCreated) { + quietly("delete log group", + () -> logs.deleteLogGroup(r -> r.logGroupName("/aws/lambda/" + functionName))); + } + quietly("close clients", () -> { + iot.close(); + lambda.close(); + iam.close(); + logs.close(); + }); + } + + // --------------------------------------------------------------------------------------------- + // TestInvokeAuthorizer + // --------------------------------------------------------------------------------------------- + + @Test + @DisplayName("test-invoke with an MQTT context returns the Lambda's verdict, policies verbatim and TTLs") + void testInvokeAllow() { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "allow-me"); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.principalId()).isEqualTo(PRINCIPAL); + assertThat(response.policyDocuments()).containsExactly(allowPolicy); + assertThat(response.refreshAfterInSeconds()).isEqualTo(300); + assertThat(response.disconnectAfterInSeconds()).isEqualTo(3600); + } + + @Test + @DisplayName("test-invoke with only an MQTT context sends protocols [mqtt] and the MQTT fields as given") + void testInvokeMqttContextEventShape() { + assertShapeOk(invokeMqtt(authorizer, "shape:ti-mqtt")); + } + + @Test + @DisplayName("test-invoke with MQTT and TLS contexts sends protocols [tls, mqtt] and the server name") + void testInvokeMqttAndTlsContextEventShape() { + String secret = "shape:ti-mqtt-tls"; + assertShapeOk(iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer) + .mqttContext(m -> m.clientId(clientId) + .username(namedUsername(authorizer)) + .password(SdkBytes.fromUtf8String(secret))) + .tlsContext(t -> t.serverName(SHAPE_SERVER_NAME)))); + } + + @Test + @DisplayName("test-invoke with only an HTTP context keeps header case and the query string with its '?'") + void testInvokeHttpContextEventShape() { + Map headers = new LinkedHashMap<>(); + headers.put("X-Secret", "shape:ti-http"); + headers.put("X-Mixed-Case", "Value"); + assertShapeOk(iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer) + .httpContext(h -> h.headers(headers).queryString("?a=b&c=d")))); + } + + @Test + @DisplayName("test-invoke fills omitted TTLs with 300 and 86400") + void testInvokeOmittedTtlsAreDefaulted() { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "ttl:-:-"); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.policyDocuments()).containsExactly(allowPolicy); + assertThat(response.refreshAfterInSeconds()).isEqualTo(300); + assertThat(response.disconnectAfterInSeconds()).isEqualTo(86400); + } + + @Test + @DisplayName("test-invoke accepts TTLs at the inclusive bounds 300 and 86400") + void testInvokeTtlBoundsAccepted() { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "ttl:300:86400"); + + assertThat(response.refreshAfterInSeconds()).isEqualTo(300); + assertThat(response.disconnectAfterInSeconds()).isEqualTo(86400); + } + + @Test + @DisplayName("test-invoke returns a policy given as a JSON object as one compact string") + void testInvokeObjectPolicyIsCompacted() throws IOException { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "objpolicy"); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.policyDocuments()).containsExactly(MAPPER.writeValueAsString(MAPPER.readTree(allowPolicy))); + } + + static Stream invalidLambdaResponses() { + return Stream.of( + Arguments.of("garbage", "Lambda invocation result is in invalid format"), + Arguments.of("noauthflag", "Lambda invocation result is in invalid format"), + Arguments.of("deny", + "null at principalId failed to satisfy constraint: Member must satisfy ([a-zA-Z0-9]){1,128}"), + Arguments.of("noprincipal", + "null at principalId failed to satisfy constraint: Member must satisfy ([a-zA-Z0-9]){1,128}"), + Arguments.of("badprincipal", "bad-principal! at principalId failed to satisfy constraint: " + + "Member must satisfy ([a-zA-Z0-9]){1,128}"), + Arguments.of("nopolicy", + "policyDocuments failed to satisfy constraint: List of policies cannot be null."), + Arguments.of("denynopolicy", + "policyDocuments failed to satisfy constraint: List of policies cannot be null."), + Arguments.of("badjson", "policyDocuments failed to satisfy constraint: " + + "Policy document is Malformed: Invalid policy syntax."), + Arguments.of("manypol", + "policyDocuments failed to satisfy constraint: Number of policies allowed cannot exceed 10"), + Arguments.of("bigpol", "policyDocuments failed to satisfy constraint: " + + "Size of a policy document cannot exceeds hard limit of 2048"), + Arguments.of("ttl:299:3600", "299 at refreshAfterInSeconds failed to satisfy constraint: " + + "Member must be greater than 5 minutes and less than 24 hours."), + Arguments.of("ttl:86401:3600", "86401 at refreshAfterInSeconds failed to satisfy constraint: " + + "Member must be greater than 5 minutes and less than 24 hours."), + Arguments.of("ttl:300:299", "299 at disconnectAfterInSeconds failed to satisfy constraint: " + + "Member must be greater than 5 minutes and less than 24 hours."), + Arguments.of("ttl:300:86401", "86401 at disconnectAfterInSeconds failed to satisfy constraint: " + + "Member must be greater than 5 minutes and less than 24 hours."), + Arguments.of("ttl:10:10", "10 at refreshAfterInSeconds failed to satisfy constraint: " + + "Member must be greater than 5 minutes and less than 24 hours."), + Arguments.of("raise", "Lambda function encountered error in execution: compat-raise: Exception")); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("invalidLambdaResponses") + @DisplayName("test-invoke rejects an invalid Lambda response with InvalidResponseException") + void testInvokeRejectsInvalidLambdaResponse(String secret, String message) { + assertAwsError(() -> invokeMqtt(authorizer, secret), InvalidResponseException.class, 400, message); + } + + @Test + @DisplayName("test-invoke returns isAuthenticated false when the Lambda denies with a principal and policies") + void testInvokeDenyWithPolicies() { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "denypol"); + + assertThat(response.isAuthenticated()).isFalse(); + assertThat(response.principalId()).isEqualTo(PRINCIPAL); + assertThat(response.policyDocuments()).containsExactly(allowPolicy); + } + + @Test + @DisplayName("test-invoke accepts an empty policyDocuments list") + void testInvokeEmptyPolicies() { + TestInvokeAuthorizerResponse response = invokeMqtt(authorizer, "emptypol"); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.principalId()).isEqualTo(PRINCIPAL); + assertThat(response.policyDocuments()).isEmpty(); + } + + @Test + @DisplayName("test-invoke rejects a token without its signature") + void testInvokeTokenWithoutSignature() { + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer).token("allow-me")), + InvalidRequestException.class, 400, "Request must provide both token and tokenSignature together."); + } + + @Test + @DisplayName("test-invoke rejects a token and signature on a signing-disabled authorizer") + void testInvokeTokenOnSigningDisabledAuthorizer() throws GeneralSecurityException { + String signature = sign(key1.getPrivate(), "allow-me"); + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer) + .token("allow-me").tokenSignature(signature)), + InvalidRequestException.class, 400, + "Token and token signature provided but signing is disabled for the authorizer."); + } + + @Test + @DisplayName("test-invoke rejects a token combined with a context") + void testInvokeTokenWithContext() throws GeneralSecurityException { + String signature = sign(key1.getPrivate(), "allow-me"); + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token("allow-me").tokenSignature(signature) + .mqttContext(m -> m.clientId(clientId).password(SdkBytes.fromUtf8String("allow-me")))), + InvalidRequestException.class, 400, "Request cannot have token and Http/Mqtt/Tls context."); + } + + @Test + @DisplayName("test-invoke rejects a request without token or context") + void testInvokeEmptyRequest() { + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer)), + InvalidRequestException.class, 400, "Request must have Http/Mqtt/Tls context."); + } + + @Test + @DisplayName("test-invoke rejects an HTTP query string without the leading '?'") + void testInvokeQueryStringWithoutQuestionMark() { + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer) + .httpContext(h -> h.headers(Map.of("X-Secret", "allow-me")).queryString("a=b"))), + InvalidRequestException.class, 400, "Invalid arguments in the call"); + } + + @Test + @DisplayName("test-invoke rejects an empty HTTP query string") + void testInvokeEmptyQueryString() { + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(authorizer) + .httpContext(h -> h.headers(Map.of("X-Secret", "allow-me")).queryString(""))), + InvalidRequestException.class, 400, + "1 validation error detected: Value at 'httpContext.queryString' failed to satisfy constraint: " + + "Member must have length greater than or equal to 1"); + } + + @Test + @DisplayName("test-invoke rejects a token signature that is not base64") + void testInvokeNonBase64Signature() { + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token("allow-me").tokenSignature("not base64!")), + InvalidRequestException.class, 400, + "1 validation error detected: Value at 'tokenSignature' failed to satisfy constraint: " + + "Member must satisfy regular expression pattern: [A-Za-z0-9+/]+={0,2}"); + } + + @Test + @DisplayName("test-invoke works on an INACTIVE authorizer") + void testInvokeInactiveAuthorizer() { + TestInvokeAuthorizerResponse response = invokeMqtt(inactiveAuthorizer, "allow-me"); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.principalId()).isEqualTo(PRINCIPAL); + assertThat(response.policyDocuments()).containsExactly(allowPolicy); + } + + @Test + @DisplayName("test-invoke on a nonexistent authorizer is 404") + void testInvokeUnknownAuthorizer() { + assertAwsError(() -> invokeMqtt(unknownAuthorizer, "allow-me"), ResourceNotFoundException.class, 404, + "Authorizer " + unknownAuthorizer + " not found"); + } + + @Test + @DisplayName("test-invoke in token mode verifies a signature by either registered key") + void testInvokeSignedTokenEitherKey() throws GeneralSecurityException { + for (KeyPair key : List.of(key1, key2)) { + String signature = sign(key.getPrivate(), "allow-me"); + TestInvokeAuthorizerResponse response = iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token("allow-me").tokenSignature(signature)); + + assertThat(response.isAuthenticated()).isTrue(); + assertThat(response.principalId()).isEqualTo(PRINCIPAL); + assertThat(response.policyDocuments()).containsExactly(allowPolicy); + } + } + + @Test + @DisplayName("test-invoke in token mode sends only token, signatureVerified true and connectionMetadata") + void testInvokeTokenModeEventShape() throws GeneralSecurityException { + String token = "shape:ti-token"; + String signature = sign(key2.getPrivate(), token); + assertShapeOk(iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token(token).tokenSignature(signature))); + } + + @Test + @DisplayName("test-invoke rejects a signature over another token or by an unregistered key") + void testInvokeSignatureMismatch() throws GeneralSecurityException { + String message = "Token signature mismatch for authorizer " + signedAuthorizer; + String otherToken = sign(key1.getPrivate(), "other-token"); + String unregistered = sign(unregisteredKey.getPrivate(), "allow-me"); + + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token("allow-me").tokenSignature(otherToken)), InvalidRequestException.class, 400, message); + assertAwsError(() -> iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .token("allow-me").tokenSignature(unregistered)), InvalidRequestException.class, 400, message); + } + + @Test + @DisplayName("test-invoke with signature and token in the MQTT username verifies them and sends the token") + void testInvokeSignedMqttUsername() throws GeneralSecurityException { + String token = "shape:ti-signed-mqtt"; + String username = signedUsername(sign(key1.getPrivate(), token), token); + assertShapeOk(iot.testInvokeAuthorizer(r -> r.authorizerName(signedAuthorizer) + .mqttContext(m -> m.clientId(clientId).username(username)))); + } + + // --------------------------------------------------------------------------------------------- + // MQTT CONNECT over WebSocket + // --------------------------------------------------------------------------------------------- + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("an allowed CONNECT succeeds and publish/subscribe round-trips") + void mqttAllowRoundTrip(Mqtt version) throws Exception { + String topic = prefix + "/rt/" + version.name().toLowerCase(); + String payload = "hello-" + version.name(); + try (Session session = open(version, target(namedUsername(authorizer), "allow-me"))) { + assertThat(session.roundTrip(topic, payload)).isEqualTo(payload); + } + } + + enum Refusal { + DENY("deny", AuthorizerKind.ACTIVE, false), + CLIENT_ID_NOT_IN_POLICY("allow-me", AuthorizerKind.ACTIVE, true), + LAMBDA_RAISES("raise", AuthorizerKind.ACTIVE, false), + GARBAGE_RESPONSE("garbage", AuthorizerKind.ACTIVE, false), + EMPTY_POLICIES("emptypol", AuthorizerKind.ACTIVE, false), + REFRESH_BELOW_RANGE("ttl:299:3600", AuthorizerKind.ACTIVE, false), + DISCONNECT_ABOVE_RANGE("ttl:300:86401", AuthorizerKind.ACTIVE, false), + UNKNOWN_AUTHORIZER("allow-me", AuthorizerKind.UNKNOWN, false), + INACTIVE_AUTHORIZER("allow-me", AuthorizerKind.INACTIVE, false); + + final String secret; + final AuthorizerKind authorizer; + final boolean otherClient; + + Refusal(String secret, AuthorizerKind authorizer, boolean otherClient) { + this.secret = secret; + this.authorizer = authorizer; + this.otherClient = otherClient; + } + } + + enum AuthorizerKind { ACTIVE, UNKNOWN, INACTIVE } + + static Stream refusals() { + List cases = new ArrayList<>(); + for (Mqtt version : Mqtt.values()) { + for (Refusal refusal : Refusal.values()) { + cases.add(Arguments.of(version, refusal)); + } + } + return cases.stream(); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("refusals") + @DisplayName("a CONNECT the authorizer does not allow is refused") + void mqttRefused(Mqtt version, Refusal refusal) { + String name = switch (refusal.authorizer) { + case ACTIVE -> authorizer; + case UNKNOWN -> unknownAuthorizer; + case INACTIVE -> inactiveAuthorizer; + }; + String client = refusal.otherClient ? otherClientId : clientId; + assertRefused(version, new Target(mqttUri, client, client + "?" + NAME_PARAM + name, + refusal.secret.getBytes(StandardCharsets.UTF_8), Map.of())); + } + + static Stream acceptedForms() { + List cases = new ArrayList<>(); + for (Mqtt version : Mqtt.values()) { + for (String secret : List.of("objpolicy", "ttl:-:-", "ttl:300:86400")) { + cases.add(Arguments.of(version, secret)); + } + } + return cases.stream(); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("acceptedForms") + @DisplayName("an object-form policy, omitted TTLs and boundary TTLs are accepted on a real CONNECT") + void mqttAcceptedForms(Mqtt version, String secret) throws Exception { + assertConnects(version, target(namedUsername(authorizer), secret)); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("the authorizer name is read from the WebSocket URL query") + void mqttNameInWebSocketQuery(Mqtt version) throws Exception { + assertConnects(version, new Target(mqttUri + "?" + NAME_PARAM + authorizer, clientId, clientId, + "allow-me".getBytes(StandardCharsets.UTF_8), Map.of())); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("the authorizer name is read from the WebSocket upgrade header") + void mqttNameInUpgradeHeader(Mqtt version) throws Exception { + assertConnects(version, new Target(mqttUri, clientId, clientId, + "allow-me".getBytes(StandardCharsets.UTF_8), Map.of(NAME_HEADER, authorizer))); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("WebSocket event: name in the username, empty query, lower-case headers") + void mqttEventShapeNameInUsername(Mqtt version) throws Exception { + assertConnects(version, target(namedUsername(authorizer), "shape:ws-user")); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("WebSocket event: name in the URL query, query string without '?', plain username") + void mqttEventShapeNameInQuery(Mqtt version) throws Exception { + assertConnects(version, new Target(mqttUri + "?" + NAME_PARAM + authorizer, clientId, clientId, + "shape:ws-query".getBytes(StandardCharsets.UTF_8), Map.of())); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("WebSocket event: name in the upgrade header, lower-cased among the headers") + void mqttEventShapeNameInHeader(Mqtt version) throws Exception { + assertConnects(version, new Target(mqttUri, clientId, clientId, + "shape:ws-header".getBytes(StandardCharsets.UTF_8), Map.of(NAME_HEADER, authorizer))); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("a signed authorizer accepts a valid signature and token in the username") + void mqttSignedAllow(Mqtt version) throws Exception { + String username = signedUsername(sign(key1.getPrivate(), "allow-me"), "allow-me"); + assertConnects(version, new Target(mqttUri, clientId, username, null, Map.of())); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("a signed CONNECT sends the token with signatureVerified true") + void mqttSignedEventShape(Mqtt version) throws Exception { + String token = "shape:ws-signed"; + String username = signedUsername(sign(key2.getPrivate(), token), token); + assertConnects(version, new Target(mqttUri, clientId, username, null, Map.of())); + } + + @ParameterizedTest(name = "{0}") + @EnumSource(Mqtt.class) + @DisplayName("a signed authorizer refuses a signature over another token") + void mqttSignedWrongSignature(Mqtt version) throws GeneralSecurityException { + String username = signedUsername(sign(key1.getPrivate(), "other-token"), "allow-me"); + assertRefused(version, new Target(mqttUri, clientId, username, null, Map.of())); + } + + // --------------------------------------------------------------------------------------------- + // Fixtures + // --------------------------------------------------------------------------------------------- + + private String createRole() { + String trust = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"; + String roleArn = iam.createRole(r -> r.roleName(roleName).assumeRolePolicyDocument(trust) + .description("floci compatibility test, IoT custom authorizer")).role().arn(); + roleCreated = true; + String logsPolicy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":" + + "[\"logs:CreateLogStream\",\"logs:PutLogEvents\"],\"Resource\":\"*\"}]}"; + iam.putRolePolicy(r -> r.roleName(roleName).policyName(rolePolicyName).policyDocument(logsPolicy)); + return roleArn; + } + + private String createFunction(String roleArn, String arnPrefix) throws InterruptedException { + Map environment = new LinkedHashMap<>(); + environment.put("ALLOW_POLICY", allowPolicy); + environment.put("IOT_ARN_PREFIX", arnPrefix); + environment.put("ALLOWED_CLIENT_ID", clientId); + environment.put("NAME_PREFIX", prefix); + environment.put("SHAPE_SERVER_NAME", SHAPE_SERVER_NAME); + SdkBytes zip = SdkBytes.fromByteArray(zip("lambda_function.py", LAMBDA_SOURCE)); + // The role may write to this group but not create one, so a late log delivery after the + // tear-down cannot re-create it. + logs.createLogGroup(r -> r.logGroupName("/aws/lambda/" + functionName)); + logGroupCreated = true; + + String functionArn = null; + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(90); + while (functionArn == null) { + try { + functionArn = lambda.createFunction(r -> r.functionName(functionName) + .runtime(Runtime.PYTHON3_12) + .role(roleArn) + .handler("lambda_function.handler") + .timeout(10) + .memorySize(128) + .environment(e -> e.variables(environment)) + .code(c -> c.zipFile(zip))).functionArn(); + } catch (InvalidParameterValueException e) { + if (System.nanoTime() > deadline || !e.getMessage().contains("cannot be assumed")) { + throw e; + } + LOG.infov("waiting for the IAM role to become assumable: {0}", e.awsErrorDetails().errorMessage()); + Thread.sleep(3000); + } + } + functionCreated = true; + + long activeDeadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(90); + while (lambda.getFunctionConfiguration(r -> r.functionName(functionName)).state() != State.ACTIVE) { + if (System.nanoTime() > activeDeadline) { + throw new IllegalStateException("function " + functionName + " did not become Active"); + } + Thread.sleep(1000); + } + return functionArn; + } + + private void createAuthorizer(String name, String functionArn, AuthorizerStatus status, boolean signed) + throws GeneralSecurityException { + Map keys = new LinkedHashMap<>(); + keys.put("k1", pem(key1.getPublic())); + keys.put("k2", pem(key2.getPublic())); + String authorizerArn = iot.createAuthorizer(r -> { + r.authorizerName(name).authorizerFunctionArn(functionArn).status(status); + if (signed) { + r.signingDisabled(false).tokenKeyName(TOKEN_KEY_NAME).tokenSigningPublicKeys(keys); + } else { + r.signingDisabled(true); + } + }).authorizerArn(); + createdAuthorizers.add(name); + lambda.addPermission(r -> r.functionName(functionName) + .statementId(name + "-invoke") + .action("lambda:InvokeFunction") + .principal("iot.amazonaws.com") + .sourceArn(authorizerArn)); + } + + /** + * Right after creation AWS IoT sporadically refuses a valid CONNECT through a new authorizer without invoking + * the Lambda. Wait for three consecutive allowed CONNECTs through each ACTIVE authorizer before the tests + * start. Not getting there is left to the tests to report. + */ + private void awaitAuthorizerReady() throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(90); + int consecutive = 0; + int attempt = 0; + while (consecutive < 3 && System.nanoTime() < deadline) { + attempt++; + boolean ok = connects(target(namedUsername(authorizer), "allow-me")) + && connects(new Target(mqttUri, clientId, signedUsername(signOrFail("allow-me"), "allow-me"), + null, Map.of())); + consecutive = ok ? consecutive + 1 : 0; + if (!ok) { + Thread.sleep(2000); + } + } + LOG.infov("authorizers ready={0} after {1} round(s)", consecutive >= 3, attempt); + } + + private boolean connects(Target target) throws InterruptedException { + try (Session ignored = open(Mqtt.V3_1_1, target)) { + return true; + } catch (ConnectRefused | GeneralSecurityException e) { + LOG.infov("not ready: {0}", e.toString()); + return false; + } + } + + private String signOrFail(String token) { + try { + return sign(key1.getPrivate(), token); + } catch (GeneralSecurityException e) { + throw new IllegalStateException(e); + } + } + + private String mqttBaseUri() { + if (TestFixtures.isRealAws()) { + String host = iot.describeEndpoint(r -> r.endpointType("iot:Data-ATS")).endpointAddress(); + return "wss://" + host + ":443/mqtt"; + } + URI endpoint = TestFixtures.endpoint(); + boolean secure = "https".equalsIgnoreCase(endpoint.getScheme()); + int port = endpoint.getPort() != -1 ? endpoint.getPort() : secure ? 443 : 80; + return (secure ? "wss://" : "ws://") + endpoint.getHost() + ":" + port + "/mqtt"; + } + + // --------------------------------------------------------------------------------------------- + // Helpers + // --------------------------------------------------------------------------------------------- + + private TestInvokeAuthorizerResponse invokeMqtt(String name, String secret) { + return iot.testInvokeAuthorizer(TestInvokeAuthorizerRequest.builder() + .authorizerName(name) + .mqttContext(m -> m.clientId(clientId) + .username(clientId + "?" + NAME_PARAM + name) + .password(SdkBytes.fromUtf8String(secret))) + .build()); + } + + private static void assertShapeOk(TestInvokeAuthorizerResponse response) { + assertThat(response.principalId()) + .as("the Lambda reports shapebad when the event differs from the AWS shape (see its log)") + .isEqualTo("shapeok"); + assertThat(response.isAuthenticated()).isTrue(); + } + + private static void assertAwsError(ThrowingCallable call, Class type, + int status, String message) { + assertThatThrownBy(call).isInstanceOfSatisfying(type, e -> { + assertThat(e.statusCode()).isEqualTo(status); + assertThat(e.awsErrorDetails().errorMessage()).isEqualTo(message); + }); + } + + private String namedUsername(String name) { + return clientId + "?" + NAME_PARAM + name; + } + + private String signedUsername(String signature, String token) { + return clientId + "?" + NAME_PARAM + signedAuthorizer + + "&" + SIGNATURE_PARAM + URLEncoder.encode(signature, StandardCharsets.UTF_8) + + "&" + TOKEN_KEY_NAME + "=" + token; + } + + private Target target(String username, String secret) { + return new Target(mqttUri, clientId, username, secret.getBytes(StandardCharsets.UTF_8), Map.of()); + } + + private static String sign(PrivateKey key, String token) throws GeneralSecurityException { + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initSign(key); + signature.update(token.getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString(signature.sign()); + } + + private static String pem(PublicKey key) { + String body = Base64.getMimeEncoder(64, new byte[] {'\n'}).encodeToString(key.getEncoded()); + return "-----BEGIN PUBLIC KEY-----\n" + body + "\n-----END PUBLIC KEY-----"; + } + + private static byte[] zip(String entryName, String content) { + try { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ZipOutputStream zip = new ZipOutputStream(bytes)) { + zip.putNextEntry(new ZipEntry(entryName)); + zip.write(content.getBytes(StandardCharsets.UTF_8)); + zip.closeEntry(); + } + return bytes.toByteArray(); + } catch (IOException e) { + throw new IllegalStateException("cannot build the Lambda deployment package", e); + } + } + + private static void quietly(String what, Runnable action) { + try { + action.run(); + } catch (RuntimeException e) { + LOG.warnv("cleanup step failed: {0}: {1}", what, e.toString()); + } + } + + // --------------------------------------------------------------------------------------------- + // MQTT clients + // --------------------------------------------------------------------------------------------- + + private record Target(String uri, String clientId, String username, byte[] password, + Map headers) {} + + private interface Session extends AutoCloseable { + String roundTrip(String topic, String payload) throws Exception; + + @Override + void close(); + } + + /** A refused CONNECT, carrying the client's own exception as the cause. */ + private static final class ConnectRefused extends Exception { + ConnectRefused(Throwable cause) { + super("CONNECT failed: " + cause, cause); + } + } + + private void assertConnects(Mqtt version, Target target) throws Exception { + try (Session ignored = open(version, target)) { + LOG.infov("{0} connected as {1}", version, target.username()); + } + } + + private void assertRefused(Mqtt version, Target target) { + Throwable thrown = catchThrowable(() -> open(version, target).close()); + if (!(thrown instanceof ConnectRefused refused)) { + fail("expected the CONNECT to be refused, got " + thrown); + return; + } + Throwable cause = refused.getCause(); + LOG.infov("{0} refused: {1} cause={2}", version, cause, cause.getCause()); + if (version == Mqtt.V3_1_1) { + assertThat(cause).isInstanceOfSatisfying(MqttException.class, + e -> assertThat(e.getReasonCode()).isEqualTo(MQTT3_REFUSAL_REASON_CODE)); + } else { + assertThat(cause).isInstanceOfSatisfying(org.eclipse.paho.mqttv5.common.MqttException.class, + e -> assertThat(e.getReasonCode()).isEqualTo(MQTT5_NOT_AUTHORIZED)); + } + } + + private Session open(Mqtt version, Target target) + throws ConnectRefused, GeneralSecurityException, InterruptedException { + Long last = lastConnectNanos.get(target.clientId()); + if (last != null) { + long waitNanos = last + CONNECT_SPACING_NANOS - System.nanoTime(); + if (waitNanos > 0) { + TimeUnit.NANOSECONDS.sleep(waitNanos); + } + } + lastConnectNanos.put(target.clientId(), System.nanoTime()); + return version == Mqtt.V3_1_1 ? openV3(target) : openV5(target); + } + + private Session openV3(Target target) throws ConnectRefused, GeneralSecurityException { + MqttClient client; + try { + client = new MqttClient(target.uri(), target.clientId(), new MemoryPersistence()); + } catch (MqttException e) { + throw new IllegalStateException(e); + } + MqttConnectOptions options = new MqttConnectOptions(); + options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); + options.setCleanSession(true); + options.setAutomaticReconnect(false); + options.setConnectionTimeout(TIMEOUT_SECONDS); + options.setKeepAliveInterval(30); + if (target.username() != null) { + options.setUserName(target.username()); + } + if (target.password() != null) { + options.setPassword(new String(target.password(), StandardCharsets.UTF_8).toCharArray()); + } + if (!target.headers().isEmpty()) { + Properties headers = new Properties(); + headers.putAll(target.headers()); + options.setCustomWebSocketHeaders(headers); + } + if (needsEmulatorTls(target)) { + options.setSocketFactory(emulatorSocketFactory()); + options.setHttpsHostnameVerificationEnabled(false); + } + try { + client.connect(options); + } catch (MqttException e) { + closeV3(client); + throw new ConnectRefused(e); + } + return new Session() { + @Override + public String roundTrip(String topic, String payload) throws Exception { + CompletableFuture received = new CompletableFuture<>(); + client.subscribe(topic, 1, (t, message) -> + received.complete(new String(message.getPayload(), StandardCharsets.UTF_8))); + client.publish(topic, payload.getBytes(StandardCharsets.UTF_8), 1, false); + return received.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void close() { + closeV3(client); + } + }; + } + + private static void closeV3(MqttClient client) { + try { + if (client.isConnected()) { + client.disconnect(5000); + } + } catch (MqttException e) { + LOG.debugv("MQTT 3.1.1 disconnect failed: {0}", e.toString()); + } + try { + client.close(); + } catch (MqttException e) { + LOG.debugv("MQTT 3.1.1 close failed: {0}", e.toString()); + } + } + + private Session openV5(Target target) throws ConnectRefused, GeneralSecurityException { + MqttAsyncClient client; + try { + client = new MqttAsyncClient(target.uri(), target.clientId(), + new org.eclipse.paho.mqttv5.client.persist.MemoryPersistence()); + } catch (org.eclipse.paho.mqttv5.common.MqttException e) { + throw new IllegalStateException(e); + } + BlockingQueue messages = new LinkedBlockingQueue<>(); + client.setCallback(new MessageCallback(message -> messages.add(message))); + MqttConnectionOptions options = new MqttConnectionOptions(); + options.setCleanStart(true); + options.setAutomaticReconnect(false); + options.setConnectionTimeout(TIMEOUT_SECONDS); + options.setKeepAliveInterval(30); + if (target.username() != null) { + options.setUserName(target.username()); + } + if (target.password() != null) { + options.setPassword(target.password()); + } + if (!target.headers().isEmpty()) { + options.setCustomWebSocketHeaders(target.headers()); + } + if (needsEmulatorTls(target)) { + options.setSocketFactory(emulatorSocketFactory()); + options.setHttpsHostnameVerificationEnabled(false); + } + try { + client.connect(options).waitForCompletion(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS)); + } catch (org.eclipse.paho.mqttv5.common.MqttException e) { + closeV5(client); + throw new ConnectRefused(e); + } + if (!client.isConnected()) { + closeV5(client); + throw new ConnectRefused(new IllegalStateException("CONNECT did not complete")); + } + return new Session() { + @Override + public String roundTrip(String topic, String payload) throws Exception { + client.subscribe(topic, 1).waitForCompletion(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS)); + client.publish(topic, payload.getBytes(StandardCharsets.UTF_8), 1, false) + .waitForCompletion(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS)); + return messages.poll(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void close() { + closeV5(client); + } + }; + } + + private static void closeV5(MqttAsyncClient client) { + try { + if (client.isConnected()) { + client.disconnect().waitForCompletion(5000); + } + } catch (org.eclipse.paho.mqttv5.common.MqttException e) { + LOG.debugv("MQTT 5 disconnect failed: {0}", e.toString()); + } + try { + client.close(); + } catch (org.eclipse.paho.mqttv5.common.MqttException e) { + LOG.debugv("MQTT 5 close failed: {0}", e.toString()); + } + } + + private static final class MessageCallback implements MqttCallback { + private final Consumer sink; + + MessageCallback(Consumer sink) { + this.sink = sink; + } + + @Override + public void disconnected(MqttDisconnectResponse response) { + LOG.debugv("MQTT 5 disconnected: {0}", response); + } + + @Override + public void mqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception) { + LOG.debugv("MQTT 5 error: {0}", exception.toString()); + } + + @Override + public void messageArrived(String topic, MqttMessage message) { + sink.accept(new String(message.getPayload(), StandardCharsets.UTF_8)); + } + + @Override + public void deliveryComplete(IMqttToken token) { + } + + @Override + public void connectComplete(boolean reconnect, String serverUri) { + } + + @Override + public void authPacketArrived(int reasonCode, MqttProperties properties) { + } + } + + private static boolean needsEmulatorTls(Target target) { + return !TestFixtures.isRealAws() && target.uri().startsWith("wss://"); + } + + /** Floci's local TLS endpoint uses a test CA, so the emulator-only WebSocket trusts it like emulatorHttpClient. */ + private static SSLSocketFactory emulatorSocketFactory() throws GeneralSecurityException { + X509TrustManager trustAll = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, new TrustManager[] {trustAll}, new SecureRandom()); + return context.getSocketFactory(); + } +} diff --git a/docs/services/index.md b/docs/services/index.md index 9ddb0ed5f7..bbd98874c8 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -142,7 +142,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [BCM Data Exports](bcm-data-exports.md) | `POST /` + `X-Amz-Target: AWSBillingAndCostManagementDataExports.*` | JSON 1.1 | 7 | | [Transfer Family](transfer.md) | `POST /` + `X-Amz-Target: TransferService.*` | JSON 1.1 | 17 | | [DataSync](datasync.md) | `POST /` + `X-Amz-Target: FmrsService.*` | JSON 1.1 | 48 | -| [IoT Core](iot.md) | `/things/...`, `/endpoint`, rules/policies REST paths | REST JSON | 70 | +| [IoT Core](iot.md) | `/things/...`, `/endpoint`, rules/policies REST paths | REST JSON | 71 | | [IoT Data](iot.md) | `/things/{thingName}/shadow`, MQTT topics | REST JSON | 11 | **Lambda, ElastiCache, RDS, MSK, MWAA, ECS, EKS, and OpenSearch** spin up real Docker containers and support IAM authentication and SigV4 request signing, the same auth flow as production AWS. **RDS Data API** executes SQL against the local RDS containers through AWS-compatible REST JSON routes. diff --git a/docs/services/iot.md b/docs/services/iot.md index c67973142f..2b1fe5c255 100644 --- a/docs/services/iot.md +++ b/docs/services/iot.md @@ -69,11 +69,11 @@ Current limitations: ## Authorizers -Status: control plane only. +Status: control plane, `TestInvokeAuthorizer` and MQTT `CONNECT` enforcement. -`CreateAuthorizer`, `DescribeAuthorizer`, `UpdateAuthorizer`, `DeleteAuthorizer`, `ListAuthorizers`, `SetDefaultAuthorizer`, `DescribeDefaultAuthorizer` and `ClearDefaultAuthorizer` are served on the REST-JSON paths the AWS SDKs use, with the shapes, error codes and messages measured against AWS: +`CreateAuthorizer`, `DescribeAuthorizer`, `UpdateAuthorizer`, `DeleteAuthorizer`, `ListAuthorizers`, `SetDefaultAuthorizer`, `DescribeDefaultAuthorizer`, `ClearDefaultAuthorizer` and `TestInvokeAuthorizer` are served on the REST-JSON paths the AWS SDKs use, with the shapes, error codes and messages measured against AWS: -- A new authorizer is `INACTIVE` with HTTP caching off unless the request says otherwise. Unless `signingDisabled` is `true`, it needs a `tokenKeyName` and one or two RSA-2048 public keys in PEM form; a signing-disabled authorizer takes no keys. The function ARN is checked for ARN syntax only and is never resolved. +- A new authorizer is `INACTIVE` with HTTP caching off unless the request says otherwise. Unless `signingDisabled` is `true`, it needs a `tokenKeyName` and one or two RSA-2048 public keys in PEM form; a signing-disabled authorizer takes no keys. The function ARN is checked for ARN syntax only; the function is resolved when it is invoked. - `DescribeAuthorizer` returns every member; an unset token key name or key map is JSON `null`, and tags are not shown. - `UpdateAuthorizer` changes only the members sent and merges signing keys by key name, refusing a third key. Any member sent moves `lastModifiedDate`; an empty body does not. - `DeleteAuthorizer` refuses an `ACTIVE` authorizer (`InvalidRequestException`) and the default authorizer (`DeleteConflictException`). @@ -82,9 +82,25 @@ Status: control plane only. - Tags work through `TagResource`, `UntagResource` and `ListTagsForResource` on the authorizer ARN. - CloudFormation provisions `AWS::IoT::Authorizer`: `Ref` is the name and `Fn::GetAtt Arn` the ARN. +The function is invoked synchronously with the event AWS IoT sends (`protocolData`, `protocols`, `token` for a signed authorizer, `signatureVerified`, `connectionMetadata`), and its answer is checked as AWS checks it: `isAuthenticated`, a `principalId` of 1 to 128 letters and digits, at most 10 `policyDocuments` of at most 2048 characters each that parse as JSON objects, and TTLs from 300 to 86400 seconds. Omitted TTLs become 300 (`refreshAfterInSeconds`) and 86400 (`disconnectAfterInSeconds`), and a policy returned as a JSON object becomes one compact string. A signed authorizer verifies the token's SHA256withRSA signature against each of its keys before the function is invoked. + +- `TestInvokeAuthorizer` works on `ACTIVE` and `INACTIVE` authorizers. It returns the request errors (`InvalidRequestException`) and answer errors (`InvalidResponseException`) with AWS's messages, and passes the contexts on as given: header case is kept and `queryString` keeps its leading `?`. +- An MQTT `CONNECT` on the plaintext listener or on [MQTT over WebSocket](#mqtt-over-websocket) that names an authorizer, in the username (`?x-amz-customauthorizer-name=`), the WebSocket URL query or the `x-amz-customauthorizer-name` upgrade header, is admitted only when the authorizer exists and is `ACTIVE`, the signature verifies for a signed authorizer, the answer is valid, `isAuthenticated` is `true`, and the returned policies allow `iot:Connect` on `arn:aws:iot:::client/`. The event carries the username with its query, the password in base64, and for WebSocket the lower-cased upgrade headers, the query string without `?` and, over TLS, the server name. +- A refused MQTT 3.1.1 client is disconnected without a `CONNACK`; an MQTT 5 client gets `CONNACK` reason code `0x87` with the reason string AWS sends, then the connection closes. A `CONNECT` that names no authorizer is handled as before. + Current limitations: -- Authorizers are not evaluated: no connection or HTTP publish invokes the function, and `TestInvokeAuthorizer` is not served (see [MQTT over WebSocket](#mqtt-over-websocket)). +- The default authorizer is not used: a `CONNECT` that names no authorizer is not authenticated. +- HTTPS publish does not invoke authorizers, and `enableCachingForHttp` has no effect. +- `refreshAfterInSeconds` and `disconnectAfterInSeconds` are returned but do not schedule a re-invocation or a disconnect. +- The returned policies decide `iot:Connect` only; publish, subscribe and receive are not checked against them. +- The function's resource policy is not checked, so `AddPermission` for `iot.amazonaws.com` is not required. +- A WebSocket upgrade without the `mqtt` subprotocol is not answered with AWS's 426. +- A refused WebSocket client sees a close frame with status `1000`. AWS sends an empty close frame to an MQTT 3.1.1 client and closes the connection without a close frame after the MQTT 5 `CONNACK`. +- An accepting MQTT 5 `CONNACK` carries no properties. +- The MQTT over TLS listener (8883) authenticates by device certificate only. +- On a connection, the authorizer is looked up in Floci's default account and region. +- The MQTT password reaches the function as the broker decodes it, as UTF-8 text, so password bytes that are not valid UTF-8 arrive altered. The broker library, Vert.x MQTT, exposes the password only as a string. ## MQTT Broker @@ -99,7 +115,7 @@ Broker scope: - Support QoS 0 and QoS 1 publish/subscribe behavior for the local AWS IoT slice. - Accept PUBLISH payloads up to 128 KB on 1883, 8883 and `/mqtt`, the AWS IoT Core quota (packets up to 146 KB including the variable header). A larger publish disconnects the client without acknowledgement, delivery or rule evaluation, as on AWS. - Serve MQTT over TLS on 8883 next to plaintext 1883 when TLS is enabled, and verify the device certificate and its `iot:Connect` permission there. -- Keep the plaintext listener permissive; topic-level authorization (`Publish`, `Subscribe`, `Receive`) is follow-up scope. +- Keep the plaintext listener permissive except for a `CONNECT` that names a [custom authorizer](#authorizers); topic-level authorization (`Publish`, `Subscribe`, `Receive`) is follow-up scope. - Keep MQTT broker logging minimal. - Validate the relevant IoT compatibility tests against the native binary before considering the phase complete. @@ -168,8 +184,10 @@ request to `/mqtt` gets the usual 404. No client certificate is requested on this path. On AWS it authenticates by SigV4 (signed query string on the upgrade URL) or by a custom authorizer (`?x-amz-customauthorizer-name=` -as the MQTT username with a token as the password); Floci accepts such a CONNECT as is and does -not evaluate the signature or the authorizer yet. +as the MQTT username with a token as the password). Floci enforces a custom authorizer named in the +username, the URL query or the `x-amz-customauthorizer-name` upgrade header, see +[Authorizers](#authorizers); it does not verify a SigV4 signature, and a CONNECT that names no +authorizer is accepted as is. ## Reserved Topics diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java index 930458b68b..d519745bed 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java @@ -1,8 +1,10 @@ package io.github.hectorvent.floci.services.iot; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; @@ -26,7 +28,8 @@ import jakarta.ws.rs.core.Response; /** * REST-JSON routes for AWS IoT custom authorizers: CreateAuthorizer, DescribeAuthorizer, * UpdateAuthorizer, DeleteAuthorizer, ListAuthorizers, SetDefaultAuthorizer, - * DescribeDefaultAuthorizer and ClearDefaultAuthorizer, on the paths and shapes the AWS SDKs use. + * DescribeDefaultAuthorizer, ClearDefaultAuthorizer and TestInvokeAuthorizer, on the paths and + * shapes the AWS SDKs use. */ @Path("/") @Produces(MediaType.APPLICATION_JSON) @@ -34,15 +37,19 @@ import jakarta.ws.rs.core.Response; public class IotAuthorizerController { private final IotAuthorizerService authorizerService; + private final IotCustomAuthorizer customAuthorizer; private final RegionResolver regionResolver; private final ObjectMapper objectMapper; + private final ObjectReader strictReader; @Inject - public IotAuthorizerController(IotAuthorizerService authorizerService, RegionResolver regionResolver, - ObjectMapper objectMapper) { + public IotAuthorizerController(IotAuthorizerService authorizerService, IotCustomAuthorizer customAuthorizer, + RegionResolver regionResolver, ObjectMapper objectMapper) { this.authorizerService = authorizerService; + this.customAuthorizer = customAuthorizer; this.regionResolver = regionResolver; this.objectMapper = objectMapper; + this.strictReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); } @POST @@ -75,6 +82,14 @@ public class IotAuthorizerController { return Response.ok(objectMapper.createObjectNode()).build(); } + @POST + @Path("/authorizer/{authorizerName}/test") + public Response testInvokeAuthorizer(@Context HttpHeaders headers, @PathParam("authorizerName") String authorizerName, + String body) { + return Response.ok(customAuthorizer.testInvoke(authorizerName, readJson(body), regionResolver.resolveRegion(headers))) + .build(); + } + @GET @Path("/authorizers") public Response listAuthorizers(@Context HttpHeaders headers, @@ -138,7 +153,7 @@ public class IotAuthorizerController { private JsonNode readJson(String body) { try { - return objectMapper.readTree(body == null || body.isBlank() ? "{}" : body); + return strictReader.readTree(body == null || body.isBlank() ? "{}" : body); } catch (JsonProcessingException e) { throw new AwsException("InvalidRequestException", e.getMessage(), 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerService.java index 50fd374997..295e90f02f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerService.java @@ -31,8 +31,8 @@ import java.util.stream.Collectors; /** * AWS IoT Core custom authorizers: the records CreateAuthorizer and its peers manage, and the - * account's default authorizer. The function ARN is checked for ARN syntax only, as on AWS; nothing - * here invokes the function or gates a connection. + * account's default authorizer. The function ARN is checked for ARN syntax only, as on AWS; + * {@link IotCustomAuthorizer} invokes the function. */ @ApplicationScoped public class IotAuthorizerService { @@ -333,8 +333,7 @@ public class IotAuthorizerService { } RSAPublicKey key; try { - byte[] der = Base64.getDecoder().decode(String.join("", rest.subList(0, end))); - key = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(der)); + key = rsaPublicKey(pem); } catch (GeneralSecurityException | IllegalArgumentException e) { throw invalid("Authorizer " + name + " public key for key name " + keyName + " not a valid RSA key"); } @@ -345,6 +344,14 @@ public class IotAuthorizerService { } } + /** The key of a PEM whose header and end lines {@link #requireRsa2048} has checked. */ + static RSAPublicKey rsaPublicKey(String pem) throws GeneralSecurityException { + List lines = pem.lines().toList(); + List body = lines.subList(lines.indexOf("-----BEGIN PUBLIC KEY-----") + 1, lines.size()); + byte[] der = Base64.getDecoder().decode(String.join("", body.subList(0, body.indexOf("-----END PUBLIC KEY-----")))); + return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(der)); + } + private static void requireFunctionArn(String name, String functionArn) { if (AwsArnUtils.resourceIfArnFor(functionArn, "lambda").filter(r -> r.startsWith("function:")).isEmpty()) { throw invalid("Lambda function arn for authorizer " + name + " is not in proper ARN syntax"); @@ -380,7 +387,7 @@ public class IotAuthorizerService { Map.of("resourceId", authorizer.getAuthorizerName(), "resourceArn", authorizer.getAuthorizerArn())); } - private static AwsException constraint(String field, String constraint) { + static AwsException constraint(String field, String constraint) { return constraints(field, List.of(constraint)); } @@ -391,7 +398,7 @@ public class IotAuthorizerService { .collect(Collectors.joining("; "))); } - private static AwsException invalid(String message) { + static AwsException invalid(String message) { return new AwsException("InvalidRequestException", message, 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizer.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizer.java new file mode 100644 index 0000000000..a6c2fb21b3 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizer.java @@ -0,0 +1,330 @@ +package io.github.hectorvent.floci.services.iot; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.MissingNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.services.iot.model.IotAuthorizer; +import io.github.hectorvent.floci.services.lambda.LambdaService; +import io.github.hectorvent.floci.services.lambda.model.InvocationType; +import io.github.hectorvent.floci.services.lambda.model.InvokeResult; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + +import java.io.IOException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.Signature; +import java.util.ArrayList; +import java.util.Base64; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.UUID; +import java.util.regex.Pattern; + +/** + * AWS IoT custom authentication: the event AWS IoT sends an authorizer's function, the invocation, + * and the checks AWS makes on the function's answer, in AWS's order and wording. TestInvokeAuthorizer + * and the MQTT CONNECT on the broker's plaintext listener, which the WebSocket bridge lands on, share + * it. + */ +@ApplicationScoped +public class IotCustomAuthorizer { + + private static final Logger LOG = Logger.getLogger(IotCustomAuthorizer.class); + static final String NAME_PARAM = "x-amz-customauthorizer-name"; + static final String SIGNATURE_PARAM = "x-amz-customauthorizer-signature"; + private static final Pattern BASE64 = Pattern.compile("[A-Za-z0-9+/]+={0,2}"); + private static final Pattern PRINCIPAL_ID = Pattern.compile("([a-zA-Z0-9]){1,128}"); + private static final String POLICIES = "policyDocuments failed to satisfy constraint: "; + private static final String TTL_RANGE = " failed to satisfy constraint: Member must be greater than 5 minutes and less than 24 hours."; + + private final IotAuthorizerService authorizers; + private final LambdaService lambdaService; + private final ObjectMapper objectMapper; + private final ObjectReader strictReader; + private final RegionResolver regionResolver; + + /** + * What a WebSocket upgrade carried: the header names lower-cased, the raw query without its + * {@code ?} ({@code ""} when there was none), the server name, null unless it came over TLS, and + * the client's IP address, as the broker sees only the bridge's loopback socket. + */ + record WebSocketUpgrade(Map headers, String queryString, String serverName, String sourceIp) { + } + + @Inject + public IotCustomAuthorizer(IotAuthorizerService authorizers, LambdaService lambdaService, ObjectMapper objectMapper, + RegionResolver regionResolver) { + this.authorizers = authorizers; + this.lambdaService = lambdaService; + this.objectMapper = objectMapper; + this.strictReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); + this.regionResolver = regionResolver; + } + + /** TestInvokeAuthorizer: the request checks AWS makes before invoking, then the function's checked answer. */ + public ObjectNode testInvoke(String name, JsonNode request, String region) { + String token = request.path("token").asText(null); + String signature = request.path("tokenSignature").asText(null); + String queryString = request.path("httpContext").path("queryString").asText(null); + if ("".equals(token)) { + throw IotAuthorizerService.constraint("token", "Member must have length greater than or equal to 1"); + } + if (token != null && token.length() > 6144) { + throw IotAuthorizerService.constraint("token", "Member must have length less than or equal to 6144"); + } + if (signature != null && !BASE64.matcher(signature).matches()) { + throw IotAuthorizerService.constraint("tokenSignature", "Member must satisfy regular expression pattern: " + BASE64); + } + if (signature != null && signature.length() > 2560) { + throw IotAuthorizerService.constraint("tokenSignature", "Member must have length less than or equal to 2560"); + } + if ("".equals(queryString)) { + throw IotAuthorizerService.constraint("httpContext.queryString", "Member must have length greater than or equal to 1"); + } + IotAuthorizer authorizer = authorizers.describeAuthorizer(name, region); + ObjectNode protocolData = objectMapper.createObjectNode(); + for (String protocol : List.of("tls", "mqtt", "http")) { + JsonNode context = request.path(protocol + "Context"); + if (context.isObject()) { + // ponytail: a context goes to the function as sent, members in request order, which SDKs send in model order. + // Nested members are not checked against the model's length and type constraints, only those measured above. + protocolData.set(protocol, context); + } + } + if ((token == null) != (signature == null)) { + throw IotAuthorizerService.invalid("Request must provide both token and tokenSignature together."); + } + if (token != null) { + if (!protocolData.isEmpty()) { + throw IotAuthorizerService.invalid("Request cannot have token and Http/Mqtt/Tls context."); + } + if (authorizer.isSigningDisabled()) { + throw IotAuthorizerService.invalid("Token and token signature provided but signing is disabled for the authorizer."); + } + return invoke(authorizer, event(null, verifiedToken(authorizer, token, signature))); + } + if (protocolData.isEmpty()) { + throw IotAuthorizerService.invalid("Request must have Http/Mqtt/Tls context."); + } + if (queryString != null && !queryString.startsWith("?")) { + throw IotAuthorizerService.invalid("Invalid arguments in the call"); + } + Map headers = new LinkedHashMap<>(); + protocolData.path("http").path("headers").properties().forEach(header -> headers.put(header.getKey(), header.getValue().asText())); + Map params = params(protocolData.path("mqtt").path("username").asText(null), headers, queryString); + return invoke(authorizer, event(protocolData, signedToken(authorizer, params))); + } + + /** The authorizer an MQTT CONNECT names in its username query, the WebSocket URL query or an upgrade header; null when none. */ + static String authorizerName(String username, WebSocketUpgrade upgrade) { + return params(username, upgrade).get(NAME_PARAM); + } + + /** + * The policies an MQTT CONNECT through the named authorizer is admitted with. None when the + * authorizer is not ACTIVE, which never invokes the function, or when the function denies or + * returns no policy; an {@link AwsException} when the authorizer does not exist, a signature + * does not verify (the function is not invoked) or the answer is one TestInvokeAuthorizer rejects. + */ + List connectPolicies(String name, String clientId, String username, String password, WebSocketUpgrade upgrade) { + // ponytail: the broker has no request context, so a connection finds authorizers in the default account and region. + IotAuthorizer authorizer = authorizers.describeAuthorizer(name, regionResolver.getDefaultRegion()); + if (!"ACTIVE".equals(authorizer.getStatus())) { + return List.of(); + } + ObjectNode protocolData = objectMapper.createObjectNode(); + if (upgrade != null && upgrade.serverName() != null) { + protocolData.putObject("tls").put("serverName", upgrade.serverName()); + } + ObjectNode mqtt = protocolData.putObject("mqtt"); + if (username != null) { + mqtt.put("username", username); + } + if (password != null) { + // ponytail: Vert.x hands the password over as UTF-8 text, so bytes that are not UTF-8 reach the function altered. + mqtt.put("password", Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8))); + } + mqtt.put("clientId", clientId); + if (upgrade != null) { + ObjectNode http = protocolData.putObject("http"); + http.set("headers", objectMapper.valueToTree(upgrade.headers())); + http.put("queryString", upgrade.queryString()); + } + ObjectNode answer = invoke(authorizer, event(protocolData, signedToken(authorizer, params(username, upgrade)))); + if (!answer.path("isAuthenticated").booleanValue()) { + return List.of(); + } + List policies = new ArrayList<>(); + answer.path("policyDocuments").forEach(policy -> policies.add(policy.asText())); + return policies; + } + + private ObjectNode event(ObjectNode protocolData, String token) { + ObjectNode event = objectMapper.createObjectNode(); + if (protocolData != null) { + event.set("protocolData", protocolData); + ArrayNode protocols = event.putArray("protocols"); + for (String protocol : List.of("tls", "http", "mqtt")) { + if (protocolData.has(protocol)) { + protocols.add(protocol); + } + } + } + if (token != null) { + event.put("token", token); + } + event.put("signatureVerified", token != null); + event.putObject("connectionMetadata").put("id", UUID.randomUUID().toString()); + return event; + } + + /** Invokes the function and checks its answer as AWS does, returning the TestInvokeAuthorizer body. */ + private ObjectNode invoke(IotAuthorizer authorizer, ObjectNode event) { + InvokeResult result = lambdaService.invokeArn(authorizer.getAuthorizerFunctionArn(), + event.toString().getBytes(StandardCharsets.UTF_8), InvocationType.RequestResponse); + JsonNode answer = readJson(result.getPayload()); + if (result.getFunctionError() != null) { + throw invalidResponse("Lambda function encountered error in execution: " + answer.path("errorMessage").asText() + + ": " + answer.path("errorType").asText()); + } + if (!answer.path("isAuthenticated").isBoolean()) { + throw invalidResponse("Lambda invocation result is in invalid format"); + } + String principalId = answer.path("principalId").asText(null); + if (principalId == null || !PRINCIPAL_ID.matcher(principalId).matches()) { + throw invalidResponse(principalId + " at principalId failed to satisfy constraint: Member must satisfy " + PRINCIPAL_ID); + } + JsonNode policies = answer.path("policyDocuments"); + if (!policies.isArray()) { + throw invalidResponse(POLICIES + "List of policies cannot be null."); + } + if (policies.size() > 10) { + throw invalidResponse(POLICIES + "Number of policies allowed cannot exceed 10"); + } + ArrayNode documents = objectMapper.createArrayNode(); + for (JsonNode policy : policies) { + String document = policy.isTextual() ? policy.asText() : policy.toString(); + // ponytail: size counts characters and syntax means a JSON object; AWS's exact rules are unmeasured. + if (document.length() > 2048) { + throw invalidResponse(POLICIES + "Size of a policy document cannot exceeds hard limit of 2048"); + } + if (!readJson(document.getBytes(StandardCharsets.UTF_8)).isObject()) { + throw invalidResponse(POLICIES + "Policy document is Malformed: Invalid policy syntax."); + } + documents.add(document); + } + int refresh = ttl(answer, "refreshAfterInSeconds", 300); + int disconnect = ttl(answer, "disconnectAfterInSeconds", 86400); + ObjectNode response = objectMapper.createObjectNode(); + response.put("disconnectAfterInSeconds", disconnect); + response.put("isAuthenticated", answer.path("isAuthenticated").booleanValue()); + response.set("policyDocuments", documents); + response.put("principalId", principalId); + response.put("refreshAfterInSeconds", refresh); + return response; + } + + /** A TTL the function returned, or its default; AWS accepts 300 to 86400 inclusive whatever its message says. */ + private static int ttl(JsonNode answer, String field, int fallback) { + JsonNode value = answer.path(field); + if (value.isMissingNode() || value.isNull()) { + return fallback; + } + if (!value.canConvertToInt() || value.asInt() < 300 || value.asInt() > 86400) { + throw invalidResponse(value.asText() + " at " + field + TTL_RANGE); + } + return value.asInt(); + } + + private JsonNode readJson(byte[] json) { + try { + JsonNode node = strictReader.readTree(json); + return node == null ? MissingNode.getInstance() : node; + } catch (IOException | IllegalArgumentException ignored) { + // Not JSON: the checks that follow report the answer the way AWS does. + return MissingNode.getInstance(); + } + } + + /** The verified token of a signed authorizer, or null for one with signing disabled. */ + private static String signedToken(IotAuthorizer authorizer, Map params) { + return authorizer.isSigningDisabled() ? null + : verifiedToken(authorizer, params.get(authorizer.getTokenKeyName()), params.get(SIGNATURE_PARAM)); + } + + /** The token, once an SHA256withRSA signature over it verifies against one of the authorizer's keys. */ + private static String verifiedToken(IotAuthorizer authorizer, String token, String signature) { + if (token == null || signature == null) { + // AWS answers a signed authorizer without its token or signature with a null message. + throw new AwsException("InvalidRequestException", null, 400); + } + for (String pem : authorizer.getTokenSigningPublicKeys().values()) { + try { + Signature verifier = Signature.getInstance("SHA256withRSA"); + verifier.initVerify(IotAuthorizerService.rsaPublicKey(pem)); + verifier.update(token.getBytes(StandardCharsets.UTF_8)); + if (verifier.verify(Base64.getDecoder().decode(signature))) { + return token; + } + } catch (GeneralSecurityException | IllegalArgumentException e) { + LOG.debugv("Token signature for authorizer {0} did not verify: {1}", authorizer.getAuthorizerName(), e.getMessage()); + } + } + throw IotAuthorizerService.invalid("Token signature mismatch for authorizer " + authorizer.getAuthorizerName()); + } + + private static Map params(String username, WebSocketUpgrade upgrade) { + return upgrade == null ? params(username, Map.of(), null) : params(username, upgrade.headers(), upgrade.queryString()); + } + + /** + * The parameters a request carries for custom authentication: the query on the MQTT username, + * then the query string, then the headers, the first one naming a parameter winning. + */ + private static Map params(String username, Map headers, String queryString) { + // ponytail: names match case-insensitively in every source; precedence between sources is unmeasured on AWS. + Map params = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + int usernameQuery = username == null ? -1 : username.indexOf('?'); + if (usernameQuery >= 0) { + addQuery(params, username.substring(usernameQuery + 1)); + } + if (queryString != null) { + addQuery(params, queryString.startsWith("?") ? queryString.substring(1) : queryString); + } + headers.forEach(params::putIfAbsent); + return params; + } + + private static void addQuery(Map params, String query) { + for (String pair : query.split("&")) { + int equals = pair.indexOf('='); + if (equals > 0) { + params.putIfAbsent(urlDecoded(pair.substring(0, equals)), urlDecoded(pair.substring(equals + 1))); + } + } + } + + private static String urlDecoded(String value) { + try { + return URLDecoder.decode(value, StandardCharsets.UTF_8); + } catch (IllegalArgumentException ignored) { + // A malformed escape is kept as sent: decoding it must not fail a CONNECT that names no authorizer. + return value; + } + } + + private static AwsException invalidResponse(String message) { + return new AwsException("InvalidResponseException", message, 400); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java index c6330539bc..4d37b702e0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.Resettable; import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage; import io.netty.handler.codec.mqtt.MqttConnectReturnCode; +import io.netty.handler.codec.mqtt.MqttProperties; import io.netty.handler.codec.mqtt.MqttQoS; import io.quarkus.runtime.ShutdownEvent; import io.quarkus.runtime.StartupEvent; @@ -49,6 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -118,6 +120,7 @@ public class IotMqttBrokerService implements Resettable { private final EmulatorConfig config; private final Vertx vertx; private final Instance iotService; + private final IotCustomAuthorizer customAuthorizer; private final TlsConfigurationRegistry tlsRegistry; final WorkerExecutor ruleWorker; private final Map sessionsByClient = new ConcurrentHashMap<>(); @@ -130,16 +133,18 @@ public class IotMqttBrokerService implements Resettable { * the one it was admitted under. */ private final AtomicLong ruleState = new AtomicLong(); + private final Map webSocketUpgrades = new ConcurrentHashMap<>(); private MqttServer server; private MqttServer tlsServer; private ReloadingKeyManager keyManager; @Inject public IotMqttBrokerService(EmulatorConfig config, Vertx vertx, Instance iotService, - TlsConfigurationRegistry tlsRegistry) { + IotCustomAuthorizer customAuthorizer, TlsConfigurationRegistry tlsRegistry) { this.config = config; this.vertx = vertx; this.iotService = iotService; + this.customAuthorizer = customAuthorizer; this.tlsRegistry = tlsRegistry; this.ruleWorker = vertx.createSharedWorkerExecutor("floci-iot-mqtt-rules", 1); } @@ -325,6 +330,15 @@ public class IotMqttBrokerService implements Resettable { ruleState.updateAndGet(state -> (state & reason) == 0 ? state : (state & ~reason) + RULE_STATE_STEP); } + /** + * The WebSocket bridge's upgrades, keyed by the address its connection to the plaintext + * listener comes from: put before the first frame is piped, taken by that connection's CONNECT, + * dropped when the bridge session ends. A method, as the bridge holds a client proxy. + */ + Map webSocketUpgrades() { + return webSocketUpgrades; + } + public synchronized boolean isRunning() { return server != null; } @@ -368,7 +382,7 @@ public class IotMqttBrokerService implements Resettable { * 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. + * every client except one that names a custom authorizer, see {@link #authorizeCustom}. */ void handleEndpoint(MqttEndpoint endpoint, boolean verifyDevice) { String clientId = endpoint.clientIdentifier(); @@ -383,7 +397,60 @@ public class IotMqttBrokerService implements Resettable { : MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED); return; } + } else { + IotCustomAuthorizer.WebSocketUpgrade upgrade = remoteAddress == null ? null : webSocketUpgrades.remove(remoteAddress); + String username = endpoint.auth() == null ? null : endpoint.auth().getUsername(); + String authorizerName = IotCustomAuthorizer.authorizerName(username, upgrade); + if (authorizerName != null) { + authorizeCustom(endpoint, authorizerName, upgrade, upgrade == null ? sourceIp : upgrade.sourceIp()); + return; + } } + admit(endpoint, principal); + } + + /** + * A CONNECT naming a custom authorizer is admitted only when the authorizer's function allows + * it and the policies it returns allow {@code iot:Connect} for the client id. The function runs + * off the event loop. A refusal closes an MQTT 3.1.1 connection without a CONNACK and answers + * MQTT 5 with reason code 0x87, as AWS IoT does. + */ + private void authorizeCustom(MqttEndpoint endpoint, String authorizerName, IotCustomAuthorizer.WebSocketUpgrade upgrade, + String sourceIp) { + String clientId = endpoint.clientIdentifier(); + String username = endpoint.auth() == null ? null : endpoint.auth().getUsername(); + String password = endpoint.auth() == null ? null : endpoint.auth().getPassword(); + String serverName = upgrade == null ? null : upgrade.serverName(); + AtomicBoolean closed = new AtomicBoolean(); + endpoint.closeHandler(ignored -> closed.set(true)); + vertx.executeBlocking(() -> { + List policies = customAuthorizer.connectPolicies(authorizerName, clientId, username, password, upgrade); + return iotService.get().isConnectAllowedBy(policies, clientId, sourceIp, serverName); + }, false).onComplete(decision -> { + if (closed.get()) { + return; + } + if (decision.succeeded() && decision.result()) { + admit(endpoint, null); + return; + } + LOG.debugv("IoT MQTT client {0} refused by custom authorizer {1}: {2}", clientId, authorizerName, + decision.failed() ? decision.cause().getMessage() : "iot:Connect is not allowed"); + if (endpoint.protocolVersion() != 5) { + endpoint.close(); + return; + } + MqttProperties properties = new MqttProperties(); + properties.add(new MqttProperties.StringProperty(MqttProperties.MqttPropertyType.REASON_STRING.value(), + "CONNACK:Client is not authenticated/authorized to send the message:" + UUID.randomUUID())); + endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED_5, properties); + }); + } + + private void admit(MqttEndpoint endpoint, String principal) { + String clientId = endpoint.clientIdentifier(); + SocketAddress remoteAddress = endpoint.remoteAddress(); + String sourceIp = remoteAddress == null ? null : remoteAddress.host(); ClientSession session = new ClientSession( clientId, endpoint, @@ -425,7 +492,7 @@ public class IotMqttBrokerService implements Resettable { return null; } String certificateArn = device.get().certificate().getCertificateArn(); - if (!iotService.get().isConnectAllowed(device.get(), clientId, sourceIp, requestedServerName(endpoint))) { + if (!iotService.get().isConnectAllowed(device.get(), clientId, sourceIp, requestedServerName(endpoint.sslSession()))) { LOG.debugv("IoT MQTT TLS client {0} refused: iot:Connect is not allowed for {1}", clientId, certificateArn); return null; } @@ -460,8 +527,8 @@ public class IotMqttBrokerService implements Resettable { * {@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)) { + static String requestedServerName(SSLSession sslSession) { + if (!(sslSession instanceof ExtendedSSLSession session)) { return null; } return session.getRequestedServerNames().stream() diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridge.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridge.java index 39af9d3d10..8e33a22b9b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridge.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridge.java @@ -5,6 +5,7 @@ import io.vertx.core.Vertx; import io.vertx.core.http.ServerWebSocket; import io.vertx.core.net.NetClient; import io.vertx.core.net.NetSocket; +import io.vertx.core.net.SocketAddress; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import jakarta.enterprise.context.ApplicationScoped; @@ -12,6 +13,9 @@ import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -19,8 +23,10 @@ import java.util.concurrent.atomic.AtomicBoolean; * IoT uses for browser clients, the Device SDK's WebSocket transport and secure tunnelling. Frames * are piped byte for byte to the broker's plaintext listener, so every broker feature and every * authorization rule applies without a second code path. No client certificate is involved: on - * AWS this path authenticates by SigV4 or a custom authorizer, and the broker stays permissive - * for it. + * AWS this path authenticates by SigV4 or a custom authorizer. The broker enforces a custom + * authorizer named in the MQTT username, the URL query or an upgrade header, and stays permissive + * otherwise; the upgrade's headers, query, server name and client address reach it through + * {@link IotMqttBrokerService#webSocketUpgrades()}. * *

Everything for one session runs on the event loop of the WebSocket's connection, the * broker socket included, so the handlers below never race each other. @@ -92,7 +98,12 @@ public class IotMqttWebSocketBridge { socket.close(); return; } - new BridgeSession(ws, socket).start(); + // Registered before start() lets the first frame through, so it is there when the CONNECT + // reaches the broker; removed only if still this very instance, as the address can be reused. + SocketAddress bridgeAddress = socket.localAddress(); + IotCustomAuthorizer.WebSocketUpgrade upgrade = upgradeOf(ws); + broker.webSocketUpgrades().put(bridgeAddress, upgrade); + new BridgeSession(ws, socket, forgetting(broker.webSocketUpgrades(), bridgeAddress, upgrade)).start(); }) .onFailure(error -> { LOG.warnv("MQTT WebSocket bridge could not reach the broker on {0}:{1}: {2}", @@ -101,15 +112,43 @@ public class IotMqttWebSocketBridge { }); } + /** Drops the session's upgrade unless the address already carries a newer session's. */ + static Runnable forgetting(Map upgrades, SocketAddress address, + IotCustomAuthorizer.WebSocketUpgrade upgrade) { + return () -> upgrades.computeIfPresent(address, (key, current) -> current == upgrade ? null : current); + } + + /** + * The upgrade as AWS hands it to a custom authorizer; the server name only over TLS, from SNI or + * else the Host header. The client's address rides along for the broker's policy evaluation. + */ + private static IotCustomAuthorizer.WebSocketUpgrade upgradeOf(ServerWebSocket ws) { + Map headers = new LinkedHashMap<>(); + // ponytail: a repeated header keeps its last value. + ws.headers().forEach(header -> headers.put(header.getKey().toLowerCase(Locale.ROOT), header.getValue())); + String serverName = null; + if (ws.isSsl()) { + serverName = IotMqttBrokerService.requestedServerName(ws.sslSession()); + if (serverName == null && ws.authority() != null && !IotMqttBrokerService.isAddressLiteral(ws.authority().host())) { + serverName = ws.authority().host(); + } + } + SocketAddress client = ws.remoteAddress(); + return new IotCustomAuthorizer.WebSocketUpgrade(headers, ws.query() == null ? "" : ws.query(), serverName, + client == null ? null : client.host()); + } + static final class BridgeSession { private final ServerWebSocket webSocket; private final NetSocket socket; + private final Runnable onClose; private final AtomicBoolean closed = new AtomicBoolean(); - BridgeSession(ServerWebSocket webSocket, NetSocket socket) { + BridgeSession(ServerWebSocket webSocket, NetSocket socket, Runnable onClose) { this.webSocket = webSocket; this.socket = socket; + this.onClose = onClose; } void start() { @@ -162,7 +201,7 @@ public class IotMqttWebSocketBridge { } private void close(short code, String reason) { - if (closed.compareAndSet(false, true)) { + if (markClosed()) { socket.close(); if (!webSocket.isClosed()) { webSocket.close(code, reason); @@ -171,16 +210,25 @@ public class IotMqttWebSocketBridge { } private void closeSocket() { - if (closed.compareAndSet(false, true)) { + if (markClosed()) { socket.close(); } } private void closeWebSocket() { - if (closed.compareAndSet(false, true) && !webSocket.isClosed()) { + if (markClosed() && !webSocket.isClosed()) { + // ponytail: on a custom-auth refusal AWS sends an empty close frame (MQTT 3.1.1) or no close frame (MQTT 5); Vert.x has no public API for either, so this sends status 1000. webSocket.close(); } } + + private boolean markClosed() { + if (!closed.compareAndSet(false, true)) { + return false; + } + onClose.run(); + return true; + } } /** The broker binds {@code 0.0.0.0} by default, which is not a connect target; loopback is. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java index 5cdeb5f322..e42fadecf6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java @@ -495,14 +495,30 @@ public class IotService { return false; } List documents = attachedPolicyDocuments(certificate.getCertificateArn(), device.region(), device.accountId()); + return isConnectAllowed(documents, connectionVariables(device, clientId, sourceIp, domainName), + device.region(), device.accountId(), clientId); + } + + /** + * Decides {@code iot:Connect} for a client a custom authorizer admitted, from the policies its + * function returned, with the basic connection variables: there is no certificate, so the thing + * variables do not resolve. + */ + public boolean isConnectAllowedBy(List documents, String clientId, String sourceIp, String domainName) { + // ponytail: the broker has no request context, so a custom-auth client connects to the default account and region. + return isConnectAllowed(documents, basicConnectionVariables(clientId, sourceIp, domainName), + regionResolver.getDefaultRegion(), regionResolver.getDefaultAccountId(), clientId); + } + + private boolean isConnectAllowed(List documents, Map variables, String region, String accountId, + String clientId) { if (documents.isEmpty()) { return false; } - Map variables = connectionVariables(device, clientId, sourceIp, domainName); List resolved = documents.stream().map(document -> resolvePolicyVariables(document, variables)).toList(); Map> 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(); + String clientArn = AwsArnUtils.Arn.of("iot", region, accountId, "client/" + clientId).toString(); return policyEvaluator.evaluate(CallerContext.of(resolved), null, "iot:Connect", clientArn, conditionContext) == IamPolicyEvaluator.Decision.ALLOW; } @@ -533,14 +549,7 @@ public class IotService { * the client id names a thing attached to the certificate, as on AWS. */ private Map connectionVariables(RegisteredDevice device, String clientId, String sourceIp, String domainName) { - Map variables = new LinkedHashMap<>(); - variables.put("iot:ClientId", clientId); - if (sourceIp != null) { - variables.put("aws:SourceIp", sourceIp); - } - if (domainName != null) { - variables.put("iot:DomainName", domainName); - } + Map variables = basicConnectionVariables(clientId, sourceIp, domainName); Optional thing = getForAccount(thingStore, device.accountId(), thingKey(device.region(), clientId)); boolean attached = thing.isPresent() && getForAccount(thingPrincipalStore, device.accountId(), thingPrincipalKey(device.region(), clientId)) @@ -556,6 +565,18 @@ public class IotService { return variables; } + private static Map basicConnectionVariables(String clientId, String sourceIp, String domainName) { + Map variables = new LinkedHashMap<>(); + variables.put("iot:ClientId", clientId); + if (sourceIp != null) { + variables.put("aws:SourceIp", sourceIp); + } + if (domainName != null) { + variables.put("iot:DomainName", domainName); + } + 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 diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java new file mode 100644 index 0000000000..3cc89bc079 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java @@ -0,0 +1,357 @@ +package io.github.hectorvent.floci.services.iot; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.lambda.LambdaService; +import io.github.hectorvent.floci.services.lambda.model.InvocationType; +import io.github.hectorvent.floci.services.lambda.model.InvokeResult; +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; +import org.eclipse.paho.mqttv5.client.MqttAsyncClient; +import org.eclipse.paho.mqttv5.client.MqttConnectionOptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Custom authorizers on the real routes and broker: {@code TestInvokeAuthorizer} on its REST-JSON + * path, and an MQTT CONNECT on the TCP listener or over WebSocket admitted or refused by the + * authorizer's answer the way AWS IoT does it (MQTT 3.1.1 closed without a CONNACK, MQTT 5 + * answered with reason 135). The function is mocked and decides by the MQTT password; what it + * received is asserted as the event. + */ +@QuarkusTest +@TestProfile(IotMqttWebSocketIntegrationTest.Profile.class) +class IotCustomAuthorizerIntegrationTest { + + private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:000000000000:function:it-custom-auth"; + private static final String NAME_PARAM = "x-amz-customauthorizer-name="; + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @ConfigProperty(name = "quarkus.http.test-port", defaultValue = "0") + int testHttpPort; + + @ConfigProperty(name = "quarkus.http.test-ssl-port", defaultValue = "0") + int testSslPort; + + @InjectMock + LambdaService lambdaService; + + private final BlockingQueue events = new LinkedBlockingQueue<>(); + private String authorizer; + + @BeforeEach + void functionDecidesByThePassword() { + when(lambdaService.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenAnswer(invocation -> { + JsonNode event = MAPPER.readTree((byte[]) invocation.getArgument(1)); + events.add(event); + JsonNode mqtt = event.path("protocolData").path("mqtt"); + String secret = new String(Base64.getDecoder().decode(mqtt.path("password").asText("")), + StandardCharsets.UTF_8); + String answer = switch (secret) { + case "allow" -> allow(mqtt.path("clientId").asText()); + case "garbage" -> "{\"foo\": 1}"; + default -> "{\"isAuthenticated\": false, \"principalId\": \"itprincipal\", \"policyDocuments\": []}"; + }; + return new InvokeResult(200, null, answer.getBytes(StandardCharsets.UTF_8), null, "request-id"); + }); + authorizer = "it-cauth-" + System.nanoTime(); + given().contentType("application/json") + .body("{\"authorizerFunctionArn\": \"" + FUNCTION_ARN + "\", \"signingDisabled\": true, \"status\": \"ACTIVE\"}") + .when().post("/authorizer/" + authorizer) + .then().statusCode(200); + } + + @Test + void testInvokeAuthorizerAnswersWithTheVerdictPoliciesAndDefaultedTtls() { + given().contentType("application/json") + .body("{\"mqttContext\": {\"username\": \"c1?" + NAME_PARAM + authorizer + "\", \"password\": \"" + + base64("allow") + "\", \"clientId\": \"c1\"}}") + .when().post("/authorizer/" + authorizer + "/test") + .then() + .statusCode(200) + .body(equalTo("{\"disconnectAfterInSeconds\":86400,\"isAuthenticated\":true,\"policyDocuments\":[" + + MAPPER.valueToTree(policy("c1")) + "],\"principalId\":\"itprincipal\",\"refreshAfterInSeconds\":300}")); + } + + @Test + void testInvokeAuthorizerReportsAnInvalidAnswerAsInvalidResponseException() { + given().contentType("application/json") + .body("{\"mqttContext\": {\"clientId\": \"c1\", \"password\": \"" + base64("garbage") + "\"}}") + .when().post("/authorizer/" + authorizer + "/test") + .then() + .statusCode(400) + .header("X-Amzn-ErrorType", "InvalidResponseException") + .body("message", equalTo("Lambda invocation result is in invalid format")); + } + + @Test + void testInvokeAuthorizerOnAMissingAuthorizerIsNotFound() { + given().contentType("application/json").body("{\"httpContext\": {}}") + .when().post("/authorizer/it-cauth-missing/test") + .then() + .statusCode(404) + .header("X-Amzn-ErrorType", "ResourceNotFoundException") + .body("message", equalTo("Authorizer it-cauth-missing not found")); + } + + @Test + void aTestInvokeBodyWithTrailingContentIsAnInvalidRequest() { + given().contentType("application/json").body("{\"mqttContext\": {}} trailing") + .when().post("/authorizer/" + authorizer + "/test") + .then() + .statusCode(400) + .header("X-Amzn-ErrorType", "InvalidRequestException"); + } + + @Test + void mqtt3AllowedConnectSendsTheWebSocketEventWithTheUsernameVerbatim() throws Exception { + String clientId = "cauth-v3-" + System.nanoTime(); + String username = clientId + "?" + NAME_PARAM + authorizer; + + connectV3(ws(""), clientId, username, "allow", Map.of()).disconnect(); + + JsonNode event = takeEvent(); + assertEquals(List.of("http", "mqtt"), protocols(event)); + JsonNode headers = event.path("protocolData").path("http").path("headers"); + headers.fieldNames().forEachRemaining(name -> assertEquals(name.toLowerCase(Locale.ROOT), name, "header names are lower-cased")); + assertTrue(headers.path("sec-websocket-protocol").asText().contains("mqtt"), headers.toString()); + assertEquals("", event.path("protocolData").path("http").path("queryString").asText(null)); + assertEquals(username, event.path("protocolData").path("mqtt").path("username").asText()); + assertEquals(base64("allow"), event.path("protocolData").path("mqtt").path("password").asText()); + assertEquals(clientId, event.path("protocolData").path("mqtt").path("clientId").asText()); + assertFalse(event.path("signatureVerified").asBoolean(true)); + } + + @Test + void mqtt3RefusedConnectIsClosedWithoutAConnack() { + String clientId = "cauth-v3-deny-" + System.nanoTime(); + + MqttException refused = assertThrows(MqttException.class, + () -> connectV3(ws(""), clientId, clientId + "?" + NAME_PARAM + authorizer, "deny", Map.of())); + + assertEquals(MqttException.REASON_CODE_CONNECTION_LOST, refused.getReasonCode()); + } + + @Test + void mqtt3ConnectForAClientIdThePolicyDoesNotCoverIsRefused() { + String clientId = "cauth-v3-other-" + System.nanoTime(); + when(lambdaService.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenReturn(new InvokeResult(200, null, allow("someone-else").getBytes(StandardCharsets.UTF_8), null, "id")); + + MqttException refused = assertThrows(MqttException.class, + () -> connectV3(ws(""), clientId, clientId + "?" + NAME_PARAM + authorizer, "allow", Map.of())); + + assertEquals(MqttException.REASON_CODE_CONNECTION_LOST, refused.getReasonCode()); + } + + @Test + void mqtt3ConnectNamingAnUnknownAuthorizerIsRefusedWithoutInvokingAnyFunction() { + String clientId = "cauth-v3-unknown-" + System.nanoTime(); + + MqttException refused = assertThrows(MqttException.class, + () -> connectV3(ws(""), clientId, clientId + "?" + NAME_PARAM + "it-cauth-nope", "allow", Map.of())); + + assertEquals(MqttException.REASON_CODE_CONNECTION_LOST, refused.getReasonCode()); + verify(lambdaService, never()).invokeArn(anyString(), any(byte[].class), any(InvocationType.class)); + } + + @Test + void mqtt5AllowedConnectWithTheNameInTheUrlQueryConnects() throws Exception { + String clientId = "cauth-v5-" + System.nanoTime(); + + MqttAsyncClient client = connectV5(ws("?" + NAME_PARAM + authorizer), clientId, clientId, "allow"); + client.disconnect().waitForCompletion(5000); + client.close(); + + JsonNode event = takeEvent(); + assertEquals(NAME_PARAM + authorizer, event.path("protocolData").path("http").path("queryString").asText()); + assertEquals(clientId, event.path("protocolData").path("mqtt").path("username").asText()); + } + + @Test + void mqtt5RefusedConnectGetsReasonCode135() { + String clientId = "cauth-v5-deny-" + System.nanoTime(); + + org.eclipse.paho.mqttv5.common.MqttException refused = assertThrows(org.eclipse.paho.mqttv5.common.MqttException.class, + () -> connectV5(ws(""), clientId, clientId + "?" + NAME_PARAM + authorizer, "deny")); + + assertEquals(135, refused.getReasonCode()); + } + + @Test + void theNameInTheUpgradeHeaderIsSentAmongTheLowerCasedHeaders() throws Exception { + String clientId = "cauth-header-" + System.nanoTime(); + + connectV3(ws(""), clientId, clientId, "allow", Map.of("X-Amz-CustomAuthorizer-Name", authorizer)).disconnect(); + + assertEquals(authorizer, takeEvent().path("protocolData").path("http").path("headers") + .path("x-amz-customauthorizer-name").asText()); + } + + @Test + void overWssTheEventCarriesTheServerName() throws Exception { + String clientId = "cauth-wss-" + System.nanoTime(); + + connectV3("wss://localhost:" + testSslPort + "/mqtt", clientId, clientId + "?" + NAME_PARAM + authorizer, "allow", + Map.of()).disconnect(); + + JsonNode event = takeEvent(); + assertEquals(List.of("tls", "http", "mqtt"), protocols(event)); + assertEquals("localhost", event.path("protocolData").path("tls").path("serverName").asText()); + List keys = new ArrayList<>(); + event.path("protocolData").fieldNames().forEachRemaining(keys::add); + assertEquals(List.of("tls", "mqtt", "http"), keys); + } + + @Test + void overWssToAnAddressLiteralTheEventCarriesNoServerName() throws Exception { + String clientId = "cauth-wss-ip-" + System.nanoTime(); + + connectV3("wss://127.0.0.1:" + testSslPort + "/mqtt", clientId, clientId + "?" + NAME_PARAM + authorizer, "allow", + Map.of()).disconnect(); + + JsonNode event = takeEvent(); + assertEquals(List.of("http", "mqtt"), protocols(event)); + assertFalse(event.path("protocolData").has("tls"), event.path("protocolData").toString()); + } + + @Test + void mqtt3AllowedConnectOnTheTcpListenerSendsAnMqttOnlyEvent() throws Exception { + String clientId = "cauth-tcp-v3-" + System.nanoTime(); + String username = clientId + "?" + NAME_PARAM + authorizer; + + connectV3(tcp(), clientId, username, "allow", Map.of()).disconnect(); + + JsonNode event = takeEvent(); + assertEquals(List.of("mqtt"), protocols(event)); + JsonNode protocolData = event.path("protocolData"); + assertFalse(protocolData.has("http"), protocolData.toString()); + assertFalse(protocolData.has("tls"), protocolData.toString()); + assertEquals(username, protocolData.path("mqtt").path("username").asText()); + assertEquals(base64("allow"), protocolData.path("mqtt").path("password").asText()); + assertEquals(clientId, protocolData.path("mqtt").path("clientId").asText()); + } + + @Test + void mqtt5RefusedConnectOnTheTcpListenerGetsReasonCode135() throws Exception { + String clientId = "cauth-tcp-v5-deny-" + System.nanoTime(); + + org.eclipse.paho.mqttv5.common.MqttException refused = assertThrows(org.eclipse.paho.mqttv5.common.MqttException.class, + () -> connectV5(tcp(), clientId, clientId + "?" + NAME_PARAM + authorizer, "deny")); + + assertEquals(135, refused.getReasonCode()); + assertEquals(base64("deny"), takeEvent().path("protocolData").path("mqtt").path("password").asText()); + } + + // Helpers + + private MqttClient connectV3(String url, String clientId, String username, String password, Map headers) + throws Exception { + MqttClient client = new MqttClient(url, clientId, new MemoryPersistence()); + client.setTimeToWait(10_000); + MqttConnectOptions options = new MqttConnectOptions(); + options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); + options.setCleanSession(true); + options.setConnectionTimeout(10); + options.setUserName(username); + options.setPassword(password.toCharArray()); + if (!headers.isEmpty()) { + Properties custom = new Properties(); + custom.putAll(headers); + options.setCustomWebSocketHeaders(custom); + } + if (url.startsWith("wss://")) { + options.setSocketFactory(IotMqttWebSocketIntegrationTest.trustOnlyFlociCa().getSocketFactory()); + } + try { + client.connect(options); + } catch (MqttException e) { + client.close(); + throw e; + } + return client; + } + + private MqttAsyncClient connectV5(String url, String clientId, String username, String password) + throws org.eclipse.paho.mqttv5.common.MqttException { + MqttAsyncClient client = new MqttAsyncClient(url, clientId, new org.eclipse.paho.mqttv5.client.persist.MemoryPersistence()); + MqttConnectionOptions options = new MqttConnectionOptions(); + options.setCleanStart(true); + options.setConnectionTimeout(10); + options.setUserName(username); + options.setPassword(password.getBytes(StandardCharsets.UTF_8)); + try { + client.connect(options).waitForCompletion(10_000); + } catch (org.eclipse.paho.mqttv5.common.MqttException e) { + client.close(true); + throw e; + } + return client; + } + + private String ws(String query) { + return "ws://127.0.0.1:" + testHttpPort + "/mqtt" + query; + } + + private static String tcp() { + return "tcp://127.0.0.1:" + IotMqttWebSocketIntegrationTest.PLAIN_PORT; + } + + private JsonNode takeEvent() throws InterruptedException { + JsonNode event = events.poll(10, TimeUnit.SECONDS); + assertNotNull(event, "the authorizer function was not invoked"); + return event; + } + + private static List protocols(JsonNode event) { + List protocols = new ArrayList<>(); + event.path("protocols").forEach(protocol -> protocols.add(protocol.asText())); + return protocols; + } + + private static String allow(String clientId) { + return "{\"isAuthenticated\": true, \"principalId\": \"itprincipal\", \"policyDocuments\": [" + + MAPPER.valueToTree(policy(clientId)) + "]}"; + } + + private static String policy(String clientId) { + return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"iot:Connect\"," + + "\"Resource\":\"arn:aws:iot:us-east-1:000000000000:client/" + clientId + "\"}]}"; + } + + private static String base64(String text) { + return Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerTest.java new file mode 100644 index 0000000000..a126a95479 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerTest.java @@ -0,0 +1,594 @@ +package io.github.hectorvent.floci.services.iot; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.iot.IotCustomAuthorizer.WebSocketUpgrade; +import io.github.hectorvent.floci.services.lambda.LambdaService; +import io.github.hectorvent.floci.services.lambda.model.InvocationType; +import io.github.hectorvent.floci.services.lambda.model.InvokeResult; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.Signature; +import java.util.ArrayList; +import java.util.Base64; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * The custom authorizer decision as AWS IoT makes it, with the function mocked: the event each + * request or connection produces, the checks on the function's answer in AWS's order and wording, + * and token signature verification. + */ +class IotCustomAuthorizerTest { + + private static final String REGION = "us-east-1"; + private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:000000000000:function:auth"; + private static final String POLICY = "{\"Version\": \"2012-10-17\", \"Statement\": [{\"Effect\": \"Allow\", " + + "\"Action\": \"iot:Connect\", \"Resource\": \"*\"}]}"; + private static final String NAME_PARAM = "x-amz-customauthorizer-name="; + private static final String SIGNATURE_PARAM = "x-amz-customauthorizer-signature="; + private static final String PRINCIPAL_CONSTRAINT = + " at principalId failed to satisfy constraint: Member must satisfy ([a-zA-Z0-9]){1,128}"; + private static final String TTL_CONSTRAINT = + " failed to satisfy constraint: Member must be greater than 5 minutes and less than 24 hours."; + + private static KeyPair key1; + private static KeyPair key2; + private static KeyPair unregistered; + + private final ObjectMapper mapper = new ObjectMapper(); + private final RegionResolver regionResolver = new RegionResolver(REGION, "000000000000"); + private final IotAuthorizerService authorizers = new IotAuthorizerService(new InMemoryStorage<>(), regionResolver); + private final LambdaService lambda = mock(LambdaService.class); + private final IotCustomAuthorizer customAuthorizer = new IotCustomAuthorizer(authorizers, lambda, mapper, regionResolver); + private final List events = new ArrayList<>(); + private String answer = response("[" + quoted(POLICY) + "]", "\"refreshAfterInSeconds\": 600, \"disconnectAfterInSeconds\": 3600"); + private String functionError; + + @BeforeAll + static void generateKeys() throws GeneralSecurityException { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + key1 = generator.generateKeyPair(); + key2 = generator.generateKeyPair(); + unregistered = generator.generateKeyPair(); + } + + @BeforeEach + void setUp() { + when(lambda.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenAnswer(invocation -> { + events.add(mapper.readTree((byte[]) invocation.getArgument(1))); + return new InvokeResult(200, functionError, answer.getBytes(StandardCharsets.UTF_8), null, "request-id"); + }); + create("a", "ACTIVE", false); + create("s", "ACTIVE", true); + create("off", "INACTIVE", false); + } + + // TestInvokeAuthorizer + + @Test + void testInvokeReturnsTheVerdictWithPoliciesVerbatimAndSendsTheMqttFieldsAsGiven() { + ObjectNode response = customAuthorizer.testInvoke("a", request(""" + {"mqttContext": {"username": "c?x-amz-customauthorizer-name=a", "password": "c2VjcmV0", "clientId": "c"}} + """), REGION); + + assertEquals("{\"disconnectAfterInSeconds\":3600,\"isAuthenticated\":true,\"policyDocuments\":[" + + quoted(POLICY) + "],\"principalId\":\"p1\",\"refreshAfterInSeconds\":600}", response.toString()); + assertEquals("{\"protocolData\":{\"mqtt\":{\"username\":\"c?x-amz-customauthorizer-name=a\",\"password\":\"c2VjcmV0\"," + + "\"clientId\":\"c\"}},\"protocols\":[\"mqtt\"],\"signatureVerified\":false," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void testInvokeOrdersProtocolDataTlsMqttHttpAndProtocolsTlsHttpMqtt() { + customAuthorizer.testInvoke("a", request(""" + {"httpContext": {"headers": {"X-Secret": "s", "X-Mixed-Case": "Value"}, "queryString": "?a=b&c=d"}, + "mqttContext": {"clientId": "c"}, "tlsContext": {"serverName": "iot.example.com"}} + """), REGION); + + assertEquals("{\"protocolData\":{\"tls\":{\"serverName\":\"iot.example.com\"},\"mqtt\":{\"clientId\":\"c\"}," + + "\"http\":{\"headers\":{\"X-Secret\":\"s\",\"X-Mixed-Case\":\"Value\"},\"queryString\":\"?a=b&c=d\"}}," + + "\"protocols\":[\"tls\",\"http\",\"mqtt\"],\"signatureVerified\":false," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void testInvokeWithAnEmptyHttpContextSendsAnEmptyHttpObject() { + customAuthorizer.testInvoke("a", request("{\"httpContext\": {}}"), REGION); + + assertEquals("{\"protocolData\":{\"http\":{}},\"protocols\":[\"http\"],\"signatureVerified\":false," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void connectionMetadataIdIsAFreshUuidForEveryInvocation() { + customAuthorizer.testInvoke("a", request("{\"httpContext\": {}}"), REGION); + customAuthorizer.testInvoke("a", request("{\"httpContext\": {}}"), REGION); + + String first = events.get(0).path("connectionMetadata").path("id").asText(); + String second = events.get(1).path("connectionMetadata").path("id").asText(); + assertTrue(first.matches("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}"), first); + assertNotEquals(first, second); + } + + @Test + void testInvokeWorksOnAnInactiveAuthorizer() { + assertTrue(customAuthorizer.testInvoke("off", request("{\"httpContext\": {}}"), REGION) + .path("isAuthenticated").asBoolean()); + } + + @Test + void testInvokeOnAMissingAuthorizerIsNotFound() { + assertAwsError("ResourceNotFoundException", 404, "Authorizer nope not found", + () -> customAuthorizer.testInvoke("nope", request("{\"httpContext\": {}}"), REGION)); + verifyNotInvoked(); + } + + @Test + void omittedTtlsAreDefaultedTo300And86400() { + answer = response("[" + quoted(POLICY) + "]", null); + + ObjectNode response = testInvoke(); + + assertEquals(300, response.path("refreshAfterInSeconds").asInt()); + assertEquals(86400, response.path("disconnectAfterInSeconds").asInt()); + } + + @Test + void ttlsAtTheInclusiveBoundsAreAccepted() { + answer = response("[" + quoted(POLICY) + "]", "\"refreshAfterInSeconds\": 300, \"disconnectAfterInSeconds\": 86400"); + + ObjectNode response = testInvoke(); + + assertEquals(300, response.path("refreshAfterInSeconds").asInt()); + assertEquals(86400, response.path("disconnectAfterInSeconds").asInt()); + } + + @Test + void aPolicyGivenAsAnObjectIsReturnedAsOneCompactString() throws Exception { + answer = response("[" + POLICY + "]", null); + + ObjectNode response = testInvoke(); + + assertEquals(mapper.writeValueAsString(mapper.readTree(POLICY)), response.path("policyDocuments").get(0).asText()); + } + + @Test + void aDenyWithPrincipalAndPoliciesIsReturnedAsIs() { + answer = "{\"isAuthenticated\": false, \"principalId\": \"p1\", \"policyDocuments\": [" + quoted(POLICY) + "]}"; + + ObjectNode response = testInvoke(); + + assertFalse(response.path("isAuthenticated").asBoolean()); + assertEquals(POLICY, response.path("policyDocuments").get(0).asText()); + } + + @Test + void anEmptyPolicyListIsReturnedAsIs() { + answer = response("[]", null); + + assertEquals(0, testInvoke().path("policyDocuments").size()); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "{\"foo\": 1}|Lambda invocation result is in invalid format", + "{\"principalId\": \"p1\", \"policyDocuments\": []}|Lambda invocation result is in invalid format", + "{\"isAuthenticated\": false}|null" + PRINCIPAL_CONSTRAINT, + "{\"isAuthenticated\": true, \"policyDocuments\": []}|null" + PRINCIPAL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"bad-principal!\"}|bad-principal!" + PRINCIPAL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"p1\"}|policyDocuments failed to satisfy constraint: List of policies cannot be null.", + "{\"isAuthenticated\": false, \"principalId\": \"p1\"}|policyDocuments failed to satisfy constraint: List of policies cannot be null.", + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [\"this is not json\"]}" + + "|policyDocuments failed to satisfy constraint: Policy document is Malformed: Invalid policy syntax.", + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [\"{} {}\"]}" + + "|policyDocuments failed to satisfy constraint: Policy document is Malformed: Invalid policy syntax.", + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\"]}" + + "|policyDocuments failed to satisfy constraint: Number of policies allowed cannot exceed 10", + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [{}], \"refreshAfterInSeconds\": 299}" + + "|299 at refreshAfterInSeconds" + TTL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [{}], \"refreshAfterInSeconds\": 86401}" + + "|86401 at refreshAfterInSeconds" + TTL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [{}], \"disconnectAfterInSeconds\": 299}" + + "|299 at disconnectAfterInSeconds" + TTL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [{}], \"disconnectAfterInSeconds\": 86401}" + + "|86401 at disconnectAfterInSeconds" + TTL_CONSTRAINT, + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": [{}], \"refreshAfterInSeconds\": 10, \"disconnectAfterInSeconds\": 10}" + + "|10 at refreshAfterInSeconds" + TTL_CONSTRAINT, + "not json|Lambda invocation result is in invalid format", + "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": []} junk|Lambda invocation result is in invalid format" + }) + void anInvalidAnswerIsAnInvalidResponseInAwsOrderAndWording(String invalidAnswer, String message) { + answer = invalidAnswer; + + assertAwsError("InvalidResponseException", 400, message, this::testInvoke); + } + + @Test + void aPolicyOverTheSizeLimitIsAnInvalidResponse() { + answer = response("[" + quoted("{\"Version\": \"2012-10-17\", \"Statement\": [], \"Id\": \"" + "x".repeat(2048) + "\"}") + + "]", null); + + assertAwsError("InvalidResponseException", 400, + "policyDocuments failed to satisfy constraint: Size of a policy document cannot exceeds hard limit of 2048", + this::testInvoke); + } + + @Test + void aFunctionErrorReportsItsMessageAndType() { + functionError = "Unhandled"; + answer = "{\"errorMessage\": \"compat-raise\", \"errorType\": \"Exception\", \"stackTrace\": []}"; + + assertAwsError("InvalidResponseException", 400, + "Lambda function encountered error in execution: compat-raise: Exception", this::testInvoke); + } + + @Test + void tokenWithoutSignatureIsRefused() { + assertInvalidRequest("Request must provide both token and tokenSignature together.", + () -> customAuthorizer.testInvoke("a", request("{\"token\": \"allow-me\"}"), REGION)); + } + + @Test + void tokenAndSignatureOnASigningDisabledAuthorizerAreRefused() throws GeneralSecurityException { + assertInvalidRequest("Token and token signature provided but signing is disabled for the authorizer.", + () -> customAuthorizer.testInvoke("a", tokenRequest("allow-me", sign(key1, "allow-me")), REGION)); + } + + @Test + void tokenWithAContextIsRefused() throws GeneralSecurityException { + ObjectNode request = tokenRequest("allow-me", sign(key1, "allow-me")); + request.putObject("mqttContext").put("clientId", "c"); + + assertInvalidRequest("Request cannot have token and Http/Mqtt/Tls context.", + () -> customAuthorizer.testInvoke("s", request, REGION)); + } + + @Test + void aRequestWithoutTokenOrContextIsRefused() { + assertInvalidRequest("Request must have Http/Mqtt/Tls context.", + () -> customAuthorizer.testInvoke("a", request("{}"), REGION)); + } + + @Test + void aSignatureThatIsNotBase64IsAValidationError() { + assertInvalidRequest("1 validation error detected: Value at 'tokenSignature' failed to satisfy constraint: " + + "Member must satisfy regular expression pattern: [A-Za-z0-9+/]+={0,2}", + () -> customAuthorizer.testInvoke("s", tokenRequest("allow-me", "not base64!"), REGION)); + } + + @Test + void aQueryStringWithoutTheQuestionMarkIsRefused() { + assertInvalidRequest("Invalid arguments in the call", () -> customAuthorizer.testInvoke("a", + request("{\"httpContext\": {\"headers\": {\"X-Secret\": \"s\"}, \"queryString\": \"a=b\"}}"), REGION)); + } + + @Test + void anEmptyQueryStringIsAValidationError() { + assertInvalidRequest("1 validation error detected: Value at 'httpContext.queryString' failed to satisfy " + + "constraint: Member must have length greater than or equal to 1", + () -> customAuthorizer.testInvoke("a", request("{\"httpContext\": {\"queryString\": \"\"}}"), REGION)); + } + + @Test + void anEmptyTokenIsAValidationError() throws GeneralSecurityException { + assertInvalidRequest("1 validation error detected: Value at 'token' failed to satisfy constraint: " + + "Member must have length greater than or equal to 1", + () -> customAuthorizer.testInvoke("s", tokenRequest("", sign(key1, "")), REGION)); + } + + @Test + void aTokenOverItsMaximumLengthIsAValidationError() throws GeneralSecurityException { + String token = "t".repeat(6145); + + assertInvalidRequest("1 validation error detected: Value at 'token' failed to satisfy constraint: " + + "Member must have length less than or equal to 6144", + () -> customAuthorizer.testInvoke("s", tokenRequest(token, sign(key1, token)), REGION)); + } + + @Test + void aSignatureOverItsMaximumLengthIsAValidationError() { + assertInvalidRequest("1 validation error detected: Value at 'tokenSignature' failed to satisfy constraint: " + + "Member must have length less than or equal to 2560", + () -> customAuthorizer.testInvoke("s", tokenRequest("allow-me", "A".repeat(2561)), REGION)); + } + + @Test + void tokenModeVerifiesASignatureByEitherKeyAndSendsOnlyTheToken() throws GeneralSecurityException { + for (KeyPair key : List.of(key1, key2)) { + assertTrue(customAuthorizer.testInvoke("s", tokenRequest("allow-me", sign(key, "allow-me")), REGION) + .path("isAuthenticated").asBoolean()); + assertEquals("{\"token\":\"allow-me\",\"signatureVerified\":true,\"connectionMetadata\":{\"id\":\"ID\"}}", + lastEvent()); + } + } + + @Test + void aSignatureOverAnotherTokenOrByAnUnregisteredKeyIsAMismatch() throws GeneralSecurityException { + assertInvalidRequest("Token signature mismatch for authorizer s", + () -> customAuthorizer.testInvoke("s", tokenRequest("allow-me", sign(key1, "other-token")), REGION)); + assertInvalidRequest("Token signature mismatch for authorizer s", + () -> customAuthorizer.testInvoke("s", tokenRequest("allow-me", sign(unregistered, "allow-me")), REGION)); + } + + @Test + void aSignedMqttUsernameIsVerifiedAndItsTokenSent() throws GeneralSecurityException { + String username = signedUsername(sign(key1, "allow-me"), "allow-me"); + ObjectNode request = mapper.createObjectNode(); + request.putObject("mqttContext").put("username", username).put("clientId", "c"); + + customAuthorizer.testInvoke("s", request, REGION); + + assertEquals("{\"protocolData\":{\"mqtt\":{\"username\":" + quoted(username) + ",\"clientId\":\"c\"}}," + + "\"protocols\":[\"mqtt\"],\"token\":\"allow-me\",\"signatureVerified\":true," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void aSignatureInHttpHeadersOrQueryStringIsVerified() throws GeneralSecurityException { + String signature = sign(key2, "allow-me"); + ObjectNode headers = mapper.createObjectNode(); + headers.putObject("httpContext").putObject("headers").put("tok", "allow-me").put("x-amz-customauthorizer-signature", signature); + ObjectNode query = mapper.createObjectNode(); + query.putObject("httpContext").put("queryString", "?tok=allow-me&" + SIGNATURE_PARAM + urlEncoded(signature)); + + customAuthorizer.testInvoke("s", headers, REGION); + assertEquals("allow-me", events.getLast().path("token").asText()); + customAuthorizer.testInvoke("s", query, REGION); + assertEquals("allow-me", events.getLast().path("token").asText()); + } + + @Test + void aSignedAuthorizerWithoutATokenInTheContextIsRefusedWithoutAMessage() { + AwsException failure = assertThrows(AwsException.class, () -> customAuthorizer.testInvoke("s", + request("{\"mqttContext\": {\"clientId\": \"c\", \"password\": \"c2VjcmV0\"}}"), REGION)); + + assertEquals("InvalidRequestException", failure.getErrorCode()); + assertNull(failure.getMessage()); + verifyNotInvoked(); + } + + // MQTT CONNECT + + @Test + void aWebSocketConnectSendsTlsMqttAndHttpWithTheUsernameVerbatim() { + Map headers = new LinkedHashMap<>(); + headers.put("host", "iot.example.com"); + headers.put("sec-websocket-protocol", "mqtt"); + + List policies = customAuthorizer.connectPolicies("a", "c", "c?" + NAME_PARAM + "a", "secret", + new WebSocketUpgrade(headers, "", "iot.example.com", null)); + + assertEquals(List.of(POLICY), policies); + assertEquals("{\"protocolData\":{\"tls\":{\"serverName\":\"iot.example.com\"},\"mqtt\":{\"username\":" + + "\"c?x-amz-customauthorizer-name=a\",\"password\":\"c2VjcmV0\",\"clientId\":\"c\"},\"http\":{\"headers\":" + + "{\"host\":\"iot.example.com\",\"sec-websocket-protocol\":\"mqtt\"},\"queryString\":\"\"}}," + + "\"protocols\":[\"tls\",\"http\",\"mqtt\"],\"signatureVerified\":false," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void aConnectWithoutUsernamePasswordOrTlsLeavesThemOut() { + customAuthorizer.connectPolicies("a", "c", null, null, new WebSocketUpgrade(Map.of(), NAME_PARAM + "a", null, null)); + + assertEquals("{\"protocolData\":{\"mqtt\":{\"clientId\":\"c\"},\"http\":{\"headers\":{}," + + "\"queryString\":\"x-amz-customauthorizer-name=a\"}},\"protocols\":[\"http\",\"mqtt\"]," + + "\"signatureVerified\":false,\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void aPlainTcpConnectSendsOnlyMqtt() { + customAuthorizer.connectPolicies("a", "c", "c?" + NAME_PARAM + "a", null, null); + + assertEquals("{\"protocolData\":{\"mqtt\":{\"username\":\"c?x-amz-customauthorizer-name=a\",\"clientId\":\"c\"}}," + + "\"protocols\":[\"mqtt\"],\"signatureVerified\":false,\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void aDenyOrAnEmptyPolicyListAdmitsNoPolicies() { + answer = "{\"isAuthenticated\": false, \"principalId\": \"p1\", \"policyDocuments\": [" + quoted(POLICY) + "]}"; + assertEquals(List.of(), connect("a")); + + answer = response("[]", null); + assertEquals(List.of(), connect("a")); + } + + @Test + void anObjectPolicyReachesTheConnectAsACompactString() throws Exception { + answer = response("[" + POLICY + "]", null); + + assertEquals(List.of(mapper.writeValueAsString(mapper.readTree(POLICY))), connect("a")); + } + + @Test + void anInvalidAnswerFailsTheConnect() { + answer = "{\"isAuthenticated\": false}"; + + assertAwsError("InvalidResponseException", 400, "null" + PRINCIPAL_CONSTRAINT, () -> connect("a")); + } + + @Test + void anInactiveAuthorizerAdmitsNothingWithoutInvokingTheFunction() { + assertEquals(List.of(), connect("off")); + verifyNotInvoked(); + } + + @Test + void anUnknownAuthorizerFailsTheConnectWithoutInvokingTheFunction() { + assertThrows(AwsException.class, () -> connect("nope")); + verifyNotInvoked(); + } + + @Test + void aSignedConnectSendsTheTokenAfterTheProtocols() throws GeneralSecurityException { + String username = signedUsername(sign(key2, "allow-me"), "allow-me"); + + customAuthorizer.connectPolicies("s", "c", username, null, null); + + assertEquals("{\"protocolData\":{\"mqtt\":{\"username\":" + quoted(username) + ",\"clientId\":\"c\"}}," + + "\"protocols\":[\"mqtt\"],\"token\":\"allow-me\",\"signatureVerified\":true," + + "\"connectionMetadata\":{\"id\":\"ID\"}}", lastEvent()); + } + + @Test + void aSignedConnectReadsTokenAndSignatureFromTheWebSocketQueryOrHeaders() throws GeneralSecurityException { + String signature = sign(key1, "allow-me"); + + customAuthorizer.connectPolicies("s", "c", "c", null, new WebSocketUpgrade(Map.of(), + NAME_PARAM + "s&" + SIGNATURE_PARAM + urlEncoded(signature) + "&tok=allow-me", null, null)); + assertEquals("allow-me", events.getLast().path("token").asText()); + + customAuthorizer.connectPolicies("s", "c", "c", null, new WebSocketUpgrade( + Map.of("x-amz-customauthorizer-signature", signature, "tok", "allow-me"), "", null, null)); + assertEquals("allow-me", events.getLast().path("token").asText()); + } + + @Test + void aSignedConnectWithABadOrMissingSignatureNeverInvokesTheFunction() throws GeneralSecurityException { + String overOtherToken = signedUsername(sign(key1, "other-token"), "allow-me"); + String withoutToken = "c?" + NAME_PARAM + "s&" + SIGNATURE_PARAM + urlEncoded(sign(key1, "allow-me")); + + for (String username : List.of(overOtherToken, withoutToken, "c?" + NAME_PARAM + "s&tok=allow-me")) { + assertThrows(AwsException.class, () -> customAuthorizer.connectPolicies("s", "c", username, null, null), username); + } + verifyNotInvoked(); + } + + @Test + void theAuthorizerNameComesFromTheUsernameTheWebSocketQueryOrTheUpgradeHeader() { + assertEquals("u", IotCustomAuthorizer.authorizerName("c?" + NAME_PARAM + "u&tok=t", null)); + assertEquals("q", IotCustomAuthorizer.authorizerName("c", new WebSocketUpgrade(Map.of(), NAME_PARAM + "q", null, null))); + assertEquals("h", IotCustomAuthorizer.authorizerName(null, + new WebSocketUpgrade(Map.of("x-amz-customauthorizer-name", "h"), "", null, null))); + } + + @Test + void aConnectNamingNoAuthorizerHasNoName() { + assertNull(IotCustomAuthorizer.authorizerName(null, null)); + assertNull(IotCustomAuthorizer.authorizerName("plain-user", null)); + assertNull(IotCustomAuthorizer.authorizerName("c?X-Amz-Algorithm=AWS4-HMAC-SHA256", new WebSocketUpgrade( + Map.of("sec-websocket-protocol", "mqtt"), "X-Amz-Signature=00", null, null))); + assertEquals("%zz", IotCustomAuthorizer.authorizerName("c?" + NAME_PARAM + "%zz", null), + "a malformed escape is kept as sent instead of failing the CONNECT"); + } + + // Helpers + + private void create(String name, String status, boolean signed) { + ObjectNode body = mapper.createObjectNode().put("authorizerFunctionArn", FUNCTION_ARN).put("status", status); + if (signed) { + body.put("tokenKeyName", "tok"); + body.putObject("tokenSigningPublicKeys").put("k1", pem(key1)).put("k2", pem(key2)); + } else { + body.put("signingDisabled", true); + } + authorizers.createAuthorizer(name, body, REGION); + } + + private ObjectNode testInvoke() { + return customAuthorizer.testInvoke("a", request("{\"mqttContext\": {\"clientId\": \"c\", \"password\": \"c2VjcmV0\"}}"), + REGION); + } + + private List connect(String name) { + return customAuthorizer.connectPolicies(name, "c", "c?" + NAME_PARAM + name, "secret", null); + } + + private ObjectNode request(String json) { + try { + return (ObjectNode) mapper.readTree(json); + } catch (Exception e) { + throw new IllegalArgumentException(json, e); + } + } + + private ObjectNode tokenRequest(String token, String signature) { + return mapper.createObjectNode().put("token", token).put("tokenSignature", signature); + } + + /** The last event the function received, with its random connection id replaced by {@code ID}. */ + private String lastEvent() { + ObjectNode event = (ObjectNode) events.getLast().deepCopy(); + ((ObjectNode) event.get("connectionMetadata")).put("id", "ID"); + return event.toString(); + } + + private void verifyNotInvoked() { + verify(lambda, never()).invokeArn(anyString(), any(byte[].class), any(InvocationType.class)); + } + + private static String response(String policies, String ttls) { + return "{\"isAuthenticated\": true, \"principalId\": \"p1\", \"policyDocuments\": " + policies + + (ttls == null ? "" : ", " + ttls) + "}"; + } + + private static String quoted(String text) { + return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; + } + + private static String signedUsername(String signature, String token) { + return "c?" + NAME_PARAM + "s&" + SIGNATURE_PARAM + urlEncoded(signature) + "&tok=" + token; + } + + private static String urlEncoded(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8); + } + + private static String sign(KeyPair key, String token) throws GeneralSecurityException { + PrivateKey privateKey = key.getPrivate(); + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initSign(privateKey); + signature.update(token.getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString(signature.sign()); + } + + private static String pem(KeyPair key) { + return "-----BEGIN PUBLIC KEY-----\n" + + Base64.getMimeEncoder(64, "\n".getBytes(StandardCharsets.US_ASCII)).encodeToString(key.getPublic().getEncoded()) + + "\n-----END PUBLIC KEY-----"; + } + + private void assertInvalidRequest(String message, Executable call) { + assertAwsError("InvalidRequestException", 400, message, call); + verifyNotInvoked(); + } + + private static void assertAwsError(String code, int status, String message, Executable call) { + AwsException failure = assertThrows(AwsException.class, call); + assertEquals(code, failure.getErrorCode()); + assertEquals(status, failure.getHttpStatus()); + assertEquals(message, failure.getMessage()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java index 06176fbdc1..5eda9573fe 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java @@ -108,7 +108,7 @@ class IotMqttBrokerDeviceVerificationTest { .addCertValue(Buffer.buffer(serverLeaf.certificatePem())) .addKeyValue(Buffer.buffer(serverLeaf.privateKeyPem()))); when(iotService.get()).thenReturn(service); - broker = new IotMqttBrokerService(config, vertx, iotService, registry); + broker = new IotMqttBrokerService(config, vertx, iotService, mock(IotCustomAuthorizer.class), registry); broker.startIfEnabled(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java index 0394f01a05..616207b751 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java @@ -32,6 +32,7 @@ import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.net.KeyCertOptions; import io.vertx.core.net.PemKeyCertOptions; +import io.vertx.core.net.SocketAddress; import io.vertx.mqtt.MqttEndpoint; import io.vertx.mqtt.MqttTopicSubscription; import io.vertx.mqtt.messages.MqttSubscribeMessage; @@ -68,6 +69,7 @@ import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.BlockingQueue; @@ -93,6 +95,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -130,6 +133,7 @@ class IotMqttBrokerServiceTest { private final TlsConfiguration tls = mock(TlsConfiguration.class); @SuppressWarnings("unchecked") private final Instance iotService = mock(Instance.class); + private final IotCustomAuthorizer customAuthorizer = mock(IotCustomAuthorizer.class); private int plainPort; private int tlsPort; private IotMqttBrokerService broker; @@ -162,7 +166,7 @@ class IotMqttBrokerServiceTest { when(config.tls().enabled()).thenReturn(true); when(registry.getDefault()).thenReturn(Optional.of(tls)); when(tls.getKeyStoreOptions()).thenReturn(pem(bootLeaf)); - broker = new IotMqttBrokerService(config, vertx, iotService, registry); + broker = new IotMqttBrokerService(config, vertx, iotService, customAuthorizer, registry); } @AfterEach @@ -481,6 +485,29 @@ class IotMqttBrokerServiceTest { } } + /** + * A WebSocket client's CONNECT reaches the broker from the bridge's loopback socket, so the + * policies its custom authorizer returns are evaluated against the address the bridge recorded + * for the WebSocket client. + */ + @Test + void aWebSocketConnectThroughACustomAuthorizerIsEvaluatedAgainstTheWebSocketClientsAddress() { + IotService service = mock(IotService.class); + when(iotService.get()).thenReturn(service); + when(customAuthorizer.connectPolicies(eq("ws-auth"), eq("ws-client"), any(), any(), any())) + .thenReturn(List.of("{}")); + SocketAddress bridge = SocketAddress.inetSocketAddress(50123, "127.0.0.1"); + broker.webSocketUpgrades().put(bridge, new IotCustomAuthorizer.WebSocketUpgrade( + Map.of(), IotCustomAuthorizer.NAME_PARAM + "=ws-auth", null, "203.0.113.7")); + MqttEndpoint endpoint = mock(MqttEndpoint.class); + when(endpoint.clientIdentifier()).thenReturn("ws-client"); + when(endpoint.remoteAddress()).thenReturn(bridge); + + broker.handleEndpoint(endpoint, false); + + verify(service, timeout(10_000)).isConnectAllowedBy(List.of("{}"), "ws-client", "203.0.113.7", null); + } + /** * Two publishes from two connections, the first one's rule action held on a latch: the second * publish is fanned out, so its rules were handed over, yet they start only once the first diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridgeTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridgeTest.java index 52d78c4009..037943dfe7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridgeTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketBridgeTest.java @@ -3,9 +3,17 @@ package io.github.hectorvent.floci.services.iot; import io.vertx.core.Handler; import io.vertx.core.http.ServerWebSocket; import io.vertx.core.net.NetSocket; +import io.vertx.core.net.SocketAddress; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyShort; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -22,7 +30,7 @@ class IotMqttWebSocketBridgeTest { when(webSocket.isClosed()).thenReturn(false); IotMqttWebSocketBridge.BridgeSession session = - new IotMqttWebSocketBridge.BridgeSession(webSocket, socket); + new IotMqttWebSocketBridge.BridgeSession(webSocket, socket, () -> { }); session.start(); ArgumentCaptor> closeCaptor = ArgumentCaptor.forClass(Handler.class); @@ -44,7 +52,7 @@ class IotMqttWebSocketBridgeTest { when(webSocket.isClosed()).thenReturn(false); IotMqttWebSocketBridge.BridgeSession session = - new IotMqttWebSocketBridge.BridgeSession(webSocket, socket); + new IotMqttWebSocketBridge.BridgeSession(webSocket, socket, () -> { }); session.start(); ArgumentCaptor> closeCaptor = ArgumentCaptor.forClass(Handler.class); @@ -66,7 +74,7 @@ class IotMqttWebSocketBridgeTest { when(webSocket.isClosed()).thenReturn(false); IotMqttWebSocketBridge.BridgeSession session = - new IotMqttWebSocketBridge.BridgeSession(webSocket, socket); + new IotMqttWebSocketBridge.BridgeSession(webSocket, socket, () -> { }); session.start(); ArgumentCaptor> exceptionCaptor = ArgumentCaptor.forClass(Handler.class); @@ -89,7 +97,7 @@ class IotMqttWebSocketBridgeTest { when(webSocket.isClosed()).thenReturn(false); IotMqttWebSocketBridge.BridgeSession session = - new IotMqttWebSocketBridge.BridgeSession(webSocket, socket); + new IotMqttWebSocketBridge.BridgeSession(webSocket, socket, () -> { }); session.start(); ArgumentCaptor> closeCaptor = ArgumentCaptor.forClass(Handler.class); @@ -104,4 +112,44 @@ class IotMqttWebSocketBridgeTest { verify(webSocket, never()).close(); verify(webSocket, never()).close(anyShort(), anyString()); } + + @Test + void everyWayTheSessionEndsRunsTheCloseHookOnce() { + for (int path = 0; path < 3; path++) { + ServerWebSocket webSocket = mock(ServerWebSocket.class); + NetSocket socket = mock(NetSocket.class); + AtomicInteger hooks = new AtomicInteger(); + new IotMqttWebSocketBridge.BridgeSession(webSocket, socket, hooks::incrementAndGet).start(); + ArgumentCaptor> socketClose = ArgumentCaptor.forClass(Handler.class); + verify(socket).closeHandler(socketClose.capture()); + ArgumentCaptor> webSocketClose = ArgumentCaptor.forClass(Handler.class); + verify(webSocket).closeHandler(webSocketClose.capture()); + ArgumentCaptor> failure = ArgumentCaptor.forClass(Handler.class); + verify(socket).exceptionHandler(failure.capture()); + + switch (path) { + case 0 -> socketClose.getValue().handle(null); + case 1 -> webSocketClose.getValue().handle(null); + default -> failure.getValue().handle(new IllegalStateException("broker failed")); + } + socketClose.getValue().handle(null); + webSocketClose.getValue().handle(null); + + assertEquals(1, hooks.get(), "path " + path); + } + } + + @Test + void closingAnOlderSessionKeepsANewerSessionsEqualUpgradeOnTheSameAddress() { + SocketAddress address = SocketAddress.inetSocketAddress(50000, "127.0.0.1"); + IotCustomAuthorizer.WebSocketUpgrade older = new IotCustomAuthorizer.WebSocketUpgrade(Map.of("host", "h"), "", null, null); + IotCustomAuthorizer.WebSocketUpgrade newer = new IotCustomAuthorizer.WebSocketUpgrade(Map.of("host", "h"), "", null, null); + Map upgrades = new ConcurrentHashMap<>(Map.of(address, newer)); + + IotMqttWebSocketBridge.forgetting(upgrades, address, older).run(); + assertSame(newer, upgrades.get(address)); + + IotMqttWebSocketBridge.forgetting(upgrades, address, newer).run(); + assertTrue(upgrades.isEmpty()); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java index 8d82c0c5f9..e262f86247 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java @@ -128,12 +128,12 @@ public class IotMqttWebSocketIntegrationTest { } @Test - void customAuthorizerStyleConnectIsAccepted() throws Exception { + void connectNamingAnUnknownCustomAuthorizerIsRefused() { String clientId = "tunnel-" + System.nanoTime(); String username = clientId + "?x-amz-customauthorizer-name=my-authorizer"; - try (WsClient client = WsClient.connect(wss("/mqtt"), clientId, username, "token-" + System.nanoTime())) { - assertTrue(client.isConnected()); - } + MqttException refused = assertThrows(MqttException.class, + () -> WsClient.connect(wss("/mqtt"), clientId, username, "token-" + System.nanoTime())); + assertEquals(MqttException.REASON_CODE_CONNECTION_LOST, refused.getReasonCode()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceDeviceAuthTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceDeviceAuthTest.java index 5d7b34fc58..3bc5c4967d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceDeviceAuthTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceDeviceAuthTest.java @@ -396,6 +396,27 @@ class IotServiceDeviceAuthTest { assertFalse(connect(device, "sensor-1")); } + @Test + void customAuthorizerPoliciesAllowTheClientArnOfTheDefaultAccountAndRegion() { + String onlySensor1 = statement("Allow", "arn:aws:iot:" + REGION + ":" + ACCOUNT + ":client/sensor-1", null); + + assertTrue(service.isConnectAllowedBy(List.of(onlySensor1), "sensor-1", "127.0.0.1", null)); + assertFalse(service.isConnectAllowedBy(List.of(onlySensor1), "sensor-2", "127.0.0.1", null)); + assertFalse(service.isConnectAllowedBy(List.of(statement("Allow", "arn:aws:iot:eu-west-1:" + ACCOUNT + ":client/sensor-1", + null)), "sensor-1", "127.0.0.1", null), "a client ARN in another region"); + assertFalse(service.isConnectAllowedBy(List.of(), "sensor-1", "127.0.0.1", null), "no policy means deny"); + } + + @Test + void customAuthorizerPoliciesResolveTheConnectionVariables() { + String policy = statement("Allow", "arn:aws:iot:*:*:client/${iot:ClientId}", + "{\"StringEquals\":{\"iot:DomainName\":\"iot.example.com\"},\"IpAddress\":{\"aws:SourceIp\":\"10.0.0.0/8\"}}"); + + assertTrue(service.isConnectAllowedBy(List.of(policy), "sensor-1", "10.1.2.3", "iot.example.com")); + assertFalse(service.isConnectAllowedBy(List.of(policy), "sensor-1", "127.0.0.1", "iot.example.com")); + assertFalse(service.isConnectAllowedBy(List.of(policy), "sensor-1", "10.1.2.3", null)); + } + private RegisteredDevice registeredDevice(boolean active) { IotCertificate created = service.createKeysAndCertificate(active, REGION); return service.findRegisteredCertificate(parse(created)).orElseThrow();