Skip to content

Commit

Permalink
feat(syslogreceiver): validate protocol name (open-telemetry#27581)
Browse files Browse the repository at this point in the history
**Description:**

Add validation of protocol name and be case insensitive

**Link to tracking Issue:**

**Testing:**

Unit Tests

**Documentation:**

N/A

---------

Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
sumo-drosiek authored and JaredTan95 committed Oct 18, 2023
1 parent 9688268 commit 407900a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
27 changes: 27 additions & 0 deletions .chloggen/drosiek-syslog-improvement.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: syslogreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: validate protocol name

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [27581]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
15 changes: 10 additions & 5 deletions pkg/stanza/operator/parser/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

sl "github.com/influxdata/go-syslog/v3"
Expand Down Expand Up @@ -77,17 +78,21 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
return nil, err
}

proto := strings.ToLower(c.Protocol)

switch {
case c.Protocol == "":
case proto == "":
return nil, fmt.Errorf("missing field 'protocol'")
case c.Protocol != RFC5424 && (c.NonTransparentFramingTrailer != nil || c.EnableOctetCounting):
case proto != RFC5424 && (c.NonTransparentFramingTrailer != nil || c.EnableOctetCounting):
return nil, errors.New("octet_counting and non_transparent_framing are only compatible with protocol rfc5424")
case c.Protocol == RFC5424 && (c.NonTransparentFramingTrailer != nil && c.EnableOctetCounting):
case proto == RFC5424 && (c.NonTransparentFramingTrailer != nil && c.EnableOctetCounting):
return nil, errors.New("only one of octet_counting or non_transparent_framing can be enabled")
case c.Protocol == RFC5424 && c.NonTransparentFramingTrailer != nil:
case proto == RFC5424 && c.NonTransparentFramingTrailer != nil:
if *c.NonTransparentFramingTrailer != NULTrailer && *c.NonTransparentFramingTrailer != LFTrailer {
return nil, fmt.Errorf("invalid non_transparent_framing_trailer '%s'. Must be either 'LF' or 'NUL'", *c.NonTransparentFramingTrailer)
}
case proto != RFC5424 && proto != RFC3164:
return nil, fmt.Errorf("unsupported protocol version: %s", proto)
}

if c.Location == "" {
Expand All @@ -101,7 +106,7 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {

return &Parser{
ParserOperator: parserOperator,
protocol: c.Protocol,
protocol: proto,
location: location,
enableOctetCounting: c.EnableOctetCounting,
nonTransparentFramingTrailer: c.NonTransparentFramingTrailer,
Expand Down
16 changes: 16 additions & 0 deletions pkg/stanza/operator/parser/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ func TestSyslogParseRFC5424_SDNameTooLong(t *testing.T) {
require.FailNow(t, "Timed out waiting for entry to be processed")
}
}

func TestSyslogProtocolConfig(t *testing.T) {
for _, proto := range []string{"RFC5424", "rfc5424", "RFC3164", "rfc3164"} {
cfg := basicConfig()
cfg.Protocol = proto
_, err := cfg.Build(testutil.Logger(t))
require.NoError(t, err)
}

for _, proto := range []string{"RFC5424a", "rfc5424b", "RFC3164c", "rfc3164d"} {
cfg := basicConfig()
cfg.Protocol = proto
_, err := cfg.Build(testutil.Logger(t))
require.Error(t, err)
}
}

0 comments on commit 407900a

Please sign in to comment.