Skip to content

Commit

Permalink
clean up validation, try not to use localhost for ci tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dehaansa committed Dec 11, 2024
1 parent f79a9ac commit 3a438a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand Down
41 changes: 22 additions & 19 deletions internal/component/otelcol/receiver/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package syslog

import (
"fmt"
net_url "net/url"
"net"

"github.com/alecthomas/units"
"github.com/grafana/alloy/internal/component"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -168,27 +168,29 @@ 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()
}

// 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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/component/otelcol/receiver/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3a438a7

Please sign in to comment.