Skip to content

Commit 89d1ece

Browse files
ycombinatorScholar-Li
authored andcommitted
Pass context with timeout to FQDN lookup (elastic#37756)
* Pass context with timeout to FQDN lookup * Add temporary gomod replace * Add CHANGELOG entry * Update dependency * Update method calls * Add nolint for rand deprecation warning * Make linter happy
1 parent 4dbf0bb commit 89d1ece

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ you can achieve this by overwriting the value using an `add_fields` processor. {
6262
- 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]
6363
- aws: Add credential caching for `AssumeRole` session tokens. {issue}37787[37787]
6464
- Lower logging level to debug when attempting to configure beats with unknown fields from autodiscovered events/environments {pull}[37816][37816]
65+
- Set timeout of 1 minute for FQDN requests {pull}37756[37756]
6566

6667
*Auditbeat*
6768

NOTICE.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14981,11 +14981,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-structform@v
1498114981

1498214982
--------------------------------------------------------------------------------
1498314983
Dependency : github.com/elastic/go-sysinfo
14984-
Version: v1.11.2
14984+
Version: v1.12.0
1498514985
Licence type (autodetected): Apache-2.0
1498614986
--------------------------------------------------------------------------------
1498714987

14988-
Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.11.2/LICENSE.txt:
14988+
Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.12.0/LICENSE.txt:
1498914989

1499014990

1499114991
Apache License

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ require (
7878
github.com/elastic/go-perf v0.0.0-20191212140718-9c656876f595
7979
github.com/elastic/go-seccomp-bpf v1.4.0
8080
github.com/elastic/go-structform v0.0.10
81-
github.com/elastic/go-sysinfo v1.11.2
81+
github.com/elastic/go-sysinfo v1.12.0
8282
github.com/elastic/go-ucfg v0.8.6
8383
github.com/elastic/gosigar v0.14.2
8484
github.com/fatih/color v1.15.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,8 @@ github.com/elastic/go-seccomp-bpf v1.4.0 h1:6y3lYrEHrLH9QzUgOiK8WDqmPaMnnB785Wxi
696696
github.com/elastic/go-seccomp-bpf v1.4.0/go.mod h1:wIMxjTbKpWGQk4CV9WltlG6haB4brjSH/dvAohBPM1I=
697697
github.com/elastic/go-structform v0.0.10 h1:oy08o/Ih2hHTkNcRY/1HhaYvIp5z6t8si8gnCJPDo1w=
698698
github.com/elastic/go-structform v0.0.10/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4=
699-
github.com/elastic/go-sysinfo v1.11.2 h1:mcm4OSYVMyws6+n2HIVMGkln5HOpo5Ie1ZmbbNn0jg4=
700-
github.com/elastic/go-sysinfo v1.11.2/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ=
699+
github.com/elastic/go-sysinfo v1.12.0 h1:ZKyB4N5XLnGFysNGNnJl8xvd+GBGCe2MemBykR+3yQI=
700+
github.com/elastic/go-sysinfo v1.12.0/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ=
701701
github.com/elastic/go-ucfg v0.8.6 h1:stUeyh2goTgGX+/wb9gzKvTv0YB0231LTpKUgCKj4U0=
702702
github.com/elastic/go-ucfg v0.8.6/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA=
703703
github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0=

libbeat/cmd/instance/beat.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func initRand() {
197197
} else {
198198
seed = n.Int64()
199199
}
200-
rand.Seed(seed)
200+
rand.Seed(seed) //nolint:staticcheck // need seed from cryptographically strong PRNG.
201201
}
202202

203203
// Run initializes and runs a Beater implementation. name is the name of the
@@ -824,7 +824,10 @@ func (b *Beat) configure(settings Settings) error {
824824
return fmt.Errorf("failed to get host information: %w", err)
825825
}
826826

827-
fqdn, err := h.FQDN()
827+
fqdnLookupCtx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
828+
defer cancel()
829+
830+
fqdn, err := h.FQDNWithContext(fqdnLookupCtx)
828831
if err != nil {
829832
// FQDN lookup is "best effort". We log the error, fallback to
830833
// the OS-reported hostname, and move on.

libbeat/processors/add_host_metadata/add_host_metadata.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
package add_host_metadata
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"sync"
2324
"time"
2425

2526
"github.com/gofrs/uuid"
2627

2728
"github.com/elastic/elastic-agent-libs/monitoring"
29+
"github.com/elastic/go-sysinfo"
2830

2931
"github.com/elastic/beats/v7/libbeat/beat"
3032
"github.com/elastic/beats/v7/libbeat/features"
@@ -35,7 +37,6 @@ import (
3537
"github.com/elastic/elastic-agent-libs/logp"
3638
"github.com/elastic/elastic-agent-libs/mapstr"
3739
"github.com/elastic/elastic-agent-system-metrics/metric/system/host"
38-
"github.com/elastic/go-sysinfo"
3940
)
4041

4142
const processorName = "add_host_metadata"
@@ -96,7 +97,7 @@ func New(cfg *config.C) (beat.Processor, error) {
9697
}
9798

9899
// create a unique ID for this instance of the processor
99-
cbIDStr := ""
100+
var cbIDStr string
100101
cbID, err := uuid.NewV4()
101102
// if we fail, fall back to the processor name, hope for the best.
102103
if err != nil {
@@ -178,7 +179,10 @@ func (p *addHostMetadata) loadData(checkCache bool, useFQDN bool) error {
178179

179180
hostname := h.Info().Hostname
180181
if useFQDN {
181-
fqdn, err := h.FQDN()
182+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
183+
defer cancel()
184+
185+
fqdn, err := h.FQDNWithContext(ctx)
182186
if err != nil {
183187
// FQDN lookup is "best effort". If it fails, we monitor the failure, fallback to
184188
// the OS-reported hostname, and move on.

0 commit comments

Comments
 (0)