Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
fjogeleit committed Dec 23, 2023
1 parent eb8d766 commit 86183cb
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 94 deletions.
1 change: 1 addition & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (s *httpServer) RegisterV1Handler(finder v1.PolicyReportFinder) {
s.mux.HandleFunc("/v1/resource-status-counts", s.middleware(handler.ResourceStatusCountsHandler()))
s.mux.HandleFunc("/v1/resource", s.middleware(handler.ResourceHandler()))
s.mux.HandleFunc("/v1/results", s.middleware(handler.ResultHandler()))
s.mux.HandleFunc("/v1/policies", s.middleware(handler.PolicyListHandler()))
}

func (s *httpServer) RegisterMetricsHandler() {
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/v1/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Filter struct {
ResourceID string
ReportLabel map[string]string
Exclude map[string][]string
Namespaced bool
Search string
}

Expand Down Expand Up @@ -82,7 +83,7 @@ type PolicyReportFinder interface {
FetchNamespacedReportLabels(context.Context, Filter) (map[string][]string, error)

FetchFindingCounts(context.Context, Filter) (*Findings, error)
FetchSources(context.Context, string) ([]*Source, error)
FetchSources(context.Context, string, Filter) ([]*Source, error)

FetchNamespacedResourceResults(context.Context, Filter, Pagination) ([]*ResourceResult, error)
FetchClusterResourceResults(context.Context, Filter, Pagination) ([]*ResourceResult, error)
Expand All @@ -94,4 +95,6 @@ type PolicyReportFinder interface {

FetchResults(context.Context, string, Filter, Pagination) ([]*ListResult, error)
CountResults(context.Context, string, Filter) (int, error)

FetchPolicies(context.Context, Filter) (any, error)
}
11 changes: 10 additions & 1 deletion pkg/api/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (h *Handler) FetchFindingCountsHandler() http.HandlerFunc {
// SourceListHandler REST API
func (h *Handler) SourceListHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
list, err := h.finder.FetchSources(req.Context(), req.URL.Query().Get("id"))
list, err := h.finder.FetchSources(req.Context(), req.URL.Query().Get("id"), buildFilter(req))
h.logError(err)
helper.SendJSONResponse(w, list, err)
}
Expand Down Expand Up @@ -322,6 +322,14 @@ func (h *Handler) ResourceHandler() http.HandlerFunc {
}
}

// ResourceHandler REST API
func (h *Handler) PolicyListHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
list, err := h.finder.FetchPolicies(req.Context(), buildFilter(req))
h.logError(err)
helper.SendJSONResponse(w, list, err)
}
}
func buildPagination(req *http.Request, defaultOrder []string) Pagination {
page, err := strconv.Atoi(req.URL.Query().Get("page"))
if err != nil || page < 1 {
Expand Down Expand Up @@ -389,6 +397,7 @@ func buildFilter(req *http.Request) Filter {
Search: req.URL.Query().Get("search"),
ResourceID: req.URL.Query().Get("resource_id"),
Exclude: exclude,
Namespaced: req.URL.Query().Get("namespaced") == "true",
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/api/v1/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ type ResourceResult struct {
Error int `json:"error"`
}

type PolicyResult struct {
Source string `json:"source,omitempty"`
Category string `json:"category,omitempty"`
Policy string `json:"policy"`
Severity string `json:"severity,omitempty"`
Results map[string]int `json:"results"`
}

type ResourceResultList struct {
Items []*ResourceResult `json:"items"`
Count int `json:"count"`
Expand Down
Loading

0 comments on commit 86183cb

Please sign in to comment.