-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Fix license of CommandManagerSettings and CommandManagerSettingsExcep…
…tion. Add PluginSettingTest
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...mmand-manager/src/main/java/com/wazuh/commandmanager/CommandManagerSettingsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...mmand-manager/src/main/java/com/wazuh/commandmanager/settings/CommandManagerSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...s/command-manager/src/test/java/com/wazuh/commandmanager/settings/PluginSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |