-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathpreflight_permission_validation_escalation_test.go
137 lines (124 loc) · 3.53 KB
/
preflight_permission_validation_escalation_test.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"fmt"
"strings"
"testing"
)
func TestPreflightPermissionValidationEscalation(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}
kubectl := Kubectl{t, env.Namespace, logger}
testName := "preflight-permission-validation-escalation"
base := `
---
apiVersion: v1
kind: Namespace
metadata:
name: __test-name__
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: scoped-sa
namespace: __ns__
---
apiVersion: v1
kind: Secret
metadata:
name: scoped-sa
namespace: __ns__
annotations:
kubernetes.io/service-account.name: scoped-sa
type: kubernetes.io/service-account-token
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: __test-name__
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["*"]
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["list"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles", "rolebindings", "clusterroles", "clusterrolebindings"]
verbs: ["get", "list", "create", "update", "delete", "escalate", "bind"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: __test-name__
subjects:
- kind: ServiceAccount
name: scoped-sa
namespace: __ns__
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: __test-name__
`
base = strings.ReplaceAll(base, "__test-name__", testName)
base = strings.ReplaceAll(base, "__ns__", env.Namespace)
baseName := "preflight-permission-validation-base-app"
appName := "preflight-permission-validation-app"
scopedContext := "scoped-context"
scopedUser := "scoped-user"
cleanUp := func() {
kapp.Run([]string{"delete", "-a", baseName})
kapp.Run([]string{"delete", "-a", appName})
RemoveClusterResource(t, "ns", testName, "", kubectl)
}
cleanUp()
defer cleanUp()
kapp.RunWithOpts([]string{"deploy", "-a", baseName, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(base)})
cleanUpContext := ScopedContext(t, kubectl, testName, scopedContext, scopedUser)
defer cleanUpContext()
roleResource := `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: __test-name__
name: __test-name__
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["*"]
`
roleResource = strings.ReplaceAll(roleResource, "__test-name__", testName)
logger.Section("deploy app with privilege escalation Role", func() {
kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(roleResource)})
NewPresentClusterResource("role", testName, testName, kubectl)
})
bindingResource := `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: __test-name__
name: __test-name__
subjects:
- kind: ServiceAccount
namespace: __test-name__
name: default
roleRef:
kind: ClusterRole
name: admin
apiGroup: rbac.authorization.k8s.io
`
bindingResource = strings.ReplaceAll(bindingResource, "__test-name__", testName)
logger.Section("deploy app with privilege escalation RoleBinding", func() {
kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(bindingResource)})
NewPresentClusterResource("rolebinding", testName, testName, kubectl)
})
}