mirror of https://github.com/docker/compose.git
Cleanup tips from output
The scan tip has been shown for two years, and most users will know about it by now. Presenting the message also involved checking if the plugin was installed, and wether or not the message was shown before, which also caused some overhead, so cleaning up the output a bit. The corresponding DOCKER_SCAN_SUGGEST environment-variable is also removed with this. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
c80d52aded
commit
87a0a57f70
|
@ -48,8 +48,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||
|
||||
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) error {
|
||||
opts := map[string]build.Options{}
|
||||
var imagesToBuild []string
|
||||
|
||||
args := flatten(options.Args.Resolve(envResolver(project.Environment)))
|
||||
|
||||
services, err := project.GetServices(options.Services...)
|
||||
|
@ -62,7 +60,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
|
|||
continue
|
||||
}
|
||||
imageName := api.GetImageNameOrDefault(service, project.Name)
|
||||
imagesToBuild = append(imagesToBuild, imageName)
|
||||
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -97,12 +94,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
|
|||
}
|
||||
|
||||
_, err = s.doBuild(ctx, project, opts, options.Progress)
|
||||
if err == nil {
|
||||
if len(imagesToBuild) > 0 && !options.Quiet {
|
||||
utils.DisplayScanSuggestMsg()
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -136,9 +127,6 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||
return err
|
||||
}
|
||||
|
||||
if len(builtImages) > 0 {
|
||||
utils.DisplayScanSuggestMsg()
|
||||
}
|
||||
for name, digest := range builtImages {
|
||||
images[name] = digest
|
||||
}
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
/*
|
||||
Copyright 2020 Docker Compose CLI authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/compose/v2/pkg/utils"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
"gotest.tools/v3/icmd"
|
||||
)
|
||||
|
||||
func TestDisplayScanMessageAfterBuild(t *testing.T) {
|
||||
c := NewParallelCLI(t)
|
||||
|
||||
// assert docker scan plugin is available
|
||||
c.RunDockerOrExitError(t, "scan", "--help")
|
||||
|
||||
t.Run("display on compose build", func(t *testing.T) {
|
||||
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p",
|
||||
"scan-msg-test-compose-build", "build")
|
||||
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-compose-build-nginx")
|
||||
res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
|
||||
})
|
||||
|
||||
t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
|
||||
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-quiet",
|
||||
"build", "--quiet")
|
||||
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
|
||||
res = c.RunDockerCmd(t, "rmi", "-f", "scan-msg-test-quiet-nginx")
|
||||
assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))
|
||||
|
||||
res = c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-q",
|
||||
"build", "-q")
|
||||
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-q-nginx")
|
||||
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
|
||||
})
|
||||
|
||||
_ = c.RunDockerOrExitError(t, "rmi", "scan-msg-test-nginx")
|
||||
|
||||
t.Run("display on compose up if image is built", func(t *testing.T) {
|
||||
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up",
|
||||
"-d")
|
||||
defer c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down")
|
||||
res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
|
||||
})
|
||||
|
||||
t.Run("do not display on compose up if no image built", func(t *testing.T) { // re-run the same Compose aproject
|
||||
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up",
|
||||
"-d")
|
||||
defer c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down", "--rmi", "all")
|
||||
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
|
||||
})
|
||||
|
||||
t.Run("do not display if scan already invoked", func(t *testing.T) {
|
||||
_ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0o755)
|
||||
scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json")
|
||||
err := os.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0o644)
|
||||
assert.NilError(t, err)
|
||||
|
||||
res := c.RunDockerCmd(t, "build", "-t", "test-image-scan-msg", "fixtures/simple-build-test/nginx-build")
|
||||
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
|
||||
})
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
/*
|
||||
Copyright 2020 Docker Compose CLI authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
pluginmanager "github.com/docker/cli/cli-plugins/manager"
|
||||
"github.com/docker/cli/cli/command"
|
||||
cliConfig "github.com/docker/cli/cli/config"
|
||||
)
|
||||
|
||||
// ScanSuggestMsg display a message after a successful build to suggest use of `docker scan` command
|
||||
const ScanSuggestMsg = "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"
|
||||
|
||||
// DisplayScanSuggestMsg displlay a message suggesting users can scan new image
|
||||
func DisplayScanSuggestMsg() {
|
||||
if os.Getenv("DOCKER_SCAN_SUGGEST") == "false" {
|
||||
return
|
||||
}
|
||||
if !scanAvailable() {
|
||||
return
|
||||
}
|
||||
if scanAlreadyInvoked() {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "\n"+ScanSuggestMsg+"\n")
|
||||
}
|
||||
|
||||
func scanAlreadyInvoked() bool {
|
||||
filename := filepath.Join(cliConfig.Dir(), "scan", "config.json")
|
||||
f, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
if f.IsDir() { // should never happen, do not bother user with suggestion if something goes wrong
|
||||
return true
|
||||
}
|
||||
type scanOptin struct {
|
||||
Optin bool `json:"optin"`
|
||||
}
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
scanConfig := scanOptin{}
|
||||
err = json.Unmarshal(data, &scanConfig)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
return scanConfig.Optin
|
||||
}
|
||||
|
||||
func scanAvailable() bool {
|
||||
cli, err := command.NewDockerCli()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
plugins, err := pluginmanager.ListPlugins(cli, nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for _, plugin := range plugins {
|
||||
if plugin.Name == "scan" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue