Skip to content

Commit

Permalink
Merge pull request #1419 from flanksource/exec-fixes
Browse files Browse the repository at this point in the history
Exec fixes
  • Loading branch information
moshloop authored Nov 6, 2023
2 parents 5892b95 + 11e83ea commit b55e013
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 264 deletions.
8 changes: 8 additions & 0 deletions api/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ type Context struct {
cache map[string]any
}

func (ctx *Context) Duty() dutyCtx.Context {
return dutyCtx.NewContext(ctx.Context).
WithDB(ctx.db, ctx.pool).
WithKubernetes(ctx.Kubernetes).
WithNamespace(ctx.Namespace).
WithObject(ctx.Canary.ObjectMeta)
}

func (ctx *Context) DB() *gorm.DB {
if ctx.db == nil {
return nil
Expand Down
16 changes: 16 additions & 0 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,22 @@ type GitCheckout struct {
Destination string `yaml:"destination,omitempty" json:"destination,omitempty"`
}

func (git GitCheckout) GetURL() types.EnvVar {
return types.EnvVar{ValueStatic: git.URL}
}

func (git GitCheckout) GetUsername() types.EnvVar {
return git.Username
}

func (git GitCheckout) GetPassword() types.EnvVar {
return git.Password
}

func (git GitCheckout) GetCertificate() types.EnvVar {
return git.Certificate
}

type ExecCheck struct {
Description `yaml:",inline" json:",inline"`
Templatable `yaml:",inline" json:",inline"`
Expand Down
4 changes: 2 additions & 2 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ type Connection struct {
}

func (c Connection) GetEndpoint() string {
return sanitizeEndpoints(c.URL)
return SanitizeEndpoints(c.URL)
}

// Obfuscate passwords of the form ' password=xxxxx ' from connectionString since
// connectionStrings are used as metric labels and we don't want to leak passwords
// Returns the Connection string with the password replaced by '###'
func sanitizeEndpoints(connection string) string {
func SanitizeEndpoints(connection string) string {
if _url, err := url.Parse(connection); err == nil {
if _url.User != nil {
_url.User = nil
Expand Down
51 changes: 32 additions & 19 deletions checks/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checks
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -65,34 +66,37 @@ func (c *ExecChecker) prepareEnvironment(ctx *context.Context, check v1.ExecChec
}

if check.Checkout != nil {
sourceURL := check.Checkout.URL

if connection, err := ctx.HydrateConnectionByURL(check.Checkout.Connection); err != nil {
var err error
var connection *models.Connection
if connection, err = ctx.HydrateConnectionByURL(check.Checkout.Connection); err != nil {
return nil, fmt.Errorf("error hydrating connection: %w", err)
} else if connection != nil {
if sourceURL != "" {
// if we are overriding the url in the connection, set it back
connection.URL = sourceURL
}
goGetterURL, err := connection.AsGoGetterURL()
} else if connection == nil {
connection = &models.Connection{Type: models.ConnectionTypeGit}
if err != nil {
return nil, fmt.Errorf("error getting go getter URL: %w", err)
}
sourceURL = goGetterURL
}

if sourceURL == "" {
return nil, fmt.Errorf("missing URL")
if connection, err = connection.Merge(ctx.Duty(), check.Checkout); err != nil {
return nil, err
}
var goGetterURL string
if goGetterURL, err = connection.AsGoGetterURL(); err != nil {
return nil, err
}

if goGetterURL == "" {
return nil, fmt.Errorf("missing URL %v", *connection)
}

result.mountPoint = check.Checkout.Destination
if result.mountPoint == "" {
pwd, _ := os.Getwd()
result.mountPoint = filepath.Join(pwd, ".downloads", hash.Sha256Hex(sourceURL))
result.mountPoint = filepath.Join(pwd, ".downloads", hash.Sha256Hex(goGetterURL))
}

if err := checkout(ctx, sourceURL, result.mountPoint); err != nil {
return nil, fmt.Errorf("error checking out %s: %w", sourceURL, err)
if err := checkout(ctx, goGetterURL, result.mountPoint); err != nil {
return nil, fmt.Errorf("error checking out: %w", err)
}
}

Expand All @@ -104,7 +108,7 @@ func (c *ExecChecker) Check(ctx *context.Context, extConfig external.Check) pkg.

env, err := c.prepareEnvironment(ctx, check)
if err != nil {
return pkg.Invalid(check, ctx.Canary, err.Error())
return pkg.New(check, ctx.Canary).AddDetails(ExecDetails{}).Invalidf(err.Error())
}

switch runtime.GOOS {
Expand Down Expand Up @@ -138,7 +142,7 @@ func execBash(ctx *context.Context, check v1.ExecCheck, envParams *execEnv) pkg.
result := pkg.Success(check, ctx.Canary).AddDetails(ExecDetails{ExitCode: -1})
fields := strings.Fields(check.Script)
if len(fields) == 0 {
return []*pkg.CheckResult{result.Failf("no script provided")}
return result.Invalidf("no script provided")
}

cmd := exec.CommandContext(ctx, "bash", "-c", check.Script)
Expand All @@ -150,7 +154,7 @@ func execBash(ctx *context.Context, check v1.ExecCheck, envParams *execEnv) pkg.
}

if err := setupConnection(ctx, check, cmd); err != nil {
return []*pkg.CheckResult{result.Failf("failed to setup connection: %v", err)}
return result.Invalidf("failed to setup connection: %v", err)
}

return checkCmd(ctx, cmd, result)
Expand Down Expand Up @@ -245,6 +249,12 @@ func runCmd(ctx *context.Context, cmd *exec.Cmd) ExecDetails {
cmd.Stderr = &stderr

result.Cmd = cmd
if ctx.IsTrace() {
ctx.Infof("%s %s", cmd.Path, cmd.Args)
cmd.Stderr = io.MultiWriter(&stderr, os.Stderr)
cmd.Stdout = io.MultiWriter(&stdout, os.Stdout)
}

result.Error = cmd.Run()
result.ExitCode = cmd.ProcessState.ExitCode()
result.Stderr = strings.TrimSpace(stderr.String())
Expand Down Expand Up @@ -280,11 +290,14 @@ func checkout(ctx *context.Context, url, dst string) error {
Options: []getter.ClientOption{},
}
if ctx.IsDebug() {
ctx.Infof("Downloading %s -> %s", url, dst)
ctx.Infof("Downloading %s -> %s", v1.SanitizeEndpoints(url), dst)
}
if err := client.Get(); err != nil {
return err
}
if ctx.IsTraceEnabled() {
ctx.Infof("Downloaded %s -> %s", v1.SanitizeEndpoints(url), dst)
}
if stashed {
if r := run(ctx, dst, "git", "stash", "pop"); r.Error != nil {
return fmt.Errorf("failed to pop: %v", r.Error)
Expand Down
Loading

0 comments on commit b55e013

Please sign in to comment.