forked from chainguard-dev/malcontent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh sample test data via new
refresh
command (chainguard-dev#634)
* Refresh test data via new refresh command Signed-off-by: egibs <[email protected]> * Export CachedRules to help with refresh scans Signed-off-by: egibs <[email protected]> --------- Signed-off-by: egibs <[email protected]>
- Loading branch information
Showing
9 changed files
with
624 additions
and
233 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
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package refresh | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/chainguard-dev/malcontent/pkg/action" | ||
"github.com/chainguard-dev/malcontent/pkg/malcontent" | ||
"github.com/chainguard-dev/malcontent/pkg/render" | ||
"github.com/chainguard-dev/malcontent/rules" | ||
thirdparty "github.com/chainguard-dev/malcontent/third_party" | ||
) | ||
|
||
type actionData struct { | ||
format string | ||
outputPath string | ||
scanPath string | ||
} | ||
|
||
// actionSampleData contains paths and formats for test data in pkg/action. | ||
var actionTestData = []actionData{ | ||
{ | ||
format: "json", | ||
scanPath: "pkg/action/testdata/static.tar.xz", | ||
outputPath: "pkg/action/testdata/scan_oci", | ||
}, | ||
{ | ||
format: "json", | ||
scanPath: "pkg/action/testdata/apko_nested.tar.gz", | ||
outputPath: "pkg/action/testdata/scan_archive", | ||
}, | ||
} | ||
|
||
func actionRefresh(ctx context.Context) ([]TestData, error) { | ||
testData := make([]TestData, 0, len(actionTestData)) | ||
|
||
for _, td := range actionTestData { | ||
output := td.outputPath | ||
scan := td.scanPath | ||
|
||
if _, err := os.Stat(scan); err != nil { | ||
return nil, fmt.Errorf("special case input file not found: %s: %w", scan, err) | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { | ||
return nil, fmt.Errorf("create output directory: %w", err) | ||
} | ||
|
||
outFile, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600) | ||
if err != nil { | ||
return nil, fmt.Errorf("create output file %s: %w", output, err) | ||
} | ||
|
||
r, err := render.New(td.format, outFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("create renderer for %s: %w", output, err) | ||
} | ||
|
||
rfs := []fs.FS{rules.FS, thirdparty.FS} | ||
yrs, err := action.CachedRules(ctx, rfs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &malcontent.Config{ | ||
IgnoreSelf: false, | ||
MinFileRisk: 0, | ||
MinRisk: 0, | ||
OCI: false, | ||
QuantityIncreasesRisk: true, | ||
Renderer: r, | ||
RuleFS: rfs, | ||
Rules: yrs, | ||
ScanPaths: []string{scan}, | ||
TrimPrefixes: []string{"pkg/action/"}, | ||
} | ||
|
||
testData = append(testData, TestData{ | ||
Config: c, | ||
OutputPath: output, | ||
}) | ||
} | ||
return testData, nil | ||
} |
Oops, something went wrong.