Skip to content

Commit

Permalink
fix: show trivy as active when activated with --no-install flag (#675)
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Kleinlercher <johannes@kleinlercher.at>
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 <alexsimonjones@gmail.com>
  • Loading branch information
3 people committed Nov 13, 2023
1 parent 4531278 commit 7368271
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7368271

Please sign in to comment.