Skip to content

Commit

Permalink
Simple tracing, some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Miroshkin committed Sep 22, 2017
1 parent 6120ed2 commit 2726d83
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
22 changes: 18 additions & 4 deletions clair/clair.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"time"

"github.com/optiopay/klar/docker"
"github.com/optiopay/klar/utils"
)

const EMPTY_LAYER_BLOB_SUM = "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"

// Clair is representation of Clair server
type Clair struct {
url string
url string
client http.Client
}

type layer struct {
Expand Down Expand Up @@ -75,7 +77,11 @@ func NewClair(url string) Clair {
if strings.LastIndex(url, ":") < 5 {
url = fmt.Sprintf("%s:6060", url)
}
return Clair{url}
client := http.Client{
Timeout: time.Minute,
}

return Clair{url, client}
}

func newLayer(image *docker.Image, index int) *layer {
Expand Down Expand Up @@ -135,10 +141,16 @@ func (c *Clair) Analyse(image *docker.Image) []Vulnerability {

func (c *Clair) analyzeLayer(layerName string) ([]Vulnerability, error) {
url := fmt.Sprintf("%s/v1/layers/%s?vulnerabilities", c.url, layerName)
response, err := http.Get(url)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("Can't create an analyze request: %s", err)
}
utils.DumpRequest(request)
response, err := c.client.Do(request)
if err != nil {
return nil, err
}
utils.DumpResponse(response)
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(response.Body)
Expand Down Expand Up @@ -169,10 +181,12 @@ func (c *Clair) pushLayer(layer *layer) error {
return fmt.Errorf("Can't create a push request: %s", err)
}
request.Header.Set("Content-Type", "application/json")
response, err := (&http.Client{Timeout: time.Minute}).Do(request)
utils.DumpRequest(request)
response, err := c.client.Do(request)
if err != nil {
return fmt.Errorf("Can't push layer to Clair: %s", err)
}
utils.DumpResponse(response)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"os"
"regexp"
"strings"
"time"

"github.com/optiopay/klar/utils"
)

const (
Expand Down Expand Up @@ -95,7 +98,10 @@ func NewImage(qname, user, password string, insecureTLS, insecureRegistry bool)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureTLS},
}
client := http.Client{Transport: tr}
client := http.Client{
Transport: tr,
Timeout: time.Minute,
}
registry := dockerHub
tag := "latest"
var nameParts, tagParts []string
Expand Down Expand Up @@ -292,10 +298,12 @@ func (i *Image) pullReq() (*http.Response, error) {

// Prefer manifest schema v2
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
utils.DumpRequest(req)
resp, err := i.client.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "Get error")
return nil, err
}
utils.DumpResponse(resp)
return resp, nil
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/optiopay/klar/clair"
"github.com/optiopay/klar/docker"
"github.com/optiopay/klar/utils"
)

type jsonOutput struct {
Expand All @@ -25,6 +26,10 @@ func main() {
os.Exit(1)
}

if os.Getenv("KLAR_TRACE") != "" {
utils.Trace = true
}

clairAddr := os.Getenv("CLAIR_ADDR")
if clairAddr == "" {
fmt.Fprintf(os.Stderr, "Clair address must be provided\n")
Expand Down
12 changes: 10 additions & 2 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@ import (
"os"
)

var Trace bool

func DumpRequest(r *http.Request) {
if !Trace {
return
}
dump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't dump HTTP request %s\n", err.Error())
} else {
fmt.Fprintf(os.Stderr, "request_dump: %s\n", string(dump[:]))
fmt.Fprintf(os.Stderr, "----> HTTP REQUEST:\n%s\n", string(dump[:]))
}
}

func DumpResponse(r *http.Response) {
if !Trace {
return
}
dump, err := httputil.DumpResponse(r, true)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't dump HTTP reqsponse %s\n", err.Error())
} else {
fmt.Fprintf(os.Stderr, "response_dump: %s\n", string(dump[:]))
fmt.Fprintf(os.Stderr, "<---- HTTP RESPONSE:\n%s\n", string(dump[:]))
}
}

0 comments on commit 2726d83

Please sign in to comment.