From 83db87cf6c0f7dd6ec08275631d7715d9b8f92d0 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Wed, 6 Dec 2023 09:14:01 +0100 Subject: [PATCH] chore(cmd,pkg): properly preload driver version for all drivers commands. Moreover, enforce that driver version is a semver and repos is a list of requestURI. Signed-off-by: Federico Di Pierro --- cmd/driver/config/config_test.go | 2 +- cmd/driver/driver_linux.go | 31 ++++++++++++++++++++++++++++++- cmd/driver/install/install.go | 29 ----------------------------- pkg/options/driver.go | 17 ++++++++++++++++- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/cmd/driver/config/config_test.go b/cmd/driver/config/config_test.go index 34ae301b..1acc1a3e 100644 --- a/cmd/driver/config/config_test.go +++ b/cmd/driver/config/config_test.go @@ -92,7 +92,7 @@ var _ = Describe("config", func() { BeforeEach(func() { args = []string{driverCmd, configCmd, "--config", configFile, "--host-root", "foo/"} }) - addAssertFailedBehavior("ERROR host-root must be an absolute path: foo/") + addAssertFailedBehavior("ERROR host-root must be an absolute path (foo/)") }) When("with invalid driver type", func() { diff --git a/cmd/driver/driver_linux.go b/cmd/driver/driver_linux.go index 4da826f2..5d0d3a90 100644 --- a/cmd/driver/driver_linux.go +++ b/cmd/driver/driver_linux.go @@ -22,7 +22,10 @@ import ( "context" "errors" "fmt" + "path/filepath" + "strings" + "github.com/blang/semver" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -76,7 +79,7 @@ func NewDriverCmd(ctx context.Context, opt *options.Common) *cobra.Command { if err != nil { return err } - if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { + if err := cmd.Flags().Set(f.Name, strings.Join(val, ",")); err != nil { return fmt.Errorf("unable to overwrite \"repo\" flag: %w", err) } } @@ -149,6 +152,11 @@ func NewDriverCmd(ctx context.Context, opt *options.Common) *cobra.Command { return fmt.Errorf("automatic driver selection failed") } } + // If empty, try to load it automatically from /usr/src sub folders, + // using the most recent (ie: the one with greatest semver) driver version. + if driver.Version == "" { + driver.Version = loadDriverVersion() + } return driver.Validate() }, } @@ -165,3 +173,24 @@ func NewDriverCmd(ctx context.Context, opt *options.Common) *cobra.Command { cmd.AddCommand(driverprintenv.NewDriverPrintenvCmd(ctx, opt, driver)) return cmd } + +func loadDriverVersion() string { + isSet := false + greatestVrs := semver.Version{} + paths, _ := filepath.Glob("/usr/src/falco-*+driver") + for _, path := range paths { + drvVer := strings.TrimPrefix(filepath.Base(path), "falco-") + sv, err := semver.Parse(drvVer) + if err != nil { + continue + } + if sv.GT(greatestVrs) { + greatestVrs = sv + isSet = true + } + } + if isSet { + return greatestVrs.String() + } + return "" +} diff --git a/cmd/driver/install/install.go b/cmd/driver/install/install.go index 8db73ade..3f9f61f3 100644 --- a/cmd/driver/install/install.go +++ b/cmd/driver/install/install.go @@ -20,11 +20,8 @@ import ( "errors" "fmt" "net/http" - "path/filepath" - "strings" "time" - "github.com/blang/semver" "github.com/spf13/cobra" "golang.org/x/net/context" @@ -65,11 +62,6 @@ func NewDriverInstallCmd(ctx context.Context, opt *options.Common, driver *optio Long: `[Preview] Install previously configured driver, either downloading it or attempting a build. ** This command is in preview and under development. **`, RunE: func(cmd *cobra.Command, args []string) error { - // If empty, try to load it automatically from /usr/src sub folders, - // using the most recent (ie: the one with greatest semver) driver version. - if o.Driver.Version == "" { - o.Driver.Version = loadDriverVersion() - } dest, err := o.RunDriverInstall(ctx) if dest != "" { // We don't care about errors at this stage @@ -100,27 +92,6 @@ func NewDriverInstallCmd(ctx context.Context, opt *options.Common, driver *optio return cmd } -func loadDriverVersion() string { - isSet := false - greatestVrs := semver.Version{} - paths, _ := filepath.Glob("/usr/src/falco-*+driver") - for _, path := range paths { - drvVer := strings.TrimPrefix(filepath.Base(path), "falco-") - sv, err := semver.Parse(drvVer) - if err != nil { - continue - } - if sv.GT(greatestVrs) { - greatestVrs = sv - isSet = true - } - } - if isSet { - return greatestVrs.String() - } - return "" -} - //nolint:gosec // this was an existent option in falco-driver-loader that we are porting. func setDefaultHTTPClientOpts(downloadOptions driverDownloadOptions) { // Skip insecure verify diff --git a/pkg/options/driver.go b/pkg/options/driver.go index 697dfca1..780ca6d1 100644 --- a/pkg/options/driver.go +++ b/pkg/options/driver.go @@ -17,9 +17,12 @@ package options import ( "fmt" + "net/url" "path/filepath" "sort" + "github.com/blang/semver" + "github.com/falcosecurity/falcoctl/internal/config" drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type" ) @@ -61,7 +64,19 @@ func (d *Driver) ToDriverConfig() *config.Driver { // Validate runs all validators steps for Driver options. func (d *Driver) Validate() error { if !filepath.IsAbs(d.HostRoot) { - return fmt.Errorf("host-root must be an absolute path: %s", d.HostRoot) + return fmt.Errorf("host-root must be an absolute path (%s)", d.HostRoot) + } + + if _, err := semver.Parse(d.Version); err != nil { + return fmt.Errorf("version must be semver compatible (%s): %w", d.Version, err) } + + for _, repo := range d.Repos { + _, err := url.ParseRequestURI(repo) + if err != nil { + return fmt.Errorf("repo must be a valid url (%s): %w", repo, err) + } + } + return nil }