-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathpreflight_permission_validation_missing_test.go
260 lines (237 loc) · 8.39 KB
/
preflight_permission_validation_missing_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestPreflightPermissionValidationMissingPermissions(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-missing-permissions"
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"]
verbs: ["get", "list"]
---
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()
basicResource := `
---
apiVersion: v1
kind: Pod
metadata:
name: __test-name__
namespace: __test-name__
spec:
containers:
- name: simple-app
image: docker.io/dkalinin/k8s-simple-app@sha256:4c8b96d4fffdfae29258d94a22ae4ad1fe36139d47288b8960d9958d1e63a9d0
env:
- name: HELLO_MSG
value: stranger
`
basicResource = strings.ReplaceAll(basicResource, "__test-name__", testName)
logger.Section("attempt to deploy app with a Pod and missing permissions to create Pods", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(basicResource), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" /v1, Resource=pods")
NewMissingClusterResource(t, "pod", testName, testName, kubectl)
})
basicResourceSSRR := `
apiVersion: kapp.k14s.io/v1alpha1
kind: Config
preflightRules:
- name: PermissionValidation
config:
permissionValidatorResource: SelfSubjectRulesReview
---
apiVersion: v1
kind: Pod
metadata:
name: __test-name__
namespace: __test-name__
spec:
containers:
- name: simple-app
image: docker.io/dkalinin/k8s-simple-app@sha256:4c8b96d4fffdfae29258d94a22ae4ad1fe36139d47288b8960d9958d1e63a9d0
env:
- name: HELLO_MSG
value: stranger
`
basicResourceSSRR = strings.ReplaceAll(basicResourceSSRR, "__test-name__", testName)
logger.Section("attempt to deploy app with a Pod and missing permissions to create Pods using SSRR for validation", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(basicResourceSSRR), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" /v1, Resource=pods")
NewMissingClusterResource(t, "pod", testName, testName, kubectl)
})
roleResource := `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: __test-name__
name: __test-name__
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "update", "delete"]
`
roleResource = strings.ReplaceAll(roleResource, "__test-name__", testName)
logger.Section("attempt to deploy app with a Role and missing permissions to create Roles", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(roleResource), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" rbac.authorization.k8s.io/v1, Resource=roles")
NewMissingClusterResource(t, "role", testName, testName, kubectl)
})
roleResourceSSRR := `
apiVersion: kapp.k14s.io/v1alpha1
kind: Config
preflightRules:
- name: PermissionValidation
config:
permissionValidatorResource: SelfSubjectRulesReview
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: __test-name__
name: __test-name__
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "update", "delete"]
`
roleResourceSSRR = strings.ReplaceAll(roleResourceSSRR, "__test-name__", testName)
logger.Section("attempt to deploy app with a Role and missing permissions to create Roles using SSRR for validation", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(roleResourceSSRR), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" rbac.authorization.k8s.io/v1, Resource=roles")
NewMissingClusterResource(t, "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("attempt to deploy app with a RoleBinding and missing permissions to create RoleBindings", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(bindingResource), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" rbac.authorization.k8s.io/v1, Resource=rolebindings")
NewMissingClusterResource(t, "rolebinding", testName, testName, kubectl)
})
bindingResourceSSRR := `
apiVersion: kapp.k14s.io/v1alpha1
kind: Config
preflightRules:
- name: PermissionValidation
config:
permissionValidatorResource: SelfSubjectRulesReview
---
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
`
bindingResourceSSRR = strings.ReplaceAll(bindingResourceSSRR, "__test-name__", testName)
logger.Section("attempt to deploy app with a RoleBinding and missing permissions to create RoleBindings using SSRR validation", func() {
_, err := kapp.RunWithOpts([]string{"deploy", "--preflight=PermissionValidation", "-a", appName, "-f", "-", fmt.Sprintf("--kubeconfig-context=%s", scopedContext)},
RunOpts{StdinReader: strings.NewReader(bindingResourceSSRR), AllowError: true})
require.Error(t, err)
require.Contains(t, err.Error(), "running preflight check \"PermissionValidation\": not permitted to \"create\" rbac.authorization.k8s.io/v1, Resource=rolebindings")
NewMissingClusterResource(t, "rolebinding", testName, testName, kubectl)
})
}