Skip to content

Commit

Permalink
fix btfhub sync tool
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Dec 20, 2024
1 parent 4734d11 commit 941995a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
27 changes: 3 additions & 24 deletions pkg/security/probe/constantfetch/btfhub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"runtime/pprof"
Expand All @@ -31,10 +30,8 @@ import (

"github.com/smira/go-xz"

"github.com/DataDog/datadog-agent/pkg/security/ebpf/kernel"
"github.com/DataDog/datadog-agent/pkg/security/probe"
"github.com/DataDog/datadog-agent/pkg/security/probe/constantfetch"
utilKernel "github.com/DataDog/datadog-agent/pkg/util/kernel"
)

func main() {
Expand Down Expand Up @@ -324,7 +321,7 @@ func (c *treeWalkCollector) extractor() error {
for query := range c.queryChan {
fmt.Println(query.counter, query.path)

constants, err := c.extractConstantsFromBTF(query.path, query.distribution, query.distribVersion)
constants, err := c.extractConstantsFromBTF(query.path)
if err != nil {
return fmt.Errorf("failed to extract constants from `%s`: %v", query.path, err)
}
Expand Down Expand Up @@ -370,7 +367,7 @@ func (c *treeWalkCollector) getCacheEntry(cacheKey string) (map[string]uint64, b
return val, ok
}

func (c *treeWalkCollector) extractConstantsFromBTF(archivePath, distribution, distribVersion string) (map[string]uint64, error) {
func (c *treeWalkCollector) extractConstantsFromBTF(archivePath string) (map[string]uint64, error) {
btfContent, err := createBTFReaderFromTarball(archivePath)
if err != nil {
return nil, err
Expand All @@ -381,31 +378,13 @@ func (c *treeWalkCollector) extractConstantsFromBTF(archivePath, distribution, d
return constants, nil
}

archiveFileName := path.Base(archivePath)
btfFileName := strings.TrimSuffix(archiveFileName, ".tar.xz")

releasePart := strings.Split(btfFileName, "-")[0]
kvCode, err := utilKernel.ParseReleaseString(releasePart)
if err != nil {
return nil, err
}

osRelease := map[string]string{
"ID": distribution,
"VERSION_ID": distribVersion,
}
kv := &kernel.Version{
Code: kvCode,
OsRelease: osRelease,
}

btfReader := bytes.NewReader(btfContent)
fetcher, err := constantfetch.NewBTFConstantFetcherFromReader(btfReader)
if err != nil {
return nil, err
}

probe.AppendProbeRequestsToFetcher(fetcher, kv)
probe.AppendProbeRequestsToFetcher(fetcher)
constants, err := fetcher.FinishAndGetResults()
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/security/tests/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func TestOctogonConstants(t *testing.T) {
})
}

func getFighterConstants(champion, challenger constantfetch.ConstantFetcher, kv *kernel.Version) (map[string]uint64, map[string]uint64, error) {
championConstants, err := getOffsetConstantsFromFetcher(champion, kv)
func getFighterConstants(champion, challenger constantfetch.ConstantFetcher) (map[string]uint64, map[string]uint64, error) {
championConstants, err := getOffsetConstantsFromFetcher(champion)
if err != nil {
return nil, nil, err
}

challengerConstants, err := getOffsetConstantsFromFetcher(challenger, kv)
challengerConstants, err := getOffsetConstantsFromFetcher(challenger)
if err != nil {
return nil, nil, err
}
Expand All @@ -86,7 +86,7 @@ func getFighterConstants(champion, challenger constantfetch.ConstantFetcher, kv

func assertConstantsEqual(t *testing.T, champion, challenger constantfetch.ConstantFetcher, kv *kernel.Version, ignoreMissing []string) {
t.Helper()
championConstants, challengerConstants, err := getFighterConstants(champion, challenger, kv)
championConstants, challengerConstants, err := getFighterConstants(champion, challenger)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func assertConstantsEqual(t *testing.T, champion, challenger constantfetch.Const
}
}

func getOffsetConstantsFromFetcher(cf constantfetch.ConstantFetcher, kv *kernel.Version) (map[string]uint64, error) {
probe.AppendProbeRequestsToFetcher(cf, kv)
func getOffsetConstantsFromFetcher(cf constantfetch.ConstantFetcher) (map[string]uint64, error) {
probe.AppendProbeRequestsToFetcher(cf)
return cf.FinishAndGetResults()
}

0 comments on commit 941995a

Please sign in to comment.