Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit bd851dc

Browse files
Merge pull request #190 from docker/check-local-snyk-version
Use Snyk binary embedded with Docker Desktop if more recent than loca…
2 parents 16a6913 + 26089d8 commit bd851dc

5 files changed

Lines changed: 31 additions & 16 deletions

File tree

builder.Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ STATIC_FLAGS= CGO_ENABLED=0
1515
LDFLAGS := "-s -w \
1616
-X $(PKG_NAME)/internal.GitCommit=$(COMMIT) \
1717
-X $(PKG_NAME)/internal.Version=$(TAG_NAME) \
18-
-X $(PKG_NAME)/internal/provider.ImageDigest=$(SNYK_IMAGE_DIGEST)"
18+
-X $(PKG_NAME)/internal/provider.ImageDigest=$(SNYK_IMAGE_DIGEST) \
19+
-X $(PKG_NAME)/internal/provider.SnykDesktopVersion=$(SNYK_DESKTOP_VERSION)"
1920
GO_BUILD = $(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
2021

2122
SNYK_DOWNLOAD_NAME:=snyk-linux
@@ -36,6 +37,7 @@ endif
3637

3738
VARS:= SNYK_DESKTOP_VERSION=${SNYK_DESKTOP_VERSION}\
3839
SNYK_USER_VERSION=${SNYK_USER_VERSION}\
40+
SNYK_OLD_VERSION=${SNYK_OLD_VERSION}\
3941
DOCKER_CONFIG=$(PWD)/docker-config\
4042
SNYK_OLD_PATH=$(PWD)/docker-config/snyk-old\
4143
SNYK_USER_PATH=$(PWD)/docker-config/snyk-user\

e2e/version_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"runtime"
2323
"testing"
2424

25+
"github.com/docker/scan-cli-plugin/internal/provider"
2526
"gotest.tools/v3/assert"
2627
is "gotest.tools/v3/assert/cmp"
2728
"gotest.tools/v3/env"
@@ -67,11 +68,13 @@ func TestVersionSnykOldBinary(t *testing.T) {
6768
cmd.Command = dockerCli.Command("scan", "--version")
6869
output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
6970
expected := fmt.Sprintf(
70-
`Version: %s
71+
`the Snyk version %s installed on your system is older as the one embedded by Docker Desktop (>=%s), using embedded Snyk version instead
72+
73+
Version: %s
7174
Git commit: %s
7275
Provider: %s
73-
The Snyk version installed on your system does not match the docker scan requirements (>=1.385.0), using embedded Snyk version instead.
74-
`, internal.Version, internal.GitCommit, getProviderVersion("SNYK_DESKTOP_VERSION"))
76+
`, os.Getenv("SNYK_OLD_VERSION"), provider.SnykDesktopVersion,
77+
internal.Version, internal.GitCommit, getProviderVersion("SNYK_DESKTOP_VERSION"))
7578

7679
assert.Equal(t, output, expected)
7780
}

internal/provider/provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ func WithAppVulns() Ops {
164164
// WithPath update the provider binary with the path from the configuration
165165
func WithPath(path string) Ops {
166166
return func(provider *Options) error {
167-
if p, err := exec.LookPath("snyk"); err == nil && checkUserSnykBinaryVersion(p) {
168-
path = p
167+
if p, err := exec.LookPath("snyk"); err == nil {
168+
if ok, err := checkUserSnykBinaryVersion(p); ok {
169+
path = p
170+
} else {
171+
fmt.Printf("%s\n\n", err.Error())
172+
}
169173
}
170174
provider.path = path
171175
return nil

internal/provider/snyk.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131
"github.com/mitchellh/go-homedir"
3232
)
3333

34-
const (
35-
minimalSnykVersion = ">=1.385.0"
34+
var (
35+
// SnykDesktopVersion is the version of the Snyk CLI Binary embedded with Docker Desktop
36+
SnykDesktopVersion = "unknown"
3637
)
3738

3839
type snykProvider struct {
@@ -156,31 +157,36 @@ func isAuthenticatedOnSnyk() (string, error) {
156157
return config.API, nil
157158
}
158159

159-
func checkUserSnykBinaryVersion(path string) bool {
160+
func checkUserSnykBinaryVersion(path string) (bool, error) {
160161
cmd := exec.Command(path, "--version")
161162
buff := bytes.NewBuffer(nil)
162163
cmd.Stdout = buff
163164
cmd.Stderr = ioutil.Discard
164165
if err := cmd.Run(); err != nil {
165166
// an error occurred, so let's use the desktop binary
166-
return false
167+
return false, err
167168
}
168169
ver, err := semver.NewVersion(cleanVersion(buff.String()))
169170
if err != nil {
170-
return false
171+
return false, err
171172
}
172-
constraint, err := semver.NewConstraint(minimalSnykVersion)
173+
constraint, err := semver.NewConstraint(minimalSnykVersion())
173174
if err != nil {
174-
return false
175+
return false, err
175176
}
176177
matchConstraint := constraint.Check(ver)
177178
if !matchConstraint {
178-
fmt.Fprintf(os.Stderr, "The Snyk version installed on your system does not match the docker scan requirements (%s), using embedded Snyk version instead.\n", minimalSnykVersion)
179+
return matchConstraint, fmt.Errorf("the Snyk version %s installed on your system is older as the one embedded by Docker Desktop (%s), using embedded Snyk version instead",
180+
ver, minimalSnykVersion())
179181
}
180-
return matchConstraint
182+
return matchConstraint, nil
181183
}
182184

183185
func cleanVersion(version string) string {
184186
version = strings.TrimSpace(version)
185187
return strings.Split(version, " ")[0]
186188
}
189+
190+
func minimalSnykVersion() string {
191+
return fmt.Sprintf(">=%s", SnykDesktopVersion)
192+
}

vars.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Pinned Versions
22
SNYK_DESKTOP_VERSION=1.801.0
3-
SNYK_USER_VERSION=1.460.0
3+
SNYK_USER_VERSION=1.806.0
44
SNYK_OLD_VERSION=1.382.1
55
# Digest of the 1.801.0 snyk/snyk:docker image
66
SNYK_IMAGE_DIGEST=sha256:bf3b57416af416edec358a184a01f2437f70502bebf0c7a8e787cf68bd6ba428

0 commit comments

Comments
 (0)