forked from opensearch-project/security-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule field validation during IndexRule Action (opensearch-project#80)
Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>
- Loading branch information
1 parent
44599c9
commit 640d96e
Showing
19 changed files
with
891 additions
and
15 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
19 changes: 19 additions & 0 deletions
19
src/main/java/org/opensearch/securityanalytics/action/ValidateRulesAction.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,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.action; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.support.master.AcknowledgedResponse; | ||
|
||
public class ValidateRulesAction extends ActionType<ValidateRulesResponse>{ | ||
|
||
public static final String NAME = "cluster:admin/opendistro/securityanalytics/rules/validate"; | ||
public static final ValidateRulesAction INSTANCE = new ValidateRulesAction(); | ||
|
||
|
||
public ValidateRulesAction() { | ||
super(NAME, ValidateRulesResponse::new); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/main/java/org/opensearch/securityanalytics/action/ValidateRulesRequest.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,136 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.action; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.xcontent.ToXContentObject; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.common.xcontent.XContentParser; | ||
import org.opensearch.common.xcontent.XContentParserUtils; | ||
|
||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
public class ValidateRulesRequest extends ActionRequest implements ToXContentObject { | ||
|
||
private static final Logger log = LogManager.getLogger(ValidateRulesRequest.class); | ||
|
||
public static final String INDEX_NAME_FIELD = "index_name"; | ||
public static final String RULES_FIELD = "rules"; | ||
|
||
String indexName; | ||
List<String> rules; | ||
|
||
public ValidateRulesRequest(String indexName, List<String> rules) { | ||
super(); | ||
this.indexName = indexName; | ||
this.rules = rules; | ||
} | ||
|
||
public ValidateRulesRequest(StreamInput sin) throws IOException { | ||
this( | ||
sin.readString(), | ||
sin.readStringList() | ||
); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException validationException = null; | ||
if (indexName == null || indexName.length() == 0) { | ||
validationException = addValidationError(String.format(Locale.getDefault(), "%s is missing", INDEX_NAME_FIELD), validationException); | ||
} | ||
if (rules == null || rules.size() == 0) { | ||
validationException = addValidationError(String.format(Locale.getDefault(), "%s are missing", RULES_FIELD), validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(indexName); | ||
out.writeStringCollection(rules); | ||
} | ||
|
||
public static ValidateRulesRequest parse(XContentParser xcp) throws IOException { | ||
String indexName = null; | ||
List<String> ruleIds = null; | ||
|
||
if (xcp.currentToken() == null) { | ||
xcp.nextToken(); | ||
} | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp); | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = xcp.currentName(); | ||
xcp.nextToken(); | ||
|
||
switch (fieldName) { | ||
case INDEX_NAME_FIELD: | ||
indexName = xcp.text(); | ||
break; | ||
case RULES_FIELD: | ||
ruleIds = new ArrayList<>(); | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp); | ||
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { | ||
ruleIds.add(xcp.text()); | ||
} | ||
break; | ||
default: | ||
xcp.skipChildren(); | ||
} | ||
} | ||
return new ValidateRulesRequest(indexName, ruleIds); | ||
} | ||
|
||
public ValidateRulesRequest indexName(String indexName) { | ||
this.indexName = indexName; | ||
return this; | ||
} | ||
|
||
public ValidateRulesRequest rules(List<String> rules) { | ||
this.rules = rules; | ||
return this; | ||
} | ||
|
||
|
||
|
||
public String getIndexName() { | ||
return this.indexName; | ||
} | ||
|
||
public List<String> getRules() { | ||
return this.rules; | ||
} | ||
|
||
public void setIndexName(String indexName) { | ||
this.indexName = indexName; | ||
} | ||
|
||
public List<String> setRules(List<String> rules) { | ||
return this.rules = rules; | ||
} | ||
|
||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return builder.startObject() | ||
.field(INDEX_NAME_FIELD, indexName) | ||
.field(RULES_FIELD, rules) | ||
.endObject(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/org/opensearch/securityanalytics/action/ValidateRulesResponse.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,89 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.action; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.opensearch.action.ActionResponse; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.xcontent.ToXContentObject; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.securityanalytics.mapper.MapperUtils; | ||
|
||
public class ValidateRulesResponse extends ActionResponse implements ToXContentObject { | ||
|
||
public static final String NONAPPLICABLE_FIELDS = "nonapplicable_fields"; | ||
|
||
List<String> nonapplicableFields; | ||
|
||
public ValidateRulesResponse(List<String> nonapplicableFields) { | ||
this.nonapplicableFields = nonapplicableFields; | ||
} | ||
|
||
public ValidateRulesResponse(StreamInput in) throws IOException { | ||
super(in); | ||
nonapplicableFields = in.readStringList(); | ||
int size = in.readVInt(); | ||
if (size > 0) { | ||
nonapplicableFields = new ArrayList<>(size); | ||
for (int i = 0; i < size; i++) { | ||
nonapplicableFields.add(in.readString()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
if (nonapplicableFields != null) { | ||
out.writeVInt(nonapplicableFields.size()); | ||
for (String f : nonapplicableFields) { | ||
out.writeString(f); | ||
} | ||
} else { | ||
out.writeVInt(0); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (nonapplicableFields != null && nonapplicableFields.size() > 0) { | ||
builder.field(NONAPPLICABLE_FIELDS, nonapplicableFields); | ||
} | ||
return builder.endObject(); | ||
} | ||
|
||
public List<String> getNonapplicableFields() { | ||
return nonapplicableFields; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(new Object[]{this.nonapplicableFields}); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
ValidateRulesResponse other = (ValidateRulesResponse) obj; | ||
return this.nonapplicableFields.equals(other.nonapplicableFields); | ||
} | ||
} |
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
Oops, something went wrong.