forked from eldada/kubernetes-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canIdo.sh
executable file
·104 lines (83 loc) · 2.7 KB
/
canIdo.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
99
100
101
102
103
104
#!/bin/bash
# Go over all resources in the Kubernetes cluster and check if current user can run an action on them
NAMESPACE=default
ACTIONS="get list watch create update patch delete"
RESOURCES=all
SCRIPT_NAME=$0
######### Functions #########
errorExit () {
echo -e "\nERROR: $1\n"
exit 1
}
usage () {
cat << END_USAGE
${SCRIPT_NAME} - Check if current user can perform the selected action in a given namespace
Usage: ${SCRIPT_NAME} <options>
-n | --namespace <name> : Namespace to check. Default: ${NAMESPACE}
-a | --actions <comma delimited list> : List of actions. Default: (${ACTIONS})
-r | --resources <comma delimited list> : List of resources to test. Default: all
-h | --help : Show this usage.
Examples:
========
Test all permissions for namespace 'foo': $ ${SCRIPT_NAME} --namespace foo
Test 'get' and 'list' permissions for namespace 'bar': $ ${SCRIPT_NAME} --namespace bar --actions get,list
Test 'get' and 'list' permissions on 'pods' and 'secrets': $ ${SCRIPT_NAME} --resources pods,secrets --actions get,list
END_USAGE
exit 1
}
# Process command line options. See usage above for supported options
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-n | --namespace)
NAMESPACE="$2"
shift 2
;;
-a | --actions)
ACTIONS="$(echo $2 | tr ',' ' ')"
shift 2
;;
-r | --resources)
RESOURCES="$(echo $2 | tr ',' ' ')"
shift 2
;;
-h | --help)
usage
;;
*)
usage
;;
esac
done
}
# Test connection to a cluster by kubectl
testConnection () {
echo "Testing connection to cluster"
kubectl cluster-info > /dev/null || errorExit "Connection to cluster failed"
}
canIdo () {
local resource
local action
local resource_list
if [[ ${RESOURCES} =~ all ]]; then
resource_list="$(kubectl api-resources --verbs=list --namespaced -o name)"
else
resource_list=${RESOURCES}
fi
# echo "List of resources:"
# echo "${resource_list}"
for resource in ${resource_list}; do
for action in ${ACTIONS}; do
echo -n "--- Checking ${action} on '${resource}': "
kubectl auth can-i "${action}" "${resource}" --namespace="${NAMESPACE}"
done
done
}
main () {
processOptions "$@"
testConnection
echo "Testing actions [${ACTIONS}] on [${RESOURCES}] resources in namespace ${NAMESPACE}"
canIdo
}
######### Main #########
main "$@"