From d92ce1b5faccad11aa6df77a2ccee96a2d6edbcc Mon Sep 17 00:00:00 2001 From: Malena Casas Date: Mon, 14 Oct 2024 10:52:16 -0300 Subject: [PATCH] Fix license of CommandManagerSettings and CommandManagerSettingsException. Add PluginSettingTest --- .../CommandManagerSettingsException.java | 7 ++ .../settings/CommandManagerSettings.java | 7 ++ .../settings/PluginSettingsTest.java | 71 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/PluginSettingsTest.java diff --git a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerSettingsException.java b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerSettingsException.java index 5669175..bb66f51 100644 --- a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerSettingsException.java +++ b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerSettingsException.java @@ -1,3 +1,10 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ package com.wazuh.commandmanager; diff --git a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/settings/CommandManagerSettings.java b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/settings/CommandManagerSettings.java index 5f26e20..0362e10 100644 --- a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/settings/CommandManagerSettings.java +++ b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/settings/CommandManagerSettings.java @@ -1,3 +1,10 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ package com.wazuh.commandmanager.settings; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Setting.Property; diff --git a/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/PluginSettingsTest.java b/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/PluginSettingsTest.java new file mode 100644 index 0000000..13bb50f --- /dev/null +++ b/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/PluginSettingsTest.java @@ -0,0 +1,71 @@ +package com.wazuh.commandmanager.settings; + +import org.junit.After; +import org.junit.Before; +import org.opensearch.common.settings.KeyStoreWrapper; +import org.opensearch.common.settings.SecureSettings; +import org.opensearch.core.common.settings.SecureString; +import org.opensearch.env.Environment; +import org.opensearch.test.OpenSearchIntegTestCase; + +import java.nio.file.Path; + +import static org.mockito.Mockito.*; + +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE) +public class PluginSettingsTest extends OpenSearchIntegTestCase { + + private PluginSettings pluginSettings; + private Environment mockEnvironment; + private final SecureString secureString = new SecureString("dummyPassword".toCharArray()); + + @Before + public void setUp() { + // Create a mock Environment + mockEnvironment = mock(Environment.class); + // Instantiate PluginSettings + pluginSettings = PluginSettings.getPluginSettingsInstance(); + pluginSettings.setEnv(mockEnvironment); + } + + @After + public void closeSecureString() { + // Cleanup if necessary + secureString.close(); + } + + public void testLoadSecureSettings_keystoreNotExist() throws Exception { + // Setup the mock to return a specific path for the config file + Path keyStorePath = Path.of("plugins/command-manager/src/test/resources/wazuh-indexer.keystoreUNEXISTENT.json"); + when(mockEnvironment.configFile()).thenReturn(keyStorePath); + + // Mocking the keyStoreWrapper to return null + KeyStoreWrapper keyStoreWrapperMock = mock(KeyStoreWrapper.class); + when(KeyStoreWrapper.load(any(), any())).thenReturn(null); + + // Check that the keystore is created + SecureSettings result = pluginSettings.loadSecureSettings(secureString); + + assertNotNull(result); + verify(keyStoreWrapperMock, times(1)).save(any(), any()); + } + + public void testLoadSecureSettings_keystoreExists() throws Exception { + // Setup the mock to return a specific path for the config file + Path keyStorePath = Path.of("plugins/command-manager/src/test/resources/"); + when(mockEnvironment.configFile()).thenReturn(keyStorePath); + + // Simulate an existing keystore + KeyStoreWrapper keyStoreWrapperMock = KeyStoreWrapper.load(keyStorePath, "wazuh-indexer.keystore.json"); + when(KeyStoreWrapper.load(any(), any())).thenReturn(keyStoreWrapperMock); + String text = "type"; + char[] passToTest = text.toCharArray(); + keyStoreWrapperMock.decrypt(passToTest); + + // Load secure settings + SecureSettings result = pluginSettings.loadSecureSettings(secureString); + + assertNotNull(result); + verify(keyStoreWrapperMock, times(1)).decrypt(secureString.getChars()); + } +}