-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recommend the use of `if` and `contains` when available as capabilities. Some fixes to tests where we didn't live up to this standard ourselves. Great! Fixes #468 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
b36f696
commit 660dc30
Showing
16 changed files
with
388 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# METADATA | ||
# description: Use the `contains` keyword | ||
package regal.rules.idiomatic["use-contains"] | ||
|
||
import future.keywords.contains | ||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
import data.regal.ast | ||
import data.regal.capabilities | ||
import data.regal.result | ||
|
||
# METADATA | ||
# description: Missing capability for keyword `contains` | ||
# custom: | ||
# severity: warning | ||
notices contains result.notice(rego.metadata.chain()) if not capabilities.has_contains | ||
|
||
report contains violation if { | ||
some rule in ast.rules | ||
|
||
rule.head.key | ||
not rule.head.value | ||
|
||
text := base64.decode(rule.head.location.text) | ||
|
||
not contains(text, " contains ") | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} |
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,47 @@ | ||
package regal.rules.idiomatic["use-contains_test"] | ||
|
||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.idiomatic["use-contains"] as rule | ||
|
||
test_fail_should_use_contains if { | ||
module := ast.with_future_keywords(`rule[item] { | ||
some item in input.items | ||
}`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Use the `contains` keyword", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 8, "text": "rule[item] {"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/use-contains", "idiomatic"), | ||
}], | ||
"title": "use-contains", | ||
}} | ||
} | ||
|
||
test_success_uses_contains if { | ||
module := ast.with_future_keywords(`rule contains item if { | ||
some item in input.items | ||
}`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_success_object_rule if { | ||
module := ast.with_future_keywords(`rule[foo] := bar if { | ||
foo := "bar" | ||
bar := "baz" | ||
}`) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# METADATA | ||
# description: Use the `if` keyword | ||
package regal.rules.idiomatic["use-if"] | ||
|
||
import future.keywords.contains | ||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
import data.regal.ast | ||
import data.regal.capabilities | ||
import data.regal.result | ||
|
||
# Note: think more about what UX we want when import_rego_v1 | ||
# capbility is available. Should we simply just recommend that | ||
# and silence this rule in that case? I'm inclined to say yes. | ||
|
||
# METADATA | ||
# description: Missing capability for keyword `if` | ||
# custom: | ||
# severity: warning | ||
notices contains result.notice(rego.metadata.chain()) if not capabilities.has_if | ||
|
||
report contains violation if { | ||
some rule in input.rules | ||
|
||
not ast.generated_body(rule) | ||
|
||
head_len := count(base64.decode(rule.head.location.text)) | ||
text := trim_space(substring(base64.decode(rule.location.text), head_len, -1)) | ||
|
||
not startswith(text, "if") | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} |
Oops, something went wrong.