-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: prevent file resolver from bubbling errors in binary cataloger (…
…#3410) Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com> Signed-off-by: Keith Zantow <kzantow@gmail.com> Co-authored-by: Keith Zantow <kzantow@gmail.com>
- Loading branch information
Showing
7 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package unknown | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/anchore/syft/internal/log" | ||
"github.com/anchore/syft/syft/file" | ||
) | ||
|
||
var pathErrorRegex = regexp.MustCompile(`.*path="([^"]+)".*`) | ||
|
||
// ProcessPathErrors replaces "path" errors returned from the file.Resolver into unknowns, | ||
// and warn logs non-unknown errors, returning only the unknown errors | ||
func ProcessPathErrors(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
errText := err.Error() | ||
if pathErrorRegex.MatchString(errText) { | ||
foundPath := pathErrorRegex.ReplaceAllString(err.Error(), "$1") | ||
if foundPath != "" { | ||
return New(file.NewLocation(foundPath), err) | ||
} | ||
} | ||
unknowns, remainingErrors := ExtractCoordinateErrors(err) | ||
log.Warn(remainingErrors) | ||
|
||
var out []error | ||
for _, u := range unknowns { | ||
out = append(out, &u) | ||
} | ||
return Join(out...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package unknown | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/anchore/syft/syft/file" | ||
) | ||
|
||
func Test_ProcessPathErrors(t *testing.T) { | ||
tests := []struct { | ||
errorText string | ||
expected error | ||
}{ | ||
{ | ||
errorText: `prefix path="/var/lib/thing" suffix`, | ||
expected: &CoordinateError{ | ||
Coordinates: file.Coordinates{ | ||
RealPath: "/var/lib/thing", | ||
}, | ||
Reason: fmt.Errorf(`prefix path="/var/lib/thing" suffix`), | ||
}, | ||
}, | ||
{ | ||
errorText: `prefix path="/var/lib/thing"`, | ||
expected: &CoordinateError{ | ||
Coordinates: file.Coordinates{ | ||
RealPath: "/var/lib/thing", | ||
}, | ||
Reason: fmt.Errorf(`prefix path="/var/lib/thing"`), | ||
}, | ||
}, | ||
{ | ||
errorText: `path="/var/lib/thing" suffix`, | ||
expected: &CoordinateError{ | ||
Coordinates: file.Coordinates{ | ||
RealPath: "/var/lib/thing", | ||
}, | ||
Reason: fmt.Errorf(`path="/var/lib/thing" suffix`), | ||
}, | ||
}, | ||
{ | ||
errorText: "all your base are belong to us", | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.errorText, func(t *testing.T) { | ||
got := ProcessPathErrors(fmt.Errorf("%s", test.errorText)) | ||
require.Equal(t, test.expected, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
FROM alpine@sha256:c5c5fda71656f28e49ac9c5416b3643eaa6a108a8093151d6d1afc9463be8e33 | ||
RUN rm -rf /lib/apk/db/installed | ||
COPY . /home/files | ||
# add a circular reference that will result in a failure while executing FindByGlob: | ||
RUN mkdir -p /etc/alternatives && ln -s /etc/alternatives/java2 /etc/alternatives/java && ln -s /etc/alternatives/java /etc/alternatives/java2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters