Skip to content

Commit

Permalink
fix: add fail case scenario to e2e for verifying attestation, collect…
Browse files Browse the repository at this point in the history
… all validation errors first

Signed-off-by: Batuhan Apaydın <[email protected]>
  • Loading branch information
developer-guy committed Sep 10, 2021
1 parent d01b4da commit f9e5de3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/cosign/cli/verify_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"flag"
"fmt"
"io"
"os"
"strings"

"github.com/in-toto/in-toto-golang/in_toto"
Expand Down Expand Up @@ -204,6 +205,7 @@ func (c *VerifyAttestationCommand) Exec(ctx context.Context, args []string) (err
return err
}

var validationErrors []error
for _, vp := range verified {
var payloadData map[string]interface{}
err := json.Unmarshal(vp.Payload, &payloadData)
Expand Down Expand Up @@ -272,10 +274,18 @@ func (c *VerifyAttestationCommand) Exec(ctx context.Context, args []string) (err
}
}
if err := cue.ValidateJSON(payload, c.Policies.EntryPoints); err != nil {
return fmt.Errorf("validating policy: %w", err)
validationErrors = append(validationErrors, err)
}
}

if len(validationErrors) > 0 {
fmt.Println("Some errors occurred during the validation:")
for _, v := range validationErrors {
_, _ = fmt.Fprintf(os.Stderr, fmt.Sprintf("- %v\n", v))
}
return fmt.Errorf("%d errors occurred", len(validationErrors))
}

// The attestations are always JSON, so use the raw "text" mode for outputting them instead of conversion
PrintVerification(imageRef, verified, co, "text")
}
Expand Down
2 changes: 2 additions & 0 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func TestAttestVerify(t *testing.T) {
t.Fatal(err)
}

mustErr(verifyAttestation.Exec(ctx, []string{imgName}), t)

// Success case
cuePolicy = `builder: id: "2"`
if err := ioutil.WriteFile(policyPath, []byte(cuePolicy), 0600); err != nil {
Expand Down

0 comments on commit f9e5de3

Please sign in to comment.