Skip to content

Commit

Permalink
model
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Jan 10, 2024
1 parent a4a7626 commit 974bcb2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
36 changes: 36 additions & 0 deletions pkg/server/model/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package model

import (
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
)

type Response struct {
Results []RuleResponse `json:"results"`
}

type RuleResponse struct {
PolicyName string `json:"policy"`
RuleName string `json:"rule"`
Identifier string `json:"identifier,omitempty"`
Result jsonengine.PolicyResult `json:"result"`
Message string `json:"message"`
}

func MakeResponse(from ...jsonengine.Response) Response {
var response Response
for _, resource := range from {
for _, policy := range resource.Policies {
for _, rule := range policy.Rules {
ruleResponse := RuleResponse{
PolicyName: policy.Policy.Name,
RuleName: rule.Rule.Name,
Identifier: rule.Identifier,
Result: rule.Result,
Message: rule.Message,
}
response.Results = append(response.Results, ruleResponse)
}
}
}
return response
}
6 changes: 4 additions & 2 deletions pkg/server/playground/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/engine/template"
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"github.com/kyverno/kyverno-json/pkg/server/model"
"github.com/loopfz/gadgeto/tonic"
"sigs.k8s.io/yaml"
)

func newHandler() (gin.HandlerFunc, error) {
return tonic.Handler(func(ctx *gin.Context, in *Request) ([]jsonengine.Response, error) {
return tonic.Handler(func(ctx *gin.Context, in *Request) (*model.Response, error) {
// check input
if in == nil {
return nil, errors.New("input is null")
Expand Down Expand Up @@ -63,6 +64,7 @@ func newHandler() (gin.HandlerFunc, error) {
Policies: []*v1alpha1.ValidatingPolicy{&policy},
}))
}
return results, nil
response := model.MakeResponse(results...)
return &response, nil
}, http.StatusOK), nil
}
6 changes: 4 additions & 2 deletions pkg/server/scan/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/engine/template"
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"github.com/kyverno/kyverno-json/pkg/server/model"
"github.com/loopfz/gadgeto/tonic"
)

func newHandler(policyProvider PolicyProvider) (gin.HandlerFunc, error) {
return tonic.Handler(func(ctx *gin.Context, in *Request) ([]jsonengine.Response, error) {
return tonic.Handler(func(ctx *gin.Context, in *Request) (*model.Response, error) {
// check input
if in == nil {
return nil, errors.New("input is null")
Expand Down Expand Up @@ -60,6 +61,7 @@ func newHandler(policyProvider PolicyProvider) (gin.HandlerFunc, error) {
}))
}
// TODO: return HTTP 403 for policy failure and HTTP 406 for policy errors
return results, nil
response := model.MakeResponse(results...)
return &response, nil
}, http.StatusOK), nil
}

0 comments on commit 974bcb2

Please sign in to comment.