Skip to content

Commit

Permalink
chore(cmd,pkg): properly preload driver version for all drivers comma…
Browse files Browse the repository at this point in the history
…nds.

Moreover, enforce that driver version is a semver and repos is a list of requestURI.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed Dec 6, 2023
1 parent b35b23a commit 83db87c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/driver/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
31 changes: 30 additions & 1 deletion cmd/driver/driver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/blang/semver"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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()
},
}
Expand All @@ -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 ""
}
29 changes: 0 additions & 29 deletions cmd/driver/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion pkg/options/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 83db87c

Please sign in to comment.