//Push image to Clair for analysis func Push(image Image) error { layerCount := len(image.FsLayers) parentID := "" if layerCount == 0 { logrus.Warningln("there is no layer to push") } localIP, err := config.LocalServerIP() if err != nil { return err } hURL := fmt.Sprintf("http://%v/v2", localIP) if IsLocal { hURL = strings.Replace(hURL, "/v2", "/local", -1) logrus.Infof("using %v as local url", hURL) } for index, layer := range image.FsLayers { lUID := xstrings.Substr(layer.BlobSum, 0, 12) logrus.Infof("Pushing Layer %d/%d [%v]", index+1, layerCount, lUID) database.InsertRegistryMapping(layer.BlobSum, image.Registry) payload := v1.LayerEnvelope{Layer: &v1.Layer{ Name: layer.BlobSum, Path: image.BlobsURI(layer.BlobSum), ParentName: parentID, Format: "Docker", }} //FIXME Update to TLS if IsLocal { payload.Layer.Name = layer.History payload.Layer.Path += "/layer.tar" } payload.Layer.Path = strings.Replace(payload.Layer.Path, image.Registry, hURL, 1) if err := clair.Push(payload); err != nil { logrus.Infof("adding layer %d/%d [%v]: %v", index+1, layerCount, lUID, err) if err != clair.OSNotSupported { return err } parentID = "" } else { parentID = payload.Layer.Name } } if IsLocal { if err := cleanLocal(); err != nil { return err } } return nil }
//Analyse return Clair Image analysis func Analyse(image Image) clair.ImageAnalysis { c := len(image.FsLayers) res := []v1.LayerEnvelope{} for i := range image.FsLayers { l := image.FsLayers[c-i-1].BlobSum lShort := xstrings.Substr(l, 0, 12) if a, err := clair.Analyse(l); err != nil { logrus.Infof("analysing layer [%v] %d/%d: %v", lShort, i+1, c, err) } else { logrus.Infof("analysing layer [%v] %d/%d", lShort, i+1, c) res = append(res, a) } } return clair.ImageAnalysis{ Registry: xstrings.TrimPrefixSuffix(image.Registry, "http://", "/v2"), ImageName: image.Name, Tag: image.Tag, Layers: res, } }
func (imageAnalysis ImageAnalysis) ShortName(l v1.Layer) string { return xstrings.Substr(l.Name, 0, 12) }