2018-01-09 18:57:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/api/admission/v1beta1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2018-07-18 14:47:22 +00:00
|
|
|
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
2018-01-09 18:57:14 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
// TODO: try this library to see if it generates correct json patch
|
|
|
|
// https://github.com/mattbaird/jsonpatch
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
patch1 string = `[
|
|
|
|
{ "op": "add", "path": "/data/mutation-stage-1", "value": "yes" }
|
|
|
|
]`
|
|
|
|
patch2 string = `[
|
|
|
|
{ "op": "add", "path": "/data/mutation-stage-2", "value": "yes" }
|
|
|
|
]`
|
2018-03-06 22:33:18 +00:00
|
|
|
addInitContainerPatch string = `[
|
|
|
|
{"op":"add","path":"/spec/initContainers","value":[{"image":"webhook-added-image","name":"webhook-added-init-container","resources":{}}]}
|
|
|
|
]`
|
2018-01-09 18:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains the server (the webhook) cert and key.
|
|
|
|
type Config struct {
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) addFlags() {
|
|
|
|
flag.StringVar(&c.CertFile, "tls-cert-file", c.CertFile, ""+
|
|
|
|
"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
|
|
|
|
"after server cert).")
|
|
|
|
flag.StringVar(&c.KeyFile, "tls-private-key-file", c.KeyFile, ""+
|
|
|
|
"File containing the default x509 private key matching --tls-cert-file.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toAdmissionResponse(err error) *v1beta1.AdmissionResponse {
|
|
|
|
return &v1beta1.AdmissionResponse{
|
|
|
|
Result: &metav1.Status{
|
|
|
|
Message: err.Error(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
// Deny all requests made to this function.
|
|
|
|
func alwaysDeny(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("calling always-deny")
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
reviewResponse.Result = &metav1.Status{Message: "this webhook denies all requests"}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
// only allow pods to pull images from specific registry.
|
|
|
|
func admitPods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("admitting pods")
|
|
|
|
podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
|
|
|
|
if ar.Request.Resource != podResource {
|
|
|
|
err := fmt.Errorf("expect resource to be %s", podResource)
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
pod := corev1.Pod{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
|
|
|
|
var msg string
|
2018-03-06 22:33:18 +00:00
|
|
|
if v, ok := pod.Labels["webhook-e2e-test"]; ok {
|
|
|
|
if v == "webhook-disallow" {
|
2018-01-09 18:57:14 +00:00
|
|
|
reviewResponse.Allowed = false
|
|
|
|
msg = msg + "the pod contains unwanted label; "
|
|
|
|
}
|
2018-03-06 22:33:18 +00:00
|
|
|
if v == "wait-forever" {
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
msg = msg + "the pod response should not be sent; "
|
|
|
|
<-make(chan int) // Sleep forever - no one sends to this channel
|
|
|
|
}
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
for _, container := range pod.Spec.Containers {
|
|
|
|
if strings.Contains(container.Name, "webhook-disallow") {
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
msg = msg + "the pod contains unwanted container name; "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !reviewResponse.Allowed {
|
|
|
|
reviewResponse.Result = &metav1.Status{Message: strings.TrimSpace(msg)}
|
|
|
|
}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-03-06 22:33:18 +00:00
|
|
|
func mutatePods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("mutating pods")
|
|
|
|
podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
|
|
|
|
if ar.Request.Resource != podResource {
|
|
|
|
glog.Errorf("expect resource to be %s", podResource)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
pod := corev1.Pod{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
if pod.Name == "webhook-to-be-mutated" {
|
|
|
|
reviewResponse.Patch = []byte(addInitContainerPatch)
|
|
|
|
pt := v1beta1.PatchTypeJSONPatch
|
|
|
|
reviewResponse.PatchType = &pt
|
|
|
|
}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
// deny configmaps with specific key-value pair.
|
|
|
|
func admitConfigMaps(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("admitting configmaps")
|
|
|
|
configMapResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
|
|
|
|
if ar.Request.Resource != configMapResource {
|
|
|
|
glog.Errorf("expect resource to be %s", configMapResource)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
configmap := corev1.ConfigMap{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(raw, nil, &configmap); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
for k, v := range configmap.Data {
|
|
|
|
if k == "webhook-e2e-test" && v == "webhook-disallow" {
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
reviewResponse.Result = &metav1.Status{
|
|
|
|
Reason: "the configmap contains unwanted key and value",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func mutateConfigmaps(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("mutating configmaps")
|
|
|
|
configMapResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
|
|
|
|
if ar.Request.Resource != configMapResource {
|
|
|
|
glog.Errorf("expect resource to be %s", configMapResource)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
configmap := corev1.ConfigMap{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(raw, nil, &configmap); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
if configmap.Data["mutation-start"] == "yes" {
|
|
|
|
reviewResponse.Patch = []byte(patch1)
|
|
|
|
}
|
|
|
|
if configmap.Data["mutation-stage-1"] == "yes" {
|
|
|
|
reviewResponse.Patch = []byte(patch2)
|
|
|
|
}
|
|
|
|
|
|
|
|
pt := v1beta1.PatchTypeJSONPatch
|
|
|
|
reviewResponse.PatchType = &pt
|
|
|
|
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func mutateCustomResource(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("mutating custom resource")
|
2018-01-09 18:57:14 +00:00
|
|
|
cr := struct {
|
|
|
|
metav1.ObjectMeta
|
|
|
|
Data map[string]string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
err := json.Unmarshal(raw, &cr)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
|
|
|
|
if cr.Data["mutation-start"] == "yes" {
|
|
|
|
reviewResponse.Patch = []byte(patch1)
|
|
|
|
}
|
|
|
|
if cr.Data["mutation-stage-1"] == "yes" {
|
|
|
|
reviewResponse.Patch = []byte(patch2)
|
|
|
|
}
|
|
|
|
pt := v1beta1.PatchTypeJSONPatch
|
|
|
|
reviewResponse.PatchType = &pt
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func admitCustomResource(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("admitting custom resource")
|
2018-01-09 18:57:14 +00:00
|
|
|
cr := struct {
|
|
|
|
metav1.ObjectMeta
|
|
|
|
Data map[string]string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
err := json.Unmarshal(raw, &cr)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
for k, v := range cr.Data {
|
|
|
|
if k == "webhook-e2e-test" && v == "webhook-disallow" {
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
reviewResponse.Result = &metav1.Status{
|
|
|
|
Reason: "the custom resource contains unwanted data",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
// Deny all crds with the label "webhook-e2e-test":"webhook-disallow"
|
|
|
|
// This function expects all CRDs submitted to it to be apiextensions.k8s.io/v1beta1
|
|
|
|
// TODO: When apiextensions.k8s.io/v1 is added we will need to update this function.
|
|
|
|
func admitCRD(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
|
|
glog.V(2).Info("admitting crd")
|
|
|
|
crdResource := metav1.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1beta1", Resource: "customresourcedefinitions"}
|
|
|
|
if ar.Request.Resource != crdResource {
|
|
|
|
err := fmt.Errorf("expect resource to be %s", crdResource)
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := ar.Request.Object.Raw
|
|
|
|
crd := apiextensionsv1beta1.CustomResourceDefinition{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(raw, nil, &crd); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return toAdmissionResponse(err)
|
|
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
|
|
reviewResponse.Allowed = true
|
|
|
|
|
|
|
|
if v, ok := crd.Labels["webhook-e2e-test"]; ok {
|
|
|
|
if v == "webhook-disallow" {
|
|
|
|
reviewResponse.Allowed = false
|
|
|
|
reviewResponse.Result = &metav1.Status{Message: "the crd contains unwanted label"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &reviewResponse
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
type admitFunc func(v1beta1.AdmissionReview) *v1beta1.AdmissionResponse
|
|
|
|
|
|
|
|
func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
|
|
|
|
var body []byte
|
|
|
|
if r.Body != nil {
|
|
|
|
if data, err := ioutil.ReadAll(r.Body); err == nil {
|
|
|
|
body = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the content type is accurate
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/json" {
|
|
|
|
glog.Errorf("contentType=%s, expect application/json", contentType)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
glog.V(2).Info(fmt.Sprintf("handling request: %v", body))
|
2018-01-09 18:57:14 +00:00
|
|
|
var reviewResponse *v1beta1.AdmissionResponse
|
|
|
|
ar := v1beta1.AdmissionReview{}
|
|
|
|
deserializer := codecs.UniversalDeserializer()
|
|
|
|
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
reviewResponse = toAdmissionResponse(err)
|
|
|
|
} else {
|
|
|
|
reviewResponse = admit(ar)
|
|
|
|
}
|
2018-07-18 14:47:22 +00:00
|
|
|
glog.V(2).Info(fmt.Sprintf("sending response: %v", reviewResponse))
|
2018-01-09 18:57:14 +00:00
|
|
|
|
|
|
|
response := v1beta1.AdmissionReview{}
|
|
|
|
if reviewResponse != nil {
|
|
|
|
response.Response = reviewResponse
|
|
|
|
response.Response.UID = ar.Request.UID
|
|
|
|
}
|
|
|
|
// reset the Object and OldObject, they are not needed in a response.
|
|
|
|
ar.Request.Object = runtime.RawExtension{}
|
|
|
|
ar.Request.OldObject = runtime.RawExtension{}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
if _, err := w.Write(resp); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func serveAlwaysDeny(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, alwaysDeny)
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
func servePods(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, admitPods)
|
|
|
|
}
|
|
|
|
|
2018-03-06 22:33:18 +00:00
|
|
|
func serveMutatePods(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, mutatePods)
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
func serveConfigmaps(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, admitConfigMaps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveMutateConfigmaps(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, mutateConfigmaps)
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func serveCustomResource(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, admitCustomResource)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func serveMutateCustomResource(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, mutateCustomResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveCRD(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serve(w, r, admitCRD)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var config Config
|
|
|
|
config.addFlags()
|
|
|
|
flag.Parse()
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
http.HandleFunc("/always-deny", serveAlwaysDeny)
|
2018-01-09 18:57:14 +00:00
|
|
|
http.HandleFunc("/pods", servePods)
|
2018-03-06 22:33:18 +00:00
|
|
|
http.HandleFunc("/mutating-pods", serveMutatePods)
|
2018-01-09 18:57:14 +00:00
|
|
|
http.HandleFunc("/configmaps", serveConfigmaps)
|
|
|
|
http.HandleFunc("/mutating-configmaps", serveMutateConfigmaps)
|
2018-07-18 14:47:22 +00:00
|
|
|
http.HandleFunc("/custom-resource", serveCustomResource)
|
|
|
|
http.HandleFunc("/mutating-custom-resource", serveMutateCustomResource)
|
2018-01-09 18:57:14 +00:00
|
|
|
http.HandleFunc("/crd", serveCRD)
|
|
|
|
clientset := getClient()
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: ":443",
|
|
|
|
TLSConfig: configTLS(config, clientset),
|
|
|
|
}
|
|
|
|
server.ListenAndServeTLS("", "")
|
|
|
|
}
|