From 6b207b0cf94089bd50569ba71c117161e680ee2b Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Mon, 17 Sep 2018 13:34:58 -0400 Subject: [PATCH] fix v3 when grpcConn is nil fixes #138 Signed-off-by: Jess Frazelle --- clair/ancestry.go | 8 ++++++++ handlers.go | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clair/ancestry.go b/clair/ancestry.go index 6cb3bde1..86d01345 100644 --- a/clair/ancestry.go +++ b/clair/ancestry.go @@ -35,8 +35,16 @@ func (c *Clair) GetAncestry(name string) (*clairpb.GetAncestryResponse_Ancestry, func (c *Clair) PostAncestry(name string, layers []*clairpb.PostAncestryRequest_PostLayer) error { c.Logf("clair.ancestry.post name=%s", name) + if c.grpcConn == nil { + return errors.New("grpcConn cannot be nil") + } + client := clairpb.NewAncestryServiceClient(c.grpcConn) + if client == nil { + return errors.New("could not establish connection to grpc clair api") + } + resp, err := client.PostAncestry(context.Background(), &clairpb.PostAncestryRequest{ AncestryName: name, Layers: layers, diff --git a/handlers.go b/handlers.go index 02642c1c..82bcfa17 100644 --- a/handlers.go +++ b/handlers.go @@ -260,17 +260,33 @@ func (rc *registryController) vulnerabilitiesHandler(w http.ResponseWriter, r *h return } - result, err := rc.cl.Vulnerabilities(rc.reg, repo, tag) + image, err := registry.ParseImage(repo + ":" + tag) if err != nil { logrus.WithFields(logrus.Fields{ "func": "vulnerabilities", "URL": r.URL, "method": r.Method, - }).Errorf("vulnerability scanning for %s:%s failed: %v", repo, tag, err) + }).Errorf("parsing image %s:%s failed: %v", repo, tag, err) w.WriteHeader(http.StatusInternalServerError) return } + // Get the vulnerability report. + result, err := rc.cl.VulnerabilitiesV3(rc.reg, image.Path, image.Reference()) + if err != nil { + // Fallback to Clair v2 API. + result, err = rc.cl.Vulnerabilities(rc.reg, image.Path, image.Reference()) + if err != nil { + logrus.WithFields(logrus.Fields{ + "func": "vulnerabilities", + "URL": r.URL, + "method": r.Method, + }).Errorf("vulnerability scanning for %s:%s failed: %v", repo, tag, err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } + if strings.HasSuffix(r.URL.String(), ".json") { js, err := json.Marshal(result) if err != nil {