-
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 plugin-security.policy * Add dependencies and async http requests service class. * Make the plugin perform periodical http queries using placeholder scheduler and config classes * Adapt the plugin for POST requests to a local server * Small fixes * Switch to using HttpClient * Fix slf4j nop warnings * Fix test failing on forbidden api usage * Add json body * Use doPrivileged * Fix socket permission denied error * Make close() work again * Add lifecycle component back to jobscheduler placeholder class * Make AsyncRequestRepository a singleton * Remove unneeded dependencies * Add license and notice files * Fix forbiddenapis error * Skip checks dependency license checks and dependencies forbidden apis check * Refactor HttpClient and add unit tests (#102) * Refactor HttpClient and add unit tests * Add more JavaDocs * Fix Javadocs --------- Signed-off-by: Álex Ruiz <[email protected]> Co-authored-by: Álex Ruiz <[email protected]>
- Loading branch information
Showing
8 changed files
with
338 additions
and
6 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
53 changes: 53 additions & 0 deletions
53
...manager/src/main/java/com/wazuh/commandmanager/utils/httpclient/HttpResponseCallback.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,53 @@ | ||
/* | ||
* 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.utils.httpclient; | ||
|
||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
import org.apache.hc.core5.http.message.StatusLine; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class HttpResponseCallback implements FutureCallback<SimpleHttpResponse> { | ||
|
||
private static final Logger log = LogManager.getLogger(HttpResponseCallback.class); | ||
|
||
/** | ||
* The Http get request. | ||
*/ | ||
SimpleHttpRequest httpRequest; | ||
|
||
/** | ||
* The Error message. | ||
*/ | ||
String errorMessage; | ||
|
||
public HttpResponseCallback(SimpleHttpRequest httpRequest, | ||
String errorMessage) { | ||
this.httpRequest = httpRequest; | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
@Override | ||
public void completed(SimpleHttpResponse response) { | ||
log.debug("{}->{}", httpRequest, new StatusLine(response)); | ||
log.debug("Got response: {}", response.getBody()); | ||
} | ||
|
||
@Override | ||
public void failed(Exception ex) { | ||
log.error("{}->{}", httpRequest, ex); | ||
// throw new HttpException(errorMessage, ex); | ||
} | ||
|
||
@Override | ||
public void cancelled() { | ||
log.debug(httpRequest + " cancelled"); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...mmand-manager/src/main/java/com/wazuh/commandmanager/utils/httpclient/HttpRestClient.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,131 @@ | ||
/* | ||
* 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.utils.httpclient; | ||
|
||
import org.apache.hc.client5.http.async.methods.*; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients; | ||
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; | ||
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.hc.core5.io.CloseMode; | ||
import org.apache.hc.core5.reactor.IOReactorConfig; | ||
import org.apache.hc.core5.util.Timeout; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.Randomness; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* HTTP Rest client. Currently used to perform | ||
* POST requests against the Wazuh Server. | ||
*/ | ||
public class HttpRestClient { | ||
|
||
private static final Logger log = LogManager.getLogger(HttpRestClient.class); | ||
private static HttpRestClient instance; | ||
private CloseableHttpAsyncClient httpClient; | ||
|
||
/** | ||
* Private default constructor | ||
*/ | ||
private HttpRestClient() { | ||
startHttpAsyncClient(); | ||
} | ||
|
||
/** | ||
* Singleton instance accessor | ||
* | ||
* @return {@link HttpRestClient#instance} | ||
*/ | ||
public static HttpRestClient getInstance() { | ||
if (HttpRestClient.instance == null) { | ||
instance = new HttpRestClient(); | ||
} | ||
return HttpRestClient.instance; | ||
} | ||
|
||
/** | ||
* Starts http async client. | ||
*/ | ||
private void startHttpAsyncClient() { | ||
if (this.httpClient == null) { | ||
try { | ||
PoolingAsyncClientConnectionManager cm = | ||
PoolingAsyncClientConnectionManagerBuilder.create().build(); | ||
|
||
IOReactorConfig ioReactorConfig = IOReactorConfig.custom() | ||
.setSoTimeout(Timeout.ofSeconds(5)) | ||
.build(); | ||
|
||
httpClient = HttpAsyncClients.custom() | ||
.setIOReactorConfig(ioReactorConfig) | ||
.setConnectionManager(cm) | ||
.build(); | ||
|
||
httpClient.start(); | ||
} catch (Exception e) { | ||
// handle exception | ||
log.error("Error starting async Http client {}", e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Stop http async client. | ||
*/ | ||
public void stopHttpAsyncClient() { | ||
if (this.httpClient != null) { | ||
log.info("Shutting down."); | ||
httpClient.close(CloseMode.GRACEFUL); | ||
httpClient = null; | ||
} | ||
} | ||
|
||
/** | ||
* Sends a POST request. | ||
* | ||
* @param uri Well-formed URI | ||
* @param payload data to send | ||
* @return HTTP response | ||
*/ | ||
public SimpleHttpResponse post(URI uri, String payload) { | ||
Long id = Randomness.get().nextLong(); | ||
|
||
try { | ||
// Create request | ||
HttpHost httpHost = HttpHost.create(uri.getHost()); | ||
|
||
SimpleHttpRequest httpPostRequest = SimpleRequestBuilder | ||
.post() | ||
.setHttpHost(httpHost) | ||
.setPath(uri.getPath()) | ||
.setBody(payload, ContentType.APPLICATION_JSON) | ||
.build(); | ||
|
||
// log request | ||
Future<SimpleHttpResponse> future = | ||
this.httpClient.execute( | ||
SimpleRequestProducer.create(httpPostRequest), | ||
SimpleResponseConsumer.create(), | ||
new HttpResponseCallback( | ||
httpPostRequest, | ||
"Failed to send data for ID: " + id | ||
) | ||
); | ||
|
||
return future.get(); | ||
} catch (Exception e) { | ||
log.error("Failed to send data for ID: {}", id); | ||
} | ||
return null; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...d-manager/src/main/java/com/wazuh/commandmanager/utils/httpclient/HttpRestClientDemo.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,50 @@ | ||
/* | ||
* 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.utils.httpclient; | ||
|
||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; | ||
import org.apache.hc.core5.net.URIBuilder; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
|
||
/** | ||
* Demo class to test the {@link HttpRestClient} class. | ||
*/ | ||
public class HttpRestClientDemo { | ||
|
||
private static final Logger log = LogManager.getLogger(HttpRestClientDemo.class); | ||
|
||
/** | ||
* Demo method to test the {@link HttpRestClient} class. | ||
* | ||
* @param endpoint POST's requests endpoint as a well-formed URI | ||
* @param body POST's request body as a JSON string. | ||
*/ | ||
public static void run(String endpoint, String body) { | ||
log.info("Executing POST request"); | ||
AccessController.doPrivileged( | ||
(PrivilegedAction<SimpleHttpResponse>) () -> { | ||
HttpRestClient httpClient = HttpRestClient.getInstance(); | ||
URI host; | ||
try { | ||
host = new URIBuilder(endpoint).build(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
SimpleHttpResponse postResponse = httpClient.post(host, body); | ||
log.info(postResponse.getBodyText()); | ||
return postResponse; | ||
} | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
grant { | ||
permission java.net.SocketPermission "*", "connect,resolve"; | ||
}; |
Oops, something went wrong.