Skip to content

Commit ee24c0d

Browse files
authored
fix: improve handling if docker exits with a non-zero code when trying to scan images (google#1285)
By capturing `stderr` and outputting it as an error when `docker` exits with a non-zero code, it should make it easier to catch and debug issues such as unsupported images, images that do not exist, or cannot be accessed due to lack of authentication. Currently this just assumes the output from Docker will be helpful enough since we're not able to rely on a particular structure that'd let us parse and understand the actual error, but it should still be a lot better then the current behaviour of saying the docker image was scanned with no packages being found (which we do still do as changing that would be more complex). Because we've not got any tests for this, here's some manual testing: ``` osv-scanner on  improve/docker-error-output [$?] via 🐹 v1.22.7 via  v20.11.0 took 4s ❯ osv-scanner --docker something --docker node:alpine Scanned docker image with 0 packages Docker command exited with code 125 > Unable to find image 'something:latest' locally > docker: Error response from daemon: pull access denied for something, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. > See 'docker run --help'. Scanned docker image with 0 packages Docker command exited with code 127 > docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/usr/bin/dpkg-query": stat /usr/bin/dpkg-query: no such file or directory: unknown. No package sources found, --help for usage information. ``` Note that since we're using `r.Errorf` this also means the scanner exits with a non-zero code. Resolves google#119
1 parent e963fef commit ee24c0d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

pkg/osvscanner/osvscanner.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,35 @@ func scanDebianDocker(r reporter.Reporter, dockerImageName string) ([]scannedPac
618618
r.Errorf("Failed to get stdout: %s\n", err)
619619
return nil, err
620620
}
621+
stderr, err := cmd.StderrPipe()
622+
623+
if err != nil {
624+
r.Errorf("Failed to get stderr: %s\n", err)
625+
return nil, err
626+
}
627+
621628
err = cmd.Start()
622629
if err != nil {
623630
r.Errorf("Failed to start docker image: %s\n", err)
624631
return nil, err
625632
}
626-
// TODO: Do error checking here
627-
//nolint:errcheck
628-
defer cmd.Wait()
633+
defer func() {
634+
var stderrlines []string
635+
636+
scanner := bufio.NewScanner(stderr)
637+
for scanner.Scan() {
638+
stderrlines = append(stderrlines, scanner.Text())
639+
}
640+
641+
err := cmd.Wait()
642+
if err != nil {
643+
r.Errorf("Docker command exited with code %d\n", cmd.ProcessState.ExitCode())
644+
for _, line := range stderrlines {
645+
r.Errorf("> %s\n", line)
646+
}
647+
}
648+
}()
649+
629650
scanner := bufio.NewScanner(stdout)
630651
var packages []scannedPackage
631652
for scanner.Scan() {

0 commit comments

Comments
 (0)