Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow more headers to be added from console #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/build/
.vscode
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Usage of ./build/cspr-collector:
enable http output
-output-http-host string
http host to send the csp violations to (default "http://localhost:80/")
-output-http-headers string
header header key with value to send with the http request. Example "Authorization: ApiKey <APIKEY>"
-output-stdout
enable stdout output
```
Expand Down Expand Up @@ -98,6 +100,12 @@ curl -X POST \
go fmt ./...
```

## Health check endpoint
endpoint with health response "OK"
```bash
curl http://localhost:8080/health
```

## License

The MIT License (MIT). Please see the [license file](LICENSE.md) for more information.
11 changes: 9 additions & 2 deletions cmd/cspr-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"context"
"flag"
cspr "github.com/mhilker/cspr-collector"
"log"
"net/http"
"os"
"os/signal"

cspr "github.com/mhilker/cspr-collector"
)

var (
Expand All @@ -16,6 +17,7 @@ var (
OutputStdout = flag.Bool("output-stdout", false, "enable stdout output")
OutputHTTPEnabled = flag.Bool("output-http", false, "enable http output")
OutputHTTPHost = flag.String("output-http-host", "http://localhost:80/", "http host to send the csp violations to")
OutputHTTPHeaders = flag.String("output-http-headers", "", "additional headers for HTTP output")
OutputESEnabled = flag.Bool("output-es", false, "enable elasticsearch output")
OutputESHost = flag.String("output-es-host", "http://localhost:9200/", "elasticsearch host to send the csp violations to")
OutputESIndex = flag.String("output-es-index", "cspr-violations", "elasticsearch index to save the csp violations in")
Expand Down Expand Up @@ -67,7 +69,12 @@ func NewOutput() *cspr.CombinedOutput {

if *OutputHTTPEnabled {
log.Printf("Enable HTTP Output.")
outputs = append(outputs, &cspr.HTTPOutput{Url: *OutputHTTPHost})
headers := cspr.ParseHeaders(*OutputHTTPHeaders)

outputs = append(outputs, &cspr.HTTPOutput{
Url: *OutputHTTPHost,
Headers: headers,
})
}

if *OutputESEnabled {
Expand Down
11 changes: 10 additions & 1 deletion collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Collector struct {
}

func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/health" {
c.handleHealth(w)
return
}

if r.RequestURI != "/" {
message := fmt.Sprintf("Path \"%s\" not found.", r.RequestURI)
c.response(w, http.StatusNotFound, message)
Expand Down Expand Up @@ -58,7 +63,6 @@ func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.WorkQueue <- data

c.response(w, http.StatusCreated, "")
return
}

func (c *Collector) response(w http.ResponseWriter, status int, message string) {
Expand All @@ -82,3 +86,8 @@ func (c *Collector) response(w http.ResponseWriter, status int, message string)
log.Fatal(err)
}
}

func (c *Collector) handleHealth(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
25 changes: 15 additions & 10 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ func (d *Dispatcher) Run() {
}

func (d *Dispatcher) start() {
for {
select {
case work := <-d.WorkQueue:
log.Print("Received work request.")
go func() {
worker := <-d.WorkerQueue
log.Print("Dispatching work request.")
worker <- work
}()
}
for work := range d.WorkQueue {
log.Print("Received work request.")
go func(w interface{}) {
worker := <-d.WorkerQueue
log.Print("Dispatching work request.")

// Perform a type assertion
typedWork, ok := w.(CSPRequest)
if !ok {
log.Print("Invalid work request type")
return
}

worker <- typedWork
}(work)
}
}
24 changes: 23 additions & 1 deletion output_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"
"log"
"net/http"
"strings"
)

type HTTPOutput struct {
Url string
Url string
Headers map[string]string
}

func (o *HTTPOutput) Write(data []CSPRequest) {
Expand All @@ -24,6 +26,10 @@ func (o *HTTPOutput) Write(data []CSPRequest) {
log.Print(err.Error())
return
}

for key, value := range o.Headers {
request.Header.Set(key, value)
}
request.Header.Set("Content-Type", "application/json")

client := &http.Client{}
Expand All @@ -37,3 +43,19 @@ func (o *HTTPOutput) Write(data []CSPRequest) {
log.Print("Response Status:", response.Status)
}
}

func ParseHeaders(headerString string) map[string]string {
headers := make(map[string]string)
pairs := strings.Split(headerString, ",")

for _, pair := range pairs {
split := strings.SplitN(pair, ":", 2)
if len(split) == 2 {
key := strings.TrimSpace(split[0])
value := strings.TrimSpace(split[1])
headers[key] = value
}
}

return headers
}