From 73682717eda4fa2e0cbc6311d5c97e01e0f2673c Mon Sep 17 00:00:00 2001 From: Johannes Kleinlercher Date: Mon, 13 Nov 2023 17:10:13 +0100 Subject: [PATCH] fix: show trivy as active when activated with --no-install flag (#675) Signed-off-by: Johannes Kleinlercher Signed-off-by: Thomas Schuetz <38893055+thschue@users.noreply.github.com> Co-authored-by: Thomas Schuetz <38893055+thschue@users.noreply.github.com> Co-authored-by: Alex Jones --- pkg/integration/trivy/trivy.go | 51 +++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/pkg/integration/trivy/trivy.go b/pkg/integration/trivy/trivy.go index 4acfea79ea..259a161e0a 100644 --- a/pkg/integration/trivy/trivy.go +++ b/pkg/integration/trivy/trivy.go @@ -16,11 +16,15 @@ package trivy import ( "context" "fmt" + "os" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/common" + "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" helmclient "github.com/mittwald/go-helm-client" + "github.com/spf13/viper" "helm.sh/helm/v3/pkg/repo" ) @@ -126,13 +130,52 @@ func (t *Trivy) UnDeploy(namespace string) error { return nil } -func (t *Trivy) IsActivate() bool { +func (t *Trivy) isDeployed() bool { + // check if aquasec apigroup is available as a marker if trivy is installed on the cluster + kubecontext := viper.GetString("kubecontext") + kubeconfig := viper.GetString("kubeconfig") + client, err := kubernetes.NewClient(kubecontext, kubeconfig) + if err != nil { + // TODO: better error handling + color.Red("Error initialising kubernetes client: %v", err) + os.Exit(1) + } + groups, _, err := client.Client.Discovery().ServerGroupsAndResources() + if err != nil { + // TODO: better error handling + color.Red("Error initialising discovery client: %v", err) + os.Exit(1) + } - if _, err := t.helm.GetRelease(ReleaseName); err != nil { - return false + for _, group := range groups { + if group.Name == "aquasecurity.github.io" { + return true + } } - return true + return false +} + +func (t *Trivy) isFilterActive() bool { + activeFilters := viper.GetStringSlice("active_filters") + + for _, filter := range t.GetAnalyzerName() { + for _, af := range activeFilters { + if af == filter { + return true + } + } + } + + return false +} + +func (t *Trivy) IsActivate() bool { + if t.isFilterActive() && t.isDeployed() { + return true + } else { + return false + } } func (t *Trivy) AddAnalyzer(mergedMap *map[string]common.IAnalyzer) {