From 3a438a75b898b4324b7f1830de694f97da96c246 Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Wed, 11 Dec 2024 15:56:01 -0500 Subject: [PATCH] clean up validation, try not to use localhost for ci tests --- .../otelcol/otelcol.receiver.syslog.md | 4 +- .../otelcol/receiver/syslog/syslog.go | 41 ++++++++++--------- .../otelcol/receiver/syslog/syslog_test.go | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/docs/sources/reference/components/otelcol/otelcol.receiver.syslog.md b/docs/sources/reference/components/otelcol/otelcol.receiver.syslog.md index 6420a6dbee..a0b888f6e7 100644 --- a/docs/sources/reference/components/otelcol/otelcol.receiver.syslog.md +++ b/docs/sources/reference/components/otelcol/otelcol.receiver.syslog.md @@ -43,7 +43,7 @@ The following arguments are supported: | `enable_octet_counting` | `bool` | Whether to enable RFC6587 octet counting. | `false` | no | | `max_octets` | `int` | The maximum octets for messages when octet counting is enabled. | `8192` | no | | `allow_skip_pri_header` | `bool` | Allow parsing records without a priority header. | `false` | no | -| `non_transparent_framing_trailer` | `string` | The framing trailer when using RFC6587 Non-Transparent-Framing. | `false` | no | +| `non_transparent_framing_trailer` | `string` | The framing trailer when using RFC6587 Non-Transparent-Framing. | `nil` | no | The `protocol` argument specifies the syslog format supported by the receiver. `protocol` must be one of `rfc5424`, `rfc3164` @@ -53,6 +53,8 @@ See [this wikipedia entry][tz-wiki] for a non-comprehensive list. The `non_transparent_framing_trailer` argument must be one of `LF`, `NUL`. +The `non_transparent_framing_trailer` and `enable_octet_counting` arguments cannot be used with a UDP syslog server. + [tz-wiki]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones ## Blocks diff --git a/internal/component/otelcol/receiver/syslog/syslog.go b/internal/component/otelcol/receiver/syslog/syslog.go index 80d48c7a11..297ee067af 100644 --- a/internal/component/otelcol/receiver/syslog/syslog.go +++ b/internal/component/otelcol/receiver/syslog/syslog.go @@ -3,7 +3,7 @@ package syslog import ( "fmt" - net_url "net/url" + "net" "github.com/alecthomas/units" "github.com/grafana/alloy/internal/component" @@ -48,7 +48,7 @@ type Arguments struct { EnableOctetCounting bool `alloy:"enable_octet_counting,attr,optional"` MaxOctets int `alloy:"max_octets,attr,optional"` AllowSkipPriHeader bool `alloy:"allow_skip_pri_header,attr,optional"` - NonTransparentFramingTrailer FramingTrailer `alloy:"non_transparent_framing_trailer,attr,optional"` + NonTransparentFramingTrailer *FramingTrailer `alloy:"non_transparent_framing_trailer,attr,optional"` ConsumerRetry otelcol.ConsumerRetryArguments `alloy:"retry_on_failure,block,optional"` TCP *TCP `alloy:"tcp,block,optional"` @@ -168,10 +168,9 @@ var _ receiver.Arguments = Arguments{} // SetToDefault implements syntax.Defaulter. func (args *Arguments) SetToDefault() { *args = Arguments{ - Location: "UTC", - Protocol: config.SyslogFormatRFC5424, - Output: &otelcol.ConsumerArguments{}, - NonTransparentFramingTrailer: LFTrailer, + Location: "UTC", + Protocol: config.SyslogFormatRFC5424, + Output: &otelcol.ConsumerArguments{}, } args.DebugMetrics.SetToDefault() args.ConsumerRetry.SetToDefault() @@ -179,16 +178,19 @@ func (args *Arguments) SetToDefault() { // Convert implements receiver.Arguments. func (args Arguments) Convert() (otelcomponent.Config, error) { - trailer := string(args.NonTransparentFramingTrailer) c := stanzainputsyslog.NewConfig() c.BaseConfig = stanzaparsersyslog.BaseConfig{ - Protocol: string(args.Protocol), - Location: args.Location, - EnableOctetCounting: args.EnableOctetCounting, - MaxOctets: args.MaxOctets, - AllowSkipPriHeader: args.AllowSkipPriHeader, - NonTransparentFramingTrailer: &trailer, + Protocol: string(args.Protocol), + Location: args.Location, + EnableOctetCounting: args.EnableOctetCounting, + MaxOctets: args.MaxOctets, + AllowSkipPriHeader: args.AllowSkipPriHeader, + } + + if args.NonTransparentFramingTrailer != nil { + s := string(*args.NonTransparentFramingTrailer) + c.BaseConfig.NonTransparentFramingTrailer = &s } if args.TCP != nil { @@ -274,12 +276,12 @@ func (args *Arguments) Validate() error { } if args.TCP != nil { - if err := validateURL(args.TCP.ListenAddress, "tcp.listen_address"); err != nil { + if err := validateListenAddress(args.TCP.ListenAddress, "tcp.listen_address"); err != nil { errs = multierror.Append(errs, err) } - if args.NonTransparentFramingTrailer != LFTrailer && args.NonTransparentFramingTrailer != NULTrailer { - errs = multierror.Append(errs, fmt.Errorf("invalid non_transparent_framing_trailer, must be one of 'LF', 'NUL': %s", args.NonTransparentFramingTrailer)) + if args.NonTransparentFramingTrailer != nil && *args.NonTransparentFramingTrailer != LFTrailer && *args.NonTransparentFramingTrailer != NULTrailer { + errs = multierror.Append(errs, fmt.Errorf("invalid non_transparent_framing_trailer, must be one of 'LF', 'NUL': %s", *args.NonTransparentFramingTrailer)) } _, err := decode.LookupEncoding(args.TCP.Encoding) @@ -293,7 +295,7 @@ func (args *Arguments) Validate() error { } if args.UDP != nil { - if err := validateURL(args.UDP.ListenAddress, "udp.listen_address"); err != nil { + if err := validateListenAddress(args.UDP.ListenAddress, "udp.listen_address"); err != nil { errs = multierror.Append(errs, err) } @@ -306,11 +308,12 @@ func (args *Arguments) Validate() error { return errs } -func validateURL(url string, urlName string) error { +func validateListenAddress(url string, urlName string) error { if url == "" { return fmt.Errorf("%s cannot be empty", urlName) } - if _, err := net_url.Parse(url); err != nil { + + if _, _, err := net.SplitHostPort(url); err != nil { return fmt.Errorf("invalid %s: %w", urlName, err) } return nil diff --git a/internal/component/otelcol/receiver/syslog/syslog_test.go b/internal/component/otelcol/receiver/syslog/syslog_test.go index 110e04bf29..ad17407043 100644 --- a/internal/component/otelcol/receiver/syslog/syslog_test.go +++ b/internal/component/otelcol/receiver/syslog/syslog_test.go @@ -118,7 +118,7 @@ func getFreeAddr(t *testing.T) string { portNumber, err := freeport.GetFreePort() require.NoError(t, err) - return fmt.Sprintf("localhost:%d", portNumber) + return fmt.Sprintf("127.0.0.1:%d", portNumber) } func TestUnmarshal(t *testing.T) {