Skip to content

Add content update to init process #421

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

Merged
merged 7 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Implement content update using Json Patch operations [(#362)](https://github.com/wazuh/wazuh-indexer-plugins/pull/362)
- Implement CTI API client rate limit and enhanced response handling [(#363)](https://github.com/wazuh/wazuh-indexer-plugins/pull/363)
- Add custom action to list modified plugins [(#388)](https://github.com/wazuh/wazuh-indexer-plugins/pull/388)
- Expose settings for the Content Manager plugin [#395](https://github.com/wazuh/wazuh-indexer-plugins/pull/395)
- Expose settings for the Content Manager plugin [(#395)](https://github.com/wazuh/wazuh-indexer-plugins/pull/395)
- Add content update to to the Content Manager init process [(#421)](https://github.com/wazuh/wazuh-indexer-plugins/pull/421)

### Dependencies
-

### Changed
- Update http components to latest versions [#403](https://github.com/wazuh/wazuh-indexer-plugins/pull/403)
- Content Manager tier 1 final wrap up [#373](https://github.com/wazuh/wazuh-indexer-plugins/pull/373)
- Update http components to latest versions [(#403)](https://github.com/wazuh/wazuh-indexer-plugins/pull/403)
- Content Manager tier 1 final wrap up [(#373)](https://github.com/wazuh/wazuh-indexer-plugins/pull/373)
- Refactor Content Manager's code and fix Catalog info indexing [(#317)](https://github.com/wazuh/wazuh-indexer-plugins/pull/317)
- Improved mdbook installation instructions [#332](https://github.com/wazuh/wazuh-indexer-plugins/pull/332)
- Third-party integrations maintenance [(#299)](https://github.com/wazuh/wazuh-indexer-plugins/pull/299) [(#374)](https://github.com/wazuh/wazuh-indexer-plugins/pull/374) [(#398)](https://github.com/wazuh/wazuh-indexer-plugins/pull/398)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.*;

import com.wazuh.contentmanager.model.cti.Changes;
import com.wazuh.contentmanager.model.cti.ConsumerInfo;
import com.wazuh.contentmanager.model.cti.ContentChanges;
import com.wazuh.contentmanager.settings.PluginSettings;
import com.wazuh.contentmanager.utils.VisibleForTesting;
import com.wazuh.contentmanager.utils.XContentUtils;
Expand Down Expand Up @@ -152,9 +151,9 @@ public static synchronized CTIClient getInstance() {
* @param fromOffset The starting offset (inclusive) for fetching changes.
* @param toOffset The ending offset (exclusive) for fetching changes.
* @param withEmpties A flag indicating whether to include empty values (Optional).
* @return {@link ContentChanges} instance with the current changes.
* @return {@link Changes} instance with the current changes.
*/
public ContentChanges getChanges(long fromOffset, long toOffset, boolean withEmpties) {
public Changes getChanges(long fromOffset, long toOffset, boolean withEmpties) {
Map<String, String> params =
CTIClient.contextQueryParameters(fromOffset, toOffset, withEmpties);
SimpleHttpResponse response =
Expand All @@ -165,23 +164,21 @@ public ContentChanges getChanges(long fromOffset, long toOffset, boolean withEmp
params,
null,
this.pluginSettings.getCtiClientMaxAttempts());

// Fail fast
if (response == null) {
log.error("No response from CTI API Changes endpoint");
return new ContentChanges();
log.error("No reply from [{}]", this.CONSUMER_CHANGES_ENDPOINT);
return new Changes();
}
if (!Arrays.asList(HttpStatus.SC_OK, HttpStatus.SC_SUCCESS).contains(response.getCode())) {
log.error("CTI API Changes endpoint returned an error: {}", response.getBody());
return new ContentChanges();
log.error("Request to [{}] failed: {}", this.CONSUMER_CHANGES_ENDPOINT, response.getBody());
return new Changes();
}

log.debug("CTI API Changes endpoint replied with status: [{}]", response.getCode());
log.debug("[{}] replied with status [{}]", this.CONSUMER_CHANGES_ENDPOINT, response.getCode());
try {
return ContentChanges.parse(XContentUtils.createJSONParser(response.getBodyBytes()));
return Changes.parse(XContentUtils.createJSONParser(response.getBodyBytes()));
} catch (IOException | IllegalArgumentException e) {
log.error("Failed to fetch changes information due to: {}", e.getMessage());
return new ContentChanges();
log.error("Failed to parse changes: {}", e.getMessage());
return new Changes();
}
}

Expand All @@ -204,9 +201,9 @@ public ConsumerInfo getConsumerInfo() throws HttpHostConnectException, IOExcepti
);
// spotless:on
if (response == null) {
throw new HttpHostConnectException("No reply to " + this.CONSUMER_INFO_ENDPOINT);
throw new HttpHostConnectException("No reply from [" + this.CONSUMER_INFO_ENDPOINT + "]");
}
log.debug("CTI API replied with status: [{}]", response.getCode());
log.debug("[{}] replied with status [{}]", this.CONSUMER_INFO_ENDPOINT, response.getCode());
return ConsumerInfo.parse(XContentUtils.createJSONParser(response.getBodyBytes()));
}

Expand Down Expand Up @@ -248,9 +245,6 @@ protected SimpleHttpResponse sendRequest(
Map<String, String> params,
Header header,
int attemptsLeft) {
// TODO used to debug the failing test "testGetChanges_SuccessfulRequest".
// log.error("sendRequest {} {} {} {} {} {}", method, endpoint, body, params, header,
// attemptsLeft);
ZonedDateTime cooldown = null;
SimpleHttpResponse response = null;
while (attemptsLeft > 0) {
Expand Down Expand Up @@ -284,7 +278,8 @@ protected SimpleHttpResponse sendRequest(
int statusCode = response.getCode();
switch (statusCode) {
case 200:
log.info("Operation succeeded: status code {} - {}", statusCode, response.getBodyText());
log.info("Operation succeeded: status code {}", statusCode);
log.debug("Response body: {}", response.getBodyText());
return response;

case 400:
Expand Down Expand Up @@ -367,7 +362,7 @@ public Path download(String snapshotURI, Environment env) {
}
}
}
log.info("Snapshot downloaded to {}", path);
log.info("Snapshot downloaded to [{}]", path);
return path;
} catch (URISyntaxException e) {
log.error("Failed to download snapshot. Invalid URL provided: {}", e.getMessage());
Expand Down
Loading