You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ld...@apache.org on 2025/03/03 19:45:44 UTC
(plc4x) branch issue/1897 created (now 9a7cadba45)
This is an automated email from the ASF dual-hosted git repository.
ldywicki pushed a change to branch issue/1897
in repository https://gitbox.apache.org/repos/asf/plc4x.git
at 9a7cadba45 feat(plc4j/opcua): Remove additional hostname resolution within connect attempts.
This branch includes the following new commits:
new 9a7cadba45 feat(plc4j/opcua): Remove additional hostname resolution within connect attempts.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(plc4x) 01/01: feat(plc4j/opcua): Remove additional hostname resolution within connect attempts.
Posted by ld...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ldywicki pushed a commit to branch issue/1897
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 9a7cadba458002d4f2b307b7179c1cc31256e251
Author: Łukasz Dywicki <lu...@code-house.org>
AuthorDate: Mon Mar 3 23:41:51 2025 +0400
feat(plc4j/opcua): Remove additional hostname resolution within connect attempts.
Closes #1897. Removed code which caused additional DNS lookups in favor of endpoint-port and endpoint-host options provided with plc4j 0.13.
Signed-off-by: Łukasz Dywicki <lu...@code-house.org>
---
.../java/opcua/config/OpcuaConfiguration.java | 4 +-
.../java/opcua/context/OpcuaDriverContext.java | 7 ---
.../plc4x/java/opcua/context/SecureChannel.java | 67 ++++++++--------------
3 files changed, 26 insertions(+), 52 deletions(-)
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 6548854009..54854ffd76 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
@@ -136,11 +136,11 @@ public class OpcuaConfiguration implements PlcConnectionConfiguration {
private Limits limits;
@ConfigurationParameter("endpoint-host")
- @Description("Endpoint host used to establish secure channel.")
+ @Description("Endpoint host used to establish secure channel connection. Used when client made connection to server which advertises different hostname than one used for network connection.")
private String endpointHost;
@ConfigurationParameter("endpoint-port")
- @Description("Endpoint port used to establish secure channel")
+ @Description("Endpoint port used to establish secure channel. Used when client made connection to server which advertises different port number than one used for network connection.")
private Integer endpointPort;
public String getProtocolCode() {
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
index 2d05af9987..a24c65798e 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
@@ -139,13 +139,6 @@ public class OpcuaDriverContext implements DriverContext, HasConfiguration<Opcua
port = matcher.group("transportPort");
transportEndpoint = matcher.group("transportEndpoint");
- if (configuration.getEndpointHost() != null) {
- host = configuration.getEndpointHost();
- }
- if (configuration.getEndpointPort() != null) {
- port = String.valueOf(configuration.getEndpointPort());
- }
-
String portAddition = port != null ? ":" + port : "";
endpoint = "opc." + code + "://" + host + portAddition + transportEndpoint;
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 99ce4c3e8f..3a39e00de3 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
@@ -28,9 +28,7 @@ import java.security.Signature;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
-import java.util.Collection;
import java.util.Comparator;
-import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
@@ -54,8 +52,6 @@ import org.apache.plc4x.java.spi.transaction.RequestTransactionManager.RequestTr
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
@@ -95,7 +91,6 @@ public class SecureChannel {
private final OpcuaDriverContext driverContext;
private final Conversation conversation;
private ScheduledFuture<?> keepAlive;
- private final Set<String> endpoints = new HashSet<>();
private double sessionTimeout;
private long revisedLifetime;
@@ -118,17 +113,6 @@ public class SecureChannel {
this.password = configuration.getPassword();
}
- // Generate a list of endpoints we can use.
- try {
- InetAddress address = InetAddress.getByName(driverContext.getHost());
- this.endpoints.add("opc.tcp://" + address.getHostAddress() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
- this.endpoints.add("opc.tcp://" + address.getHostName() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
- this.endpoints.add("opc.tcp://" + address.getCanonicalHostName() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
- } catch (UnknownHostException e) {
- LOGGER.warn("Unable to resolve host name. Using original host from connection string which may cause issues connecting to server");
- this.endpoints.add(driverContext.getHost());
- }
-
if (conversation.getSecurityPolicy() == SecurityPolicy.NONE) {
this.localCertificateString = NULL_BYTE_STRING;
this.remoteCertificateThumbprint = NULL_BYTE_STRING;
@@ -314,23 +298,10 @@ public class SecureChannel {
conversation.setRemoteCertificate(getX509Certificate(sessionResponse.getServerCertificate().getStringValue()));
conversation.setRemoteNonce(sessionResponse.getServerNonce().getStringValue());
- List<String> contactPoints = new ArrayList<>(3);
- String port = driverContext.getPort() == null ? "" : ":" + driverContext.getPort();
- try {
- InetAddress address = InetAddress.getByName(driverContext.getHost());
- contactPoints.add("opc.tcp://" + address.getHostAddress() + port + driverContext.getTransportEndpoint());
- contactPoints.add("opc.tcp://" + address.getHostName() + port + driverContext.getTransportEndpoint());
- contactPoints.add("opc.tcp://" + address.getCanonicalHostName() + port + driverContext.getTransportEndpoint());
- } catch (UnknownHostException e) {
- // fall back to declared host
- contactPoints.add("opc.tcp://" + driverContext.getHost() + port + driverContext.getTransportEndpoint());
- LOGGER.warn("Could not reach host {}, possible network failure", driverContext.getHost(), e);
- }
-
- Entry<EndpointDescription, UserTokenPolicy> selectedEndpoint = selectEndpoint(sessionResponse.getServerEndpoints(), contactPoints,
+ Entry<EndpointDescription, UserTokenPolicy> selectedEndpoint = selectEndpoint(sessionResponse.getServerEndpoints(),
configuration.getSecurityPolicy(), configuration.getMessageSecurity());
if (selectedEndpoint == null) {
- throw new PlcRuntimeException("Unable to find endpoint matching " + contactPoints.get(0));
+ throw new PlcRuntimeException("Unable to find endpoint matching " + driverContext.getEndpoint());
}
PascalString policyId = selectedEndpoint.getValue().getPolicyId();
@@ -421,7 +392,8 @@ public class SecureChannel {
);
return conversation.submit(endpointsRequest, GetEndpointsResponse.class).thenApply(response -> {
- Entry<EndpointDescription, UserTokenPolicy> entry = selectEndpoint(response.getEndpoints(), this.endpoints, this.configuration.getSecurityPolicy(), this.configuration.getMessageSecurity());
+ Entry<EndpointDescription, UserTokenPolicy> entry = selectEndpoint(response.getEndpoints(),
+ this.configuration.getSecurityPolicy(), this.configuration.getMessageSecurity());
if (entry == null) {
Set<String> endpointUris = response.getEndpoints().stream()
@@ -494,19 +466,18 @@ public class SecureChannel {
* Selects the endpoint and authentication policy based on client settings.
*
* @param extensionObjects Endpoint descriptions returned by the server.
- * @param contactPoints Contact points expected by client.
* @param securityPolicy Security policy searched in endpoints.
* @param messageSecurity Message security needed by client.
* @return Endpoint matching given.
*/
- private Entry<EndpointDescription, UserTokenPolicy> selectEndpoint(List<EndpointDescription> extensionObjects, Collection<String> contactPoints,
+ private Entry<EndpointDescription, UserTokenPolicy> selectEndpoint(List<EndpointDescription> extensionObjects,
SecurityPolicy securityPolicy, MessageSecurity messageSecurity) throws PlcRuntimeException {
// Get a list of the endpoints which match ours.
MessageSecurityMode effectiveMessageSecurity = SecurityPolicy.NONE == securityPolicy ? MessageSecurityMode.messageSecurityModeNone : messageSecurity.getMode();
List<Entry<EndpointDescription, UserTokenPolicy>> serverEndpoints = new ArrayList<>();
for (EndpointDescription endpointDescription : extensionObjects) {
- if (isMatchingEndpoint(endpointDescription, contactPoints)) {
+ if (isMatchingEndpointDescription(endpointDescription)) {
boolean policyMatch = endpointDescription.getSecurityPolicyUri().getStringValue().equals(securityPolicy.getSecurityPolicyUri());
boolean msgSecurityMatch = endpointDescription.getSecurityMode().equals(effectiveMessageSecurity);
@@ -530,22 +501,32 @@ public class SecureChannel {
return serverEndpoints.get(0);
}
+ private boolean isMatchingEndpointDescription(EndpointDescription endpointDescription) {
+ if (isMatchingEndpoint(endpointDescription, driverContext.getHost(), driverContext.getPort(), driverContext.getTransportEndpoint())) {
+ return true;
+ }
+ if (configuration.getEndpointHost() != null) {
+ return isMatchingEndpoint(endpointDescription, configuration.getEndpointHost(), configuration.getEndpointPort() == null ? driverContext.getPort() : String.valueOf(configuration.getEndpointPort()), driverContext.getTransportEndpoint());
+ } else if (configuration.getEndpointPort() != null) {
+ return isMatchingEndpoint(endpointDescription, driverContext.getHost(), configuration.getEndpointPort().toString(), driverContext.getTransportEndpoint());
+ }
+ return false;
+ }
+
/**
* Checks each component of the return endpoint description against the connection string.
* If all are correct then return true.
*
* @param endpoint - EndpointDescription returned from server
+ * @param host Permitted host
+ * @param port Permitted port
+ * @param transportEndpoint Transport endpoint
* @return true if this endpoint matches our configuration
* @throws PlcRuntimeException - If the returned endpoint string doesn't match the format expected
*/
- private static boolean isMatchingEndpoint(EndpointDescription endpoint, Collection<String> contactPoints) throws PlcRuntimeException {
- // Split up the connection string into it's individual segments.
- for (String contactPoint : contactPoints) {
- if (endpoint.getEndpointUrl().getStringValue().startsWith(contactPoint)) {
- return true;
- }
- }
- return false;
+ private static boolean isMatchingEndpoint(EndpointDescription endpoint, String host, String port, String transportEndpoint) throws PlcRuntimeException {
+ String portAddition = port == null ? "" : ":" + port;
+ return endpoint.getEndpointUrl().getStringValue().startsWith("opc.tcp://" + host + portAddition + transportEndpoint);
}
/**