Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get image before send to analysis #70

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ DOCKER_TAG ?= $(shell echo ${VERSION} | sed 's/\//-/')
GOLANGCI_VERSION = 1.21.0
LICENSEI_VERSION = 0.1.0

GOLANG_VERSION = 1.13
GOLANG_VERSION = 1.15

.PHONY: clean
clean: ## Clean the working area and the project
Expand Down
52 changes: 47 additions & 5 deletions pkg/anchore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"

"github.com/banzaicloud/anchore-image-validator/pkg/apis/security/v1alpha1"
Expand Down Expand Up @@ -64,7 +65,7 @@ func anchoreRequest(path string, bodyParams map[string]string, method string) ([
logrus.WithFields(logrus.Fields{
"url": fullURL,
"bodyParams": bodyParams,
}).Info("Sending request")
}).Debug("Sending request")

req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
Expand All @@ -78,7 +79,7 @@ func anchoreRequest(path string, bodyParams map[string]string, method string) ([

logrus.WithFields(logrus.Fields{
"response": string(bodyText),
}).Info("Anchore Response Body")
}).Debug("Anchore Response Body")

if err != nil {
return nil, fmt.Errorf("failed to complete request to Anchore: %v", err)
Expand All @@ -92,7 +93,15 @@ func anchoreRequest(path string, bodyParams map[string]string, method string) ([
}

func getStatus(digest string, tag string) bool {
path := fmt.Sprintf("/v1/images/%s/check?history=false&detail=false&tag=%s", digest, tag)
params := url.Values{}
params.Add("history", "false")
params.Add("datall", "false")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Copy link
Member Author

@pbalogh-sa pbalogh-sa Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on master 52fdc98

params.Add("tag", tag)

path := fmt.Sprintf("/v1/images/%s/check?%s", digest, params.Encode())

// path := fmt.Sprintf("/v1/images/%s/check?history=false&detail=false&tag=%s", digest, tag)

body, err := anchoreRequest(path, nil, "GET")

if err != nil {
Expand All @@ -113,9 +122,42 @@ func getStatus(digest string, tag string) bool {
return result[0][digest][resultIndex][0].Status == "pass"
}

func getImage(imageRef string) (Image, error) {
params := url.Values{}
params.Add("fulltag", imageRef)
params.Add("history", "false")
path := fmt.Sprintf("/v1/images?%s", params.Encode())

body, err := anchoreRequest(path, nil, "GET")

if err != nil {
return Image{}, err
}

var images []Image
err = json.Unmarshal(body, &images)
if err != nil {
return Image{}, fmt.Errorf("failed to unmarshal JSON from response: %v", err)
}

return images[0], nil
}

func getOrAddImage(imageRef string) (Image, error) {
image, err := getImage(imageRef)
if err == nil {
logrus.WithFields(logrus.Fields{
"Image": imageRef,
}).Info("Image has already sent to scan")
return image, nil
}

logrus.WithFields(logrus.Fields{
"Image": imageRef,
}).Info("Image hasn't sent to scan yet, sending it")

params := map[string]string{"tag": imageRef}
body, err := anchoreRequest("/v1/images?history=false", params, "POST")
body, err := anchoreRequest("/v1/images", params, "POST")

if err != nil {
return Image{}, err
Expand All @@ -130,7 +172,7 @@ func getOrAddImage(imageRef string) (Image, error) {

logrus.WithFields(logrus.Fields{
"Image": images[0],
}).Info("Get or Added image")
}).Debug("Image to add")

return images[0], nil
}
Expand Down