Skip to content

Commit

Permalink
chore: use driverkit builder.Type as SupportedDistros map keys.
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 38d44a9 commit 745c66b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 151 deletions.
5 changes: 3 additions & 2 deletions cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func NewGenerateCmd() *cobra.Command {
Long: `
In auto mode, configs will be generated starting from kernel-crawler output.
In this scenario, target-{distro,kernelrelease,kernelversion} are available to filter to-be-generated configs. Regexes are allowed.
Instead, when auto mode is not enabled, the tool is able to generate a single config (for each driver version).
Moreover, you can pass special value "load" as target-distro to make the tool automatically fetch latest distro kernel-crawler ran against.
Instead, when auto mode is disabled, the tool is able to generate a single config (for each driver version).
In this scenario, target-{distro,kernelrelease,kernelversion} CANNOT be regexes but must be exact values.
Also, in non-automatic mode, kernelurls driverkit config key will be constructed using driverkit logic.
Also, in non-automatic mode, kernelurls driverkit config key will be constructed using driverkit libraries.
`,
RunE: execute,
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log/slog"
"os"
"runtime"
"strings"
)

var (
Expand Down Expand Up @@ -64,6 +65,7 @@ func init() {
if err != nil {
log.Fatal(err)
}

flags := rootCmd.PersistentFlags()
flags.Bool("dry-run", false, "enable dry-run mode.")
flags.StringP("log-level", "l", slog.LevelInfo.String(), "set log verbosity.")
Expand All @@ -75,16 +77,13 @@ func init() {
flags.String("target-kernelversion", "",
`target kernel version to work against. By default tool will work on any kernel version. Can be a regex.`)
flags.String("target-distro", "",
`target distro to work against. By default tool will work on any supported distro. Can be a regex.`)
`target distro to work against. By default tool will work on any supported distro. Can be a regex.
Supported distros: [`+strings.Join(root.SupportedDistroSlice, ",")+"].")

// Custom completions
rootCmd.RegisterFlagCompletionFunc("target-distro", func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
supportedDistrosSlice := make([]string, 0)
for distro, _ := range root.SupportedDistros {
supportedDistrosSlice = append(supportedDistrosSlice, string(distro))
}
return supportedDistrosSlice, cobra.ShellCompDirectiveDefault
return root.SupportedDistroSlice, cobra.ShellCompDirectiveDefault
})
// Custom completions
rootCmd.RegisterFlagCompletionFunc("architecture", func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return utils.SupportedArchList(), cobra.ShellCompDirectiveDefault
})
Expand Down
12 changes: 3 additions & 9 deletions pkg/cleanup/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ func TestCleanupFiltered(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "Ubun*",
KernelRelease: "",
KernelVersion: "",
Distro: "ubun*",
},
}},
expectedOutputContains: []string{"ubuntu_5.15", "ubuntu_5.19"},
Expand All @@ -132,8 +130,6 @@ func TestCleanupFiltered(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "",
KernelRelease: "",
KernelVersion: "24",
},
}},
Expand All @@ -146,9 +142,7 @@ func TestCleanupFiltered(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "",
KernelRelease: "6.0.0",
KernelVersion: "",
},
}},
expectedOutputContains: []string{"amazonlinux_6.0", "talos_6.0"},
Expand Down Expand Up @@ -230,7 +224,7 @@ func TestCleanupS3(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "Debian",
Distro: "debian",
},
}},
remainingObjects: []string{
Expand All @@ -246,7 +240,7 @@ func TestCleanupS3(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "AmazonLin*",
Distro: "amazonlin*",
},
}},
remainingObjects: []string{
Expand Down
95 changes: 55 additions & 40 deletions pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,75 @@ 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 {
url := fmt.Sprintf(urlArchFmt, opts.Architecture)
slog.Debug("downloading json data", "url", url)

// Fetch kernel list json
var (
jsonData []byte
err error
)

// In case testJsonData is set,
if testJsonData != nil {
jsonData = testJsonData
} else {
jsonData, err = utils.GetURL(url)
}
if err != nil {
return err
}
slog.Debug("fetched json")
if testCacheData {
testJsonData = jsonData
}

if opts.Distro == "" {
// Fetch last distro kernel-crawler ran against
opts.Distro, err = loadLastRunDistro()
if err != nil {
return err
}
slog.Debug("loaded last-distro")
}
return autogenerateConfigs(opts, jsonData)
return autogenerateConfigs(opts)
} else if opts.IsSet() {
return generateSingleConfig(opts)
}
return fmt.Errorf(`either "auto" or target-{distro,kernelrelease,kernelversion} must be passed`)
}

func autogenerateConfigs(opts Options, jsonData []byte) error {
// This is the only function where opts.Distro gets overridden using KernelCrawler namings
func autogenerateConfigs(opts Options) error {
url := fmt.Sprintf(urlArchFmt, opts.Architecture)
slog.Debug("downloading json data", "url", url)

// Fetch kernel list json
var (
jsonData []byte
err error
)

// In case testJsonData is set,
if testJsonData != nil {
jsonData = testJsonData
} else {
jsonData, err = utils.GetURL(url)
}
if err != nil {
return err
}
slog.Debug("fetched json")
if testCacheData {
testJsonData = jsonData
}

// either download latest distro from kernel-crawler
// 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()
if err != nil {
return err
}
slog.Debug("loaded last-distro")
} else {
opts.Distro, err = toKernelCrawlerDistro(opts.Options)
if err != nil {
return err
}
}

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 distro, _ := range root.SupportedDistros {
distroStr := string(distro)
for _, kcDistro := range root.SupportedDistros {
distroStr := string(kcDistro)
if opts.DistroFilter(distroStr) {
tag := fmt.Sprintf(`json:"%s"`, distroStr)
instanceBuilder.AddField(distroStr, []validate.KernelEntry{}, tag)
Expand All @@ -98,7 +117,7 @@ func autogenerateConfigs(opts Options, jsonData []byte) error {
dynamicInstance := instanceBuilder.Build().New()

// Unmarshal the big json
err := json.Unmarshal(jsonData, &dynamicInstance)
err = json.Unmarshal(jsonData, &dynamicInstance)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,10 +200,6 @@ func loadKernelHeadersFromDk(opts Options) ([]string, error) {
}

func generateSingleConfig(opts Options) error {
// Translate opts.Distro to a driverkit distro
kDistro := root.KernelCrawlerDistro(opts.Distro)
opts.Distro = kDistro.ToDriverkitDistro().String()

kernelheaders, err := loadKernelHeadersFromDk(opts)
if err != nil {
var unsupportedTargetError *unsupportedTargetErr
Expand Down
34 changes: 8 additions & 26 deletions pkg/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver", "2.0.0+driver"},
Target: root.Target{
Distro: "", // Should load it from lastDistro kernel crawler file
KernelRelease: "",
KernelVersion: "",
Distro: "load", // Should load it from lastDistro kernel crawler file
},
},
DriverName: "falco",
Expand All @@ -61,11 +59,6 @@ func TestGenerate(t *testing.T) {
RepoRoot: "./test/",
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "^.*$", // Avoid loading from lastDistro kernel-crawler file, instead force-set any distro
KernelRelease: "",
KernelVersion: "",
},
},
DriverName: "falco",
Auto: true,
Expand All @@ -79,9 +72,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "CentOS",
KernelRelease: "",
KernelVersion: "",
Distro: "centos",
},
},
DriverName: "falco",
Expand All @@ -96,9 +87,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "WRONG_DISTRO",
KernelRelease: "",
KernelVersion: "",
Distro: "WRONG_DISTRO",
},
},
DriverName: "falco",
Expand All @@ -113,9 +102,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "CentOS",
KernelRelease: "",
KernelVersion: "",
Distro: "centos",
},
},
DriverName: "CUSTOM",
Expand All @@ -130,9 +117,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "^Cent.*$",
KernelRelease: "",
KernelVersion: "",
Distro: "^cent.*$",
},
},
DriverName: "CUSTOM",
Expand All @@ -147,8 +132,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "CentOS",
KernelRelease: "",
Distro: "centos",
KernelVersion: "1",
},
},
Expand All @@ -163,9 +147,8 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "CentOS",
Distro: "centos",
KernelRelease: "5.10.0",
KernelVersion: "",
},
},
DriverName: "falco",
Expand All @@ -179,7 +162,6 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "",
KernelRelease: "5.10.0",
KernelVersion: "1",
},
Expand Down Expand Up @@ -213,7 +195,7 @@ func TestGenerate(t *testing.T) {
Architecture: "x86_64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "Debian",
Distro: "debian",
KernelRelease: "6.1.38-2-amd64",
KernelVersion: "1",
},
Expand Down
45 changes: 22 additions & 23 deletions pkg/root/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@ package root

import (
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
"strings"
"sort"
)

type KernelCrawlerDistro string

var (
SupportedDistroSlice []string
// SupportedDistros keeps the list of distros supported by test-infra.
// We don't want to generate configs for unsupported distros after all.
// Please add new supported build-new-drivers structures here,
// so that the utility starts building configs for them.
// Keys must have the same name used in kernel-crawler json keys.
// Values must have the same name used by driverkit targets.
SupportedDistros = map[KernelCrawlerDistro]builder.Type{
"AlmaLinux": "almalinux",
"AmazonLinux": "amazonlinux",
"AmazonLinux2": "amazonlinux2",
"AmazonLinux2022": "amazonlinux2022",
"AmazonLinux2023": "amazonlinux2023",
"BottleRocket": "bottlerocket",
"CentOS": "centos",
"Debian": "debian",
"Fedora": "fedora",
"Minikube": "minikube",
"Talos": "talos",
"Ubuntu": "ubuntu",
// Keys must have the same name used by driverkit targets.
// Values must have the same name used by kernel-crawler json keys.
SupportedDistros = map[builder.Type]KernelCrawlerDistro{
builder.TargetTypeAlma: "AlmaLinux",
builder.TargetTypeAmazonLinux: "AmazonLinux",
builder.TargetTypeAmazonLinux2: "AmazonLinux2",
builder.TargetTypeAmazonLinux2022: "AmazonLinux2022",
builder.TargetTypeAmazonLinux2023: "AmazonLinux2023",
builder.TargetTypeBottlerocket: "BottleRocket",
builder.TargetTypeCentos: "CentOS",
builder.TargetTypeDebian: "Debian",
builder.TargetTypeFedora: "Fedora",
builder.TargetTypeMinikube: "Minikube",
builder.TargetTypeTalos: "Talos",
builder.TargetTypeUbuntu: "Ubuntu",
}
)

func (kDistro KernelCrawlerDistro) ToDriverkitDistro() builder.Type {
dkDistro, found := SupportedDistros[kDistro]
if found {
return dkDistro
} else {
// Perhaps a regex? ToLower and pray
return builder.Type(strings.ToLower(string(kDistro)))
func init() {
SupportedDistroSlice = make([]string, 0)
for distro, _ := range SupportedDistros {
SupportedDistroSlice = append(SupportedDistroSlice, string(distro))
}
sort.Strings(SupportedDistroSlice)
}
Loading

0 comments on commit 745c66b

Please sign in to comment.