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

[Backport 2.12] Feature findings enhancemnt #1497

Open
wants to merge 1 commit into
base: 2.12
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 @@ -45,6 +45,8 @@ class RestGetFindingsAction : BaseRestHandler() {
val size = request.paramAsInt("size", 20)
val startIndex = request.paramAsInt("startIndex", 0)
val searchString = request.param("searchString", "")
val severity: String? = request.param("severity", "ALL")
val detectionType: String? = request.param("detectionType", "rules")

val table = Table(
sortOrder,
Expand All @@ -57,7 +59,9 @@ class RestGetFindingsAction : BaseRestHandler() {

val getFindingsSearchRequest = GetFindingsRequest(
findingID,
table
table,
severity,
detectionType
)
return RestChannelConsumer {
channel ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class TransportGetFindingsSearchAction @Inject constructor(
val getFindingsRequest = request as? GetFindingsRequest
?: recreateObject(request) { GetFindingsRequest(it) }
val tableProp = getFindingsRequest.table
val severity = getFindingsRequest.severity
val detectionType = getFindingsRequest.detectionType
val searchString = tableProp.searchString

val sortBuilder = SortBuilders
.fieldSort(tableProp.sortString)
Expand All @@ -106,12 +109,74 @@ class TransportGetFindingsSearchAction @Inject constructor(
queryBuilder.filter(QueryBuilders.termQuery("_id", getFindingsRequest.findingId))
}

if (!getFindingsRequest.findingIds.isNullOrEmpty()) {
queryBuilder.filter(QueryBuilders.termsQuery("id", getFindingsRequest.findingIds))
}

if (getFindingsRequest.monitorId != null) {
queryBuilder.filter(QueryBuilders.termQuery("monitor_id", getFindingsRequest.monitorId))
} else if (getFindingsRequest.monitorIds.isNullOrEmpty() == false) {
queryBuilder.filter(QueryBuilders.termsQuery("monitor_id", getFindingsRequest.monitorIds))
}

if (getFindingsRequest.startTime != null && getFindingsRequest.endTime != null) {
val startTime = getFindingsRequest.startTime!!.toEpochMilli()
val endTime = getFindingsRequest.endTime!!.toEpochMilli()
val timeRangeQuery = QueryBuilders.rangeQuery("timestamp")
.from(startTime) // Greater than or equal to start time
.to(endTime) // Less than or equal to end time
queryBuilder.filter(timeRangeQuery)
}

if (!detectionType.isNullOrBlank()) {
val nestedQueryBuilder = QueryBuilders.nestedQuery(
"queries",
when {
detectionType.equals("threat", ignoreCase = true) -> {
QueryBuilders.boolQuery().filter(
QueryBuilders.prefixQuery("queries.id", "threat_intel_")
)
}
else -> {
QueryBuilders.boolQuery().mustNot(
QueryBuilders.prefixQuery("queries.id", "threat_intel_")
)
}
},
ScoreMode.None
)

// Add the nestedQueryBuilder to the main queryBuilder
queryBuilder.must(nestedQueryBuilder)
}

if (!searchString.isNullOrBlank()) {
queryBuilder
.should(QueryBuilders.matchQuery("index", searchString))
.should(
QueryBuilders.nestedQuery(
"queries",
QueryBuilders.matchQuery("queries.tags", searchString),
ScoreMode.None
)
)
.should(QueryBuilders.regexpQuery("monitor_name", searchString + ".*"))
.minimumShouldMatch(1)
}

if (!severity.isNullOrBlank()) {
queryBuilder
.must(
QueryBuilders.nestedQuery(
"queries",
QueryBuilders.boolQuery().should(
QueryBuilders.matchQuery("queries.tags", severity)
),
ScoreMode.None
)
)
}

if (!tableProp.searchString.isNullOrBlank()) {
queryBuilder
.should(
Expand All @@ -133,7 +198,6 @@ class TransportGetFindingsSearchAction @Inject constructor(
)
)
}

searchSourceBuilder.query(queryBuilder)

client.threadPool().threadContext.stashContext().use {
Expand Down
Loading