Skip to content

Commit

Permalink
Merge pull request #6 from ChengaDev/run-command-errors-display
Browse files Browse the repository at this point in the history
Report errors to client on `run` command
  • Loading branch information
jondot authored Nov 27, 2022
2 parents d611250 + 1436a61 commit 53a64db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ func main() {

err = preflight.ExecPiped(string(s), CLI.Run.Hash)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}

case "run <hash|url> <cmd>":
err := preflight.Exec(CLI.Run.Cmd, CLI.Run.Hash)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}

Expand All @@ -92,7 +94,7 @@ func main() {
os.Exit(1)
}
if !res.Ok {
preflight.Porcelain.CheckFailed(res)
preflight.Porcelain.ReportCheckResult(res)
os.Exit(1)
}
fmt.Print(content) // give back so piping can continue
Expand All @@ -111,7 +113,7 @@ func main() {
}

if !res.Ok {
preflight.Porcelain.CheckFailed(res)
preflight.Porcelain.ReportCheckResult(res)
os.Exit(1)
}

Expand All @@ -133,7 +135,7 @@ func main() {
}

if res.HasLookupVulns() {
preflight.Porcelain.CheckFailed(res)
preflight.Porcelain.ReportCheckResult(res)
os.Exit(1)
}

Expand All @@ -152,7 +154,7 @@ func main() {
res, err := preflight.Check(string(s), fmt.Sprintf("%v=?", CLI.Create.Digest))

if res.HasLookupVulns() {
preflight.Porcelain.CheckFailed(res)
preflight.Porcelain.ReportCheckResult(res)
os.Exit(1)
}

Expand Down
23 changes: 4 additions & 19 deletions pkg/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package pkg
import (
//nolint
"crypto/md5"
"errors"
"io"
"net/http"

Expand Down Expand Up @@ -70,20 +69,6 @@ type CheckResult struct {
Ok bool
}

func (c *CheckResult) Error() error {
if c.Ok {
return nil
}

if c.HasLookupVulns() {
return fmt.Errorf("vulnerable digest: %v", c.ActualDigest)
} else if c.HasValidationVulns() {
return fmt.Errorf("digest mismatch: actual: %v, expected: %v", c.ActualDigest, c.ExpectedDigests)
} else {
return errors.New("unknown result error")
}
}

func (c *CheckResult) HasValidationVulns() bool {
return c.ValidDigest == nil && len(c.ExpectedDigests) > 0
}
Expand Down Expand Up @@ -216,8 +201,8 @@ func (a *Preflight) ExecPiped(script, sig string) error {
return err
}
if !check.Ok {
a.Porcelain.CheckFailed(check)
return check.Error()
a.Porcelain.ReportCheckResult(check)
return nil
}
a.Porcelain.RunOk()

Expand All @@ -240,8 +225,8 @@ func (a *Preflight) Exec(args []string, sig string) error {
}

if !check.Ok {
a.Porcelain.CheckFailed(check)
return check.Error()
a.Porcelain.ReportCheckResult(check)
return nil
}

a.Porcelain.RunOk()
Expand Down
6 changes: 3 additions & 3 deletions pkg/porcelain.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (p *Porcelain) RunOk() {
fmt.Printf("%v Preflight verified\n", EMO_CHECK)
}

func (p *Porcelain) CheckFailed(check *CheckResult) {
if check.ValidDigest == nil {
func (p *Porcelain) ReportCheckResult(check *CheckResult) {
if check.HasValidationVulns() {

green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
Expand All @@ -61,7 +61,7 @@ Actual:
green(fmtSigs(check.ExpectedDigests)),
red(check.ActualDigest.String()),
)
} else if check.LookupResult != nil && check.LookupResult.Vulnerable {
} else if check.HasLookupVulns() {
fmt.Printf(`%v Preflight failed:`, EMO_FAILED)
fmt.Printf(` Digest matches but marked as vulnerable.
Expand Down

0 comments on commit 53a64db

Please sign in to comment.