From 9481654f5ba793ee4c2bdae8dbc0bcfb8fcd8926 Mon Sep 17 00:00:00 2001 From: Viacheslav Kukhtyn Date: Tue, 19 Jan 2021 20:32:18 +0200 Subject: [PATCH] Refactoring, deleting redundant classes --- .../credentials/AnonymousCredentials.java | 12 +++++- .../engine/credentials/BasicCredentials.java | 13 +++++- .../credentials/CertPemCredentials.java | 7 +++- .../ClientCredentials.java} | 21 ++++------ .../rule/engine/mqtt/TbMqttNode.java | 11 ++++- .../engine/mqtt/TbMqttNodeConfiguration.java | 8 ++-- .../mqtt/azure/AzureIotHubSasCredentials.java | 4 +- .../engine/mqtt/azure/TbAzureIotHubNode.java | 40 ++++++++----------- .../credentials/MqttAnonymousCredentials.java | 26 ------------ .../credentials/MqttBasicCredentials.java | 33 --------------- .../credentials/MqttCertPemCredentials.java | 26 ------------ .../rule/engine/rest/TbHttpClient.java | 24 ++++++----- .../rest/TbRestApiCallNodeConfiguration.java | 8 ++-- .../credentials/HttpAnonymousCredentials.java | 26 ------------ .../credentials/HttpBasicCredentials.java | 35 ---------------- .../credentials/HttpCertPemCredentials.java | 26 ------------ .../credentials/HttpClientCredentials.java | 32 --------------- ...alsTest.java => BasicCredentialsTest.java} | 10 ++--- 18 files changed, 92 insertions(+), 270 deletions(-) rename rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/{mqtt/credentials/MqttClientCredentials.java => credentials/ClientCredentials.java} (65%) delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttAnonymousCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttBasicCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttCertPemCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpAnonymousCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpBasicCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpCertPemCredentials.java delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpClientCredentials.java rename rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/credentials/{HttpBasicCredentialsTest.java => BasicCredentialsTest.java} (76%) diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java index 1228256b30..133d08fe1c 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java @@ -16,7 +16,17 @@ package org.thingsboard.rule.engine.credentials; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.netty.handler.ssl.SslContext; @JsonIgnoreProperties(ignoreUnknown = true) -public class AnonymousCredentials { +public class AnonymousCredentials implements ClientCredentials { + @Override + public CredentialsType getType() { + return CredentialsType.ANONYMOUS; + } + + @Override + public SslContext initSslContext() { + return null; + } } diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java index 35287f4d73..b8901c5d54 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java @@ -16,11 +16,22 @@ package org.thingsboard.rule.engine.credentials; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.netty.handler.ssl.SslContext; import lombok.Data; @Data @JsonIgnoreProperties(ignoreUnknown = true) -public class BasicCredentials { +public class BasicCredentials implements ClientCredentials { private String username; private String password; + + @Override + public CredentialsType getType() { + return CredentialsType.BASIC; + } + + @Override + public SslContext initSslContext() { + return null; + } } diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java index 5aa1ede5e9..a1c40f0d24 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java @@ -53,7 +53,7 @@ import java.security.spec.PKCS8EncodedKeySpec; @Data @Slf4j @JsonIgnoreProperties(ignoreUnknown = true) -public class CertPemCredentials { +public class CertPemCredentials implements ClientCredentials { private static final String TLS_VERSION = "TLSv1.2"; private String caCert; @@ -61,6 +61,11 @@ public class CertPemCredentials { private String privateKey; private String password; + @Override + public CredentialsType getType() { + return CredentialsType.CERT_PEM; + } + public SslContext initSslContext() { try { Security.addProvider(new BouncyCastleProvider()); diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttClientCredentials.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java similarity index 65% rename from rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttClientCredentials.java rename to rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java index e7c72b2ee7..c30fe68895 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttClientCredentials.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java @@ -13,31 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.thingsboard.rule.engine.mqtt.credentials; +package org.thingsboard.rule.engine.credentials; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import io.netty.handler.ssl.SslContext; -import org.thingsboard.mqtt.MqttClientConfig; -import org.thingsboard.rule.engine.credentials.CredentialsType; import org.thingsboard.rule.engine.mqtt.azure.AzureIotHubSasCredentials; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes({ - @JsonSubTypes.Type(value = MqttAnonymousCredentials.class, name = "anonymous"), - @JsonSubTypes.Type(value = MqttBasicCredentials.class, name = "basic"), + @JsonSubTypes.Type(value = AnonymousCredentials.class, name = "anonymous"), + @JsonSubTypes.Type(value = BasicCredentials.class, name = "basic"), @JsonSubTypes.Type(value = AzureIotHubSasCredentials.class, name = "sas"), - @JsonSubTypes.Type(value = MqttCertPemCredentials.class, name = "cert.PEM")}) -public interface MqttClientCredentials { + @JsonSubTypes.Type(value = CertPemCredentials.class, name = "cert.PEM")}) +public interface ClientCredentials { @JsonIgnore CredentialsType getType(); - default SslContext initSslContext() { - return null; - } - - default void configure(MqttClientConfig config) { - } + @JsonIgnore + SslContext initSslContext(); } - diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java index 47b950554a..89df78b148 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java @@ -31,6 +31,9 @@ import org.thingsboard.rule.engine.api.TbNode; import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.util.TbNodeUtils; +import org.thingsboard.rule.engine.credentials.BasicCredentials; +import org.thingsboard.rule.engine.credentials.ClientCredentials; +import org.thingsboard.rule.engine.credentials.CredentialsType; import org.thingsboard.server.common.data.plugin.ComponentType; import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsgMetaData; @@ -105,7 +108,13 @@ public class TbMqttNode implements TbNode { config.setClientId(this.mqttNodeConfiguration.getClientId()); } config.setCleanSession(this.mqttNodeConfiguration.isCleanSession()); - this.mqttNodeConfiguration.getCredentials().configure(config); + + ClientCredentials credentials = this.mqttNodeConfiguration.getCredentials(); + if (credentials.getType() == CredentialsType.BASIC) { + config.setUsername(((BasicCredentials) credentials).getUsername()); + config.setPassword(((BasicCredentials) credentials).getPassword()); + } + MqttClient client = MqttClient.create(config, null); client.setEventLoop(ctx.getSharedEventLoop()); Future connectFuture = client.connect(this.mqttNodeConfiguration.getHost(), this.mqttNodeConfiguration.getPort()); diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java index d8d265f9c6..e06824180e 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java @@ -17,8 +17,8 @@ package org.thingsboard.rule.engine.mqtt; import lombok.Data; import org.thingsboard.rule.engine.api.NodeConfiguration; -import org.thingsboard.rule.engine.mqtt.credentials.MqttAnonymousCredentials; -import org.thingsboard.rule.engine.mqtt.credentials.MqttClientCredentials; +import org.thingsboard.rule.engine.credentials.AnonymousCredentials; +import org.thingsboard.rule.engine.credentials.ClientCredentials; @Data public class TbMqttNodeConfiguration implements NodeConfiguration { @@ -31,7 +31,7 @@ public class TbMqttNodeConfiguration implements NodeConfiguration headers.add(TbNodeUtils.processPattern(k, metaData), TbNodeUtils.processPattern(v, metaData))); - addAuthorizationHeader(headers); + getBasicAuthHeaderValue(config.getCredentials()).ifPresent(authString -> headers.add("Authorization", authString)); return headers; } @@ -278,10 +281,13 @@ public class TbHttpClient { } } - private void addAuthorizationHeader(HttpHeaders headers) { - HttpClientCredentials credentials = config.getCredentials(); + public static Optional getBasicAuthHeaderValue(ClientCredentials credentials) { if (CredentialsType.BASIC == credentials.getType()) { - headers.add("Authorization", ((HttpBasicCredentials) credentials).getBasicAuthHeaderValue()); + BasicCredentials basicCredentials = (BasicCredentials) credentials; + String authString = basicCredentials.getUsername() + ":" + basicCredentials.getPassword(); + String encodedAuthString = new String(Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8))); + return Optional.of("Basic " + encodedAuthString); } + return Optional.empty(); } } diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java index 38936244ae..40b79163b8 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java @@ -18,8 +18,8 @@ package org.thingsboard.rule.engine.rest; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import org.thingsboard.rule.engine.api.NodeConfiguration; -import org.thingsboard.rule.engine.rest.credentials.HttpAnonymousCredentials; -import org.thingsboard.rule.engine.rest.credentials.HttpClientCredentials; +import org.thingsboard.rule.engine.credentials.AnonymousCredentials; +import org.thingsboard.rule.engine.credentials.ClientCredentials; import java.util.Collections; import java.util.Map; @@ -44,7 +44,7 @@ public class TbRestApiCallNodeConfiguration implements NodeConfiguration