Skip to content

Commit

Permalink
Use import rego.v1 (#471)
Browse files Browse the repository at this point in the history
Note that for now, this change only affects what
we use internally in Regal, and not what we enforce.

This will be done too in due time, but given that
`rego_v1_import` isn't a capability in any released
version of OPA yet, we should hold off on recommending
`import rego.v1` until there is, and the feature works
without known bugs, which will be from the next release.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Nov 16, 2023
1 parent 459bcc8 commit 5b3855f
Show file tree
Hide file tree
Showing 144 changed files with 602 additions and 499 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
go-version-file: go.mod
- uses: open-policy-agent/setup-opa@v2
with:
version: latest
version: edge
- run: go install git.sr.ht/~charles/rq/cmd/rq@latest
- run: build/do.rq pull_request
- uses: golangci/[email protected]
Expand All @@ -36,7 +36,7 @@ jobs:
go-version-file: go.mod
- uses: open-policy-agent/setup-opa@v2
with:
version: latest
version: edge
# rq doesn't work well on windows currently, or at least
# so it seemed, so until we've resolved that together with
# the rq maintainers, we'll just run the build steps directly
Expand Down
5 changes: 3 additions & 2 deletions build/capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -6013,8 +6013,9 @@
"if",
"in"
],
"wasm_abi_versions": null,
"features": [
"rule_head_ref_string_prefixes"
"rule_head_ref_string_prefixes",
"rule_head_refs",
"rego_v1_import"
]
}
12 changes: 3 additions & 9 deletions bundle/regal/ast.rego
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package regal.ast

import future.keywords.contains
import future.keywords.every
import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.config

Expand Down Expand Up @@ -102,13 +99,10 @@ policy(snippet) := regal.parse_module("policy.rego", concat("", [

# METADATA
# description: parses provided policy with all future keywords imported. Primarily for testing.
with_future_keywords(policy) := regal.parse_module("policy.rego", concat("", [
with_rego_v1(policy) := regal.parse_module("policy.rego", concat("", [
`package policy
import future.keywords.contains
import future.keywords.every
import future.keywords.if
import future.keywords.in
import rego.v1
`,
policy,
Expand Down
33 changes: 18 additions & 15 deletions bundle/regal/ast_test.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package regal.ast_test

import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.ast

Expand All @@ -10,7 +9,7 @@ test_find_vars if {
policy := `
package p
import future.keywords
import rego.v1
global := "foo"
Expand Down Expand Up @@ -60,7 +59,7 @@ test_find_vars_comprehension_lhs if {
policy := `
package p
import future.keywords
import rego.v1
allow if {
a := [b | input[b]]
Expand All @@ -86,7 +85,7 @@ test_find_vars_comprehension_lhs if {
test_function_decls_multiple_same_name if {
policy := `package p
import future.keywords.if
import rego.v1
f(x) := x if true
f(y) := y if false
Expand Down Expand Up @@ -134,11 +133,11 @@ test_find_vars_in_local_scope if {
policy := `
package p
import future.keywords
import rego.v1
global := "foo"
allow {
allow if {
a := global
b := [c | c := input[d]]
Expand Down Expand Up @@ -172,9 +171,9 @@ test_find_vars_in_local_scope_complex_comprehension_term if {
policy := `
package p
import future.keywords
import rego.v1
allow {
allow if {
a := [{"b": b} | c := input[b]]
}`

Expand All @@ -194,15 +193,15 @@ test_find_names_in_scope if {
policy := `
package p
import future.keywords
import rego.v1
bar := "baz"
global := "foo"
comp := [foo | foo := input[_]]
allow {
allow if {
a := global
b := [c | c := input[_]]
Expand All @@ -224,7 +223,9 @@ test_find_some_decl_vars if {
policy := `
package p
allow {
import rego.v1
allow if {
foo := 1
some x
input[x]
Expand All @@ -242,7 +243,9 @@ test_find_some_decl_vars if {
test_find_some_decl_names_in_scope if {
policy := `package p
allow {
import rego.v1
allow if {
foo := 1
some x
input[x]
Expand All @@ -252,8 +255,8 @@ test_find_some_decl_names_in_scope if {

module := regal.parse_module("p.rego", policy)

ast.find_some_decl_names_in_scope(module.rules[0], {"col": 1, "row": 6}) == {"x"}
ast.find_some_decl_names_in_scope(module.rules[0], {"col": 1, "row": 8}) == {"x", "y", "z"}
ast.find_some_decl_names_in_scope(module.rules[0], {"col": 1, "row": 8}) == {"x"}
ast.find_some_decl_names_in_scope(module.rules[0], {"col": 1, "row": 10}) == {"x", "y", "z"}
}

var_names(vars) := {var.value | some var in vars}
Expand Down
3 changes: 1 addition & 2 deletions bundle/regal/capabilities.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package regal.capabilities

import data.regal.config

import future.keywords.if
import future.keywords.in
import rego.v1

default provided := {}

Expand Down
3 changes: 1 addition & 2 deletions bundle/regal/config/config.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package regal.config

import future.keywords.if
import future.keywords.in
import rego.v1

docs["base_url"] := "https://docs.styra.com/regal/rules"

Expand Down
2 changes: 1 addition & 1 deletion bundle/regal/config/config_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package regal.config_test

import future.keywords.if
import rego.v1

import data.regal.config

Expand Down
3 changes: 1 addition & 2 deletions bundle/regal/config/exclusion.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package regal.config

import future.keywords.if
import future.keywords.in
import rego.v1

excluded_file(category, title, file) if {
force_exclude_file(file)
Expand Down
2 changes: 1 addition & 1 deletion bundle/regal/config/exclusion_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package regal.config_test

import future.keywords.if
import rego.v1

import data.regal.config

Expand Down
4 changes: 1 addition & 3 deletions bundle/regal/main.rego
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package regal.main

import future.keywords.contains
import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.ast
import data.regal.config
Expand Down
2 changes: 1 addition & 1 deletion bundle/regal/main_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package regal.main_test

import future.keywords.if
import rego.v1

import data.regal.config
import data.regal.main
Expand Down
3 changes: 1 addition & 2 deletions bundle/regal/result.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package regal.result

import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.config

Expand Down
2 changes: 1 addition & 1 deletion bundle/regal/result_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package regal.result_test

import future.keywords.if
import rego.v1

import data.regal.config
import data.regal.result
Expand Down
4 changes: 1 addition & 3 deletions bundle/regal/rules/bugs/constant_condition.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# description: Constant condition
package regal.rules.bugs["constant-condition"]

import future.keywords.contains
import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.ast
import data.regal.result
Expand Down
4 changes: 2 additions & 2 deletions bundle/regal/rules/bugs/constant_condition_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package regal.rules.bugs["constant-condition_test"]

import future.keywords.if
import rego.v1

import data.regal.ast
import data.regal.config
Expand Down Expand Up @@ -67,6 +67,6 @@ test_success_non_constant_condition if {
}

test_success_adding_constant_to_set if {
r := rule.report with input as ast.with_future_keywords(`rule contains "message"`)
r := rule.report with input as ast.with_rego_v1(`rule contains "message"`)
r == set()
}
4 changes: 1 addition & 3 deletions bundle/regal/rules/bugs/if_empty_object.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# 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 rego.v1

import data.regal.capabilities
import data.regal.result
Expand Down
9 changes: 4 additions & 5 deletions bundle/regal/rules/bugs/if_empty_object_test.rego
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package regal.rules.bugs["if-empty-object_test"]

import future.keywords.if
import future.keywords.in
import rego.v1

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 {}")
module := ast.with_rego_v1("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 {}"},
"location": {"col": 1, "file": "policy.rego", "row": 5, "text": "rule if {}"},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/if-empty-object", "bugs"),
Expand All @@ -27,7 +26,7 @@ test_fail_if_empty_object if {
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"}`)
module := ast.with_rego_v1(`rule if {"foo": "bar"}`)
r := rule.report with input as module
r == set()
}
4 changes: 1 addition & 3 deletions bundle/regal/rules/bugs/inconsistent_args.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# description: Inconsistently named function arguments
package regal.rules.bugs["inconsistent-args"]

import future.keywords.contains
import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.ast
import data.regal.result
Expand Down
17 changes: 8 additions & 9 deletions bundle/regal/rules/bugs/inconsistent_args_test.rego
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package regal.rules.bugs["inconsistent-args_test"]

import future.keywords.if
import future.keywords.in
import rego.v1

import data.regal.ast

import data.regal.rules.bugs["inconsistent-args"] as rule

test_fail_inconsistent_args if {
module := ast.with_future_keywords(`
module := ast.with_rego_v1(`
foo(a, b) if a == b
foo(b, a) if b > a
bar(b, a) if b > a
`)
r := rule.report with input as module
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\tfoo(b, a) if b > a"})
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 7, "text": "\tfoo(b, a) if b > a"})
}

test_fail_nested_inconsistent_args if {
module := ast.with_future_keywords(`
module := ast.with_rego_v1(`
a.b.foo(a, b) if a == b
a.b.foo(b, a) if b > a
`)
r := rule.report with input as module
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\ta.b.foo(b, a) if b > a"})
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 7, "text": "\ta.b.foo(b, a) if b > a"})
}

test_success_not_inconsistent_args if {
module := ast.with_future_keywords(`
module := ast.with_rego_v1(`
foo(a, b) if a == b
foo(a, b) if a > b
Expand All @@ -42,7 +41,7 @@ test_success_not_inconsistent_args if {
}

test_success_using_wildcard if {
module := ast.with_future_keywords(`
module := ast.with_rego_v1(`
foo(a, b) if a == b
foo(_, b) if b.foo
Expand All @@ -53,7 +52,7 @@ test_success_using_wildcard if {
}

test_success_using_pattern_matching if {
module := ast.with_future_keywords(`
module := ast.with_rego_v1(`
foo(a, b) if a == b
foo(a, "foo") if a.foo
Expand Down
Loading

0 comments on commit 5b3855f

Please sign in to comment.