Skip to content

Commit

Permalink
Add Targets API
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Jogeleit committed Mar 12, 2021
1 parent 11fd2a9 commit faf97db
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 9 deletions.
18 changes: 18 additions & 0 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ func clusterPolicyReportHandler(s *report.ClusterPolicyReportStore) http.Handler
}
}
}

func targetsHandler(targets []Target) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

if len(targets) == 0 {
fmt.Fprint(w, "[]")

return
}

if err := json.NewEncoder(w).Encode(targets); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{ "message": "%s" }`, err.Error())
}
}
}
21 changes: 21 additions & 0 deletions pkg/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/fjogeleit/policy-reporter/pkg/report"
"github.com/fjogeleit/policy-reporter/pkg/target"
)

// Resource API Model
Expand Down Expand Up @@ -138,3 +139,23 @@ func mapClusterPolicyReport(c report.ClusterPolicyReport) ClusterPolicyReport {
Results: results,
}
}

// Target API Model
type Target struct {
Name string `json:"name"`
MinimumPriority string `json:"minimumPriority"`
SkipExistingOnStartup bool `json:"skipExistingOnStartup"`
}

func mapTarget(t target.Client) Target {
minPrio := t.MinimumPriority()
if minPrio == "" {
minPrio = report.Priority(report.DebugPriority).String()
}

return Target{
Name: t.Name(),
MinimumPriority: minPrio,
SkipExistingOnStartup: t.SkipExistingOnStartup(),
}
}
30 changes: 21 additions & 9 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ import (
"net/http"

"github.com/fjogeleit/policy-reporter/pkg/report"
"github.com/fjogeleit/policy-reporter/pkg/target"
)

// Server for the optional HTTP REST API
type Server interface {
// Start the HTTP REST API
Start() error
}

type httpServer struct {
port int
mux *http.ServeMux
pStore *report.PolicyReportStore
cStore *report.ClusterPolicyReportStore
port int
mux *http.ServeMux
pStore *report.PolicyReportStore
cStore *report.ClusterPolicyReportStore
targets []Target
}

func (s *httpServer) registerHandler() {
s.mux.HandleFunc("/policy-reports", policyReportHandler(s.pStore))
s.mux.HandleFunc("/cluster-policy-reports", clusterPolicyReportHandler(s.cStore))
s.mux.HandleFunc("/targets", targetsHandler(s.targets))
}

func (s *httpServer) Start() error {
Expand All @@ -32,12 +37,19 @@ func (s *httpServer) Start() error {
return server.ListenAndServe()
}

func NewServer(pStore *report.PolicyReportStore, cStore *report.ClusterPolicyReportStore, port int) Server {
// NewServer constructor for a new API Server
func NewServer(pStore *report.PolicyReportStore, cStore *report.ClusterPolicyReportStore, targets []target.Client, port int) Server {
apiTargets := make([]Target, 0, len(targets))
for _, t := range targets {
apiTargets = append(apiTargets, mapTarget(t))
}

s := &httpServer{
port: port,
cStore: cStore,
pStore: pStore,
mux: http.NewServeMux(),
port: port,
targets: apiTargets,
cStore: cStore,
pStore: pStore,
mux: http.NewServeMux(),
}

s.registerHandler()
Expand Down
1 change: 1 addition & 0 deletions pkg/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (r *Resolver) APIServer() api.Server {
return api.NewServer(
r.PolicyReportStore(),
r.ClusterPolicyReportStore(),
r.TargetClients(),
r.config.API.Port,
)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/target/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ type Client interface {
Send(result report.Result)
// SkipExistingOnStartup skips already existing PolicyReportResults on startup
SkipExistingOnStartup() bool
// Name is a unique identifier for each Target
Name() string
// MinimumPriority for a triggered Result to send to this target
MinimumPriority() string
}
8 changes: 8 additions & 0 deletions pkg/target/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func (d *client) SkipExistingOnStartup() bool {
return d.skipExistingOnStartup
}

func (d *client) Name() string {
return "Discord"
}

func (d *client) MinimumPriority() string {
return d.minimumPriority
}

// NewClient creates a new loki.client to send Results to Loki
func NewClient(webhook, minimumPriority string, skipExistingOnStartup bool, httpClient httpClient) target.Client {
return &client{
Expand Down
14 changes: 14 additions & 0 deletions pkg/target/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,18 @@ func Test_LokiTarget(t *testing.T) {
t.Error("Should return configured SkipExistingOnStartup")
}
})
t.Run("Name", func(t *testing.T) {
client := discord.NewClient("http://localhost:9200", "", true, testClient{})

if client.Name() != "Discord" {
t.Errorf("Unexpected Name %s", client.Name())
}
})
t.Run("MinimumPriority", func(t *testing.T) {
client := discord.NewClient("http://localhost:9200", "debug", true, testClient{})

if client.MinimumPriority() != "debug" {
t.Errorf("Unexpected MinimumPriority %s", client.MinimumPriority())
}
})
}
8 changes: 8 additions & 0 deletions pkg/target/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func (e *client) SkipExistingOnStartup() bool {
return e.skipExistingOnStartup
}

func (e *client) Name() string {
return "Elasticsearch"
}

func (e *client) MinimumPriority() string {
return e.minimumPriority
}

// NewClient creates a new loki.client to send Results to Loki
func NewClient(host, index, rotation, minimumPriority string, skipExistingOnStartup bool, httpClient httpClient) target.Client {
return &client{
Expand Down
14 changes: 14 additions & 0 deletions pkg/target/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,18 @@ func Test_ElasticsearchTarget(t *testing.T) {
t.Error("Should return configured SkipExistingOnStartup")
}
})
t.Run("Name", func(t *testing.T) {
client := elasticsearch.NewClient("http://localhost:9200", "policy-reporter", "none", "", true, testClient{})

if client.Name() != "Elasticsearch" {
t.Errorf("Unexpected Name %s", client.Name())
}
})
t.Run("MinimumPriority", func(t *testing.T) {
client := elasticsearch.NewClient("http://localhost:9200", "policy-reporter", "none", "debug", true, testClient{})

if client.MinimumPriority() != "debug" {
t.Errorf("Unexpected MinimumPriority %s", client.MinimumPriority())
}
})
}
8 changes: 8 additions & 0 deletions pkg/target/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (l *client) SkipExistingOnStartup() bool {
return l.skipExistingOnStartup
}

func (l *client) Name() string {
return "Loki"
}

func (l *client) MinimumPriority() string {
return l.minimumPriority
}

// NewClient creates a new loki.client to send Results to Loki
func NewClient(host, minimumPriority string, skipExistingOnStartup bool, httpClient httpClient) target.Client {
return &client{
Expand Down
14 changes: 14 additions & 0 deletions pkg/target/loki/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ func Test_LokiTarget(t *testing.T) {
t.Error("Should return configured SkipExistingOnStartup")
}
})
t.Run("Name", func(t *testing.T) {
client := loki.NewClient("http://localhost:9200", "", true, testClient{})

if client.Name() != "Loki" {
t.Errorf("Unexpected Name %s", client.Name())
}
})
t.Run("MinimumPriority", func(t *testing.T) {
client := loki.NewClient("http://localhost:9200", "debug", true, testClient{})

if client.MinimumPriority() != "debug" {
t.Errorf("Unexpected MinimumPriority %s", client.MinimumPriority())
}
})
}

func convertAndValidateBody(req *http.Request, t *testing.T) (string, string) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/target/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func (s *client) SkipExistingOnStartup() bool {
return s.skipExistingOnStartup
}

func (s *client) Name() string {
return "Slack"
}

func (s *client) MinimumPriority() string {
return s.minimumPriority
}

// NewClient creates a new slack.client to send Results to Loki
func NewClient(host, minimumPriority string, skipExistingOnStartup bool, httpClient httpClient) target.Client {
return &client{
Expand Down
14 changes: 14 additions & 0 deletions pkg/target/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,18 @@ func Test_LokiTarget(t *testing.T) {
t.Error("Should return configured SkipExistingOnStartup")
}
})
t.Run("Name", func(t *testing.T) {
client := slack.NewClient("http://localhost:9200", "", true, testClient{})

if client.Name() != "Slack" {
t.Errorf("Unexpected Name %s", client.Name())
}
})
t.Run("MinimumPriority", func(t *testing.T) {
client := slack.NewClient("http://localhost:9200", "debug", true, testClient{})

if client.MinimumPriority() != "debug" {
t.Errorf("Unexpected MinimumPriority %s", client.MinimumPriority())
}
})
}

0 comments on commit faf97db

Please sign in to comment.