Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix license of CommandManagerSettings and CommandManagerSettingsExcep…
Browse files Browse the repository at this point in the history
…tion. Add PluginSettingTest
mcasas993 committed Oct 14, 2024

Verified

This commit was signed with the committer’s verified signature.
mcasas993 Malena Casas
1 parent a4e5f72 commit d92ce1b
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;


Original file line number Diff line number Diff line change
@@ -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;
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());
}
}

0 comments on commit d92ce1b

Please sign in to comment.