/
vulnerabilityreport_metrics.go
158 lines (143 loc) · 3.64 KB
/
vulnerabilityreport_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package vulnerabilityreport
import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"github.com/giantswarm/starboard-exporter/utils"
)
const (
metricNamespace = "starboard_exporter"
metricSubsystem = "vulnerabilityreport"
LabelGroupAll = "all"
labelGroupSummary = "summary"
)
type FieldScope string
const (
FieldScopeReport FieldScope = "report"
FieldScopeVulnerability FieldScope = "vulnerability"
)
type VulnerabilityLabel struct {
Name string
Groups []string
Scope FieldScope
// Handler valueFromReport
}
var metricLabels = []VulnerabilityLabel{
{
Name: "report_name",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "image_namespace",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "image_registry",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "image_repository",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "image_tag",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "image_digest",
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeReport,
},
{
Name: "severity",
// Note - Summary metrics use a different severity field than per-vulnerability severity.
Groups: []string{LabelGroupAll, labelGroupSummary},
Scope: FieldScopeVulnerability,
},
{
Name: "vulnerability_id",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
{
Name: "vulnerable_resource_name",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
{
Name: "installed_resource_version",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
{
Name: "fixed_resource_version",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
{
Name: "vulnerability_title",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
{
Name: "vulnerability_link",
Groups: []string{LabelGroupAll},
Scope: FieldScopeVulnerability,
},
}
func LabelWithName(name string) (label VulnerabilityLabel, ok bool) {
for _, label := range metricLabels {
if label.Name == name {
return label, true
}
}
return VulnerabilityLabel{}, false
}
func LabelsForGroup(group string) []VulnerabilityLabel {
l := []VulnerabilityLabel{}
for _, label := range metricLabels {
if utils.SliceContains(label.Groups, group) {
l = append(l, label)
}
}
return l
}
func labelNamesForGroup(group string) []string {
l := []string{}
for _, label := range metricLabels {
if utils.SliceContains(label.Groups, group) {
l = append(l, label.Name)
}
}
return l
}
func LabelNamesForList(list []VulnerabilityLabel) []string {
l := []string{}
for _, label := range list {
l = append(l, label.Name)
}
return l
}
// Gauge for the count of all vulnerabilities of a particular severity contained in an image.
var (
VulnerabilitySummary = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: metricNamespace,
Subsystem: metricSubsystem,
Name: "image_vulnerability_severity_count",
Help: "Exposes the number of vulnerabilities of a particular severity per-image.",
},
labelNamesForGroup(labelGroupSummary),
)
)
// Gauge reporting the score of each CVE present in an image.
// Registered during first reconcile loop in registerMetrics().
var VulnerabilityInfo *prometheus.GaugeVec
func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(VulnerabilitySummary) // VulnerabilityInfo
}