From c445d7865f1ded1c62f720abb70382b2963fff57 Mon Sep 17 00:00:00 2001 From: dc443y Date: Fri, 21 Jan 2022 14:43:18 -0600 Subject: Add configurable port and authentication to DMaaP simulator Issue-ID: POLICY-3890 Signed-off-by: dc443y Change-Id: Idbeb3590a767e51aa0058a7a45d56a61e81301cb --- .../main/java/org/onap/policy/simulators/Util.java | 19 +++- .../simulators/dmaap/AuthDmaapParameters.json | 11 ++ .../onap/policy/simulators/DmaapSimulatorTest.java | 111 +++++++++++++++++++-- .../src/test/resources/keystore-test | Bin 0 -> 3895 bytes .../policy/simulators/dmaap/TopicParameters.json | 23 +++++ 5 files changed, 151 insertions(+), 13 deletions(-) create mode 100644 models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/dmaap/AuthDmaapParameters.json create mode 100644 models-interactions/model-simulators/src/test/resources/keystore-test (limited to 'models-interactions/model-simulators') diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java index 24fd7ece5..155107bbd 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java @@ -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 index 000000000..0ef03169a --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/dmaap/AuthDmaapParameters.json @@ -0,0 +1,11 @@ +{ + "name": "AuthDMaapSim", + "topicSweepSec": 300, + "restServerParameters": { + "host": "0.0.0.0", + "port": 3903, + "useHttps": true, + "userName": "my-username", + "password": "my-password" + } +} diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/DmaapSimulatorTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/DmaapSimulatorTest.java index 19c399db8..197c25f6c 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/DmaapSimulatorTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/DmaapSimulatorTest.java @@ -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 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 index 000000000..5820e0f0c Binary files /dev/null and b/models-interactions/model-simulators/src/test/resources/keystore-test differ diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json index ba1f4806f..03531918b 100644 --- a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json @@ -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": [ @@ -16,6 +28,17 @@ "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 -- cgit 1.2.3-korg