Skip to content

Commit

Permalink
If SBOM ref has .json suffix, assume JSON mediatype (#1859)
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <josh@dolit.ski>
  • Loading branch information
jdolitsky committed May 11, 2022
1 parent 7a3c04f commit 471dde0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmd/cosign/cli/options/attach.go
Expand Up @@ -17,6 +17,7 @@ package options

import (
"fmt"
"strings"

"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,12 +70,16 @@ func (o *AttachSBOMOptions) AddFlags(cmd *cobra.Command) {
}

func (o *AttachSBOMOptions) MediaType() (types.MediaType, error) {
var looksLikeJSON bool
if strings.HasSuffix(o.SBOM, ".json") {
looksLikeJSON = true
}
switch o.SBOMType {
case "cyclonedx":
if o.SBOMInputFormat != "" && o.SBOMInputFormat != ctypes.XMLInputFormat && o.SBOMInputFormat != ctypes.JSONInputFormat {
return "invalid", fmt.Errorf("invalid SBOM input format: %q, expected (json|xml)", o.SBOMInputFormat)
}
if o.SBOMInputFormat == ctypes.JSONInputFormat {
if o.SBOMInputFormat == ctypes.JSONInputFormat || looksLikeJSON {
return ctypes.CycloneDXJSONMediaType, nil
}
return ctypes.CycloneDXXMLMediaType, nil
Expand All @@ -83,7 +88,7 @@ func (o *AttachSBOMOptions) MediaType() (types.MediaType, error) {
if o.SBOMInputFormat != "" && o.SBOMInputFormat != ctypes.TextInputFormat && o.SBOMInputFormat != ctypes.JSONInputFormat {
return "invalid", fmt.Errorf("invalid SBOM input format: %q, expected (json|text)", o.SBOMInputFormat)
}
if o.SBOMInputFormat == ctypes.JSONInputFormat {
if o.SBOMInputFormat == ctypes.JSONInputFormat || looksLikeJSON {
return ctypes.SPDXJSONMediaType, nil
}
return ctypes.SPDXMediaType, nil
Expand Down

0 comments on commit 471dde0

Please sign in to comment.