2019-02-07 19:22:04 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-03-04 20:40:02 +02:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-03-21 18:14:26 +02:00
|
|
|
"github.com/nirmata/kube-policy/config"
|
2019-03-21 15:57:30 +02:00
|
|
|
"github.com/nirmata/kube-policy/utils"
|
2019-03-21 18:14:26 +02:00
|
|
|
"github.com/nirmata/kube-policy/webhooks"
|
2019-03-15 19:03:55 +02:00
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
2019-02-07 19:22:04 +02:00
|
|
|
)
|
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
// WebhookServer contains configured TLS server with MutationWebhook.
|
|
|
|
// MutationWebhook gets policies from policyController and takes control of the cluster with kubeclient.
|
2019-02-07 19:22:04 +02:00
|
|
|
type WebhookServer struct {
|
2019-03-22 22:11:55 +02:00
|
|
|
server http.Server
|
|
|
|
mutationWebhook *webhooks.MutationWebhook
|
|
|
|
logger *log.Logger
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebhookServer creates new instance of WebhookServer accordingly to given configuration
|
|
|
|
// Policy Controller and Kubernetes Client should be initialized in configuration
|
2019-03-22 22:11:55 +02:00
|
|
|
func NewWebhookServer(tlsPair *utils.TlsPemPair, mutationWebhook *webhooks.MutationWebhook, logger *log.Logger) (*WebhookServer, error) {
|
2019-03-04 20:40:02 +02:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.New(os.Stdout, "HTTPS Server: ", log.LstdFlags|log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:11:55 +02:00
|
|
|
if tlsPair == nil || mutationWebhook == nil {
|
|
|
|
return nil, errors.New("NewWebhookServer is not initialized properly")
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig tls.Config
|
2019-03-22 22:11:55 +02:00
|
|
|
pair, err := tls.X509KeyPair(tlsPair.Certificate, tlsPair.PrivateKey)
|
2019-03-04 20:40:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{pair}
|
|
|
|
|
|
|
|
ws := &WebhookServer{
|
2019-03-07 17:42:37 +02:00
|
|
|
logger: logger,
|
2019-03-22 22:11:55 +02:00
|
|
|
mutationWebhook: mutationWebhook,
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
2019-03-21 18:09:14 +02:00
|
|
|
mux.HandleFunc(config.WebhookServicePath, ws.serve)
|
2019-03-04 20:40:02 +02:00
|
|
|
|
|
|
|
ws.server = http.Server{
|
|
|
|
Addr: ":443", // Listen on port for HTTPS requests
|
|
|
|
TLSConfig: &tlsConfig,
|
|
|
|
Handler: mux,
|
|
|
|
ErrorLog: logger,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, nil
|
2019-02-21 20:31:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// Main server endpoint for all requests
|
2019-02-07 19:22:04 +02:00
|
|
|
func (ws *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
2019-03-21 18:09:14 +02:00
|
|
|
if r.URL.Path == config.WebhookServicePath {
|
2019-03-04 20:40:02 +02:00
|
|
|
admissionReview := ws.parseAdmissionReview(r, w)
|
|
|
|
if admissionReview == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var admissionResponse *v1beta1.AdmissionResponse
|
|
|
|
if webhooks.AdmissionIsRequired(admissionReview.Request) {
|
2019-03-07 17:42:37 +02:00
|
|
|
admissionResponse = ws.mutationWebhook.Mutate(admissionReview.Request)
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if admissionResponse == nil {
|
|
|
|
admissionResponse = &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
admissionReview.Response = admissionResponse
|
|
|
|
admissionReview.Response.UID = admissionReview.Request.UID
|
|
|
|
|
|
|
|
responseJson, err := json.Marshal(admissionReview)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("Could not encode response: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.logger.Printf("Response body\n:%v", string(responseJson))
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if _, err := w.Write(responseJson); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, fmt.Sprintf("Unexpected method path: %v", r.URL.Path), http.StatusNotFound)
|
|
|
|
}
|
2019-02-15 20:00:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Answers to the http.ResponseWriter if request is not valid
|
|
|
|
func (ws *WebhookServer) parseAdmissionReview(request *http.Request, writer http.ResponseWriter) *v1beta1.AdmissionReview {
|
2019-03-04 20:40:02 +02:00
|
|
|
var body []byte
|
|
|
|
if request.Body != nil {
|
|
|
|
if data, err := ioutil.ReadAll(request.Body); err == nil {
|
|
|
|
body = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(body) == 0 {
|
|
|
|
ws.logger.Print("Error: empty body")
|
|
|
|
http.Error(writer, "empty body", http.StatusBadRequest)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := request.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/json" {
|
|
|
|
ws.logger.Printf("Error: invalid Content-Type: %v", contentType)
|
|
|
|
http.Error(writer, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
admissionReview := &v1beta1.AdmissionReview{}
|
|
|
|
if err := json.Unmarshal(body, &admissionReview); err != nil {
|
|
|
|
ws.logger.Printf("Error: Can't decode body as AdmissionReview: %v", err)
|
|
|
|
http.Error(writer, "Can't decode body as AdmissionReview", http.StatusExpectationFailed)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
ws.logger.Printf("Request body:\n%v", string(body))
|
|
|
|
return admissionReview
|
|
|
|
}
|
2019-02-15 20:00:49 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// Runs TLS server in separate thread and returns control immediately
|
2019-02-07 19:22:04 +02:00
|
|
|
func (ws *WebhookServer) RunAsync() {
|
2019-03-04 20:40:02 +02:00
|
|
|
go func(ws *WebhookServer) {
|
|
|
|
err := ws.server.ListenAndServeTLS("", "")
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Fatal(err)
|
|
|
|
}
|
|
|
|
}(ws)
|
2019-02-07 19:22:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// Stops TLS server and returns control after the server is shut down
|
2019-02-07 19:22:04 +02:00
|
|
|
func (ws *WebhookServer) Stop() {
|
2019-03-04 20:40:02 +02:00
|
|
|
err := ws.server.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
// Error from closing listeners, or context timeout:
|
|
|
|
ws.logger.Printf("Server Shutdown error: %v", err)
|
|
|
|
ws.server.Close()
|
|
|
|
}
|
2019-02-07 19:22:04 +02:00
|
|
|
}
|