From 51a638c85a2fc5d71c96e65505a0e0b51bd14d20 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Sun, 14 Jan 2024 12:44:03 +0100 Subject: [PATCH] Update API Signed-off-by: Frank Jogeleit --- backend/go.mod | 1 + backend/go.sum | 2 + backend/pkg/api/plugin/client.go | 11 +- backend/pkg/api/plugin/model.go | 56 -------- backend/pkg/server/api/handler.go | 2 +- backend/pkg/server/api/mapper.go | 14 +- backend/pkg/service/mapper.go | 47 +++++++ backend/pkg/service/model.go | 4 +- backend/pkg/service/service.go | 37 +---- .../core/components/policy/Details.vue | 129 +++++++++--------- frontend/modules/core/types.ts | 4 +- 11 files changed, 140 insertions(+), 167 deletions(-) delete mode 100644 backend/pkg/api/plugin/model.go diff --git a/backend/go.mod b/backend/go.mod index d53e2f05..6ccf62b9 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -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 diff --git a/backend/go.sum b/backend/go.sum index ef71f1e1..6f7b191d 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -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= diff --git a/backend/pkg/api/plugin/client.go b/backend/pkg/api/plugin/client.go index 10bc3304..09fb8c9f 100644 --- a/backend/pkg/api/plugin/client.go +++ b/backend/pkg/api/plugin/client.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" + plugin "github.com/kyverno/policy-reporter-plugins/sdk/api" "github.com/kyverno/policy-reporter-ui/pkg/api" ) @@ -12,24 +13,24 @@ 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) { diff --git a/backend/pkg/api/plugin/model.go b/backend/pkg/api/plugin/model.go deleted file mode 100644 index 582f9983..00000000 --- a/backend/pkg/api/plugin/model.go +++ /dev/null @@ -1,56 +0,0 @@ -package plugin - -import "fmt" - -type Policy struct { - Category string `json:"category"` - Namespace string `json:"namespace,omitempty"` - Name string `json:"name"` - Title string `json:"title"` - Description string `json:"description"` - Severity string `json:"severity,omitempty"` -} - -func (p Policy) ID() string { - if p.Namespace == "" { - return p.Name - } - - return fmt.Sprintf("%s/%s", p.Namespace, p.Name) -} - -type Engine struct { - Name string `json:"name"` - KubernetesVersion string `json:"kubernetesVersion,omitempty"` - Version string `json:"version,omitempty"` - Subjects []string `json:"subjects,omitempty"` -} - -type SourceCode struct { - ContentType string `json:"contentType"` - Content string `json:"content"` -} - -type Item struct { - Title string `json:"title"` - Value string `json:"value"` -} - -type Details struct { - Title string `json:"title"` - Items []Item `json:"items"` -} - -type PolicyDetails struct { - Category string `json:"category"` - Namespace string `json:"namespace,omitempty"` - Name string `json:"name"` - Title string `json:"title"` - Description string `json:"description"` - Severity string `json:"severity,omitempty"` - Engine Engine `json:"engine,omitempty"` - SourceCode SourceCode `json:"code,omitempty"` - Additional []Item `json:"additional"` - Details []Details `json:"details,omitempty"` - References []string `json:"references,omitempty"` -} diff --git a/backend/pkg/server/api/handler.go b/backend/pkg/server/api/handler.go index c7980002..ccd4f87f 100644 --- a/backend/pkg/server/api/handler.go +++ b/backend/pkg/server/api/handler.go @@ -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 { diff --git a/backend/pkg/server/api/mapper.go b/backend/pkg/server/api/mapper.go index c8286df0..75dc144a 100644 --- a/backend/pkg/server/api/mapper.go +++ b/backend/pkg/server/api/mapper.go @@ -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" ) @@ -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 { @@ -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] } @@ -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) +} diff --git a/backend/pkg/service/mapper.go b/backend/pkg/service/mapper.go index 398040da..f101bb60 100644 --- a/backend/pkg/service/mapper.go +++ b/backend/pkg/service/mapper.go @@ -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" ) @@ -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 +} diff --git a/backend/pkg/service/model.go b/backend/pkg/service/model.go index 584ce5ab..e6c93062 100644 --- a/backend/pkg/service/model.go +++ b/backend/pkg/service/model.go @@ -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"` } diff --git a/backend/pkg/service/service.go b/backend/pkg/service/service.go index 482a3896..49d64fe9 100644 --- a/backend/pkg/service/service.go +++ b/backend/pkg/service/service.go @@ -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" @@ -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) @@ -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) { diff --git a/frontend/modules/core/components/policy/Details.vue b/frontend/modules/core/components/policy/Details.vue index bf7e2672..bda501d3 100644 --- a/frontend/modules/core/components/policy/Details.vue +++ b/frontend/modules/core/components/policy/Details.vue @@ -9,8 +9,9 @@ -