2022-03-31 17:34:10 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-09-30 15:25:19 +08:00
|
|
|
"io"
|
2022-03-31 17:34:10 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
2022-07-11 23:19:47 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/tracing"
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2022-03-31 17:34:10 +02:00
|
|
|
)
|
|
|
|
|
2022-11-18 10:18:00 +01:00
|
|
|
func (h AdmissionHandler) WithAdmission(logger logr.Logger) HttpHandler {
|
2022-11-09 11:52:20 +01:00
|
|
|
return withAdmission(logger, h)
|
|
|
|
}
|
|
|
|
|
2022-11-18 10:18:00 +01:00
|
|
|
func withAdmission(logger logr.Logger, inner AdmissionHandler) HttpHandler {
|
2022-03-31 17:34:10 +02:00
|
|
|
return func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
startTime := time.Now()
|
|
|
|
if request.Body == nil {
|
|
|
|
logger.Info("empty body", "req", request.URL.String())
|
|
|
|
http.Error(writer, "empty body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer request.Body.Close()
|
2022-09-30 15:25:19 +08:00
|
|
|
body, err := io.ReadAll(request.Body)
|
2022-03-31 17:34:10 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Info("failed to read HTTP body", "req", request.URL.String())
|
|
|
|
http.Error(writer, "failed to read HTTP body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contentType := request.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/json" {
|
2022-11-01 13:51:20 +00:00
|
|
|
logger.Info("invalid Content-Type", "contentType", contentType)
|
2022-03-31 17:34:10 +02:00
|
|
|
http.Error(writer, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
|
|
|
|
return
|
|
|
|
}
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionReview := &admissionv1.AdmissionReview{}
|
2022-03-31 17:34:10 +02:00
|
|
|
if err := json.Unmarshal(body, &admissionReview); err != nil {
|
|
|
|
logger.Error(err, "failed to decode request body to type 'AdmissionReview")
|
|
|
|
http.Error(writer, "Can't decode body as AdmissionReview", http.StatusExpectationFailed)
|
|
|
|
return
|
|
|
|
}
|
2022-05-12 17:47:44 +02:00
|
|
|
logger := logger.WithValues(
|
2022-03-31 17:34:10 +02:00
|
|
|
"kind", admissionReview.Request.Kind,
|
|
|
|
"namespace", admissionReview.Request.Namespace,
|
|
|
|
"name", admissionReview.Request.Name,
|
|
|
|
"operation", admissionReview.Request.Operation,
|
|
|
|
"uid", admissionReview.Request.UID,
|
2022-10-14 18:29:48 +02:00
|
|
|
"user", admissionReview.Request.UserInfo,
|
2022-03-31 17:34:10 +02:00
|
|
|
)
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionReview.Response = &admissionv1.AdmissionResponse{
|
2022-03-31 17:34:10 +02:00
|
|
|
Allowed: true,
|
|
|
|
UID: admissionReview.Request.UID,
|
|
|
|
}
|
2022-07-11 23:19:47 +05:30
|
|
|
// start span from request context
|
2022-11-17 16:17:52 +01:00
|
|
|
ctx, span := tracing.StartSpan(
|
|
|
|
request.Context(),
|
2022-11-20 14:42:57 +01:00
|
|
|
"webhooks/handlers",
|
|
|
|
fmt.Sprintf("ADMISSION %s %s", admissionReview.Request.Operation, admissionReview.Request.Kind),
|
|
|
|
admissionRequestAttributes(admissionReview.Request)...,
|
2022-11-17 15:41:49 +01:00
|
|
|
)
|
2022-07-11 23:19:47 +05:30
|
|
|
defer span.End()
|
2022-11-17 16:17:52 +01:00
|
|
|
adminssionResponse := inner(ctx, logger, admissionReview.Request, startTime)
|
|
|
|
if adminssionResponse != nil {
|
|
|
|
admissionReview.Response = adminssionResponse
|
|
|
|
}
|
|
|
|
responseJSON, err := json.Marshal(admissionReview)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, fmt.Sprintf("Could not encode response: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-03-31 17:34:10 +02:00
|
|
|
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if _, err := writer.Write(responseJSON); err != nil {
|
|
|
|
http.Error(writer, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
|
|
|
|
}
|
2022-04-25 20:20:40 +08:00
|
|
|
if admissionReview.Request.Kind.Kind == "Lease" {
|
|
|
|
logger.V(6).Info("admission review request processed", "time", time.Since(startTime).String())
|
|
|
|
} else {
|
|
|
|
logger.V(4).Info("admission review request processed", "time", time.Since(startTime).String())
|
|
|
|
}
|
2022-03-31 17:34:10 +02:00
|
|
|
}
|
|
|
|
}
|