From 7ae21ca1387c85a7a108e61ce427633fc5f0dff9 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Fri, 19 Oct 2018 12:30:54 +0200 Subject: [PATCH] Fix a race condition on add_host_metadata --- CHANGELOG.asciidoc | 2 +- .../add_host_metadata/add_host_metadata.go | 44 +++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 3ce970461f0..b7d00c0416e 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -45,7 +45,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff] - Replace index patterns in TSVB visualizations. {pull}7929[7929] - Fixed Support `add_docker_metadata` in Windows by identifying systems' path separator. {issue}7797[7797] - Add backoff support to x-pack monitoring outputs. {issue}7966[7966] -- Fix a race condition with the `add_host_metadata` and the event serialization. {pull}8223[8223] +- Fix a race condition with the `add_host_metadata` and the event serialization. {pull}8653[8653] - Enforce that data used by k8s or docker doesn't use any reference. {pull}8240[8240] - Switch to different UUID lib due to to non-random generated UUIDs. {pull}8485[8485] diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index f9597cb79f2..17834f0dcdf 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -20,6 +20,7 @@ package add_host_metadata import ( "fmt" "net" + "sync" "time" "github.com/joeshaw/multierror" @@ -39,6 +40,8 @@ func init() { } type addHostMetadata struct { + sync.Mutex + info types.HostInfo lastUpdate time.Time data common.MapStr @@ -64,38 +67,43 @@ func newHostMetadataProcessor(cfg *common.Config) (processors.Processor, error) info: h.Info(), config: config, } + p.loadData() return p, nil } // Run enriches the given event with the host meta data func (p *addHostMetadata) Run(event *beat.Event) (*beat.Event, error) { + p.Lock() + defer p.Unlock() + p.loadData() - event.Fields.DeepUpdate(p.data.Clone()) + event.Fields.DeepUpdate(p.data) return event, nil } func (p *addHostMetadata) loadData() { - // Check if cache is expired - if p.lastUpdate.Add(cacheExpiration).Before(time.Now()) { - p.data = host.MapHostInfo(p.info) - - if p.config.NetInfoEnabled { - // IP-address and MAC-address - var ipList, hwList, err = p.getNetInfo() - if err != nil { - logp.Info("Error when getting network information %v", err) - } + if p.lastUpdate.Add(cacheExpiration).After(time.Now()) { + return + } - if len(ipList) > 0 { - p.data.Put("host.ip", ipList) - } - if len(hwList) > 0 { - p.data.Put("host.mac", hwList) - } + p.data = host.MapHostInfo(p.info) + + if p.config.NetInfoEnabled { + // IP-address and MAC-address + var ipList, hwList, err = p.getNetInfo() + if err != nil { + logp.Info("Error when getting network information %v", err) + } + + if len(ipList) > 0 { + p.data.Put("host.ip", ipList) + } + if len(hwList) > 0 { + p.data.Put("host.mac", hwList) } - p.lastUpdate = time.Now() } + p.lastUpdate = time.Now() } func (p addHostMetadata) getNetInfo() ([]string, []string, error) {