Skip to content

Commit

Permalink
Add mediatype string builder
Browse files Browse the repository at this point in the history
This commit adds a mediatype build string to generate media type strings

Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Apr 23, 2024
1 parent 8a2315a commit 0a944d8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var ErrMissingEnvelope = fmt.Errorf("%w: missing envelope", ErrInvalidAttestatio
var ErrDecodingJSON = fmt.Errorf("%w: decoding json", ErrInvalidAttestation)
var ErrDecodingB64 = fmt.Errorf("%w: decoding base64", ErrInvalidAttestation)

const mediaTypeBase = "application/vnd.dev.sigstore.bundle"

func ErrValidationError(err error) error {
return fmt.Errorf("%w: %w", ErrValidation, err)
}
Expand Down Expand Up @@ -113,17 +115,32 @@ func (b *ProtobufBundle) validate() error {
return nil
}

// MediaTypeString returns a mediatype string for the specified bundle version.
// The function returns an error if the resulting string does validate.
func MediaTypeString(version string) (string, error) {
if version == "" {
return "", fmt.Errorf("unable to build media type string, no version defined")
}

mtString := fmt.Sprintf("%s+json;version=%s", mediaTypeBase, strings.TrimPrefix(version, "v"))
if _, err := getBundleVersion(mtString); err != nil {
return "", fmt.Errorf("unable to build mediatype: %w", err)
}

return mtString, nil
}

func getBundleVersion(mediaType string) (string, error) {
switch mediaType {
case "application/vnd.dev.sigstore.bundle+json;version=0.1":
case mediaTypeBase + "+json;version=0.1":
return "v0.1", nil
case "application/vnd.dev.sigstore.bundle+json;version=0.2":
case mediaTypeBase + "+json;version=0.2":
return "v0.2", nil
case "application/vnd.dev.sigstore.bundle+json;version=0.3":
case mediaTypeBase + "+json;version=0.3":
return "v0.3", nil
}
if strings.HasPrefix(mediaType, "application/vnd.dev.sigstore.bundle.v") && strings.HasSuffix(mediaType, "+json") {
version := strings.TrimPrefix(mediaType, "application/vnd.dev.sigstore.bundle.")
if strings.HasPrefix(mediaType, mediaTypeBase+".v") && strings.HasSuffix(mediaType, "+json") {
version := strings.TrimPrefix(mediaType, mediaTypeBase+".")
version = strings.TrimSuffix(version, "+json")
if semver.IsValid(version) {
return version, nil
Expand Down
30 changes: 30 additions & 0 deletions pkg/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package bundle
import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func Test_getBundleVersion(t *testing.T) {
Expand Down Expand Up @@ -94,3 +96,31 @@ func Test_getBundleVersion(t *testing.T) {
})
}
}

func TestMediaTypeString(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
ver string
expected string
mustErr bool
}{
// Test for input, and valid an invalid versions. All validation
// is in getBundleVersion.
{"normal-semver", "v0.2", "application/vnd.dev.sigstore.bundle+json;version=0.2", false},
{"blank", "", "", true},
{"invalid", "garbage", "", true},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
res, err := MediaTypeString(tc.ver)
if tc.mustErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tc.expected, res)
})
}
}

0 comments on commit 0a944d8

Please sign in to comment.