bump RackSec/srslog a4725f04ec91af1a91b380da679d6e0c2f061e59

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-04-06 01:34:05 +02:00
parent dfa863db45
commit d72d3adf1c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
6 changed files with 102 additions and 15 deletions

View file

@ -22,7 +22,7 @@ golang.org/x/text f21a4dfb5e38f5895301dc265a8def02365cc3d0 # v0.3.0
gotest.tools 1083505acf35a0bd8a696b26837e1fb3187a7a83 # v2.3.0
github.com/google/go-cmp v0.2.0
github.com/RackSec/srslog 456df3a81436d29ba874f3590eeeee25d666f8a5
github.com/RackSec/srslog a4725f04ec91af1a91b380da679d6e0c2f061e59
github.com/imdario/mergo 7c29201646fa3de8506f701213473dd407f19646 # v0.3.7
golang.org/x/sync e225da77a7e68af35c70ccbf71af2b83e6acac3c

View file

@ -90,6 +90,22 @@ w.Debug("this is debug")
w.Write([]byte("these are some bytes"))
```
If you need further control over connection attempts, you can use the DialWithCustomDialer
function. To continue with the DialWithTLSConfig example:
```
netDialer := &net.Dialer{Timeout: time.Second*5} // easy timeouts
realNetwork := "tcp" // real network, other vars your dail func can close over
dial := func(network, addr string) (net.Conn, error) {
// cannot use "network" here as it'll simply be "custom" which will fail
return tls.DialWithDialer(netDialer, realNetwork, addr, &config)
}
w, err := DialWithCustomDialer("custom", "192.168.0.52:514", syslog.LOG_ERR, "testtag", dial)
```
Your custom dial func can set timeouts, proxy connections, and do whatever else it needs before returning a net.Conn.
# Generating TLS Certificates
We've provided a script that you can use to generate a self-signed keypair:

View file

@ -37,6 +37,7 @@ func (w *Writer) getDialer() dialerFunctionWrapper {
dialers := map[string]dialerFunctionWrapper{
"": dialerFunctionWrapper{"unixDialer", w.unixDialer},
"tcp+tls": dialerFunctionWrapper{"tlsDialer", w.tlsDialer},
"custom": dialerFunctionWrapper{"customDialer", w.customDialer},
}
dialer, ok := dialers[w.network]
if !ok {
@ -85,3 +86,19 @@ func (w *Writer) basicDialer() (serverConn, string, error) {
}
return sc, hostname, err
}
// customDialer uses the custom dialer when the Writer was created
// giving developers total control over how connections are made and returned.
// Note it does not check if cdialer is nil, as it should only be referenced from getDialer.
func (w *Writer) customDialer() (serverConn, string, error) {
c, err := w.customDial(w.network, w.raddr)
var sc serverConn
hostname := w.hostname
if err == nil {
sc = &netConn{conn: c}
if hostname == "" {
hostname = c.LocalAddr().String()
}
}
return sc, hostname, err
}

View file

@ -6,6 +6,8 @@ import (
"time"
)
const appNameMaxLength = 48 // limit to 48 chars as per RFC5424
// Formatter is a type of function that takes the consituent parts of a
// syslog message and returns a formatted string. A different Formatter is
// defined for each different syslog protocol we support.
@ -37,12 +39,20 @@ func RFC3164Formatter(p Priority, hostname, tag, content string) string {
return msg
}
// if string's length is greater than max, then use the last part
func truncateStartStr(s string, max int) string {
if (len(s) > max) {
return s[len(s) - max:]
}
return s
}
// RFC5424Formatter provides an RFC 5424 compliant message.
func RFC5424Formatter(p Priority, hostname, tag, content string) string {
timestamp := time.Now().Format(time.RFC3339)
pid := os.Getpid()
appName := os.Args[0]
msg := fmt.Sprintf("<%d>%d %s %s %s %d %s %s",
appName := truncateStartStr(os.Args[0], appNameMaxLength)
msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
p, 1, timestamp, hostname, appName, pid, tag, content)
return msg
}

View file

@ -3,8 +3,10 @@ package srslog
import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"log"
"net"
"os"
)
@ -15,6 +17,10 @@ type serverConn interface {
close() error
}
// DialFunc is the function signature to be used for a custom dialer callback
// with DialWithCustomDialer
type DialFunc func(string, string) (net.Conn, error)
// New establishes a new connection to the system log daemon. Each
// write to the returned Writer sends a log message with the given
// priority and prefix.
@ -31,6 +37,22 @@ func Dial(network, raddr string, priority Priority, tag string) (*Writer, error)
return DialWithTLSConfig(network, raddr, priority, tag, nil)
}
// ErrNilDialFunc is returned from DialWithCustomDialer when a nil DialFunc is passed,
// avoiding a nil pointer deference panic.
var ErrNilDialFunc = errors.New("srslog: nil DialFunc passed to DialWithCustomDialer")
// DialWithCustomDialer establishes a connection by calling customDial.
// Each write to the returned Writer sends a log message with the given facility, severity and tag.
// Network must be "custom" in order for this package to use customDial.
// While network and raddr will be passed to customDial, it is allowed for customDial to ignore them.
// If customDial is nil, this function returns ErrNilDialFunc.
func DialWithCustomDialer(network, raddr string, priority Priority, tag string, customDial DialFunc) (*Writer, error) {
if customDial == nil {
return nil, ErrNilDialFunc
}
return dialAllParameters(network, raddr, priority, tag, nil, customDial)
}
// DialWithTLSCertPath establishes a secure connection to a log daemon by connecting to
// address raddr on the specified network. It uses certPath to load TLS certificates and configure
// the secure connection.
@ -59,6 +81,11 @@ func DialWithTLSCert(network, raddr string, priority Priority, tag string, serve
// DialWithTLSConfig establishes a secure connection to a log daemon by connecting to
// address raddr on the specified network. It uses tlsConfig to configure the secure connection.
func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config) (*Writer, error) {
return dialAllParameters(network, raddr, priority, tag, tlsConfig, nil)
}
// implementation of the various functions above
func dialAllParameters(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config, customDial DialFunc) (*Writer, error) {
if err := validatePriority(priority); err != nil {
return nil, err
}
@ -69,12 +96,13 @@ func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tls
hostname, _ := os.Hostname()
w := &Writer{
priority: priority,
tag: tag,
hostname: hostname,
network: network,
raddr: raddr,
tlsConfig: tlsConfig,
priority: priority,
tag: tag,
hostname: hostname,
network: network,
raddr: raddr,
tlsConfig: tlsConfig,
customDial: customDial,
}
_, err := w.connect()

View file

@ -17,6 +17,9 @@ type Writer struct {
framer Framer
formatter Formatter
//non-nil if custom dialer set, used in getDialer
customDial DialFunc
mu sync.RWMutex // guards conn
conn serverConn
}
@ -71,15 +74,20 @@ func (w *Writer) SetFramer(f Framer) {
w.framer = f
}
// SetHostname changes the hostname for syslog messages if needed.
func (w *Writer) SetHostname(hostname string) {
w.hostname = hostname
}
// Write sends a log message to the syslog daemon using the default priority
// passed into `srslog.New` or the `srslog.Dial*` functions.
func (w *Writer) Write(b []byte) (int, error) {
return w.writeAndRetry(w.priority, string(b))
}
// WriteWithPriority sends a log message with a custom priority
// WriteWithPriority sends a log message with a custom priority.
func (w *Writer) WriteWithPriority(p Priority, b []byte) (int, error) {
return w.writeAndRetry(p, string(b))
return w.writeAndRetryWithPriority(p, string(b))
}
// Close closes a connection to the syslog daemon.
@ -149,12 +157,20 @@ func (w *Writer) Debug(m string) (err error) {
return err
}
func (w *Writer) writeAndRetry(p Priority, s string) (int, error) {
pr := (w.priority & facilityMask) | (p & severityMask)
// writeAndRetry takes a severity and the string to write. Any facility passed to
// it as part of the severity Priority will be ignored.
func (w *Writer) writeAndRetry(severity Priority, s string) (int, error) {
pr := (w.priority & facilityMask) | (severity & severityMask)
return w.writeAndRetryWithPriority(pr, s)
}
// writeAndRetryWithPriority differs from writeAndRetry in that it allows setting
// of both the facility and the severity.
func (w *Writer) writeAndRetryWithPriority(p Priority, s string) (int, error) {
conn := w.getConn()
if conn != nil {
if n, err := w.write(conn, pr, s); err == nil {
if n, err := w.write(conn, p, s); err == nil {
return n, err
}
}
@ -163,7 +179,7 @@ func (w *Writer) writeAndRetry(p Priority, s string) (int, error) {
if conn, err = w.connect(); err != nil {
return 0, err
}
return w.write(conn, pr, s)
return w.write(conn, p, s)
}
// write generates and writes a syslog formatted string. It formats the