-
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.
Add CommangManagerSettings and the respective tests (#120)
* Add CommangManagerSettings and the respective tests * Apply spotless * Apply spotless --------- Co-authored-by: Álex Ruiz <[email protected]>
- Loading branch information
Showing
8 changed files
with
299 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
109 changes: 109 additions & 0 deletions
109
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.settings.SecureSetting; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.common.settings.SecureString; | ||
import org.opensearch.env.Environment; | ||
|
||
/** This class manages the configuration of the plugin. */ | ||
public class CommandManagerSettings { | ||
|
||
/** The access key (ie login username) for connecting to api. */ | ||
public static final Setting<SecureString> M_API_AUTH_USERNAME = | ||
SecureSetting.secureString("m_api.auth.username", null); | ||
|
||
/** The secret key (ie password) for connecting to api. */ | ||
public static final Setting<SecureString> M_API_AUTH_PASSWORD = | ||
SecureSetting.secureString("m_api.auth.password", null); | ||
|
||
/** The uri for connecting to api. */ | ||
public static final Setting<SecureString> M_API_URI = | ||
SecureSetting.secureString("m_api.uri", null); | ||
|
||
/** The access key (ie login username) for connecting to api. */ | ||
private final String authUsername; | ||
|
||
/** The password for connecting to api. */ | ||
private final String authPassword; | ||
|
||
/** The uri for connecting to api. */ | ||
private final String uri; | ||
|
||
private static final Logger log = LogManager.getLogger(CommandManagerSettings.class); | ||
private static CommandManagerSettings instance; | ||
|
||
/** Private default constructor */ | ||
private CommandManagerSettings(String authUsername, String authPassword, String uri) { | ||
this.authUsername = authUsername; | ||
this.authPassword = authPassword; | ||
this.uri = uri; | ||
log.info("CommandManagerSettings created "); | ||
} | ||
|
||
/** | ||
* Singleton instance accessor | ||
* | ||
* @return {@link CommandManagerSettings#instance} | ||
*/ | ||
public static CommandManagerSettings getInstance(Environment environment) { | ||
if (CommandManagerSettings.instance == null) { | ||
instance = CommandManagerSettings.getSettings(environment); | ||
} | ||
return CommandManagerSettings.instance; | ||
} | ||
|
||
/** Parse settings for a single client. */ | ||
public static CommandManagerSettings getSettings(Environment environment) { | ||
|
||
final Settings settings = environment.settings(); | ||
if (settings != null) { | ||
log.info("Settings created with the keystore information."); | ||
try (SecureString authUsername = M_API_AUTH_USERNAME.get(settings); | ||
SecureString authPassword = M_API_AUTH_PASSWORD.get(settings); | ||
SecureString uri = M_API_URI.get(settings); ) { | ||
return new CommandManagerSettings( | ||
authUsername.toString(), authPassword.toString(), uri.toString()); | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public String getAuthPassword() { | ||
return authPassword; | ||
} | ||
|
||
public String getAuthUsername() { | ||
return authUsername; | ||
} | ||
|
||
public String getUri() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CommandManagerSettings{" | ||
+ " authUsername='" | ||
+ authUsername | ||
+ '\'' | ||
+ ", authPassword='" | ||
+ authPassword | ||
+ '\'' | ||
+ ", uri='" | ||
+ uri | ||
+ '\'' | ||
+ '}'; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ager/src/main/java/com/wazuh/commandmanager/settings/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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; | ||
|
||
public class CommandManagerSettingsException extends Exception { | ||
|
||
// Constructor that accepts a message | ||
public CommandManagerSettingsException(String message) { | ||
super(message); | ||
} | ||
|
||
// Exception for the case when load keystore failed | ||
public static CommandManagerSettingsException loadSettingsFailed( | ||
String keyStorePath, String errorMessage) { | ||
return new CommandManagerSettingsException( | ||
"Load settings from: " + keyStorePath + " failed. Error: " + errorMessage); | ||
} | ||
|
||
// 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); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
plugins/command-manager/src/main/plugin-metadata/plugin-security.policy
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
grant { | ||
permission java.net.SocketPermission "*", "connect,resolve"; | ||
}; | ||
}; |
Oops, something went wrong.