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

Commit

Permalink
Better API for ValidatedProvenance
Browse files Browse the repository at this point in the history
  • Loading branch information
rbehjati committed Sep 14, 2022
1 parent 8366177 commit ab3e11a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
6 changes: 2 additions & 4 deletions experimental/auth-logic/wrappers/provenance_build_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ func (pbw ProvenanceBuildWrapper) EmitStatement() (UnattributedStatement, error)
return UnattributedStatement{}, fmt.Errorf("provenance build wrapper couldn't parse provenance file: %v", err)
}

provenance := validatedProvenance.GetProvenance()

// TODO(#69): Set the verifier as a field in pbw, and use that here.
verifier := verify.AmberProvenanceMetadataVerifier{}
if err := verifier.Verify(pbw.ProvenanceFilePath); err != nil {
return UnattributedStatement{}, fmt.Errorf("verification of the provenance file failed: %v", err)
}

simpleProv := simplifiedProvenance{
AppName: SanitizeName(provenance.Subject[0].Name),
MeasuredSha256: provenance.Subject[0].Digest["sha256"],
AppName: SanitizeName(validatedProvenance.GetBinaryName()),
MeasuredSha256: validatedProvenance.GetBinarySHA256Hash(),
}

policyTemplate, err := template.ParseFiles(provenanceBuilderTemplate)
Expand Down
12 changes: 4 additions & 8 deletions experimental/auth-logic/wrappers/provenance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ func (p ProvenanceWrapper) EmitStatement() (UnattributedStatement, error) {
return UnattributedStatement{}, fmt.Errorf("provenance wrapper couldn't prase provenance file: %v", err)
}

provenance := validatedProvenance.GetProvenance()
sanitizedAppName := SanitizeName(provenance.Subject[0].Name)
expectedHash, hashOk := provenance.Subject[0].Digest["sha256"]

if !hashOk {
return UnattributedStatement{}, fmt.Errorf("provenance file did not give an expected hash")
}
sanitizedAppName := SanitizeName(validatedProvenance.GetBinaryName())
expectedHash := validatedProvenance.GetBinarySHA256Hash()

provenance := validatedProvenance.GetProvenance()
predicate := provenance.Predicate.(slsa.ProvenancePredicate)
builderName := predicate.Builder.ID

Expand All @@ -66,5 +62,5 @@ func GetAppNameFromProvenance(provenanceFilePath string) (string, error) {
return "", fmt.Errorf("provenance wrapper couldn't prase provenance file: %v", err)
}

return validatedProvenance.GetProvenance().Subject[0].Name, nil
return validatedProvenance.GetBinaryName(), nil
}
3 changes: 2 additions & 1 deletion internal/verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ReproducibleProvenanceVerifier struct {
// it and verifying that the resulting binary has a hash equal to the one
// specified in the subject of the given provenance file. If the hashes are
// different returns an error, otherwise returns nil.
// TODO(#126): Refactor and separate verification logic from the logic for reading the file.
func (verifier *ReproducibleProvenanceVerifier) Verify(provenanceFilePath string) error {
provenance, err := amber.ParseProvenanceFile(provenanceFilePath)
if err != nil {
Expand All @@ -68,7 +69,7 @@ func (verifier *ReproducibleProvenanceVerifier) Verify(provenanceFilePath string
}

// The provenance is valid, therefore `expectedBinaryHash` is guaranteed to be non-empty.
expectedBinaryHash := provenance.GetProvenance().Subject[0].Digest["sha256"]
expectedBinaryHash := provenance.GetBinarySHA256Hash()

if err := buildConfig.VerifyBinarySha256Hash(expectedBinaryHash); err != nil {
return fmt.Errorf("failed to verify the hash of the built binary: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions internal/verifier/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestReproducibleProvenanceVerifier_invalidHash(t *testing.T) {

want := "failed to verify the hash of the built binary"

if err := verifier.Verify(invalidHashProvenancePath); !strings.Contains(err.Error(), want) {
t.Fatalf("want error containing message %q, got %v", want, err)
if got := verifier.Verify(invalidHashProvenancePath); !strings.Contains(got.Error(), want) {
t.Fatalf("got %v, want error message containing %q,", got, want)
}
}

Expand All @@ -75,8 +75,8 @@ func TestReproducibleProvenanceVerifier_badCommand(t *testing.T) {

want := "couldn't build the binary"

if err := verifier.Verify(badCommandProvenancePath); !strings.Contains(err.Error(), want) {
t.Fatalf("want error containing message %q, got %v", want, err)
if got := verifier.Verify(badCommandProvenancePath); !strings.Contains(got.Error(), want) {
t.Fatalf("got %v, want error message containing %q,", got, want)
}
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/amber/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ func (p *ValidatedProvenance) GetProvenance() intoto.Statement {
}
}

// GetBinarySHA256Hash returns the SHA256 hash of the subject.
func (p *ValidatedProvenance) GetBinarySHA256Hash() string {
return p.provenance.Subject[0].Digest["sha256"]
}

// GetBinaryName returns the name of the subject.
func (p *ValidatedProvenance) GetBinaryName() string {
return p.provenance.Subject[0].Name
}

func validateSLSAProvenanceJSON(provenanceFile []byte) error {
schemaFile, err := os.ReadFile(SchemaPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/amber/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestExampleProvenance(t *testing.T) {
testutil.AssertEq(t, "commitHash length", len(predicate.Materials[1].Digest["sha1"]), wantSHA1HexDigitLength)
testutil.AssertEq(t, "builderImageID length", len(predicate.Materials[0].Digest["sha256"]), wantSHA256HexDigitLength)
testutil.AssertEq(t, "builderImageURI", predicate.Materials[0].URI, fmt.Sprintf("gcr.io/oak-ci/oak@sha256:%s", predicate.Materials[0].Digest["sha256"]))
testutil.AssertEq(t, "subjectName", provenance.Subject[0].Name, "oak_functions_loader")
testutil.AssertEq(t, "subjectName", validatedProvenance.GetBinaryName(), "oak_functions_loader")
testutil.AssertNonEmpty(t, "command[0]", buildConfig.Command[0])
testutil.AssertNonEmpty(t, "command[1]", buildConfig.Command[1])
testutil.AssertNonEmpty(t, "builderId", predicate.Builder.ID)
Expand Down

0 comments on commit ab3e11a

Please sign in to comment.