Created
May 17, 2023 00:50
-
-
Save ericreeves/1dd6c37fdc18048cc53371f9efbb0541 to your computer and use it in GitHub Desktop.
Bash Script to List Vault Namespaces Recursively
This file contains hidden or 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
#!/bin/bash | |
# First passing argument is the depth | |
# The second passing argument is the starting namespace | |
# Example usage to list namespaces 10 levels deep from the "root" namespace: | |
# ./vault_recursive_namespace_list.sh 10 | |
# Example usage to list namespaces 5 levels deep from the "customer1" namespace. | |
# ./vault_recursive_namespace_list.sh 5 customer1 | |
export all_namespaces="" | |
function getnamespaces() | |
{ | |
# Recursive function for collecting all the namespaces in a variable | |
# First argument is the depth of recursivity | |
# Second argument is the starting namespace | |
# If the first argument is not a number than we bail out | |
case $1 in | |
''|*[!0-9]*) return 1;; | |
*) : ;; | |
esac | |
# If we reach to the last level then we end the recursive loop | |
[[ $1 -lt 1 ]] && return 0 | |
for i in $(vault namespace list -ns=$2 2>>/dev/null |sed -e '1,2d') ; do | |
{ | |
export all_namespaces=$(echo $all_namespaces "$2$i") | |
if [ "X$2" == "X" ] ; then | |
[ $1 -gt 1 ] && getnamespaces $(($1 - 1 )) "$i" | |
else | |
[ $1 -gt 1 ] && getnamespaces $(($1 - 1 )) "$2$i" | |
fi | |
} | |
done | |
return 0 | |
} | |
# Getting all arguments from OS as ARGS | |
# By default we use the level depth 1 | |
case $1 in | |
''|*[!0-9]*) export level=1;; | |
*) export level=$(echo $1) ; level=${level:=1};; | |
esac | |
namespace=$(echo $2) | |
namespace=${namespace:=root} | |
# If the namespace is default we empty the namespace starting point | |
[ "X${namespace}" == "Xroot" ] && export namespace="" | |
# If the namespace is not empty we ensure that the last character is '/' | |
if [ "X${namespace}" == "X" ] ; then | |
: | |
else | |
[ $(echo ${2: -1}) == '/' ] || export namespace="${namespace}/" | |
fi | |
echo "### Searching namespaces starting with $namespace ." | |
getnamespaces $level $namespace | |
echo "$all_namespaces ###" |sed 's/ /\n/g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment