Skip to content

Commit

Permalink
Simple input checking for event generation (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcktdmp authored Mar 6, 2020
1 parent ba33c4a commit fea519a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Write tests
* Use interfaces
* Improve overall code construct
* Work with default values or force mandatory fields
* Implement extension fields
* Implement interface for struct and write use reflect to verify
mandatory fields being set
* ..
49 changes: 38 additions & 11 deletions cefevent/cefevent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cefevent

import (
"errors"
"fmt"
"strings"
)
Expand All @@ -16,6 +17,15 @@ type CefEvent struct {
Extensions map[string]string
}

func isSet(field ...string) bool {
for _, f := range field {
if f == "" {
return false
}
}
return true
}

// todo: don't dupe the function but handle
// with methods part of an Extension struct
func cefEscapeField(field string) string {
Expand All @@ -41,16 +51,30 @@ func cefEscapeExtension(field string) string {
return replacer.Replace(field)
}

func (event *CefEvent) Generate() string {
func (event *CefEvent) Generate() (string, error) {

if (isSet)(
event.Version,
event.DeviceVendor,
event.DeviceProduct,
event.DeviceVersion,
event.DeviceEventClassId,
event.Name,
event.Severity) {

event.Version = cefEscapeField(event.Version)
event.DeviceVendor = cefEscapeField(event.DeviceVendor)
event.DeviceProduct = cefEscapeField(event.DeviceProduct)
event.DeviceVersion = cefEscapeField(event.DeviceVersion)
event.DeviceEventClassId = cefEscapeField(event.DeviceEventClassId)
event.Name = cefEscapeField(event.Name)
event.Severity = cefEscapeField(event.Severity)

// todo: do this nicely with methods
event.Version = cefEscapeField(event.Version)
event.DeviceVendor = cefEscapeField(event.DeviceVendor)
event.DeviceProduct = cefEscapeField(event.DeviceProduct)
event.DeviceVersion = cefEscapeField(event.DeviceVersion)
event.DeviceEventClassId = cefEscapeField(event.DeviceEventClassId)
event.Name = cefEscapeField(event.Name)
event.Severity = cefEscapeField(event.Severity)
} else {

return "", errors.New("Not all mandatory CEF fields are set.")

}

var p strings.Builder

Expand All @@ -67,10 +91,13 @@ func (event *CefEvent) Generate() string {
// fields according to the CEF standard.
extensionString := strings.TrimSpace(p.String())

return fmt.Sprintf(
eventCef := fmt.Sprintf(
"CEF:%v|%v|%v|%v|%v|%v|%v|%v",
event.Version, event.DeviceVendor,
event.DeviceProduct, event.DeviceVersion,
event.DeviceEventClassId, event.Name,
event.Severity, extensionString)
event.Severity, extensionString,
)

return eventCef, nil
}
25 changes: 23 additions & 2 deletions cefevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestCefEventExpected(t *testing.T) {
}

want := "CEF:0|Cool Vendor|Cool Product|1.0|COOL_THING|Something cool happened.|Unknown|sourceAddress=127.0.0.1"
got := event.Generate()
got, _ := event.Generate()

if want != got {
t.Errorf("event.Generate() = %q, want %q", got, want)
Expand All @@ -47,10 +47,31 @@ func TestCefEventEscape(t *testing.T) {
}

want := "CEF:0|\\\\Cool\\nVendor\\||Cool Product|1.0|COOL_THING|Something cool happened.|Unknown|sourceAddress\\\\=\\n127.0.0.1\\="
got := event.Generate()
got, _ := event.Generate()

if want != got {
t.Errorf("event.Generate() = %q, want %q", got, want)
}

}

func TestCefEventMandatoryFields(t *testing.T) {

event := cefevent.CefEvent{
Version: "0",
DeviceVendor: "Cool Vendor",
DeviceProduct: "Cool Product",
DeviceVersion: "1.0",
DeviceEventClassId: "COOL_THING",
Name: "Something cool happened.",
Severity: "Unknown",
}

noVersion := event
noVersion.Version = ""
_, err := noVersion.Generate()

if err == nil {
t.Errorf("%v", err)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module cefevent

go 1.12

require github.com/pkg/errors v0.9.1

0 comments on commit fea519a

Please sign in to comment.