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 2024/10/17 22:41:48 UTC
(plc4x) 01/01: fix(plc4j): Adjust endpoint selection logic.
This is an automated email from the ASF dual-hosted git repository.
ldywicki pushed a commit to branch opc-ua-starfive-server
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit bf2ebe85c496603d091ea9f5ab8a5cb2d9985af7
Author: Łukasz Dywicki <lu...@code-house.org>
AuthorDate: Fri Oct 18 00:37:43 2024 +0200
fix(plc4j): Adjust endpoint selection logic.
Amended selection logic made use of streams with side effects.
When server had more than one endpoint and these endpoints had same policy (i.e. anonymous) with different identifiers (i.e. anonymous_1, anonymous_2), it lead to wrong activate session requests which mixed endpoint and user token policy.
Updated logic removes side effects and makes sure that only policies of matched server endpoint are tested against client configuration.
Closes #1705.
Signed-off-by: Łukasz Dywicki <lu...@code-house.org>
---
.../plc4x/java/opcua/context/SecureChannel.java | 64 +++++++++++-----------
1 file changed, 31 insertions(+), 33 deletions(-)
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 44427db25a..fce73e460f 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
@@ -18,6 +18,7 @@
*/
package org.apache.plc4x.java.opcua.context;
+import static java.util.Map.entry;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static org.apache.plc4x.java.opcua.readwrite.ChunkType.*;
@@ -27,6 +28,7 @@ import java.security.Signature;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
+import java.util.Map.Entry;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -56,7 +58,6 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import java.util.stream.Stream;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
@@ -324,12 +325,17 @@ public class SecureChannel {
LOGGER.debug("error getting host", e);
}
- selectEndpoint(sessionResponse);
-
- if (this.policyId == null) {
+ Entry<EndpointDescription, UserTokenPolicy> endpointAndAuthPolicy = selectEndpoint(sessionResponse);
+ if (endpointAndAuthPolicy == null) {
throw new PlcRuntimeException("Unable to find endpoint - " + endpoints[1]);
}
+ //Entry<EndpointDescription, UserTokenPolicy> endpointAndAuthPolicy = selectEndpoint(sessionResponse);
+
+ policyId = endpointAndAuthPolicy.getValue().getPolicyId();
+ tokenType = endpointAndAuthPolicy.getValue().getTokenType();
+
+ System.out.println(policyId.getStringValue() + " " + policyId);
ExtensionObject userIdentityToken = getIdentityToken(this.tokenType, policyId.getStringValue());
RequestHeader requestHeader = conversation.createRequestHeader();
SignatureData clientSignature = new SignatureData(NULL_STRING, NULL_BYTE_STRING);
@@ -501,31 +507,28 @@ public class SecureChannel {
}
/**
- * Selects the endpoint to use based on the connection string provided.
- * If Discovery is disabled it will use the host address return from the server
+ * Selects the endpoint to use based on the connection string provided. If Discovery is disabled
+ * it will use the host address return from the server
*
* @param sessionResponse - The CreateSessionResponse message returned by the server
- * @throws PlcRuntimeException - If no endpoint with a compatible policy is found raise and error.
*/
- private void selectEndpoint(CreateSessionResponse sessionResponse) throws PlcRuntimeException {
+ private Entry<EndpointDescription, UserTokenPolicy> selectEndpoint(CreateSessionResponse sessionResponse) {
// Get a list of the endpoints which match ours.
- Stream<EndpointDescription> filteredEndpoints = sessionResponse.getServerEndpoints().stream()
- .map(e -> (EndpointDescription) e)
- .filter(this::isEndpoint);
-
- //Determine if the requested security policy is included in the endpoint
- filteredEndpoints.forEach(endpoint -> hasIdentity(
- endpoint.getUserIdentityTokens().stream()
- .map(p -> (UserTokenPolicy) p)
- .toArray(UserTokenPolicy[]::new)
- ));
-
- if (this.policyId == null) {
- throw new PlcRuntimeException("Unable to find endpoint - " + this.endpoints.get(0));
+ EndpointDescription selectedEndpoint = null;
+ for (EndpointDescription endpoint : sessionResponse.getServerEndpoints()) {
+ if (isEndpoint(endpoint)) {
+ selectedEndpoint = endpoint;
+ break;
+ }
}
- if (this.tokenType == null) {
- throw new PlcRuntimeException("Unable to find Security Policy for endpoint - " + this.endpoints.get(0));
+
+ for (UserTokenPolicy tokenPolicy : selectedEndpoint.getUserIdentityTokens()) {
+ if (hasIdentity(tokenPolicy)) {
+ return entry(selectedEndpoint, tokenPolicy);
+ }
}
+
+ return null;
}
/**
@@ -572,18 +575,13 @@ public class SecureChannel {
* Confirms that a policy that matches the connection string is available from
* the returned endpoints. It sets the policyId and tokenType for the policy to use.
*
- * @param policies - A list of policies returned with the endpoint description.
+ * @param policy - A list of policies returned with the endpoint description.
*/
- private void hasIdentity(UserTokenPolicy[] policies) {
- for (UserTokenPolicy identityToken : policies) {
- if ((identityToken.getTokenType() == UserTokenType.userTokenTypeAnonymous) && (this.username == null)) {
- policyId = identityToken.getPolicyId();
- tokenType = identityToken.getTokenType();
- } else if ((identityToken.getTokenType() == UserTokenType.userTokenTypeUserName) && (this.username != null)) {
- policyId = identityToken.getPolicyId();
- tokenType = identityToken.getTokenType();
- }
+ private boolean hasIdentity(UserTokenPolicy policy) {
+ if ((policy.getTokenType() == UserTokenType.userTokenTypeAnonymous) && this.username == null) {
+ return true;
}
+ return policy.getTokenType() == UserTokenType.userTokenTypeUserName && this.username != null;
}
/**