-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
command.action.args
field object-type (#209)
* Update command template * Add an Args model class to handle undetermined command.action.args types * Improve parsing logic to handle lists, objects and primitive values * Revert to a simpler model that looks for key-value pairs * Make setup plugin mapping dynamic * Update openapi.yml definition * Fix JavaDocs --------- Co-authored-by: Álex Ruiz <[email protected]>
- Loading branch information
Showing
5 changed files
with
100 additions
and
21 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
84 changes: 84 additions & 0 deletions
84
plugins/command-manager/src/main/java/com/wazuh/commandmanager/model/Args.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,84 @@ | ||
/* | ||
* Copyright (C) 2024, Wazuh Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.wazuh.commandmanager.model; | ||
|
||
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.HashMap; | ||
import java.util.Map; | ||
|
||
/** Handles the command.action.args object */ | ||
public class Args implements ToXContentObject { | ||
|
||
public static final String ARGS = "args"; | ||
private final Map<String, Object> args; | ||
|
||
/** | ||
* Constructor method | ||
* | ||
* @param args Initializes the args object | ||
*/ | ||
public Args(Map<String, Object> args) { | ||
this.args = args; | ||
} | ||
|
||
/** | ||
* Parses an args XContentParser into an Args object. A {@code Map<String,Object>} is created | ||
* with the fields and values from the command.action.args object | ||
* | ||
* @param parser An XContentParser containing an args to be deserialized | ||
* @return An Args object | ||
* @throws IOException Rethrows the exception from list() and objectText() methods | ||
*/ | ||
public static Args parse(XContentParser parser) throws IOException { | ||
Map<String, Object> args = new HashMap<>(); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
args.put(fieldName, parser.objectText()); | ||
} | ||
return new Args(args); | ||
} | ||
|
||
/** | ||
* Builds an Args XContentBuilder. Iterates over the args map adding key-value pairs | ||
* | ||
* @param builder This is received from the parent object | ||
* @param params Not used | ||
* @return A complete args XContentBuilder object | ||
* @throws IOException rethrown from XContentBuilder objects within | ||
*/ | ||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(Args.ARGS); | ||
for (String key : this.args.keySet()) { | ||
builder.field(key, this.args.get(key)); | ||
} | ||
return builder.endObject(); | ||
} | ||
|
||
/** | ||
* @return a String representation of the contents of the Args object | ||
*/ | ||
@Override | ||
public String toString() { | ||
return "Args{" + "args='" + args + '\'' + '}'; | ||
} | ||
} |
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