Skip to content

Commit

Permalink
fix(pkg): fixed generate. Dropped dynamic builder.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Aug 31, 2023
1 parent 745c66b commit fbf6b12
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/falcosecurity/driverkit v0.14.1-0.20230828134718-835307efe091
github.com/johannesboyne/gofakes3 v0.0.0-20230506070712-04da935ef877
github.com/olekukonko/tablewriter v0.0.4
github.com/ompluscator/dynamic-struct v1.4.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/ompluscator/dynamic-struct v1.4.0 h1:I/Si9LZtItSwiTMe7vosEuIu2TKdOvWbE3R/lokpN4Q=
github.com/ompluscator/dynamic-struct v1.4.0/go.mod h1:ADQ1+6Ox1D+ntuNwTHyl1NvpAqY2lBXPSPbcO4CJdeA=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7XWJGmnM7r3wg034=
Expand Down
77 changes: 31 additions & 46 deletions pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/fededp/dbg-go/pkg/root"
"github.com/fededp/dbg-go/pkg/utils"
"github.com/fededp/dbg-go/pkg/validate"
dynamicstruct "github.com/ompluscator/dynamic-struct"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
"log/slog"
Expand All @@ -36,16 +35,6 @@ func loadLastRunDistro() (string, error) {
return lastDistro, nil
}

func toKernelCrawlerDistro(opts root.Options) (string, error) {
kcDistro, found := root.SupportedDistros[builder.Type(opts.Distro)]
if found {
return string(kcDistro), nil
}
// either distro is empty (all!), a non-existent distro was passed, or a regex was passed.
// Try to go on.
return opts.Distro, nil
}

func Run(opts Options) error {
if opts.Auto {
return autogenerateConfigs(opts)
Expand All @@ -71,10 +60,11 @@ func autogenerateConfigs(opts Options) error {
jsonData = testJsonData
} else {
jsonData, err = utils.GetURL(url)
if err != nil {
return err
}
}
if err != nil {
return err
}

slog.Debug("fetched json")
if testCacheData {
testJsonData = jsonData
Expand All @@ -84,65 +74,60 @@ func autogenerateConfigs(opts Options) error {
// or translate the driverkit distro provided by the user to its kernel-crawler naming
if opts.Distro == "load" {
// Fetch last distro kernel-crawler ran against
opts.Distro, err = loadLastRunDistro()
lastDistro, err := loadLastRunDistro()
if err != nil {
return err
}
slog.Debug("loaded last-distro")
} else {
opts.Distro, err = toKernelCrawlerDistro(opts.Options)
if err != nil {
return err
slog.Debug("loaded last-distro", "distro", lastDistro)

// Map back the kernel crawler distro to our internal driverkit distro
opts.Distro = root.ToDriverkitDistro(root.KernelCrawlerDistro(lastDistro)).String()
if opts.Distro == "" {
return fmt.Errorf("kernel-crawler last run distro '%s' unsupported.\n", lastDistro)
}
}

slog.SetDefault(slog.With("target-distro", opts.Distro))

// Generate a dynamic struct with all needed distros
// NOTE: we might need a single distro when `lastDistro` is != "*";
// else, we will add all SupportedDistros found in constants.go
distroCtr := 0
instanceBuilder := dynamicstruct.NewStruct()
for _, kcDistro := range root.SupportedDistros {
distroStr := string(kcDistro)
if opts.DistroFilter(distroStr) {
tag := fmt.Sprintf(`json:"%s"`, distroStr)
instanceBuilder.AddField(distroStr, []validate.KernelEntry{}, tag)
distroCtr++
}
}
if distroCtr == 0 {
return fmt.Errorf("unsupported target distro: %s. Must be one of: %v", opts.Distro, root.SupportedDistros)
}
dynamicInstance := instanceBuilder.Build().New()
fullJson := map[string][]validate.KernelEntry{}

// Unmarshal the big json
err = json.Unmarshal(jsonData, &dynamicInstance)
err = json.Unmarshal(jsonData, &fullJson)
if err != nil {
return err
}
slog.Debug("unmarshaled json")
var errGrp errgroup.Group

reader := dynamicstruct.NewReader(dynamicInstance)
for _, f := range reader.GetAllFields() {
slog.Info("generating configs", "distro", f.Name())
if opts.DryRun {
slog.Info("skipping because of dry-run.")
for kcDistro, f := range fullJson {
kernelEntries := f

dkDistro := root.ToDriverkitDistro(root.KernelCrawlerDistro(kcDistro))

// Skip unneeded kernel entries
// optimization for target-distro: skip entire key
// instead of skipping objects one by one.
if !opts.DistroFilter(dkDistro.String()) {
continue
}
kernelEntries := f.Interface().([]validate.KernelEntry)

// A goroutine for each distro
errGrp.Go(func() error {
for _, kernelEntry := range kernelEntries {
// Skip unneeded kernel entries
if !opts.KernelVersionFilter(kernelEntry.KernelRelease) {
continue
}
if !opts.KernelVersionFilter(kernelEntry.KernelVersion) {
continue
}

slog.Info("generating configs",
"target", kernelEntry.Target,
"kernelrelease", kernelEntry.KernelRelease,
"kernelversion", kernelEntry.KernelVersion)
if opts.DryRun {
slog.Info("skipping because of dry-run.")
continue
}
if pvtErr := dumpConfig(opts, kernelEntry); pvtErr != nil {
return pvtErr
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/root/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ func init() {
}
sort.Strings(SupportedDistroSlice)
}

func ToDriverkitDistro(distro KernelCrawlerDistro) builder.Type {
for key, val := range SupportedDistros {
if val == distro {
return key
}
}
return builder.Type("")
}
6 changes: 6 additions & 0 deletions pkg/root/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
"fmt"
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
"github.com/spf13/viper"
"log/slog"
"regexp"
Expand Down Expand Up @@ -33,6 +34,11 @@ func (t Target) ToGlob() string {

func (t Target) DistroFilter(distro string) bool {
matched, _ := regexp.MatchString(t.Distro, distro)
// check if key is actually supported
if matched {
_, ok := SupportedDistros[builder.Type(distro)]
return ok
}
return matched
}

Expand Down

0 comments on commit fbf6b12

Please sign in to comment.