Skip to content

Commit

Permalink
Apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRuiz7 committed Oct 18, 2024
1 parent 99cf140 commit 4b8cc0e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -99,16 +97,16 @@ public List<Setting<?>> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
Expand All @@ -7,7 +8,6 @@
*/
package com.wazuh.commandmanager;


public class CommandManagerSettingsException extends Exception {

// Default constructor
Expand All @@ -32,27 +32,29 @@ 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
public static CommandManagerSettingsException reloadPluginFailed(String pluginName) {
return new CommandManagerSettingsException("Reload failed for plugin: " + pluginName);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
Expand All @@ -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;
Expand All @@ -22,69 +22,56 @@
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<SecureString> 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<SecureString> 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<SecureString> 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<String> 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<String> 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(
String keystore,
String authUsername,
String authPassword,
String uri,
String authType
) {
String authType) {
this.keystore = keystore;
this.authUsername = authUsername;
this.authPassword = authPassword;
Expand All @@ -93,106 +80,121 @@ 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
if (keyStoreWrapper.hasPassword()) {
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
+ '\''
+ '}';
}
}

Loading

0 comments on commit 4b8cc0e

Please sign in to comment.