You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2026/07/27 18:53:09 UTC

(streampipes) branch fix-opcua-trust-list-manager created (now a974ab8395)

This is an automated email from the ASF dual-hosted git repository.

dominikriemer pushed a change to branch fix-opcua-trust-list-manager
in repository https://gitbox.apache.org/repos/asf/streampipes.git


      at a974ab8395 fix: Use shared trust list manager for OPC UA adapters

This branch includes the following new commits:

     new a974ab8395 fix: Use shared trust list manager for OPC UA adapters

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.



(streampipes) 01/01: fix: Use shared trust list manager for OPC UA adapters

Posted by ri...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dominikriemer pushed a commit to branch fix-opcua-trust-list-manager
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit a974ab83958455bd1fe0f0fdab03f8688095f0ea
Author: Dominik Riemer <do...@gmail.com>
AuthorDate: Mon Jul 27 20:52:49 2026 +0200

    fix: Use shared trust list manager for OPC UA adapters
---
 .../opcua/config/security/SecurityConfig.java      |  12 +--
 .../security/SharedTrustListManagerProvider.java   | 106 +++++++++++++++++++++
 .../opcua/config/security/SecurityConfigTest.java  |  47 +++++++++
 .../SharedTrustListManagerProviderTest.java        |  74 ++++++++++++++
 4 files changed, 233 insertions(+), 6 deletions(-)

diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfig.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfig.java
index ccd64b78ea..925e86c644 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfig.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfig.java
@@ -26,7 +26,6 @@ import org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaCertificate
 import org.apache.streampipes.model.opcua.Certificate;
 
 import org.eclipse.milo.opcua.sdk.client.OpcUaClientConfigBuilder;
-import org.eclipse.milo.opcua.stack.core.security.FileBasedTrustListManager;
 import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode;
 import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
@@ -41,7 +40,6 @@ import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.util.Base64;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicReference;
 
 public class SecurityConfig {
 
@@ -104,14 +102,16 @@ public class SecurityConfig {
       try {
         var env = Environments.getEnvironment();
         var securityDir = Paths.get(env.getOpcUaSecurityDir().getValueOrDefault());
-        var trustListManager = FileBasedTrustListManager.createAndInitialize(securityDir.resolve("pki"));
+        var trustListManager = SharedTrustListManagerProvider
+            .getInstance()
+            .getOrCreate(securityDir.resolve("pki"));
 
-        var loadedCerts = new AtomicReference<>(fetchTrustedCertsFromRest());
+        var loadedCerts = fetchTrustedCertsFromRest();
 
         var compositeValidator = new CompositeCertificateValidator(
             config,
             trustListManager,
-            loadedCerts.get(),
+            loadedCerts,
             List.of(),
             streamPipesClient
         );
@@ -153,7 +153,7 @@ public class SecurityConfig {
         original.getSecurityLevel());
   }
 
-  private List<X509Certificate> fetchTrustedCertsFromRest() throws SpConfigurationException {
+  List<X509Certificate> fetchTrustedCertsFromRest() throws SpConfigurationException {
     try {
       var response = streamPipesClient.customRequest().getList(OpcUaCertificateUtils.getCoreTrustedCertificatePath(), Certificate.class);
       return response
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProvider.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProvider.java
new file mode 100644
index 0000000000..002dfd8834
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProvider.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.extensions.connectors.opcua.config.security;
+
+import org.eclipse.milo.opcua.stack.core.security.FileBasedTrustListManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+class SharedTrustListManagerProvider implements AutoCloseable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(SharedTrustListManagerProvider.class);
+
+  private final TrustListManagerFactory factory;
+  private final Map<Path, FileBasedTrustListManager> trustListManagers;
+
+  SharedTrustListManagerProvider(TrustListManagerFactory factory) {
+    this.factory = factory;
+    this.trustListManagers = new HashMap<>();
+  }
+
+  static SharedTrustListManagerProvider getInstance() {
+    return InstanceHolder.INSTANCE;
+  }
+
+  synchronized FileBasedTrustListManager getOrCreate(Path trustListDirectory) throws IOException {
+    var normalizedDirectory = trustListDirectory.toAbsolutePath().normalize();
+    var trustListManager = trustListManagers.get(normalizedDirectory);
+
+    if (trustListManager == null) {
+      trustListManager = factory.create(normalizedDirectory);
+      trustListManagers.put(normalizedDirectory, trustListManager);
+    }
+
+    return trustListManager;
+  }
+
+  @Override
+  public synchronized void close() throws IOException {
+    IOException closeException = null;
+
+    for (var trustListManager : trustListManagers.values()) {
+      try {
+        trustListManager.close();
+      } catch (IOException e) {
+        if (closeException == null) {
+          closeException = e;
+        } else {
+          closeException.addSuppressed(e);
+        }
+      }
+    }
+    trustListManagers.clear();
+
+    if (closeException != null) {
+      throw closeException;
+    }
+  }
+
+  @FunctionalInterface
+  interface TrustListManagerFactory {
+
+    FileBasedTrustListManager create(Path trustListDirectory) throws IOException;
+  }
+
+  private static final class InstanceHolder {
+
+    private static final SharedTrustListManagerProvider INSTANCE = createInstance();
+
+    private static SharedTrustListManagerProvider createInstance() {
+      var provider = new SharedTrustListManagerProvider(FileBasedTrustListManager::createAndInitialize);
+      Runtime.getRuntime().addShutdownHook(
+          new Thread(() -> closeOnShutdown(provider), "opcua-trust-list-manager-shutdown")
+      );
+      return provider;
+    }
+
+    private static void closeOnShutdown(SharedTrustListManagerProvider provider) {
+      try {
+        provider.close();
+      } catch (IOException e) {
+        LOG.warn("Could not close the shared OPC UA trust list manager", e);
+      }
+    }
+  }
+}
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfigTest.java b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfigTest.java
index ebfa2d0fda..dad46aad63 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfigTest.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SecurityConfigTest.java
@@ -18,8 +18,14 @@
 
 package org.apache.streampipes.extensions.connectors.opcua.config.security;
 
+import org.apache.streampipes.client.api.ICustomRequestApi;
+import org.apache.streampipes.client.api.IStreamPipesClient;
 import org.apache.streampipes.commons.exceptions.SpConfigurationException;
 import org.apache.streampipes.extensions.connectors.opcua.config.OpcUaConfig;
+import org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaCertificateUtils;
+import org.apache.streampipes.model.opcua.Certificate;
+import org.apache.streampipes.model.opcua.CertificateBuilder;
+import org.apache.streampipes.model.opcua.CertificateState;
 
 import org.eclipse.milo.opcua.sdk.client.OpcUaClientConfigBuilder;
 import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
@@ -29,6 +35,8 @@ import org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode;
 import org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription;
 import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
 import org.eclipse.milo.opcua.stack.core.types.structured.UserTokenPolicy;
+import org.eclipse.milo.opcua.stack.core.util.SelfSignedCertificateBuilder;
+import org.eclipse.milo.opcua.stack.core.util.SelfSignedCertificateGenerator;
 import org.junit.jupiter.api.Test;
 
 import java.net.URI;
@@ -39,6 +47,10 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 class SecurityConfigTest {
 
@@ -138,6 +150,41 @@ class SecurityConfigTest {
     ));
   }
 
+  @Test
+  void fetchesCurrentTrustedCertificatesForEveryConnectionConfiguration() throws Exception {
+    var streamPipesClient = mock(IStreamPipesClient.class);
+    var customRequestApi = mock(ICustomRequestApi.class);
+    var trustedCertificate = makeTrustedCertificate();
+    when(streamPipesClient.customRequest()).thenReturn(customRequestApi);
+    when(customRequestApi.getList(OpcUaCertificateUtils.getCoreTrustedCertificatePath(), Certificate.class))
+        .thenReturn(List.of(trustedCertificate), List.of());
+
+    var securityConfig = new SecurityConfig(
+        MessageSecurityMode.Sign,
+        SecurityPolicy.Basic256Sha256,
+        streamPipesClient,
+        false
+    );
+
+    var firstFetch = securityConfig.fetchTrustedCertsFromRest();
+    var secondFetch = securityConfig.fetchTrustedCertsFromRest();
+
+    assertEquals(1, firstFetch.size());
+    assertTrue(secondFetch.isEmpty());
+    verify(customRequestApi, times(2))
+        .getList(OpcUaCertificateUtils.getCoreTrustedCertificatePath(), Certificate.class);
+  }
+
+  private Certificate makeTrustedCertificate() throws Exception {
+    var keyPair = SelfSignedCertificateGenerator.generateRsaKeyPair(2048);
+    var certificate = new SelfSignedCertificateBuilder(keyPair)
+        .setCommonName("StreamPipes OPC UA test")
+        .setApplicationUri("urn:org:apache:streampipes:opcua:test")
+        .addDnsName("localhost")
+        .build();
+    return CertificateBuilder.fromX509(certificate, CertificateState.TRUSTED);
+  }
+
   private OpcUaConfig makeConfig() {
     var config = new OpcUaConfig();
     config.setOpcServerURL("opc.tcp://127.0.0.1:4840/milo");
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProviderTest.java b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProviderTest.java
new file mode 100644
index 0000000000..c9218d7b59
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/config/security/SharedTrustListManagerProviderTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.extensions.connectors.opcua.config.security;
+
+import org.eclipse.milo.opcua.stack.core.security.FileBasedTrustListManager;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class SharedTrustListManagerProviderTest {
+
+  @TempDir
+  Path tempDir;
+
+  @Test
+  void reusesTrustListManagerForSameDirectory() throws Exception {
+    var factory = mock(SharedTrustListManagerProvider.TrustListManagerFactory.class);
+    var trustListManager = mock(FileBasedTrustListManager.class);
+    var trustListDirectory = tempDir.resolve("pki");
+    when(factory.create(trustListDirectory)).thenReturn(trustListManager);
+
+    try (var provider = new SharedTrustListManagerProvider(factory)) {
+      var first = provider.getOrCreate(trustListDirectory);
+      var second = provider.getOrCreate(trustListDirectory.resolve("."));
+
+      assertSame(first, second);
+      verify(factory, times(1)).create(trustListDirectory);
+    }
+
+    verify(trustListManager).close();
+  }
+
+  @Test
+  void createsSeparateManagersForDifferentDirectoriesAndClosesBoth() throws Exception {
+    var factory = mock(SharedTrustListManagerProvider.TrustListManagerFactory.class);
+    var firstManager = mock(FileBasedTrustListManager.class);
+    var secondManager = mock(FileBasedTrustListManager.class);
+    var firstDirectory = tempDir.resolve("first");
+    var secondDirectory = tempDir.resolve("second");
+    when(factory.create(firstDirectory)).thenReturn(firstManager);
+    when(factory.create(secondDirectory)).thenReturn(secondManager);
+
+    try (var provider = new SharedTrustListManagerProvider(factory)) {
+      assertSame(firstManager, provider.getOrCreate(firstDirectory));
+      assertSame(secondManager, provider.getOrCreate(secondDirectory));
+    }
+
+    verify(firstManager).close();
+    verify(secondManager).close();
+  }
+}