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

Add command management endpoints #72

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,73 @@
*/
package com.wazuh.commandmanager;

import com.wazuh.commandmanager.rest.action.RestPostCommandAction;
import com.wazuh.commandmanager.utils.CommandManagerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

public class CommandManagerPlugin extends Plugin {
// Implement the relevant Plugin Interfaces here

public class CommandManagerPlugin extends Plugin implements ActionPlugin {
public static final String COMMAND_MANAGER_BASE_URI = "/_plugins/_commandmanager";
public static final String COMMAND_MANAGER_INDEX_NAME = "command-manager";
private static final Logger log = LogManager.getLogger(CommandManagerPlugin.class);

private CommandManagerService commandManagerService;

@Override
public Collection<Object> createComponents(
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
ScriptService scriptService,
NamedXContentRegistry xContentRegistry,
Environment environment,
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
this.commandManagerService = new CommandManagerService(client, clusterService);

return Collections.emptyList();
}

public List<RestHandler> getRestHandlers(
Settings settings,
RestController restController,
ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings,
SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster
) {
RestPostCommandAction restPostCommandAction = new RestPostCommandAction(commandManagerService);
return Collections.singletonList(restPostCommandAction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
package com.wazuh.commandmanager.model;

import org.opensearch.common.Nullable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

public class CommandDetails implements ToXContentObject {


private String commandOrderId;
private String commandRequestId;
private String commandSource;
private String commandTarget;
private String commandTimeout;
private String commandType;
private String commandUser;
private Map<String, Object> commandAction;
private Map<String, Object> commandResult;

public static final String DOCUMENT_ID = "document_id";
public static final String COMMAND_ORDER_ID = "command_order_id";
public static final String COMMAND_REQUEST_ID = "command_request_id";
public static final String COMMAND_SOURCE = "command_source";
public static final String COMMAND_TARGET = "command_target";
public static final String COMMAND_TIMEOUT = "command_timeout";
public static final String COMMAND_TYPE = "command_type";
public static final String COMMAND_USER = "command_user";
public static final String COMMAND_ACTION = "command_action";
public static final String COMMAND_RESULT = "command_result";

public CommandDetails() {}

public CommandDetails(
String commandOrderId,
String commandRequestId,
String commandSource,
String commandTarget,
String commandTimeout,
String commandType,
String commandUser,
Map<String,Object> commandAction,
Map<String,Object> commandResult
) {
this.commandOrderId = commandOrderId;
this.commandRequestId = commandRequestId;
this.commandSource = commandSource;
this.commandTarget = commandTarget;
this.commandTimeout = commandTimeout;
this.commandType = commandType;
this.commandUser = commandUser;
this.commandAction = commandAction;
this.commandResult = commandResult;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
XContentBuilder xContentBuilder = builder.startObject();
if (commandOrderId != null) {
xContentBuilder.field(COMMAND_ORDER_ID, commandOrderId);
}
if (commandRequestId != null) {
xContentBuilder.field(COMMAND_REQUEST_ID, commandRequestId);
}
if (commandSource != null) {
xContentBuilder.field(COMMAND_SOURCE, commandSource);
}
if (commandTarget != null) {
xContentBuilder.field(COMMAND_TARGET, commandTarget);
}
if (commandTimeout != null) {
xContentBuilder.field(COMMAND_TIMEOUT, commandTimeout);
}
if (commandType != null) {
xContentBuilder.field(COMMAND_TYPE, commandType);
}
if (commandUser != null) {
xContentBuilder.field(COMMAND_USER, commandUser);
}
if (commandAction != null) {
xContentBuilder.field(COMMAND_ACTION, commandAction);
}
if (commandResult != null) {
xContentBuilder.field(COMMAND_RESULT, commandResult);
}
return xContentBuilder.endObject();
}

public static CommandDetails parse(XContentParser parser) throws IOException {
String commandOrderId = null;
String commandRequestId = null;
String commandSource = null;
String commandTarget = null;
String commandTimeout = null;
String commandType = null;
String commandUser = null;
Map<String,Object> commandAction = null;
Map<String,Object> commandResult = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case COMMAND_ORDER_ID:
commandOrderId = parser.text();
break;
case COMMAND_REQUEST_ID:
commandRequestId = parser.text();
break;
case COMMAND_SOURCE:
commandSource = parser.text();
break;
case COMMAND_TARGET:
commandTarget = parser.text();
break;
case COMMAND_TIMEOUT:
commandTimeout = parser.text();
break;
case COMMAND_TYPE:
commandType = parser.text();
break;
case COMMAND_USER:
commandUser = parser.text();
break;
case COMMAND_ACTION:
commandAction = parser.map();
break;
case COMMAND_RESULT:
commandResult = parser.map();
break;
default:
parser.skipChildren();
break;
}
}

return new CommandDetails(
commandOrderId,
commandRequestId,
commandSource,
commandTarget,
commandTimeout,
commandType,
commandUser,
commandAction,
commandResult
);
}

public CommandDetails(final CommandDetails copyCommandDetails) {
this(
copyCommandDetails.commandOrderId,
copyCommandDetails.commandRequestId,
copyCommandDetails.commandSource,
copyCommandDetails.commandTarget,
copyCommandDetails.commandTimeout,
copyCommandDetails.commandType,
copyCommandDetails.commandUser,
copyCommandDetails.commandAction,
copyCommandDetails.commandResult
);
}

@Nullable
public String getCommandOrderId() {
return commandOrderId;
}
public void setCommandOrderId(String commandOrderId) {
this.commandOrderId = commandOrderId;
}

@Nullable
public String getCommandRequestId() {
return commandRequestId;
}
public void setCommandRequestId(String commandRequestId) {
this.commandRequestId = commandRequestId;
}

@Nullable
public String getCommandSource() {
return commandSource;
}
public void setCommandSource(String commandSource) {
this.commandSource = commandSource;
}

@Nullable
public String getCommandTarget() {
return commandTarget;
}
public void setCommandTarget(String commandTarget) {
this.commandTarget = commandTarget;
}

@Nullable
public String getCommandTimeout() {
return commandTimeout;
}
public void setCommandTimeout(String commandTimeout) {
this.commandTimeout = commandTimeout;
}

@Nullable
public String getCommandType() {
return commandType;
}
public void setCommandType(String commandType) {
this.commandType = commandType;
}

@Nullable
public String getCommandUser() {
return commandUser;
}
public void setCommandUser(String commandUser) {
this.commandUser = commandUser;
}

@Nullable
public Map<String, Object> getCommandAction() {
return commandAction;
}
public void setCommandAction(Map<String,Object> commandAction) {
this.commandAction = commandAction;
}

@Nullable
public Map<String, Object> getCommandResult() {
return commandResult;
}
public void setCommandResult(Map<String,Object> commandResult) {
this.commandResult = commandResult;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandDetails that = (CommandDetails) o;
return Objects.equals(commandOrderId, that.commandOrderId)
&& Objects.equals(commandRequestId, that.commandRequestId)
&& Objects.equals(commandSource, that.commandSource)
&& Objects.equals(commandTarget, that.commandTarget)
&& Objects.equals(commandTimeout, that.commandTimeout)
&& Objects.equals(commandType, that.commandType)
&& Objects.equals(commandUser, that.commandUser)
&& Objects.equals(commandAction, that.commandAction)
&& Objects.equals(commandResult, that.commandResult);
}

@Override
public int hashCode() {
return Objects.hash(
commandOrderId,
commandRequestId,
commandSource,
commandTarget,
commandTimeout,
commandType,
commandUser,
commandAction,
commandResult
);
}

@Override
public String toString() {
return "CommandDetails{"
+ "commandOrderId='"
+ commandOrderId
+ '\''
+ ", commandRequestId='"
+ commandRequestId
+ '\''
+ ", commandSource='"
+ commandSource
+ '\''
+ ", commandTarget='"
+ commandTarget
+ '\''
+ ", commandTimeout='"
+ commandTimeout
+ '\''
+ ", commandType='"
+ commandType
+ '\''
+ ", commandUser='"
+ commandUser
+ '\''
+ ", commandAction='"
+ commandAction
+ '\''
+ ", commandResult='"
+ commandResult
+ '\''
+ '}';
}
}
Loading