mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(iot): enforce custom authorizer decisions on MQTT connections (#4835)
* test(iot): cover custom authorizer invocation and enforcement against AWS * feat(iot): enforce custom authorizer decisions on MQTT connections Serve TestInvokeAuthorizer and invoke an authorizer's function on an MQTT CONNECT that names it, on the plaintext listener and the /mqtt WebSocket bridge. The event, the request and answer checks, signed-token verification and the refusal per MQTT version follow what was measured against AWS IoT Core. The bridge hands the upgrade's headers, query and server name to the broker keyed by its connection's local address. Closes #4823 * fix(iot): evaluate WebSocket custom authorizer policies against the client's address * docs(iot): note that a non-UTF-8 MQTT password reaches the authorizer altered * test(iot): cover custom authorizer CONNECTs on the plain MQTT listener * fix(iot): refuse TestInvokeAuthorizer tokens and signatures outside their length bounds * fix(iot): refuse authorizer JSON with trailing content TestInvokeAuthorizer, CreateAuthorizer and UpdateAuthorizer bodies, a custom authorizer's answer and each policy document it returns are now read with FAIL_ON_TRAILING_TOKENS, as the shared JSON controllers do, so a valid prefix followed by junk is refused instead of half-read. * fix(iot): keep address literals out of the WebSocket server name Over WSS without SNI the bridge fell back to the Host header unfiltered, so a client dialing an IP got that IP as tls.serverName in the authorizer event and as iot:DomainName in policy evaluation. Apply the same address literal filter SNI already uses. Also name the context validation ceiling in TestInvokeAuthorizer's ponytail note.
This commit is contained in:
1 parent
e9f2463103
commit
a33eea05cd
17 files changed
+2862
-50
No files matched your search
@@ -666,6 +666,20 @@
|
||||
<version>1.84</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- MQTT 3.1.1 and MQTT 5 clients for the IoT custom-authorizer WebSocket tests,
|
||||
the versions the emulator module itself tests with -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.mqttv5.client</artifactId>
|
||||
<version>1.2.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Decodes the Snappy variants Firehose delivers to S3 (issue #2328).
|
||||
Pinned because this module imports no BOM; keep it in step with the
|
||||
version the Quarkus BOM manages for the emulator itself. -->
|
||||
|
||||
+1245
File diff suppressed because it is too large.
Load diff
@@ -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.
|
||||
|
||||
+25
-7
@@ -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 (`<clientId>?x-amz-customauthorizer-name=<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:<region>:<account>:client/<clientId>`. 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 (`<clientId>?x-amz-customauthorizer-name=<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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<String> lines = pem.lines().toList();
|
||||
List<String> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, String> 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<String, String> headers = new LinkedHashMap<>();
|
||||
protocolData.path("http").path("headers").properties().forEach(header -> headers.put(header.getKey(), header.getValue().asText()));
|
||||
Map<String, String> 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<String> 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<String> 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<String, String> 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<String, String> 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<String, String> params(String username, Map<String, String> headers, String queryString) {
|
||||
// ponytail: names match case-insensitively in every source; precedence between sources is unmeasured on AWS.
|
||||
Map<String, String> 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<String, String> 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);
|
||||
}
|
||||
}
|
||||
@@ -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> iotService;
|
||||
private final IotCustomAuthorizer customAuthorizer;
|
||||
private final TlsConfigurationRegistry tlsRegistry;
|
||||
final WorkerExecutor ruleWorker;
|
||||
private final Map<String, ClientSession> 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<SocketAddress, IotCustomAuthorizer.WebSocketUpgrade> webSocketUpgrades = new ConcurrentHashMap<>();
|
||||
private MqttServer server;
|
||||
private MqttServer tlsServer;
|
||||
private ReloadingKeyManager keyManager;
|
||||
|
||||
@Inject
|
||||
public IotMqttBrokerService(EmulatorConfig config, Vertx vertx, Instance<IotService> 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<SocketAddress, IotCustomAuthorizer.WebSocketUpgrade> 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<String> 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()
|
||||
|
||||
@@ -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()}.
|
||||
*
|
||||
* <p>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<SocketAddress, IotCustomAuthorizer.WebSocketUpgrade> 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<String, String> 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. */
|
||||
|
||||
@@ -495,14 +495,30 @@ public class IotService {
|
||||
return false;
|
||||
}
|
||||
List<String> 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<String> 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<String> documents, Map<String, String> variables, String region, String accountId,
|
||||
String clientId) {
|
||||
if (documents.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Map<String, String> variables = connectionVariables(device, clientId, sourceIp, domainName);
|
||||
List<String> resolved = documents.stream().map(document -> resolvePolicyVariables(document, variables)).toList();
|
||||
Map<String, List<String>> conditionContext = new LinkedHashMap<>();
|
||||
variables.forEach((key, value) -> conditionContext.put(key, List.of(value)));
|
||||
String clientArn = AwsArnUtils.Arn.of("iot", device.region(), device.accountId(), "client/" + clientId).toString();
|
||||
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<String, String> connectionVariables(RegisteredDevice device, String clientId, String sourceIp, String domainName) {
|
||||
Map<String, String> variables = new LinkedHashMap<>();
|
||||
variables.put("iot:ClientId", clientId);
|
||||
if (sourceIp != null) {
|
||||
variables.put("aws:SourceIp", sourceIp);
|
||||
}
|
||||
if (domainName != null) {
|
||||
variables.put("iot:DomainName", domainName);
|
||||
}
|
||||
Map<String, String> variables = basicConnectionVariables(clientId, sourceIp, domainName);
|
||||
Optional<Thing> 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<String, String> basicConnectionVariables(String clientId, String sourceIp, String domainName) {
|
||||
Map<String, String> variables = new LinkedHashMap<>();
|
||||
variables.put("iot:ClientId", clientId);
|
||||
if (sourceIp != null) {
|
||||
variables.put("aws:SourceIp", sourceIp);
|
||||
}
|
||||
if (domainName != null) {
|
||||
variables.put("iot:DomainName", domainName);
|
||||
}
|
||||
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
|
||||
|
||||
+357
@@ -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<JsonNode> 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<String> 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<String, String> 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<String> protocols(JsonNode event) {
|
||||
List<String> 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));
|
||||
}
|
||||
}
|
||||
@@ -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<JsonNode> 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<String, String> headers = new LinkedHashMap<>();
|
||||
headers.put("host", "iot.example.com");
|
||||
headers.put("sec-websocket-protocol", "mqtt");
|
||||
|
||||
List<String> 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<String> 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());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
+28
-1
@@ -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> 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
|
||||
|
||||
+52
-4
@@ -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<Handler<Void>> 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<Handler<Void>> 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<Handler<Throwable>> 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<Handler<Void>> 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<Handler<Void>> socketClose = ArgumentCaptor.forClass(Handler.class);
|
||||
verify(socket).closeHandler(socketClose.capture());
|
||||
ArgumentCaptor<Handler<Void>> webSocketClose = ArgumentCaptor.forClass(Handler.class);
|
||||
verify(webSocket).closeHandler(webSocketClose.capture());
|
||||
ArgumentCaptor<Handler<Throwable>> 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<SocketAddress, IotCustomAuthorizer.WebSocketUpgrade> 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());
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user