Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Expose LoadProvenances (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbehjati authored Jan 27, 2023
1 parent a4f7d82 commit d120edf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
49 changes: 48 additions & 1 deletion internal/endorser/endorser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package endorser

import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
Expand All @@ -30,6 +32,7 @@ import (
"github.com/project-oak/transparent-release/internal/verifier"
"github.com/project-oak/transparent-release/pkg/amber"
"github.com/project-oak/transparent-release/pkg/intoto"
"github.com/project-oak/transparent-release/pkg/types"
)

// ParsedProvenance contains a provenance in the internal ProvenanceIR format,
Expand Down Expand Up @@ -127,7 +130,51 @@ func verifyConsistency(provenanceIRs []common.ProvenanceIR) error {
return errs
}

func getProvenanceBytes(provenanceURI string) ([]byte, error) {
// LoadProvenance loads a number of provenance from the give URIs. Returns an
// array of ParsedProvenance instances, or an error if loading or parsing any
// of the provenances fails. See LoadProvenance for more details.
func LoadProvenances(provenanceURIs []string) ([]ParsedProvenance, error) {
// load provenanceIRs from URIs
provenances := make([]ParsedProvenance, 0, len(provenanceURIs))
for _, uri := range provenanceURIs {
parsedProvenance, err := LoadProvenance(uri)
if err != nil {
return nil, fmt.Errorf("couldn't load the provenance from %s: %v", uri, err)
}
provenances = append(provenances, *parsedProvenance)
}
return provenances, nil
}

// LoadProvenance loads a provenance from the give URI (either a local file or
// a remote file on an HTTP/HTTPS server). Returns an instance of
// ParsedProvenance if loading and parsing is successful, or an error Otherwise.
func LoadProvenance(provenanceURI string) (*ParsedProvenance, error) {
provenanceBytes, err := GetProvenanceBytes(provenanceURI)
if err != nil {
return nil, fmt.Errorf("couldn't load the provenance bytes from %s: %v", provenanceURI, err)
}
// Parse into a validated provenance to get the predicate/build type of the provenance.
validatedProvenance, err := types.ParseStatementData(provenanceBytes)
if err != nil {
return nil, fmt.Errorf("couldn't parse bytes from %s into a validated provenance: %v", provenanceURI, err)
}
// Map to internal provenance representation based on the predicate/build type.
provenanceIR, err := common.FromValidatedProvenance(validatedProvenance)
if err != nil {
return nil, fmt.Errorf("couldn't map from %s to internal representation: %v", validatedProvenance, err)
}
sum256 := sha256.Sum256(provenanceBytes)
return &ParsedProvenance{
Provenance: *provenanceIR,
SourceMetadata: amber.ProvenanceData{
URI: provenanceURI,
SHA256Digest: hex.EncodeToString(sum256[:]),
},
}, nil
}

func GetProvenanceBytes(provenanceURI string) ([]byte, error) {
uri, err := url.Parse(provenanceURI)
if err != nil {
return nil, fmt.Errorf("could not parse the URI (%q): %v", provenanceURI, err)
Expand Down
50 changes: 8 additions & 42 deletions internal/endorser/endorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package endorser

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strings"
Expand All @@ -26,7 +24,6 @@ import (
"github.com/project-oak/transparent-release/internal/common"
"github.com/project-oak/transparent-release/internal/testutil"
"github.com/project-oak/transparent-release/pkg/amber"
"github.com/project-oak/transparent-release/pkg/types"
)

const (
Expand All @@ -35,37 +32,6 @@ const (
errorBinaryDigest = "do not contain the actual binary SHA256 digest"
)

func loadProvenances(provenanceURIs []string) ([]ParsedProvenance, error) {
// load provenanceIRs from URIs
provenances := make([]ParsedProvenance, 0, len(provenanceURIs))
for _, uri := range provenanceURIs {
provenanceBytes, err := getProvenanceBytes(uri)
if err != nil {
return nil, fmt.Errorf("couldn't load the provenance bytes from %s: %v", uri, err)
}
// Parse into a validated provenance to get the predicate/build type of the provenance.
validatedProvenance, err := types.ParseStatementData(provenanceBytes)
if err != nil {
return nil, fmt.Errorf("couldn't parse bytes from %s into a validated provenance: %v", uri, err)
}
// Map to internal provenance representation based on the predicate/build type.
provenanceIR, err := common.FromValidatedProvenance(validatedProvenance)
if err != nil {
return nil, fmt.Errorf("couldn't map from %s to internal representation: %v", validatedProvenance, err)
}
sum256 := sha256.Sum256(provenanceBytes)
parsedProvenance := ParsedProvenance{
Provenance: *provenanceIR,
SourceMetadata: amber.ProvenanceData{
URI: uri,
SHA256Digest: hex.EncodeToString(sum256[:]),
},
}
provenances = append(provenances, parsedProvenance)
}
return provenances, nil
}

func TestGenerateEndorsement_SingleValidEndorsement(t *testing.T) {
tomorrow := time.Now().AddDate(0, 0, 1)
nextWeek := time.Now().AddDate(0, 0, 7)
Expand All @@ -79,7 +45,7 @@ func TestGenerateEndorsement_SingleValidEndorsement(t *testing.T) {
t.Fatalf("Could not load provenance: %v", err)
}
tempURI := "file://" + tempPath
provenances, err := loadProvenances([]string{tempURI})
provenances, err := LoadProvenances([]string{tempURI})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand Down Expand Up @@ -112,7 +78,7 @@ func TestLoadAndVerifyProvenances_MultipleValidEndorsement(t *testing.T) {
if err != nil {
t.Fatalf("Could not load provenance: %v", err)
}
provenances, err := loadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
provenances, err := LoadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand All @@ -131,8 +97,8 @@ func TestLoadAndVerifyProvenances_MultipleValidEndorsement(t *testing.T) {
}

func TestLoadProvenances_FailingSingleRemoteProvenanceEndorsement(t *testing.T) {
_, err := loadProvenances([]string{"https://github.com/project-oak/transparent-release/blob/main/testdata/amber_provenance.json"})
want := "couldn't parse bytes from"
_, err := LoadProvenances([]string{"https://github.com/project-oak/transparent-release/blob/main/testdata/amber_provenance.json"})
want := "couldn't load the provenance"
if err == nil || !strings.Contains(err.Error(), want) {
t.Fatalf("got %q, want error message containing %q,", err, want)
}
Expand All @@ -144,7 +110,7 @@ func TestLoadAndVerifyProvenances_ConsistentNotVerified(t *testing.T) {
t.Fatalf("Could not load provenance: %v", err)
}

provenances, err := loadProvenances([]string{"file://" + tempPath1, "file://" + tempPath1})
provenances, err := LoadProvenances([]string{"file://" + tempPath1, "file://" + tempPath1})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand All @@ -170,7 +136,7 @@ func TestLoadAndVerify_InconsistentVerified(t *testing.T) {
t.Fatalf("Could not load provenance: %v", err)
}

provenances, err := loadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
provenances, err := LoadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand All @@ -197,7 +163,7 @@ func TestLoadAndVerify_InconsistentNotVerified(t *testing.T) {
t.Fatalf("Could not load provenance: %v", err)
}

provenances, err := loadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
provenances, err := LoadProvenances([]string{"file://" + tempPath1, "file://" + tempPath2})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand All @@ -222,7 +188,7 @@ func TestLoadAndVerifyProvenances_NotVerified(t *testing.T) {
t.Fatalf("Could not load provenance: %v", err)
}

provenances, err := loadProvenances([]string{"file://" + tempPath1})
provenances, err := LoadProvenances([]string{"file://" + tempPath1})
if err != nil {
t.Fatalf("Could not load provenances: %v", err)
}
Expand Down

0 comments on commit d120edf

Please sign in to comment.