Skip to content

Commit cf32cbc

Browse files
committed
fix: switch to cluster-admin instead of edit cluster role to include CRDs. Additional tests
1 parent f8ee711 commit cf32cbc

5 files changed

Lines changed: 77 additions & 3 deletions

File tree

Models/K8sRoleBinding.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public static K8sRoleBinding NamespaceFullAccess(string namespacename,
5252
Kind = "RoleBinding",
5353
ApiVersion = "rbac.authorization.k8s.io/v1",
5454
Metadata = new K8sMetadata { Name = $"copsnamespace-user", Namespace = namespacename },
55-
// the in-built clusterrole edit has all the api resources and CRDs aalways up to date,
56-
// so we use that clusterrole instead of writing our own which is far more brittle
57-
RoleRef = new K8sRoleRef("ClusterRole", "edit", "rbac.authorization.k8s.io")
55+
// The in-built clusterrole cluster-admin allows access to all resources (wildcard),
56+
// so that we can use that clusterrole instead of writing our own which is far more brittle.
57+
// Since we scope the cluster-admin to a namespace using RoleBinding (in contrast to ClusterRoleBinding)
58+
// this is a good approach.
59+
RoleRef = new K8sRoleRef("ClusterRole", "cluster-admin", "rbac.authorization.k8s.io")
5860
};
5961

6062
var subjects = users.ToList()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: networking.k8s.io/v1beta1
2+
kind: Ingress
3+
metadata:
4+
name: test-ingress
5+
namespace: {{NAMESPACE}}
6+
annotations:
7+
nginx.ingress.kubernetes.io/rewrite-target: /
8+
spec:
9+
rules:
10+
- http:
11+
paths:
12+
- path: /testpath
13+
backend:
14+
serviceName: test
15+
servicePort: 80
16+
---
17+
apiVersion: networking.k8s.io/v1
18+
kind: NetworkPolicy
19+
metadata:
20+
name: allow-all-ingress
21+
namespace: {{NAMESPACE}}
22+
spec:
23+
podSelector: {}
24+
ingress:
25+
- {}
26+
policyTypes:
27+
- Ingress

tests/allowed-resources/notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Actually all the resources should be allowed to be deployed inside a namespace, so this is more of a collection of those that we actually test.

tests/allowed-resources/rbac.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: Role
3+
metadata:
4+
name: test-role
5+
namespace: {{NAMESPACE}}
6+
labels:
7+
tests: cops-controller-component-tests
8+
rules: # rules here are completely irrelevant
9+
- apiGroups:
10+
- ""
11+
resources:
12+
- configmaps
13+
verbs:
14+
- get
15+
---
16+
apiVersion: rbac.authorization.k8s.io/v1
17+
kind: RoleBinding
18+
metadata:
19+
name: test-role-binding
20+
namespace: {{NAMESPACE}}
21+
labels:
22+
tests: cops-controller-component-tests
23+
roleRef:
24+
apiGroup: rbac.authorization.k8s.io
25+
kind: Role
26+
name: test-role
27+
subjects:
28+
- apiGroup: rbac.authorization.k8s.io
29+
kind: User
30+
name: test.user@conplement.de

tests/tests.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ function ensureAccessToNamespace {
108108
kubectl get pods,svc,deploy -n $namespaceName || fail "It was expected that the namespace setup is completed at this point."
109109
}
110110

111+
function ensureAllResourcesAreSupported {
112+
namespaceName=$1
113+
114+
# IMPORTANT
115+
# you should not test custom CRD resources here, because that is an additional test dependency which might
116+
# come in conflict with existing CRDs, depending on the cluster where this test is run
117+
cat "tests/allowed-resources/rbac.yaml" | sed "s/{{NAMESPACE}}/$namespaceName/g" \
118+
| kubectl apply -f -
119+
cat "tests/allowed-resources/networking.yaml" | sed "s/{{NAMESPACE}}/$namespaceName/g" \
120+
| kubectl apply -f -
121+
}
122+
111123
function expectApplyToFail {
112124
hasFailed="no"
113125

@@ -167,6 +179,7 @@ function test_invalidDefinitions {
167179

168180
# Tests following business cases:
169181
# - user can create a cops namespace and gain rights inside it
182+
# - the rights are additionaly tested by deploying different sample k8s resources, which should all succeed
170183
# - all other users are denied access
171184
function test_shouldDeployEmpireCnsWithValidRbac {
172185
logTestStarted ${FUNCNAME[0]}
@@ -183,6 +196,7 @@ function test_shouldDeployEmpireCnsWithValidRbac {
183196

184197
# Assert
185198
ensureAccessToNamespace $namespaceName
199+
ensureAllResourcesAreSupported $namespaceName
186200

187201
# no access for other accounts
188202
kubectl config use-context $kyloRenAccount

0 commit comments

Comments
 (0)