TLS support for TCP logger

This commit is contained in:
Christian Schwarz 2017-09-24 14:34:50 +02:00
parent 0cb5ffb48b
commit c1a5b04065
9 changed files with 171 additions and 2 deletions

View File

@ -1,9 +1,12 @@
package cmd
import (
"crypto/tls"
"crypto/x509"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/zrepl/zrepl/logger"
"io/ioutil"
"os"
"time"
)
@ -34,6 +37,11 @@ func parseLogging(i interface{}) (c *LoggingConfig, err error) {
Net string
Address string
RetryInterval string `mapstructure:"retry_interval"`
TLS *struct {
CA string
Cert string
Key string
}
}
Syslog struct {
Enable bool
@ -91,6 +99,38 @@ func parseLogging(i interface{}) (c *LoggingConfig, err error) {
out.Net, out.Address = asMap.TCP.Net, asMap.TCP.Address
if asMap.TCP.TLS != nil {
cert, err := tls.LoadX509KeyPair(asMap.TCP.TLS.Cert, asMap.TCP.TLS.Key)
if err != nil {
return nil, errors.Wrap(err, "cannot load client cert")
}
var rootCAs *x509.CertPool
if asMap.TCP.TLS.CA == "" {
if rootCAs, err = x509.SystemCertPool(); err != nil {
return nil, errors.Wrap(err, "cannot open system cert pool")
}
} else {
rootCAs = x509.NewCertPool()
rootCAPEM, err := ioutil.ReadFile(asMap.TCP.TLS.CA)
if err != nil {
return nil, errors.Wrap(err, "cannot load CA cert")
}
if !rootCAs.AppendCertsFromPEM(rootCAPEM) {
return nil, errors.New("cannot parse CA cert")
}
}
if err != nil && asMap.TCP.TLS.CA == "" {
return nil, errors.Wrap(err, "cannot load root ca pool")
}
out.TLS = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: rootCAs,
}
}
c.Outlets.Add(out, lvl)
}

View File

@ -22,10 +22,14 @@ func TestSampleConfigsAreParsedWithoutErrors(t *testing.T) {
for _, p := range paths {
_, err := ParseConfig(p)
c, err := ParseConfig(p)
if err != nil {
t.Errorf("error parsing %s:\n%+v", p, err)
}
t.Logf("file: %s", p)
t.Log(pretty.Sprint(c))
}
}

View File

@ -2,6 +2,7 @@ package cmd
import (
"context"
"crypto/tls"
"github.com/pkg/errors"
"github.com/zrepl/zrepl/logger"
"io"
@ -29,6 +30,7 @@ type TCPOutlet struct {
Formatter EntryFormatter
Net, Address string
Dialer net.Dialer
TLS *tls.Config
RetryInterval time.Duration
conn net.Conn
retry time.Time
@ -46,8 +48,14 @@ func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error {
return nil // this is not an error toward the logger
//return errors.New("TCP hook reconnect prohibited by retry interval")
}
h.conn, err = h.Dialer.DialContext(ctx, h.Net, h.Address)
if h.TLS != nil {
h.conn, err = tls.DialWithDialer(&h.Dialer, h.Net, h.Address, h.TLS)
} else {
h.conn, err = h.Dialer.DialContext(ctx, h.Net, h.Address)
}
if err != nil {
h.conn = nil
h.retry = time.Now()
return errors.Wrap(err, "cannot dial")
}
@ -55,6 +63,7 @@ func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error {
_, err = h.conn.Write(b)
if err != nil {
return errors.Wrap(err, "cannot write")
h.conn.Close()
h.conn = nil
return err

View File

@ -9,6 +9,10 @@ global:
net: tcp
address: 127.0.0.1:8080
retry_interval: 1s
tls: # if not specified, use plain TCP
ca: sampleconf/random/logging/logserver.crt
cert: sampleconf/random/logging/client.crt
key: sampleconf/random/logging/client.key
syslog:
enable: true
format: logfmt

View File

@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDIzCCAgsCAQEwDQYJKoZIhvcNAQELBQAwWTELMAkGA1UEBhMCQVUxEzARBgNV
BAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0
ZDESMBAGA1UEAwwJbG9nc2VydmVyMB4XDTE3MDkyNDEyMzAzNloXDTE3MTAyNDEy
MzAzNlowVjELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNV
BAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGY2xpZW50MIIB
IjAN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