-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #778 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
f9e8949
commit 47c6247
Showing
14 changed files
with
322 additions
and
16 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
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
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,57 @@ | ||
# METADATA | ||
# description: Ambiguous metadata scope | ||
package regal.rules.idiomatic["ambiguous-scope"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
import data.regal.util | ||
|
||
report contains violation if { | ||
some name, rules in _incremental_rules | ||
|
||
# should internal rules have metadata at all? not sure, but | ||
# let's loosen up the restrictions on them for the moment | ||
not startswith(name, "_") | ||
|
||
rules_with_annotations := [rule | | ||
some rule in rules | ||
|
||
rule.annotations | ||
] | ||
|
||
# a single rule with a single annotation — that means it is also | ||
# has scope == rule as if it had a scope == document it would be seen | ||
# on each of the rules | ||
count(rules_with_annotations) == 1 | ||
count(rules_with_annotations[0].annotations) == 1 | ||
|
||
annotation := rules_with_annotations[0].annotations[0] | ||
|
||
# treat this as an exception for now — it's arguably an issue in itself | ||
# that an entrypoint can be scoped to "rule", as clearly that's not how | ||
# entrypoints work | ||
not annotation.entrypoint | ||
|
||
not _explicit_scope(rules_with_annotations[0], input.regal.file.lines) | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(annotation)) | ||
} | ||
|
||
_incremental_rules[name] contains rule if { | ||
some rule in input.rules | ||
|
||
name := ast.ref_to_string(rule.head.ref) | ||
|
||
util.has_duplicates(_rule_names, name) | ||
} | ||
|
||
_rule_names := [ast.ref_to_string(rule.head.ref) | some rule in input.rules] | ||
|
||
_explicit_scope(rule, lines) if { | ||
some i in numbers.range(rule.annotations[0].location.row - 1, rule.head.location.row - 2) | ||
line := lines[i] | ||
|
||
startswith(line, "# scope:") | ||
} |
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,127 @@ | ||
package regal.rules.idiomatic["ambiguous-scope_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.idiomatic["ambiguous-scope"] as rule | ||
|
||
test_ambiguous_scope_lonely_annotated_rule if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# title: allow | ||
allow if input.x | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Ambiguous metadata scope", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 6, "text": "# METADATA"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/ambiguous-scope", "idiomatic"), | ||
}], | ||
"title": "ambiguous-scope", | ||
}} | ||
} | ||
|
||
test_ambiguous_scope_lonely_annotated_function if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# title: func | ||
func(x) if x < 10 | ||
func(x) if x < "10" | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Ambiguous metadata scope", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 6, "text": "# METADATA"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/ambiguous-scope", "idiomatic"), | ||
}], | ||
"title": "ambiguous-scope", | ||
}} | ||
} | ||
|
||
test_not_ambiguous_scope_all_scoped_to_rule if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# description: input has x | ||
allow if input.x | ||
# METADATA | ||
# description: input has y | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_not_ambiguous_document_scope if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# description: input has x or y | ||
# scope: document | ||
allow if input.x | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_not_ambiguous_entrypoint_exception if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# entrypoint: true | ||
allow if input.x | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_not_ambiguous_both_document_and_rule if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# description: input has x or y | ||
# scope: document | ||
# METADATA | ||
# description: input has x | ||
allow if input.x | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_not_ambiguous_explicit_rule_scope if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# description: input has x | ||
# scope: rule | ||
allow if input.x | ||
allow if input.y | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} |
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
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
Oops, something went wrong.