Skip to content

Commit a3c3411

Browse files
committed
Slides for 20241113
1 parent 78cd106 commit a3c3411

14 files changed

+507
-286
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Aggregated ClusterRoles
2+
3+
Inspect individual ClusterRoles
4+
5+
```sh
6+
kubectl get clusterrole -l rbac.authorization.k8s.io/aggregate-to-view=true
7+
```
8+
9+
Create first ClusterRole
10+
11+
```sh
12+
cat <<EOF | kubectl apply -f -
13+
apiVersion: rbac.authorization.k8s.io/v1
14+
kind: ClusterRole
15+
metadata:
16+
name: monitoring-endpoints
17+
labels:
18+
aggregate-to-monitoring: "true"
19+
rules:
20+
- apiGroups: [""]
21+
resources: ["services", "endpointslices", "pods"]
22+
verbs: ["get", "list", "watch"]
23+
EOF
24+
```
25+
26+
Create second ClusterRole
27+
28+
```sh
29+
cat <<EOF | kubectl apply -f -
30+
apiVersion: rbac.authorization.k8s.io/v1
31+
kind: ClusterRole
32+
metadata:
33+
name: monitoring-deployments
34+
labels:
35+
aggregate-to-monitoring: "true"
36+
rules:
37+
- apiGroups: ["apps"]
38+
resources: ["deployments"]
39+
verbs: ["get", "list", "watch"]
40+
EOF
41+
```
42+
43+
Display new ClusterRoles
44+
45+
```sh
46+
kubectl get clusterrole -l aggregate-to-monitoring=true
47+
```
48+
49+
Create receiving ClusterRole
50+
51+
```sh
52+
cat <<EOF | kubectl apply -f -
53+
apiVersion: rbac.authorization.k8s.io/v1
54+
kind: ClusterRole
55+
metadata:
56+
name: monitoring
57+
aggregationRule:
58+
clusterRoleSelectors:
59+
- matchLabels:
60+
aggregate-to-monitoring: "true"
61+
rules: []
62+
EOF
63+
```
64+
65+
Show aggregated ClusterRole
66+
67+
```sh
68+
kubectl get clusterrole monitoring -o yaml
69+
```

120_kubernetes/rbac/author.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ Find supported resources:
1818
kubectl api-resources
1919
```
2020

21+
--
22+
23+
```plaintext
24+
NAME APIVERSION NAMESPACED KIND
25+
configmaps v1 true ConfigMap
26+
endpoints v1 true Endpoints
27+
namespaces v1 false Namespace
28+
nodes v1 false Node
29+
persistentvolumeclaims v1 true PersistentVolumeClaim
30+
persistentvolumes v1 false PersistentVolume
31+
pods v1 true Pod
32+
secrets v1 true Secret
33+
serviceaccounts v1 true ServiceAccount
34+
services v1 true Service
35+
daemonsets apps/v1 true DaemonSet
36+
deployments apps/v1 true Deployment
37+
replicasets apps/v1 true ReplicaSet
38+
statefulsets apps/v1 true StatefulSet
39+
horizontalpodautoscalers autoscaling/v2 true HorizontalPodAutoscaler
40+
cronjobs batch/v1 true CronJob
41+
jobs batch/v1 true Job
42+
endpointslices discovery.k8s.io/v1 true EndpointSlice
43+
ingresses networking.k8s.io/v1 true Ingress
44+
poddisruptionbudgets policy/v1 true PodDisruptionBudget
45+
clusterrolebindings rbac.authorization.k8s.io/v1 false ClusterRoleBinding
46+
clusterroles rbac.authorization.k8s.io/v1 false ClusterRole
47+
rolebindings rbac.authorization.k8s.io/v1 true RoleBinding
48+
roles rbac.authorization.k8s.io/v1 true Role
49+
```
50+
2151
---
2252

2353
## How to write roles 2/
@@ -42,6 +72,20 @@ Find supported verbs for resources:
4272
kubectl api-resources --output wide
4373
```
4474

75+
--
76+
77+
```plaintext
78+
NAME ... VERBS
79+
bindings ... create
80+
componentstatuses ... get,list
81+
configmaps ... create,delete,deletecollection,get,list,patch,update,watch
82+
endpoints ... create,delete,deletecollection,get,list,patch,update,watch
83+
events ... create,delete,deletecollection,get,list,patch,update,watch
84+
limitranges ... create,delete,deletecollection,get,list,patch,update,watch
85+
namespaces ... create,delete,get,list,patch,update,watch
86+
nodes ... create,delete,deletecollection,get,list,patch,update,watch
87+
```
88+
4589
---
4690

4791
## How to write roles 3/3
@@ -59,14 +103,41 @@ kubectl api-resources --output wide
59103
Some resources have subresources, e.g. `pods/portforward`
60104

61105
```bash
62-
kubectl get --raw / | jq -r '.paths[]' | grep "^/apis/"
106+
kubectl get --raw / | jq -r '.paths[]' | grep -E "^/apis?/" \
63107
| while read -r API; do
64108
echo "=== ${API}"
65109
kubectl get --raw "${API}" \
66110
| jq -r 'select(.resources != null) | .resources[].name'
67111
done
68112
```
69113

114+
--
115+
116+
```plaintext
117+
=== /api/v1
118+
namespaces/finalize
119+
namespaces/status
120+
nodes/proxy
121+
nodes/status
122+
persistentvolumeclaims/status
123+
persistentvolumes/status
124+
pods/attach
125+
pods/binding
126+
pods/ephemeralcontainers
127+
pods/eviction
128+
pods/exec
129+
pods/log
130+
pods/portforward
131+
pods/proxy
132+
pods/status
133+
replicationcontrollers/scale
134+
replicationcontrollers/status
135+
resourcequotas/status
136+
serviceaccounts/token
137+
services/proxy
138+
services/status
139+
```
140+
70141
---
71142

72143
## How to specify subjects
@@ -87,6 +158,8 @@ Authentication backends can add users and groups
87158

88159
Certificate authentication maps to users
89160

161+
OIDC maps to users and groups
162+
90163
---
91164

92165
## How to specify resource names

120_kubernetes/rbac/impersonation.runme.md

+24-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Deploy namespace
44

5-
```shell
5+
```sh
66
cat <<EOF | kubectl apply -f -
77
apiVersion: v1
88
kind: Namespace
@@ -13,7 +13,7 @@ EOF
1313

1414
Deploy namespace admin
1515

16-
```shell
16+
```sh
1717
cat <<EOF | kubectl apply -f -
1818
apiVersion: rbac.authorization.k8s.io/v1
1919
kind: Role
@@ -43,9 +43,9 @@ subjects:
4343
EOF
4444
```
4545

46-
Deploy namespace reader
46+
Deploy service account in namespace
4747

48-
```shell
48+
```sh
4949
cat <<EOF | kubectl apply -f -
5050
apiVersion: v1
5151
kind: ServiceAccount
@@ -61,7 +61,14 @@ metadata:
6161
annotations:
6262
kubernetes.io/service-account.name: reader
6363
type: kubernetes.io/service-account-token
64-
---
64+
EOF
65+
66+
```
67+
68+
Deploy role and rolebinding in namespace
69+
70+
```sh
71+
cat <<EOF | kubectl apply -f -
6572
apiVersion: rbac.authorization.k8s.io/v1
6673
kind: Role
6774
metadata:
@@ -94,7 +101,7 @@ EOF
94101

95102
Deploy impersonation role
96103

97-
```shell
104+
```sh
98105
cat <<EOF | kubectl apply -f -
99106
apiVersion: rbac.authorization.k8s.io/v1
100107
kind: ClusterRole
@@ -125,9 +132,9 @@ subjects:
125132
EOF
126133
```
127134

128-
Create new user in kubeconfig
135+
Create user in kubeconfig
129136

130-
```shell
137+
```sh
131138
TOKEN="$(
132139
kubectl -n test get secrets reader --output json \
133140
| jq --raw-output '.data.token' \
@@ -137,50 +144,50 @@ kubectl config set-credentials test-reader --token=${TOKEN}
137144
kubectl config set-context kind-test --user=test-reader --cluster=kind-kind
138145
```
139146

140-
Switch context
147+
Switch namespace
141148

142-
```shell
149+
```sh
143150
kubectl config use-context kind-test
144151
```
145152

146153
Show permissions in namespace test
147154

148-
```shell
155+
```sh
149156
kubectl auth can-i --list --namespace test
150157
```
151158

152159
Succeed to access to namespace test
153160

154-
```shell
161+
```sh
155162
kubectl -n test get all
156163
```
157164

158165
Fail to access namespace default
159166

160-
```shell
167+
```sh
161168
kubectl -n default get all
162169
```
163170

164171
Fail to run pod in namespace test
165172

166-
```shell
173+
```sh
167174
kubectl -n test run -it --image=alpine --command -- sh
168175
```
169176

170177
Run pod in namespace test using impersonation
171178

172-
```shell
179+
```sh
173180
kubectl -n test run -it --image=alpine --command --as=test-admin -- sh
174181
```
175182

176183
Fail to remove pod
177184

178-
```shell
185+
```sh
179186
kubectl -n test delete pod sh
180187
```
181188

182189
Remove pod using impersonation
183190

184-
```shell
191+
```sh
185192
kubectl -n test delete pod sh --as=test-admin
186193
```

0 commit comments

Comments
 (0)