Skip to content

Commit

Permalink
severity dashbaords
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
fjogeleit committed Aug 30, 2024
1 parent ad498ae commit 3c3fe7b
Show file tree
Hide file tree
Showing 39 changed files with 948 additions and 107 deletions.
40 changes: 40 additions & 0 deletions backend/pkg/api/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func (c *Client) GetResourceStatusCounts(ctx context.Context, id string, query u
return api.DecodeList[ResourceStatusCount](resp.Body)
}

func (c *Client) GetResourceSeverityCounts(ctx context.Context, id string, query url.Values) ([]ResourceSeverityCount, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/v2/resource/%s/severity-counts", id), query)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.DecodeList[ResourceSeverityCount](resp.Body)
}

func (c *Client) ListSourceCategoryTree(ctx context.Context, query url.Values) ([]SourceCategoryTree, error) {
resp, err := c.Get(ctx, "/v2/sources/categories", query)
if err != nil {
Expand Down Expand Up @@ -62,6 +72,16 @@ func (c *Client) GetFindings(ctx context.Context, query url.Values) (*Findings,
return api.Decode[Findings](resp.Body)
}

func (c *Client) GetSeverityFindings(ctx context.Context, query url.Values) (*Findings, error) {
resp, err := c.Get(ctx, "/v2/severity-findings", query)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.Decode[Findings](resp.Body)
}

func (c *Client) GetNamespaceStatusCounts(ctx context.Context, source string, query url.Values) (NamespaceStatusCounts, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/v2/namespace-scoped/%s/status-counts", source), query)
if err != nil {
Expand All @@ -72,6 +92,16 @@ func (c *Client) GetNamespaceStatusCounts(ctx context.Context, source string, qu
return api.DecodeMap[string, map[string]int](resp.Body)
}

func (c *Client) GetNamespaceSeverityCounts(ctx context.Context, source string, query url.Values) (NamespaceStatusCounts, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/v2/namespace-scoped/%s/severity-counts", source), query)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.DecodeMap[string, map[string]int](resp.Body)
}

func (c *Client) GetClusterStatusCounts(ctx context.Context, source string, query url.Values) (map[string]int, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/v2/cluster-scoped/%s/status-counts", source), query)
if err != nil {
Expand All @@ -82,6 +112,16 @@ func (c *Client) GetClusterStatusCounts(ctx context.Context, source string, quer
return api.DecodeMap[string, int](resp.Body)
}

func (c *Client) GetClusterSeverityCounts(ctx context.Context, source string, query url.Values) (map[string]int, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/v2/cluster-scoped/%s/severity-counts", source), query)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return api.DecodeMap[string, int](resp.Body)
}

func (c *Client) ListSources(ctx context.Context, query url.Values) ([]string, error) {
resp, err := c.Get(ctx, "/v2/sources", query)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions backend/pkg/api/core/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Status struct {
}

type Severities struct {
Unknown int `json:"unknown"`
Low int `json:"low"`
Info int `json:"info"`
Medium int `json:"medium"`
Expand Down Expand Up @@ -48,6 +49,16 @@ type ResourceStatusCount struct {
Skip int `json:"skip"`
}

type ResourceSeverityCount struct {
Source string `json:"source,omitempty"`
Info int `json:"info"`
Low int `json:"low"`
Medium int `json:"medium"`
High int `json:"high"`
Critical int `json:"critical"`
Unknown int `json:"unknown"`
}

type SourceCategoryTree struct {
Name string `json:"name"`
Categories []Category `json:"categories"`
Expand Down
21 changes: 20 additions & 1 deletion backend/pkg/api/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const (
Warn string = "warn"
Error string = "error"
Skip string = "skip"

Unknown string = "unknown"
Info string = "info"
Low string = "low"
Medium string = "medium"
High string = "high"
Critical string = "critical"
)

const (
Expand All @@ -26,8 +33,9 @@ type Endpoints struct {

type SourceConfig struct {
Results []string
Severities []string
Exceptions bool
ChartType string
ViewType string
}

func (s SourceConfig) EnabledResults() []string {
Expand All @@ -40,3 +48,14 @@ func (s SourceConfig) EnabledResults() []string {
return !utils.Contains(s.Results, result)
})
}

func (s SourceConfig) EnabledSeverities() []string {
list := []string{Unknown, Info, Low, Medium, High, Critical}
if len(s.Severities) == 0 {
return list
}

return utils.Filter(list, func(result string) bool {
return !utils.Contains(s.Severities, result)
})
}
3 changes: 2 additions & 1 deletion backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ type Server struct {
type Source struct {
Name string `mapstructure:"name"`
Exceptions bool `mapstructure:"exceptions"`
ChartType string `mapstructure:"chartType"`
ViewType string `mapstructure:"type"`
Excludes struct {
NamespaceKinds []string `mapstructure:"namespaceKinds"`
ClusterKinds []string `mapstructure:"clusterKinds"`
Results []string `mapstructure:"results"`
Severities []string `mapstructure:"severities"`
} `mapstructure:"excludes"`
}

Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/config/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func MapConfig(c *Config) *api.Config {
Sources: utils.Map(c.Sources, func(s Source) api.Source {
return api.Source{
Name: s.Name,
ChartType: s.ChartType,
ViewType: s.ViewType,
Exceptions: s.Exceptions,
Excludes: api.Excludes{
NamespaceKinds: s.Excludes.NamespaceKinds,
ClusterKinds: s.Excludes.ClusterKinds,
Results: s.Excludes.Results,
Severities: s.Excludes.Severities,
},
}
}),
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/server/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (h *Handler) NamespaceReport(ctx *gin.Context) {
func NewHandler(config *Config, apis map[string]*model.Endpoints, customBoards map[string]CustomBoard) *Handler {
sources := make(map[string]model.SourceConfig, len(config.Sources))
for _, s := range config.Sources {
sources[s.Name] = model.SourceConfig{Results: s.Excludes.Results, Exceptions: s.Exceptions, ChartType: s.ChartType}
sources[s.Name] = model.SourceConfig{Results: s.Excludes.Results, Exceptions: s.Exceptions, ViewType: s.ViewType, Severities: s.Excludes.Severities}
}

return &Handler{config, apis, customBoards, service.New(apis, sources), reports.New(apis)}
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/server/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type Excludes struct {
NamespaceKinds []string `json:"namespaceKinds"`
ClusterKinds []string `json:"clusterKinds"`
Results []string `json:"results"`
Severities []string `json:"severities"`
}

type Source struct {
Name string `json:"name"`
ChartType string `mapstructure:"chartType"`
ViewType string `mapstructure:"type"`
Exceptions bool `mapstructure:"exceptions"`
Excludes Excludes `json:"excludes"`
}
Expand Down
80 changes: 72 additions & 8 deletions backend/pkg/service/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,50 @@ func MapFindingsToSourceStatusChart(title string, findings *core.Findings) *Char
}
}

func MapNamespaceScopeChartVariant(title string, namespaces core.NamespaceStatusCounts, status []string) *ChartVariants {
func MapSeverityFindingsToSourceStatusChart(title string, findings *core.Findings) *Chart {
if len(findings.Counts) == 0 {
return &Chart{
Name: title,
Labels: make([]string, 0),
Datasets: []*Dataset{{Data: make([]int, 0, 0)}},
Type: model.Severity,
}
}

counts := findings.Counts[0]

values := map[string]int{
SeverityUnknown: counts.Counts[SeverityUnknown],
SeverityInfo: counts.Counts[SeverityInfo],
SeverityLow: counts.Counts[SeverityLow],
SeverityMedium: counts.Counts[SeverityMedium],
SeverityHigh: counts.Counts[SeverityHigh],
SeverityCritical: counts.Counts[SeverityCritical],
}

labels := make([]string, 0, 5)
dataset := &Dataset{Data: make([]int, 0, 6)}

for s, c := range values {
if c == 0 {
continue
}

labels = append(labels, utils.Title(s))
dataset.Data = append(dataset.Data, c)
}

return &Chart{
Name: utils.Title(counts.Source),
Labels: labels,
Datasets: []*Dataset{dataset},
Type: model.Severity,
}
}

func MapNamespaceScopeChartVariant(title string, namespaces core.NamespaceStatusCounts, chartType string, status []string, defaults []string) *ChartVariants {
chart := &ChartVariants{
Complete: MapNamespaceStatusCountsToChart(title, namespaces, status),
Complete: MapNamespaceStatusCountsToChart(title, namespaces, chartType, status, defaults),
}

if len(namespaces) > 8 {
Expand Down Expand Up @@ -148,16 +189,16 @@ func MapNamespaceScopeChartVariant(title string, namespaces core.NamespaceStatus
}
}

chart.Preview = MapNamespaceStatusCountsToChart(title, prev, status)
chart.Preview = MapNamespaceStatusCountsToChart(title, prev, chartType, status, defaults)
}

return chart
}

func MapNamespaceStatusCountsToChart(title string, namespaces core.NamespaceStatusCounts, status []string) *Chart {
func MapNamespaceStatusCountsToChart(title string, namespaces core.NamespaceStatusCounts, chartTyp string, status []string, defaults []string) *Chart {
var sets = make(map[string]*Dataset)
if len(status) == 0 {
status = allStatus
status = defaults
}

for _, s := range status {
Expand Down Expand Up @@ -195,7 +236,7 @@ func MapNamespaceStatusCountsToChart(title string, namespaces core.NamespaceStat
}

datasets := make([]*Dataset, 0, len(sets))
for _, s := range allStatus {
for _, s := range defaults {
if set, ok := sets[s]; ok {
datasets = append(datasets, set)
}
Expand All @@ -205,14 +246,15 @@ func MapNamespaceStatusCountsToChart(title string, namespaces core.NamespaceStat
Name: title,
Labels: labels,
Datasets: datasets,
Type: chartTyp,
}
}

func MapNamespaceStatusCountsToCharts(findings map[string]core.NamespaceStatusCounts, status []string) map[string]*ChartVariants {
func MapNamespaceStatusCountsToCharts(findings map[string]core.NamespaceStatusCounts, chartType string, status []string, defaults []string) map[string]*ChartVariants {
charts := make(map[string]*ChartVariants, len(findings))

for source, namespaces := range findings {
charts[source] = MapNamespaceScopeChartVariant(utils.Title(source), namespaces, status)
charts[source] = MapNamespaceScopeChartVariant(utils.Title(source), namespaces, chartType, status, defaults)
}

return charts
Expand Down Expand Up @@ -253,6 +295,28 @@ func SumResourceCounts(results []core.ResourceStatusCount) map[string]int {
return values
}

func SumResourceSeverityCounts(results []core.ResourceSeverityCount) map[string]int {
values := map[string]int{
SeverityUnknown: 0,
SeverityInfo: 0,
SeverityLow: 0,
SeverityMedium: 0,
SeverityHigh: 0,
SeverityCritical: 0,
}

for _, result := range results {
values[SeverityUnknown] += result.Unknown
values[SeverityInfo] += result.Info
values[SeverityLow] += result.Low
values[SeverityMedium] += result.Medium
values[SeverityHigh] += result.High
values[SeverityCritical] += result.Critical
}

return values
}

func MapResourceSourceChart(results []core.ResourceStatusCount, status []string) *Chart {
var sets = make(map[string]*Dataset)
if len(status) == 0 {
Expand Down
14 changes: 9 additions & 5 deletions backend/pkg/service/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Total struct {

type Dashboard struct {
Title string `json:"title"`
Type string `json:"type"`
FilterSources []string `json:"filterSources,omitempty"`
ClusterScope bool `json:"clusterScope"`
Sources []string `json:"sources"`
Expand All @@ -67,14 +68,17 @@ type Dashboard struct {
Total Total `json:"total"`
ShowResults []string `json:"showResults"`
Status []string `json:"status"`
Severities []string `json:"severities"`
}

type ResourceDetails struct {
Resource *core.Resource `json:"resource"`
Results map[string]int `json:"results"`
Chart *Chart `json:"chart"`
Sources []Source `json:"sources"`
Status []string `json:"status"`
Resource *core.Resource `json:"resource"`
Results map[string]int `json:"results"`
Chart *Chart `json:"chart"`
Sources []Source `json:"sources"`
Status []string `json:"status"`
Severities []string `json:"severities"`
SeverityResults map[string]int `json:"severityResults"`
}

type Source struct {
Expand Down
Loading

0 comments on commit 3c3fe7b

Please sign in to comment.