Skip to content

Commit 3185069

Browse files
TalgarrSUSTAPLE117
andauthored
Optimize skip rule (#287)
* Optimize skip rule Do not compile rules that are going to be filtered by the config Move HasOnlyRule to valid place Add cli * Update cli message Signed-off-by: Sébastien Graveline <[email protected]> * Update opa/opa.go Co-authored-by: Alexis-Maurer Fortin <[email protected]> Signed-off-by: Sébastien Graveline <[email protected]> --------- Signed-off-by: Sébastien Graveline <[email protected]> Co-authored-by: Alexis-Maurer Fortin <[email protected]>
1 parent 680725a commit 3185069

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

cmd/root.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"os/signal"
8+
"path/filepath"
9+
"strings"
10+
"syscall"
11+
612
"github.com/boostsecurityio/poutine/analyze"
713
"github.com/boostsecurityio/poutine/formatters/json"
814
"github.com/boostsecurityio/poutine/formatters/pretty"
@@ -11,15 +17,10 @@ import (
1117
"github.com/boostsecurityio/poutine/opa"
1218
"github.com/boostsecurityio/poutine/providers/gitops"
1319
"github.com/boostsecurityio/poutine/providers/scm"
14-
"github.com/boostsecurityio/poutine/providers/scm/domain"
20+
scm_domain "github.com/boostsecurityio/poutine/providers/scm/domain"
1521
"github.com/rs/zerolog"
1622
"github.com/rs/zerolog/log"
1723
"github.com/spf13/viper"
18-
"os"
19-
"os/signal"
20-
"path/filepath"
21-
"strings"
22-
"syscall"
2324

2425
"github.com/spf13/cobra"
2526
)
@@ -36,6 +37,7 @@ var (
3637
var token string
3738
var cfgFile string
3839
var config *models.Config = models.DefaultConfig()
40+
var skipRules []string
3941

4042
var legacyFlags = []string{"-token", "-format", "-verbose", "-scm", "-scm-base-uri", "-threads"}
4143

@@ -112,6 +114,7 @@ func init() {
112114
rootCmd.PersistentFlags().StringVarP(&ScmProvider, "scm", "s", "github", "SCM platform (github, gitlab)")
113115
rootCmd.PersistentFlags().VarP(&ScmBaseURL, "scm-base-url", "b", "Base URI of the self-hosted SCM instance (optional)")
114116
rootCmd.PersistentFlags().BoolVarP(&config.Quiet, "quiet", "q", false, "Disable progress output")
117+
rootCmd.PersistentFlags().StringSliceVar(&skipRules, "skip", []string{}, "Adds rules to the configured skip list for the current run (optional)")
115118

116119
viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))
117120
}
@@ -186,6 +189,9 @@ func GetAnalyzer(ctx context.Context, command string) (*analyze.Analyzer, error)
186189
}
187190

188191
func newOpa(ctx context.Context) (*opa.Opa, error) {
192+
if len(skipRules) > 0 {
193+
config.Skip = append(config.Skip, models.ConfigSkip{Rule: skipRules})
194+
}
189195
opaClient, err := opa.NewOpa(ctx, config)
190196
if err != nil {
191197
log.Error().Err(err).Msg("Failed to create OPA client")

models/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ type ConfigSkip struct {
99
Level StringList `json:"level,omitempty"`
1010
}
1111

12+
func (c *ConfigSkip) HasOnlyRule() bool {
13+
return len(c.Purl) == 0 &&
14+
len(c.Path) == 0 &&
15+
len(c.OsvId) == 0 &&
16+
len(c.Job) == 0 &&
17+
len(c.Level) == 0 &&
18+
len(c.Rule) != 0
19+
}
20+
1221
type ConfigInclude struct {
1322
Path StringList `json:"path,omitempty"`
1423
}

opa/opa.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import (
55
"embed"
66
"encoding/json"
77
"fmt"
8+
"io/fs"
9+
"os"
10+
"path/filepath"
11+
"slices"
12+
"strings"
13+
814
"github.com/boostsecurityio/poutine/models"
915
"github.com/open-policy-agent/opa/v1/ast"
1016
"github.com/open-policy-agent/opa/v1/loader"
@@ -13,9 +19,6 @@ import (
1319
"github.com/open-policy-agent/opa/v1/storage/inmem"
1420
"github.com/open-policy-agent/opa/v1/topdown/print"
1521
"github.com/rs/zerolog/log"
16-
"io/fs"
17-
"os"
18-
"strings"
1922
)
2023

2124
//go:embed rego
@@ -45,7 +48,14 @@ func NewOpa(ctx context.Context, config *models.Config) (*Opa, error) {
4548
return nil, fmt.Errorf("failed to set opa with config: %w", err)
4649
}
4750

48-
err = newOpa.Compile(ctx)
51+
subset := []string{}
52+
for _, skip := range config.Skip {
53+
if skip.HasOnlyRule() {
54+
subset = append(subset, skip.Rule...)
55+
}
56+
}
57+
58+
err = newOpa.Compile(ctx, subset)
4959
if err != nil {
5060
return nil, fmt.Errorf("failed to initialize opa compiler: %w", err)
5161
}
@@ -77,13 +87,22 @@ func (o *Opa) WithConfig(ctx context.Context, config *models.Config) error {
7787
)
7888
}
7989

80-
func (o *Opa) Compile(ctx context.Context) error {
90+
func (o *Opa) Compile(ctx context.Context, skip []string) error {
8191
modules := make(map[string]string)
8292
err := fs.WalkDir(regoFs, "rego", func(path string, d fs.DirEntry, err error) error {
8393
if d.IsDir() {
8494
return err
8595
}
8696

97+
if len(skip) != 0 {
98+
if filepath.Dir(path) == filepath.Join("rego", "rules") {
99+
filename := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
100+
if slices.Contains(skip, filename) {
101+
return nil
102+
}
103+
}
104+
}
105+
87106
content, err := regoFs.ReadFile(path)
88107
if err != nil {
89108
return err

0 commit comments

Comments
 (0)