Skip to content

Commit

Permalink
Allow more headers to be added from console
Browse files Browse the repository at this point in the history
  • Loading branch information
Spyros Psarras committed Jul 4, 2023
1 parent 347bac5 commit 983660a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/build/
.vscode
2 changes: 2 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 "uthorization: ApiKey <APIKEY>"
-output-stdout
enable stdout output
```
Expand Down
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
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
}

0 comments on commit 983660a

Please sign in to comment.