-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
213 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package vulndump | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/stackrox/scanner/database" | ||
) | ||
|
||
const defaultBatchSize = 10_000 | ||
|
||
// osLoader batch loads OS-level vulnerabilities into a buffer. | ||
type osLoader struct { | ||
rc io.ReadCloser | ||
|
||
dec *json.Decoder | ||
|
||
batchSize int | ||
buf []database.Vulnerability | ||
done bool | ||
err error | ||
} | ||
|
||
func newOSLoader(source io.ReadCloser) (*osLoader, error) { | ||
// See https://pkg.go.dev/encoding/json#example-Decoder.Decode-Stream | ||
// for an example of how this decoder will be used. | ||
dec := json.NewDecoder(source) | ||
// Read the initial token, "[". | ||
_, err := dec.Token() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &osLoader{ | ||
rc: source, | ||
dec: dec, | ||
batchSize: defaultBatchSize, | ||
buf: make([]database.Vulnerability, 0, defaultBatchSize), | ||
}, nil | ||
} | ||
|
||
// Next loads the next batch of vulnerabilities and returns | ||
// whether it was successful or not. | ||
func (l *osLoader) Next() bool { | ||
if l.done || l.err != nil { | ||
return false | ||
} | ||
|
||
l.buf = l.buf[:0] | ||
for i := 0; i < l.batchSize; i++ { | ||
if !l.dec.More() { | ||
// JSON array has no more values. | ||
l.done = true | ||
return true | ||
} | ||
l.buf = append(l.buf, database.Vulnerability{}) | ||
if err := l.dec.Decode(&l.buf[i]); err != nil { | ||
l.err = err | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Vulns returns the "next" bath of vulnerabilities. | ||
// It is expected to be called once for each successful call to Next. | ||
func (l *osLoader) Vulns() []database.Vulnerability { | ||
return l.buf | ||
} | ||
|
||
// Err returns the error associated with loading vulnerabilities. | ||
// It is expected to be non-nil upon an unsuccessful call to Next. | ||
func (l *osLoader) Err() error { | ||
return l.err | ||
} | ||
|
||
// Close closes the loader. | ||
func (l *osLoader) Close() error { | ||
l.buf = nil // hint to GC to clean this. | ||
// Don't bother reading the final token, "]", | ||
// as it is possible there was a failure reading | ||
// the JSON array. Just close the file. | ||
return l.rc.Close() | ||
} |
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,62 @@ | ||
package vulndump | ||
|
||
import ( | ||
"archive/zip" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stackrox/rox/pkg/utils" | ||
"github.com/stackrox/scanner/pkg/ziputil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const defURL = "https://definitions.stackrox.io/93AEC554-29EE-4E24-96D6-744092A98444/diff.zip" | ||
|
||
func mustFetchOSVulns(b *testing.B) *os.File { | ||
f, err := os.Create(filepath.Join(b.TempDir(), "vulns.zip")) | ||
require.NoError(b, err) | ||
|
||
c := &http.Client{Timeout: 30 * time.Second} | ||
resp, err := c.Get(defURL) | ||
require.NoError(b, err) | ||
defer utils.IgnoreError(resp.Body.Close) | ||
|
||
_, err = io.Copy(f, resp.Body) | ||
require.NoError(b, err) | ||
|
||
return f | ||
} | ||
|
||
func BenchmarkOSLoader(b *testing.B) { | ||
f := mustFetchOSVulns(b) | ||
defer utils.IgnoreError(f.Close) | ||
|
||
zipR, err := zip.OpenReader(f.Name()) | ||
require.NoError(b, err) | ||
vulnsF, err := ziputil.OpenFile(&zipR.Reader, OSVulnsFileName) | ||
require.NoError(b, err) | ||
|
||
runtime.GC() | ||
|
||
loader, err := newOSLoader(vulnsF) | ||
require.NoError(b, err) | ||
defer func() { | ||
require.NoError(b, loader.Close()) | ||
}() | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
var n int | ||
for loader.Next() { | ||
vulns := loader.Vulns() | ||
n += len(vulns) | ||
} | ||
require.NoError(b, loader.Err()) | ||
b.Logf("Loaded %d vulns", n) | ||
} | ||
} |
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,28 @@ | ||
package vulndump | ||
|
||
import ( | ||
"archive/zip" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stackrox/rox/pkg/utils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkLoadOSVulnsFromDump(b *testing.B) { | ||
f := mustFetchOSVulns(b) | ||
defer utils.IgnoreError(f.Close) | ||
|
||
zipR, err := zip.OpenReader(f.Name()) | ||
require.NoError(b, err) | ||
defer utils.IgnoreError(zipR.Close) | ||
|
||
runtime.GC() | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
vulns, err := LoadOSVulnsFromDump(&zipR.Reader) | ||
require.NoError(b, err) | ||
b.Logf("Loaded %d vulns", len(vulns)) | ||
} | ||
} |