From 4b8cc0ef0ce3932f6d0f517bebc6fdadfebeaf47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Ruiz?= Date: Fri, 18 Oct 2024 19:05:05 +0200 Subject: [PATCH] Apply spotless --- .../commandmanager/CommandManagerPlugin.java | 14 +- .../CommandManagerSettingsException.java | 14 +- .../settings/CommandManagerSettings.java | 152 +++++++++--------- .../settings/CommandManagerSettingsTests.java | 68 ++++---- 4 files changed, 126 insertions(+), 122 deletions(-) diff --git a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerPlugin.java b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerPlugin.java index 3c62157..08a5673 100644 --- a/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerPlugin.java +++ b/plugins/command-manager/src/main/java/com/wazuh/commandmanager/CommandManagerPlugin.java @@ -8,9 +8,6 @@ */ package com.wazuh.commandmanager; -import com.wazuh.commandmanager.settings.CommandManagerSettings; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.node.DiscoveryNodes; @@ -39,6 +36,7 @@ import com.wazuh.commandmanager.index.CommandIndex; import com.wazuh.commandmanager.rest.RestPostCommandAction; +import com.wazuh.commandmanager.settings.CommandManagerSettings; import com.wazuh.commandmanager.utils.httpclient.HttpRestClient; import com.wazuh.commandmanager.utils.httpclient.HttpRestClientDemo; @@ -99,16 +97,16 @@ public List> getSettings() { CommandManagerSettings.AUTH_USERNAME, CommandManagerSettings.AUTH_PASSWORD, CommandManagerSettings.URI, - CommandManagerSettings.AUTH_TYPE - ); + CommandManagerSettings.AUTH_TYPE); } @Override public void reload(Settings settings) { // secure settings should be readable - //final CommandManagerSettings commandManagerSettings = CommandManagerSettings.getClientSettings(secureSettingsPassword); - //I don't know what I have to do when we want to reload the settings already - //xxxService.refreshAndClearCache(commandManagerSettings); + // final CommandManagerSettings commandManagerSettings = + // CommandManagerSettings.getClientSettings(secureSettingsPassword); + // I don't know what I have to do when we want to reload the settings already + // xxxService.refreshAndClearCache(commandManagerSettings); } /** 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 78a24fc..9cc073c 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,4 +1,5 @@ /* + * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to @@ -7,7 +8,6 @@ */ package com.wazuh.commandmanager; - public class CommandManagerSettingsException extends Exception { // Default constructor @@ -32,22 +32,25 @@ public CommandManagerSettingsException(Throwable cause) { // Exception for the case when the keystore does not exist public static CommandManagerSettingsException keystoreNotExist(String keystorePath) { - return new CommandManagerSettingsException("The keystore does not exist at the path: " + keystorePath); + return new CommandManagerSettingsException( + "The keystore does not exist at the path: " + keystorePath); } // Exception for the case when the keystore is empty public static CommandManagerSettingsException keystoreEmpty(String keystorePath) { - return new CommandManagerSettingsException("The keystore is empty at the path: " + keystorePath); + return new CommandManagerSettingsException( + "The keystore is empty at the path: " + keystorePath); } // Exception for the case when load keystore failed public static CommandManagerSettingsException loadKeystoreFailed(String keyStorePath) { - return new CommandManagerSettingsException("Load keystore: "+ keyStorePath +" failed."); + return new CommandManagerSettingsException("Load keystore: " + keyStorePath + " failed."); } // Exception for the case when load keystore failed public static CommandManagerSettingsException decryptKeystoreFailed(String keyStorePath) { - return new CommandManagerSettingsException("Decrypt keystore: "+ keyStorePath +" failed."); + return new CommandManagerSettingsException( + "Decrypt keystore: " + keyStorePath + " failed."); } // Exception for the case when reload plugin with the keystore failed @@ -55,4 +58,3 @@ public static CommandManagerSettingsException reloadPluginFailed(String pluginNa return new CommandManagerSettingsException("Reload failed for plugin: " + pluginName); } } - 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 3fa0d86..438f06d 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,4 +1,5 @@ /* + * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to @@ -7,7 +8,6 @@ */ package com.wazuh.commandmanager.settings; -import com.wazuh.commandmanager.CommandManagerSettingsException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.common.settings.KeyStoreWrapper; @@ -22,60 +22,48 @@ import java.nio.file.Path; import java.security.GeneralSecurityException; +import com.wazuh.commandmanager.CommandManagerSettingsException; + public final class CommandManagerSettings { - /** - * The access key (ie login id) for connecting to api. - */ + /** The access key (ie login id) for connecting to api. */ public static final Setting KEYSTORE = SecureSetting.secureString("command.manager.keystore", null); - /** - * The access key (ie login username) for connecting to api. - */ + + /** The access key (ie login username) for connecting to api. */ public static final Setting AUTH_USERNAME = SecureSetting.secureString("command.manager.auth.username", null); - /** - * The secret key (ie password) for connecting to api. - */ + + /** The secret key (ie password) for connecting to api. */ public static final Setting AUTH_PASSWORD = SecureSetting.secureString("command.manager.auth.password", null); - /** - * The uri for connecting to api. - */ + + /** The uri for connecting to api. */ public static final Setting URI = SecureSetting.simpleString("command.manager.uri", Setting.Property.NodeScope); - /** - * The auth type for connecting to api. - */ + + /** The auth type for connecting to api. */ public static final Setting AUTH_TYPE = Setting.simpleString("command.manager.auth.type", Setting.Property.NodeScope); + private static final Logger log = LogManager.getLogger(CommandManagerSettings.class); - /** - * The name of own keystore. - */ - private static final String KEYSTORE_FILENAME = "opensearch.keystore"; //"wazuh-indexer.keystore"; - /** - * The access key (ie login username) for connecting to api. - */ + + /** The name of own keystore. */ + private static final String KEYSTORE_FILENAME = + "opensearch.keystore"; // "wazuh-indexer.keystore"; + + /** The access key (ie login username) for connecting to api. */ final String keystore; - /** - * The access key (ie login username) for connecting to api. - */ + /** The access key (ie login username) for connecting to api. */ final String authUsername; - /** - * The password for connecting to api. - */ + /** The password for connecting to api. */ final String authPassword; - /** - * The uri for connecting to api. - */ + /** The uri for connecting to api. */ final String uri; - /** - * The auth type for connecting to api. - */ + /** The auth type for connecting to api. */ final String authType; private CommandManagerSettings( @@ -83,8 +71,7 @@ private CommandManagerSettings( String authUsername, String authPassword, String uri, - String authType - ) { + String authType) { this.keystore = keystore; this.authUsername = authUsername; this.authPassword = authPassword; @@ -93,21 +80,25 @@ private CommandManagerSettings( log.info("Plugin settings: {}", this.toString()); } - /** - * Parse settings for a single client. - */ - public static CommandManagerSettings getSettings(Environment environment, SecureString secureSettingsPassword) { + /** Parse settings for a single client. */ + public static CommandManagerSettings getSettings( + Environment environment, SecureString secureSettingsPassword) { KeyStoreWrapper keyStoreWrapper = null; try { keyStoreWrapper = KeyStoreWrapper.load(environment.configFile(), KEYSTORE_FILENAME); } catch (IOException e) { - log.error(CommandManagerSettingsException.loadKeystoreFailed(environment.configFile().toAbsolutePath() + KEYSTORE_FILENAME).getMessage()); + log.error( + CommandManagerSettingsException.loadKeystoreFailed( + environment.configFile().toAbsolutePath() + KEYSTORE_FILENAME) + .getMessage()); } if (keyStoreWrapper == null) { - log.error(CommandManagerSettingsException.keystoreNotExist(KEYSTORE_FILENAME).getMessage()); + log.error( + CommandManagerSettingsException.keystoreNotExist(KEYSTORE_FILENAME) + .getMessage()); return null; } else { // Decrypt the keystore using the password from the request @@ -115,84 +106,95 @@ public static CommandManagerSettings getSettings(Environment environment, Secure try { keyStoreWrapper.decrypt(secureSettingsPassword.getChars()); } catch (GeneralSecurityException | IOException e) { - log.error(CommandManagerSettingsException.decryptKeystoreFailed(KEYSTORE_FILENAME).getMessage()); + log.error( + CommandManagerSettingsException.decryptKeystoreFailed(KEYSTORE_FILENAME) + .getMessage()); } } final Settings settings = Settings.builder().setSecureSettings(keyStoreWrapper).build(); - try ( - SecureString authUsername = AUTH_USERNAME.get(settings); - SecureString authPassword = AUTH_PASSWORD.get(settings); - ) { + try (SecureString authUsername = AUTH_USERNAME.get(settings); + SecureString authPassword = AUTH_PASSWORD.get(settings); ) { return new CommandManagerSettings( KEYSTORE_FILENAME, authUsername.toString(), authPassword.toString(), URI.get(settings), - AUTH_TYPE.get(settings) - ); + AUTH_TYPE.get(settings)); } } } - /** - * Parse settings for a single client. - */ + /** Parse settings for a single client. */ public static CommandManagerSettings getSettings(Environment environment) { KeyStoreWrapper keyStoreWrapper = null; Path keystoreFile = Path.of(environment.configFile() + "/" + KEYSTORE_FILENAME); try { if (!Files.exists(keystoreFile)) { - throw CommandManagerSettingsException.keystoreNotExist(keystoreFile.toAbsolutePath().toString()); - //Path keyStorePath = Files.createFile(keystoreFile); - //log.warn("CREADA KeyStoreWrapper en "+keyStorePath.toString()); + throw CommandManagerSettingsException.keystoreNotExist( + keystoreFile.toAbsolutePath().toString()); + // Path keyStorePath = Files.createFile(keystoreFile); + // log.warn("CREADA KeyStoreWrapper en "+keyStorePath.toString()); } else { - log.warn("Por hacer load de KeyStoreWrapper en " + environment.configFile().toString()); + log.warn( + "Por hacer load de KeyStoreWrapper en " + + environment.configFile().toString()); keyStoreWrapper = KeyStoreWrapper.load(environment.configFile(), KEYSTORE_FILENAME); } } catch (Exception e) { - log.error(CommandManagerSettingsException.loadKeystoreFailed(keystoreFile.toString()).getMessage()); + log.error( + CommandManagerSettingsException.loadKeystoreFailed(keystoreFile.toString()) + .getMessage()); } if (keyStoreWrapper == null) { - log.error(CommandManagerSettingsException.keystoreNotExist(keystoreFile.toString()).getMessage()); + log.error( + CommandManagerSettingsException.keystoreNotExist(keystoreFile.toString()) + .getMessage()); return null; } else { // Decrypt the keystore using the password from the request try { keyStoreWrapper.decrypt(new char[0]); } catch (GeneralSecurityException | IOException e) { - log.error(CommandManagerSettingsException.decryptKeystoreFailed(KEYSTORE_FILENAME).getMessage()); + log.error( + CommandManagerSettingsException.decryptKeystoreFailed(KEYSTORE_FILENAME) + .getMessage()); } final Settings settings = Settings.builder().setSecureSettings(keyStoreWrapper).build(); - try ( - SecureString authUsername = AUTH_USERNAME.get(settings); - SecureString authPassword = AUTH_PASSWORD.get(settings); - ) { + try (SecureString authUsername = AUTH_USERNAME.get(settings); + SecureString authPassword = AUTH_PASSWORD.get(settings); ) { return new CommandManagerSettings( KEYSTORE_FILENAME, authUsername.toString(), authPassword.toString(), URI.get(settings), - AUTH_TYPE.get(settings) - ); + AUTH_TYPE.get(settings)); } } } - @Override public String toString() { - return "CommandManagerSettings{" + - "keystore='" + keystore + '\'' + - ", authUsername='" + authUsername + '\'' + - ", authPassword='" + authPassword + '\'' + - ", uri='" + uri + '\'' + - ", authType='" + authType + '\'' + - '}'; + return "CommandManagerSettings{" + + "keystore='" + + keystore + + '\'' + + ", authUsername='" + + authUsername + + '\'' + + ", authPassword='" + + authPassword + + '\'' + + ", uri='" + + uri + + '\'' + + ", authType='" + + authType + + '\'' + + '}'; } } - diff --git a/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/CommandManagerSettingsTests.java b/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/CommandManagerSettingsTests.java index 6d81384..a8a61cd 100644 --- a/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/CommandManagerSettingsTests.java +++ b/plugins/command-manager/src/test/java/com/wazuh/commandmanager/settings/CommandManagerSettingsTests.java @@ -1,4 +1,5 @@ /* + * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to @@ -9,15 +10,12 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; import org.opensearch.common.settings.KeyStoreWrapper; import org.opensearch.core.common.settings.SecureString; import org.opensearch.env.Environment; import org.opensearch.test.OpenSearchIntegTestCase; +import org.junit.After; +import org.junit.Before; import java.io.IOException; import java.nio.file.Path; @@ -25,26 +23,24 @@ import java.security.AccessController; import java.security.PrivilegedAction; +import org.mockito.Mock; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE) -public class CommandManagerSettingsTests extends OpenSearchIntegTestCase{ - @Mock - private Environment mockEnvironment; - +public class CommandManagerSettingsTests extends OpenSearchIntegTestCase { + @Mock private Environment mockEnvironment; private final SecureString secureString = new SecureString("dummyPassword".toCharArray()); - @Mock - KeyStoreWrapper mockedKeyStoreWrapper; + @Mock KeyStoreWrapper mockedKeyStoreWrapper; private CommandManagerSettings commandManagerSettings; private static final Logger log = LogManager.getLogger(CommandManagerSettingsTests.class); - @Before @Override public void setUp() throws Exception { @@ -64,30 +60,36 @@ public void testGetSettings_keystoreExists() throws Exception { Path keyStorePath = Path.of("plugins/command-manager/src/test/resources/").toAbsolutePath(); when(mockEnvironment.configFile()).thenReturn(keyStorePath); - //logger.error(String.format(" Attempting to read file: %s%s", keyStorePath,"wazuh-indexer.keystore.json")); + // logger.error(String.format(" Attempting to read file: %s%s", + // keyStorePath,"wazuh-indexer.keystore.json")); try { AccessController.doPrivileged( - (PrivilegedAction) () -> { - // Simulate an existing keystore - try { - KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.load(keyStorePath, "wazuh-indexer.keystore.json"); - when(KeyStoreWrapper.load(any(), any())).thenReturn(keyStoreWrapper); - log.warn("test INSIDE+EE"); - - this.commandManagerSettings = CommandManagerSettings.getSettings(mockEnvironment); - - assertNotNull(commandManagerSettings); - log.warn("keystore INSIDE"+commandManagerSettings.keystore); - // verify(keyStoreWrapper, times(1)).decrypt(secureString.getChars()); - } catch (IOException e) { - log.warn("ERROR TEST: "+e.getMessage()); - } - log.warn("RETURN"); - return null; - } - ); - }catch(AccessControlException e){ + (PrivilegedAction) + () -> { + // Simulate an existing keystore + try { + KeyStoreWrapper keyStoreWrapper = + KeyStoreWrapper.load( + keyStorePath, "wazuh-indexer.keystore.json"); + when(KeyStoreWrapper.load(any(), any())) + .thenReturn(keyStoreWrapper); + log.warn("test INSIDE+EE"); + + this.commandManagerSettings = + CommandManagerSettings.getSettings(mockEnvironment); + + assertNotNull(commandManagerSettings); + log.warn("keystore INSIDE" + commandManagerSettings.keystore); + // verify(keyStoreWrapper, + // times(1)).decrypt(secureString.getChars()); + } catch (IOException e) { + log.warn("ERROR TEST: " + e.getMessage()); + } + log.warn("RETURN"); + return null; + }); + } catch (AccessControlException e) { } }