-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure internal rules aren't used as entrypoints. Fixes #783 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
2c095e6
commit 86c28d7
Showing
7 changed files
with
132 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# METADATA | ||
# description: Entrypoint can't be marked internal | ||
package regal.rules.bugs["internal-entrypoint"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
report contains violation if { | ||
some rule in ast.rules | ||
some annotation in rule.annotations | ||
|
||
annotation.entrypoint == true | ||
startswith(ast.ref_to_string(rule.head.ref), "_") | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule.head)) | ||
} |
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 @@ | ||
package regal.rules.bugs["internal-entrypoint_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.bugs["internal-entrypoint"] as rule | ||
|
||
test_fail_internal_entrypoint if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# entrypoint: true | ||
_allow := true | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "bugs", | ||
"description": "Entrypoint can't be marked internal", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 9, "text": "_allow := true"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/internal-entrypoint", "bugs"), | ||
}], | ||
"title": "internal-entrypoint", | ||
}} | ||
} | ||
|
||
test_success_non_internal_entrypoint if { | ||
module := ast.with_rego_v1(` | ||
# METADATA | ||
# entrypoint: true | ||
allow := true | ||
`) | ||
|
||
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,62 @@ | ||
# internal-entrypoint | ||
|
||
**Summary**: Entrypoint can't be marked internal | ||
|
||
**Category**: Bugs | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
# METADATA | ||
# entrypoint: true | ||
_authorized if { | ||
# some conditions | ||
} | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
# METADATA | ||
# entrypoint: true | ||
allow if _authorized | ||
_authorized if { | ||
# some conditions | ||
} | ||
``` | ||
|
||
## Rationale | ||
|
||
Rules marked as internal using the [underscore prefix convention](https://docs.styra.com/opa/rego-style-guide#optionally-use-leading-underscore-for-rules-intended-for-internal-use) | ||
cannot be used as entrypoints, as entrypoints by definition are public. Either rename the rule to mark it as public, | ||
or use another public rule as an entrypoint, which may reference the internal rule. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
bugs: | ||
internal-entrypoint: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## Related Resources | ||
- Rego Style Guide: [Optionally, use leading underscore for rules intended for internal use](https://docs.styra.com/opa/rego-style-guide#optionally-use-leading-underscore-for-rules-intended-for-internal-use) | ||
- Regal Docs: [no-defined-entrypoint](https://docs.styra.com/regal/rules/idiomatic/no-defined-entrypoint) | ||
## 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
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