From 30631eea03429d9662763f1ad49b739417fb3fd4 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 24 Feb 2021 17:21:10 +0100 Subject: [PATCH] scan message in compose build and compose up Signed-off-by: Guillaume Tardif --- local/compose/build.go | 36 +++++++++++++------ .../compose/fixtures/build-test/compose.yml | 8 ++++- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/local/compose/build.go b/local/compose/build.go index 1e35befd4..a15246d06 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -34,18 +34,37 @@ import ( func (s *composeService) Build(ctx context.Context, project *types.Project) error { opts := map[string]build.Options{} + imagesToBuild := []string{} for _, service := range project.Services { if service.Build != nil { imageName := getImageName(service, project.Name) + imagesToBuild = append(imagesToBuild, imageName) opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) } } - return s.build(ctx, project, opts) + err := s.build(ctx, project, opts) + if err == nil { + displayScanMessage(imagesToBuild) + } + + return err +} + +func displayScanMessage(builtImages []string) { + if len(builtImages) > 0 { + commands := []string{} + for _, image := range builtImages { + commands = append(commands, fmt.Sprintf("docker scan %s", image)) + } + allCommands := strings.Join(commands, ", ") + fmt.Printf("Try scanning the image you have just built to identify vulnerabilities with Docker’s new security tool: %s\n", allCommands) + } } func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error { opts := map[string]build.Options{} + imagesToBuild := []string{} for _, service := range project.Services { if service.Image == "" && service.Build == nil { return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name) @@ -66,6 +85,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. if localImagePresent && service.PullPolicy != types.PullPolicyBuild { continue } + imagesToBuild = append(imagesToBuild, imageName) opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) continue } @@ -84,7 +104,11 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. } - return s.build(ctx, project, opts) + err := s.build(ctx, project, opts) + if err == nil { + displayScanMessage(imagesToBuild) + } + return err } func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) { @@ -127,14 +151,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts if err == nil { err = errW } - if err == nil { - commands := make([]string, 0, len(opts)) - for image, _ := range opts { - commands = append(commands, fmt.Sprintf("docker scan %s", image)) - } - allCommands := strings.Join(commands, ", ") - fmt.Printf("Try scanning the image you have just built to identify vulnerabilities with Docker’s new security tool: %s\n", allCommands) - } return err } diff --git a/local/e2e/compose/fixtures/build-test/compose.yml b/local/e2e/compose/fixtures/build-test/compose.yml index 1ccd3234e..8cf112fbe 100644 --- a/local/e2e/compose/fixtures/build-test/compose.yml +++ b/local/e2e/compose/fixtures/build-test/compose.yml @@ -6,4 +6,10 @@ services: nginx2: build: nginx-build - image: gtardif/custom-nginx + image: gtardif/custom-nginx:1.0 + + mongo: + image: mongo + + sentences: + image: gtardif/sentences-web \ No newline at end of file