Skip to content

Commit f825274

Browse files
authored
[exporter/syslog]: fix setting network connection (#31202)
**Description:** fix setting network connection, do not load TLS configuration for UDP **Link to tracking Issue:** #31130 **Testing:** unit test, manual tests with syslog server **Documentation:** added information that TLS config is applied only when TCP is used Signed-off-by: Katarzyna Kujawa <[email protected]>
1 parent 357e717 commit f825274

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
lines changed

.chloggen/fix-31130.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: syslogexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: fix setting network connection, do not load TLS configuration for UDP
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31130]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/syslogexporter/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This means that syslog messages received via the Syslog receiver and exported vi
2828
- `rfc5424` - Expects the syslog messages to be rfc5424 compliant
2929
- `rfc3164` - Expects the syslog messages to be rfc3164 compliant
3030
- `enable_octet_counting` (default = `false`) - Whether or not to enable rfc6587 octet counting
31-
- `tls` - configuration for TLS/mTLS
31+
- `tls` - configuration for TLS/mTLS (applied only when `network` is set to `tcp`)
3232
- `insecure` (default = `false`) whether to enable client transport security, by default, TLS is enabled.
3333
- `cert_file` - Path to the TLS cert to use for TLS required connections. Should only be used if `insecure` is set to `false`.
3434
- `key_file` - Path to the TLS key to use for TLS required connections. Should only be used if `insecure` is set to `false`.

exporter/syslogexporter/exporter.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ type syslogexporter struct {
2525
}
2626

2727
func initExporter(cfg *Config, createSettings exporter.CreateSettings) (*syslogexporter, error) {
28-
tlsConfig, err := cfg.TLSSetting.LoadTLSConfig()
29-
if err != nil {
30-
return nil, err
31-
}
32-
3328
cfg.Network = strings.ToLower(cfg.Network)
3429

30+
var loadedTLSConfig *tls.Config
31+
if cfg.Network == "tcp" {
32+
var err error
33+
loadedTLSConfig, err = cfg.TLSSetting.LoadTLSConfig()
34+
if err != nil {
35+
return nil, err
36+
}
37+
}
38+
3539
s := &syslogexporter{
3640
config: cfg,
3741
logger: createSettings.Logger,
38-
tlsConfig: tlsConfig,
42+
tlsConfig: loadedTLSConfig,
3943
formatter: createFormatter(cfg.Protocol, cfg.EnableOctetCounting),
4044
}
4145

exporter/syslogexporter/exporter_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package syslogexporter
55

66
import (
77
"context"
8+
"crypto/tls"
89
"errors"
910
"io"
1011
"net"
@@ -15,6 +16,7 @@ import (
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
1718
"go.opentelemetry.io/collector/component"
19+
"go.opentelemetry.io/collector/config/configtls"
1820
"go.opentelemetry.io/collector/consumer/consumererror"
1921
"go.opentelemetry.io/collector/exporter"
2022
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -183,3 +185,55 @@ func TestSyslogExportFail(t *testing.T) {
183185
assert.ErrorContains(t, consumerErr, "dial tcp 127.0.0.1:112: connect")
184186
assert.Equal(t, droppedLog, originalForm)
185187
}
188+
189+
func TestTLSConfig(t *testing.T) {
190+
191+
tests := []struct {
192+
name string
193+
network string
194+
tlsSettings configtls.TLSClientSetting
195+
tlsConfig *tls.Config
196+
}{
197+
{name: "TCP with TLS configuration",
198+
network: "tcp",
199+
tlsSettings: configtls.TLSClientSetting{},
200+
tlsConfig: &tls.Config{},
201+
},
202+
{name: "TCP insecure",
203+
network: "tcp",
204+
tlsSettings: configtls.TLSClientSetting{Insecure: true},
205+
tlsConfig: nil,
206+
},
207+
{name: "UDP with TLS configuration",
208+
network: "udp",
209+
tlsSettings: configtls.TLSClientSetting{},
210+
tlsConfig: nil,
211+
},
212+
{name: "UDP insecure",
213+
network: "udp",
214+
tlsSettings: configtls.TLSClientSetting{Insecure: true},
215+
tlsConfig: nil,
216+
},
217+
}
218+
219+
for _, testInstance := range tests {
220+
t.Run(testInstance.name, func(t *testing.T) {
221+
222+
exporter, err := initExporter(
223+
&Config{Endpoint: "test.com",
224+
Network: testInstance.network,
225+
Port: 514,
226+
Protocol: "rfc5424",
227+
TLSSetting: testInstance.tlsSettings},
228+
createExporterCreateSettings())
229+
230+
assert.NoError(t, err)
231+
if testInstance.tlsConfig != nil {
232+
assert.NotNil(t, exporter.tlsConfig)
233+
} else {
234+
assert.Nil(t, exporter.tlsConfig)
235+
}
236+
237+
})
238+
}
239+
}

exporter/syslogexporter/sender.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func (s *sender) dial() error {
7878
s.conn = nil
7979
}
8080
var err error
81-
if s.tlsConfig != nil {
82-
s.conn, err = tls.Dial("tcp", s.addr, s.tlsConfig)
81+
if s.tlsConfig != nil && s.network == "tcp" {
82+
s.conn, err = tls.Dial(s.network, s.addr, s.tlsConfig)
8383
} else {
8484
s.conn, err = net.Dial(s.network, s.addr)
8585
}

0 commit comments

Comments
 (0)