From e772be5f6f75af54bff1c2febd3c863308d53956 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Wed, 19 Apr 2017 16:23:23 -0400 Subject: [PATCH] contrib: only extract layers from history This tool was written before v2 existed and deduplicated and listed layers out of order in their manifests. --- contrib/analyze-local-images/main.go | 34 ++-------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/contrib/analyze-local-images/main.go b/contrib/analyze-local-images/main.go index f3c836b8..10e571a6 100644 --- a/contrib/analyze-local-images/main.go +++ b/contrib/analyze-local-images/main.go @@ -154,10 +154,7 @@ func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, m // Retrieve history. log.Println("Retrieving image history") - layerIDs, err := historyFromManifest(tmpPath) - if err != nil { - layerIDs, err = historyFromCommand(imageName) - } + layerIDs, err = historyFromCommand(imageName) if err != nil || len(layerIDs) == 0 { return fmt.Errorf("Could not get image's history: %s", err) } @@ -272,7 +269,7 @@ func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, m fmt.Printf("%s No vulnerabilities matching the minimum severity level were detected in your image\n", color.YellowString("NOTE:")) } else { return fmt.Errorf("A total of %d vulnerabilities have been detected in your image", len(vulnerabilities)) - } + } return nil } @@ -309,33 +306,6 @@ func save(imageName, path string) error { return nil } -func historyFromManifest(path string) ([]string, error) { - mf, err := os.Open(path + "/manifest.json") - if err != nil { - return nil, err - } - defer mf.Close() - - // https://github.com/docker/docker/blob/master/image/tarexport/tarexport.go#L17 - type manifestItem struct { - Config string - RepoTags []string - Layers []string - } - - var manifest []manifestItem - if err = json.NewDecoder(mf).Decode(&manifest); err != nil { - return nil, err - } else if len(manifest) != 1 { - return nil, err - } - var layers []string - for _, layer := range manifest[0].Layers { - layers = append(layers, strings.TrimSuffix(layer, "/layer.tar")) - } - return layers, nil -} - func historyFromCommand(imageName string) ([]string, error) { var stderr bytes.Buffer cmd := exec.Command("docker", "history", "-q", "--no-trunc", imageName)