Swap buggy timeout code for http.TimeoutHandler
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/docker/attest/pkg/attest"
|
||||
"github.com/docker/attest/pkg/oci"
|
||||
@@ -18,7 +19,17 @@ import (
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
func Handler(w http.ResponseWriter, req *http.Request) {
|
||||
func Handler() http.Handler {
|
||||
return http.HandlerFunc(handler)
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, req *http.Request) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
klog.Error(string(debug.Stack()))
|
||||
klog.ErrorS(fmt.Errorf("%v", r), "panic occurred")
|
||||
}
|
||||
}()
|
||||
|
||||
// read request body
|
||||
requestBody, err := io.ReadAll(req.Body)
|
||||
|
||||
@@ -13,8 +13,7 @@ const (
|
||||
kind = "ProviderResponse"
|
||||
)
|
||||
|
||||
// sendResponse sends back the response to Gatekeeper.
|
||||
func SendResponse(results *[]externaldata.Item, systemErr string, w http.ResponseWriter) {
|
||||
func GatekeeperResponse(results *[]externaldata.Item, systemErr string) []byte {
|
||||
response := externaldata.ProviderResponse{
|
||||
APIVersion: apiVersion,
|
||||
Kind: kind,
|
||||
@@ -29,16 +28,25 @@ func SendResponse(results *[]externaldata.Item, systemErr string, w http.Respons
|
||||
response.Response.SystemError = systemErr
|
||||
}
|
||||
|
||||
klog.InfoS("sending response", "response", response)
|
||||
|
||||
body, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "unable to marshal response")
|
||||
panic(err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func GatekeeperError(systemErr string) []byte {
|
||||
return GatekeeperResponse(nil, systemErr)
|
||||
}
|
||||
|
||||
// sendResponse sends back the response to Gatekeeper.
|
||||
func SendResponse(results *[]externaldata.Item, systemErr string, w http.ResponseWriter) {
|
||||
body := GatekeeperResponse(results, systemErr)
|
||||
klog.InfoS("sending response", "response", string(body))
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(body)
|
||||
_, err := w.Write(body)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "unable to write response")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user