2016-09-29 02:44:05 +00:00
|
|
|
// Package service holds the implementation of the kolide service interface and the HTTP endpoints
|
2016-09-01 04:51:38 +00:00
|
|
|
// for the API
|
2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
import (
|
2016-09-04 05:13:42 +00:00
|
|
|
"io"
|
2017-11-16 16:53:42 +00:00
|
|
|
"net/http"
|
2017-03-03 17:21:48 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-11-16 16:53:42 +00:00
|
|
|
"time"
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-21 03:08:11 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2016-09-01 04:51:38 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/config"
|
|
|
|
"github.com/kolide/fleet/server/kolide"
|
|
|
|
"github.com/kolide/fleet/server/logwriter"
|
|
|
|
"github.com/kolide/fleet/server/sso"
|
2017-11-16 16:53:42 +00:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2016-09-01 04:51:38 +00:00
|
|
|
)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
// NewService creates a new service from the config struct
|
2017-05-09 00:43:48 +00:00
|
|
|
func NewService(ds kolide.Datastore, resultStore kolide.QueryResultStore,
|
|
|
|
logger kitlog.Logger, kolideConfig config.KolideConfig, mailService kolide.MailService,
|
2017-09-01 16:42:46 +00:00
|
|
|
c clock.Clock, sso sso.SessionStore) (kolide.Service, error) {
|
2016-08-28 03:59:17 +00:00
|
|
|
var svc kolide.Service
|
2017-04-03 21:48:50 +00:00
|
|
|
statusWriter, err := osqueryLogFile(kolideConfig.Osquery.StatusLogFile, logger, kolideConfig.Osquery.EnableLogRotation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultWriter, err := osqueryLogFile(kolideConfig.Osquery.ResultLogFile, logger, kolideConfig.Osquery.EnableLogRotation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
svc = service{
|
2017-09-01 16:42:46 +00:00
|
|
|
ds: ds,
|
|
|
|
resultStore: resultStore,
|
|
|
|
logger: logger,
|
|
|
|
config: kolideConfig,
|
|
|
|
clock: c,
|
2016-09-14 16:11:06 +00:00
|
|
|
|
2017-03-03 17:21:48 +00:00
|
|
|
osqueryStatusLogWriter: statusWriter,
|
|
|
|
osqueryResultLogWriter: resultWriter,
|
2016-10-01 02:18:27 +00:00
|
|
|
mailService: mailService,
|
2017-05-09 00:43:48 +00:00
|
|
|
ssoSessionStore: sso,
|
2017-11-16 16:53:42 +00:00
|
|
|
metaDataClient: &http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
},
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
2017-05-09 00:43:48 +00:00
|
|
|
svc = validationMiddleware{svc, ds, sso}
|
2016-08-28 03:59:17 +00:00
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:21:48 +00:00
|
|
|
// osqueryLogFile creates a log file for osquery status/result logs
|
2017-04-03 21:48:50 +00:00
|
|
|
// the logFile can be rotated by sending a `SIGHUP` signal to kolide if
|
|
|
|
// enableRotation is true
|
|
|
|
func osqueryLogFile(path string, appLogger kitlog.Logger, enableRotation bool) (io.Writer, error) {
|
|
|
|
if enableRotation {
|
|
|
|
osquerydLogger := &lumberjack.Logger{
|
|
|
|
Filename: path,
|
|
|
|
MaxSize: 500, // megabytes
|
|
|
|
MaxBackups: 3,
|
|
|
|
MaxAge: 28, //days
|
2017-03-03 17:21:48 +00:00
|
|
|
}
|
2017-04-03 21:48:50 +00:00
|
|
|
appLogger = kitlog.With(appLogger, "component", "osqueryd-logger")
|
|
|
|
sig := make(chan os.Signal)
|
|
|
|
signal.Notify(sig, syscall.SIGHUP)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-sig //block on signal
|
|
|
|
if err := osquerydLogger.Rotate(); err != nil {
|
|
|
|
appLogger.Log("err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return osquerydLogger, nil
|
|
|
|
}
|
|
|
|
// no log rotation
|
|
|
|
return logwriter.New(path)
|
2017-03-03 17:21:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
type service struct {
|
2017-09-01 16:42:46 +00:00
|
|
|
ds kolide.Datastore
|
|
|
|
resultStore kolide.QueryResultStore
|
|
|
|
logger kitlog.Logger
|
|
|
|
config config.KolideConfig
|
|
|
|
clock clock.Clock
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2016-10-01 02:18:27 +00:00
|
|
|
osqueryStatusLogWriter io.Writer
|
|
|
|
osqueryResultLogWriter io.Writer
|
2016-09-15 14:52:17 +00:00
|
|
|
|
2017-05-09 00:43:48 +00:00
|
|
|
mailService kolide.MailService
|
|
|
|
ssoSessionStore sso.SessionStore
|
2017-11-16 16:53:42 +00:00
|
|
|
metaDataClient *http.Client
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
|
|
|
|
func (s service) SendEmail(mail kolide.Email) error {
|
|
|
|
return s.mailService.SendEmail(mail)
|
|
|
|
}
|
2017-01-04 21:16:17 +00:00
|
|
|
|
|
|
|
func (s service) Clock() clock.Clock {
|
|
|
|
return s.clock
|
|
|
|
}
|
2017-05-09 00:43:48 +00:00
|
|
|
|
|
|
|
type validationMiddleware struct {
|
|
|
|
kolide.Service
|
|
|
|
ds kolide.Datastore
|
|
|
|
ssoSessionStore sso.SessionStore
|
|
|
|
}
|