Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a new KeyStore file to configure the persistence logic #97

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7329507
Create the initial structure
mcasas993 Oct 9, 2024
ae988a8
Manage the settings of the plugin. Create and reload the keystore.
mcasas993 Oct 11, 2024
a4e5f72
Add the security settings
mcasas993 Oct 11, 2024
d92ce1b
Fix license of CommandManagerSettings and CommandManagerSettingsExcep…
mcasas993 Oct 14, 2024
4342cb2
Merge branch 'master' into 95-configuration-persistence-logic
AlexRuiz7 Oct 14, 2024
a97e2d6
Rename test class to match convention
AlexRuiz7 Oct 14, 2024
65f6f9d
Fix comments in the merge request
mcasas993 Oct 14, 2024
f69c68d
Add password in save of KeyStoreWrapper and do some changes to reload…
mcasas993 Oct 15, 2024
67c7434
Plugin Settings Unit Test
mcasas993 Oct 16, 2024
4eb7339
Refactor of CommandManagerSettings to manage all the settings of the …
mcasas993 Oct 17, 2024
bba618d
Fix some errors in COmmandManagerSettings and create the associated u…
mcasas993 Oct 17, 2024
6f8a9b5
Merge branch 'master' of github.com:wazuh/wazuh-indexer-plugins into …
AlexRuiz7 Oct 18, 2024
99cf140
Log settings on load
AlexRuiz7 Oct 18, 2024
4b8cc0e
Apply spotless
AlexRuiz7 Oct 18, 2024
a7cf4b6
Unificate the CommandManagerSettings getSetting methods. Add some log…
mcasas993 Oct 21, 2024
4c105dd
Add another test for the diferents possibilities on the method getSet…
mcasas993 Oct 21, 2024
bc61647
Fix settings name in CommanManagerSettings
mcasas993 Oct 22, 2024
24f1699
Delete @Test annotation
mcasas993 Oct 22, 2024
d643f1f
Send commands to the M_API using the configuration service
AlexRuiz7 Oct 23, 2024
428b365
Commments
AlexRuiz7 Oct 23, 2024
78f29dc
Merge branch 'master' into 95-configuration-persistence-logic
AlexRuiz7 Oct 23, 2024
49ef455
Replace @Ignore with @AwaitsFix
AlexRuiz7 Oct 23, 2024
9ff0ff5
Merge branch '95-configuration-persistence-logic' of github.com:wazuh…
AlexRuiz7 Oct 23, 2024
a6f60ae
Simplify the general logic of CommandManagerSettings. Apply Singleton…
mcasas993 Oct 24, 2024
8e587e5
Modify the test according to the new behavior of CommandManagerSettings
mcasas993 Oct 24, 2024
57f4d4c
Merge branch 'master' into 95-configuration-persistence-logic
AlexRuiz7 Oct 25, 2024
0d14eda
Delete testing logs of CommandManagerPlugin
mcasas993 Oct 25, 2024
ad18cad
Delete unnecesary resource wazuh-indexer.keystore.json
mcasas993 Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions plugins/command-manager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ testClusters.integTest {
plugin(project.tasks.bundlePlugin.archiveFile)

// add customized keystore
keystore 'command.manager.keystore', new File("$projectDir/src/test/resources/", 'wazuh-indexer.keystore.json')
keystore 'command.manager.auth.username', 'admin'
keystore 'command.manager.auth.password', 'test'

keystore 'command.manager.uri', 'https://httpbin.org/post'
AlexRuiz7 marked this conversation as resolved.
Show resolved Hide resolved
}

run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Collection<Object> createComponents(
Supplier<RepositoriesService> repositoriesServiceSupplier) {
this.commandIndex = new CommandIndex(client, clusterService, threadPool);

this.commandManagerSettings = CommandManagerSettings.getSettings(environment);
this.commandManagerSettings = CommandManagerSettings.getSettings(environment, null);

// HttpRestClient stuff
String uri = "https://httpbin.org/post";
Expand All @@ -93,11 +93,9 @@ public List<RestHandler> getRestHandlers(
public List<Setting<?>> getSettings() {
return Arrays.asList(
// Register API settings
CommandManagerSettings.KEYSTORE,
CommandManagerSettings.AUTH_USERNAME,
CommandManagerSettings.AUTH_PASSWORD,
CommandManagerSettings.URI,
CommandManagerSettings.AUTH_TYPE);
CommandManagerSettings.M_API_USERNAME,
CommandManagerSettings.M_API_PASSWORD,
CommandManagerSettings.M_API_URI);
}

@Override
Expand Down
mcasas993 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,24 @@
import com.wazuh.commandmanager.CommandManagerSettingsException;

public final class CommandManagerSettings {
/** 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. */
public static final Setting<SecureString> AUTH_USERNAME =
public static final Setting<SecureString> M_API_USERNAME =
SecureSetting.secureString("command.manager.auth.username", null);

/** The secret key (ie password) for connecting to api. */
public static final Setting<SecureString> AUTH_PASSWORD =
public static final Setting<SecureString> M_API_PASSWORD =
SecureSetting.secureString("command.manager.auth.password", null);
mcasas993 marked this conversation as resolved.
Show resolved Hide resolved

/** The uri for connecting to api. */
public static final Setting<String> URI =
SecureSetting.simpleString("command.manager.uri", Setting.Property.NodeScope);
public static final Setting<SecureString> M_API_URI =
SecureSetting.secureString("command.manager.uri", null);

/** 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. */
final String keystore;
private static final String KEYSTORE_FILENAME = "opensearch.keystore";

/** The access key (ie login username) for connecting to api. */
final String authUsername;
Expand All @@ -63,89 +53,37 @@ public final class CommandManagerSettings {
/** The uri for connecting to api. */
final String uri;

/** The auth type for connecting to api. */
final String authType;

private CommandManagerSettings(
String keystore,
String authUsername,
String authPassword,
String uri,
String authType) {
this.keystore = keystore;
String uri) {
this.authUsername = authUsername;
this.authPassword = authPassword;
this.uri = uri;
this.authType = authType;
log.info("Plugin settings: {}", this.toString());
log.info("CommandManagerSettings created ");
}

/** 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());
}

if (keyStoreWrapper == null) {
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());
}
}

final Settings settings = Settings.builder().setSecureSettings(keyStoreWrapper).build();

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));
}
}
}

/** Parse settings for a single client. */
public static CommandManagerSettings getSettings(Environment environment) {
public static CommandManagerSettings getSettings(Environment environment, SecureString secureSettingsPassword) {
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());
log.error(CommandManagerSettingsException.keystoreNotExist(
keystoreFile.toAbsolutePath().toString()).getMessage());
return null;
} else {
log.warn(
"Por hacer load de KeyStoreWrapper en "
+ environment.configFile().toString());
keyStoreWrapper = KeyStoreWrapper.load(environment.configFile(), KEYSTORE_FILENAME);
log.info(
"Keystore load: "
+ keystoreFile.toAbsolutePath().toString());
}
} catch (Exception e) {
log.error(
CommandManagerSettingsException.loadKeystoreFailed(keystoreFile.toString())
.getMessage());
return null;
}

if (keyStoreWrapper == null) {
Expand All @@ -155,35 +93,38 @@ public static CommandManagerSettings getSettings(Environment environment) {
return null;
} else {
// Decrypt the keystore using the password from the request

try {
keyStoreWrapper.decrypt(new char[0]);
log.info("Decrypting the keystore.");
if (secureSettingsPassword == null || secureSettingsPassword.length() == 0) {
keyStoreWrapper.decrypt(new char[0]);
} else {
keyStoreWrapper.decrypt(secureSettingsPassword.getChars());
}
} catch (GeneralSecurityException | IOException e) {
log.error(
CommandManagerSettingsException.decryptKeystoreFailed(KEYSTORE_FILENAME)
.getMessage());
}

final Settings settings = Settings.builder().setSecureSettings(keyStoreWrapper).build();
log.info("Settings created with the keystore information.");

try (SecureString authUsername = AUTH_USERNAME.get(settings);
SecureString authPassword = AUTH_PASSWORD.get(settings); ) {
try (SecureString authUsername = M_API_USERNAME.get(settings);
SecureString authPassword = M_API_PASSWORD.get(settings);
SecureString uri = M_API_URI.get(settings);) {
return new CommandManagerSettings(
KEYSTORE_FILENAME,
authUsername.toString(),
authPassword.toString(),
URI.get(settings),
AUTH_TYPE.get(settings));
uri.toString());
}
}
}

@Override
public String toString() {
return "CommandManagerSettings{"
+ "keystore='"
+ keystore
+ '\''
+ ", authUsername='"
+ " authUsername='"
+ authUsername
+ '\''
+ ", authPassword='"
Expand All @@ -192,9 +133,6 @@ public String toString() {
+ ", uri='"
+ uri
+ '\''
+ ", authType='"
+ authType
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ public void testGetSettings_keystoreExists() throws Exception {
log.warn("test INSIDE+EE");

this.commandManagerSettings =
CommandManagerSettings.getSettings(mockEnvironment);
CommandManagerSettings.getSettings(mockEnvironment, null);

assertNotNull(commandManagerSettings);
log.warn("keystore INSIDE" + commandManagerSettings.keystore);
// verify(keyStoreWrapper,
log.info("Plugin settings: {}", commandManagerSettings.toString()); // verify(keyStoreWrapper,
// times(1)).decrypt(secureString.getChars());
} catch (IOException e) {
log.warn("ERROR TEST: " + e.getMessage());
Expand Down