Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mediatype string builder to bundle package #154

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 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,40 @@ 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")
}

var mtString string

version = strings.TrimPrefix(version, "v")
mtString = fmt.Sprintf("%s.v%s+json", mediaTypeBase, strings.TrimPrefix(version, "v"))

if version == "0.1" || version == "0.2" {
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
}{
{"normal-semver", "v0.3", "application/vnd.dev.sigstore.bundle.v0.3+json", false},
{"old-semver1", "v0.1", "application/vnd.dev.sigstore.bundle+json;version=0.1", false},
{"old-semver2", "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)
})
}
}
Loading