Skip to content

Commit

Permalink
Add Test_PackUnpackBadlyFormatted
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Oct 21, 2023
1 parent e331608 commit 102abf9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 56 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Go framework for syslog sidecars creation
- structured_data
- **message**

### Non-RFC parts
### Non-RFC parts

syslogsidecar adds rfc of produced message:
- Part name: "rfc"
Expand Down Expand Up @@ -91,8 +91,11 @@ Go framework for syslog sidecars creation
```
All messages with severity above 4 will be discarded.

### Timestamp format

### Configuration
syslogsidecar saves timestamps in [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339) format

### Configuration

Configuration of receiver part of syslogsidecar is saved in the file syslogreceiver.json:
```json
Expand Down
4 changes: 2 additions & 2 deletions parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func newparts(initialCapacity int) *parts {
}

// Initiates parts
func (p *parts) set() {
func (p *parts) set(initialCapacity int) {
if len(p.data) == 0 {
p.data = make([]rune, 128)
p.data = make([]rune, initialCapacity)
}
p.rewind()
}
Expand Down
33 changes: 1 addition & 32 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package syslogsidecar

import (
"crypto/tls"
"crypto/x509"
"os"
"sync/atomic"

"github.com/g41797/go-syslog"
Expand Down Expand Up @@ -89,7 +86,7 @@ func (s *Server) Init() error {
}

if len(s.config.ADDRTCPTLS) != 0 {
t, err := PrepareTLS(s.config.CLIENT_CERT_PATH, s.config.CLIENT_KEY_PATH, s.config.ROOT_CA_PATH)
t, err := prepareTLS(s.config.CLIENT_CERT_PATH, s.config.CLIENT_KEY_PATH, s.config.ROOT_CA_PATH)

if err != nil {
return err
Expand Down Expand Up @@ -163,31 +160,3 @@ func (s *Server) forHandle(logParts format.LogParts) bool {

return sevvalue <= s.config.SEVERITYLEVEL
}

func PrepareTLS(CLIENT_CERT_PATH, CLIENT_KEY_PATH, ROOT_CA_PATH string) (*tls.Config, error) {

if CLIENT_CERT_PATH == "" || CLIENT_KEY_PATH != "" || ROOT_CA_PATH != "" {
return nil, nil
}

cert, err := tls.LoadX509KeyPair(CLIENT_CERT_PATH, CLIENT_KEY_PATH)
if err != nil {
return nil, err
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, err
}
TLSConfig := &tls.Config{MinVersion: tls.VersionTLS12}
TLSConfig.Certificates = []tls.Certificate{cert}
certs := x509.NewCertPool()

pemData, err := os.ReadFile(ROOT_CA_PATH)
if err != nil {
return nil, err
}
certs.AppendCertsFromPEM(pemData)
TLSConfig.RootCAs = certs

return TLSConfig, nil
}
53 changes: 33 additions & 20 deletions syslogmsgparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (
ParserError = "parsererror"
FormerMessage = "data"
BrokenParts = 2
BadMessageParts = 1
RFC5424Parts = len(rfc5424parts) // RFC + 11
RFC3164Parts = len(rfc3164parts) // RFC + 7
BadMessageParts = len(formerMessage)
RFC5424Parts = len(rfc5424parts)
RFC3164Parts = len(rfc3164parts)
)

type partType struct {
Expand All @@ -40,7 +40,7 @@ type partType struct {
// https://documentation.solarwinds.com/en/success_center/kss/content/kss_adminguide_syslog_protocol.htm

var rfc3164parts = [...]partType{
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogcar
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogsidecar
{"priority", "int"},
{"facility", "int"},
{SeverityKey, "int"},
Expand All @@ -53,7 +53,7 @@ var rfc3164parts = [...]partType{
// RFC5424 parameters with type
// https://hackmd.io/@njjack/syslogformat
var rfc5424parts = [...]partType{
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogcar
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogsidecar
{"priority", "int"},
{"facility", "int"},
{SeverityKey, "int"},
Expand All @@ -76,38 +76,43 @@ type syslogmsgparts struct {
parts
}

func (mp *syslogmsgparts) pack(logParts format.LogParts, msgLen int64, err error) bool {
func (mp *syslogmsgparts) pack(logParts format.LogParts, err error) error {

if mp == nil {
return fmt.Errorf("nil syslogmsgparts")
}

if logParts == nil {
return false
return fmt.Errorf("nil logParts")
}

if len(logParts) == 0 {
return false
return fmt.Errorf("empty logParts")
}

if err != nil {
mp.packParts(formerMessage[:], logParts)
return true
return nil
}

if _, exists := logParts[RFC5424OnlyKey]; exists {
logParts[RFCFormatKey] = RFC5424
mp.packParts(rfc5424parts[:], logParts)
return true
return nil
}

if _, exists := logParts[RFC3164OnlyKey]; exists {
logParts[RFCFormatKey] = RFC3164
mp.packParts(rfc3164parts[:], logParts)
return true
return nil
}
mp.packParts(formerMessage[:], logParts)
return true
return nil
}

func (mp *syslogmsgparts) packParts(parts []partType, logParts format.LogParts) {
mp.set()

mp.set(128)

count := len(parts)
mp.setRuneAt(0, rune(count))
Expand All @@ -125,23 +130,31 @@ func (mp *syslogmsgparts) packParts(parts []partType, logParts format.LogParts)
}
}

func (mp *syslogmsgparts) Unpack(f func(name, value string) error) error {
func (mp *syslogmsgparts) Unpack(put func(name, value string) error) error {

if mp == nil {
return fmt.Errorf("nil syslogmsgparts")
}

if len(mp.data) == 0 {
return fmt.Errorf("empty syslogmsgparts")
}

count, _ := mp.runeAt(0)

switch int(count) {
case BadMessageParts:
return mp.unpackParts(formerMessage[:], f)
return mp.unpackParts(formerMessage[:], put)
case RFC5424Parts:
return mp.unpackParts(rfc5424parts[:], f)
return mp.unpackParts(rfc5424parts[:], put)
case RFC3164Parts:
return mp.unpackParts(rfc3164parts[:], f)
return mp.unpackParts(rfc3164parts[:], put)
}

return fmt.Errorf("Wrong packed syslog message")
}

func (mp *syslogmsgparts) unpackParts(parts []partType, f func(name, value string) error) error {
func (mp *syslogmsgparts) unpackParts(parts []partType, put func(name, value string) error) error {
mp.rewind()
count, _ := mp.runeAt(0)
mp.skip(int(count + 1))
Expand All @@ -152,7 +165,7 @@ func (mp *syslogmsgparts) unpackParts(parts []partType, f func(name, value strin
if err != nil {
return err
}
err = f(part.name, value)
err = put(part.name, value)
if err != nil {
return err
}
Expand All @@ -178,7 +191,7 @@ func toString(val any, typ string) string {
return result
case "time.Time":
tval, _ := val.(time.Time)
result = tval.UTC().String()
result = tval.Format(time.RFC3339)
return result
}

Expand Down
98 changes: 98 additions & 0 deletions syslogmsgparts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package syslogsidecar

import (
"fmt"
"reflect"
"strconv"
"testing"
"time"

"github.com/g41797/go-syslog/format"
)

func Test_PackUnpackBadlyFormatted(t *testing.T) {

in := map[string]string{
FormerMessage: "FormerMessage",
}

logparts, err := toLogParts(in, formerMessage[:])
if err != nil {
t.Fatalf("toLogParts error %v", err)
}

msgparts := new(syslogmsgparts)

err = msgparts.pack(logparts, fmt.Errorf("bad formatted message"))
if err != nil {
t.Errorf("pack error %v", err)
}

hlp := newpuhelper()

err = msgparts.Unpack(hlp.put)
if err != nil {
t.Errorf("Unpack error %v", err)
}

if !reflect.DeepEqual(in, hlp.slmparts) {
t.Errorf("Expected %v Actual %v", in, hlp.slmparts)
}
}

type puhelper struct {
slmparts map[string]string
}

func newpuhelper() puhelper {
var result puhelper
result.slmparts = make(map[string]string)
return result
}

func (hlp *puhelper) put(name, value string) error {
if hlp.slmparts == nil {
return fmt.Errorf("nil slmparts")
}

if _, present := hlp.slmparts[name]; present {
return fmt.Errorf("%s already exists", name)
}

hlp.slmparts[name] = value
return nil
}

func toLogParts(in map[string]string, parts []partType) (format.LogParts, error) {
if len(in) == 0 {
return nil, fmt.Errorf("empty in")
}

logParts := make(format.LogParts)

for _, part := range parts {
val, exists := in[part.name]
if !exists {
return nil, fmt.Errorf("%s does not exist", part.name)
}
logParts[part.name] = toValue(val, part.kind)
}

return logParts, nil
}

func toValue(str string, typ string) any {

switch typ {
case "string":
return str
case "int":
result, _ := strconv.Atoi(str)
return result
case "time.Time":
result, _ := time.Parse(time.RFC3339, str)
return result
}

return nil
}
35 changes: 35 additions & 0 deletions tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package syslogsidecar

import (
"crypto/tls"
"crypto/x509"
"os"
)

func prepareTLS(CLIENT_CERT_PATH, CLIENT_KEY_PATH, ROOT_CA_PATH string) (*tls.Config, error) {

if CLIENT_CERT_PATH == "" || CLIENT_KEY_PATH != "" || ROOT_CA_PATH != "" {
return nil, nil
}

cert, err := tls.LoadX509KeyPair(CLIENT_CERT_PATH, CLIENT_KEY_PATH)
if err != nil {
return nil, err
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, err
}
TLSConfig := &tls.Config{MinVersion: tls.VersionTLS12}
TLSConfig.Certificates = []tls.Certificate{cert}
certs := x509.NewCertPool()

pemData, err := os.ReadFile(ROOT_CA_PATH)
if err != nil {
return nil, err
}
certs.AppendCertsFromPEM(pemData)
TLSConfig.RootCAs = certs

return TLSConfig, nil
}

0 comments on commit 102abf9

Please sign in to comment.