From cfc1e8182d0e02bcaa0ef17d3dc10c1a592be881 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 25 Jan 2024 11:46:41 -0800 Subject: [PATCH 1/7] Pass context with timeout to FQDN lookup --- libbeat/cmd/instance/beat.go | 5 ++++- libbeat/processors/add_host_metadata/add_host_metadata.go | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index efe8bd48f79a..05f738b459ee 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -824,7 +824,10 @@ func (b *Beat) configure(settings Settings) error { return fmt.Errorf("failed to get host information: %w", err) } - fqdn, err := h.FQDN() + fqdnLookupCtx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + + fqdn, err := h.FQDN(fqdnLookupCtx) if err != nil { // FQDN lookup is "best effort". We log the error, fallback to // the OS-reported hostname, and move on. diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index db3cbbc5ee30..1f8e2500b91e 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -18,6 +18,7 @@ package add_host_metadata import ( + "context" "fmt" "sync" "time" @@ -178,7 +179,10 @@ func (p *addHostMetadata) loadData(checkCache bool, useFQDN bool) error { hostname := h.Info().Hostname if useFQDN { - fqdn, err := h.FQDN() + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + + fqdn, err := h.FQDN(ctx) if err != nil { // FQDN lookup is "best effort". If it fails, we monitor the failure, fallback to // the OS-reported hostname, and move on. From 209f013c6b1270ee0bbfe3e7248c9350a389a169 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 25 Jan 2024 12:03:46 -0800 Subject: [PATCH 2/7] Add temporary gomod replace --- go.mod | 3 +++ go.sum | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e39b37f445eb..2ebce0436da3 100644 --- a/go.mod +++ b/go.mod @@ -417,5 +417,8 @@ replace ( k8s.io/kubernetes v1.13.0 => k8s.io/kubernetes v1.24.15 ) +// TODO: remove once https://github.com/elastic/go-sysinfo/pull/199 and new version of library is released. +replace github.com/elastic/go-sysinfo => github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0 + // Exclude this version because the version has an invalid checksum. exclude github.com/docker/distribution v2.8.0+incompatible diff --git a/go.sum b/go.sum index e11edad3d976..227bd96f4c32 100644 --- a/go.sum +++ b/go.sum @@ -692,8 +692,6 @@ github.com/elastic/go-seccomp-bpf v1.4.0 h1:6y3lYrEHrLH9QzUgOiK8WDqmPaMnnB785Wxi github.com/elastic/go-seccomp-bpf v1.4.0/go.mod h1:wIMxjTbKpWGQk4CV9WltlG6haB4brjSH/dvAohBPM1I= github.com/elastic/go-structform v0.0.10 h1:oy08o/Ih2hHTkNcRY/1HhaYvIp5z6t8si8gnCJPDo1w= github.com/elastic/go-structform v0.0.10/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4= -github.com/elastic/go-sysinfo v1.11.2 h1:mcm4OSYVMyws6+n2HIVMGkln5HOpo5Ie1ZmbbNn0jg4= -github.com/elastic/go-sysinfo v1.11.2/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ= github.com/elastic/go-ucfg v0.8.6 h1:stUeyh2goTgGX+/wb9gzKvTv0YB0231LTpKUgCKj4U0= github.com/elastic/go-ucfg v0.8.6/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA= github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0= @@ -1896,6 +1894,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0 h1:W3pYycLTytbzwv5z4ernYl4hQdLhtZc/EfvgLGpvxNQ= +github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= From b145464323124213ccd10f9dc84b6d856975b790 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 25 Jan 2024 12:07:51 -0800 Subject: [PATCH 3/7] Add CHANGELOG entry --- CHANGELOG.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 8d09997f774d..b208e9849b47 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -62,6 +62,7 @@ you can achieve this by overwriting the value using an `add_fields` processor. { - Upgrade elastic-agent-libs to v0.7.5. Removes obsolete "Treating the CommonName field on X.509 certificates as a host name..." deprecation warning for 8.0. {pull}37755[37755] - aws: Add credential caching for `AssumeRole` session tokens. {issue}37787[37787] - Lower logging level to debug when attempting to configure beats with unknown fields from autodiscovered events/environments {pull}[37816][37816] +- Set timeout of 1 minute for FQDN requests {pull}37756[37756] *Auditbeat* From decda55b2fe7a662c19b59e3673c3c8d619ebbb5 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 31 Jan 2024 18:09:37 -0800 Subject: [PATCH 4/7] Update dependency --- NOTICE.txt | 4 ++-- go.mod | 5 +---- go.sum | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/NOTICE.txt b/NOTICE.txt index 7e0e27d091c7..9b238edfa0cb 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -14955,11 +14955,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-structform@v -------------------------------------------------------------------------------- Dependency : github.com/elastic/go-sysinfo -Version: v1.11.2 +Version: v1.12.0 Licence type (autodetected): Apache-2.0 -------------------------------------------------------------------------------- -Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.11.2/LICENSE.txt: +Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.12.0/LICENSE.txt: Apache License diff --git a/go.mod b/go.mod index 2ebce0436da3..eac16e9393f4 100644 --- a/go.mod +++ b/go.mod @@ -78,7 +78,7 @@ require ( github.com/elastic/go-perf v0.0.0-20191212140718-9c656876f595 github.com/elastic/go-seccomp-bpf v1.4.0 github.com/elastic/go-structform v0.0.10 - github.com/elastic/go-sysinfo v1.11.2 + github.com/elastic/go-sysinfo v1.12.0 github.com/elastic/go-ucfg v0.8.6 github.com/elastic/gosigar v0.14.2 github.com/fatih/color v1.15.0 @@ -417,8 +417,5 @@ replace ( k8s.io/kubernetes v1.13.0 => k8s.io/kubernetes v1.24.15 ) -// TODO: remove once https://github.com/elastic/go-sysinfo/pull/199 and new version of library is released. -replace github.com/elastic/go-sysinfo => github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0 - // Exclude this version because the version has an invalid checksum. exclude github.com/docker/distribution v2.8.0+incompatible diff --git a/go.sum b/go.sum index 227bd96f4c32..92d0580fd685 100644 --- a/go.sum +++ b/go.sum @@ -692,6 +692,8 @@ github.com/elastic/go-seccomp-bpf v1.4.0 h1:6y3lYrEHrLH9QzUgOiK8WDqmPaMnnB785Wxi github.com/elastic/go-seccomp-bpf v1.4.0/go.mod h1:wIMxjTbKpWGQk4CV9WltlG6haB4brjSH/dvAohBPM1I= github.com/elastic/go-structform v0.0.10 h1:oy08o/Ih2hHTkNcRY/1HhaYvIp5z6t8si8gnCJPDo1w= github.com/elastic/go-structform v0.0.10/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4= +github.com/elastic/go-sysinfo v1.12.0 h1:ZKyB4N5XLnGFysNGNnJl8xvd+GBGCe2MemBykR+3yQI= +github.com/elastic/go-sysinfo v1.12.0/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ= github.com/elastic/go-ucfg v0.8.6 h1:stUeyh2goTgGX+/wb9gzKvTv0YB0231LTpKUgCKj4U0= github.com/elastic/go-ucfg v0.8.6/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA= github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0= @@ -1894,8 +1896,6 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0 h1:W3pYycLTytbzwv5z4ernYl4hQdLhtZc/EfvgLGpvxNQ= -github.com/ycombinator/go-sysinfo v0.0.0-20240125195429-01b75c6787f0/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= From e5d69f23fe539341b4e979301fbc122d2ce095c9 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 31 Jan 2024 18:34:16 -0800 Subject: [PATCH 5/7] Update method calls --- libbeat/cmd/instance/beat.go | 2 +- libbeat/processors/add_host_metadata/add_host_metadata.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 05f738b459ee..88777d06a737 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -827,7 +827,7 @@ func (b *Beat) configure(settings Settings) error { fqdnLookupCtx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() - fqdn, err := h.FQDN(fqdnLookupCtx) + fqdn, err := h.FQDNWithContext(fqdnLookupCtx) if err != nil { // FQDN lookup is "best effort". We log the error, fallback to // the OS-reported hostname, and move on. diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index 1f8e2500b91e..3a383059fbe4 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -26,6 +26,7 @@ import ( "github.com/gofrs/uuid" "github.com/elastic/elastic-agent-libs/monitoring" + "github.com/elastic/go-sysinfo" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/features" @@ -36,7 +37,6 @@ import ( "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/elastic-agent-system-metrics/metric/system/host" - "github.com/elastic/go-sysinfo" ) const processorName = "add_host_metadata" @@ -182,7 +182,7 @@ func (p *addHostMetadata) loadData(checkCache bool, useFQDN bool) error { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() - fqdn, err := h.FQDN(ctx) + fqdn, err := h.FQDNWithContext(ctx) if err != nil { // FQDN lookup is "best effort". If it fails, we monitor the failure, fallback to // the OS-reported hostname, and move on. From 8855ce8cedda7fe4fadd526a108fc04f7fa8c6b2 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 2 Feb 2024 05:06:01 -0800 Subject: [PATCH 6/7] Add nolint for rand deprecation warning --- libbeat/cmd/instance/beat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 88777d06a737..7a70b1a55baa 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -197,7 +197,7 @@ func initRand() { } else { seed = n.Int64() } - rand.Seed(seed) + rand.Seed(seed) //nolint:staticcheck // need seed from cryptographically strong PRNG. } // Run initializes and runs a Beater implementation. name is the name of the From 9f3b2eeb642fbb228b0b530baa0eeed1e8f3c57d Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 2 Feb 2024 05:20:39 -0800 Subject: [PATCH 7/7] Make linter happy --- libbeat/processors/add_host_metadata/add_host_metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index 3a383059fbe4..5fe28194b555 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -97,7 +97,7 @@ func New(cfg *config.C) (beat.Processor, error) { } // create a unique ID for this instance of the processor - cbIDStr := "" + var cbIDStr string cbID, err := uuid.NewV4() // if we fail, fall back to the processor name, hope for the best. if err != nil {