Skip to content

Commit

Permalink
Use sentinel errors bundle validation in validateBundle func (#291)
Browse files Browse the repository at this point in the history
* use existing err when bundle verification content is missing

Signed-off-by: Meredith Lancaster <malancas@github.com>

* use sentinel errors for empty bundle and missing bundle content

Signed-off-by: Meredith Lancaster <malancas@github.com>

---------

Signed-off-by: Meredith Lancaster <malancas@github.com>
  • Loading branch information
malancas authored Sep 9, 2024
1 parent 01e70e8 commit 8c0e75b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import (

var ErrValidation = errors.New("validation error")
var ErrUnsupportedMediaType = fmt.Errorf("%w: unsupported media type", ErrValidation)
var ErrEmptyBundle = fmt.Errorf("%w: empty protobuf bundle", ErrValidation)
var ErrMissingVerificationMaterial = fmt.Errorf("%w: missing verification material", ErrValidation)
var ErrMissingBundleContent = fmt.Errorf("%w: missing bundle content", ErrValidation)
var ErrUnimplemented = errors.New("unimplemented")
var ErrInvalidAttestation = fmt.Errorf("%w: invalid attestation", ErrValidation)
var ErrMissingEnvelope = fmt.Errorf("%w: missing valid envelope", ErrInvalidAttestation)
Expand Down Expand Up @@ -172,11 +174,11 @@ func getBundleVersion(mediaType string) (string, error) {

func validateBundle(b *protobundle.Bundle) error {
if b == nil {
return fmt.Errorf("empty protobuf bundle")
return ErrEmptyBundle
}

if b.Content == nil {
return fmt.Errorf("missing bundle content")
return ErrMissingBundleContent
}

switch b.Content.(type) {
Expand All @@ -185,12 +187,8 @@ func validateBundle(b *protobundle.Bundle) error {
return fmt.Errorf("invalid bundle content: bundle content must be either a message signature or dsse envelope")
}

if b.VerificationMaterial == nil {
return fmt.Errorf("missing verification material")
}

if b.VerificationMaterial.Content == nil {
return fmt.Errorf("missing verification material content")
if b.VerificationMaterial == nil || b.VerificationMaterial.Content == nil {
return ErrMissingVerificationMaterial
}

switch b.VerificationMaterial.Content.(type) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ func Test_BundleValidation(t *testing.T) {
Content: &protobundle.Bundle_MessageSignature{},
},
},
errMsg: "invalid bundle: missing verification material content",
errMsg: "invalid bundle: validation error: missing verification material",
wantErr: true,
},
{
Expand All @@ -1067,7 +1067,7 @@ func Test_BundleValidation(t *testing.T) {
Content: nil,
},
},
errMsg: "invalid bundle: missing bundle content",
errMsg: "invalid bundle: validation error: missing bundle content",
wantErr: true,
},
{
Expand All @@ -1079,7 +1079,7 @@ func Test_BundleValidation(t *testing.T) {
VerificationMaterial: nil,
},
},
errMsg: "invalid bundle: missing verification material",
errMsg: "invalid bundle: validation error: missing verification material",
wantErr: true,
},
{
Expand Down

0 comments on commit 8c0e75b

Please sign in to comment.