Enable test framework to test rego-format ruletypes - #6639
#6639Enable test framework to test rego-format ruletypes#6639evankanderson wants to merge 4 commits intomindersec:mainmindersec/minder:mainfrom evankanderson:rego-test-frameworkevankanderson/minder:rego-test-frameworkCopy head branch name to clipboard
Conversation
|
I'd like to get this in alongside #6637 for a v0.3.0 client tools release (which would include the exit-code updates for |
| if err != nil { | ||
| return nil, fmt.Errorf("globbing rego files: %w", err) | ||
| } | ||
| ruleFiles := append(yamlFiles, regoFiles...) |
There was a problem hiding this comment.
Minor nit: append(yamlFiles, regoFiles...) may reuse yamlFiles's backing array if it has excess capacity, silently clobbering it. filepath.Glob returns a tightly-sized slice in practice so this is safe today, but it's a fragile assumption. Consider:
ruleFiles := make([]string, 0, len(yamlFiles)+len(regoFiles))
ruleFiles = append(ruleFiles, yamlFiles...)
ruleFiles = append(ruleFiles, regoFiles...)| for _, yf := range ruleFiles { | ||
| rt, err := loadSingleRule(yf) | ||
| if err != nil { | ||
| continue // skip files that aren't valid rule types |
There was a problem hiding this comment.
This silent continue was fine for YAML (where non-ruletype YAML files legitimately live in directories). For .rego files the assumption is much stronger -- any .rego file in a testdata directory is almost certainly meant to be a ruletype, so swallowing the parse error gives no signal when metadata is malformed. At minimum a log.Printf or zerolog warn here would save a lot of head-scratching.
| rt.Context = &minderv1.Context{} | ||
| } | ||
| if rt.Context.GetProject() == "" { | ||
| rt.Context.Project = ptr.Ptr(uuid.UUID{}.String()) |
There was a problem hiding this comment.
The zero UUID is a fine sentinel, but uuid.UUID{}.String() is a somewhat roundabout way to write it. It constructs a value type just to call String(). The literal "00000000-0000-0000-0000-000000000000" (used in the deleted YAML file) is clearer and avoids the uuid import entirely:
rt.Context.Project = ptr.Ptr("00000000-0000-0000-0000-000000000000")| } | ||
| // Ensure that ruletypes have a project context; the Minder CLI + API tooling | ||
| // will automatically apply the "current project" when creating a ruletype. | ||
| if rt.Context == nil { |
There was a problem hiding this comment.
The previous code guarded this with if rt != nil. ReadResourceTyped can't actually return (nil, nil) today (it always returns a pointer), so removing the guard is technically correct -- but it's a non-obvious invariant. A short comment here would prevent someone from reflexively restoring the guard in a future cleanup:
// rt is never nil when err is nil; ReadResource always returns a pointer value.| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # METADATA | ||
| # |
There was a problem hiding this comment.
Trailing space on a blank comment line (# instead of #). The parser handles it fine, but OPA convention (and most editors) use a bare # for blank comment lines. Worth trimming.
Summary
Enables the
mindev testcommand to discover and load ruletypes stored as.regofiles in addition to the existing support for.yamlfiles.Testing
Updated the existing tests to migrate one (of two) ruletype definitions from
.yamlto.rego.