-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from xuzhu-591/feat-validating-admission-webhook
Feat validating admission webhook
- Loading branch information
Showing
12 changed files
with
727 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package admission | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/horizoncd/horizon/core/common" | ||
"github.com/horizoncd/horizon/core/middleware" | ||
admissionwebhook "github.com/horizoncd/horizon/pkg/admission" | ||
admissionmodels "github.com/horizoncd/horizon/pkg/admission/models" | ||
"github.com/horizoncd/horizon/pkg/auth" | ||
"github.com/horizoncd/horizon/pkg/server/response" | ||
"github.com/horizoncd/horizon/pkg/server/rpcerror" | ||
"github.com/horizoncd/horizon/pkg/util/log" | ||
) | ||
|
||
// Middleware to validate and mutate admission request | ||
func Middleware(skippers ...middleware.Skipper) gin.HandlerFunc { | ||
return middleware.New(func(c *gin.Context) { | ||
// get auth record | ||
record, ok := c.Get(common.ContextAuthRecord) | ||
if !ok { | ||
response.AbortWithRPCError(c, | ||
rpcerror.BadRequestError.WithErrMsg("request with no auth record")) | ||
return | ||
} | ||
attr := record.(auth.AttributesRecord) | ||
// non resource request or read only request should be ignored | ||
if !attr.IsResourceRequest() || attr.IsReadOnly() { | ||
c.Next() | ||
return | ||
} | ||
var object interface{} | ||
// 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 | ||
} | ||
// restore the request body | ||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
if len(bodyBytes) > 0 { | ||
contentType := c.ContentType() | ||
if contentType == binding.MIMEJSON || contentType == "" { | ||
if err := json.Unmarshal(bodyBytes, &object); err != nil { | ||
response.AbortWithRPCError(c, | ||
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("unmarshal request body failed, err: %v", err))) | ||
return | ||
} | ||
} else { | ||
log.Errorf(c, "unsupported content type: %s", contentType) | ||
response.AbortWithRPCError(c, | ||
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("unsupported content type: %s", contentType))) | ||
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(), | ||
Name: attr.GetName(), | ||
SubResource: attr.GetSubResource(), | ||
Version: attr.GetAPIVersion(), | ||
Object: object, | ||
Options: options, | ||
} | ||
if err := admissionwebhook.Validating(c, admissionRequest); err != nil { | ||
response.AbortWithRPCError(c, | ||
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("admission validating failed: %v", err))) | ||
return | ||
} | ||
c.Next() | ||
}, skippers...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.