Skip to content

Commit

Permalink
fix(syslogexporter): Change config validation to follow OTC standards…
Browse files Browse the repository at this point in the history
… and fix timestamp format issues
  • Loading branch information
rnishtala-sumo authored and kasia-kujawa committed Feb 9, 2023
1 parent d226b6f commit 1f63adf
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 300 deletions.
46 changes: 41 additions & 5 deletions pkg/exporter/syslogexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,72 @@
package syslogexporter

import (
"errors"
"strings"

"github.com/THREATINT/go-net"

"go.opentelemetry.io/collector/exporter/exporterhelper"
)

var (
unsupportedPort = errors.New("Unsupported Port: Port is required, must be in the range 1-65535")
invalidFQDN = errors.New("Invalid FQDN: Endpoint is required, must be a valid FQDN")
unsupportedProtocol = errors.New("Unsupported protocol: Protocol is required, only tcp/udp supported")
unsupportedFormat = errors.New("Unsupported format: Only rfc5424 and rfc3164 supported")
)

// Config defines configuration for Syslog exporter.
type Config struct {
// Syslog server address
Endpoint string `mapstructure:"endpoint" validate:"required,fqdn"`
Endpoint string `mapstructure:"endpoint"`
// Syslog server port
Port int `mapstructure:"port" validate:"required,port"`
Port int `mapstructure:"port"`
// Protocol for syslog communication
// options: tcp, udp
Protocol string `mapstructure:"protocol" validate:"required,protocol type"`
Protocol string `mapstructure:"protocol"`
// CA certificate of syslog server
CACertificate string `mapstructure:"ca_certificate"`
// Format of syslog messages
Format string `mapstructure:"format" validate:"required,format"`
Format string `mapstructure:"format"`
// Additional structured data added to structured data in RFC5424
AdditionalStructuredData []string `mapstructure:"additional_structured_data"`

exporterhelper.QueueSettings `mapstructure:"sending_queue"`
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
}

// Validate the configuration for errors. This is required by component.Config.
func (cfg *Config) Validate() error {
if cfg.Port < 1 || cfg.Port > 65525 {
return unsupportedPort
}

if !net.IsFQDN(cfg.Endpoint) || cfg.Endpoint == "" {
return invalidFQDN
}

if strings.ToLower(cfg.Protocol) != "tcp" && strings.ToLower(cfg.Protocol) != "udp" {
return unsupportedProtocol
}

switch cfg.Format {
case formatRFC3164Str:
case formatRFC5424Str:
default:
return unsupportedFormat
}

return nil
}

const (
// Syslog Protocol
DefaultProtocol = "tcp"
// Syslog Port
DefaultPort = 514
// Syslog Endpoint
DefaultEndpoint = ""
DefaultEndpoint = "host.domain.com"
// Syslog format
DefaultFormat = "rfc5424"
)
69 changes: 69 additions & 0 deletions pkg/exporter/syslogexporter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package syslogexporter

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidate(t *testing.T) {

tests := []struct {
name string
cfg *Config
err string
}{
{
name: "invalid Port",
cfg: &Config{
Port: 515444,
Endpoint: "host.domain.com",
Format: "rfc5424",
Protocol: "udp",
},
err: "Unsupported Port: Port is required, must be in the range 1-65535",
},

{
name: "invalid Endpoint",
cfg: &Config{
Port: 514,
Endpoint: "",
Format: "rfc5424",
Protocol: "udp",
},
err: "Invalid FQDN: Endpoint is required, must be a valid FQDN",
},

{
name: "unsupported Protocol",
cfg: &Config{
Port: 514,
Endpoint: "host.domain.com",
Format: "rfc5424",
Protocol: "ftp",
},
err: "Unsupported protocol: Protocol is required, only tcp/udp supported",
},
{
name: "Unsupported Format",
cfg: &Config{
Port: 514,
Endpoint: "host.domain.com",
Protocol: "udp",
Format: "rfc",
},
err: "Unsupported format: Only rfc5424 and rfc3164 supported",
},
}
for _, testInstance := range tests {
t.Run(testInstance.name, func(t *testing.T) {
err := testInstance.cfg.Validate()
if testInstance.err != "" {
assert.EqualError(t, err, testInstance.err)
} else {
assert.NoError(t, err)
}
})
}
}
66 changes: 0 additions & 66 deletions pkg/exporter/syslogexporter/config_validation.go

This file was deleted.

102 changes: 0 additions & 102 deletions pkg/exporter/syslogexporter/config_validation_test.go

This file was deleted.

Loading

0 comments on commit 1f63adf

Please sign in to comment.