Skip to content

Commit

Permalink
fix: Fix missing checks on product include/exclude glob for attestation.
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Glastra <[email protected]>
  • Loading branch information
matglas committed Apr 12, 2024
1 parent 9fb8891 commit 81bff39
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
24 changes: 19 additions & 5 deletions attestation/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"os"
"path/filepath"

"github.com/gobwas/glob"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/log"
)

// recordArtifacts will walk basePath and record the digests of each file with each of the functions in hashes.
// If file already exists in baseArtifacts and the two artifacts are equal the artifact will not be in the
// returned map of artifacts.
func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.DigestSet, hashes []cryptoutil.DigestValue, visitedSymlinks map[string]struct{}) (map[string]cryptoutil.DigestSet, error) {
func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.DigestSet, hashes []cryptoutil.DigestValue, visitedSymlinks map[string]struct{}, includeGlob glob.Glob, excludeGlob glob.Glob) (map[string]cryptoutil.DigestSet, error) {
artifacts := make(map[string]cryptoutil.DigestSet)
err := filepath.Walk(basePath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -57,15 +58,15 @@ func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.Digest
}

visitedSymlinks[linkedPath] = struct{}{}
symlinkedArtifacts, err := RecordArtifacts(linkedPath, baseArtifacts, hashes, visitedSymlinks)
symlinkedArtifacts, err := RecordArtifacts(linkedPath, baseArtifacts, hashes, visitedSymlinks, includeGlob, excludeGlob)
if err != nil {
return err
}

for artifactPath, artifact := range symlinkedArtifacts {
// all artifacts in the symlink should be recorded relative to our basepath
joinedPath := filepath.Join(relPath, artifactPath)
if shouldRecord(joinedPath, artifact, baseArtifacts) {
if shouldRecord(joinedPath, artifact, baseArtifacts, includeGlob, excludeGlob) {
artifacts[filepath.Join(relPath, artifactPath)] = artifact
}
}
Expand All @@ -78,7 +79,7 @@ func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.Digest
return err
}

if shouldRecord(relPath, artifact, baseArtifacts) {
if shouldRecord(relPath, artifact, baseArtifacts, includeGlob, excludeGlob) {
artifacts[relPath] = artifact
}

Expand All @@ -91,7 +92,20 @@ func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.Digest
// shouldRecord determines whether artifact should be recorded.
// if the artifact is already in baseArtifacts, check if it's changed
// if it is not equal to the existing artifact, return true, otherwise return false
func shouldRecord(path string, artifact cryptoutil.DigestSet, baseArtifacts map[string]cryptoutil.DigestSet) bool {
func shouldRecord(path string, artifact cryptoutil.DigestSet, baseArtifacts map[string]cryptoutil.DigestSet, includeGlob glob.Glob, excludeGlob glob.Glob) bool {

includePath := true
if excludeGlob != nil && excludeGlob.Match(path) {
includePath = false
}
if includeGlob != nil && includeGlob.Match(path) {
includePath = true
}

if !includePath {
return false
}

if previous, ok := baseArtifacts[path]; ok && artifact.Equal(previous) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion attestation/material/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func New(opts ...Option) *Attestor {
}

func (a *Attestor) Attest(ctx *attestation.AttestationContext) error {
materials, err := file.RecordArtifacts(ctx.WorkingDir(), nil, ctx.Hashes(), map[string]struct{}{})
materials, err := file.RecordArtifacts(ctx.WorkingDir(), nil, ctx.Hashes(), map[string]struct{}{}, nil, nil)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions attestation/product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (a *Attestor) Attest(ctx *attestation.AttestationContext) error {
a.compiledExcludeGlob = compiledExcludeGlob

a.baseArtifacts = ctx.Materials()
products, err := file.RecordArtifacts(ctx.WorkingDir(), a.baseArtifacts, ctx.Hashes(), map[string]struct{}{})
products, err := file.RecordArtifacts(ctx.WorkingDir(), a.baseArtifacts, ctx.Hashes(), map[string]struct{}{}, compiledIncludeGlob, compiledExcludeGlob)
if err != nil {
return err
}
Expand Down Expand Up @@ -194,15 +194,18 @@ func (a *Attestor) Products() map[string]attestation.Product {
func (a *Attestor) Subjects() map[string]cryptoutil.DigestSet {
subjects := make(map[string]cryptoutil.DigestSet)
for productName, product := range a.products {

includeSubject := true
if a.compiledExcludeGlob != nil && a.compiledExcludeGlob.Match(productName) {
continue
includeSubject = false
}

if a.compiledIncludeGlob != nil && !a.compiledIncludeGlob.Match(productName) {
continue
if a.compiledIncludeGlob != nil && a.compiledIncludeGlob.Match(productName) {
includeSubject = true
}

subjects[fmt.Sprintf("file:%v", productName)] = product.Digest
if includeSubject {
subjects[fmt.Sprintf("file:%v", productName)] = product.Digest
}
}

return subjects
Expand Down

0 comments on commit 81bff39

Please sign in to comment.