-
Notifications
You must be signed in to change notification settings - Fork 24
/
test_utils.sh
executable file
·98 lines (91 loc) · 3.62 KB
/
test_utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Usage: `source` this script from a testing script in a subdirectory. This
# will make the `validate` and `authorize` functions available. Use these to
# test that polices validate and authorizer as expected. The env var
# `any_failed` is exported as `0` when this script is sourced. A failing test
# case causes the `any_failed` var to be set to `1`. A passing test does
# not change the variable. After running all tests `exit "$any_failed"` to exit
# non-zero for any failing test case.
passed() {
local message=$1
echo " ✅ PASS: $message"
}
any_failed=0
failed() {
local message=$1
echo " ❌ FAIL: $message"
any_failed=1
}
# Call this function to assert that policies in the directory `$1/$2` validate
# with the schema `$1/$3`. Set `any_failed` env var to `1` if the policy does
# not validate.
validate() {
local folder=$1
local policies=$2
local schema=$3
local links=$4
echo " Running validation on ${policies}"
if [ -z "$links" ]
then
res="$(cedar validate --policies "$folder/$policies" --schema "$folder/$schema" --schema-format cedar)"
else
res="$(cedar validate --policies "$folder/$policies" --schema "$folder/$schema" --schema-format cedar -k "$folder/$links" )"
fi
if [[ $? == 0 ]]
then
passed "validate succeeded"
else
failed "validate on ${policies} with result: ${res}"
fi
}
# Call this function to assert that authorization requests defined in
# `$1/ALLOW/*.json` and `$1/DENY/*.json` evaluate with expected authorization
# result given policies in directory `$1/$2` and entities in `$1/$3`.
authorize() {
local folder=$1
local policies=$2
local entities=$3
local schema=$4
local links=$5
echo " Running authorization on ${policies}"
for decision in ALLOW DENY
do
for file in "$folder/$decision"/*.json
do
if [ -z "$schema" -a -z "$links" ]
then
IFS=$'\n' read -r -d '' -a tmp_array < <(cedar authorize --policies "$folder/$policies" --entities "$folder/$entities" --request-json "$file" -v && printf '\0')
elif [ -z "$links" ]
then
IFS=$'\n' read -r -d '' -a tmp_array < <(cedar authorize --policies "$folder/$policies" --schema "$folder/$schema" --schema-format cedar --entities "$folder/$entities" --request-json "$file" -v && printf '\0')
else
IFS=$'\n' read -r -d '' -a tmp_array < <(cedar authorize --policies "$folder/$policies" -k "$folder/$links" --schema "$folder/$schema" --schema-format cedar --entities "$folder/$entities" --request-json "$file" -v && printf '\0')
fi
res="${tmp_array[0]}"
unset tmp_array[0]
unset tmp_array[1]
policyIds="$(IFS=\;; echo "${tmp_array[*]}")"
jsonfile="$(echo "$file" | cut -d '/' -f 3)"
if [ "$res" != "$decision" ]
then
failed "decision \"${res}\" for ${jsonfile}, expected \"${decision}\""
else
passed "decision \"${decision}\" for ${jsonfile} determined by policy id(s):${policyIds}"
fi
done
done
}
# Call this function to assert that policies in the directory `$1/$2` are formatted.
# Set `any_failed` env var to `1` if a policy is not formatted.
format() {
local folder=$1
local policies=$2
echo " Checking formatting of ${policies}"
res="$(cedar format --policies "$folder/$policies" --check)"
if [[ $? == 0 ]]
then
passed "format check succeeded"
else
failed "format check on ${policies} with result: ${res}"
fi
}
echo "Using $(cedar --version)"