-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include metrics about number of custom rules
- Loading branch information
1 parent
bc2df8a
commit 34bec46
Showing
4 changed files
with
218 additions
and
1 deletion.
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,55 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/open-policy-agent/opa/loader" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func RetrieveRules(path string) ([]string, error) { | ||
result, err := loader.AllRegos([]string{path}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fileNames := []string{} | ||
for _, module := range result.Modules { | ||
fileName := filepath.Clean(module.Name) | ||
if !strings.Contains(fileName, "_test.rego") { | ||
fileNames = append(fileNames, fileName) | ||
} | ||
} | ||
|
||
var publicIds = []string{} | ||
for _, fileName := range fileNames { | ||
publicId, err := getPublicIdFromFile(fileName) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
if publicId != "" { | ||
publicIds = append(publicIds, publicId) | ||
} | ||
} | ||
return publicIds, nil | ||
} | ||
|
||
func getPublicIdFromFile(fileName string) (string, error) { | ||
data, err := os.ReadFile(fileName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
publicId := extractPublicIdFromRego(string(data)) | ||
return publicId, nil | ||
} | ||
|
||
func extractPublicIdFromRego(rego string) string { | ||
re := regexp.MustCompile("\"publicId\"\\s*:\\s*\"(.*?)\"") | ||
match := re.FindStringSubmatch(rego) | ||
if len(match) > 0 { | ||
return match[1] | ||
} | ||
return "" | ||
} |
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,88 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/open-policy-agent/opa/util/test" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRetrieveRulesWitInvalidPath(t *testing.T) { | ||
_, err := RetrieveRules("./invalid") | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), "./invalid: no such file or directory") | ||
} | ||
|
||
func TestRetrieveRulesWithNoFiles(t *testing.T) { | ||
files := map[string]string{} | ||
test.WithTempFS(files, func(root string) { | ||
rules, err := RetrieveRules(root) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(rules)) | ||
}) | ||
} | ||
|
||
func TestRetrieveRulesWithNoRules(t *testing.T) { | ||
files := map[string]string{ | ||
"test.json": ` | ||
test | ||
`, | ||
} | ||
|
||
test.WithTempFS(files, func(root string) { | ||
rules, err := RetrieveRules(root) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(rules)) | ||
}) | ||
} | ||
|
||
func TestRetrieveRulesWithRulesWithoutPublicId(t *testing.T) { | ||
files := map[string]string{ | ||
"test.rego": ` | ||
package test | ||
msg = { | ||
"publicI": | ||
"1" | ||
} | ||
`, | ||
} | ||
|
||
test.WithTempFS(files, func(root string) { | ||
rules, err := RetrieveRules(root) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(rules)) | ||
}) | ||
} | ||
|
||
func TestRetrieveRulesWithRulesWithPublicId(t *testing.T) { | ||
files := map[string]string{ | ||
"test1.rego": ` | ||
package test | ||
msg = { | ||
"publicId": | ||
"1" | ||
} | ||
`, | ||
"test2.rego": ` | ||
package test | ||
msg = { | ||
"publicId": | ||
"2" | ||
} | ||
`, | ||
"test2_test.rego": ` | ||
package test | ||
msg = { | ||
"publicId": | ||
"3" | ||
} | ||
`, | ||
} | ||
|
||
test.WithTempFS(files, func(root string) { | ||
rules, err := RetrieveRules(root) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 2, len(rules)) | ||
assert.Equal(t, "1", rules[0]) | ||
assert.Equal(t, "2", rules[1]) | ||
}) | ||
} |