-
Notifications
You must be signed in to change notification settings - Fork 2
feat: NAUT-1300 schema-only projection lint + 19 rule declarations #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b0d32c6
deps: bump armoapi-go to v0.0.707 for ProfileDataRequired schema
slashben a7d6d42
feat: add lint-projection schema-only lint tool
slashben b6f5a0c
test: golden testdata and Makefile for lint-projection
slashben 4a5e6db
feat: add profileDataRequired declarations to 19 rules
slashben 5eb8ff9
ci+docs: enforce lint-projection in CI and add authoring guide
slashben 85e4c59
fix: bump node-agent to 4aefa65 and update matthyx fork replacements
slashben 140b96f
docs: note C4 warning for NotRequired rules with profileDataRequired
slashben f934c37
lint: add C5 error for unrecognized profileDependency values
slashben File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,3 @@ | ||
| .PHONY: lint-projection | ||
| lint-projection: | ||
| go run ./cmd/lint-projection ./pkg/rules |
This file contains hidden or 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 hidden or 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,140 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/armosec/armoapi-go/armotypes" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| type Severity string | ||
|
|
||
| const ( | ||
| SeverityError Severity = "ERROR" | ||
| SeverityWarn Severity = "WARN" | ||
| ) | ||
|
|
||
| type Finding struct { | ||
| File string | ||
| Line int | ||
| RuleID string | ||
| Severity Severity | ||
| Check string | ||
| Message string | ||
| } | ||
|
|
||
| func (f Finding) String() string { | ||
| return fmt.Sprintf("%s:%d: %s: %s: %s: %s", f.File, f.Line, f.RuleID, f.Severity, f.Check, f.Message) | ||
| } | ||
|
|
||
| // ruleDoc mirrors the relevant fields of a kubescape/rulelibrary rule YAML. | ||
| // We only need profileDependency and profileDataRequired here. | ||
| type ruleDoc struct { | ||
| Spec struct { | ||
| Rules []struct { | ||
| ID string `yaml:"id"` | ||
| ProfileDependency armotypes.ProfileDependency `yaml:"profileDependency"` | ||
| ProfileDataRequired *armotypes.ProfileDataRequired `yaml:"profileDataRequired,omitempty"` | ||
| } `yaml:"rules"` | ||
| } `yaml:"spec"` | ||
| } | ||
|
|
||
| func lintFiles(files []string) []Finding { | ||
| var findings []Finding | ||
| for _, path := range files { | ||
| findings = append(findings, lintFile(path)...) | ||
| } | ||
| return findings | ||
| } | ||
|
|
||
| func lintFile(path string) []Finding { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return []Finding{{File: path, Line: 0, RuleID: "?", Severity: SeverityError, Check: "C3", Message: fmt.Sprintf("read failed: %v", err)}} | ||
| } | ||
| var doc ruleDoc | ||
| if err := yaml.Unmarshal(data, &doc); err != nil { | ||
| return []Finding{{File: path, Line: 0, RuleID: "?", Severity: SeverityError, Check: "C3", Message: fmt.Sprintf("yaml unmarshal: %v", err)}} | ||
| } | ||
| lineByID := indexRuleLines(data) | ||
|
|
||
| var findings []Finding | ||
| for _, r := range doc.Spec.Rules { | ||
| line := lineByID[r.ID] | ||
| findings = append(findings, checkRule(path, line, r.ID, r.ProfileDependency, r.ProfileDataRequired)...) | ||
| } | ||
| return findings | ||
| } | ||
|
|
||
| func checkRule(path string, line int, id string, dep armotypes.ProfileDependency, pdr *armotypes.ProfileDataRequired) []Finding { | ||
| var out []Finding | ||
| switch dep { | ||
|
slashben marked this conversation as resolved.
|
||
| case armotypes.Required, armotypes.Optional: | ||
| if pdr == nil || pdr.IsEmpty() { | ||
| out = append(out, Finding{File: path, Line: line, RuleID: id, Severity: SeverityError, Check: "C1", | ||
| Message: fmt.Sprintf("rule has profileDependency=%v; profileDataRequired must be present and declare at least one surface", profileDependencyName(dep))}) | ||
| } | ||
| case armotypes.NotRequired: | ||
| if pdr != nil { | ||
| out = append(out, Finding{File: path, Line: line, RuleID: id, Severity: SeverityWarn, Check: "C4", | ||
| Message: "rule has profileDependency=NotRequired but declares profileDataRequired; if the rule does not query profile data, remove the declaration"}) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| default: | ||
| out = append(out, Finding{File: path, Line: line, RuleID: id, Severity: SeverityError, Check: "C5", | ||
| Message: fmt.Sprintf("rule has unrecognized profileDependency value %d; must be 0 (Required), 1 (Optional), or 2 (NotRequired)", int(dep))}) | ||
| } | ||
| if pdr != nil && !pdr.IsEmpty() { | ||
| if err := pdr.Validate(); err != nil { | ||
| out = append(out, Finding{File: path, Line: line, RuleID: id, Severity: SeverityError, Check: "C2", Message: err.Error()}) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func profileDependencyName(d armotypes.ProfileDependency) string { | ||
| switch d { | ||
| case armotypes.Required: | ||
| return "Required" | ||
| case armotypes.Optional: | ||
| return "Optional" | ||
| case armotypes.NotRequired: | ||
| return "NotRequired" | ||
| } | ||
| return fmt.Sprintf("Unknown(%d)", d) | ||
| } | ||
|
|
||
| func indexRuleLines(data []byte) map[string]int { | ||
| out := map[string]int{} | ||
| var doc yaml.Node | ||
| if err := yaml.Unmarshal(data, &doc); err != nil { | ||
| return out | ||
| } | ||
| if doc.Kind != yaml.DocumentNode || len(doc.Content) == 0 { | ||
| return out | ||
| } | ||
| root := doc.Content[0] | ||
| spec := findMappingValue(root, "spec") | ||
| rules := findMappingValue(spec, "rules") | ||
| if rules == nil || rules.Kind != yaml.SequenceNode { | ||
| return out | ||
| } | ||
| for _, ruleMap := range rules.Content { | ||
| if id := findMappingValue(ruleMap, "id"); id != nil { | ||
| out[id.Value] = id.Line | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func findMappingValue(n *yaml.Node, key string) *yaml.Node { | ||
| if n == nil || n.Kind != yaml.MappingNode { | ||
| return nil | ||
| } | ||
| for i := 0; i+1 < len(n.Content); i += 2 { | ||
| if n.Content[i].Value == key { | ||
| return n.Content[i+1] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,54 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestLintFile(t *testing.T) { | ||
| cases := []struct { | ||
| path string | ||
| wantSeverities []Severity | ||
| wantSubstrs []string | ||
| }{ | ||
| {"testdata/valid-required.yaml", nil, nil}, | ||
| {"testdata/valid-optional-all.yaml", nil, nil}, | ||
| {"testdata/valid-notrequired-no-decl.yaml", nil, nil}, | ||
| {"testdata/error-required-missing.yaml", []Severity{SeverityError}, []string{"C1", "must be present"}}, | ||
| {"testdata/error-required-empty.yaml", []Severity{SeverityError}, []string{"C1", "at least one surface"}}, | ||
| {"testdata/error-bad-pattern-multikey.yaml", []Severity{SeverityError}, []string{"C2", "exactly one"}}, | ||
| {"testdata/error-field-empty.yaml", []Severity{SeverityError}, []string{"C2", "at least one pattern"}}, | ||
| {"testdata/warn-notrequired-with-decl.yaml", []Severity{SeverityWarn}, []string{"C4"}}, | ||
| {"testdata/error-bad-dependency-value.yaml", []Severity{SeverityError}, []string{"C5", "unrecognized profileDependency"}}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.path, func(t *testing.T) { | ||
| findings := lintFile(c.path) | ||
| require.Len(t, findings, len(c.wantSeverities), "unexpected finding count: %v", findings) | ||
| for i, sev := range c.wantSeverities { | ||
| assert.Equal(t, sev, findings[i].Severity) | ||
| } | ||
| joined := "" | ||
| for _, f := range findings { | ||
| joined += f.String() + "\n" | ||
| } | ||
| for _, s := range c.wantSubstrs { | ||
| assert.True(t, strings.Contains(joined, s), "expected %q in output:\n%s", s, joined) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLintExitCode(t *testing.T) { | ||
| findings := lintFiles([]string{"testdata/valid-required.yaml", "testdata/error-required-missing.yaml"}) | ||
| hasError := false | ||
| for _, f := range findings { | ||
| if f.Severity == SeverityError { | ||
| hasError = true | ||
| } | ||
| } | ||
| assert.True(t, hasError) | ||
| } |
This file contains hidden or 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,50 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| func main() { | ||
| if len(os.Args) < 2 { | ||
| fmt.Fprintln(os.Stderr, "usage: lint-projection DIR [DIR ...]") | ||
| os.Exit(2) | ||
| } | ||
| var files []string | ||
| for _, dir := range os.Args[1:] { | ||
| matches, err := walkYAMLs(dir) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "walk %s: %v\n", dir, err) | ||
| os.Exit(2) | ||
| } | ||
| files = append(files, matches...) | ||
| } | ||
| findings := lintFiles(files) | ||
| for _, f := range findings { | ||
| fmt.Println(f.String()) | ||
| } | ||
| for _, f := range findings { | ||
| if f.Severity == SeverityError { | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func walkYAMLs(root string) ([]string, error) { | ||
| var out []string | ||
| err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if d.IsDir() { | ||
| return nil | ||
| } | ||
| if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") { | ||
| out = append(out, path) | ||
| } | ||
| return nil | ||
| }) | ||
| return out, err | ||
| } |
This file contains hidden or 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,4 @@ | ||
| spec: | ||
| rules: | ||
| - id: R9999 | ||
| profileDependency: 99 |
This file contains hidden or 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,8 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0002 | ||
| profileDependency: 0 | ||
| profileDataRequired: | ||
| opens: | ||
| - exact: "/a" | ||
| prefix: "/b" |
This file contains hidden or 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,6 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0002 | ||
| profileDependency: 0 | ||
| profileDataRequired: | ||
| opens: [] |
This file contains hidden or 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,5 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0001 | ||
| profileDependency: 0 | ||
| profileDataRequired: {} |
This file contains hidden or 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,4 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0001 | ||
| profileDependency: 0 |
This file contains hidden or 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,4 @@ | ||
| spec: | ||
| rules: | ||
| - id: R1000 | ||
| profileDependency: 2 |
This file contains hidden or 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,7 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0010 | ||
| profileDependency: 1 | ||
| profileDataRequired: | ||
| opens: | ||
| - prefix: "/etc/shadow" |
This file contains hidden or 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,6 @@ | ||
| spec: | ||
| rules: | ||
| - id: R0001 | ||
| profileDependency: 0 | ||
| profileDataRequired: | ||
| execs: all |
This file contains hidden or 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,7 @@ | ||
| spec: | ||
| rules: | ||
| - id: R1000 | ||
| profileDependency: 2 | ||
| profileDataRequired: | ||
| opens: | ||
| - exact: "/a" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.