-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtrivy-operator-jira.rego
140 lines (115 loc) · 3.56 KB
/
trivy-operator-jira.rego
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
package postee.trivyoperator.jira
############################################# Common functions ############################################
with_default(obj, prop, default_value) = default_value{
not obj[prop]
}
with_default(obj, prop, default_value) = obj[prop]{
obj[prop]
}
################################################ Templates ################################################
# main template to render message
tpl:=`
h1. Image name: %s
%s
%s
%s
%s
%s
%s
`
sum_tpl := `
h4. Summary totals:
|critical: %s|high: %s|medium: %s|low: %s|
`
vlnrb_tpl = `
h4. %s severity vulnerabilities
%s
`
#Extra % is required in width:100%
table_tpl := `
%s
`
cell_tpl := `| %s `
header_tpl := `|| %s `
row_tpl_head := `
%s ||`
row_tpl := `
%s |`
colored_text_tpl := "{color:%s}%s{color}"
############################################## Html rendering #############################################
render_table_headers(headers) = row {
count(headers) > 0
ths := [th |
header := headers[_]
th := sprintf(header_tpl, [header])
]
row := sprintf(row_tpl_head, [concat("", ths)])
}
render_table_headers(headers) = "" { #if headers not specified return empty results
count(headers) == 0
}
render_table(headers, content_array) = s {
rows := [tr |
cells := content_array[_]
tds := [td |
ctext := cells[_]
td := to_cell(ctext)
]
tr = sprintf(row_tpl, [concat("", tds)])
]
s := sprintf(table_tpl, [concat("", array.concat([render_table_headers(headers)], rows))])
}
## why I added it?
to_cell(txt) = c {
c := sprintf(cell_tpl, [txt])
}
to_colored_text(color, txt) = spn {
spn := sprintf(colored_text_tpl, [color, txt])
}
####################################### Template specific functions #######################################
to_severity_color(color, level) = spn {
spn := to_colored_text(color, format_int(with_default(input.report.summary,level, 0), 10))
}
render_summary := sprintf(sum_tpl,[
to_severity_color("#c00000", "criticalCount"),
to_severity_color("#e0443d", "highCount"),
to_severity_color("#f79421", "mediumCount"),
to_severity_color("#e1c930", "lowCount")
])
vlnrb_headers := ["ID","Title", "Resource name", "Path", "Installed version", "Fix version", "Url"]
render_vlnrb(severity, list) = sprintf(vlnrb_tpl, [severity, render_table(vlnrb_headers, list)]) {
count(list) > 0
}
render_vlnrb(severity, list) = "" { #returns empty string if list of vulnerabilities is passed
count(list) == 0
}
# builds 2-dimension array for vulnerability table
vln_list(severity) = vlnrb {
some j
vlnrb := [r |
item := input.report.vulnerabilities[j]
vlnname := item.vulnerabilityID
title := item.title
fxvrsn := with_default(item, "fixedVersion", "none")
resource_name = with_default(item, "packageType", "none")
resource_path = with_default(item, "resource", "none")
resource_version = with_default(item, "installedVersion", "none")
primaryurl = with_default(item, "primaryLink", "none")
references = with_default(item, "links", "none")
item.severity == severity # only items with severity matched
r := [vlnname, title, resource_name, resource_path, resource_version, fxvrsn, primaryurl]
]
}
###########################################################################################################
title = sprintf("%s vulnerability scan report", [input.metadata.name])
result = msg {
msg := sprintf(tpl, [
input.metadata.name,
render_summary,
render_vlnrb("Critical", vln_list("CRITICAL")),
render_vlnrb("High", vln_list("HIGH")),
render_vlnrb("Medium", vln_list("MEDIUM")),
render_vlnrb("Low", vln_list("LOW")),
render_vlnrb("Negligible", vln_list("NEGLIGIBLE"))
])
}