Skip to content

Commit f60918d

Browse files
committed
Update trivy dependency and fix the code due to breaking changes
1 parent 17b62a1 commit f60918d

File tree

8 files changed

+1108
-1128
lines changed

8 files changed

+1108
-1128
lines changed

.github/workflows/default.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Default
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-22.04
8+
steps:
9+
- uses: actions/checkout@v4
10+
- name: Setup Go
11+
uses: actions/setup-go@v4
12+
with:
13+
go-version: '1.21.4'
14+
- name: Test
15+
run: go test ./...
16+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# image-analyzer
22
OCI images analyzer
3+
4+
This repository exists for 2 reasons:
5+
- `github.com/castai/image-analyzer/image/daemon.Image` interface.
6+
- Having various analyzers bundled in a single module.

artifact.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type Artifact struct {
8585

8686
type ArtifactOption = artifact.Option
8787

88-
func NewArtifact(img types.Image, log logrus.FieldLogger, c CacheClient, opt artifact.Option) (*Artifact, error) {
88+
func NewArtifact(img types.Image, log logrus.FieldLogger, c CacheClient, opt ArtifactOption) (*Artifact, error) {
8989
a, err := analyzer.NewAnalyzerGroup(analyzer.AnalyzerOptions{
9090
Group: opt.AnalyzerGroup,
9191
DisabledAnalyzers: opt.DisabledAnalyzers,
@@ -108,7 +108,7 @@ func NewArtifact(img types.Image, log logrus.FieldLogger, c CacheClient, opt art
108108
log: log,
109109
image: img,
110110
cache: c,
111-
walker: walker.NewLayerTar(opt.SkipFiles, opt.SkipDirs, opt.Slow),
111+
walker: walker.NewLayerTar(opt.SkipFiles, opt.SkipDirs),
112112
analyzer: a,
113113
configAnalyzer: ca,
114114
artifactOption: opt,
@@ -249,11 +249,7 @@ func (a Artifact) inspect(ctx context.Context, missingImageKey string, layerKeys
249249
blobInfo := make(chan types.BlobInfo)
250250

251251
errCh := make(chan error)
252-
limit := semaphore.NewWeighted(parallel)
253-
if a.artifactOption.Slow {
254-
// Inspect layers in series
255-
limit = semaphore.NewWeighted(1)
256-
}
252+
limit := semaphore.NewWeighted(int64(a.artifactOption.Parallel))
257253

258254
var osFound types.OS
259255

@@ -337,11 +333,7 @@ func (a Artifact) inspectLayer(ctx context.Context, diffID string, disabled []an
337333
var wg sync.WaitGroup
338334
opts := analyzer.AnalysisOptions{Offline: a.artifactOption.Offline}
339335
result := analyzer.NewAnalysisResult()
340-
limit := semaphore.NewWeighted(parallel)
341-
if a.artifactOption.Slow {
342-
// Inspect layers in series
343-
limit = semaphore.NewWeighted(1)
344-
}
336+
limit := semaphore.NewWeighted(int64(a.artifactOption.Parallel))
345337

346338
// Walk a tar layer
347339
opqDirs, whFiles, err := a.walker.Walk(r, func(filePath string, info os.FileInfo, opener analyzer.Opener) error {

artifact_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package analyzer
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/aquasecurity/trivy/pkg/fanal/types"
8+
"github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/castai/image-analyzer/image"
12+
)
13+
14+
func TestArtifact(t *testing.T) {
15+
r := require.New(t)
16+
ctx := context.Background()
17+
log := logrus.New()
18+
log.SetLevel(logrus.DebugLevel)
19+
20+
digest := "alpine@sha256:60eda2a7bc29a54fe6beae0d72312ea995eb3b8387535e8dbf6767fd1b765d34" // linux/amd64 digest
21+
img, err := image.NewFromRemote(ctx, digest, types.ImageOptions{})
22+
r.NoError(err)
23+
24+
artifact, err := NewArtifact(img, log, mockBlockCache{}, ArtifactOption{
25+
Offline: true,
26+
Parallel: 1,
27+
})
28+
r.NoError(err)
29+
30+
ref, err := artifact.Inspect(ctx)
31+
r.NoError(err)
32+
r.NotNil(ref)
33+
r.NotNil(ref.BlobsInfo)
34+
r.Len(ref.BlobsInfo, 1)
35+
r.Len(ref.BlobsInfo[0].PackageInfos, 1)
36+
r.Len(ref.BlobsInfo[0].PackageInfos[0].Packages, 15)
37+
38+
r.NotNil(ref.ConfigFile)
39+
r.Equal("amd64", ref.ConfigFile.Architecture)
40+
r.Equal("linux", ref.ConfigFile.OS)
41+
42+
r.NotNil(ref.ArtifactInfo)
43+
r.Equal("amd64", ref.ArtifactInfo.Architecture)
44+
r.Equal("linux", ref.ArtifactInfo.OS)
45+
46+
r.NotNil(ref.OsInfo)
47+
r.Equal("alpine", string(ref.OsInfo.Family))
48+
}
49+
50+
type mockBlockCache struct{}
51+
52+
func (mockBlockCache) PutBlob(ctx context.Context, key string, blob []byte) error {
53+
return nil
54+
}
55+
56+
func (mockBlockCache) GetBlob(ctx context.Context, key string) ([]byte, error) {
57+
return nil, ErrCacheNotFound
58+
}

dpkg/copyright.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var (
3131

3232
// dpkgLicenseAnalyzer parses copyright files and detect licenses
3333
type dpkgLicenseAnalyzer struct {
34-
licenseFull bool
34+
licenseFull bool
35+
classifierConfidenceLevel float64
3536
}
3637

3738
// Analyze parses /usr/share/doc/*/copyright files
@@ -45,7 +46,7 @@ func (a *dpkgLicenseAnalyzer) Analyze(_ context.Context, input analyzer.Analysis
4546
return nil, xerrors.Errorf("seek error: %w", err)
4647
}
4748

48-
licenseFile, err := licensing.Classify(input.FilePath, input.Content)
49+
licenseFile, err := licensing.Classify(input.FilePath, input.Content, a.classifierConfidenceLevel)
4950
if err != nil {
5051
return nil, xerrors.Errorf("license classification error: %w", err)
5152
}
@@ -117,6 +118,7 @@ func (a *dpkgLicenseAnalyzer) parseCopyright(r dio.ReadSeekerAt) []types.License
117118

118119
func (a *dpkgLicenseAnalyzer) Init(opt analyzer.AnalyzerOptions) error {
119120
a.licenseFull = opt.LicenseScannerOption.Full
121+
a.classifierConfidenceLevel = opt.LicenseScannerOption.ClassifierConfidenceLevel
120122
return nil
121123
}
122124

0 commit comments

Comments
 (0)