github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/logger/syslog/syslog.go (about)

     1  // Package syslog provides the logdriver for forwarding server logs to syslog endpoints.
     2  package syslog // import "github.com/demonoid81/moby/daemon/logger/syslog"
     3  
     4  import (
     5  	"crypto/tls"
     6  	"errors"
     7  	"fmt"
     8  	"net"
     9  	"net/url"
    10  	"os"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  
    15  	syslog "github.com/RackSec/srslog"
    16  
    17  	"github.com/demonoid81/moby/daemon/logger"
    18  	"github.com/demonoid81/moby/daemon/logger/loggerutils"
    19  	"github.com/demonoid81/moby/pkg/urlutil"
    20  	"github.com/docker/go-connections/tlsconfig"
    21  	"github.com/sirupsen/logrus"
    22  )
    23  
    24  const (
    25  	name        = "syslog"
    26  	secureProto = "tcp+tls"
    27  )
    28  
    29  var facilities = map[string]syslog.Priority{
    30  	"kern":     syslog.LOG_KERN,
    31  	"user":     syslog.LOG_USER,
    32  	"mail":     syslog.LOG_MAIL,
    33  	"daemon":   syslog.LOG_DAEMON,
    34  	"auth":     syslog.LOG_AUTH,
    35  	"syslog":   syslog.LOG_SYSLOG,
    36  	"lpr":      syslog.LOG_LPR,
    37  	"news":     syslog.LOG_NEWS,
    38  	"uucp":     syslog.LOG_UUCP,
    39  	"cron":     syslog.LOG_CRON,
    40  	"authpriv": syslog.LOG_AUTHPRIV,
    41  	"ftp":      syslog.LOG_FTP,
    42  	"local0":   syslog.LOG_LOCAL0,
    43  	"local1":   syslog.LOG_LOCAL1,
    44  	"local2":   syslog.LOG_LOCAL2,
    45  	"local3":   syslog.LOG_LOCAL3,
    46  	"local4":   syslog.LOG_LOCAL4,
    47  	"local5":   syslog.LOG_LOCAL5,
    48  	"local6":   syslog.LOG_LOCAL6,
    49  	"local7":   syslog.LOG_LOCAL7,
    50  }
    51  
    52  type syslogger struct {
    53  	writer *syslog.Writer
    54  }
    55  
    56  func init() {
    57  	if err := logger.RegisterLogDriver(name, New); err != nil {
    58  		logrus.Fatal(err)
    59  	}
    60  	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
    61  		logrus.Fatal(err)
    62  	}
    63  }
    64  
    65  // rsyslog uses appname part of syslog message to fill in an %syslogtag% template
    66  // attribute in rsyslog.conf. In order to be backward compatible to rfc3164
    67  // tag will be also used as an appname
    68  func rfc5424formatterWithAppNameAsTag(p syslog.Priority, hostname, tag, content string) string {
    69  	timestamp := time.Now().Format(time.RFC3339)
    70  	pid := os.Getpid()
    71  	msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
    72  		p, 1, timestamp, hostname, tag, pid, tag, content)
    73  	return msg
    74  }
    75  
    76  // The timestamp field in rfc5424 is derived from rfc3339. Whereas rfc3339 makes allowances
    77  // for multiple syntaxes, there are further restrictions in rfc5424, i.e., the maximum
    78  // resolution is limited to "TIME-SECFRAC" which is 6 (microsecond resolution)
    79  func rfc5424microformatterWithAppNameAsTag(p syslog.Priority, hostname, tag, content string) string {
    80  	timestamp := time.Now().Format("2006-01-02T15:04:05.000000Z07:00")
    81  	pid := os.Getpid()
    82  	msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
    83  		p, 1, timestamp, hostname, tag, pid, tag, content)
    84  	return msg
    85  }
    86  
    87  // New creates a syslog logger using the configuration passed in on
    88  // the context. Supported context configuration variables are
    89  // syslog-address, syslog-facility, syslog-format.
    90  func New(info logger.Info) (logger.Logger, error) {
    91  	tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	proto, address, err := parseAddress(info.Config["syslog-address"])
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	facility, err := parseFacility(info.Config["syslog-facility"])
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	syslogFormatter, syslogFramer, err := parseLogFormat(info.Config["syslog-format"], proto)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	var log *syslog.Writer
   112  	if proto == secureProto {
   113  		tlsConfig, tlsErr := parseTLSConfig(info.Config)
   114  		if tlsErr != nil {
   115  			return nil, tlsErr
   116  		}
   117  		log, err = syslog.DialWithTLSConfig(proto, address, facility, tag, tlsConfig)
   118  	} else {
   119  		log, err = syslog.Dial(proto, address, facility, tag)
   120  	}
   121  
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	log.SetFormatter(syslogFormatter)
   127  	log.SetFramer(syslogFramer)
   128  
   129  	return &syslogger{
   130  		writer: log,
   131  	}, nil
   132  }
   133  
   134  func (s *syslogger) Log(msg *logger.Message) error {
   135  	line := string(msg.Line)
   136  	source := msg.Source
   137  	logger.PutMessage(msg)
   138  	if source == "stderr" {
   139  		return s.writer.Err(line)
   140  	}
   141  	return s.writer.Info(line)
   142  }
   143  
   144  func (s *syslogger) Close() error {
   145  	return s.writer.Close()
   146  }
   147  
   148  func (s *syslogger) Name() string {
   149  	return name
   150  }
   151  
   152  func parseAddress(address string) (string, string, error) {
   153  	if address == "" {
   154  		return "", "", nil
   155  	}
   156  	if !urlutil.IsTransportURL(address) {
   157  		return "", "", fmt.Errorf("syslog-address should be in form proto://address, got %v", address)
   158  	}
   159  	url, err := url.Parse(address)
   160  	if err != nil {
   161  		return "", "", err
   162  	}
   163  
   164  	// unix and unixgram socket validation
   165  	if url.Scheme == "unix" || url.Scheme == "unixgram" {
   166  		if _, err := os.Stat(url.Path); err != nil {
   167  			return "", "", err
   168  		}
   169  		return url.Scheme, url.Path, nil
   170  	}
   171  
   172  	// here we process tcp|udp
   173  	host := url.Host
   174  	if _, _, err := net.SplitHostPort(host); err != nil {
   175  		if !strings.Contains(err.Error(), "missing port in address") {
   176  			return "", "", err
   177  		}
   178  		host = host + ":514"
   179  	}
   180  
   181  	return url.Scheme, host, nil
   182  }
   183  
   184  // ValidateLogOpt looks for syslog specific log options
   185  // syslog-address, syslog-facility.
   186  func ValidateLogOpt(cfg map[string]string) error {
   187  	for key := range cfg {
   188  		switch key {
   189  		case "env":
   190  		case "env-regex":
   191  		case "labels":
   192  		case "labels-regex":
   193  		case "syslog-address":
   194  		case "syslog-facility":
   195  		case "syslog-tls-ca-cert":
   196  		case "syslog-tls-cert":
   197  		case "syslog-tls-key":
   198  		case "syslog-tls-skip-verify":
   199  		case "tag":
   200  		case "syslog-format":
   201  		default:
   202  			return fmt.Errorf("unknown log opt '%s' for syslog log driver", key)
   203  		}
   204  	}
   205  	if _, _, err := parseAddress(cfg["syslog-address"]); err != nil {
   206  		return err
   207  	}
   208  	if _, err := parseFacility(cfg["syslog-facility"]); err != nil {
   209  		return err
   210  	}
   211  	if _, _, err := parseLogFormat(cfg["syslog-format"], ""); err != nil {
   212  		return err
   213  	}
   214  	return nil
   215  }
   216  
   217  func parseFacility(facility string) (syslog.Priority, error) {
   218  	if facility == "" {
   219  		return syslog.LOG_DAEMON, nil
   220  	}
   221  
   222  	if syslogFacility, valid := facilities[facility]; valid {
   223  		return syslogFacility, nil
   224  	}
   225  
   226  	fInt, err := strconv.Atoi(facility)
   227  	if err == nil && 0 <= fInt && fInt <= 23 {
   228  		return syslog.Priority(fInt << 3), nil
   229  	}
   230  
   231  	return syslog.Priority(0), errors.New("invalid syslog facility")
   232  }
   233  
   234  func parseTLSConfig(cfg map[string]string) (*tls.Config, error) {
   235  	_, skipVerify := cfg["syslog-tls-skip-verify"]
   236  
   237  	opts := tlsconfig.Options{
   238  		CAFile:             cfg["syslog-tls-ca-cert"],
   239  		CertFile:           cfg["syslog-tls-cert"],
   240  		KeyFile:            cfg["syslog-tls-key"],
   241  		InsecureSkipVerify: skipVerify,
   242  	}
   243  
   244  	return tlsconfig.Client(opts)
   245  }
   246  
   247  func parseLogFormat(logFormat, proto string) (syslog.Formatter, syslog.Framer, error) {
   248  	switch logFormat {
   249  	case "":
   250  		return syslog.UnixFormatter, syslog.DefaultFramer, nil
   251  	case "rfc3164":
   252  		return syslog.RFC3164Formatter, syslog.DefaultFramer, nil
   253  	case "rfc5424":
   254  		if proto == secureProto {
   255  			return rfc5424formatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
   256  		}
   257  		return rfc5424formatterWithAppNameAsTag, syslog.DefaultFramer, nil
   258  	case "rfc5424micro":
   259  		if proto == secureProto {
   260  			return rfc5424microformatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
   261  		}
   262  		return rfc5424microformatterWithAppNameAsTag, syslog.DefaultFramer, nil
   263  	default:
   264  		return nil, nil, errors.New("Invalid syslog format")
   265  	}
   266  
   267  }