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

Commit c98e6de

Browse files
Merge pull request #115 from docker/warn-old-snyk-version
Check and warn for old snyk version installed
2 parents 1e69e70 + 1ffe2a6 commit c98e6de

9 files changed

Lines changed: 72 additions & 15 deletions

File tree

Dockerfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ FROM docker:${CLI_VERSION} AS cli
117117
# DOWNLOAD
118118
####
119119
FROM golang:${GO_VERSION} AS download
120-
ARG SNYK_DESKTOP_VERSION=1.332.0
121-
ARG SNYK_USER_VERSION=1.334.0
122120
COPY builder.Makefile vars.mk ./
123121
RUN make -f builder.Makefile download
124122

@@ -128,14 +126,8 @@ RUN make -f builder.Makefile download
128126
FROM builder AS e2e
129127
ARG TARGETOS
130128
ARG TARGETARCH
131-
ARG SNYK_DESKTOP_VERSION=1.332.0
132-
ENV SNYK_DESKTOP_VERSION=${SNYK_DESKTOP_VERSION}
133-
ARG SNYK_USER_VERSION=1.334.0
134-
ENV SNYK_USER_VERSION=${SNYK_USER_VERSION}
135129
ARG TAG_NAME
136130
ENV TAG_NAME=$TAG_NAME
137-
ENV SNYK_USER_PATH="/root/e2e"
138-
ENV SNYK_DESKTOP_PATH="/root/.docker/scan"
139131
ENV DOCKER_CONFIG="/root/.docker"
140132

141133
# install snyk binaries

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
include vars.mk
1515
export DOCKER_BUILDKIT=1
1616

17-
BUILD_ARGS := --build-arg SNYK_DESKTOP_VERSION=$(SNYK_DESKTOP_VERSION)\
18-
--build-arg SNYK_USER_VERSION=$(SNYK_USER_VERSION)\
19-
--build-arg GO_VERSION=$(GO_VERSION)\
17+
BUILD_ARGS := --build-arg GO_VERSION=$(GO_VERSION)\
2018
--build-arg CLI_VERSION=$(CLI_VERSION)\
2119
--build-arg ALPINE_VERSION=$(ALPINE_VERSION)\
2220
--build-arg GOLANGCI_LINT_VERSION=$(GOLANGCI_LINT_VERSION) \

builder.Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ endif
3636
VARS:= SNYK_DESKTOP_VERSION=${SNYK_DESKTOP_VERSION}\
3737
SNYK_USER_VERSION=${SNYK_USER_VERSION}\
3838
DOCKER_CONFIG=$(PWD)/docker-config\
39+
SNYK_OLD_PATH=$(PWD)/docker-config/snyk-old\
3940
SNYK_USER_PATH=$(PWD)/docker-config/snyk-user\
4041
SNYK_DESKTOP_PATH=$(PWD)/docker-config/snyk-desktop
4142

@@ -72,6 +73,10 @@ download:
7273
curl https://github.com/snyk/snyk/releases/download/v${SNYK_USER_VERSION}/${SNYK_DOWNLOAD_NAME} -L -s -S -o docker-config/snyk-user/${SNYK_BINARY}
7374
chmod +x docker-config/snyk-user/${SNYK_BINARY}
7475

76+
mkdir -p docker-config/snyk-old
77+
curl https://github.com/snyk/snyk/releases/download/v${SNYK_OLD_VERSION}/${SNYK_DOWNLOAD_NAME} -L -s -S -o docker-config/snyk-old/${SNYK_BINARY}
78+
chmod +x docker-config/snyk-old/${SNYK_BINARY}
79+
7580
mkdir -p docker-config/snyk-desktop
7681
curl https://github.com/snyk/snyk/releases/download/v${SNYK_DESKTOP_VERSION}/${SNYK_DOWNLOAD_NAME} -L -s -S -o docker-config/snyk-desktop/${SNYK_BINARY}
7782
chmod +x docker-config/snyk-desktop/${SNYK_BINARY}

e2e/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
func TestSnykAuthentication(t *testing.T) {
3232
// Add snyk binary to the path
3333
path := os.Getenv("PATH")
34-
defer env.Patch(t, "PATH", fmt.Sprintf(pathFormat(), os.Getenv("SNYK_USER_PATH"), path))()
34+
defer env.Patch(t, "PATH", fmt.Sprintf(pathFormat(), os.Getenv("SNYK_DESKTOP_PATH"), path))()
3535

3636
// create Snyk config file with empty token
3737
homeDir, cleanFunction := createSnykConfFile(t, "")

e2e/version_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ Provider: %s
5050
assert.Equal(t, output, expected)
5151
}
5252

53+
func TestVersionSnykOldBinary(t *testing.T) {
54+
// Add old snyk binary to the $PATH
55+
path := os.Getenv("PATH")
56+
defer env.Patch(t, "PATH", fmt.Sprintf(pathFormat(), os.Getenv("SNYK_OLD_PATH"), path))()
57+
58+
cmd, configDir, cleanup := dockerCli.createTestCmd()
59+
defer cleanup()
60+
61+
createScanConfigFile(t, configDir)
62+
63+
// docker scan --version should fallback to desktop's Snyk binary and print a message on
64+
// stderr stating that the user should upgrade Snyk.
65+
cmd.Command = dockerCli.Command("scan", "--version")
66+
output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
67+
expected := fmt.Sprintf(
68+
`Version: %s
69+
Git commit: %s
70+
Provider: %s
71+
The Snyk version installed on your system does not match the docker scan requirements (>=1.385.0), using embedded Snyk version instead.
72+
`, internal.Version, internal.GitCommit, getProviderVersion("SNYK_DESKTOP_VERSION"))
73+
74+
assert.Equal(t, output, expected)
75+
}
76+
5377
func TestVersionSnykDesktopBinary(t *testing.T) {
5478
cmd, configDir, cleanup := dockerCli.createTestCmd()
5579
defer cleanup()

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
7+
github.com/Masterminds/semver/v3 v3.1.0
78
github.com/Microsoft/hcsshim v0.8.9 // indirect
89
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
910
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
77
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
88
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
99
github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0=
10+
github.com/Masterminds/semver/v3 v3.1.0 h1:Y2lUDsFKVRSYGojLJ1yLxSXdMmMYTYls0rCvoqmMUQk=
11+
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
1012
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 h1:ygIc8M6trr62pF5DucadTWGdEB4mEyvzi0e2nbcmcyA=
1113
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw=
1214
github.com/Microsoft/hcsshim v0.8.9 h1:VrfodqvztU8YSOvygU+DN1BGaSGxmrNfqOv5oOuX2Bk=

internal/provider/snyk.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"path/filepath"
2828
"strings"
2929

30+
"github.com/Masterminds/semver/v3"
3031
"github.com/docker/docker/api/types"
3132
"github.com/docker/docker/api/types/registry"
3233
"github.com/docker/scan-cli-plugin/internal/authentication"
@@ -35,6 +36,10 @@ import (
3536
"github.com/mitchellh/go-homedir"
3637
)
3738

39+
const (
40+
minimalSnykVersion = ">=1.385.0"
41+
)
42+
3843
type snykProvider struct {
3944
path string
4045
flags []string
@@ -69,7 +74,7 @@ func WithContext(ctx context.Context) SnykProviderOps {
6974
// WithPath update the Snyk provider with the path from the configuration
7075
func WithPath(path string) SnykProviderOps {
7176
return func(provider *snykProvider) error {
72-
if p, err := exec.LookPath("snyk"); err == nil {
77+
if p, err := exec.LookPath("snyk"); err == nil && checkUserSnykBinaryVersion(p) {
7378
path = p
7479
}
7580
provider.path = path
@@ -221,3 +226,32 @@ func isAuthenticatedOnSnyk() (bool, error) {
221226

222227
return config.API != "", nil
223228
}
229+
230+
func checkUserSnykBinaryVersion(path string) bool {
231+
cmd := exec.Command(path, "--version")
232+
buff := bytes.NewBuffer(nil)
233+
cmd.Stdout = buff
234+
cmd.Stderr = ioutil.Discard
235+
if err := cmd.Run(); err != nil {
236+
// an error occurred, so let's use the desktop binary
237+
return false
238+
}
239+
ver, err := semver.NewVersion(cleanVersion(buff.String()))
240+
if err != nil {
241+
return false
242+
}
243+
constraint, err := semver.NewConstraint(minimalSnykVersion)
244+
if err != nil {
245+
return false
246+
}
247+
matchConstraint := constraint.Check(ver)
248+
if !matchConstraint {
249+
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)
250+
}
251+
return matchConstraint
252+
}
253+
254+
func cleanVersion(version string) string {
255+
version = strings.TrimSpace(version)
256+
return strings.Split(version, " ")[0]
257+
}

vars.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Pinned Versions
2-
SNYK_DESKTOP_VERSION=1.383.1
3-
SNYK_USER_VERSION=1.382.1
2+
SNYK_DESKTOP_VERSION=1.385.0
3+
SNYK_USER_VERSION=1.385.1
4+
SNYK_OLD_VERSION=1.382.1
45
GO_VERSION=1.15.0
56
CLI_VERSION=19.03.9
67
ALPINE_VERSION=3.12.0

0 commit comments

Comments
 (0)