fix clair health port

This commit is contained in:
jgsqware 2016-05-20 18:13:56 +02:00
parent 368227e4dd
commit 21c68cd795
4 changed files with 27 additions and 31 deletions

View File

@ -12,16 +12,16 @@ import (
) )
var uri string var uri string
var healthPort int var healthURI string
//Report Reporting Config value //Report Reporting Config value
var Report ReportConfig var Report ReportConfig
//VulnerabiliesCounts Total count of vulnerabilities by type //VulnerabiliesCounts Total count of vulnerabilities by type
type VulnerabiliesCounts map[types.Priority]int type vulnerabiliesCounts map[types.Priority]int
//Total return to total of Vulnerabilities //Total return to total of Vulnerabilities
func (v VulnerabiliesCounts) Total() int { func (v vulnerabiliesCounts) Total() int {
var c int var c int
for _, count := range v { for _, count := range v {
c += count c += count
@ -30,12 +30,12 @@ func (v VulnerabiliesCounts) Total() int {
} }
//Count return count of severities in Vulnerabilities //Count return count of severities in Vulnerabilities
func (v VulnerabiliesCounts) Count(severity string) int { func (v vulnerabiliesCounts) Count(severity string) int {
return v[types.Priority(severity)] return v[types.Priority(severity)]
} }
//RelativeCount get the percentage of vulnerabilities of a severity //RelativeCount get the percentage of vulnerabilities of a severity
func (v VulnerabiliesCounts) RelativeCount(severity string) float64 { func (v vulnerabiliesCounts) RelativeCount(severity string) float64 {
count := v[types.Priority(severity)] count := v[types.Priority(severity)]
result := float64(count) / float64(v.Total()) * 100 result := float64(count) / float64(v.Total()) * 100
return math.Ceil(result*100) / 100 return math.Ceil(result*100) / 100
@ -52,7 +52,7 @@ func (imageAnalysis ImageAnalysis) String() string {
} }
// CountVulnerabilities counts all image vulnerability // CountVulnerabilities counts all image vulnerability
func (imageAnalysis ImageAnalysis) CountVulnerabilities(l v1.Layer) int { func (imageAnalysis ImageAnalysis) countVulnerabilities(l v1.Layer) int {
count := 0 count := 0
for _, f := range l.Features { for _, f := range l.Features {
count += len(f.Vulnerabilities) count += len(f.Vulnerabilities)
@ -61,8 +61,8 @@ func (imageAnalysis ImageAnalysis) CountVulnerabilities(l v1.Layer) int {
} }
// CountAllVulnerabilities Total count of vulnerabilities // CountAllVulnerabilities Total count of vulnerabilities
func (imageAnalysis ImageAnalysis) CountAllVulnerabilities() VulnerabiliesCounts { func (imageAnalysis ImageAnalysis) CountAllVulnerabilities() vulnerabiliesCounts {
result := make(VulnerabiliesCounts) result := make(vulnerabiliesCounts)
l := imageAnalysis.Layers[len(imageAnalysis.Layers)-1] l := imageAnalysis.Layers[len(imageAnalysis.Layers)-1]
@ -81,24 +81,21 @@ func (imageAnalysis ImageAnalysis) LastLayer() *v1.Layer {
return imageAnalysis.Layers[len(imageAnalysis.Layers)-1].Layer return imageAnalysis.Layers[len(imageAnalysis.Layers)-1].Layer
} }
type VulnerabilityWithFeature struct { type vulnerabilityWithFeature struct {
v1.Vulnerability v1.Vulnerability
Feature string Feature string
} }
func fmtURI(u string, port int) string {
func fmtURI(u string, port int) {
uri = u
if port != 0 { if port != 0 {
uri += ":" + strconv.Itoa(port) u += ":" + strconv.Itoa(port)
} }
if !strings.HasSuffix(uri, "/v1") { if !strings.HasPrefix(u, "http://") && !strings.HasPrefix(u, "https://") {
uri += "/v1" u = "http://" + u
}
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
uri = "http://" + uri
} }
return u
} }
func (imageAnalysis ImageAnalysis) ShortName(l v1.Layer) string { func (imageAnalysis ImageAnalysis) ShortName(l v1.Layer) string {
@ -107,8 +104,8 @@ func (imageAnalysis ImageAnalysis) ShortName(l v1.Layer) string {
//Config configure Clair from configFile //Config configure Clair from configFile
func Config() { func Config() {
fmtURI(viper.GetString("clair.uri"), viper.GetInt("clair.port")) uri = fmtURI(viper.GetString("clair.uri"), viper.GetInt("clair.port")) + "/v1"
healthPort = viper.GetInt("clair.healthPort") healthURI = fmtURI(viper.GetString("clair.uri"), viper.GetInt("clair.healthPort")) + "/health"
Report.Path = viper.GetString("clair.report.path") Report.Path = viper.GetString("clair.report.path")
Report.Format = viper.GetString("clair.report.format") Report.Format = viper.GetString("clair.report.format")
} }

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"strconv"
"strings" "github.com/Sirupsen/logrus"
) )
func IsHealthy() bool { func IsHealthy() bool {
healthURI := strings.Replace(uri, "6060/v1", strconv.Itoa(healthPort), 1) + "/health" logrus.Debugln("requesting health on: " + healthURI)
response, err := http.Get(healthURI) response, err := http.Get(healthURI)
if err != nil { if err != nil {

View File

@ -26,8 +26,7 @@ func ReportAsHTML(analyzes ImageAnalysis) (string, error) {
} }
funcs := template.FuncMap{ funcs := template.FuncMap{
"invertedPriorities": InvertedPriorities, "vulnerabilities": vulnerabilities,
"vulnerabilities": Vulnerabilities,
"sortedVulnerabilities": SortedVulnerabilities, "sortedVulnerabilities": SortedVulnerabilities,
} }
@ -41,7 +40,7 @@ func ReportAsHTML(analyzes ImageAnalysis) (string, error) {
return doc.String(), nil return doc.String(), nil
} }
func InvertedPriorities() []types.Priority { func invertedPriorities() []types.Priority {
ip := make([]types.Priority, len(types.Priorities)) ip := make([]types.Priority, len(types.Priorities))
for i, j := 0, len(types.Priorities)-1; i <= j; i, j = i+1, j-1 { for i, j := 0, len(types.Priorities)-1; i <= j; i, j = i+1, j-1 {
ip[i], ip[j] = types.Priorities[j], types.Priorities[i] ip[i], ip[j] = types.Priorities[j], types.Priorities[i]
@ -51,15 +50,15 @@ func InvertedPriorities() []types.Priority {
} }
//Vulnerabilities return a list a vulnerabilities //Vulnerabilities return a list a vulnerabilities
func Vulnerabilities(imageAnalysis ImageAnalysis) map[types.Priority][]VulnerabilityWithFeature { func vulnerabilities(imageAnalysis ImageAnalysis) map[types.Priority][]vulnerabilityWithFeature {
result := make(map[types.Priority][]VulnerabilityWithFeature) result := make(map[types.Priority][]vulnerabilityWithFeature)
l := imageAnalysis.Layers[len(imageAnalysis.Layers)-1] l := imageAnalysis.Layers[len(imageAnalysis.Layers)-1]
for _, f := range l.Layer.Features { for _, f := range l.Layer.Features {
for _, v := range f.Vulnerabilities { for _, v := range f.Vulnerabilities {
result[types.Priority(v.Severity)] = append(result[types.Priority(v.Severity)], VulnerabilityWithFeature{Vulnerability: v, Feature: f.Name + ":" + f.Version}) result[types.Priority(v.Severity)] = append(result[types.Priority(v.Severity)], vulnerabilityWithFeature{Vulnerability: v, Feature: f.Name + ":" + f.Version})
} }
} }
@ -75,7 +74,7 @@ func SortedVulnerabilities(imageAnalysis ImageAnalysis) []v1.Feature {
for _, f := range l.Layer.Features { for _, f := range l.Layer.Features {
if len(f.Vulnerabilities) > 0 { if len(f.Vulnerabilities) > 0 {
vulnerabilities := []v1.Vulnerability{} vulnerabilities := []v1.Vulnerability{}
for _, p := range InvertedPriorities() { for _, p := range invertedPriorities() {
for _, v := range f.Vulnerabilities { for _, v := range f.Vulnerabilities {
if types.Priority(v.Severity) == p { if types.Priority(v.Severity) == p {
vulnerabilities = append(vulnerabilities, v) vulnerabilities = append(vulnerabilities, v)

View File

@ -83,7 +83,7 @@ func templatesAnalysisTemplateHtml() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "templates/analysis-template.html", size: 16409, mode: os.FileMode(420), modTime: time.Unix(1463667501, 0)} info := bindataFileInfo{name: "templates/analysis-template.html", size: 16409, mode: os.FileMode(420), modTime: time.Unix(1463760760, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }