Skip to content

Commit

Permalink
fix(cmd,pkg,validate): src-dir is not a mandatory parameter for `loca…
Browse files Browse the repository at this point in the history
…l` cmd.

Moreover, properly fill CmakeCmd for local target too.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed Feb 28, 2024
1 parent 2d58a7c commit 784f575
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 77 deletions.
42 changes: 10 additions & 32 deletions cmd/local.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cmd

import (
"fmt"
"github.com/falcosecurity/driverkit/pkg/driverbuilder"
"github.com/falcosecurity/driverkit/validate"
"github.com/go-playground/validator/v10"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -15,22 +12,9 @@ import (
)

type localCmdOptions struct {
UseDKMS bool
SrcDir string `validate:"required,abs_dirpath" name:"src-dir"`
EnvMap map[string]string
}

func (l *localCmdOptions) validate() []error {
if err := validate.V.Struct(l); err != nil {
errors := err.(validator.ValidationErrors)
errArr := []error{}
for _, e := range errors {
// Translate each error one at a time
errArr = append(errArr, fmt.Errorf(e.Translate(validate.T)))
}
return errArr
}
return nil
useDKMS bool
srcDir string
envMap map[string]string
}

// NewLocalCmd creates the `driverkit local` command.
Expand All @@ -39,15 +23,15 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F
localCmd := &cobra.Command{
Use: "local",
Short: "Build Falco kernel modules and eBPF probes in local env with local kernel sources and gcc/clang.",
PersistentPreRunE: persistentPreRunFunc(rootCommand, rootOpts, &opts),
PersistentPreRunE: persistentPreRunFunc(rootCommand, rootOpts),
Run: func(c *cobra.Command, args []string) {
slog.With("processor", c.Name()).Info("driver building, it will take a few seconds")
if !configOptions.DryRun {
b := rootOpts.ToBuild()
if !b.HasOutputs() {
return
}
if opts.UseDKMS {
if opts.useDKMS {
currentUser, err := user.Current()
if err != nil {
slog.With("err", err.Error()).Error("Failed to retrieve user. Exiting.")
Expand All @@ -58,7 +42,7 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F
os.Exit(1)
}
}
if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"), opts.UseDKMS, opts.SrcDir, opts.EnvMap).Start(b); err != nil {
if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"), opts.useDKMS, opts.srcDir, opts.envMap).Start(b); err != nil {
slog.With("err", err.Error()).Error("exiting")
os.Exit(1)
}
Expand Down Expand Up @@ -86,25 +70,19 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F
flagSet.AddFlag(flag)
}
})
flagSet.BoolVar(&opts.UseDKMS, "dkms", false, "Enforce usage of DKMS to build the kernel module.")
flagSet.StringVar(&opts.SrcDir, "src-dir", "", "Enforce usage of local source dir to build drivers.")
flagSet.StringToStringVar(&opts.EnvMap, "env", nil, "Env variables to be enforced during the driver build.")
flagSet.BoolVar(&opts.useDKMS, "dkms", false, "Enforce usage of DKMS to build the kernel module.")
flagSet.StringVar(&opts.srcDir, "src-dir", "", "Enforce usage of local source dir to build drivers.")
flagSet.StringToStringVar(&opts.envMap, "env", nil, "Env variables to be enforced during the driver build.")
localCmd.PersistentFlags().AddFlagSet(flagSet)
return localCmd
}

// Partially overrides rootCmd.persistentPreRunFunc setting some defaults before config init/validation stage.
func persistentPreRunFunc(rootCommand *RootCmd, rootOpts *RootOptions, localOpts *localCmdOptions) func(c *cobra.Command, args []string) error {
func persistentPreRunFunc(rootCommand *RootCmd, rootOpts *RootOptions) func(c *cobra.Command, args []string) error {
return func(c *cobra.Command, args []string) error {
// Default values
rootOpts.Target = "local"
rootOpts.Architecture = runtime.GOARCH
if errs := localOpts.validate(); errs != nil {
for _, err := range errs {
slog.With("err", err.Error()).Error("error validating local command options")
}
return fmt.Errorf("exiting for validation errors")
}
return rootCommand.c.PersistentPreRunE(c, args)
}
}
9 changes: 9 additions & 0 deletions pkg/driverbuilder/builder/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func (l *LocalBuilder) TemplateData(c Config, kr kernelrelease.KernelRelease, _
BuildModule: len(c.ModuleFilePath) > 0,
BuildProbe: len(c.ProbeFilePath) > 0,
GCCVersion: l.GccPath,
CmakeCmd: fmt.Sprintf(cmakeCmdFmt,
c.DriverName,
c.DriverName,
c.DriverVersion,
c.DriverVersion,
c.DriverVersion,
c.DeviceName,
c.DeviceName,
c.DriverVersion),
},
UseDKMS: l.UseDKMS,
DownloadSrc: len(l.SrcDir) == 0, // if no srcdir is provided, download src!
Expand Down
3 changes: 2 additions & 1 deletion pkg/driverbuilder/builder/templates/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ modinfo {{ .ModuleFullPath }}
echo "* Building eBPF probe"
if [ ! -d /sys/kernel/debug/tracing ]; then
echo "* Mounting debugfs"
mount -t debugfs nodev /sys/kernel/debug
# Do not fail if this fails.
mount -t debugfs nodev /sys/kernel/debug || :
fi

{{ if .DownloadSrc }}
Expand Down
43 changes: 0 additions & 43 deletions validate/isAbsDirPath.go

This file was deleted.

1 change: 0 additions & 1 deletion validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func init() {

V.RegisterValidation("loglevel", isLogLevel)
V.RegisterValidation("filepath", isFilePath)
V.RegisterValidation("abs_dirpath", isAbsDirPath)
V.RegisterValidation("sha1", isSHA1)
V.RegisterValidation("target", isTargetSupported)
V.RegisterValidation("architecture", isArchitectureSupported)
Expand Down

0 comments on commit 784f575

Please sign in to comment.