Skip to content

Commit

Permalink
Supports syslog via tls
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed May 14, 2024
1 parent 0f95d50 commit 6718757
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func NewMetronEmitter(logger lager.Logger, conf *config.Config) (Emitter, error)
conf.LoggregatorConfig.TLS.CertFile,
conf.LoggregatorConfig.TLS.KeyFile,
)

if err != nil {
logger.Error("could-not-create-TLS-config", err, lager.Data{"config": conf})
return &MetronEmitter{}, err
Expand Down
36 changes: 29 additions & 7 deletions src/autoscaler/metricsforwarder/forwarder/syslog_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,46 @@ func (c *Counter) Set(delta float64) {
}

func NewSyslogEmitter(logger lager.Logger, conf *config.Config) (Emitter, error) {
var writer egress.WriteCloser
var protocol string

tlsConfig, _ := conf.SyslogConfig.TLS.CreateClientConfig()

netConf := syslog.NetworkTimeoutConfig{
WriteTimeout: time.Second,
DialTimeout: 100 * time.Millisecond,
}

url, _ := url.Parse(fmt.Sprintf("syslog-tls://%s", conf.SyslogConfig.ServerAddress))
if conf.SyslogConfig.TLS.CACertFile != "" {
protocol = "syslog-tls"
} else {
protocol = "syslog"
}

url, _ := url.Parse(fmt.Sprintf("%s://%s", protocol, conf.SyslogConfig.ServerAddress))

binding := &syslog.URLBinding{
URL: url,
Hostname: "test-hostname",
}

writer := syslog.NewTCPWriter(
binding,
netConf,
&Counter{},
syslog.NewConverter(),
)
switch binding.URL.Scheme {
case "syslog":
writer = syslog.NewTCPWriter(
binding,
netConf,
&Counter{},
syslog.NewConverter(),
)
case "syslog-tls":
writer = syslog.NewTLSWriter(
binding,
netConf,
tlsConfig,
&Counter{},
syslog.NewConverter(),
)
}

return &SyslogEmitter{
Writer: writer,
Expand Down
98 changes: 48 additions & 50 deletions src/autoscaler/metricsforwarder/forwarder/syslog_emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,92 @@ import (

var _ = Describe("SyslogEmitter", func() {
var (
listener net.Listener
err error
conf *config.Config
syslogConfig *config.SyslogConfig
emitter forwarder.Emitter
listener net.Listener
err error
port int
conf *config.Config
tlsCerts models.TLSCerts
emitter forwarder.Emitter
)

BeforeEach(func() {
port := 10000 + GinkgoParallelProcess()
port = 10000 + GinkgoParallelProcess()
listener, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
Expect(err).ToNot(HaveOccurred())

url, err := url.Parse(fmt.Sprintf("syslog://%s", listener.Addr()))
tlsCerts = models.TLSCerts{}
Expect(err).ToNot(HaveOccurred())

syslogConfig = &config.SyslogConfig{
ServerAddress: url.Host,
Port: port,
}
})

JustBeforeEach(func() {
conf = &config.Config{SyslogConfig: *syslogConfig}
url, err := url.Parse(fmt.Sprintf("syslog://%s", listener.Addr()))
conf = &config.Config{
SyslogConfig: config.SyslogConfig{
ServerAddress: url.Host,
Port: port,
TLS: tlsCerts,
},
}

//TODO: set server with tls
//filepath.Join(testCertDir, "metron.crt"),
//filepath.Join(testCertDir, "metron.key"),
//filepath.Join(testCertDir, "loggregator-ca.crt"),
//
logger := lager.NewLogger("metricsforwarder-test")
emitter, err = forwarder.NewSyslogEmitter(logger, conf)
Expect(err).ToNot(HaveOccurred())

})

AfterEach(func() {
listener.Close()
})

Describe("EmitMetric", func() {
Describe("NewSyslogEmitter", func() {
Context("When tls config is provided", func() {
BeforeEach(func() {
testCertDir := "../../../../test-certs"
//on server
//filepath.Join(testCertDir, "metron.crt"),
//filepath.Join(testCertDir, "metron.key"),
//filepath.Join(testCertDir, "loggregator-ca.crt"),
//
conf.SyslogConfig.TLS = models.TLSCerts{
tlsCerts = models.TLSCerts{
KeyFile: filepath.Join(testCertDir, "cf-app.key"),
CertFile: filepath.Join(testCertDir, "cf-app.crt"),
CACertFile: filepath.Join(testCertDir, "log-cache-syslog-server-ca.crt"),
}
})

XIt("should send message to syslog server", func() {
emitter, err = forwarder.NewSyslogEmitter(lager.NewLogger("metricsforwarder-test"), conf)
Expect(err).ToNot(HaveOccurred())

It("Writer should be TLS", func() {
// cast emitter to syslogEmitter to access writer
Expect(emitter.(*forwarder.SyslogEmitter).Writer).To(BeAssignableToTypeOf(syslog.TLSWriter{}))

metric := &models.CustomMetric{Name: "queuelength", Value: 12, Unit: "bytes", InstanceIndex: 123, AppGUID: "dummy-guid"}
emitter.EmitMetric(metric)

conn, err := listener.Accept()
Expect(err).ToNot(HaveOccurred())
buf := bufio.NewReader(conn)

actual, err := buf.ReadString('\n')
Expect(err).ToNot(HaveOccurred())

expected := fmt.Sprintf(`130 <14>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+\d{2}:\d{2} test-hostname %s \[%d\] - \[gauge@47450 name="%s" value="%.0f" unit="%s"\]`, metric.AppGUID, metric.InstanceIndex, metric.Name, metric.Value, metric.Unit)
Expect(actual).To(MatchRegexp(expected))
Expect(emitter.(*forwarder.SyslogEmitter).Writer).To(BeAssignableToTypeOf(&syslog.TLSWriter{}))
})

})

Context("When tls config is not provided", func() {
It("should send message to syslog server", func() {
metric := &models.CustomMetric{Name: "queuelength", Value: 12, Unit: "bytes", InstanceIndex: 123, AppGUID: "dummy-guid"}
JustBeforeEach(func() {
conf.SyslogConfig.TLS = models.TLSCerts{}
})

It("Writer should be TCP", func() {
Expect(emitter.(*forwarder.SyslogEmitter).Writer).To(BeAssignableToTypeOf(&syslog.TCPWriter{}))
emitter.EmitMetric(metric)
})
})
})

conn, err := listener.Accept()
Expect(err).ToNot(HaveOccurred())
buf := bufio.NewReader(conn)
Describe("EmitMetric", func() {

actual, err := buf.ReadString('\n')
Expect(err).ToNot(HaveOccurred())
It("should send message to syslog server", func() {
metric := &models.CustomMetric{Name: "queuelength", Value: 12, Unit: "bytes", InstanceIndex: 123, AppGUID: "dummy-guid"}

expected := fmt.Sprintf(`130 <14>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+\d{2}:\d{2} test-hostname %s \[%d\] - \[gauge@47450 name="%s" value="%.0f" unit="%s"\]`, metric.AppGUID, metric.InstanceIndex, metric.Name, metric.Value, metric.Unit)
Expect(actual).To(MatchRegexp(expected))
})
emitter.EmitMetric(metric)

conn, err := listener.Accept()
Expect(err).ToNot(HaveOccurred())
buf := bufio.NewReader(conn)

actual, err := buf.ReadString('\n')
Expect(err).ToNot(HaveOccurred())

expected := fmt.Sprintf(`130 <14>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+\d{2}:\d{2} test-hostname %s \[%d\] - \[gauge@47450 name="%s" value="%.0f" unit="%s"\]`, metric.AppGUID, metric.InstanceIndex, metric.Name, metric.Value, metric.Unit)
Expect(actual).To(MatchRegexp(expected))
})
})
})

0 comments on commit 6718757

Please sign in to comment.