-
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.
Flag empty "body" like `{}` as that's no longer considered a body. Also some light refactoring here, breaking out capabilities checks to a separate package. More to happen here soon! Fixes #451 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
21d0405
commit ec443db
Showing
16 changed files
with
153 additions
and
34 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,20 @@ | ||
package regal.capabilities | ||
|
||
import data.regal.config | ||
|
||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
default provided := {} | ||
|
||
# METADATA | ||
# description: | | ||
# The capabilities object for Regal itself. Use `config.capabilities` | ||
# to get the capabilities for the target environment (i.e. the policies | ||
# getting linted). | ||
provided := data.internal.capabilities | ||
|
||
# if if if! | ||
has_if if "if" in config.capabilities.future_keywords | ||
|
||
has_if if "rego_v1_import" in config.capabilities.features |
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,27 @@ | ||
# METADATA | ||
# description: Empty object following `if` | ||
package regal.rules.bugs["if-empty-object"] | ||
|
||
import future.keywords.contains | ||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
import data.regal.capabilities | ||
import data.regal.result | ||
|
||
# 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 | ||
|
||
count(rule.body) == 1 | ||
|
||
rule.body[0].terms.type == "object" | ||
rule.body[0].terms.value == [] | ||
|
||
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,33 @@ | ||
package regal.rules.bugs["if-empty-object_test"] | ||
|
||
import future.keywords.if | ||
import future.keywords.in | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.bugs["if-empty-object"] as rule | ||
|
||
test_fail_if_empty_object if { | ||
module := ast.with_future_keywords("rule if {}") | ||
r := rule.report with input as module | ||
r == {{ | ||
"category": "bugs", | ||
"description": "Empty object following `if`", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 8, "text": "rule if {}"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/if-empty-object", "bugs"), | ||
}], | ||
"title": "if-empty-object", | ||
}} | ||
} | ||
|
||
test_success_if_non_empty_object if { | ||
# this is arguably just as useless, but we'll defer | ||
# to the constant-condition rule for these cases | ||
module := ast.with_future_keywords(`rule if {"foo": "bar"}`) | ||
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
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,42 @@ | ||
# if-empty-object | ||
|
||
**Summary**: Empty object following `if` | ||
|
||
**Category**: Bugs | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
import future.keywords.if | ||
allow if {} | ||
``` | ||
|
||
## Rationale | ||
|
||
An empty rule body would previously be flagged as an error. With the introduction and use of the `if` keyword, that is | ||
no longer the case. In fact, empty `{}` is not considered a rule body _at all_, but rather an empty object, meaning | ||
that `if {}` will always evaluate. This is likely a mistake, and while hopefully caught by tests, should be avoided. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
bugs: | ||
if-empty-object: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## Related Resources | ||
- Regal Docs: [constant-condition](https://docs.styra.com/regal/rules/bugs/constant-condition) | ||
## Community | ||
If you think you've found a problem with this rule or its documentation, would like to suggest improvements, new rules, | ||
or just talk about Regal in general, please join us in the `#regal` channel in the Styra Community | ||
[Slack](https://communityinviter.com/apps/styracommunity/signup)! |
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