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 commands expansion logic #229

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3f93798
Add getters to Target model
QU3B1M Jan 17, 2025
b1304f5
Merge branch 'master' into enhancement/88-implement-commands-expansion
QU3B1M Jan 17, 2025
663688f
Implement agents search logic
QU3B1M Jan 20, 2025
fe2ed9e
Merge branch 'master' into enhancement/88-implement-commands-expansion
QU3B1M Jan 20, 2025
4f5fcb6
Remove unused code
QU3B1M Jan 20, 2025
32e5014
Use the .agents search results to instance new Agents objects
QU3B1M Jan 20, 2025
df80be8
Add agent search for command.target type agent
QU3B1M Jan 21, 2025
931a6b7
Add countdown latch to await async search
QU3B1M Jan 21, 2025
e7ce20e
Update document generation to cover the 'server' command.target.type
QU3B1M Jan 21, 2025
aded803
Implement Search utils class with generic search functions
QU3B1M Jan 21, 2025
1a1616f
Rename functions to be more self-explanatory
QU3B1M Jan 22, 2025
9112b11
Replace Document model with Order
QU3B1M Jan 22, 2025
743bf7d
Apply spotless formatting
QU3B1M Jan 22, 2025
c9a6f26
Add docstrings
QU3B1M Jan 22, 2025
4105917
Move generic search-related code from SearchThread to Search util class
QU3B1M Jan 22, 2025
ad9ccd7
Add ID field to Agent class
QU3B1M Jan 22, 2025
c440ded
Add info log to check the search results amount
QU3B1M Jan 22, 2025
e58b6e1
Remove async option from search executor
QU3B1M Jan 22, 2025
6c85018
Add docstrings
QU3B1M Jan 22, 2025
ee07cb3
Improve classes documentation
QU3B1M Jan 23, 2025
b5afbc9
Fix commands with invalid target id were indexed after a valid search
QU3B1M Jan 23, 2025
38f368f
Merge branch 'master' into enhancement/88-implement-commands-expansion
QU3B1M Jan 27, 2025
6fe8f7a
Update agents index name references
QU3B1M Jan 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

import com.wazuh.commandmanager.model.Document;
import com.wazuh.commandmanager.model.Order;
import com.wazuh.commandmanager.settings.PluginSettings;
import com.wazuh.commandmanager.utils.IndexTemplateUtils;

Expand Down Expand Up @@ -72,22 +72,22 @@ public boolean indexExists() {
/**
* Indexes an array of documents asynchronously.
*
* @param documents list of instances of the document model to persist in the index.
* @param orders list of instances of the document model to persist in the index.
* @return A CompletableFuture with the RestStatus response from the operation
*/
public CompletableFuture<RestStatus> asyncBulkCreate(ArrayList<Document> documents) {
public CompletableFuture<RestStatus> asyncBulkCreate(ArrayList<Order> orders) {
final CompletableFuture<RestStatus> future = new CompletableFuture<>();
final ExecutorService executor = this.threadPool.executor(ThreadPool.Names.WRITE);

final BulkRequest bulkRequest = new BulkRequest();
for (Document document : documents) {
log.info("Adding command with id [{}] to the bulk request", document.getId());
for (Order order : orders) {
log.info("Adding command with id [{}] to the bulk request", order.getId());
try {
bulkRequest.add(createIndexRequest(document));
bulkRequest.add(createIndexRequest(order));
} catch (IOException e) {
log.error(
"Error creating IndexRequest with document id [{}] due to {}",
document.getId(),
order.getId(),
e.getMessage());
}
}
Expand Down Expand Up @@ -120,15 +120,15 @@ public CompletableFuture<RestStatus> asyncBulkCreate(ArrayList<Document> documen
/**
* Create an IndexRequest object from a Document object.
*
* @param document the document to create the IndexRequest for COMMAND_MANAGER_INDEX
* @param order the document to create the IndexRequest for COMMAND_MANAGER_INDEX
* @return an IndexRequest object
* @throws IOException thrown by XContentFactory.jsonBuilder()
*/
private IndexRequest createIndexRequest(Document document) throws IOException {
private IndexRequest createIndexRequest(Order order) throws IOException {
return new IndexRequest()
.index(PluginSettings.getIndexName())
.source(document.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.id(document.getId())
.source(order.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.id(order.getId())
.create(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
public class Agent implements ToXContentObject {
public static final String AGENT = "agent";
public static final String GROUPS = "groups";
public static final String ID = "id";
private final String id;
private final List<String> groups;

/**
* Default constructor.
*
* @param groups Agent's groups
*/
public Agent(List<String> groups) {
public Agent(String id, List<String> groups) {
this.id = id;
this.groups = groups;
}

Expand All @@ -47,20 +50,23 @@ public Agent(List<String> groups) {
*/
public static Agent parse(XContentParser parser) throws IOException {
List<Object> groups = List.of();
String id = null;

while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
if (fieldName.equals(GROUPS)) {
groups = parser.list();
} else if (fieldName.equals(ID)) {
id = parser.text();
} else {
parser.skipChildren();
}
}

// Cast args field Object list to String list
List<String> convertedGroupFields = (List<String>) (List<?>) (groups);
return new Agent(convertedGroupFields);
return new Agent(id, convertedGroupFields);
}

@Override
Expand All @@ -72,6 +78,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public String toString() {
return "Agent{" + "groups=" + groups + '}';
return "Agent{" + "id=" + id + '\'' + ", groups=" + groups + '}';
}
}

This file was deleted.

This file was deleted.

Loading