Skip to content

Commit

Permalink
Adapt the plugin for POST requests to a local server
Browse files Browse the repository at this point in the history
  • Loading branch information
f-galland committed Oct 3, 2024
1 parent 2e89ff9 commit acceed2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.wazuh.commandmanager;

import com.wazuh.commandmanager.config.reader.ConfigReader;
import com.wazuh.commandmanager.index.CommandIndex;
import com.wazuh.commandmanager.rest.action.RestPostCommandAction;
import com.wazuh.commandmanager.scheduler.JobScheduler;
Expand Down Expand Up @@ -48,7 +49,6 @@ public class CommandManagerPlugin extends Plugin implements ActionPlugin {
public static final String COMMAND_MANAGER_INDEX_TEMPLATE_NAME = "index-template-commands";

private CommandIndex commandIndex;
private JobScheduler jobScheduler;

@Override
public Collection<Object> createComponents(
Expand All @@ -65,7 +65,8 @@ public Collection<Object> createComponents(
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
this.commandIndex = new CommandIndex(client, clusterService, threadPool);
this.jobScheduler = new JobScheduler(threadPool);
ConfigReader configReader = new ConfigReader("127.0.0.1", 8080, "", "admin", "admin");
JobScheduler jobScheduler = new JobScheduler(threadPool, configReader);
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ public class ConfigReader {
String password;

public ConfigReader() {
// Hardcoded output API values for testing purposes
//this.ipAddress = "127.0.0.1";
//this.port = 5000;
//this.path = "/test/post";
//this.username = "admin";
//this.password = "admin";
this.hostName = "jsonplaceholder.typicode.com";
this.port = 80;
this.path = "/posts/1";
this.username = "admin";
this.password = "admin";
}

public ConfigReader(String hostName, int port, String path, String username, String password) {
this.hostName = hostName;
this.port = port;
this.path = path;
this.username = username;
this.password = password;
}

public String getHostName() {
return hostName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -93,16 +94,14 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke
}

public CompletableFuture<String> performAsyncRequest() throws Exception {
// @Todo: Remove hardcoded values

this.requester.start();

CompletableFuture<String> future = new CompletableFuture<>();

this.requester.execute(
AsyncRequestBuilder.get()
AsyncRequestBuilder.post()
.setHttpHost(this.target)
.setPath(this.requestUri)
.setEntity("{\"field\":\"value\"}",ContentType.APPLICATION_JSON)
.build(),
new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
Timeout.ofSeconds(5),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.threadpool.ThreadPool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

public class JobScheduler {

private static final Logger logger = LogManager.getLogger(JobScheduler.class);
private final ConfigReader configReader;

public JobScheduler(ThreadPool threadPool) {
public JobScheduler(ThreadPool threadPool, ConfigReader configReader) {
this.configReader = configReader;
start(threadPool);
}

Expand All @@ -25,8 +26,7 @@ private void start(ThreadPool threadPool) {
try {
Thread.sleep(5000);
logger.info("Running task");
ConfigReader configReader = new ConfigReader();
AsyncRequestRepository asyncRequestRepository = new AsyncRequestRepository(configReader);
AsyncRequestRepository asyncRequestRepository = new AsyncRequestRepository(this.configReader);
asyncRequestRepository.performAsyncRequest()
.thenAccept(
logger::info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
grant {
permission java.net.SocketPermission "*:80", "connect,resolve";
permission java.net.SocketPermission "*:8080", "connect,resolve";
};

0 comments on commit acceed2

Please sign in to comment.