osel/syslog.go
Nate Johnston ca0e1ca769 Initial import of osel code
This is an initial import of the osel codebase.  The osel tool is a tool that
initiates external security scans (initially through Qualys) upon reciept of
AMQP events that indicate certain sensitive events have occurred, like a
security group rule change.

The commit history had to be thrown away because it contained some non-public
data, so I would like to call out the following contributors:

This uses go 1.10 and vgo for dependency management.

Co-Authored-By: Charles Bitter <Charles_Bitter@cable.comcast.com>
Co-Authored-By: Olivier Gagnon <Olivier_Gagnon@cable.comcast.com>
Co-Authored-By: Joseph Sleiman <Joseph_Sleiman@comcast.com>

Change-Id: Ib6abe2024fd91978b783ceee4cff8bb4678d7b15
2018-03-24 15:30:57 +00:00

82 lines
2.1 KiB
Go

package main
/*
syslog - This file includes all of the logic necessary to interact with syslog.
This is extrapolated out so that a SyslogActioner interface can be
passed to functions. Doing this allows testing by mock classes to be created
that can be passed to functions.
Since this is a wrapper around the log/syslog library, this does not need
testing.
*/
import (
"fmt"
"log"
"log/syslog"
"net"
)
// SyslogActioner is an interface for an SyslogActions class. Having
// this as an interface allows us to pass in a dummy class for testing that
// just returns mocked data.
type SyslogActioner interface {
Connect() error
Info(string)
}
// SyslogActions is a class that handles all interactions directly with Syslog.
// See the comment on SyslogActioner for rationale.
type SyslogActions struct {
logger *syslog.Writer
Options SyslogOptions
}
// SyslogOptions is a class to convey all of the configurable options for the
// SyslogActions class.
type SyslogOptions struct {
Host string
Protocol string
Retry bool
Port string
}
// Info is the main method for the SyslogActioner class, it writes an
// info-level message to the syslog stream.
func (s SyslogActions) Info(writeMe string) {
log.Println("Logged: ", writeMe)
s.logger.Info(writeMe)
}
// Connect is the method that establishes the connection to the syslog server
// over the network.
func (s *SyslogActions) Connect() error {
var err error
address := net.JoinHostPort(s.Options.Host, s.Options.Port)
if Debug {
log.Printf("Opening %q syslog socket to %q\n", s.Options.Protocol, s.Options.Host)
}
s.logger, err = syslog.Dial(s.Options.Protocol, address, syslog.LOG_INFO, "osel")
if err != nil {
log.Printf("error opening syslog socket to %s: %s\n", s.Options.Host, err)
if s.Options.Retry {
for err != nil {
log.Println("retrying")
s.logger, err = syslog.Dial(s.Options.Protocol, address, syslog.LOG_INFO, "osel")
}
}
return fmt.Errorf("error opening syslog socket to %s: %s", s.Options.Host, err)
}
if Debug {
log.Println("Successfully connected to syslog host", s.Options.Host)
}
return nil
}