2019-05-13 18:33:01 +00:00
|
|
|
package webhooks
|
2019-02-07 17:22:04 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-04 18:40:02 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-05-30 19:28:56 +00:00
|
|
|
"github.com/golang/glog"
|
2019-08-17 16:58:14 +00:00
|
|
|
kyvernoclient "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
|
|
|
|
kyvernoinformer "github.com/nirmata/kyverno/pkg/client/informers/externalversions/kyverno/v1alpha1"
|
2019-08-20 00:17:52 +00:00
|
|
|
kyvernolister "github.com/nirmata/kyverno/pkg/client/listers/kyverno/v1alpha1"
|
2019-05-22 17:29:10 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/config"
|
2019-05-29 21:12:09 +00:00
|
|
|
client "github.com/nirmata/kyverno/pkg/dclient"
|
2019-06-27 01:04:50 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/event"
|
2019-08-20 19:51:25 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/policy"
|
2019-05-22 17:29:10 +00:00
|
|
|
tlsutils "github.com/nirmata/kyverno/pkg/tls"
|
2019-08-21 08:07:32 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/webhookconfig"
|
2019-03-04 18:40:02 +00:00
|
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
2019-08-24 01:34:23 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-11-07 20:13:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2019-08-12 17:02:07 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2019-02-07 17:22:04 +00:00
|
|
|
)
|
|
|
|
|
2019-03-04 18:40:02 +00:00
|
|
|
// WebhookServer contains configured TLS server with MutationWebhook.
|
|
|
|
// MutationWebhook gets policies from policyController and takes control of the cluster with kubeclient.
|
2019-02-07 17:22:04 +00:00
|
|
|
type WebhookServer struct {
|
2019-08-08 01:01:28 +00:00
|
|
|
server http.Server
|
|
|
|
client *client.Client
|
2019-08-15 02:00:37 +00:00
|
|
|
kyvernoClient *kyvernoclient.Clientset
|
2019-09-03 21:51:51 +00:00
|
|
|
pLister kyvernolister.ClusterPolicyLister
|
|
|
|
pvLister kyvernolister.ClusterPolicyViolationLister
|
2019-08-15 02:00:37 +00:00
|
|
|
pListerSynced cache.InformerSynced
|
|
|
|
pvListerSynced cache.InformerSynced
|
|
|
|
eventGen event.Interface
|
2019-08-21 08:07:32 +00:00
|
|
|
webhookRegistrationClient *webhookconfig.WebhookRegistrationClient
|
2019-08-20 19:51:25 +00:00
|
|
|
// API to send policy stats for aggregation
|
2019-10-19 00:38:46 +00:00
|
|
|
policyStatus policy.PolicyStatusInterface
|
|
|
|
// helpers to validate against current loaded configuration
|
|
|
|
configHandler config.Interface
|
|
|
|
// channel for cleanup notification
|
|
|
|
cleanUp chan<- struct{}
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebhookServer creates new instance of WebhookServer accordingly to given configuration
|
|
|
|
// Policy Controller and Kubernetes Client should be initialized in configuration
|
2019-05-13 18:27:47 +00:00
|
|
|
func NewWebhookServer(
|
2019-08-10 02:12:50 +00:00
|
|
|
kyvernoClient *kyvernoclient.Clientset,
|
2019-05-20 17:56:12 +00:00
|
|
|
client *client.Client,
|
2019-05-14 15:10:25 +00:00
|
|
|
tlsPair *tlsutils.TlsPemPair,
|
2019-09-03 21:51:51 +00:00
|
|
|
pInformer kyvernoinformer.ClusterPolicyInformer,
|
|
|
|
pvInformer kyvernoinformer.ClusterPolicyViolationInformer,
|
2019-08-09 20:41:56 +00:00
|
|
|
eventGen event.Interface,
|
2019-08-21 08:07:32 +00:00
|
|
|
webhookRegistrationClient *webhookconfig.WebhookRegistrationClient,
|
2019-08-20 19:51:25 +00:00
|
|
|
policyStatus policy.PolicyStatusInterface,
|
2019-10-19 00:38:46 +00:00
|
|
|
configHandler config.Interface,
|
2019-08-27 23:44:10 +00:00
|
|
|
cleanUp chan<- struct{}) (*WebhookServer, error) {
|
2019-03-04 18:40:02 +00:00
|
|
|
|
2019-05-13 18:27:47 +00:00
|
|
|
if tlsPair == nil {
|
2019-03-22 20:11:55 +00:00
|
|
|
return nil, errors.New("NewWebhookServer is not initialized properly")
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig tls.Config
|
2019-03-22 20:11:55 +00:00
|
|
|
pair, err := tls.X509KeyPair(tlsPair.Certificate, tlsPair.PrivateKey)
|
2019-03-04 18:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{pair}
|
|
|
|
|
|
|
|
ws := &WebhookServer{
|
2019-08-15 02:00:37 +00:00
|
|
|
|
2019-08-08 01:01:28 +00:00
|
|
|
client: client,
|
2019-08-15 02:00:37 +00:00
|
|
|
kyvernoClient: kyvernoClient,
|
|
|
|
pLister: pInformer.Lister(),
|
2019-08-26 20:34:42 +00:00
|
|
|
pvLister: pvInformer.Lister(),
|
|
|
|
pListerSynced: pvInformer.Informer().HasSynced,
|
2019-08-15 02:00:37 +00:00
|
|
|
pvListerSynced: pInformer.Informer().HasSynced,
|
|
|
|
eventGen: eventGen,
|
2019-08-08 01:01:28 +00:00
|
|
|
webhookRegistrationClient: webhookRegistrationClient,
|
2019-08-20 19:51:25 +00:00
|
|
|
policyStatus: policyStatus,
|
2019-10-19 00:38:46 +00:00
|
|
|
configHandler: configHandler,
|
2019-08-27 23:44:10 +00:00
|
|
|
cleanUp: cleanUp,
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
2019-05-14 15:10:25 +00:00
|
|
|
mux.HandleFunc(config.MutatingWebhookServicePath, ws.serve)
|
|
|
|
mux.HandleFunc(config.ValidatingWebhookServicePath, ws.serve)
|
2019-07-03 01:42:07 +00:00
|
|
|
mux.HandleFunc(config.PolicyValidatingWebhookServicePath, ws.serve)
|
2019-08-27 23:44:10 +00:00
|
|
|
mux.HandleFunc(config.PolicyMutatingWebhookServicePath, ws.serve)
|
2019-03-04 18:40:02 +00:00
|
|
|
|
|
|
|
ws.server = http.Server{
|
|
|
|
Addr: ":443", // Listen on port for HTTPS requests
|
|
|
|
TLSConfig: &tlsConfig,
|
|
|
|
Handler: mux,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, nil
|
2019-02-21 18:31:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:57:43 +00:00
|
|
|
// Main server endpoint for all requests
|
2019-02-07 17:22:04 +00:00
|
|
|
func (ws *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
2019-05-14 15:10:25 +00:00
|
|
|
admissionReview := ws.bodyToAdmissionReview(r, w)
|
|
|
|
if admissionReview == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-04 18:40:02 +00:00
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
admissionReview.Response = &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: true,
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
2019-07-03 00:24:38 +00:00
|
|
|
|
2019-06-19 21:05:23 +00:00
|
|
|
// Do not process the admission requests for kinds that are in filterKinds for filtering
|
2019-10-19 00:38:46 +00:00
|
|
|
request := admissionReview.Request
|
|
|
|
if !ws.configHandler.ToFilter(request.Kind.Kind, request.Namespace, request.Name) {
|
2019-08-14 17:01:47 +00:00
|
|
|
// Resource CREATE
|
|
|
|
// Resource UPDATE
|
|
|
|
switch r.URL.Path {
|
|
|
|
case config.MutatingWebhookServicePath:
|
2019-10-19 00:38:46 +00:00
|
|
|
admissionReview.Response = ws.handleAdmissionRequest(request)
|
2019-08-14 17:01:47 +00:00
|
|
|
case config.PolicyValidatingWebhookServicePath:
|
2019-10-19 00:38:46 +00:00
|
|
|
admissionReview.Response = ws.handlePolicyValidation(request)
|
2019-08-27 23:44:10 +00:00
|
|
|
case config.PolicyMutatingWebhookServicePath:
|
2019-10-19 00:38:46 +00:00
|
|
|
admissionReview.Response = ws.handlePolicyMutation(request)
|
2019-06-18 06:41:18 +00:00
|
|
|
}
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:38:46 +00:00
|
|
|
admissionReview.Response.UID = request.UID
|
2019-05-14 15:10:25 +00:00
|
|
|
|
2019-06-26 01:16:02 +00:00
|
|
|
responseJSON, err := json.Marshal(admissionReview)
|
2019-05-14 15:10:25 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("Could not encode response: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2019-06-26 01:16:02 +00:00
|
|
|
if _, err := w.Write(responseJSON); err != nil {
|
2019-05-14 15:10:25 +00:00
|
|
|
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
2019-02-15 18:00:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 23:44:10 +00:00
|
|
|
func (ws *WebhookServer) handleAdmissionRequest(request *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse {
|
2019-11-07 20:13:16 +00:00
|
|
|
policies, err := ws.pLister.List(labels.NewSelector())
|
|
|
|
if err != nil {
|
|
|
|
//TODO check if the CRD is created ?
|
|
|
|
// Unable to connect to policy Lister to access policies
|
|
|
|
glog.Errorf("Unable to connect to policy controller to access policies. Policies are NOT being applied: %v", err)
|
|
|
|
return &v1beta1.AdmissionResponse{Allowed: true}
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
// MUTATION
|
2019-11-07 20:13:16 +00:00
|
|
|
ok, patches, msg := ws.HandleMutation(request, policies)
|
2019-08-24 01:34:23 +00:00
|
|
|
if !ok {
|
2019-10-16 03:56:41 +00:00
|
|
|
glog.V(4).Infof("Deny admission request: %v/%s/%s", request.Kind, request.Namespace, request.Name)
|
2019-08-14 18:51:01 +00:00
|
|
|
return &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: false,
|
2019-08-24 01:34:23 +00:00
|
|
|
Result: &metav1.Status{
|
|
|
|
Status: "Failure",
|
|
|
|
Message: msg,
|
|
|
|
},
|
2019-08-14 18:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
// patch the resource with patches before handling validation rules
|
|
|
|
patchedResource := processResourceWithPatches(patches, request.Object.Raw)
|
|
|
|
|
|
|
|
// VALIDATION
|
2019-11-07 20:13:16 +00:00
|
|
|
ok, msg = ws.HandleValidation(request, policies, patchedResource)
|
2019-08-24 01:34:23 +00:00
|
|
|
if !ok {
|
2019-10-16 03:56:41 +00:00
|
|
|
glog.V(4).Infof("Deny admission request: %v/%s/%s", request.Kind, request.Namespace, request.Name)
|
2019-08-24 01:34:23 +00:00
|
|
|
return &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: false,
|
|
|
|
Result: &metav1.Status{
|
|
|
|
Status: "Failure",
|
|
|
|
Message: msg,
|
|
|
|
},
|
|
|
|
}
|
2019-08-14 18:51:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
// Succesfful processing of mutation & validation rules in policy
|
|
|
|
patchType := v1beta1.PatchTypeJSONPatch
|
|
|
|
return &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: true,
|
|
|
|
Result: &metav1.Status{
|
|
|
|
Status: "Success",
|
|
|
|
},
|
|
|
|
Patch: patches,
|
|
|
|
PatchType: &patchType,
|
|
|
|
}
|
2019-08-14 18:51:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
// RunAsync TLS server in separate thread and returns control immediately
|
2019-02-07 17:22:04 +00:00
|
|
|
func (ws *WebhookServer) RunAsync() {
|
2019-03-04 18:40:02 +00:00
|
|
|
go func(ws *WebhookServer) {
|
2019-07-24 00:54:31 +00:00
|
|
|
glog.V(3).Infof("serving on %s\n", ws.server.Addr)
|
2019-08-28 00:00:16 +00:00
|
|
|
if err := ws.server.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
|
|
|
glog.Infof("HTTP server error: %v", err)
|
2019-03-04 18:40:02 +00:00
|
|
|
}
|
|
|
|
}(ws)
|
2019-05-30 19:28:56 +00:00
|
|
|
glog.Info("Started Webhook Server")
|
2019-02-07 17:22:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
// Stop TLS server and returns control after the server is shut down
|
2019-02-07 17:22:04 +00:00
|
|
|
func (ws *WebhookServer) Stop() {
|
2019-03-04 18:40:02 +00:00
|
|
|
err := ws.server.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
// Error from closing listeners, or context timeout:
|
2019-06-07 11:46:18 +00:00
|
|
|
glog.Info("Server Shutdown error: ", err)
|
2019-03-04 18:40:02 +00:00
|
|
|
ws.server.Close()
|
|
|
|
}
|
2019-08-28 00:00:16 +00:00
|
|
|
// cleanUp
|
|
|
|
// remove the static webhookconfigurations for policy CRD
|
|
|
|
ws.webhookRegistrationClient.RemovePolicyWebhookConfigurations(ws.cleanUp)
|
|
|
|
|
2019-02-07 17:22:04 +00:00
|
|
|
}
|
2019-05-13 18:27:47 +00:00
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
// bodyToAdmissionReview creates AdmissionReview object from request body
|
|
|
|
// Answers to the http.ResponseWriter if request is not valid
|
|
|
|
func (ws *WebhookServer) bodyToAdmissionReview(request *http.Request, writer http.ResponseWriter) *v1beta1.AdmissionReview {
|
|
|
|
var body []byte
|
|
|
|
if request.Body != nil {
|
|
|
|
if data, err := ioutil.ReadAll(request.Body); err == nil {
|
|
|
|
body = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(body) == 0 {
|
2019-05-30 19:28:56 +00:00
|
|
|
glog.Error("Error: empty body")
|
2019-05-14 15:10:25 +00:00
|
|
|
http.Error(writer, "empty body", http.StatusBadRequest)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := request.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/json" {
|
2019-06-07 11:46:18 +00:00
|
|
|
glog.Error("Error: invalid Content-Type: ", contentType)
|
2019-05-14 15:10:25 +00:00
|
|
|
http.Error(writer, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
admissionReview := &v1beta1.AdmissionReview{}
|
|
|
|
if err := json.Unmarshal(body, &admissionReview); err != nil {
|
2019-05-30 19:28:56 +00:00
|
|
|
glog.Errorf("Error: Can't decode body as AdmissionReview: %v", err)
|
2019-05-14 15:10:25 +00:00
|
|
|
http.Error(writer, "Can't decode body as AdmissionReview", http.StatusExpectationFailed)
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-06 00:43:59 +00:00
|
|
|
|
|
|
|
return admissionReview
|
2019-05-13 18:27:47 +00:00
|
|
|
}
|