Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configauditreport #609

Merged
merged 8 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ListFilters() ([]string, []string, []string) {
for k := range additionalAnalyzerMap {
additionalKeys = append(additionalKeys, k)
}

// Current analyzer
integrationProvider := integration.NewIntegration()
var integrationAnalyzers []string

Expand All @@ -73,7 +73,10 @@ func ListFilters() ([]string, []string, []string) {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
integrationAnalyzers = append(integrationAnalyzers, in.GetAnalyzerName())
for _, analyzers := range in.GetAnalyzerName() {

integrationAnalyzers = append(integrationAnalyzers, analyzers)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type PreAnalysis struct {
MutatingWebhook regv1.MutatingWebhookConfiguration
// Integrations
TrivyVulnerabilityReport trivy.VulnerabilityReport
TrivyConfigAuditReport trivy.ConfigAuditReport
}

type Result struct {
Expand Down
20 changes: 14 additions & 6 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type IIntegration interface {
// RemoveAnalyzer removes an analyzer from the cluster
RemoveAnalyzer() error

GetAnalyzerName() string
GetAnalyzerName() []string

IsActivate() bool
}
Expand Down Expand Up @@ -71,7 +71,11 @@ func (*Integration) Activate(name string, namespace string, activeFilters []stri
return errors.New("integration not found")
}

mergedFilters := append(activeFilters, integrations[name].GetAnalyzerName())
mergedFilters := activeFilters

for _, integrationAnalyzer := range integrations[name].GetAnalyzerName() {
mergedFilters = append(mergedFilters, integrationAnalyzer)
}

uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(mergedFilters)

Expand Down Expand Up @@ -108,11 +112,15 @@ func (*Integration) Deactivate(name string, namespace string) error {
// This might be a bad idea, but we cannot reference analyzer here
foundFilter := false
for i, v := range activeFilters {
if v == integrations[name].GetAnalyzerName() {
foundFilter = true
activeFilters = append(activeFilters[:i], activeFilters[i+1:]...)
break

for _, intanal := range integrations[name].GetAnalyzerName() {
AlexsJones marked this conversation as resolved.
Show resolved Hide resolved
if v == intanal {
foundFilter = true
activeFilters = append(activeFilters[:i], activeFilters[i+1:]...)
break
}
}

}
if !foundFilter {
color.Red("Ingregation %s does not exist in configuration file. Please use k8sgpt integration add.", name)
Expand Down
86 changes: 84 additions & 2 deletions pkg/integration/trivy/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
)

type TrivyAnalyzer struct {
vulernabilityReportAnalysis bool
configAuditReportAnalysis bool
}

func (TrivyAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {

func (TrivyAnalyzer) analyzeVulnerabilityReports(a common.Analyzer) ([]common.Result, error) {
// Get all trivy VulnerabilityReports
result := &v1alpha1.VulnerabilityReportList{}

Expand Down Expand Up @@ -84,4 +85,85 @@ func (TrivyAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
}

return a.Results, nil

}

func (t TrivyAnalyzer) analyzeConfigAuditReports(a common.Analyzer) ([]common.Result, error) {
// Get all trivy VulnerabilityReports
result := &v1alpha1.ConfigAuditReportList{}

config := a.Client.GetConfig()
// Add group version to sceheme
config.ContentConfig.GroupVersion = &v1alpha1.SchemeGroupVersion
config.UserAgent = rest.DefaultKubernetesUserAgent()
config.APIPath = "/apis"

restClient, err := rest.UnversionedRESTClientFor(config)
if err != nil {
return nil, err
}
err = restClient.Get().Resource("configauditreports").Do(a.Context).Into(result)
if err != nil {
return nil, err
}

// Find criticals and get CVE
var preAnalysis = map[string]common.PreAnalysis{}

for _, report := range result.Items {

var failures []common.Failure
if report.Report.Summary.HighCount > 0 {

failures = append(failures, common.Failure{
Text: fmt.Sprintf("Config audit report %s detected at least one high issue", report.Name),
Sensitive: []common.Sensitive{},
})

}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", report.Labels["trivy-operator.resource.namespace"],
report.Labels["trivy-operator.resource.name"])] = common.PreAnalysis{
TrivyConfigAuditReport: report,
FailureDetails: failures,
}
}
}

for key, value := range preAnalysis {
var currentAnalysis = common.Result{
Kind: "ConfigAuditReport",
Name: key,
Error: value.FailureDetails,
}

parent, _ := util.GetParent(a.Client, value.TrivyConfigAuditReport.ObjectMeta)
currentAnalysis.ParentObject = parent
a.Results = append(a.Results, currentAnalysis)
}

return a.Results, nil
}

func (t TrivyAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {

if t.vulernabilityReportAnalysis {
common := make([]common.Result, 0)
vresult, err := t.analyzeVulnerabilityReports(a)
if err != nil {
return nil, err
}
common = append(common, vresult...)
return common, nil
}
if t.configAuditReportAnalysis {
common := make([]common.Result, 0)
cresult, err := t.analyzeConfigAuditReports(a)
if err != nil {
return nil, err
}
common = append(common, cresult...)
return common, nil
}
return make([]common.Result, 0), nil
}
15 changes: 12 additions & 3 deletions pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ func NewTrivy() *Trivy {
}
}

func (t *Trivy) GetAnalyzerName() string {
return "VulnerabilityReport"
func (t *Trivy) GetAnalyzerName() []string {
return []string{
"VulnerabilityReport",
"ConfigAuditReport",
}
}

func (t *Trivy) Deploy(namespace string) error {
Expand Down Expand Up @@ -107,10 +110,16 @@ func (t *Trivy) IsActivate() bool {

func (t *Trivy) AddAnalyzer(mergedMap *map[string]common.IAnalyzer) {

(*mergedMap)["VulnerabilityReport"] = &TrivyAnalyzer{}
(*mergedMap)["VulnerabilityReport"] = &TrivyAnalyzer{
vulernabilityReportAnalysis: true,
}
(*mergedMap)["ConfigAuditReport"] = &TrivyAnalyzer{
configAuditReportAnalysis: true,
}

}

func (t *Trivy) RemoveAnalyzer() error {

return nil
}