GithubHelp home page GithubHelp logo

racksec / srslog Goto Github PK

View Code? Open in Web Editor NEW
92.0 11.0 65.0 73 KB

Golang package that is a drop-in replacement for the standard library log/syslog, but with extra features.

License: BSD 3-Clause "New" or "Revised" License

Shell 2.21% Python 3.35% Go 94.44%

srslog's Introduction

Build Status

srslog

Go has a syslog package in the standard library, but it has the following shortcomings:

  1. It doesn't have TLS support
  2. According to bradfitz on the Go team, it is no longer being maintained.

I agree that it doesn't need to be in the standard library. So, I've followed Brad's suggestion and have made a separate project to handle syslog.

This code was taken directly from the Go project as a base to start from.

However, this does have TLS support.

Usage

Basic usage retains the same interface as the original syslog package. We only added to the interface where required to support new functionality.

Switch from the standard library:

import(
    //"log/syslog"
    syslog "github.com/RackSec/srslog"
)

You can still use it for local syslog:

w, err := syslog.Dial("", "", syslog.LOG_ERR, "testtag")

Or to unencrypted UDP:

w, err := syslog.Dial("udp", "192.168.0.50:514", syslog.LOG_ERR, "testtag")

Or to unencrypted TCP:

w, err := syslog.Dial("tcp", "192.168.0.51:514", syslog.LOG_ERR, "testtag")

But now you can also send messages via TLS-encrypted TCP:

w, err := syslog.DialWithTLSCertPath("tcp+tls", "192.168.0.52:514", syslog.LOG_ERR, "testtag", "/path/to/servercert.pem")

And if you need more control over your TLS configuration :

pool := x509.NewCertPool()
serverCert, err := ioutil.ReadFile("/path/to/servercert.pem")
if err != nil {
    return nil, err
}
pool.AppendCertsFromPEM(serverCert)
config := tls.Config{
    RootCAs: pool,
}

w, err := DialWithTLSConfig(network, raddr, priority, tag, &config)

(Note that in both TLS cases, this uses a self-signed certificate, where the remote syslog server has the keypair and the client has only the public key.)

And then to write log messages, continue like so:

if err != nil {
    log.Fatal("failed to connect to syslog:", err)
}
defer w.Close()

w.Alert("this is an alert")
w.Crit("this is critical")
w.Err("this is an error")
w.Warning("this is a warning")
w.Notice("this is a notice")
w.Info("this is info")
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:

pip install cryptography
python script/gen-certs.py

That outputs the public key and private key to standard out. Put those into .pem files. (And don't put them into any source control. The certificate in the test directory is used by the unit tests, and please do not actually use it anywhere else.)

Running Tests

Run the tests as usual:

go test

But we've also provided a test coverage script that will show you which lines of code are not covered:

script/coverage --html

That will open a new browser tab showing coverage information.

License

This project uses the New BSD License, the same as the Go project itself.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

srslog's People

Contributors

calavera avatar cpuguy83 avatar derwolfe avatar ehashman avatar fxfitz avatar iaburton avatar lk4d4 avatar lvh avatar morganxf avatar oschwald avatar reaperhulk avatar scorptec68 avatar sirsean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

srslog's Issues

Enhancement to allow adding custom dialer

My solution needs to be FIPS-140-2 compliant, which is partly done by using a Go wrap of openssl for all cryptography instead of the standard net/crypto packages.
If a method were exposed that allowed me to add an entry to the dialers map in dialer.go, then I could register a method to call the dialer of my choice.
I've done this on a local copy of srslog. Is the any interest in adding this to the public branch?

Minimal log level

Hi

Is there any way to set loggers log level ? I would like to log messages up to LOG_ERR.

RFC5424 formatter does not generate the STRUCTURED-DATA field

The RFC says the syslog format is (according to the syslog-ng doc):
HEADER STRUCTURED-DATA MSG

A sample could be

<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - BOM'su root' failed for lonvick on /dev/pts/8

that corresponds to the following format

<priority>VERSION ISOTIMESTAMP HOSTNAME APPLICATION PID MESSAGEID STRUCTURED-DATA MSG

(ref: https://www.balabit.com/documents/syslog-ng-ose-latest-guides/en/syslog-ng-ose-guide-admin/html/concepts-message-ietfsyslog.html)

The RFC documentation (from IETF) says:

STRUCTURED-DATA provides a mechanism to express information in a well
defined, easily parseable and interpretable data format.  There are
multiple usage scenarios.  For example, it may express meta-
information about the syslog message or application-specific
information such as traffic counters or IP addresses.

STRUCTURED-DATA can contain zero, one, or multiple structured data
elements, which are referred to as "SD-ELEMENT" in this document.

In case of zero structured data elements, the STRUCTURED-DATA field
MUST contain the NILVALUE.

(ref: https://tools.ietf.org/html/rfc5424#section-6.3)

In our case, the best would be to add an hyphen (that is the NILVALUE indicated by the RFC) between the HEADER and the MESSAGE.

What do you think?

Use a TCP writer to `Write()` to a syslog server running with TLS only returns no `error` but actually failed

I use

w, err := syslog.Dial("tcp", "192.168.0.51:514", syslog.LOG_ERR, "testtag")
if err != nil {
  if n, err := w.Write([]byte("these are some bytes")); err != nil {
    log.Println("Sent %d bytes to syslog server\n", n)
  }
}

to connect to a rsyslog server, and the server is set to $InputTCPServerStreamDriverMode 1, which means it accepts TLS connection only.

The running result is that I was told

Sent 21 bytes to syslog server

but actually it's not the truth. Because the rsyslog server said,

rsyslogd: gnutls returned error on handshake: An unexpected TLS packet was received. [v8.31.0 try http://www.rsyslog.com/e/2083 ]
rsyslogd: gnutls returned error on handshake: An unexpected TLS packet was received. [v8.31.0 try http://www.rsyslog.com/e/2083 ]

Consider to support RELP

Since TCP protocol only guarantee no data loss on the air, but the data in the TCP send buffer may lose. The RELP initialized by rsyslog aims to fix it. Could we consider to suppport RELP?

Support per-msg MSGID

The way I see it, a tag is specified in the Dial 'constructor' and used for both the tag and msgid fields for RFC3164 and RFC5424 msg formats respectively. This is fine for RFC3164 because the tag doesn't typically change from message to message. But for RFC5424, the msgid should likely change, based on the msg type.

RFC5424 detailing MSGID: https://tools.ietf.org/html/rfc5424#section-6.2.7

I'm looking for consensus that this should be considered a 'defect', then I can put my thinking cap on regarding solutions.

Retry with another procotol if the first one is not available

Linking from: moby/moby#21613 .

Currently users of srslog (like Docker) must explicitly specify unix:// or unixgram:// when connecting to a Unix socket. Should srslog abstract this and simply retry connecting with another protocol if the first one fails? This is also the behavior of glibc (see comments in the issue above).

RFC3164 format not correctly being generated

I noticed that the formatter.go does not generate the Log according to RFC3164 specifications. There is a space between the priority and the timestamp whereas in the RFC the format specified says that there should be no space. Please find below an example taken from the RFC:
Example 1
<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8

The reason I pointed this out is because docker syslog uses this package for formatting and this is causing problems when I try to send the logs to a remote syslog (and trying to process them - gives me a parse error).

Also, if required I can open a pull request to do this change, as it seems to me as simple as removing a space. Please let me know if I am missing something here.

Support for timeouted connection (net.DialTimeout)

Currently net.Dial and tls.Dial allows connection to take forever, it can stuck setup of syslog service.

However net.Dial can be changed to net.DialWithTimeout and tls.Dial to tls.DialWithDialer passing net.Dialer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.