-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from arminc/refactoring
Refactoring
- Loading branch information
Showing
12 changed files
with
520 additions
and
374 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/coreos/clair/api/v1" | ||
) | ||
|
||
func analyzeLayers(layerIds []string, clairURL string, scannerIP string) { | ||
tmpPath := "http://" + scannerIP + ":" + httpPort | ||
|
||
for i := 0; i < len(layerIds); i++ { | ||
Logger.Infof("Analyzing %s", layerIds[i]) | ||
|
||
if i > 0 { | ||
analyzeLayer(clairURL, tmpPath+"/"+layerIds[i]+"/layer.tar", layerIds[i], layerIds[i-1]) | ||
} else { | ||
analyzeLayer(clairURL, tmpPath+"/"+layerIds[i]+"/layer.tar", layerIds[i], "") | ||
} | ||
} | ||
} | ||
|
||
func analyzeLayer(clairURL, path, layerName, parentLayerName string) { | ||
payload := v1.LayerEnvelope{ | ||
Layer: &v1.Layer{ | ||
Name: layerName, | ||
Path: path, | ||
ParentName: parentLayerName, | ||
Format: "Docker", | ||
}, | ||
} | ||
jsonPayload, err := json.Marshal(payload) | ||
if err != nil { | ||
Logger.Fatalf("Could not analyze layer, payload is not json %s", err) | ||
} | ||
|
||
request, err := http.NewRequest("POST", clairURL+postLayerURI, bytes.NewBuffer(jsonPayload)) | ||
if err != nil { | ||
Logger.Fatalf("Could not analyze layer, could not prepare request for Clair %s", err) | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
client := &http.Client{} | ||
response, err := client.Do(request) | ||
if err != nil { | ||
Logger.Fatalf("Could not analyze layer, POST to Clair failed %s", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != 201 { | ||
body, _ := ioutil.ReadAll(response.Body) | ||
Logger.Fatalf("Could not analyze layer, Clair responded with a failure: Got response %d with message %s", response.StatusCode, string(body)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/docker/docker/client" | ||
) | ||
|
||
// TODO Add support for older version of docker | ||
|
||
type manifestJson struct { | ||
Layers []string | ||
} | ||
|
||
// saveDockerImage saves Docker image to temorary folder | ||
func saveDockerImage(imageName string, tmpPath string) { | ||
docker := createDockerClient() | ||
|
||
imageReader, err := docker.ImageSave(context.Background(), []string{imageName}) | ||
if err != nil { | ||
Logger.Fatalf("Could not save Docker image [%v] : %v", imageName, err) | ||
} | ||
|
||
defer imageReader.Close() | ||
|
||
if err = untar(imageReader, tmpPath); err != nil { | ||
Logger.Fatalf("Could not save Docker image, could not untar [%v] : %v", imageName, err) | ||
} | ||
} | ||
|
||
func createDockerClient() client.APIClient { | ||
docker, err := client.NewEnvClient() | ||
if err != nil { | ||
Logger.Fatalf("Could not create a Docker client: %v", err) | ||
} | ||
return docker | ||
} | ||
|
||
// TODO make a test | ||
func getImageLayerIds(path string) []string { | ||
manifest := readManifestFile(path) | ||
|
||
var layers []string | ||
for _, layer := range manifest[0].Layers { | ||
layers = append(layers, strings.TrimSuffix(layer, "/layer.tar")) | ||
} | ||
return layers | ||
} | ||
|
||
func readManifestFile(path string) []manifestJson { | ||
manifestFile := path + "/manifest.json" | ||
mf, err := os.Open(manifestFile) | ||
if err != nil { | ||
Logger.Fatalf("Could not read Docker image layers, could not open [%v]: %v", manifestFile, err) | ||
} | ||
defer mf.Close() | ||
|
||
return parseAndValidateManifestFile(mf) | ||
} | ||
|
||
func parseAndValidateManifestFile(manifestFile io.Reader) []manifestJson { | ||
var manifest []manifestJson | ||
if err := json.NewDecoder(manifestFile).Decode(&manifest); err != nil { | ||
Logger.Fatalf("Could not read Docker image layers, manifest.json is not json: %v", err) | ||
} else if len(manifest) != 1 { | ||
Logger.Fatalf("Could not read Docker image layers, manifest.json is not valid") | ||
} else if len(manifest[0].Layers) == 0 { | ||
Logger.Fatalf("Could not read Docker image layers, no layers can be found") | ||
} | ||
return manifest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
images: | ||
alpine: | ||
CVE-2016-9840: zlib | ||
CVE-2016-9841: zlib | ||
CVE-2016-9842: zlib | ||
CVE-2016-9843: zlib |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.