Skip to content

Commit

Permalink
fix golangci-lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkroh committed Aug 22, 2023
1 parent d88d401 commit 4598a67
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
2 changes: 2 additions & 0 deletions libbeat/processors/dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func (res *MiekgResolver) getOrCreateNameserverStats(ns string) *nameserverStats
failure: monitoring.NewInt(reg, "failure"),
requestDuration: metrics.NewUniformSample(1028),
}

//nolint:errcheck // Register should never fail because this is a new empty registry.
adapter.NewGoMetrics(reg, "request_duration", adapter.Accept).
Register("histogram", metrics.NewHistogram(stats.requestDuration))
res.nsStats[ns] = stats
Expand Down
65 changes: 48 additions & 17 deletions libbeat/processors/dns/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ package dns

import (
"crypto/tls"
"errors"
"net"
"strings"
"testing"

"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/monitoring"
)

var _ resolver = (*MiekgResolver)(nil)

func TestMiekgResolverLookupPTR(t *testing.T) {
stop, addr, err := ServeDNS(FakeDNSHandler)
stop, addr, err := serveDNS(fakeDNSHandler)
if err != nil {
t.Fatal(err)
}
defer stop()
defer func() {
require.NoError(t, stop())
}()

reg := monitoring.NewRegistry()
res, err := NewMiekgResolver(reg.NewRegistry(logName), 0, "udp", addr)
Expand Down Expand Up @@ -71,31 +75,34 @@ func TestMiekgResolverLookupPTR(t *testing.T) {

func TestMiekgResolverLookupPTRTLS(t *testing.T) {
// Build Cert
cert, err := tls.X509KeyPair(CertPEMBlock, KeyPEMBlock)
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
t.Fatalf("unable to build certificate: %v", err)
}
config := tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS13,
}
// serve TLS with cert
stop, addr, err := ServeDNSTLS(FakeDNSHandler, &config)
stop, addr, err := serveDNSTLS(fakeDNSHandler, &config)
if err != nil {
t.Fatal(err)
}
defer stop()
defer func() {
require.NoError(t, stop())
}()

reg := monitoring.NewRegistry()

res, err := NewMiekgResolver(reg.NewRegistry(logName), 0, "tls", addr)
if err != nil {
t.Fatal(err)
}
// we use a self-signed certificate for localhost
// we have to pass InsecureSSL to the DNS resolver
//nolint:gosec // Don't verify the self-signed cert. This is only for testing purposes.
res.client.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}

// Success
ptr, err := res.Lookup("8.8.8.8", typePTR)
if err != nil {
Expand All @@ -121,7 +128,7 @@ func TestMiekgResolverLookupPTRTLS(t *testing.T) {
assert.Equal(t, 12, metricCount)
}

func ServeDNS(h dns.HandlerFunc) (cancel func() error, addr string, err error) {
func serveDNS(h dns.HandlerFunc) (cancel func() error, addr string, err error) {
// Setup listener on ephemeral port.

a, err := net.ResolveUDPAddr("udp4", "localhost:0")
Expand All @@ -136,11 +143,23 @@ func ServeDNS(h dns.HandlerFunc) (cancel func() error, addr string, err error) {
var s dns.Server
s.PacketConn = l
s.Handler = h
go s.ActivateAndServe()
return s.Shutdown, s.PacketConn.LocalAddr().String(), err

serveErr := make(chan error, 1)
go func() {
defer close(serveErr)
serveErr <- s.ActivateAndServe()
}()

cancel = func() error {
return errors.Join(
s.Shutdown(),
<-serveErr,
)
}
return cancel, s.PacketConn.LocalAddr().String(), err
}

func ServeDNSTLS(h dns.HandlerFunc, config *tls.Config) (cancel func() error, addr string, err error) {
func serveDNSTLS(h dns.HandlerFunc, config *tls.Config) (cancel func() error, addr string, err error) {
// Setup listener on ephemeral port.
l, err := tls.Listen("tcp", "localhost:0", config)
if err != nil {
Expand All @@ -150,11 +169,23 @@ func ServeDNSTLS(h dns.HandlerFunc, config *tls.Config) (cancel func() error, ad
var s dns.Server
s.Handler = h
s.Listener = l
go s.ActivateAndServe()
return s.Shutdown, l.Addr().String(), err

serveErr := make(chan error, 1)
go func() {
defer close(serveErr)
serveErr <- s.ActivateAndServe()
}()

cancel = func() error {
return errors.Join(
s.Shutdown(),
<-serveErr,
)
}
return cancel, l.Addr().String(), err
}

func FakeDNSHandler(w dns.ResponseWriter, msg *dns.Msg) {
func fakeDNSHandler(w dns.ResponseWriter, msg *dns.Msg) {
m := new(dns.Msg)
m.SetReply(msg)
switch {
Expand All @@ -164,11 +195,11 @@ func FakeDNSHandler(w dns.ResponseWriter, msg *dns.Msg) {
default:
m.SetRcode(msg, dns.RcodeNameError)
}
w.WriteMsg(m)
_ = w.WriteMsg(m)
}

var (
KeyPEMBlock = []byte(`-----BEGIN RSA PRIVATE KEY-----
keyPEMBlock = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA2g2zpEtWaIUx5o6MEnWnGsf0Ba1SDc3AwgOmxeNIPBJYVCrk
sWe8Qt/5nymReVFcum76995ncr/zT+e4e8l+hXuGzTKZJpOj27Igb0/wa3j2hIcu
rnbzfwkJ+KMag2UUKdSo31ChMU+64bwziEXunF347Ot7dBLtw3PJKbabNCP+/oil
Expand Down Expand Up @@ -196,7 +227,7 @@ LatVl7h6ud25ZJYnP7DelGxHsZnDXNirLFlSB0CL4F6I5xNoBvCoH0Q8ckDSh4C7
tlAyD5m9gwvgdkNFWq6/lcUPxGksTtTk8dGnhJz8pGlZvp6+dZCM
-----END RSA PRIVATE KEY-----`)

CertPEMBlock = []byte(`-----BEGIN CERTIFICATE-----
certPEMBlock = []byte(`-----BEGIN CERTIFICATE-----
MIIDaTCCAlGgAwIBAgIQGqg47wLgbjwwrZASuakmwjANBgkqhkiG9w0BAQsFADAy
MRQwEgYDVQQKEwtMb2cgQ291cmllcjEaMBgGA1UEAxMRYmVhdHMuZWxhc3RpYy5j
b20wHhcNMjAwNjIzMDY0NDEwWhcNMjEwNjIzMDY0NDEwWjAyMRQwEgYDVQQKEwtM
Expand Down

0 comments on commit 4598a67

Please sign in to comment.