You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2021/08/20 20:15:46 UTC
[plc4x] branch develop updated: [PLC4X-303] Support hostname lookup
when resolving endpoints
This is an automated email from the ASF dual-hosted git repository.
hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 553abf1 [PLC4X-303] Support hostname lookup when resolving endpoints
553abf1 is described below
commit 553abf1a1177b331ab03ff1ae3d135ded595f718
Author: hutcheb <hu...@apache.org>
AuthorDate: Sat Aug 21 06:14:56 2021 +1000
[PLC4X-303] Support hostname lookup when resolving endpoints
---
.../apache/plc4x/java/opcua/OpcuaPlcDriver.java | 1 +
.../java/opcua/config/OpcuaConfiguration.java | 7 ++++
.../plc4x/java/opcua/context/SecureChannel.java | 45 +++++++++++++++-------
3 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
index 7ad5fe2..088853c 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
@@ -169,6 +169,7 @@ public class OpcuaPlcDriver extends GeneratedDriverBase<OpcuaAPU> {
configuration.setTransportCode(transportCode);
configuration.setHost(transportHost);
configuration.setPort(transportPort);
+ configuration.setTransportEndpoint(transportEndpoint);
configuration.setEndpoint("opc." + transportCode + "://" + transportHost + ":" + transportPort + "" + transportEndpoint);
// Try to find a transport in order to create a communication channel.
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/config/OpcuaConfiguration.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/config/OpcuaConfiguration.java
index 083a4e2..bc31751 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/config/OpcuaConfiguration.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/config/OpcuaConfiguration.java
@@ -56,6 +56,7 @@ public class OpcuaConfiguration implements Configuration, TcpTransportConfigurat
private String host;
private String port;
private String endpoint;
+ private String transportEndpoint;
private String params;
private Boolean isEncrypted = false;
private PascalByteString thumbprint;
@@ -170,6 +171,10 @@ public class OpcuaConfiguration implements Configuration, TcpTransportConfigurat
return endpoint;
}
+ public String getTransportEndpoint() {
+ return transportEndpoint;
+ }
+
public byte[] getSenderCertificate() {
return this.senderCertificate;
}
@@ -190,6 +195,8 @@ public class OpcuaConfiguration implements Configuration, TcpTransportConfigurat
this.endpoint = endpoint;
}
+ public void setTransportEndpoint(String transportEndpoint) { this.transportEndpoint = transportEndpoint; }
+
public void openKeyStore() throws Exception {
this.isEncrypted = true;
File securityTempDir = new File(certDirectory, "security");
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
index 43974c8..250fe3b 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
@@ -35,6 +35,8 @@ import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;
@@ -466,31 +468,46 @@ public class SecureChannel {
}
}
- private void onConnectActivateSessionRequest(ConversationContext<OpcuaAPU> context, CreateSessionResponse opcuaMessageResponse, CreateSessionResponse sessionResponse) throws PlcConnectionException {
+ private void onConnectActivateSessionRequest(ConversationContext<OpcuaAPU> context, CreateSessionResponse opcuaMessageResponse, CreateSessionResponse sessionResponse) throws PlcConnectionException, ParseException {
senderCertificate = sessionResponse.getServerCertificate().getStringValue();
encryptionHandler.setServerCertificate(EncryptionHandler.getCertificateX509(senderCertificate));
this.senderNonce = sessionResponse.getServerNonce().getStringValue();
UserTokenType tokenType = UserTokenType.userTokenTypeAnonymous;
+ String[] endpoints = new String[3];
+ try {
+ InetAddress address = InetAddress.getByName(this.configuration.getHost());
+ endpoints[0] = "opc.tcp://" + address.getHostAddress() + ":" + configuration.getPort() + configuration.getTransportEndpoint();
+ endpoints[1] = "opc.tcp://" + address.getHostName() + ":" + configuration.getPort() + configuration.getTransportEndpoint();
+ endpoints[2] = "opc.tcp://" + address.getCanonicalHostName() + ":" + configuration.getPort() + configuration.getTransportEndpoint();
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ }
- for (ExtensionObjectDefinition extensionObject: sessionResponse.getServerEndpoints()) {
- EndpointDescription endpointDescription = (EndpointDescription) extensionObject;
- if (endpointDescription.getEndpointUrl().getStringValue().equals(this.endpoint.getStringValue())) {
- for (ExtensionObjectDefinition userTokenCast : endpointDescription.getUserIdentityTokens()) {
- UserTokenPolicy identityToken = (UserTokenPolicy) userTokenCast;
- if ((identityToken.getTokenType() == UserTokenType.userTokenTypeAnonymous) && (this.username == null)) {
- LOGGER.info("Using Endpoint {} with security {}", endpointDescription.getEndpointUrl().getStringValue(), identityToken.getPolicyId().getStringValue());
- policyId = identityToken.getPolicyId();
- tokenType = identityToken.getTokenType();
- } else if ((identityToken.getTokenType() == UserTokenType.userTokenTypeUserName) && (this.username != null)) {
- LOGGER.info("Using Endpoint {} with security {}", endpointDescription.getEndpointUrl().getStringValue(), identityToken.getPolicyId().getStringValue());
- policyId = identityToken.getPolicyId();
- tokenType = identityToken.getTokenType();
+ for (String hostEndpoints : endpoints) {
+ for (ExtensionObjectDefinition extensionObject : sessionResponse.getServerEndpoints()) {
+ EndpointDescription endpointDescription = (EndpointDescription) extensionObject;
+ if (endpointDescription.getEndpointUrl().getStringValue().equals(hostEndpoints)) {
+ for (ExtensionObjectDefinition userTokenCast : endpointDescription.getUserIdentityTokens()) {
+ UserTokenPolicy identityToken = (UserTokenPolicy) userTokenCast;
+ if ((identityToken.getTokenType() == UserTokenType.userTokenTypeAnonymous) && (this.username == null)) {
+ LOGGER.info("Using Endpoint {} with security {}", endpointDescription.getEndpointUrl().getStringValue(), identityToken.getPolicyId().getStringValue());
+ policyId = identityToken.getPolicyId();
+ tokenType = identityToken.getTokenType();
+ } else if ((identityToken.getTokenType() == UserTokenType.userTokenTypeUserName) && (this.username != null)) {
+ LOGGER.info("Using Endpoint {} with security {}", endpointDescription.getEndpointUrl().getStringValue(), identityToken.getPolicyId().getStringValue());
+ policyId = identityToken.getPolicyId();
+ tokenType = identityToken.getTokenType();
+ }
}
}
}
}
+ if (this.policyId == null) {
+ throw new PlcRuntimeException("Unable to find endpoint - " + endpoints[1]);
+ }
+
ExtensionObject userIdentityToken = getIdentityToken(tokenType, policyId.getStringValue());
int requestHandle = getRequestHandle();