mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 23:00:42 -08:00
refactor(internal): export internal packages (#887)
* refactor: export internal packages * refactor(server): define Server * refactor: fix lint issues * test(integration): fix imports
This commit is contained in:
@@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal"
|
"github.com/aquasecurity/trivy/pkg/commands"
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := internal.NewApp(version)
|
app := commands.NewApp(version)
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
testcontainers "github.com/testcontainers/testcontainers-go"
|
testcontainers "github.com/testcontainers/testcontainers-go"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal"
|
"github.com/aquasecurity/trivy/pkg/commands"
|
||||||
"github.com/aquasecurity/trivy/pkg/report"
|
"github.com/aquasecurity/trivy/pkg/report"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -484,7 +484,7 @@ func setup(t *testing.T, options setupOptions) (*cli.App, string, string) {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// Setup CLI App
|
// Setup CLI App
|
||||||
app := internal.NewApp(version)
|
app := commands.NewApp(version)
|
||||||
app.Writer = ioutil.Discard
|
app.Writer = ioutil.Discard
|
||||||
osArgs := setupServer(addr, options.token, options.tokenHeader, cacheDir, options.cacheBackend)
|
osArgs := setupServer(addr, options.token, options.tokenHeader, cacheDir, options.cacheBackend)
|
||||||
|
|
||||||
@@ -497,7 +497,7 @@ func setup(t *testing.T, options setupOptions) (*cli.App, string, string) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Setup CLI App
|
// Setup CLI App
|
||||||
app := internal.NewApp(version)
|
app := commands.NewApp(version)
|
||||||
app.Writer = ioutil.Discard
|
app.Writer = ioutil.Discard
|
||||||
|
|
||||||
return app, addr, cacheDir
|
return app, addr, cacheDir
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal"
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/aquasecurity/trivy/pkg/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRun_WithDockerEngine(t *testing.T) {
|
func TestRun_WithDockerEngine(t *testing.T) {
|
||||||
@@ -283,7 +283,7 @@ func TestRun_WithDockerEngine(t *testing.T) {
|
|||||||
defer os.Remove(of.Name())
|
defer os.Remove(of.Name())
|
||||||
|
|
||||||
// run trivy
|
// run trivy
|
||||||
app := internal.NewApp("dev")
|
app := commands.NewApp("dev")
|
||||||
trivyArgs := []string{"trivy"}
|
trivyArgs := []string{"trivy"}
|
||||||
trivyArgs = append(trivyArgs, "--cache-dir", cacheDir)
|
trivyArgs = append(trivyArgs, "--cache-dir", cacheDir)
|
||||||
if tc.withImageSubcommand {
|
if tc.withImageSubcommand {
|
||||||
|
|||||||
@@ -14,8 +14,9 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
|
||||||
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
var update = flag.Bool("update", false, "update golden files")
|
var update = flag.Bool("update", false, "update golden files")
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
_ "github.com/aquasecurity/fanal/analyzer"
|
_ "github.com/aquasecurity/fanal/analyzer"
|
||||||
testdocker "github.com/aquasecurity/trivy/integration/docker"
|
testdocker "github.com/aquasecurity/trivy/integration/docker"
|
||||||
"github.com/aquasecurity/trivy/internal"
|
"github.com/aquasecurity/trivy/pkg/commands"
|
||||||
"github.com/aquasecurity/trivy/pkg/report"
|
"github.com/aquasecurity/trivy/pkg/report"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ func scan(imageRef name.Reference, baseDir, goldenFile string, opt registryOptio
|
|||||||
defer unsetEnv()
|
defer unsetEnv()
|
||||||
|
|
||||||
// Setup CLI App
|
// Setup CLI App
|
||||||
app := internal.NewApp("dev")
|
app := commands.NewApp("dev")
|
||||||
app.Writer = ioutil.Discard
|
app.Writer = ioutil.Discard
|
||||||
|
|
||||||
osArgs := []string{"trivy", "--cache-dir", cacheDir, "--format", "json", "--skip-update", "--output", outputFile, imageRef.Name()}
|
osArgs := []string{"trivy", "--cache-dir", cacheDir, "--format", "json", "--skip-update", "--output", outputFile, imageRef.Name()}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal"
|
"github.com/aquasecurity/trivy/pkg/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRun_WithTar(t *testing.T) {
|
func TestRun_WithTar(t *testing.T) {
|
||||||
@@ -378,7 +378,7 @@ func TestRun_WithTar(t *testing.T) {
|
|||||||
defer os.RemoveAll(cacheDir)
|
defer os.RemoveAll(cacheDir)
|
||||||
|
|
||||||
// Setup CLI App
|
// Setup CLI App
|
||||||
app := internal.NewApp("dev")
|
app := commands.NewApp("dev")
|
||||||
app.Writer = ioutil.Discard
|
app.Writer = ioutil.Discard
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package internal
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -13,10 +13,10 @@ import (
|
|||||||
|
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
"github.com/aquasecurity/trivy-db/pkg/types"
|
"github.com/aquasecurity/trivy-db/pkg/types"
|
||||||
"github.com/aquasecurity/trivy/internal/artifact"
|
"github.com/aquasecurity/trivy/pkg/commands/artifact"
|
||||||
"github.com/aquasecurity/trivy/internal/client"
|
"github.com/aquasecurity/trivy/pkg/commands/client"
|
||||||
"github.com/aquasecurity/trivy/internal/plugin"
|
"github.com/aquasecurity/trivy/pkg/commands/plugin"
|
||||||
"github.com/aquasecurity/trivy/internal/server"
|
"github.com/aquasecurity/trivy/pkg/commands/server"
|
||||||
tdb "github.com/aquasecurity/trivy/pkg/db"
|
tdb "github.com/aquasecurity/trivy/pkg/db"
|
||||||
"github.com/aquasecurity/trivy/pkg/utils"
|
"github.com/aquasecurity/trivy/pkg/utils"
|
||||||
"github.com/aquasecurity/trivy/pkg/vulnerability"
|
"github.com/aquasecurity/trivy/pkg/vulnerability"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package internal
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -9,13 +9,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_showVersion(t *testing.T) {
|
func Test_showVersion(t *testing.T) {
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package config
|
package artifact
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds the artifact config
|
// Config holds the artifact config
|
||||||
@@ -24,8 +24,8 @@ type Config struct {
|
|||||||
autoRefresh bool
|
autoRefresh bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New is the factory method to return config
|
// NewConfig is the factory method to return config
|
||||||
func New(c *cli.Context) (Config, error) {
|
func NewConfig(c *cli.Context) (Config, error) {
|
||||||
gc, err := config.NewGlobalConfig(c)
|
gc, err := config.NewGlobalConfig(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Config{}, xerrors.Errorf("failed to initialize global options: %w", err)
|
return Config{}, xerrors.Errorf("failed to initialize global options: %w", err)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package artifact
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"go.uber.org/zap/zaptest/observer"
|
"go.uber.org/zap/zaptest/observer"
|
||||||
|
|
||||||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfig_Init(t *testing.T) {
|
func TestConfig_Init(t *testing.T) {
|
||||||
@@ -188,7 +188,7 @@ func TestConfig_Init(t *testing.T) {
|
|||||||
ctx := cli.NewContext(app, set, nil)
|
ctx := cli.NewContext(app, set, nil)
|
||||||
_ = set.Parse(tt.args)
|
_ = set.Parse(tt.args)
|
||||||
|
|
||||||
c, err := New(ctx)
|
c, err := NewConfig(ctx)
|
||||||
require.NoError(t, err, err)
|
require.NoError(t, err, err)
|
||||||
|
|
||||||
c.GlobalConfig.Logger = logger.Sugar()
|
c.GlobalConfig.Logger = logger.Sugar()
|
||||||
@@ -204,6 +204,7 @@ func TestConfig_Init(t *testing.T) {
|
|||||||
// test the error
|
// test the error
|
||||||
switch {
|
switch {
|
||||||
case tt.wantErr != "":
|
case tt.wantErr != "":
|
||||||
|
require.NotNil(t, err)
|
||||||
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
|
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/aquasecurity/fanal/analyzer"
|
"github.com/aquasecurity/fanal/analyzer"
|
||||||
"github.com/aquasecurity/fanal/cache"
|
"github.com/aquasecurity/fanal/cache"
|
||||||
"github.com/aquasecurity/trivy/internal/artifact/config"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/scanner"
|
"github.com/aquasecurity/trivy/pkg/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ func filesystemScanner(ctx context.Context, dir string, ac cache.ArtifactCache,
|
|||||||
|
|
||||||
// FilesystemRun runs scan on filesystem
|
// FilesystemRun runs scan on filesystem
|
||||||
func FilesystemRun(cliCtx *cli.Context) error {
|
func FilesystemRun(cliCtx *cli.Context) error {
|
||||||
c, err := config.New(cliCtx)
|
c, err := NewConfig(cliCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/aquasecurity/fanal/analyzer"
|
"github.com/aquasecurity/fanal/analyzer"
|
||||||
"github.com/aquasecurity/fanal/cache"
|
"github.com/aquasecurity/fanal/cache"
|
||||||
"github.com/aquasecurity/trivy/internal/artifact/config"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/scanner"
|
"github.com/aquasecurity/trivy/pkg/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,7 +33,7 @@ func dockerScanner(ctx context.Context, imageName string, ac cache.ArtifactCache
|
|||||||
|
|
||||||
// ImageRun runs scan on docker image
|
// ImageRun runs scan on docker image
|
||||||
func ImageRun(cliCtx *cli.Context) error {
|
func ImageRun(cliCtx *cli.Context) error {
|
||||||
c, err := config.New(cliCtx)
|
c, err := NewConfig(cliCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/aquasecurity/fanal/analyzer"
|
"github.com/aquasecurity/fanal/analyzer"
|
||||||
"github.com/aquasecurity/fanal/cache"
|
"github.com/aquasecurity/fanal/cache"
|
||||||
"github.com/aquasecurity/trivy/internal/artifact/config"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/scanner"
|
"github.com/aquasecurity/trivy/pkg/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +24,7 @@ func repositoryScanner(ctx context.Context, dir string, ac cache.ArtifactCache,
|
|||||||
|
|
||||||
// RepositoryRun runs scan on repository
|
// RepositoryRun runs scan on repository
|
||||||
func RepositoryRun(cliCtx *cli.Context) error {
|
func RepositoryRun(cliCtx *cli.Context) error {
|
||||||
c, err := config.New(cliCtx)
|
c, err := NewConfig(cliCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -12,8 +12,7 @@ import (
|
|||||||
"github.com/aquasecurity/fanal/analyzer"
|
"github.com/aquasecurity/fanal/analyzer"
|
||||||
"github.com/aquasecurity/fanal/cache"
|
"github.com/aquasecurity/fanal/cache"
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
"github.com/aquasecurity/trivy/internal/artifact/config"
|
"github.com/aquasecurity/trivy/pkg/commands/operation"
|
||||||
"github.com/aquasecurity/trivy/internal/operation"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
"github.com/aquasecurity/trivy/pkg/report"
|
"github.com/aquasecurity/trivy/pkg/report"
|
||||||
"github.com/aquasecurity/trivy/pkg/scanner"
|
"github.com/aquasecurity/trivy/pkg/scanner"
|
||||||
@@ -27,14 +26,14 @@ var errSkipScan = errors.New("skip subsequent processes")
|
|||||||
type InitializeScanner func(context.Context, string, cache.ArtifactCache, cache.LocalArtifactCache, time.Duration,
|
type InitializeScanner func(context.Context, string, cache.ArtifactCache, cache.LocalArtifactCache, time.Duration,
|
||||||
[]analyzer.Type) (scanner.Scanner, func(), error)
|
[]analyzer.Type) (scanner.Scanner, func(), error)
|
||||||
|
|
||||||
func run(conf config.Config, initializeScanner InitializeScanner) error {
|
func run(conf Config, initializeScanner InitializeScanner) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return runWithContext(ctx, conf, initializeScanner)
|
return runWithContext(ctx, conf, initializeScanner)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runWithContext(ctx context.Context, conf config.Config, initializeScanner InitializeScanner) error {
|
func runWithContext(ctx context.Context, conf Config, initializeScanner InitializeScanner) error {
|
||||||
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
||||||
l.Fatal(err)
|
l.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -75,7 +74,7 @@ func runWithContext(ctx context.Context, conf config.Config, initializeScanner I
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initCache(c config.Config) (operation.Cache, error) {
|
func initCache(c Config) (operation.Cache, error) {
|
||||||
utils.SetCacheDir(c.CacheDir)
|
utils.SetCacheDir(c.CacheDir)
|
||||||
cache, err := operation.NewCache(c.CacheBackend)
|
cache, err := operation.NewCache(c.CacheBackend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,7 +99,7 @@ func initCache(c config.Config) (operation.Cache, error) {
|
|||||||
return cache, nil
|
return cache, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDB(c config.Config) error {
|
func initDB(c Config) error {
|
||||||
// download the database file
|
// download the database file
|
||||||
noProgress := c.Quiet || c.NoProgress
|
noProgress := c.Quiet || c.NoProgress
|
||||||
if err := operation.DownloadDB(c.AppVersion, c.CacheDir, noProgress, c.Light, c.SkipUpdate); err != nil {
|
if err := operation.DownloadDB(c.AppVersion, c.CacheDir, noProgress, c.Light, c.SkipUpdate); err != nil {
|
||||||
@@ -117,7 +116,7 @@ func initDB(c config.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scan(ctx context.Context, conf config.Config, initializeScanner InitializeScanner, cacheClient cache.Cache) (
|
func scan(ctx context.Context, conf Config, initializeScanner InitializeScanner, cacheClient cache.Cache) (
|
||||||
report.Results, error) {
|
report.Results, error) {
|
||||||
target := conf.Target
|
target := conf.Target
|
||||||
if conf.Input != "" {
|
if conf.Input != "" {
|
||||||
@@ -152,7 +151,7 @@ func scan(ctx context.Context, conf config.Config, initializeScanner InitializeS
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func filter(ctx context.Context, conf config.Config, results report.Results) (report.Results, error) {
|
func filter(ctx context.Context, conf Config, results report.Results) (report.Results, error) {
|
||||||
vulnClient := initializeVulnerabilityClient()
|
vulnClient := initializeVulnerabilityClient()
|
||||||
for i := range results {
|
for i := range results {
|
||||||
vulnClient.FillInfo(results[i].Vulnerabilities, results[i].Type)
|
vulnClient.FillInfo(results[i].Vulnerabilities, results[i].Type)
|
||||||
@@ -166,7 +165,7 @@ func filter(ctx context.Context, conf config.Config, results report.Results) (re
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func exit(c config.Config, results report.Results) {
|
func exit(c Config, results report.Results) {
|
||||||
if c.ExitCode != 0 {
|
if c.ExitCode != 0 {
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
if len(result.Vulnerabilities) > 0 {
|
if len(result.Vulnerabilities) > 0 {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds the Trivy client config
|
// Config holds the Trivy client config
|
||||||
@@ -26,8 +26,8 @@ type Config struct {
|
|||||||
CustomHeaders http.Header
|
CustomHeaders http.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
// New is the factory method for Config
|
// NewConfig is the factory method for Config
|
||||||
func New(c *cli.Context) (Config, error) {
|
func NewConfig(c *cli.Context) (Config, error) {
|
||||||
gc, err := config.NewGlobalConfig(c)
|
gc, err := config.NewGlobalConfig(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Config{}, xerrors.Errorf("failed to initialize global options: %w", err)
|
return Config{}, xerrors.Errorf("failed to initialize global options: %w", err)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"go.uber.org/zap/zaptest/observer"
|
"go.uber.org/zap/zaptest/observer"
|
||||||
|
|
||||||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfig_Init(t *testing.T) {
|
func TestConfig_Init(t *testing.T) {
|
||||||
@@ -228,7 +228,7 @@ func TestConfig_Init(t *testing.T) {
|
|||||||
ctx := cli.NewContext(app, set, nil)
|
ctx := cli.NewContext(app, set, nil)
|
||||||
_ = set.Parse(tt.args)
|
_ = set.Parse(tt.args)
|
||||||
|
|
||||||
c, err := New(ctx)
|
c, err := NewConfig(ctx)
|
||||||
require.NoError(t, err, err)
|
require.NoError(t, err, err)
|
||||||
|
|
||||||
c.GlobalConfig.Logger = logger.Sugar()
|
c.GlobalConfig.Logger = logger.Sugar()
|
||||||
@@ -244,6 +244,7 @@ func TestConfig_Init(t *testing.T) {
|
|||||||
// test the error
|
// test the error
|
||||||
switch {
|
switch {
|
||||||
case tt.wantErr != "":
|
case tt.wantErr != "":
|
||||||
|
require.NotNil(t, err)
|
||||||
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
|
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/aquasecurity/fanal/analyzer"
|
"github.com/aquasecurity/fanal/analyzer"
|
||||||
"github.com/aquasecurity/trivy/internal/client/config"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/cache"
|
"github.com/aquasecurity/trivy/pkg/cache"
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
"github.com/aquasecurity/trivy/pkg/report"
|
"github.com/aquasecurity/trivy/pkg/report"
|
||||||
@@ -20,21 +19,21 @@ import (
|
|||||||
|
|
||||||
// Run runs the scan
|
// Run runs the scan
|
||||||
func Run(cliCtx *cli.Context) error {
|
func Run(cliCtx *cli.Context) error {
|
||||||
c, err := config.New(cliCtx)
|
c, err := NewConfig(cliCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return run(c)
|
return run(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(conf config.Config) error {
|
func run(conf Config) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return runWithContext(ctx, conf)
|
return runWithContext(ctx, conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runWithContext(ctx context.Context, conf config.Config) error {
|
func runWithContext(ctx context.Context, conf Config) error {
|
||||||
if err := initialize(&conf); err != nil {
|
if err := initialize(&conf); err != nil {
|
||||||
return xerrors.Errorf("initialize error: %w", err)
|
return xerrors.Errorf("initialize error: %w", err)
|
||||||
}
|
}
|
||||||
@@ -80,7 +79,7 @@ func runWithContext(ctx context.Context, conf config.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize(conf *config.Config) error {
|
func initialize(conf *Config) error {
|
||||||
// Initialize logger
|
// Initialize logger
|
||||||
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
||||||
return xerrors.Errorf("failed to initialize a logger: %w", err)
|
return xerrors.Errorf("failed to initialize a logger: %w", err)
|
||||||
@@ -98,7 +97,7 @@ func initialize(conf *config.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeScanner(ctx context.Context, conf config.Config) (scanner.Scanner, func(), error) {
|
func initializeScanner(ctx context.Context, conf Config) (scanner.Scanner, func(), error) {
|
||||||
remoteCache := cache.NewRemoteCache(cache.RemoteURL(conf.RemoteAddr), conf.CustomHeaders)
|
remoteCache := cache.NewRemoteCache(cache.RemoteURL(conf.RemoteAddr), conf.CustomHeaders)
|
||||||
|
|
||||||
// By default, apk commands are not analyzed.
|
// By default, apk commands are not analyzed.
|
||||||
@@ -127,7 +126,7 @@ func initializeScanner(ctx context.Context, conf config.Config) (scanner.Scanner
|
|||||||
return s, cleanup, nil
|
return s, cleanup, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func exit(c config.Config, results report.Results) {
|
func exit(c Config, results report.Results) {
|
||||||
if c.ExitCode != 0 {
|
if c.ExitCode != 0 {
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
if len(result.Vulnerabilities) > 0 {
|
if len(result.Vulnerabilities) > 0 {
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCacheConfig(t *testing.T) {
|
func TestNewCacheConfig(t *testing.T) {
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewDBConfig(t *testing.T) {
|
func TestNewDBConfig(t *testing.T) {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewGlobalConfig(t *testing.T) {
|
func TestNewGlobalConfig(t *testing.T) {
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
"github.com/aquasecurity/trivy/pkg/plugin"
|
"github.com/aquasecurity/trivy/pkg/plugin"
|
||||||
)
|
)
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package config
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds the Trivy config
|
// Config holds the Trivy config
|
||||||
@@ -17,8 +17,8 @@ type Config struct {
|
|||||||
TokenHeader string
|
TokenHeader string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New is the factory method to return config
|
// NewConfig is the factory method to return config
|
||||||
func New(c *cli.Context) Config {
|
func NewConfig(c *cli.Context) Config {
|
||||||
// the error is ignored because logger is unnecessary
|
// the error is ignored because logger is unnecessary
|
||||||
gc, _ := config.NewGlobalConfig(c) // nolint: errcheck
|
gc, _ := config.NewGlobalConfig(c) // nolint: errcheck
|
||||||
return Config{
|
return Config{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package config_test
|
package server_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
@@ -8,20 +8,20 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/internal/config"
|
"github.com/aquasecurity/trivy/pkg/commands/config"
|
||||||
c "github.com/aquasecurity/trivy/internal/server/config"
|
"github.com/aquasecurity/trivy/pkg/commands/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
want c.Config
|
want server.Config
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "happy path",
|
name: "happy path",
|
||||||
args: []string{"-quiet", "--no-progress", "--reset", "--skip-update", "--listen", "localhost:8080"},
|
args: []string{"-quiet", "--no-progress", "--reset", "--skip-update", "--listen", "localhost:8080"},
|
||||||
want: c.Config{
|
want: server.Config{
|
||||||
GlobalConfig: config.GlobalConfig{
|
GlobalConfig: config.GlobalConfig{
|
||||||
Quiet: true,
|
Quiet: true,
|
||||||
},
|
},
|
||||||
@@ -49,7 +49,7 @@ func TestNew(t *testing.T) {
|
|||||||
|
|
||||||
tt.want.GlobalConfig.Context = ctx
|
tt.want.GlobalConfig.Context = ctx
|
||||||
|
|
||||||
got := c.New(ctx)
|
got := server.NewConfig(ctx)
|
||||||
assert.Equal(t, tt.want.GlobalConfig.Quiet, got.Quiet, tt.name)
|
assert.Equal(t, tt.want.GlobalConfig.Quiet, got.Quiet, tt.name)
|
||||||
assert.Equal(t, tt.want.DBConfig, got.DBConfig, tt.name)
|
assert.Equal(t, tt.want.DBConfig, got.DBConfig, tt.name)
|
||||||
assert.Equal(t, tt.want.Listen, got.Listen, tt.name)
|
assert.Equal(t, tt.want.Listen, got.Listen, tt.name)
|
||||||
@@ -88,7 +88,7 @@ func TestConfig_Init(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
c := &c.Config{
|
c := &server.Config{
|
||||||
DBConfig: tt.dbConfig,
|
DBConfig: tt.dbConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5,19 +5,18 @@ import (
|
|||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
"github.com/aquasecurity/trivy/internal/operation"
|
"github.com/aquasecurity/trivy/pkg/commands/operation"
|
||||||
"github.com/aquasecurity/trivy/internal/server/config"
|
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
"github.com/aquasecurity/trivy/pkg/rpc/server"
|
rpcServer "github.com/aquasecurity/trivy/pkg/rpc/server"
|
||||||
"github.com/aquasecurity/trivy/pkg/utils"
|
"github.com/aquasecurity/trivy/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run runs the scan
|
// Run runs the scan
|
||||||
func Run(ctx *cli.Context) error {
|
func Run(ctx *cli.Context) error {
|
||||||
return run(config.New(ctx))
|
return run(NewConfig(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(c config.Config) (err error) {
|
func run(c Config) (err error) {
|
||||||
if err = log.InitLogger(c.Debug, c.Quiet); err != nil {
|
if err = log.InitLogger(c.Debug, c.Quiet); err != nil {
|
||||||
return xerrors.Errorf("failed to initialize a logger: %w", err)
|
return xerrors.Errorf("failed to initialize a logger: %w", err)
|
||||||
}
|
}
|
||||||
@@ -53,5 +52,6 @@ func run(c config.Config) (err error) {
|
|||||||
return xerrors.Errorf("error in vulnerability DB initialize: %w", err)
|
return xerrors.Errorf("error in vulnerability DB initialize: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.ListenAndServe(c, cache)
|
server := rpcServer.NewServer(c.AppVersion, c.Listen, c.CacheDir, c.Token, c.TokenHeader)
|
||||||
|
return server.ListenAndServe(cache)
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
|
|
||||||
"github.com/aquasecurity/fanal/cache"
|
"github.com/aquasecurity/fanal/cache"
|
||||||
"github.com/aquasecurity/trivy-db/pkg/db"
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
||||||
"github.com/aquasecurity/trivy/internal/server/config"
|
|
||||||
dbFile "github.com/aquasecurity/trivy/pkg/db"
|
dbFile "github.com/aquasecurity/trivy/pkg/db"
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
"github.com/aquasecurity/trivy/pkg/utils"
|
"github.com/aquasecurity/trivy/pkg/utils"
|
||||||
@@ -28,26 +27,46 @@ var DBWorkerSuperSet = wire.NewSet(
|
|||||||
newDBWorker,
|
newDBWorker,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Server represents Trivy server
|
||||||
|
type Server struct {
|
||||||
|
appVersion string
|
||||||
|
addr string
|
||||||
|
cacheDir string
|
||||||
|
token string
|
||||||
|
tokenHeader string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewServer returns an instance of Server
|
||||||
|
func NewServer(appVersion, addr, cacheDir, token, tokenHeader string) Server {
|
||||||
|
return Server{
|
||||||
|
appVersion: appVersion,
|
||||||
|
addr: addr,
|
||||||
|
cacheDir: cacheDir,
|
||||||
|
token: token,
|
||||||
|
tokenHeader: tokenHeader,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ListenAndServe starts Trivy server
|
// ListenAndServe starts Trivy server
|
||||||
func ListenAndServe(c config.Config, serverCache cache.Cache) error {
|
func (s Server) ListenAndServe(serverCache cache.Cache) error {
|
||||||
requestWg := &sync.WaitGroup{}
|
requestWg := &sync.WaitGroup{}
|
||||||
dbUpdateWg := &sync.WaitGroup{}
|
dbUpdateWg := &sync.WaitGroup{}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
worker := initializeDBWorker(c.CacheDir, true)
|
worker := initializeDBWorker(s.cacheDir, true)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
for {
|
for {
|
||||||
time.Sleep(1 * time.Hour)
|
time.Sleep(1 * time.Hour)
|
||||||
if err := worker.update(ctx, c.AppVersion, c.CacheDir, dbUpdateWg, requestWg); err != nil {
|
if err := worker.update(ctx, s.appVersion, s.cacheDir, dbUpdateWg, requestWg); err != nil {
|
||||||
log.Logger.Errorf("%+v\n", err)
|
log.Logger.Errorf("%+v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
mux := newServeMux(serverCache, dbUpdateWg, requestWg, c.Token, c.TokenHeader)
|
mux := newServeMux(serverCache, dbUpdateWg, requestWg, s.token, s.tokenHeader)
|
||||||
log.Logger.Infof("Listening %s...", c.Listen)
|
log.Logger.Infof("Listening %s...", s.addr)
|
||||||
|
|
||||||
return http.ListenAndServe(c.Listen, mux)
|
return http.ListenAndServe(s.addr, mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServeMux(serverCache cache.Cache, dbUpdateWg, requestWg *sync.WaitGroup, token, tokenHeader string) *http.ServeMux {
|
func newServeMux(serverCache cache.Cache, dbUpdateWg, requestWg *sync.WaitGroup, token, tokenHeader string) *http.ServeMux {
|
||||||
|
|||||||
Reference in New Issue
Block a user