Skip to content

Commit

Permalink
Merge pull request #122 from mp-loki/master
Browse files Browse the repository at this point in the history
Fix for OPC-UA extention security type 'None'
  • Loading branch information
mp-loki authored Sep 13, 2018
2 parents 17fb359 + 973df38 commit e32492e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>org.thingsboard</groupId>
<artifactId>gateway</artifactId>
<version>2.0.4-SNAPSHOT</version>
<version>2.1.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Thingsboard Gateway</name>
Expand Down Expand Up @@ -57,7 +57,7 @@
<pkg.unixLogFolder>/var/log/${pkg.name}</pkg.unixLogFolder>
<pkg.installFolder>/usr/share/${pkg.name}</pkg.installFolder>
<pkg.win.dist>${project.build.directory}/windows</pkg.win.dist>
<thingsboard.version>2.0.4-SNAPSHOT</thingsboard.version>
<thingsboard.version>2.1.1-SNAPSHOT</thingsboard.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright © 2017 The Thingsboard Authors
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -18,6 +18,7 @@
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder;
import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider;
import org.eclipse.milo.opcua.sdk.client.api.nodes.VariableNode;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
Expand All @@ -42,6 +43,8 @@
import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -82,7 +85,6 @@ public OpcUaServerMonitor(GatewayService gateway, OpcUaServerConfiguration confi
public void connect(Boolean isRemote) {
try {
log.info("Initializing OPC-UA server connection to [{}:{}]!", configuration.getHost(), configuration.getPort());
CertificateInfo certificate = ConfigurationTools.loadCertificate(configuration.getKeystore(), isRemote);

SecurityPolicy securityPolicy = SecurityPolicy.valueOf(configuration.getSecurity());
IdentityProvider identityProvider = configuration.getIdentity().toProvider();
Expand All @@ -93,15 +95,7 @@ public void connect(Boolean isRemote) {
.filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getSecurityPolicyUri()))
.findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));

OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english(configuration.getApplicationName()))
.setApplicationUri(configuration.getApplicationUri())
.setCertificate(certificate.getCertificate())
.setKeyPair(certificate.getKeyPair())
.setEndpoint(endpoint)
.setIdentityProvider(identityProvider)
.setRequestTimeout(uint(configuration.getTimeoutInMillis()))
.build();
OpcUaClientConfig config = getOpcUaClientConfig(securityPolicy, identityProvider, endpoint, isRemote);

client = new OpcUaClient(config);
client.connect().get();
Expand All @@ -115,6 +109,25 @@ public void connect(Boolean isRemote) {
}
}

private OpcUaClientConfig getOpcUaClientConfig(SecurityPolicy securityPolicy, IdentityProvider identityProvider, EndpointDescription endpoint, boolean isRemote) throws GeneralSecurityException, IOException {

OpcUaClientConfigBuilder builder = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english(configuration.getApplicationName()))
.setApplicationUri(configuration.getApplicationUri())

.setEndpoint(endpoint)
.setIdentityProvider(identityProvider)
.setRequestTimeout(uint(configuration.getTimeoutInMillis()));


if (securityPolicy != SecurityPolicy.None) {
CertificateInfo certificate = ConfigurationTools.loadCertificate(configuration.getKeystore(), isRemote);
builder.setCertificate(certificate.getCertificate())
.setKeyPair(certificate.getKeyPair());
}
return builder.build();
}

public void disconnect() {
if (client != null) {
log.info("Disconnecting from OPC-UA server!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ private boolean checkOutgoingQueueIsEmpty() {
outgoingQueue = new ConcurrentLinkedQueue();
return true;
}
log.info("Outgoing queue is not empty. [{}] messages are still in progress", pendingCount);
return false;
}
return true;
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/thingsboard/gateway/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@
import org.springframework.context.annotation.Configuration;
import org.thingsboard.gateway.mqtt.TestMqttHandler;

import java.util.TimeZone;

/**
* Created by Valerii Sosliuk on 6/12/2018.
*/
@Configuration
@ComponentScan("org.thingsboard.gateway")
public class TestConfiguration {

@Bean
public TimeZone timeZone(){
TimeZone defaultTimeZone = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(defaultTimeZone);
return defaultTimeZone;
}

@Bean
public TestMqttHandler getMqttHandler() {
return new TestMqttHandler();
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</layout>
</appender>

<logger name="org.thingsboard" level="TRACE" />
<logger name="org.thingsboard" level="DEBUG" />
<logger name="org.eclipse.milo" level="INFO" />
<logger name="org.eclipse.paho" level="INFO" />
<logger name="io.moquette.spi" level="WARN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"SN-001":[{"ts":1528651057789,"values":{"humidity":0.68}}]}
{"SN-001":[{"ts":1528636657789,"values":{"humidity":0.68}}]}

0 comments on commit e32492e

Please sign in to comment.