-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b6f224
commit 346ef18
Showing
12 changed files
with
525 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package kube | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1" | ||
) | ||
|
||
const configAuditReportsResource = "configauditreports" | ||
|
||
// GetConfigAuditReportList retrieves all resources of type configauditreport in all namespaces. | ||
func GetConfigAuditReportList() (*v1alpha1.ConfigAuditReportList, error) { | ||
var list v1alpha1.ConfigAuditReportList | ||
err := client. | ||
Get(). | ||
Resource(configAuditReportsResource). | ||
Do(context.TODO()). | ||
Into(&list) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &list, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package role | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1" | ||
) | ||
|
||
// Filters contains the supported filters for the configaudit view | ||
type Filters struct { | ||
Name string | ||
Namespace string | ||
Kind string | ||
|
||
// optional | ||
Severity string | ||
} | ||
|
||
// GetView converts some report data to the /configaudit view | ||
// returns view data and "true" if the image was found in the report list | ||
func GetView(data *v1alpha1.ConfigAuditReportList, filters Filters) (View, bool) { | ||
for _, item := range data.Items { | ||
var name string | ||
if val, ok := item.ObjectMeta.Labels["trivy-operator.resource.name"]; ok { | ||
name = val | ||
} else if val, ok := item.ObjectMeta.Annotations["trivy-operator.resource.name"]; ok { | ||
name = val | ||
} else { | ||
name = item.Name | ||
} | ||
|
||
if filters.Name != name { | ||
continue | ||
} | ||
if filters.Namespace != item.ObjectMeta.Labels["trivy-operator.resource.namespace"] { | ||
continue | ||
} | ||
if filters.Kind != item.ObjectMeta.Labels["trivy-operator.resource.kind"] { | ||
continue | ||
} | ||
|
||
view := View{ | ||
Kind: item.ObjectMeta.Labels["trivy-operator.resource.kind"], | ||
Name: name, | ||
Namespace: item.ObjectMeta.Labels["trivy-operator.resource.namespace"], | ||
} | ||
|
||
for _, v := range item.Report.Checks { | ||
ksvNum := strings.TrimPrefix(v.ID, "KSV") | ||
url := fmt.Sprintf("https://avd.aquasec.com/misconfig/kubernetes/general/avd-ksv-%04s/", ksvNum) | ||
|
||
vuln := Vulnerability{ | ||
ID: v.ID, | ||
URL: url, | ||
Severity: string(v.Severity), | ||
Title: v.Title, | ||
Description: v.Description, | ||
} | ||
|
||
if filters.Severity != "" && !strings.EqualFold(vuln.Severity, filters.Severity) { | ||
continue | ||
} | ||
|
||
view.Vulnerabilities = append(view.Vulnerabilities, vuln) | ||
} | ||
|
||
view = sortView(view) | ||
|
||
return view, true | ||
} | ||
|
||
return View{}, false | ||
} | ||
|
||
func sortView(v View) View { | ||
// Create an order for severities to sort by | ||
// Define custom priority order | ||
severityOrder := map[string]int{ | ||
"CRITICAL": 3, | ||
"HIGH": 2, | ||
"MEDIUM": 1, | ||
"LOW": 0, | ||
} | ||
|
||
// Sort the slice by severity in descending order | ||
sort.Slice(v.Vulnerabilities, func(j, k int) bool { | ||
return severityOrder[v.Vulnerabilities[j].Severity] > severityOrder[v.Vulnerabilities[k].Severity] | ||
}) | ||
|
||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package role | ||
|
||
// View a list of data about role vulnerabilities | ||
type View Data | ||
|
||
// Data data about a role and its vulnerabilities | ||
type Data struct { | ||
Name string | ||
Namespace string | ||
Kind string | ||
Vulnerabilities []Vulnerability | ||
} | ||
|
||
// Vulnerability data related to a role | ||
type Vulnerability struct { | ||
ID string | ||
URL string | ||
Severity string | ||
Title string | ||
Description string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package roles | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1" | ||
) | ||
|
||
// GetView converts some report data to the /roles view | ||
func GetView(data *v1alpha1.ConfigAuditReportList) View { | ||
var view View | ||
|
||
for _, item := range data.Items { | ||
var name string | ||
if val, ok := item.ObjectMeta.Labels["trivy-operator.resource.name"]; ok { | ||
name = val | ||
} else if val, ok := item.ObjectMeta.Annotations["trivy-operator.resource.name"]; ok { | ||
name = val | ||
} else { | ||
name = item.Name | ||
} | ||
|
||
audit := Data{ | ||
Kind: item.ObjectMeta.Labels["trivy-operator.resource.kind"], | ||
Name: name, | ||
Namespace: item.ObjectMeta.Labels["trivy-operator.resource.namespace"], | ||
} | ||
|
||
index, unique := view.isUnique(audit.Name, audit.Namespace, audit.Kind) | ||
if unique { | ||
view = append(view, audit) | ||
index = len(view) - 1 | ||
} | ||
|
||
for _, v := range item.Report.Checks { | ||
severity := v.Severity | ||
vuln := Vulnerability{ | ||
ID: v.ID, | ||
Title: v.Title, | ||
Description: v.Description, | ||
} | ||
|
||
switch strings.ToLower(string(severity)) { | ||
case "critical": | ||
view[index].CriticalVulnerabilities = append(view[index].CriticalVulnerabilities, vuln) | ||
case "high": | ||
view[index].HighVulnerabilities = append(view[index].HighVulnerabilities, vuln) | ||
case "medium": | ||
view[index].MediumVulnerabilities = append(view[index].MediumVulnerabilities, vuln) | ||
case "low": | ||
view[index].LowVulnerabilities = append(view[index].LowVulnerabilities, vuln) | ||
} | ||
} | ||
} | ||
|
||
view = sortView(view) | ||
|
||
return view | ||
} | ||
|
||
func (a View) isUnique(name, namespace, kind string) (int, bool) { | ||
for i, audit := range a { | ||
if name == audit.Name && kind == audit.Kind { | ||
return i, false | ||
} | ||
} | ||
|
||
return 0, true | ||
} | ||
|
||
func sortView(a View) View { | ||
// Sort the slice by severity in descending order | ||
sort.Slice(a, func(j, k int) bool { | ||
if len(a[j].CriticalVulnerabilities) != len(a[k].CriticalVulnerabilities) { | ||
return len(a[j].CriticalVulnerabilities) > len(a[k].CriticalVulnerabilities) | ||
} | ||
|
||
if len(a[j].HighVulnerabilities) != len(a[k].HighVulnerabilities) { | ||
return len(a[j].HighVulnerabilities) > len(a[k].HighVulnerabilities) | ||
} | ||
|
||
if len(a[j].MediumVulnerabilities) != len(a[k].MediumVulnerabilities) { | ||
return len(a[j].MediumVulnerabilities) > len(a[k].MediumVulnerabilities) | ||
} | ||
|
||
return len(a[j].LowVulnerabilities) > len(a[k].LowVulnerabilities) | ||
}) | ||
|
||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package roles | ||
|
||
// View a list of data about role vulnerabilities | ||
type View []Data | ||
|
||
// Data data about a role and its vulnerabilities | ||
type Data struct { | ||
Name string | ||
Namespace string | ||
Kind string | ||
CriticalVulnerabilities []Vulnerability | ||
HighVulnerabilities []Vulnerability | ||
MediumVulnerabilities []Vulnerability | ||
LowVulnerabilities []Vulnerability | ||
} | ||
|
||
// Vulnerability data related to a role | ||
type Vulnerability struct { | ||
ID string | ||
Title string | ||
Description string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.