Add configurable port and authentication to DMaaP simulator 12/126712/1
authordc443y <dc443y@att.com>
Fri, 21 Jan 2022 20:43:18 +0000 (14:43 -0600)
committerdc443y <dc443y@att.com>
Fri, 21 Jan 2022 20:59:42 +0000 (14:59 -0600)
Issue-ID: POLICY-3890
Signed-off-by: dc443y <dc443y@att.com>
Change-Id: Idbeb3590a767e51aa0058a7a45d56a61e81301cb

models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java
models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/dmaap/AuthDmaapParameters.json [new file with mode: 0644]
models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/DmaapSimulatorTest.java
models-interactions/model-simulators/src/test/resources/keystore-test [new file with mode: 0644]
models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json
models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/parameters/RestServerParameters.java

index 24fd7ec..155107b 100644 (file)
@@ -156,19 +156,30 @@ public final class Util {
     }
 
     /**
-     * Build a DMaaP simulator.
+     * Builds an unauthenticated HTTP DMaaP simulator.
      *
      * @return the simulator
      * @throws InterruptedException if a thread is interrupted
      */
     public static HttpServletServer buildDmaapSim() throws InterruptedException {
-        var json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
+        return buildDmaapSim("org/onap/policy/simulators/dmaap/DmaapParameters.json");
+    }
+
+    /**
+     * Build a DMaaP simulator from a properties file.
+     *
+     * @param resourceName the name of the properties file
+     * @return the simulator
+     * @throws InterruptedException if a thread is interrupted
+     */
+    public static HttpServletServer buildDmaapSim(String resourceName) throws InterruptedException {
+        var json = ResourceUtils.getResourceAsString(resourceName);
         DmaapSimParameterGroup params = null;
         try {
             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
         } catch (CoderException ce) {
             throw new ParameterRuntimeException(
-                    CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
+                    CANNOT_PROCESS_PARAMETERS + resourceName, ce);
         }
 
         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
@@ -177,8 +188,6 @@ public final class Util {
 
         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
                 + params.getRestServerParameters().getName();
-        props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
-                Integer.toString(DMAAPSIM_SERVER_PORT));
         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
 
         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/dmaap/AuthDmaapParameters.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/dmaap/AuthDmaapParameters.json
new file mode 100644 (file)
index 0000000..0ef0316
--- /dev/null
@@ -0,0 +1,11 @@
+{
+    "name": "AuthDMaapSim",
+    "topicSweepSec": 300,
+    "restServerParameters": {
+        "host": "0.0.0.0",
+        "port": 3903,
+        "useHttps": true,
+        "userName": "my-username",
+        "password": "my-password"
+    }
+}
index 19c399d..197c25f 100644 (file)
@@ -24,24 +24,30 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.File;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.util.HashMap;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink;
 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
 import org.onap.policy.common.utils.coder.StandardCoder;
 
 public class DmaapSimulatorTest {
     private static final int MAX_WAIT_SEC = 5;
     private static final String TOPIC = "MY-TOPIC";
+    private static final String AUTH_TOPIC = "MY-AUTH-TOPIC";
+    private static final String AUTH_PORT = "3903";
+
+    private static final HashMap<String, String> savedValuesMap = new HashMap<>();
 
     /**
      * Messages from the topic are placed here by the endpoint.
@@ -61,11 +67,14 @@ public class DmaapSimulatorTest {
     @Before
     public void setUp() throws Exception {
         assertNotNull(Util.buildDmaapSim());
+        setSystemProperties();
+        assertNotNull(Util.buildDmaapSim("org/onap/policy/simulators/dmaap/AuthDmaapParameters.json"));
 
-        String topicJson = new String(Files.readAllBytes(
-                        new File("src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json").toPath()),
-                        StandardCharsets.UTF_8);
-        topicJson = topicJson.replace("${port}", String.valueOf(Util.DMAAPSIM_SERVER_PORT));
+        String topicJson = Files.readString(
+                new File("src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json").toPath());
+        topicJson = topicJson
+                .replace("${port}", String.valueOf(Util.DMAAPSIM_SERVER_PORT))
+                .replace("${authPort}", AUTH_PORT);
 
         TopicParameterGroup topicConfig = new StandardCoder().decode(topicJson, TopicParameterGroup.class);
 
@@ -75,6 +84,11 @@ public class DmaapSimulatorTest {
         queue = new LinkedBlockingQueue<>();
     }
 
+    @AfterClass
+    public static void tearDownAfterClass() {
+        clearSystemProperties();
+    }
+
     @After
     public void tearDown() {
         TopicEndpointManager.getManager().shutdown();
@@ -83,15 +97,96 @@ public class DmaapSimulatorTest {
 
     @Test
     public void test() throws InterruptedException {
-        TopicEndpointManager.getManager().getDmaapTopicSource(TOPIC)
-                        .register((infra, topic, event) -> queue.add(event));
+        // test basic DMaaP simulator (default port, http, no auth)
+        sendAndPoll(TOPIC);
+        // test custom DMaaP simulator (custom port, https, basic auth)
+        sendAndPoll(AUTH_TOPIC);
+    }
+
+    /**
+     * Sets up keystore and truststore for https test server.
+     */
+    private static void setSystemProperties() {
+        String keyStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
+        if (keyStoreSystemProperty != null) {
+            savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, keyStoreSystemProperty);
+        }
+
+        String keyStorePasswordSystemProperty =
+                System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
+        if (keyStorePasswordSystemProperty != null) {
+            savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
+                    keyStorePasswordSystemProperty);
+        }
+
+        String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
+        if (trustStoreSystemProperty != null) {
+            savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
+        }
 
-        DmaapTopicSink sink = TopicEndpointManager.getManager().getDmaapTopicSink(TOPIC);
+        String trustStorePasswordSystemProperty =
+                System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
+        if (trustStorePasswordSystemProperty != null) {
+            savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
+                    trustStorePasswordSystemProperty);
+        }
+
+        System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
+        System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
+
+        System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
+        System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
+    }
+
+    /**
+     * Clears the keystore/truststore properties.
+     */
+    private static void clearSystemProperties() {
+        if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME)) {
+            System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
+                    savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME));
+            savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
+        } else {
+            System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
+        }
+
+        if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME)) {
+            System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
+                    savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME));
+            savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
+        } else {
+            System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
+        }
+
+        if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
+            System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME,
+                    savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME));
+            savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
+        } else {
+            System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
+        }
+
+        if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME)) {
+            System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
+                    savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME));
+            savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
+        } else {
+            System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
+        }
+    }
+
+    private void sendAndPoll(String topicName) throws InterruptedException {
+        TopicEndpointManager.getManager().getDmaapTopicSource(topicName)
+                .register((infra, topic, event) -> queue.add(event));
+
+        DmaapTopicSink sink = TopicEndpointManager.getManager().getDmaapTopicSink(topicName);
         assertThat(queue.poll(1, TimeUnit.SECONDS)).isNull();
+
         sink.send("hello");
         assertEquals("hello", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
 
         sink.send("world");
         assertEquals("world", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
     }
+
 }
diff --git a/models-interactions/model-simulators/src/test/resources/keystore-test b/models-interactions/model-simulators/src/test/resources/keystore-test
new file mode 100644 (file)
index 0000000..5820e0f
Binary files /dev/null and b/models-interactions/model-simulators/src/test/resources/keystore-test differ
index ba1f480..0353191 100644 (file)
@@ -7,6 +7,18 @@
             ],
             "topicCommInfrastructure": "dmaap",
             "fetchTimeout": 100
+        },
+        {
+            "topic": "MY-AUTH-TOPIC",
+            "servers": [
+                "localhost:${authPort}"
+            ],
+            "topicCommInfrastructure": "dmaap",
+            "fetchTimeout": 100,
+            "useHttps": true,
+            "allowSelfSignedCerts": true,
+            "userName": "my-username",
+            "password": "my-password"
         }
     ],
     "topicSinks": [
                 "localhost:${port}"
             ],
             "topicCommInfrastructure": "dmaap"
+        },
+        {
+            "topic": "MY-AUTH-TOPIC",
+            "servers": [
+                "localhost:${authPort}"
+            ],
+            "topicCommInfrastructure": "dmaap",
+            "useHttps": true,
+            "allowSelfSignedCerts": true,
+            "userName": "my-username",
+            "password": "my-password"
         }
     ]
 }
\ No newline at end of file
index 8414d07..04d914c 100644 (file)
@@ -45,6 +45,12 @@ public class RestServerParameters extends ParameterGroupImpl {
     @Min(value = 1)
     private int port;
 
+    private String userName;
+
+    private String password;
+
+    private boolean useHttps;
+
     public RestServerParameters() {
         super(RestServerParameters.class.getSimpleName());
     }
@@ -69,6 +75,12 @@ public class RestServerParameters extends ParameterGroupImpl {
                         DmaapSimRestControllerV1.class.getName());
         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
+        props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, Boolean.toString(isUseHttps()));
+
+        if (getUserName() != null && getPassword() != null) {
+            props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, getUserName());
+            props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, getPassword());
+        }
 
         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
                         String.join(",", CambriaMessageBodyHandler.class.getName(),