-
Notifications
You must be signed in to change notification settings - Fork 82
/
webhook.go
152 lines (138 loc) · 5.84 KB
/
webhook.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
//go:build e2e
package e2e
import (
"context"
"fmt"
"strings"
"github.com/onsi/ginkgo/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/utils/pointer"
"github.com/Azure/azure-workload-identity/pkg/webhook"
)
var _ = ginkgo.Describe("Webhook", func() {
f := framework.NewDefaultFramework("webhook")
ginkgo.It("should mutate a labeled pod", func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", nil, nil)
pod, err := createPodWithServiceAccount(
f.ClientSet,
f.Namespace.Name,
serviceAccount,
"registry.k8s.io/e2e-test-images/busybox:1.29-4",
[]string{"sleep"},
[]string{"3600"},
nil,
nil,
map[string]string{webhook.UseWorkloadIdentityLabel: "true"},
false,
)
framework.ExpectNoError(err, "failed to create pod %s in %s", pod.Name, f.Namespace.Name)
validateMutatedPod(f, pod, nil)
})
ginkgo.It("should mutate the init containers within a pod", func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", map[string]string{webhook.UseWorkloadIdentityLabel: "true"}, nil)
pod := generatePodWithServiceAccount(
f.ClientSet,
f.Namespace.Name,
serviceAccount,
"registry.k8s.io/e2e-test-images/busybox:1.29-4",
[]string{"sleep"},
[]string{"3600"},
nil,
nil,
map[string]string{webhook.UseWorkloadIdentityLabel: "true"},
false,
)
pod.Spec.InitContainers = []corev1.Container{{
Name: "init-container",
Image: "registry.k8s.io/e2e-test-images/busybox:1.29-4",
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"sleep"},
Args: []string{"5"},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: pointer.Bool(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
RunAsNonRoot: pointer.Bool(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
RunAsUser: pointer.Int64(1000),
},
}}
pod, err := createPod(f.ClientSet, pod)
framework.ExpectNoError(err, "failed to create pod %s in %s", pod.Name, f.Namespace.Name)
defer f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{})
validateMutatedPod(f, pod, nil)
})
ginkgo.It("should mutate a deployment pod with a labeled pod spec", func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", map[string]string{webhook.UseWorkloadIdentityLabel: "true"}, nil)
pod := createPodUsingDeploymentWithServiceAccount(f, serviceAccount)
validateMutatedPod(f, pod, nil)
})
ginkgo.It("should mutate a deployment pod with an annotated service account", func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", nil, map[string]string{webhook.UseWorkloadIdentityLabel: "true"})
pod := createPodUsingDeploymentWithServiceAccount(f, serviceAccount)
validateMutatedPod(f, pod, nil)
})
ginkgo.It(fmt.Sprintf("should not mutate selected containers if the pod has %s annotated", webhook.SkipContainersAnnotation), func() {
const skipContainers = busybox1 + ";"
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", map[string]string{webhook.UseWorkloadIdentityLabel: "true"}, nil)
pod, err := createPodWithServiceAccount(
f.ClientSet,
f.Namespace.Name,
serviceAccount,
"registry.k8s.io/e2e-test-images/busybox:1.29-4",
[]string{"sleep"},
[]string{"3600"},
nil,
map[string]string{webhook.SkipContainersAnnotation: skipContainers},
map[string]string{webhook.UseWorkloadIdentityLabel: "true"},
false,
)
framework.ExpectNoError(err, "failed to create pod %s in %s", pod.Name, f.Namespace.Name)
validateMutatedPod(f, pod, strings.Split(skipContainers, ";"))
validateUnmutatedContainers(f, pod, strings.Split(skipContainers, ";"))
})
for _, annotations := range []map[string]string{
{webhook.ServiceAccountTokenExpiryAnnotation: "100"}, // less than 3600 (the minimum expiry)
{webhook.ServiceAccountTokenExpiryAnnotation: "invalid"}, // non-numeric value
} {
ginkgo.It(fmt.Sprintf("should not mutate a pod if '%s: \"%s\"' is annotated to the service account", webhook.ServiceAccountTokenExpiryAnnotation, annotations[webhook.ServiceAccountTokenExpiryAnnotation]), func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", map[string]string{webhook.UseWorkloadIdentityLabel: "true"}, annotations)
_, err := createPodWithServiceAccount(
f.ClientSet,
f.Namespace.Name,
serviceAccount,
"registry.k8s.io/e2e-test-images/busybox:1.29-4",
[]string{"sleep"},
[]string{"3600"},
nil,
nil,
map[string]string{webhook.UseWorkloadIdentityLabel: "true"},
false,
)
framework.Logf("ensuring that the creation of pod is denied by the webhook")
framework.ExpectError(err, "creation of pod should be denied by the webhook")
})
ginkgo.It(fmt.Sprintf("should not mutate a pod if '%s: \"%s\"' is annotated to the pod", webhook.ServiceAccountTokenExpiryAnnotation, annotations[webhook.ServiceAccountTokenExpiryAnnotation]), func() {
serviceAccount := createServiceAccount(f.ClientSet, f.Namespace.Name, f.Namespace.Name+"-sa", map[string]string{webhook.UseWorkloadIdentityLabel: "true"}, nil)
_, err := createPodWithServiceAccount(
f.ClientSet,
f.Namespace.Name,
serviceAccount,
"registry.k8s.io/e2e-test-images/busybox:1.29-4",
[]string{"sleep"},
[]string{"3600"},
nil,
annotations,
map[string]string{webhook.UseWorkloadIdentityLabel: "true"},
false,
)
framework.Logf("ensuring that the creation of pod is denied by the webhook")
framework.ExpectError(err, "creation of pod should be denied by the webhook")
})
}
})