kubernetes/test/integration/examples/webhook_test.go

134 lines
4.3 KiB
Go
Raw Normal View History

2018-04-17 01:03:11 -04:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apiserver
import (
"sync/atomic"
"testing"
"time"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/core/v1"
2018-04-17 01:03:11 -04:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
auditinternal "k8s.io/apiserver/pkg/apis/audit"
"k8s.io/apiserver/pkg/audit"
2018-04-17 01:03:11 -04:00
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
2020-09-02 13:47:23 -04:00
"k8s.io/kubernetes/pkg/controlplane"
"k8s.io/kubernetes/pkg/controlplane/reconcilers"
2018-10-16 18:17:33 -04:00
"k8s.io/kubernetes/test/integration/framework"
"k8s.io/kubernetes/test/utils/ktesting"
2018-04-17 01:03:11 -04:00
)
func TestWebhookLoopback(t *testing.T) {
webhookPath := "/webhook-test"
called := int32(0)
tCtx := ktesting.Init(t)
client, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
2018-04-17 01:03:11 -04:00
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
},
2020-09-02 13:47:23 -04:00
ModifyServerConfig: func(config *controlplane.Config) {
2022-06-15 00:38:48 -04:00
// Avoid resolvable kubernetes service
config.Extra.EndpointReconcilerType = reconcilers.NoneEndpointReconcilerType
2018-04-17 01:03:11 -04:00
// Hook into audit to watch requests
config.ControlPlane.Generic.AuditBackend = auditSinkFunc(func(events ...*auditinternal.Event) {})
config.ControlPlane.Generic.AuditPolicyRuleEvaluator = auditPolicyRuleEvaluator(func(attrs authorizer.Attributes) audit.RequestAuditConfig {
2018-04-17 01:03:11 -04:00
if attrs.GetPath() == webhookPath {
if attrs.GetUser().GetName() != "system:apiserver" {
t.Errorf("expected user %q, got %q", "system:apiserver", attrs.GetUser().GetName())
}
atomic.AddInt32(&called, 1)
}
return audit.RequestAuditConfig{
Level: auditinternal.LevelNone,
}
2018-04-17 01:03:11 -04:00
})
},
})
2022-05-12 06:10:02 -04:00
defer tearDownFn()
2018-04-17 01:03:11 -04:00
fail := admissionregistrationv1.Fail
noSideEffects := admissionregistrationv1.SideEffectClassNone
_, err := client.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(tCtx, &admissionregistrationv1.MutatingWebhookConfiguration{
2018-04-17 01:03:11 -04:00
ObjectMeta: metav1.ObjectMeta{Name: "webhooktest.example.com"},
Webhooks: []admissionregistrationv1.MutatingWebhook{{
2018-04-17 01:03:11 -04:00
Name: "webhooktest.example.com",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{Namespace: "default", Name: "kubernetes", Path: &webhookPath},
2018-04-17 01:03:11 -04:00
},
Rules: []admissionregistrationv1.RuleWithOperations{{
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.OperationAll},
Rule: admissionregistrationv1.Rule{APIGroups: []string{""}, APIVersions: []string{"v1"}, Resources: []string{"configmaps"}},
2018-04-17 01:03:11 -04:00
}},
FailurePolicy: &fail,
SideEffects: &noSideEffects,
AdmissionReviewVersions: []string{"v1"},
2018-04-17 01:03:11 -04:00
}},
2020-02-08 12:30:21 -05:00
}, metav1.CreateOptions{})
2018-04-17 01:03:11 -04:00
if err != nil {
t.Fatal(err)
}
err = wait.PollImmediate(100*time.Millisecond, 30*time.Second, func() (done bool, err error) {
_, err = client.CoreV1().ConfigMaps("default").Create(tCtx, &v1.ConfigMap{
2018-04-17 01:03:11 -04:00
ObjectMeta: metav1.ObjectMeta{Name: "webhook-test"},
Data: map[string]string{"invalid key": "value"},
2020-02-08 12:30:21 -05:00
}, metav1.CreateOptions{})
2018-04-17 01:03:11 -04:00
if err == nil {
t.Fatal("Unexpected success")
}
if called > 0 {
return true, nil
}
t.Logf("%v", err)
t.Logf("webhook not called yet, continuing...")
return false, nil
})
if err != nil {
t.Fatal(err)
}
}
type auditPolicyRuleEvaluator func(authorizer.Attributes) audit.RequestAuditConfig
2018-04-17 01:03:11 -04:00
func (f auditPolicyRuleEvaluator) EvaluatePolicyRule(attrs authorizer.Attributes) audit.RequestAuditConfig {
2018-04-17 01:03:11 -04:00
return f(attrs)
}
type auditSinkFunc func(events ...*auditinternal.Event)
func (f auditSinkFunc) ProcessEvents(events ...*auditinternal.Event) bool {
2018-04-17 01:03:11 -04:00
f(events...)
return true
2018-04-17 01:03:11 -04:00
}
2018-04-17 01:03:11 -04:00
func (auditSinkFunc) Run(stopCh <-chan struct{}) error {
return nil
}
2018-04-17 01:03:11 -04:00
func (auditSinkFunc) Shutdown() {
}
func (auditSinkFunc) String() string {
return ""
}