Skip to content

feature: [34] Implement TLS connection#171

Open
crazyrokr wants to merge 33 commits into
developfrom
feature/34-implement-tls-connection
Open

feature: [34] Implement TLS connection#171
crazyrokr wants to merge 33 commits into
developfrom
feature/34-implement-tls-connection

Conversation

@crazyrokr
Copy link
Copy Markdown
Collaborator

@crazyrokr crazyrokr commented May 17, 2026

Issue

#34

Description

This pull request adds comprehensive support for Transport Layer Security (TLS) to the MQTT broker, enabling secure client-server communication. The implementation covers the entire stack, from network-level SSL/TLS handling to broker-side configuration and integration testing.

Changes

  • Network Layer:
    • Implemented MqttSslConnection and associated readers/writers for SSL handling.
    • Added TlsProperties for flexible SSL configuration.
    • Refactored message handling by extracting MqttPacketCreator to decouple logic from PlainMqttMessageReader and SslMqttMessageReader.
  • Core Service Integration:
    • Introduced MqttSslConnectionFactory to manage secure connections.
    • Updated existing connection and message processing services to support SSL-enabled transports.
  • Application Configuration:
    • Added MqttTlsSpringConfig to enable and configure TLS support within the Spring context.
  • Testing:
    • Added extensive integration tests (TlsCommunicationTest, TlsIntegrationSpecification) to verify secure end-to-end communication.

Notes:

  • TLS support is integrated as a configurable option alongside plain connections.
  • The separation of MqttPacketCreator improves code maintainability and allows cleaner reuse of message creation logic across different transport types.

@crazyrokr crazyrokr linked an issue May 17, 2026 that may be closed by this pull request
@crazyrokr crazyrokr self-assigned this May 17, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 17, 2026

Test Coverage Report

Overall Project 85.49% -0.48% 🍏
Files changed 84.81% 🍏

File Coverage
MqttExternalPlainNetworkConfig.java 100% 🍏
TlsProperties.java 100% 🍏
TlsMqttMessageReader.java 100% 🍏
TlsMqttMessageWriter.java 100% 🍏
TlsMqttConnectionFactory.java 100% 🍏
PlainMqttConnectionFactory.java 100% 🍏
PlainMqttMessageWriter.java 100% 🍏
PlainMqttMessageReader.java 100% 🍏
MqttBrokerSpringConfig.java 98.78% 🍏
MqttPacketCodec.java 93.02% 🍏
TlsMqttConnection.java 83.77% -16.23% 🍏
MqttConnection.java 74.86% -13.14% 🍏
MqttExternalTlsNetworkConfig.java 70.62% -29.38% 🍏
PropertyAssert.java 21.54% -18.46%
TlsProtocolException.java 0%

@crazyrokr crazyrokr requested a review from JavaSaBr May 18, 2026 20:27
@crazyrokr crazyrokr marked this pull request as ready for review May 18, 2026 20:28
Comment thread buildSrc/src/main/groovy/configure-java.gradle Outdated
Comment thread test-support/src/main/groovy/javasabr/mqtt/test/support/TestSslContexts.groovy Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces end-to-end TLS support for the MQTT broker, spanning the network transport (TLS connection + SSL packet reader/writer), Spring Boot application configuration, and integration tests to validate secure client communication.

Changes:

  • Added TLS transport implementation (TlsMqttConnection, TLS reader/writer) and configuration model (TlsProperties).
  • Refactored packet parsing by extracting MqttPacketCreator and splitting plain vs TLS message readers/writers.
  • Added Spring configuration and integration tests to bootstrap a TLS listener and verify MQTT 3.1.1 / MQTT 5 communication over TLS.

Reviewed changes

Copilot reviewed 37 out of 38 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
test-support/src/main/groovy/javasabr/mqtt/test/support/TestSslContexts.groovy Generates temporary keystores/truststores for TLS tests.
network/src/test/groovy/javasabr/mqtt/network/TlsMqttConnectionTest.groovy Adds a basic TLS connection construction test.
network/src/test/groovy/javasabr/mqtt/network/message/PlainMqttMessageReaderTest.groovy Adds a regression test for incomplete MBI handling.
network/src/main/java/javasabr/mqtt/network/TlsProperties.java Introduces validated TLS configuration record.
network/src/main/java/javasabr/mqtt/network/TlsMqttConnection.java Adds TLS-backed MQTT connection implementation.
network/src/main/java/javasabr/mqtt/network/MqttConnectionFactory.java Generalizes connection factory with generics.
network/src/main/java/javasabr/mqtt/network/MqttConnection.java Refactors to use MqttPacketCreator + plain reader/writer; improves close logging.
network/src/main/java/javasabr/mqtt/network/message/ssl/TlsMqttMessageWriter.java Implements SSL-aware writer for MQTT packets.
network/src/main/java/javasabr/mqtt/network/message/ssl/TlsMqttMessageReader.java Implements SSL-aware reader for MQTT packets.
network/src/main/java/javasabr/mqtt/network/message/ssl/package-info.java Adds @NullMarked package annotation for ssl message package.
network/src/main/java/javasabr/mqtt/network/message/plain/PlainMqttMessageWriter.java Renames/moves plain writer into dedicated package.
network/src/main/java/javasabr/mqtt/network/message/plain/PlainMqttMessageReader.java Introduces plain reader using shared MqttPacketCreator.
network/src/main/java/javasabr/mqtt/network/message/MqttPacketCreator.java Extracts packet length parsing + packet instantiation logic.
network/src/main/java/javasabr/mqtt/network/message/MqttMessageReader.java Removes old reader (logic moved into creator + plain reader).
network/src/main/java/javasabr/mqtt/network/exception/TlsProtocolException.java Adds TLS-specific runtime exception wrapper.
network/build.gradle Adjusts network module dependencies (logging deps moved to convention).
gradle/libs.versions.toml Bumps RLib + HiveMQ client versions.
embedded/build.gradle Removes explicit logging dependency (moved to convention).
credentials-source-db/src/main/java/javasabr/mqtt/auth/credentials/source/DatabaseCredentialsSource.java Switches debug output/toString to DebugUtils JSON representation.
core-service/src/test/groovy/javasabr/mqtt/service/IntegrationServiceSpecification.groovy Updates test setup to supply MqttPacketCreator.
core-service/src/test/groovy/javasabr/mqtt/service/impl/TlsMqttConnectionFactoryTest.groovy Adds test for TLS factory allocator sharing.
core-service/src/main/java/javasabr/mqtt/service/impl/TlsMqttConnectionFactory.java Adds TLS connection factory wiring SSLContext + TLS props.
core-service/src/main/java/javasabr/mqtt/service/impl/PlainMqttConnectionFactory.java Renames/updates plain connection factory to pass MqttPacketCreator.
buildSrc/src/test/resources/log4j2-test.xml Adds buildSrc test Log4j2 config.
buildSrc/src/main/groovy/configure-java.gradle Adds logging deps to convention and adjusts test runtime classpath.
build.gradle Centralizes excludes and adds forced SLF4J resolution strategy.
application/src/test/resources/test-acl.gacl Extends test ACL rules for TLS client IDs/topics.
application/src/test/groovy/javasabr/mqtt/broker/application/TlsIntegrationSpecification.groovy Adds TLS client builders and TLS Spring test configuration.
application/src/test/groovy/javasabr/mqtt/broker/application/TlsCommunicationTest.groovy Adds TLS connect/publish tests for MQTT 3.1.1 and MQTT 5.
application/src/test/groovy/javasabr/mqtt/broker/application/TestSslPropertiesInitializer.groovy Injects generated keystore/truststore paths into Spring test env.
application/src/test/groovy/javasabr/mqtt/broker/application/IntegrationSpecification.groovy Switches test config to plain-network test config.
application/src/test/groovy/javasabr/mqtt/broker/application/config/TlsNetworkTestConfig.groovy Adds Spring test config to start TLS network on an ephemeral port.
application/src/test/groovy/javasabr/mqtt/broker/application/config/PlainNetworkTestConfig.groovy Adds Spring test config to start plain network on an ephemeral port.
application/src/test/groovy/javasabr/mqtt/broker/application/config/MqttTlsSpringConfigTest.groovy Adds unit test for TLS property mapping.
application/src/test/groovy/javasabr/mqtt/broker/application/config/MqttBrokerTestConfig.groovy Removes older test config that probed for random ports.
application/src/main/java/javasabr/mqtt/broker/application/config/MqttTlsSpringConfig.java Adds Spring beans for TLS properties, SSLContext, TLS network + starter.
application/src/main/java/javasabr/mqtt/broker/application/config/MqttBrokerSpringConfig.java Wires MqttPacketCreator, plain connection factory, and imports TLS config.
application/build.gradle Removes explicit logging deps (moved to convention).

Comment thread network/src/main/java/javasabr/mqtt/network/TlsMqttConnection.java
Comment thread network/src/main/java/javasabr/mqtt/network/MqttConnection.java
Comment thread buildSrc/src/test/resources/log4j2-test.xml Outdated
@crazyrokr crazyrokr requested a review from JavaSaBr May 27, 2026 09:12
@Value("${mqtt.external.tls.truststore-path:}") String truststorePath,
@Value("${mqtt.external.tls.truststore-password:}") String truststorePassword,
@Value("${mqtt.external.tls.truststore-type:PKCS12}") String truststoreType,
@Value("${mqtt.external.tls.require-client-cert:true}") boolean requireClientCert,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 'require-client-cert' should not be true by default, because this connection can be used just to have encrypted traffic

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero Trust is a preferable strategy in terms of private network. Disabling client certificate validation is appropriate only for public network. If the developer still wants to trust all clients indiscriminately, he can always disable this check using the property

Comment thread network/src/main/java/javasabr/mqtt/network/MqttConnection.java Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 38 out of 39 changed files in this pull request and generated 11 comments.

Comment on lines +41 to +45
MqttPacketCodec mqttPacketCodec) {
this.sslEngine = sslContext.createSSLEngine();
super(network, channel, bufferAllocator, maxPacketsByRead, serverConnectionConfig, mqttUserFactory, mqttPacketCodec);
this.sslEngine.setUseClientMode(clientMode);
this.sslEngine.setNeedClientAuth(tlsProperties.requireClientCert());
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a new Java 25 feature, so the comment is not relevant

Comment thread buildSrc/src/main/groovy/configure-java.gradle Outdated
Comment thread network/src/main/java/javasabr/mqtt/network/TlsProperties.java Outdated
Comment thread buildSrc/src/main/groovy/configure-java.gradle Outdated
Comment on lines +95 to +96
Files.list(tempDir).forEach { Files.deleteIfExists(it) }
Files.deleteIfExists(tempDir)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream inherit nor Closable neither AutoClosable, so the comment is not relevant

Comment on lines 50 to 54
super(network, channel, bufferAllocator, maxPacketsByRead);
this.serverConnectionConfig = serverConnectionConfig;
this.packetReader = createPacketReader();
this.packetWriter = createPacketWriter();
this.packetReader = createPacketReader(mqttPacketCodec);
this.packetWriter = createPacketWriter(mqttPacketCodec);
this.user = mqttUserFactory.createNetworkUser(this);
crazyrokr and others added 7 commits May 27, 2026 19:51
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement TLS connection

3 participants