Skip to content

Commit

Permalink
fix: support multiple ES filter term in dpluger (#159)
Browse files Browse the repository at this point in the history
* fix: support multiple ES filter terms in dpluger

* fix: add note about multiple terms in generated cf
  • Loading branch information
mmta authored and mergify[bot] committed Jul 15, 2019
1 parent 0d88652 commit 1f7adce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/dpluger/dpluger.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func CreateConfig(confFile, address, index, name, typ string) error {
plugin.IdentifierField = getStaticText("LOGSTASH_IDENTIFYING_FIELD") + " (example: [application] or [fields][log_type] etc)"
plugin.IdentifierValue = getStaticText("IDENTIFYING_FIELD_VALUE") + " (example: suricata)"
plugin.IdentifierFilter = getStaticText("ADDITIONAL_FILTER") + " (example: and [alert])"
plugin.ESCollectionFilter = getStaticText("ES_TERM_FILTER") + " (example: type=http will only collect SIDs from documents whose type field is http)"
plugin.ESCollectionFilter = getStaticText("ES_TERM_FILTER") + " (example: type=http will only collect SIDs from documents whose type field is http). Separate multiple term with ; character"
plugin.Fields.Timestamp = defMappingText
plugin.Fields.TimestampFormat = getStaticText("TIMESTAMP_FORMAT") + " (example: ISO8601)"
plugin.Fields.Title = defMappingText
Expand Down
17 changes: 10 additions & 7 deletions internal/pkg/dpluger/es5client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ func (es *es5Client) Collect(plugin Plugin, confFile, sidSource, esFilter string
size := 1000
c.init(plugin.Name, confFile)
terms := elastic5.NewTermsAggregation().Field(sidSource).Size(size)
var query elastic5.Query
query := elastic5.NewBoolQuery()
if esFilter != "" {
s := strings.Split(esFilter, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
coll := strings.Split(esFilter, ";")
for _, v := range coll {
s := strings.Split(v, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
}
query = query.Must(elastic5.NewTermQuery(s[0], s[1]))
}
query = elastic5.NewTermsQuery(s[0], s[1])
} else {
query = elastic5.NewMatchAllQuery()
query = query.Must(elastic5.NewMatchAllQuery())
}

ctx := context.Background()
Expand Down
17 changes: 10 additions & 7 deletions internal/pkg/dpluger/es6client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ func (es *es6Client) Collect(plugin Plugin, confFile, sidSource, esFilter string
size := 1000
c.init(plugin.Name, confFile)
terms := elastic6.NewTermsAggregation().Field(sidSource).Size(size)
var query elastic6.Query
query := elastic6.NewBoolQuery()
if esFilter != "" {
s := strings.Split(esFilter, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
coll := strings.Split(esFilter, ";")
for _, v := range coll {
s := strings.Split(v, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
}
query = query.Must(elastic6.NewTermQuery(s[0], s[1]))
}
query = elastic6.NewTermsQuery(s[0], s[1])
} else {
query = elastic6.NewMatchAllQuery()
query = query.Must(elastic6.NewMatchAllQuery())
}

ctx := context.Background()
Expand Down
17 changes: 10 additions & 7 deletions internal/pkg/dpluger/es7client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,19 @@ func (es *es7Client) Collect(plugin Plugin, confFile, sidSource, esFilter string
size := 1000
c.init(plugin.Name, confFile)
terms := elastic7.NewTermsAggregation().Field(sidSource).Size(size)
var query elastic7.Query
query := elastic7.NewBoolQuery()
if esFilter != "" {
s := strings.Split(esFilter, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
coll := strings.Split(esFilter, ";")
for _, v := range coll {
s := strings.Split(v, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
return
}
query = query.Must(elastic7.NewTermQuery(s[0], s[1]))
}
query = elastic7.NewTermsQuery(s[0], s[1])
} else {
query = elastic7.NewMatchAllQuery()
query = query.Must(elastic7.NewMatchAllQuery())
}

ctx := context.Background()
Expand Down

0 comments on commit 1f7adce

Please sign in to comment.