Skip to content

Commit

Permalink
Update API
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
  • Loading branch information
Frank Jogeleit committed Jan 14, 2024
1 parent 91f2b48 commit 51a638c
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 167 deletions.
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/go-redis/redis/v8 v8.11.5
github.com/gosimple/slug v1.13.1
github.com/kyverno/policy-reporter-plugins/sdk/api v0.0.0-20240111213703-af5a15ec3660
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.0
go.uber.org/zap v1.26.0
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kyverno/policy-reporter-plugins/sdk/api v0.0.0-20240111213703-af5a15ec3660 h1:8LfJjFMlWtOgkB218Lg6ndwHhihaT1jtPUe9yxHJB7A=
github.com/kyverno/policy-reporter-plugins/sdk/api v0.0.0-20240111213703-af5a15ec3660/go.mod h1:B6bwQr0mBUEMXltiWfgI8czu0O8bz6y/gVhZ2AvuCpk=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
11 changes: 6 additions & 5 deletions backend/pkg/api/plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ import (
"fmt"
"net/url"

plugin "github.com/kyverno/policy-reporter-plugins/sdk/api"
"github.com/kyverno/policy-reporter-ui/pkg/api"
)

type Client struct {
*api.Client
}

func (c *Client) ListPolicies(ctx context.Context, query url.Values) ([]Policy, error) {
resp, err := c.Get(ctx, "/policies", query)
func (c *Client) GetPolicies(ctx context.Context) ([]plugin.PolicyListItem, error) {
resp, err := c.Get(ctx, "/policies", url.Values{})
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.DecodeList[Policy](resp.Body)
return api.DecodeList[plugin.PolicyListItem](resp.Body)
}

func (c *Client) GetPolicy(ctx context.Context, name string) (*PolicyDetails, error) {
func (c *Client) GetPolicy(ctx context.Context, name string) (*plugin.Policy, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/policies/%s", name), url.Values{})
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.Decode[PolicyDetails](resp.Body)
return api.Decode[plugin.Policy](resp.Body)
}

func New(options []api.ClientOption) (*Client, error) {
Expand Down
56 changes: 0 additions & 56 deletions backend/pkg/api/plugin/model.go

This file was deleted.

2 changes: 1 addition & 1 deletion backend/pkg/server/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (h *Handler) Policies(ctx *gin.Context) {
}

if plugin, ok := endpoints.Plugins[source]; ok {
policies, err := plugin.ListPolicies(ctx, query)
policies, err := plugin.GetPolicies(ctx)
if err != nil {
zap.L().Error("failed to load policies from plugin", zap.String("cluster", ctx.Param("cluster")), zap.String("plugin", source), zap.Error(err))
} else {
Expand Down
14 changes: 11 additions & 3 deletions backend/pkg/server/api/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"sort"

plugin "github.com/kyverno/policy-reporter-plugins/sdk/api"
"github.com/kyverno/policy-reporter-ui/pkg/api/core"
"github.com/kyverno/policy-reporter-ui/pkg/api/plugin"
"github.com/kyverno/policy-reporter-ui/pkg/utils"
)

Expand Down Expand Up @@ -119,7 +119,7 @@ func MapPoliciesFromCore(policies []core.Policy) map[string][]Policy {
return results
}

func MapPluginPolicies(policies []plugin.Policy, coreList []core.Policy) map[string][]Policy {
func MapPluginPolicies(policies []plugin.PolicyListItem, coreList []core.Policy) map[string][]Policy {
results := make(map[string][]Policy)

if coreList == nil || len(coreList) == 0 {
Expand All @@ -140,7 +140,7 @@ func MapPluginPolicies(policies []plugin.Policy, coreList []core.Policy) map[str
continue
}

corePolicy := cache[policy.Category][policy.ID()]
corePolicy := cache[policy.Category][policyID(policy)]
if corePolicy == nil {
corePolicy = cache[policy.Category][policy.Name]
}
Expand Down Expand Up @@ -171,3 +171,11 @@ func MapPluginPolicies(policies []plugin.Policy, coreList []core.Policy) map[str

return results
}

func policyID(p plugin.PolicyListItem) string {
if p.Namespace == "" {
return p.Name
}

return fmt.Sprintf("%s/%s", p.Namespace, p.Name)
}
47 changes: 47 additions & 0 deletions backend/pkg/service/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sort"

plugin "github.com/kyverno/policy-reporter-plugins/sdk/api"
pluginAPI "github.com/kyverno/policy-reporter-plugins/sdk/api"
"github.com/kyverno/policy-reporter-ui/pkg/api/core"
"github.com/kyverno/policy-reporter-ui/pkg/utils"
)
Expand Down Expand Up @@ -284,3 +286,48 @@ func MapCategoriesToChart(title string, categories []core.Category) *Chart {
},
}
}

func MapPolicyDetails(details *PolicyDetails, policy *plugin.Policy) *PolicyDetails {
if policy == nil {
return details
}

details.Title = policy.Title
details.Description = policy.Description
details.Severity = policy.Severity
details.References = utils.Map(policy.References, func(ref pluginAPI.Reference) string {
return ref.URL
})

details.ShowDetails = true

if policy.Engine != nil {
details.Engine = &Engine{
Name: policy.Engine.Name,
Version: policy.Engine.Version,
Subjects: policy.Engine.Subjects,
}
}

if policy.SourceCode != nil {
details.SourceCode = &SourceCode{
ContentType: policy.SourceCode.ContentType,
Content: policy.SourceCode.Content,
}
}

details.Additional = utils.Map(policy.Additional, func(d pluginAPI.Details) Details {
return Details{
Title: d.Title,
Items: utils.Map(d.Items, func(i pluginAPI.DetailsItem) Item {
return Item{Title: i.Title, Value: i.Value}
}),
}
})

details.Details = utils.Map(policy.Details, func(i pluginAPI.DetailsItem) Item {
return Item{Title: i.Title, Value: i.Value}
})

return details
}
4 changes: 2 additions & 2 deletions backend/pkg/service/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ type PolicyDetails struct {
Severity string `json:"severity,omitempty"`
Engine *Engine `json:"engine,omitempty"`
SourceCode *SourceCode `json:"sourceCode,omitempty"`
Additional []Item `json:"additional,omitempty"`
Details []Details `json:"details,omitempty"`
Details []Item `json:"details,omitempty"`
Additional []Details `json:"additional,omitempty"`
References []string `json:"references,omitempty"`
ShowDetails bool `json:"showDetails"`
}
37 changes: 3 additions & 34 deletions backend/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"sync"

pluginAPI "github.com/kyverno/policy-reporter-plugins/sdk/api"
"github.com/kyverno/policy-reporter-ui/pkg/api/core"
"github.com/kyverno/policy-reporter-ui/pkg/api/plugin"
"github.com/kyverno/policy-reporter-ui/pkg/utils"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (s *Service) PolicyDetails(ctx context.Context, cluster, source, policy str

g := &errgroup.Group{}

var details *plugin.PolicyDetails
var details *pluginAPI.Policy
if plugin, ok := s.plugin(cluster, source); ok {
g.Go(func() error {
details, err = plugin.GetPolicy(ctx, policy)
Expand Down Expand Up @@ -123,39 +124,7 @@ func (s *Service) PolicyDetails(ctx context.Context, cluster, source, policy str
},
}

if details != nil {
response.Title = details.Title
response.Description = details.Description
response.Severity = details.Severity
response.References = details.References
response.Additional = utils.Map(details.Additional, func(i plugin.Item) Item {
return Item{Title: i.Title, Value: i.Value}
})
response.ShowDetails = true

response.Engine = &Engine{
Name: details.Engine.Name,
Version: details.Engine.Version,
KubernetesVersion: details.Engine.KubernetesVersion,
Subjects: details.Engine.Subjects,
}

response.SourceCode = &SourceCode{
ContentType: details.SourceCode.ContentType,
Content: details.SourceCode.Content,
}

response.Details = utils.Map(details.Details, func(d plugin.Details) Details {
return Details{
Title: d.Title,
Items: utils.Map(d.Items, func(i plugin.Item) Item {
return Item{Title: i.Title, Value: i.Value}
}),
}
})
}

return response, nil
return MapPolicyDetails(response, details), nil
}

func (s *Service) PolicySources(ctx context.Context, cluster string, query url.Values) ([]Source, error) {
Expand Down
Loading

0 comments on commit 51a638c

Please sign in to comment.