[ 
https://issues.apache.org/jira/browse/CAMEL-12286?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16374379#comment-16374379
 ] 

ASF GitHub Bot commented on CAMEL-12286:
----------------------------------------

oscerd closed pull request #2233: CAMEL-12286: Fix camel-milo client component
URL: https://github.com/apache/camel/pull/2233
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/components/camel-milo/src/main/docs/milo-client-component.adoc 
b/components/camel-milo/src/main/docs/milo-client-component.adoc
index a911e3f306f..eb3e2b5db37 100644
--- a/components/camel-milo/src/main/docs/milo-client-component.adoc
+++ b/components/camel-milo/src/main/docs/milo-client-component.adoc
@@ -95,7 +95,7 @@ with the following path and query parameters:
 | *endpointUri* | *Required* The OPC UA server endpoint |  | String
 |===
 
-==== Query Parameters (22 parameters):
+==== Query Parameters (23 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
@@ -108,6 +108,7 @@ with the following path and query parameters:
 | *exceptionHandler* (consumer) | To let the consumer use a custom 
ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this 
options is not in use. By default the consumer will deal with exceptions, that 
will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer 
creates an exchange. |  | ExchangePattern
 | *synchronous* (advanced) | Sets whether synchronous processing should be 
strictly used, or Camel is allowed to use asynchronous processing (if 
supported). | false | boolean
+| *allowedSecurityPolicies* (client) | A set of allowed security policy URIs. 
Default is to accept all and use the highest. |  | String
 | *applicationName* (client) | The application name | Apache Camel adapter for 
Eclipse Milo | String
 | *applicationUri* (client) | The application URI | 
http://camel.apache.org/EclipseMilo/Client | String
 | *channelLifetime* (client) | Channel lifetime in milliseconds |  | Long
@@ -161,6 +162,15 @@ However Camel allows to wrap the actual value inside 
`RAW(…)`, which makes esc
 
milo-client://user:password@localhost:12345?node=RAW(nsu=http://foo.bar;s=foo/bar)
 ------------------------
 
+==== Security policies
+
+When setting the allowing security policies is it possible to use the well 
known OPC UA URIs (e.g. 
`http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15`)
+or to use the Milo enum literals (e.g. `None`). Specifying an unknown security 
policy URI or enum is an error.
+
+The known security policy URIs and enum literals are can be seen here: 
https://github.com/eclipse/milo/blob/master/opc-ua-stack/stack-core/src/main/java/org/eclipse/milo/opcua/stack/core/security/SecurityPolicy.java[SecurityPolicy.java]
 
+
+**Note:** In any case security policies are considered case sensitive.
+
 === See Also
 
 * Configuring Camel
diff --git 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
index 8f1e1847961..1a78648a93c 100644
--- 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
+++ 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
@@ -18,10 +18,13 @@
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.camel.component.milo.KeyStoreLoader;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
+import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
 
 @UriParams
 public class MiloClientConfiguration implements Cloneable {
@@ -79,6 +82,9 @@
     @UriParam(label = "client", secret = true)
     private String keyPassword;
 
+    @UriParam(label = "client", javaType = "java.lang.String")
+    private Set<String> allowedSecurityPolicies = new HashSet<>();
+
     public MiloClientConfiguration() {
     }
 
@@ -88,6 +94,7 @@ public MiloClientConfiguration(final MiloClientConfiguration 
other) {
         this.applicationName = other.applicationName;
         this.productUri = other.productUri;
         this.requestTimeout = other.requestTimeout;
+        this.allowedSecurityPolicies = allowedSecurityPolicies != null ? new 
HashSet<>(other.allowedSecurityPolicies) : null;
     }
 
     public void setEndpointUri(final String endpointUri) {
@@ -263,6 +270,55 @@ public String getKeyPassword() {
         return this.keyPassword;
     }
 
+    /**
+     * A set of allowed security policy URIs. Default is to accept all and use
+     * the highest.
+     */
+    public void setAllowedSecurityPolicies(final Set<String> 
allowedSecurityPolicies) {
+        this.allowedSecurityPolicies = allowedSecurityPolicies;
+    }
+
+    public void setAllowedSecurityPolicies(final String 
allowedSecurityPolicies) {
+
+        // check if we are reset or set
+
+        if (allowedSecurityPolicies == null) {
+            // resetting to null
+            this.allowedSecurityPolicies = null;
+            return;
+        }
+
+        // split and convert
+
+        this.allowedSecurityPolicies = new HashSet<>();
+        final String[] policies = allowedSecurityPolicies.split(",");
+        for (final String policy : policies) {
+
+            String adding = null;
+            try {
+                adding = SecurityPolicy.fromUri(policy).getSecurityPolicyUri();
+            } catch (Exception e) {
+            }
+            if (adding == null) {
+                try {
+                    adding = 
SecurityPolicy.valueOf(policy).getSecurityPolicyUri();
+                } catch (Exception e) {
+                }
+            }
+
+            if (adding == null) {
+                throw new RuntimeException("Unknown security policy: " + 
policy);
+            }
+
+            this.allowedSecurityPolicies.add(adding);
+        }
+
+    }
+
+    public Set<String> getAllowedSecurityPolicies() {
+        return allowedSecurityPolicies;
+    }
+
     @Override
     public MiloClientConfiguration clone() {
         return new MiloClientConfiguration(this);
diff --git 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
index 877bf084df8..f2d31025a4b 100644
--- 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
+++ 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
@@ -23,6 +23,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
@@ -31,6 +32,7 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
+import java.util.function.Predicate;
 
 import org.apache.camel.component.milo.NamespaceId;
 import org.apache.camel.component.milo.PartialNodeId;
@@ -445,6 +447,7 @@ private Connected performConnect() throws Exception {
         cfg.setEndpoint(endpoint);
 
         final OpcUaClient client = new OpcUaClient(cfg.build());
+        client.connect().get();
 
         try {
             final UaSubscription manager = 
client.getSubscriptionManager().createSubscription(1_000.0).get();
@@ -493,8 +496,23 @@ private synchronized void triggerReconnect(final boolean 
immediate) {
     }
 
     private EndpointDescription findEndpoint(final EndpointDescription[] 
endpoints) {
+
+        final Predicate<String> allowed;
+        final Set<String> uris = 
this.configuration.getAllowedSecurityPolicies();
+
+        if (this.configuration.getAllowedSecurityPolicies() == null || 
this.configuration.getAllowedSecurityPolicies().isEmpty()) {
+            allowed = uri -> true;
+        } else {
+            allowed = uris::contains;
+        }
+
         EndpointDescription best = null;
         for (final EndpointDescription ep : endpoints) {
+
+            if (!allowed.test(ep.getSecurityPolicyUri())) {
+                continue;
+            }
+
             if (best == null || 
ep.getSecurityLevel().compareTo(best.getSecurityLevel()) > 0) {
                 best = ep;
             }
diff --git 
a/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
index b653f8b6836..cd194aa1384 100644
--- 
a/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
+++ 
b/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.milo.client.springboot;
 
+import java.util.Set;
 import javax.annotation.Generated;
 import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon;
 import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -169,6 +170,11 @@ public void setResolvePropertyPlaceholders(
          * The key password
          */
         private String keyPassword;
+        /**
+         * A set of allowed security policy URIs. Default is to accept all and
+         * use the highest.
+         */
+        private Set allowedSecurityPolicies;
 
         public String getEndpointUri() {
             return endpointUri;
@@ -289,5 +295,13 @@ public String getKeyPassword() {
         public void setKeyPassword(String keyPassword) {
             this.keyPassword = keyPassword;
         }
+
+        public Set getAllowedSecurityPolicies() {
+            return allowedSecurityPolicies;
+        }
+
+        public void setAllowedSecurityPolicies(Set allowedSecurityPolicies) {
+            this.allowedSecurityPolicies = allowedSecurityPolicies;
+        }
     }
 }
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Milo client broken
> ------------------
>
>                 Key: CAMEL-12286
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12286
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-milo
>    Affects Versions: 2.21.0
>            Reporter: Jens Reimann
>            Assignee: Jens Reimann
>            Priority: Major
>
> It looks to me as if the camel-milo client is broken in the most recent 
> version of camel (2.21.x). Running a working example from camel 2.20.x (and 
> 2.19.x) with Camel 2.21-SNAPSHOT results in:
> {code:java}
> 2018-02-22 11:56:45.248  INFO 30400 --- [           main] 
> o.e.milo.opcua.sdk.client.OpcUaClient    : Eclipse Milo OPC UA Stack version: 
> 0.2.1
> 2018-02-22 11:56:45.248  INFO 30400 --- [           main] 
> o.e.milo.opcua.sdk.client.OpcUaClient    : Eclipse Milo OPC UA Client SDK 
> version: 0.2.1
> 2018-02-22 11:56:45.407  INFO 30400 --- [           main] 
> o.a.c.c.m.c.i.SubscriptionManager        : Failed to connect
> java.util.concurrent.ExecutionException: UaException: 
> status=Bad_SessionClosed, message=The session was closed by the client.
>     at 
> java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) 
> ~[na:1.8.0_161]
>     at 
> java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) 
> ~[na:1.8.0_161]
>     at 
> org.apache.camel.component.milo.client.internal.SubscriptionManager.performConnect(SubscriptionManager.java:450)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.internal.SubscriptionManager.performAndEvalConnect(SubscriptionManager.java:375)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.internal.SubscriptionManager.connect(SubscriptionManager.java:370)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.internal.SubscriptionManager.<init>(SubscriptionManager.java:340)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.MiloClientConnection.init(MiloClientConnection.java:52)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.MiloClientConnection.checkInit(MiloClientConnection.java:69)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.MiloClientConnection.monitorValue(MiloClientConnection.java:86)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at 
> org.apache.camel.component.milo.client.MiloClientConsumer.doStart(MiloClientConsumer.java:57)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) 
> [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.startService(DefaultCamelContext.java:3701)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:4019)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:3954)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3874)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3638)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3490)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:208)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3249)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3245)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3268)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3245)
>  [camel-core-2.20.2.jar:2.20.2]
>     at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) 
> [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3168)
>  [camel-core-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133) 
> [camel-spring-2.20.2.jar:2.20.2]
>     at 
> org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:174)
>  [camel-spring-2.20.2.jar:2.20.2]
>     at 
> org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
>  [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
>     at 
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
>  [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
>     at 
> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
>  [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
>     at 
> org.springframework.boot.SpringApplication.run(SpringApplication.java:303) 
> [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
>     at 
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) 
> [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
>     at 
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) 
> [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
>     at de.dentrassi.bcx18.opcua.Application.main(Application.java:19) 
> [classes/:na]
> Caused by: org.eclipse.milo.opcua.stack.core.UaException: 
> status=Bad_SessionClosed, description=The session was closed by the client.
>     at 
> org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture(FutureUtils.java:100)
>  ~[stack-core-0.2.1.jar:0.2.1]
>     at 
> org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture(FutureUtils.java:88)
>  ~[stack-core-0.2.1.jar:0.2.1]
>     at 
> org.eclipse.milo.opcua.sdk.client.session.states.Inactive.<init>(Inactive.java:28)
>  ~[sdk-client-0.2.1.jar:0.2.1]
>     at 
> org.eclipse.milo.opcua.sdk.client.session.SessionFsm.<init>(SessionFsm.java:69)
>  ~[sdk-client-0.2.1.jar:0.2.1]
>     at 
> org.eclipse.milo.opcua.sdk.client.OpcUaClient.<init>(OpcUaClient.java:159) 
> ~[sdk-client-0.2.1.jar:0.2.1]
>     at 
> org.apache.camel.component.milo.client.internal.SubscriptionManager.performConnect(SubscriptionManager.java:447)
>  [camel-milo-2.21.0-SNAPSHOT.jar:2.21.0-SNAPSHOT]
>     ... 36 common frames omitted
> {code}
> And no more connections are possible.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to