@@ -22,40 +22,21 @@ import (
2222 "fmt"
2323 "github.com/enescakir/emoji"
2424 "github.com/rabbitstack/fibratus/internal/bootstrap"
25- "github.com/rabbitstack/fibratus/pkg/config"
2625 "github.com/rabbitstack/fibratus/pkg/filter"
2726 "github.com/rabbitstack/fibratus/pkg/filter/fields"
28- "github.com/spf13/cobra"
2927 "path/filepath"
28+ "strings"
3029)
3130
32- var Command = & cobra.Command {
33- Use : "rules" ,
34- Short : "Validate, list, or search detection rules" ,
35- }
36-
37- var validateCmd = & cobra.Command {
38- Use : "validate" ,
39- Short : "Validate rules for structural and syntactic correctness" ,
40- RunE : validate ,
41- }
42-
43- var cfg = config .NewWithOpts (config .WithValidate ())
44-
45- func init () {
46- cfg .MustViperize (Command )
47- Command .AddCommand (validateCmd )
48- }
49-
50- func validate (cmd * cobra.Command , args []string ) error {
31+ func validateRules () error {
5132 if err := bootstrap .InitConfigAndLogger (cfg ); err != nil {
5233 return err
5334 }
5435
5536 isValidExt := func (path string ) bool {
5637 return filepath .Ext (path ) == ".yml" || filepath .Ext (path ) == ".yaml"
5738 }
58-
39+ // load macros and rules
5940 for _ , m := range cfg .Filters .Macros .FromPaths {
6041 paths , err := filepath .Glob (m )
6142 if err != nil {
@@ -65,7 +46,7 @@ func validate(cmd *cobra.Command, args []string) error {
6546 if ! isValidExt (path ) {
6647 continue
6748 }
68- emo ("%v Loading macros from %s\n " , emoji .Magnet , path )
49+ emo ("%v Loading macros from %s\n " , emoji .Hook , path )
6950 }
7051 }
7152 if err := cfg .Filters .LoadMacros (); err != nil {
@@ -87,31 +68,34 @@ func validate(cmd *cobra.Command, args []string) error {
8768 if err := cfg .Filters .LoadGroups (); err != nil {
8869 return fmt .Errorf ("%v %v" , emoji .DisappointedFace , err )
8970 }
71+ if len (cfg .GetRuleGroups ()) == 0 {
72+ return fmt .Errorf ("%v no rules found in %s" , emoji .DisappointedFace , strings .Join (cfg .Filters .Rules .FromPaths , "," ))
73+ }
9074
75+ warnings := make ([]string , 0 )
76+ // validate rule for every group
9177 for _ , group := range cfg .GetRuleGroups () {
9278 for _ , rule := range group .Rules {
9379 f := filter .New (rule .Condition , cfg )
9480 err := f .Compile ()
9581 if err != nil {
9682 return fmt .Errorf ("%v %v" , emoji .DisappointedFace , filter .ErrInvalidFilter (rule .Name , group .Name , err ))
9783 }
98- for _ , field := range f .GetFields () {
99- deprecated , d := fields .IsDeprecated (field )
100- if deprecated {
101- emo ("%v Deprecation: %s rule uses " +
102- "the [%s] field which was deprecated starting " +
103- "from version %s. " +
104- "Please consider migrating to %s field(s) " +
105- "because [%s] will be removed in future versions\n " ,
106- emoji .Warning , rule .Name , field , d .Since , d .Fields , field )
84+ for _ , fld := range f .GetFields () {
85+ if isDeprecated , dep := fields .IsDeprecated (fld ); isDeprecated {
86+ warnings = append (warnings ,
87+ fmt .Sprintf ("%s field deprecated in favor of %v in rule %s" , fld .String (), dep .Fields , rule .Name ))
10788 }
10889 }
10990 }
11091 }
92+ if len (warnings ) > 0 {
93+ for _ , warn := range warnings {
94+ emo ("%v %s\n " , emoji .Warning , warn )
95+ }
96+ fmt .Printf ("%d warning(s)\n " , len (warnings ))
97+ }
11198
112- emo ("%v Detection rules OK. Ready to go!" , emoji .Rocket )
113-
99+ emo ("%v Validation successful. Ready to go!" , emoji .Rocket )
114100 return nil
115101}
116-
117- func emo (s string , args ... any ) { fmt .Printf (s , args ... ) }
0 commit comments