Skip to content

Instantly share code, notes, and snippets.

@iamleot
Created September 15, 2023 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamleot/2017b3c27ddb44572c32ea90630dfcee to your computer and use it in GitHub Desktop.
Save iamleot/2017b3c27ddb44572c32ea90630dfcee to your computer and use it in GitHub Desktop.
Mechanically try to solve test-outside-test-package Regal violation for conftest policies
#!/usr/bin/awk -f
#
# Mechanically rewrite OPA policy in order to satisfy requirements of Regal
# test-outside-test-package check. Limited to conftest by only checking and
# adjusting `deny', `warning', `violation' rules.
#
# <https://docs.styra.com/regal/rules/testing/test-outside-test-package>
#
/^package/ {
# Sanity check that full_package and package are not defined multiple
# times
if (full_package) {
printf "error: full_package already defined as %s\n",
full_package > "/dev/stderr"
exit 1
}
if (package) {
printf "error: package already defined as %s\n",
package > "/dev/stderr"
exit 1
}
# Extract last part of the package name, if any
full_package = $2
n = split($2, a, "\.")
if (n > 0) {
package = a[n]
} else {
package = full_package
}
# Rewrite the package by adding the `_test' suffix as required and
# import the actual package.
printf "package %s_test\n", full_package
printf "\n"
printf "import data.%s\n", full_package
next
}
/^test_.*{/ {
# ignore any possible `deny' or similar in the middle of the test name
print
next
}
{
# Rewrite all `deny', `violation', `warn' rules by prefixing according
# the corresponding imported package.
gsub("deny", package ".deny")
gsub("violation", package ".violation")
gsub("warn", package ".warn")
print
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment