Skip to content

Instantly share code, notes, and snippets.

@garethahealy
Created July 9, 2020 10:01
Show Gist options
  • Save garethahealy/d5ce3ff9457e3c3f1487269195bc49e5 to your computer and use it in GitHub Desktop.
Save garethahealy/d5ce3ff9457e3c3f1487269195bc49e5 to your computer and use it in GitHub Desktop.
resolved packages in rego policy directory, filters based on a regex input
# get_rego_namespaces
# ====================
#
# Summary: Resolves the package names in your rego policies against a regex lookup
#
# Usage: get_rego_namespaces ${regex}
#
# Options:
# <regex> Regex pattern matching package name
# Globals:
# none
# Returns:
# string - list of --namespaces {which-matched-regex}
get_rego_namespaces() {
local regex="${1:-.*}"
declare -a namespaces
# shellcheck disable=SC2038
for file in $(find policy/* -name "*.rego" -type f | xargs); do
read -r line < "${file}"
local current="$(echo ${line/package/} | xargs)"
if [[ "${current}" =~ ${regex} ]]; then
if [[ ! "${namespaces[*]}" =~ ${current} ]]; then
namespaces+=("--namespace ${current}")
fi
fi
done
echo "${namespaces[*]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment