fix(vex): handle 304 status code#10307
Conversation
e2637f6 to
486981c
Compare
940a4a3 to
3809871
Compare
|
Could we simplify this a bit? I think If so, we could handle everything in func Download(ctx context.Context, src, dst, pwd string, opts Options) (string, error) {
- // go-getter doesn't allow the dst directory already exists if the src is directory.
- _ = os.RemoveAll(dst)
+ // go-getter doesn't allow the dst directory already exists if the src is directory.
+ // We rename dst as a backup and restore it if the download is skipped or fails.
+ dst = filepath.Clean(dst)
+ cleanup, err := backupDst(dst)
+ if err != nil {
+ return "", xerrors.Errorf("failed to back up dst: %w", err)
+ }
+ defer cleanup()// backupDst renames dst aside so that go-getter can create it fresh.
// It returns a cleanup function that restores dst on error or removes the backup on success.
func backupDst(dst string) (cleanup func(), err error) {
backup := dst + ".backup"
_ = os.RemoveAll(backup)
if err := os.Rename(dst, backup); errors.Is(err, os.ErrNotExist) {
return func() {}, nil
} else if err != nil {
return nil, xerrors.Errorf("failed to rename dst: %w", err)
}
return func() {
if _, err := os.Stat(dst); errors.Is(err, os.ErrNotExist) {
if err := os.Rename(backup, dst); err != nil {
log.Warn("Failed to restore backup", log.FilePath(backup), log.Err(err))
}
return
}
if err := os.RemoveAll(backup); err != nil {
log.Warn("Failed to remove backup", log.FilePath(backup), log.Err(err))
}
}, nil
} |
|
Hello @knqyf263, I used a platform-specific files because I had failing tests on windows. I can restore it and test again. Maybe they were just flaky. |
0a30633 to
8d58439
Compare
|
@knqyf263 fixed. Thanks for the suggestion. |
Co-authored-by: Fabrizio Sestito <fabrizio.sestito@suse.com> Signed-off-by: Alessio Greggi <alessio.greggi@suse.com>
8d58439 to
33241ab
Compare
|
@knqyf263 I've rebased the branch to main but some tests are failing. Could you please re-run them? |
|
@knqyf263 any updates on this? |
|
Due to the impact of the recent incident, all tokens have been revoked, and IP restrictions and rulesets have been introduced for the aquasecurity organization. As a result, the CI/CD is currently not functioning. We are working to restore it as soon as possible, so we would appreciate your patience. |
Apologize for that. Take your time and thanks for clarifying ;) |
Description
Currently, trivy deletes the content of the vex cache every time a new download happens.
This action is not always needed, since when an ETag is provided, the server may respond with
304 Not Modified, meaning the existing content atdstis still valid.In that case, trivy should not destroy
dst. Instead, we can move it aside as a backup and restore it on 304.This particular case happens when a Vexhub repository has a small ' update_interval '.
Here's the original PR where we noticed the bug: kubewarden/sbomscanner#867
cc @fabriziosestito
Related issues
There's no open issue for it.
Remove this section if you don't have related PRs.
Checklist