Skip to content

Commit

Permalink
Convert pkg/util/winutil to module (#20851)
Browse files Browse the repository at this point in the history
* Convert pkg/util/winutil to module

* create pkg/secrets module

* Fix compiliation issues

* go tidy

* fix gofmt

* remove un-needed files

* add doc file

* fix lint issues

* Add deleted files back

* deleted un-needed

* PR feedback update codeowners

* fix typo

* apply suggestion for doc.go

* Fix revive error

* Fix compilation error

* Fix revive error that I missed

* Update test to handle new mod

* fix lint + test run

* remove pkg/secrets

---------

Co-authored-by: Julien Lebot <[email protected]>
  • Loading branch information
dineshg13 and julien-lebot authored Nov 29, 2023
1 parent 03728b0 commit 2017a14
Show file tree
Hide file tree
Showing 33 changed files with 137 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@
/pkg/util/cgroups/ @DataDog/container-integrations
/pkg/util/retry/ @DataDog/container-integrations
/pkg/util/intern/ @DataDog/ebpf-platform
/pkg/util/crashreport/ @DataDog/windows-kernel-integrations
/pkg/util/pdhutil/ @DataDog/windows-agent
/pkg/util/winutil/ @DataDog/windows-agent
/pkg/languagedetection @DataDog/processes @DataDog/universal-service-monitoring
/pkg/logs/ @DataDog/agent-metrics-logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,23 @@ import (
"fmt"
"strings"

"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"go.uber.org/fx"
"golang.org/x/sys/windows/registry"
yaml "gopkg.in/yaml.v2"

"github.com/DataDog/datadog-agent/comp/checks/agentcrashdetect"
compsysconfig "github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
comptraceconfig "github.com/DataDog/datadog-agent/comp/trace/config"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"

"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/system/wincrashdetect/probe"

"github.com/DataDog/datadog-agent/comp/checks/agentcrashdetect"
compsysconfig "github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
comptraceconfig "github.com/DataDog/datadog-agent/comp/trace/config"

"github.com/DataDog/datadog-agent/pkg/internaltelemetry"
traceconfig "github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/util/crashreport"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/winutil/crashreport"
"golang.org/x/sys/windows/registry"
yaml "gopkg.in/yaml.v2"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion comp/checks/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/DataDog/datadog-agent/comp/core"
comptraceconfig "github.com/DataDog/datadog-agent/comp/trace/config"
"github.com/DataDog/datadog-agent/pkg/util/crashreport"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil/crashreport"
"go.uber.org/fx"
)

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ replace (
github.com/DataDog/datadog-agent/pkg/util/statstracker => ./pkg/util/statstracker
github.com/DataDog/datadog-agent/pkg/util/system/socket => ./pkg/util/system/socket/
github.com/DataDog/datadog-agent/pkg/util/testutil => ./pkg/util/testutil
github.com/DataDog/datadog-agent/pkg/util/winutil => ./pkg/util/winutil/
github.com/DataDog/datadog-agent/pkg/version => ./pkg/version
)

Expand Down Expand Up @@ -611,6 +612,7 @@ require (
github.com/DataDog/datadog-agent/pkg/util/sort v0.50.0-rc.4
github.com/DataDog/datadog-agent/pkg/util/statstracker v0.50.0-rc.4
github.com/DataDog/datadog-agent/pkg/util/testutil v0.50.0-rc.4
github.com/DataDog/datadog-agent/pkg/util/winutil v0.0.0-00010101000000-000000000000
github.com/DataDog/datadog-agent/pkg/version v0.50.0-rc.4
github.com/DataDog/go-libddwaf/v2 v2.2.0
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs v0.8.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/system/cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

const cpuCheckName = "cpu"
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/system/cpu/cpu_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
gohaicpu "github.com/DataDog/datadog-agent/pkg/gohai/cpu"
gohaiutils "github.com/DataDog/datadog-agent/pkg/gohai/utils"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

func CPUInfo() *gohaicpu.Info {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"

"golang.org/x/sys/windows"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

func testGetDriveType(drive string) uintptr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

const fileHandlesCheckName = "file_handle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

func TestFhCheckWindows(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/system/memory/memory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"

core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
)

// For testing purpose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
)

func VirtualMemory() (*winutil.VirtualMemoryStat, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ package wincrashdetect
import (
"fmt"

"golang.org/x/sys/windows/registry"
yaml "gopkg.in/yaml.v2"

"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/system/wincrashdetect/probe"
"github.com/DataDog/datadog-agent/pkg/metrics/event"
"github.com/DataDog/datadog-agent/pkg/util/crashreport"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/winutil/crashreport"
"golang.org/x/sys/windows/registry"
yaml "gopkg.in/yaml.v2"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/system/winproc/winproc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

const winprocCheckName = "winproc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
pdhtest "github.com/DataDog/datadog-agent/pkg/util/pdhutil"
)

func TestWinprocCheckWindows(t *testing.T) {
Expand Down
58 changes: 46 additions & 12 deletions pkg/network/protocols/http/etw_http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ package http
import (
"encoding/binary"
"errors"
"fmt"
"net/netip"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -329,8 +331,8 @@ func completeReqRespTracking(eventInfo *etw.DDEtwEventInfo, httpConnLink *HttpCo
log.Infof(" ConnActivityId: %v\n", etw.FormatGUID(httpConnLink.connActivityId))
log.Infof(" ActivityId: %v\n", etw.FormatGUID(eventInfo.Event.ActivityId))
if connFound {
log.Infof(" Local: %v\n", etw.IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", etw.IPFormat(connOpen.conn.tup, false))
log.Infof(" Local: %v\n", IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", IPFormat(connOpen.conn.tup, false))
log.Infof(" Family: %v\n", connOpen.conn.tup.Family)
}
log.Infof(" AppPool: %v\n", httpConnLink.http.AppPool)
Expand All @@ -345,8 +347,8 @@ func completeReqRespTracking(eventInfo *etw.DDEtwEventInfo, httpConnLink *HttpCo
log.Infof("%v. %v L[%v], R[%v], F[%v], P[%v], C[%v], V[%v], U[%v]\n",
completedRequestCount,
etw.FormatUnixTime(httpConnLink.http.Txn.RequestStarted),
etw.IPFormat(connOpen.conn.tup, true),
etw.IPFormat(connOpen.conn.tup, false),
IPFormat(connOpen.conn.tup, true),
IPFormat(connOpen.conn.tup, false),
connOpen.conn.tup.Family,
httpConnLink.http.AppPool,
httpConnLink.http.Txn.ResponseStatusCode,
Expand Down Expand Up @@ -504,8 +506,8 @@ func httpCallbackOnHTTPConnectionTraceTaskConnConn(eventInfo *etw.DDEtwEventInfo
if HttpServiceLogVerbosity == HttpServiceLogVeryVerbose {
log.Infof(" Time: %v\n", etw.FormatUnixTime(connOpen.conn.connected))
log.Infof(" ActivityId: %v\n", etw.FormatGUID(eventInfo.Event.ActivityId))
log.Infof(" Local: %v\n", etw.IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", etw.IPFormat(connOpen.conn.tup, false))
log.Infof(" Local: %v\n", IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", IPFormat(connOpen.conn.tup, false))
log.Infof(" Family: %v\n", connOpen.conn.tup.Family)
log.Infof("\n")
}
Expand Down Expand Up @@ -542,8 +544,8 @@ func httpCallbackOnHTTPConnectionTraceTaskConnClose(eventInfo *etw.DDEtwEventInf
if found {
log.Infof(" ActivityId: %v, Local[%v], Remote[%v], Family[%v]\n",
etw.FormatGUID(eventInfo.Event.ActivityId),
etw.IPFormat(connOpen.conn.tup, true),
etw.IPFormat(connOpen.conn.tup, false),
IPFormat(connOpen.conn.tup, true),
IPFormat(connOpen.conn.tup, false),
connOpen.conn.tup.Family)
} else {
log.Infof(" ActivityId: %v not found\n", etw.FormatGUID(eventInfo.Event.ActivityId))
Expand Down Expand Up @@ -634,8 +636,8 @@ func httpCallbackOnHTTPRequestTraceTaskRecvReq(eventInfo *etw.DDEtwEventInfo) {
log.Infof(" ActivityId: %v\n", etw.FormatGUID(eventInfo.Event.ActivityId))
log.Infof(" RelActivityId: %v\n", etw.FormatGUID(*eventInfo.RelatedActivityId))
if connFound {
log.Infof(" Local: %v\n", etw.IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", etw.IPFormat(connOpen.conn.tup, false))
log.Infof(" Local: %v\n", IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", IPFormat(connOpen.conn.tup, false))
log.Infof(" Family: %v\n", connOpen.conn.tup.Family)
}
log.Infof("\n")
Expand Down Expand Up @@ -804,8 +806,8 @@ func httpCallbackOnHTTPRequestTraceTaskDeliver(eventInfo *etw.DDEtwEventInfo) {
log.Infof(" AppPool: %v\n", httpConnLink.http.AppPool)
log.Infof(" Url: %v\n", httpConnLink.url)
if connFound {
log.Infof(" Local: %v\n", etw.IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", etw.IPFormat(connOpen.conn.tup, false))
log.Infof(" Local: %v\n", IPFormat(connOpen.conn.tup, true))
log.Infof(" Remote: %v\n", IPFormat(connOpen.conn.tup, false))
log.Infof(" Family: %v\n", connOpen.conn.tup.Family)
}
log.Infof("\n")
Expand Down Expand Up @@ -1345,3 +1347,35 @@ func (hei *EtwInterface) OnStop() {
iisConfig = nil
}
}
func ipAndPortFromTup(tup driver.ConnTupleType, local bool) ([16]uint8, uint16) {
if local {
return tup.LocalAddr, tup.LocalPort
}
return tup.RemoteAddr, tup.RemotePort
}

func ip4format(ip [16]uint8) string {
ipObj := netip.AddrFrom4(*(*[4]byte)(ip[:4]))
return ipObj.String()
}

func ip6format(ip [16]uint8) string {
ipObj := netip.AddrFrom16(ip)
return ipObj.String()
}

// IPFormat takes a binary ip representation and returns a string type
func IPFormat(tup driver.ConnTupleType, local bool) string {
ip, port := ipAndPortFromTup(tup, local)

if tup.Family == 2 {
// IPv4
return fmt.Sprintf("%v:%v", ip4format(ip), port)
} else if tup.Family == 23 {
// IPv6
return fmt.Sprintf("[%v]:%v", ip6format(ip), port)
} else {
// everything else
return "<UNKNOWN>"
}
}
2 changes: 1 addition & 1 deletion pkg/process/procutil/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"golang.org/x/sys/windows"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/pdhutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
"github.com/DataDog/datadog-agent/pkg/util/winutil/pdhutil"
)

var (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func refreshPdhObjectCache(forceRefresh bool) (didrefresh bool, err error) {
// log errors and warnings but will decrease the time before the failing
// counters have a chance to initialize successfully.

//revive:disable:redefines-builtin-id
var len uint32
//revive:enable:redefines-builtin-id

refreshInterval := config.Datadog.GetInt("windows_counter_refresh_interval")
if refreshInterval == 0 {
Expand Down Expand Up @@ -191,7 +193,9 @@ func pdhMakeCounterPath(machine string, object string, instance string, counter
if counter != "" {
elems.countername = uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(counter)))
}
//revive:disable:redefines-builtin-id
var len uint32
//revive:enable:redefines-builtin-id
r, _, _ := procPdhMakeCounterPath.Call(
uintptr(unsafe.Pointer(&elems)),
uintptr(0),
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions pkg/util/winutil/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018-present Datadog, Inc.
//go:build !windows

// Package winutil - Windows utility functions
package winutil
36 changes: 0 additions & 36 deletions pkg/util/winutil/etw/etw-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ package etw
import (
"bytes"
"fmt"
"net/netip"
"reflect"
"strconv"
"time"
"unsafe"

"golang.org/x/sys/windows"

"github.com/DataDog/datadog-agent/pkg/network/driver"
)

/*
Expand Down Expand Up @@ -294,36 +291,3 @@ func skipASCIIString(data []byte, offset int) (nextOffset int, valFound bool, fo

return (offset + termZeroIdx + 1), true, (offset + termZeroIdx + 1)
}

func ip4format(ip [16]uint8) string {
ipObj := netip.AddrFrom4(*(*[4]byte)(ip[:4]))
return ipObj.String()
}

func ip6format(ip [16]uint8) string {
ipObj := netip.AddrFrom16(ip)
return ipObj.String()
}

func ipAndPortFromTup(tup driver.ConnTupleType, local bool) ([16]uint8, uint16) {
if local {
return tup.LocalAddr, tup.LocalPort
}
return tup.RemoteAddr, tup.RemotePort
}

// IPFormat takes a binary ip representation and returns a string type
func IPFormat(tup driver.ConnTupleType, local bool) string {
ip, port := ipAndPortFromTup(tup, local)

if tup.Family == 2 {
// IPv4
return fmt.Sprintf("%v:%v", ip4format(ip), port)
} else if tup.Family == 23 {
// IPv6
return fmt.Sprintf("[%v]:%v", ip6format(ip), port)
} else {
// everything else
return "<UNKNOWN>"
}
}
Loading

0 comments on commit 2017a14

Please sign in to comment.