Skip to content

Commit

Permalink
chore: handle misc plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Aug 11, 2023
1 parent 1891485 commit 51b46c5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
28 changes: 23 additions & 5 deletions pkg/worker/parse.go → pkg/worker/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import (
"strconv"
"strings"

"github.com/go-logr/logr"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/undistro/zora/api/zora/v1alpha1"
zora "github.com/undistro/zora/pkg/clientset/versioned"
"github.com/undistro/zora/pkg/worker/report/marvin"
"github.com/undistro/zora/pkg/worker/report/popeye"
)

// pluginParsers maps parse function by plugin name
var pluginParsers = map[string]func(ctx context.Context, reader io.Reader) ([]v1alpha1.ClusterIssueSpec, error){
// miscPlugins maps parse function by misc plugin name
var miscPlugins = map[string]func(ctx context.Context, reader io.Reader) ([]v1alpha1.ClusterIssueSpec, error){
"popeye": popeye.Parse,
"marvin": marvin.Parse,
}
Expand All @@ -42,9 +44,25 @@ var clusterIssueTypeMeta = metav1.TypeMeta{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

// parseResults converts the given results into ClusterIssues
func parseResults(ctx context.Context, cfg *config, results io.Reader) ([]v1alpha1.ClusterIssue, error) {
parseFunc, ok := pluginParsers[cfg.PluginName]
func handleMisconfiguration(ctx context.Context, cfg *config, results io.Reader, client *zora.Clientset) error {
log := logr.FromContextOrDiscard(ctx)
issues, err := parseMiscResults(ctx, cfg, results)
if err != nil {
return err
}
for _, issue := range issues {
issue, err := client.ZoraV1alpha1().ClusterIssues(cfg.Namespace).Create(ctx, &issue, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create ClusterIssue %q: %v", issue.Name, err)
}
log.Info(fmt.Sprintf("cluster issue %q successfully created", issue.Name), "resource version", issue.ResourceVersion)
}
return nil
}

// parseMiscResults converts the given results into ClusterIssues
func parseMiscResults(ctx context.Context, cfg *config, results io.Reader) ([]v1alpha1.ClusterIssue, error) {
parseFunc, ok := miscPlugins[cfg.PluginName]
if !ok {
return nil, errors.New(fmt.Sprintf("invalid plugin %q", cfg.PluginName))
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/worker/parse_test.go → pkg/worker/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/undistro/zora/api/zora/v1alpha1"
)

func TestParseResults(t *testing.T) {
func TestParseMiscResults(t *testing.T) {
type args struct {
cfg *config
filename string
Expand Down Expand Up @@ -331,20 +331,20 @@ func TestParseResults(t *testing.T) {
if tt.args.filename != "" {
f, err := os.Open(tt.args.filename)
if err != nil {
t.Errorf("parseResults() setup error = %v", err)
t.Errorf("parseMiscResults() setup error = %v", err)
return
}
r = f
}
got, err := parseResults(context.TODO(), tt.args.cfg, r)
got, err := parseMiscResults(context.TODO(), tt.args.cfg, r)
if (err != nil) != tt.wantErr {
t.Errorf("parseResults() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("parseMiscResults() error = %v, wantErr %v", err, tt.wantErr)
return
}
sortClusterIssues(got)
sortClusterIssues(tt.want)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseResults() got unexpect result, diff = %v", cmp.Diff(got, tt.want))
t.Errorf("parseMiscResults() got unexpect result, diff = %v", cmp.Diff(got, tt.want))
}
})
}
Expand Down
16 changes: 5 additions & 11 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"

zora "github.com/undistro/zora/pkg/clientset/versioned"
Expand All @@ -44,16 +43,11 @@ func Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to gather results: %v", err)
}
issues, err := parseResults(ctx, cfg, results)
if err != nil {
return err
}
for _, issue := range issues {
issue, err := client.ZoraV1alpha1().ClusterIssues(cfg.Namespace).Create(ctx, &issue, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create ClusterIssue %q: %v", issue.Name, err)
}
log.Info(fmt.Sprintf("cluster issue %q successfully created", issue.Name), "resource version", issue.ResourceVersion)
switch cfg.PluginType {
case "misconfiguration":
return handleMisconfiguration(ctx, cfg, results, client)
case "vulnerability":
log.Info("vuln")
}
return nil
}
Expand Down

0 comments on commit 51b46c5

Please sign in to comment.