Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
Signed-off-by: xu.zhu <[email protected]>
  • Loading branch information
xuzhu-591 committed Dec 18, 2023
1 parent 63eb270 commit f23593b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions core/middleware/admission/admission.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package admission

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"

"github.com/gin-gonic/gin"
"github.com/horizoncd/horizon/core/common"
Expand Down Expand Up @@ -30,19 +33,33 @@ func Middleware(skippers ...middleware.Skipper) gin.HandlerFunc {
return
}
var object interface{}
if err := c.ShouldBind(object); err != nil {
// read request body and avoid side-effects on c.Request.Body
bodyBytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("request body is invalid, err: %v", err)))
return
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
if err := json.Unmarshal(bodyBytes, &object); err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("unmarshal request body failed, err: %v", err)))
return
}
// fill in the request url query into admission request options
queries := c.Request.URL.Query()
options := make(map[string]interface{}, len(queries))
for k, v := range queries {
options[k] = v
}
admissionRequest := &admissionwebhook.Request{
Operation: admissionmodels.Operation(attr.GetVerb()),
Resource: attr.GetResource(),
ResourceName: attr.GetName(),
SubResource: attr.GetSubResource(),
Version: attr.GetAPIVersion(),
Object: object,
OldObject: nil,
Options: options,
}
if err := admissionwebhook.Validating(c, admissionRequest); err != nil {
response.AbortWithRPCError(c,
Expand Down

0 comments on commit f23593b

Please sign in to comment.