Skip to content

Commit

Permalink
refacotr: rename cosign predicate data field
Browse files Browse the repository at this point in the history
  • Loading branch information
otms61 committed Aug 3, 2022
1 parent 50727bb commit 87b2d21
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
11 changes: 4 additions & 7 deletions pkg/attestation/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
)

type Statement struct {
in_toto.StatementHeader
Predicate interface{} `json:"-"`
RawPredicate interface{} `json:"predicate"`
in_toto.Statement
CosignPredicateData interface{} `json:"-"`
}

// Decode returns the in-toto statement from the in-toto attestation.
Expand Down Expand Up @@ -42,10 +41,8 @@ func Decode(r io.Reader) (Statement, error) {

// When cosign creates an SBOM attestation, it stores the predicate under a "Data" key.
// https://github.com/sigstore/cosign/blob/938ad43f84aa183850014c8cc6d999f4b7ec5e8d/pkg/cosign/attestation/attestation.go#L39-L43
if _, found := st.RawPredicate.(map[string]interface{})["Data"]; found {
st.Predicate = st.RawPredicate.(map[string]interface{})["Data"]
} else {
st.Predicate = st.RawPredicate
if _, found := st.Predicate.(map[string]interface{})["Data"]; found {
st.CosignPredicateData = st.Predicate.(map[string]interface{})["Data"]
}

return st, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/sbom/attestation/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (u Unmarshaler) Unmarshal(r io.Reader) (sbom.SBOM, error) {

var predicateByte []byte

switch attest.Predicate.(type) {
switch attest.CosignPredicateData.(type) {
case map[string]interface{}:
predicateByte, err = json.Marshal(attest.Predicate)
predicateByte, err = json.Marshal(attest.CosignPredicateData)
if err != nil {
return sbom.SBOM{}, xerrors.Errorf("failed to marshal predicate: %w", err)
}
case string:
predicateByte = []byte(attest.Predicate.(string))
predicateByte = []byte(attest.CosignPredicateData.(string))
}

return u.predicateUnmarshaler.Unmarshal(bytes.NewReader(predicateByte))
Expand Down
2 changes: 1 addition & 1 deletion pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func DetectFormat(r io.ReadSeeker) (Format, error) {
// Try Attestation
if attest, err := attestation.Decode(r); err == nil {
if attest.PredicateType == in_toto.PredicateCycloneDX {
switch attest.Predicate.(type) {
switch attest.CosignPredicateData.(type) {
case map[string]interface{}:
return FormatAttestCycloneDXJSON, nil
case string:
Expand Down

0 comments on commit 87b2d21

Please sign in to comment.