Skip to content

Commit

Permalink
add helper to extract regular files
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Feb 6, 2022
1 parent 79a06f1 commit 31d9e30
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 30 deletions.
7 changes: 4 additions & 3 deletions internal/err_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ func CloseAndLogError(closer io.Closer, location string) {
}

type ErrPath struct {
Path string
Err error
Cataloger string
Path string
Err error
}

func (e ErrPath) Error() string {
return fmt.Sprintf("unable to observe contents of %+v: %v", e.Path, e.Err)
return fmt.Sprintf("%s unable to observe contents of %+v: %v", e.Cataloger, e.Path, e.Err)
}

func IsErrPath(err error) bool {
Expand Down
30 changes: 30 additions & 0 deletions syft/file/all_regular_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package file

import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/source"
)

func allRegularFiles(resolver source.FileResolver) (locations []source.Location) {
for location := range resolver.AllLocations() {
resolvedLocations, err := resolver.FilesByPath(location.RealPath)
if err != nil {
log.Warnf("unable to resolve %+v: %+v", location, err)
continue
}

for _, resolvedLocation := range resolvedLocations {
metadata, err := resolver.FileMetadataByLocation(resolvedLocation)
if err != nil {
log.Warnf("unable to get metadata for %+v: %+v", location, err)
continue
}

if metadata.Type != source.RegularFile {
continue
}
locations = append(locations, resolvedLocation)
}
}
return locations
}
2 changes: 1 addition & 1 deletion syft/file/classification_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (i *ClassificationCataloger) Catalog(resolver source.FileResolver) (map[sou
results := make(map[source.Coordinates][]Classification)

numResults := 0
for location := range resolver.AllLocations() {
for _, location := range allRegularFiles(resolver) {
for _, classifier := range i.classifiers {
result, err := classifier.Classify(resolver, location)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion syft/file/contents_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (i *ContentsCataloger) catalogLocation(resolver source.FileResolver, locati

buf := &bytes.Buffer{}
if _, err = io.Copy(base64.NewEncoder(base64.StdEncoding, buf), contentReader); err != nil {
return "", internal.ErrPath{Path: location.RealPath, Err: err}
return "", internal.ErrPath{Cataloger: "contents-cataloger", Path: location.RealPath, Err: err}
}

return buf.String(), nil
Expand Down
7 changes: 2 additions & 5 deletions syft/file/digest_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func NewDigestsCataloger(hashes []crypto.Hash) (*DigestsCataloger, error) {

func (i *DigestsCataloger) Catalog(resolver source.FileResolver) (map[source.Coordinates][]Digest, error) {
results := make(map[source.Coordinates][]Digest)
var locations []source.Location
for location := range resolver.AllLocations() {
locations = append(locations, location)
}
locations := allRegularFiles(resolver)
stage, prog := digestsCatalogingProgress(int64(len(locations)))
for _, location := range locations {
stage.Current = location.RealPath
Expand Down Expand Up @@ -90,7 +87,7 @@ func (i *DigestsCataloger) catalogLocation(resolver source.FileResolver, locatio

size, err := io.Copy(io.MultiWriter(writers...), contentReader)
if err != nil {
return nil, internal.ErrPath{Path: location.RealPath, Err: err}
return nil, internal.ErrPath{Cataloger: "digests-cataloger", Path: location.RealPath, Err: err}
}

if size == 0 {
Expand Down
7 changes: 2 additions & 5 deletions syft/file/secrets_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func NewSecretsCataloger(patterns map[string]*regexp.Regexp, revealValues bool,

func (i *SecretsCataloger) Catalog(resolver source.FileResolver) (map[source.Coordinates][]SearchResult, error) {
results := make(map[source.Coordinates][]SearchResult)
var locations []source.Location
for location := range resolver.AllLocations() {
locations = append(locations, location)
}
locations := allRegularFiles(resolver)
stage, prog, secretsDiscovered := secretsCatalogingProgress(int64(len(locations)))
for _, location := range locations {
stage.Current = location.RealPath
Expand Down Expand Up @@ -86,7 +83,7 @@ func (i *SecretsCataloger) catalogLocation(resolver source.FileResolver, locatio
// TODO: in the future we can swap out search strategies here
secrets, err := catalogLocationByLine(resolver, location, i.patterns)
if err != nil {
return nil, internal.ErrPath{Path: location.RealPath, Err: err}
return nil, internal.ErrPath{Cataloger: "secrets-cataloger", Path: location.RealPath, Err: err}
}

if i.revealValues {
Expand Down
2 changes: 2 additions & 0 deletions syft/source/directory_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ func (r directoryResolver) addSymlinkToIndex(p string, info os.FileInfo) (string
}

location := NewLocationFromDirectory(p, *ref)
location.VirtualPath = p
metadata := fileMetadataFromPath(p, usedInfo, r.isInIndex(location))
metadata.LinkDestination = linkTarget
r.addFileMetadataToIndex(ref, metadata)

return targetAbsPath, nil
Expand Down
1 change: 1 addition & 0 deletions syft/source/file_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func fileMetadataFromPath(path string, info os.FileInfo, withMIMEType bool) File
// unsupported across platforms
UserID: uid,
GroupID: gid,
Size: info.Size(),
MIMEType: mimeType,
}
}
28 changes: 14 additions & 14 deletions test/cli/packages_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,55 +209,55 @@ func TestRegistryAuth(t *testing.T) {
}{
{
name: "fallback to keychain",
args: []string{"packages", "-vv", "registry:localhost:5000/something:latest"},
args: []string{"packages", "-vv", "registry:localhost:17/something:latest"},
assertions: []traitAssertion{
assertInOutput("source=OciRegistry"),
assertInOutput("localhost:5000/something:latest"),
assertInOutput("localhost:17/something:latest"),
assertInOutput("no registry credentials configured, using the default keychain"),
},
},
{
name: "use creds",
args: []string{"packages", "-vv", "registry:localhost:5000/something:latest"},
args: []string{"packages", "-vv", "registry:localhost:17/something:latest"},
env: map[string]string{
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:5000",
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:17",
"SYFT_REGISTRY_AUTH_USERNAME": "username",
"SYFT_REGISTRY_AUTH_PASSWORD": "password",
},
assertions: []traitAssertion{
assertInOutput("source=OciRegistry"),
assertInOutput("localhost:5000/something:latest"),
assertInOutput(`using basic auth for registry "localhost:5000"`),
assertInOutput("localhost:17/something:latest"),
assertInOutput(`using basic auth for registry "localhost:17"`),
},
},
{
name: "use token",
args: []string{"packages", "-vv", "registry:localhost:5000/something:latest"},
args: []string{"packages", "-vv", "registry:localhost:17/something:latest"},
env: map[string]string{
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:5000",
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:17",
"SYFT_REGISTRY_AUTH_TOKEN": "token",
},
assertions: []traitAssertion{
assertInOutput("source=OciRegistry"),
assertInOutput("localhost:5000/something:latest"),
assertInOutput(`using token for registry "localhost:5000"`),
assertInOutput("localhost:17/something:latest"),
assertInOutput(`using token for registry "localhost:17"`),
},
},
{
name: "not enough info fallsback to keychain",
args: []string{"packages", "-vv", "registry:localhost:5000/something:latest"},
args: []string{"packages", "-vv", "registry:localhost:17/something:latest"},
env: map[string]string{
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:5000",
"SYFT_REGISTRY_AUTH_AUTHORITY": "localhost:17",
},
assertions: []traitAssertion{
assertInOutput("source=OciRegistry"),
assertInOutput("localhost:5000/something:latest"),
assertInOutput("localhost:17/something:latest"),
assertInOutput(`no registry credentials configured, using the default keychain`),
},
},
{
name: "allows insecure http flag",
args: []string{"packages", "-vv", "registry:localhost:5000/something:latest"},
args: []string{"packages", "-vv", "registry:localhost:17/something:latest"},
env: map[string]string{
"SYFT_REGISTRY_INSECURE_USE_HTTP": "true",
},
Expand Down
2 changes: 1 addition & 1 deletion test/cli/power_user_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestPowerUserCmdFlags(t *testing.T) {
},
},
{
name: "defaut-secrets-dir-results-w-reveal-values",
name: "default-secrets-dir-results-w-reveal-values",
env: map[string]string{
"SYFT_SECRETS_REVEAL_VALUES": "true",
},
Expand Down

0 comments on commit 31d9e30

Please sign in to comment.