-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Rules validation command (#215)
- Loading branch information
1 parent
f033d3e
commit 2bba481
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: rules | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["master", "pr"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
validate: | ||
runs-on: windows-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- uses: actions/download-artifact@v3 | ||
with: | ||
name: fibratus-amd64.exe | ||
path: . | ||
- name: Validate rules | ||
run: | | ||
./fibratus-amd64.exe rules validate --filters.rules.from-paths=rules\* --filters.macros.from-paths=rules\macros\* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2021-2022 by Nedim Sabic Sabic | ||
* https://www.fibratus.io | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"github.com/enescakir/emoji" | ||
"github.com/rabbitstack/fibratus/internal/bootstrap" | ||
"github.com/rabbitstack/fibratus/pkg/config" | ||
"github.com/rabbitstack/fibratus/pkg/filter" | ||
"github.com/rabbitstack/fibratus/pkg/filter/fields" | ||
"github.com/spf13/cobra" | ||
"path/filepath" | ||
) | ||
|
||
var Command = &cobra.Command{ | ||
Use: "rules", | ||
Short: "Validate, list, or search detection rules", | ||
} | ||
|
||
var validateCmd = &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate rules for structural and syntactic correctness", | ||
RunE: validate, | ||
} | ||
|
||
var cfg = config.NewWithOpts(config.WithValidate()) | ||
|
||
func init() { | ||
cfg.MustViperize(Command) | ||
Command.AddCommand(validateCmd) | ||
} | ||
|
||
func validate(cmd *cobra.Command, args []string) error { | ||
if err := bootstrap.InitConfigAndLogger(cfg); err != nil { | ||
return err | ||
} | ||
|
||
isValidExt := func(path string) bool { | ||
return filepath.Ext(path) == ".yml" || filepath.Ext(path) == ".yaml" | ||
} | ||
|
||
for _, m := range cfg.Filters.Macros.FromPaths { | ||
paths, err := filepath.Glob(m) | ||
if err != nil { | ||
return err | ||
} | ||
for _, path := range paths { | ||
if !isValidExt(path) { | ||
continue | ||
} | ||
emo("%v Loading macros from %s\n", emoji.Magnet, path) | ||
} | ||
} | ||
if err := cfg.Filters.LoadMacros(); err != nil { | ||
return fmt.Errorf("%v %v", emoji.DisappointedFace, err) | ||
} | ||
|
||
for _, r := range cfg.Filters.Rules.FromPaths { | ||
paths, err := filepath.Glob(r) | ||
if err != nil { | ||
return err | ||
} | ||
for _, path := range paths { | ||
if !isValidExt(path) { | ||
continue | ||
} | ||
emo("%v Loading rules from %s\n", emoji.Package, path) | ||
} | ||
} | ||
if err := cfg.Filters.LoadGroups(); err != nil { | ||
return fmt.Errorf("%v %v", emoji.DisappointedFace, err) | ||
} | ||
|
||
for _, group := range cfg.GetRuleGroups() { | ||
for _, rule := range group.Rules { | ||
f := filter.New(rule.Condition, cfg) | ||
err := f.Compile() | ||
if err != nil { | ||
return fmt.Errorf("%v %v", emoji.DisappointedFace, filter.ErrInvalidFilter(rule.Name, group.Name, err)) | ||
} | ||
for _, field := range f.GetFields() { | ||
deprecated, d := fields.IsDeprecated(field) | ||
if deprecated { | ||
emo("%v Deprecation: %s rule uses "+ | ||
"the [%s] field which was deprecated starting "+ | ||
"from version %s. "+ | ||
"Please consider migrating to %s field(s) "+ | ||
"because [%s] will be removed in future versions\n", | ||
emoji.Warning, rule.Name, field, d.Since, d.Fields, field) | ||
} | ||
} | ||
} | ||
} | ||
|
||
emo("%v Detection rules OK. Ready to go!", emoji.Rocket) | ||
|
||
return nil | ||
} | ||
|
||
func emo(s string, args ...any) { fmt.Printf(s, args...) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters