-
Notifications
You must be signed in to change notification settings - Fork 153
/
test.sh
executable file
·57 lines (44 loc) · 1.52 KB
/
test.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
#!/usr/bin/env bash
k() {
kubectl --context kind-opa-authorizer "$@"
}
expect() {
if [[ "$1" != "$2" ]]; then
echo "Expected $1 == $2"
exit 1
fi
}
expect_ends_with() {
if [[ "$1" != *"$2" ]]; then
echo "Expected $1 == *$2"
exit 1
fi
}
echo "Waiting for OPA pod to come up"
exit_code=1
retries=0
opa_pod=""
while [[ "$exit_code" != 0 && "$retries" -lt 20 ]]
do
sleep 5
opa_pod=$(k --namespace opa get pods -l app=opa -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
exit_code="$?"
((retries++))
done
echo "OPA pod is up - awaiting condition=Ready"
# Wait for the OPA pod to become ready
k --namespace opa wait --for=condition=Ready --timeout=100s pods/"$opa_pod" > /dev/null
echo "OPA pod ready. Running tests."
echo "============================="
# Access to kube-system should be denied
result=$(k --namespace kube-system --as=someuser --as-group=system:authenticated get pods 2>&1)
expect "$?" 1
expect_ends_with "$result" "OPA: denied access to namespace kube-system"
# Access to opa namespace denied unless in devops group
result=$(k --namespace opa --as=someuser --as-group=system:authenticated get pods 2>&1)
expect "$?" 1
expect_ends_with "$result" "OPA: provided groups (system:authenticated) does not include all required groups: (devops, system:authenticated)"
# Access to opa namespace allowed if in devops group
result=$(k --namespace opa --as=someuser --as-group=system:authenticated --as-group=devops get pods 2>&1)
expect "$?" 0
echo "All tests successful!"