mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 07:10:41 -08:00
Signed-off-by: knqyf263 <knqyf263@gmail.com> Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com> Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
|
"github.com/aquasecurity/trivy/pkg/cache"
|
|
"github.com/aquasecurity/trivy/pkg/commands/operation"
|
|
"github.com/aquasecurity/trivy/pkg/flag"
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
"github.com/aquasecurity/trivy/pkg/module"
|
|
rpcServer "github.com/aquasecurity/trivy/pkg/rpc/server"
|
|
)
|
|
|
|
// Run runs the scan
|
|
func Run(ctx context.Context, opts flag.Options) (err error) {
|
|
log.InitLogger(opts.Debug, opts.Quiet)
|
|
|
|
// configure cache dir
|
|
cacheClient, err := cache.New(opts.CacheDir, opts.CacheOptions.CacheBackendOptions)
|
|
if err != nil {
|
|
return xerrors.Errorf("server cache error: %w", err)
|
|
}
|
|
defer cacheClient.Close()
|
|
log.Debug("Cache", log.String("dir", opts.CacheDir))
|
|
|
|
// download the database file
|
|
if err = operation.DownloadDB(ctx, opts.AppVersion, opts.CacheDir, opts.DBRepository,
|
|
true, opts.SkipDBUpdate, opts.RegistryOpts()); err != nil {
|
|
return err
|
|
}
|
|
|
|
if opts.DownloadDBOnly {
|
|
return nil
|
|
}
|
|
|
|
if err = db.Init(opts.CacheDir); err != nil {
|
|
return xerrors.Errorf("error in vulnerability DB initialize: %w", err)
|
|
}
|
|
|
|
// Initialize WASM modules
|
|
m, err := module.NewManager(ctx, module.Options{
|
|
Dir: opts.ModuleDir,
|
|
EnabledModules: opts.EnabledModules,
|
|
})
|
|
if err != nil {
|
|
return xerrors.Errorf("WASM module error: %w", err)
|
|
}
|
|
m.Register()
|
|
|
|
server := rpcServer.NewServer(opts.AppVersion, opts.Listen, opts.CacheDir, opts.Token, opts.TokenHeader,
|
|
opts.DBRepository, opts.RegistryOpts())
|
|
return server.ListenAndServe(ctx, cacheClient, opts.SkipDBUpdate)
|
|
}
|