Skip to content

Commit

Permalink
fix: allow the latest tag (aquasecurity#864)
Browse files Browse the repository at this point in the history
* fix: allow the latest tag

* docs: update README
  • Loading branch information
knqyf263 committed Feb 25, 2021
1 parent 425eaf8 commit fcb9a93
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 157 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -234,7 +234,7 @@ $ go install

## Image

Simply specify an image name (and a tag). **The `latest` tag should be avoided as problems occur with the image cache.** See [Clear caches](#clear-caches).
Simply specify an image name (and a tag).

### Basic

Expand Down Expand Up @@ -1405,7 +1405,7 @@ $ trivy server --cache-backend redis://localhost:6379

### Clear caches

The `--clear-cache` option removes caches. This option is useful if the image which has the same tag is updated (such as when using `latest` tag).
The `--clear-cache` option removes caches.

**The scan is not performed.**

Expand Down
8 changes: 1 addition & 7 deletions internal/artifact/config/config.go
Expand Up @@ -46,7 +46,7 @@ func New(c *cli.Context) (Config, error) {
}

// Init initializes the artifact config
func (c *Config) Init(image bool) error {
func (c *Config) Init() error {
if c.onlyUpdate != "" || c.refresh || c.autoRefresh {
c.Logger.Warn("--only-update, --refresh and --auto-refresh are unnecessary and ignored now. These commands will be removed in the next version.")
}
Expand All @@ -64,12 +64,6 @@ func (c *Config) Init(image bool) error {
return err
}

if image {
if err := c.ImageConfig.Init(c.Context.Args(), c.Logger); err != nil {
return err
}
}

return nil
}

Expand Down
26 changes: 1 addition & 25 deletions internal/artifact/config/config_test.go
Expand Up @@ -152,25 +152,6 @@ func TestConfig_Init(t *testing.T) {
},
},
},
{
name: "with latest tag",
args: []string{"--auto-refresh", "gcr.io/distroless/base"},
logs: []string{
"--only-update, --refresh and --auto-refresh are unnecessary and ignored now. These commands will be removed in the next version.",
"You should avoid using the :latest tag as it is cached. You need to specify '--clear-cache' option when :latest image is changed",
},
want: Config{
ReportConfig: config.ReportConfig{
Severities: []dbTypes.Severity{dbTypes.SeverityCritical},
Output: os.Stdout,
VulnType: []string{"os", "library"},
},
ArtifactConfig: config.ArtifactConfig{
Target: "gcr.io/distroless/base",
},
autoRefresh: true,
},
},
{
name: "sad: skip and download db",
args: []string{"--skip-update", "--download-db-only", "alpine:3.10"},
Expand All @@ -184,11 +165,6 @@ func TestConfig_Init(t *testing.T) {
},
wantErr: "arguments error",
},
{
name: "sad: invalid image name",
args: []string{`!"#$%&'()`},
wantErr: "could not parse reference",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -216,7 +192,7 @@ func TestConfig_Init(t *testing.T) {
require.NoError(t, err, err)

c.GlobalConfig.Logger = logger.Sugar()
err = c.Init(true)
err = c.Init()

// tests log messages
var gotMessages []string
Expand Down
2 changes: 1 addition & 1 deletion internal/artifact/fs.go
Expand Up @@ -30,7 +30,7 @@ func FilesystemRun(cliCtx *cli.Context) error {
}

// initialize config
if err = c.Init(false); err != nil {
if err = c.Init(); err != nil {
return xerrors.Errorf("failed to initialize options: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/artifact/image.go
Expand Up @@ -40,7 +40,7 @@ func ImageRun(cliCtx *cli.Context) error {
}

// initialize config
if err := c.Init(true); err != nil {
if err := c.Init(); err != nil {
return xerrors.Errorf("failed to initialize options: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/artifact/repository.go
Expand Up @@ -31,7 +31,7 @@ func RepositoryRun(cliCtx *cli.Context) error {
}

// initialize config
if err = c.Init(false); err != nil {
if err = c.Init(); err != nil {
return xerrors.Errorf("failed to initialize options: %w", err)
}

Expand Down
4 changes: 0 additions & 4 deletions internal/client/config/config.go
Expand Up @@ -67,10 +67,6 @@ func (c *Config) Init() (err error) {
return err
}

if err := c.ImageConfig.Init(c.Context.Args(), c.Logger); err != nil {
return err
}

return nil
}

Expand Down
23 changes: 0 additions & 23 deletions internal/client/config/config_test.go
Expand Up @@ -198,24 +198,6 @@ func TestConfig_Init(t *testing.T) {
CustomHeaders: http.Header{},
},
},
{
name: "with latest tag",
args: []string{"gcr.io/distroless/base"},
logs: []string{
"You should avoid using the :latest tag as it is cached. You need to specify '--clear-cache' option when :latest image is changed",
},
want: Config{
ReportConfig: config.ReportConfig{
Severities: []dbTypes.Severity{dbTypes.SeverityCritical},
Output: os.Stdout,
VulnType: []string{"os", "library"},
},
ArtifactConfig: config.ArtifactConfig{
Target: "gcr.io/distroless/base",
},
CustomHeaders: http.Header{},
},
},
{
name: "sad: multiple image names",
args: []string{"centos:7", "alpine:3.10"},
Expand All @@ -224,11 +206,6 @@ func TestConfig_Init(t *testing.T) {
},
wantErr: "arguments error",
},
{
name: "sad: invalid image name",
args: []string{`!"#$%&'()`},
wantErr: "could not parse reference",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
21 changes: 0 additions & 21 deletions internal/config/image.go
@@ -1,10 +1,7 @@
package config

import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"golang.org/x/xerrors"
)

// ImageConfig holds the config for scanning images
Expand All @@ -20,21 +17,3 @@ func NewImageConfig(c *cli.Context) ImageConfig {
ListAllPkgs: c.Bool("list-all-pkgs"),
}
}

// Init initializes the imageConfig
func (c *ImageConfig) Init(args cli.Args, logger *zap.SugaredLogger) (err error) {
imageName := args.First()

// Check whether 'latest' tag is used
if imageName != "" {
ref, err := name.ParseReference(imageName)
if err != nil {
return xerrors.Errorf("invalid image: %w", err)
}
if ref.Identifier() == "latest" {
logger.Warn("You should avoid using the :latest tag as it is cached. You need to specify '--clear-cache' option when :latest image is changed")
}
}

return nil
}
72 changes: 0 additions & 72 deletions internal/config/image_test.go

This file was deleted.

0 comments on commit fcb9a93

Please sign in to comment.