Skip to content

Commit c605fa0

Browse files
fix detector writeTo() method missing fields (#695) (#699)
* fix detector writeTo() method missing fields Signed-off-by: Surya Sashank Nistala <[email protected]> * fix test Signed-off-by: Surya Sashank Nistala <[email protected]> --------- Signed-off-by: Surya Sashank Nistala <[email protected]> (cherry picked from commit 7a45203) Co-authored-by: Surya Sashank Nistala <[email protected]>
1 parent 1f3b093 commit c605fa0

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/main/java/org/opensearch/securityanalytics/model/Detector.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ public Detector(StreamInput sin) throws IOException {
166166
sin.readList(DetectorInput::readFrom),
167167
sin.readList(DetectorTrigger::readFrom),
168168
sin.readStringList(),
169-
sin.readString(),
170-
sin.readString(),
171-
sin.readString(),
172-
sin.readString(),
173-
sin.readString(),
174-
sin.readString(),
169+
sin.readOptionalString(),
170+
sin.readOptionalString(),
171+
sin.readOptionalString(),
172+
sin.readOptionalString(),
173+
sin.readOptionalString(),
174+
sin.readOptionalString(),
175175
sin.readMap(StreamInput::readString, StreamInput::readString),
176176
sin.readStringList(),
177177
sin.readBoolean()
@@ -206,8 +206,12 @@ public void writeTo(StreamOutput out) throws IOException {
206206
it.writeTo(out);
207207
}
208208
out.writeStringCollection(monitorIds);
209-
out.writeString(ruleIndex);
210-
209+
out.writeOptionalString(ruleIndex);
210+
out.writeOptionalString(alertsIndex);
211+
out.writeOptionalString(alertsHistoryIndex);
212+
out.writeOptionalString(alertsHistoryIndexPattern);
213+
out.writeOptionalString(findingsIndex);
214+
out.writeOptionalString(findingsIndexPattern);
211215
out.writeMap(ruleIdMonitorIdMap, StreamOutput::writeString, StreamOutput::writeString);
212216

213217
if (workflowIds != null) {

src/test/java/org/opensearch/securityanalytics/model/WriteableTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,42 @@
1313
import java.io.IOException;
1414
import java.util.List;
1515

16+
import static org.opensearch.securityanalytics.TestHelpers.parser;
1617
import static org.opensearch.securityanalytics.TestHelpers.randomDetector;
1718
import static org.opensearch.securityanalytics.TestHelpers.randomUser;
1819
import static org.opensearch.securityanalytics.TestHelpers.randomUserEmpty;
20+
import static org.opensearch.securityanalytics.TestHelpers.toJsonStringWithUser;
1921

2022
public class WriteableTests extends OpenSearchTestCase {
2123

2224
public void testDetectorAsAStream() throws IOException {
2325
Detector detector = randomDetector(List.of());
2426
detector.setInputs(List.of(new DetectorInput("", List.of(), List.of(), List.of())));
27+
logger.error(toJsonStringWithUser(detector));
28+
BytesStreamOutput out = new BytesStreamOutput();
29+
detector.writeTo(out);
30+
StreamInput sin = StreamInput.wrap(out.bytes().toBytesRef().bytes);
31+
Detector newDetector = new Detector(sin);
32+
Assert.assertEquals("Round tripping Detector doesn't work", detector, newDetector);
33+
}
34+
35+
public void testDetector() throws IOException { // an edge case of detector serialization that failed testDetectorAsAStream() intermittently
36+
String detectorString = "{\"type\":\"detector\",\"name\":\"MczAuRCrve\",\"detector_type\":\"test_windows\"," +
37+
"\"user\":{\"name\":\"QhKrfthgxw\",\"backend_roles\":[\"uYvGLCPhfX\",\"fOLkcRxMWR\"],\"roles\"" +
38+
":[\"YuucNpVzTm\",\"all_access\"],\"custom_attribute_names\":[\"test_attr=test\"]," +
39+
"\"user_requested_tenant\":null},\"threat_intel_enabled\":false,\"enabled\":false,\"enabled_time\"" +
40+
":null,\"schedule\":{\"period\":{\"interval\":5,\"unit\":\"MINUTES\"}},\"inputs\":[{\"detector_input\"" +
41+
":{\"description\":\"\",\"indices\":[],\"custom_rules\":[],\"pre_packaged_rules\":[]}}],\"triggers\"" +
42+
":[{\"id\":\"SiWfaosBBiNA8if0E1bC\",\"name\":\"windows-trigger\",\"severity\":\"1\",\"types\"" +
43+
":[\"test_windows\"],\"ids\":[\"QuarksPwDump Clearing Access History\"],\"sev_levels\":[\"high\"]," +
44+
"\"tags\":[\"T0008\"],\"actions\":[],\"detection_types\":[\"rules\"]}],\"last_update_time\":" +
45+
"1698300892093,\"monitor_id\":[\"\"],\"workflow_ids\":[],\"bucket_monitor_id_rule_id\"" +
46+
":{},\"rule_topic_index\":\"\",\"alert_index\":\"\",\"alert_history_index\":\"\"," +
47+
"\"alert_history_index_pattern\":\"\",\"findings_index\":\"\",\"findings_index_pattern\":\"\"}";
48+
Detector detector = Detector.parse(parser(detectorString), null, null);
49+
// Detector detector = randomDetector(List.of());
50+
// detector.setInputs(List.of(new DetectorInput("", List.of(), List.of(), List.of())));
51+
// logger.error(toJsonStringWithUser(detector));
2552
BytesStreamOutput out = new BytesStreamOutput();
2653
detector.writeTo(out);
2754
StreamInput sin = StreamInput.wrap(out.bytes().toBytesRef().bytes);

0 commit comments

Comments
 (0)