Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Add offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cdupuis committed Jan 19, 2023
1 parent 97956e0 commit 0d53038
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
8 changes: 4 additions & 4 deletions sbom/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/anchore/syft/syft/pkg/cataloger/deb"
"github.com/anchore/syft/syft/pkg/cataloger/rpm"
"github.com/anchore/syft/syft/source"
"github.com/atomist-skills/go-skill"
"github.com/pkg/errors"

"github.com/docker/index-cli-plugin/registry"
Expand All @@ -49,15 +50,14 @@ func syftSbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan cha
}

defer close(resultChan)

packageCatalog, packageRelationships, distro, err := syft.CatalogPackages(cache.Source, cataloger.DefaultConfig())
if err != nil {
result.Status = types.Failed
result.Error = errors.Wrap(err, "failed to index image")
resultChan <- result
return
}

d, qualifiers := osQualifiers(distro)
result.Distro = d

Expand Down Expand Up @@ -111,15 +111,15 @@ func syftSbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan cha
}
}
}

result.Packages = make([]types.Package, 0)
packages := packageCatalog.Sorted()
for _, p := range packages {
pkg := toPackage(p, packageRelationships, qualifiers, lm, pm)
result.Packages = append(result.Packages, pkg...)
}

result.Packages = append(result.Packages, detect.AdditionalPackages(result.Packages, cache.Source, lm)...)
skill.Log.Debug("syft indexing completed")
resultChan <- result
}

Expand Down
37 changes: 24 additions & 13 deletions sbom/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package sbom
import (
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
Expand All @@ -31,9 +33,9 @@ import (
aimage "github.com/aquasecurity/trivy/pkg/fanal/artifact/image"
"github.com/aquasecurity/trivy/pkg/fanal/cache"
"github.com/aquasecurity/trivy/pkg/fanal/image"
"github.com/aquasecurity/trivy/pkg/fanal/secret"
stypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
"github.com/atomist-skills/go-skill"
"github.com/pkg/errors"

"github.com/docker/index-cli-plugin/registry"
Expand All @@ -47,7 +49,6 @@ func trivySbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan ch
Packages: make([]types.Package, 0),
Secrets: make([]types.Secret, 0),
}

defer close(resultChan)

cacheClient, err := initializeCache()
Expand All @@ -66,8 +67,8 @@ func trivySbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan ch
resultChan <- result
return
}

art, err := aimage.NewArtifact(img, cacheClient, artifact.Option{})
art, err := aimage.NewArtifact(img, cacheClient, configOptions())
if err != nil {
result.Status = types.Failed
result.Error = errors.Wrap(err, "failed to create new artifact")
Expand All @@ -84,14 +85,15 @@ func trivySbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan ch
}

a := applier.NewApplier(cacheClient)
scanner, err := secret.NewScanner("")
/*scanner, err := secret.NewScanner("")
if err != nil {
result.Status = types.Failed
result.Error = errors.Wrap(err, "failed to create secret scanner")
resultChan <- result
return
}
config := &cache.Source.Image.Metadata.Config
}*/

/*config := &cache.Source.Image.Metadata.Config
for o, h := range config.History {
secrets := scanner.Scan(secret.ScanArgs{
FilePath: "history",
Expand Down Expand Up @@ -129,7 +131,7 @@ func trivySbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan ch
Type: "env",
}))
}
}
}*/
for v := range imageInfo.BlobIDs {
mergedLayer, err := a.ApplyLayers(imageInfo.ID, []string{imageInfo.BlobIDs[v]})
if err != nil {
Expand Down Expand Up @@ -211,15 +213,24 @@ func trivySbom(cache *registry.ImageCache, lm *types.LayerMapping, resultChan ch
}
}
}

skill.Log.Debug("trivy indexing completed")
resultChan <- result
}

func initializeCache() (cache.Cache, error) {
var cacheClient cache.Cache
var err error
cacheClient, err = cache.NewFSCache(utils.CacheDir())
return cacheClient, err
return cache.NewFSCache(utils.CacheDir())
}

func configOptions() artifact.Option {
opts := artifact.Option{
DisabledAnalyzers: []analyzer.Type{analyzer.TypeDockerfile, analyzer.TypeSecret, analyzer.TypeHelm, analyzer.TypeTerraform, analyzer.TypeJSON, analyzer.TypeYaml},
}
if v, ok := os.LookupEnv("ATOMIST_OFFLINE"); ok {
if o, err := strconv.ParseBool(v); err == nil && o{
opts.Offline = true
}
}
return opts
}

func convertSecretFindings(s stypes.Secret, source types.SecretSource) types.Secret {
Expand Down

0 comments on commit 0d53038

Please sign in to comment.