Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger for docker environment is set to db #454

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions admin/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"

"github.com/jmpsec/osctrl/admin/sessions"
"github.com/jmpsec/osctrl/backend"
"github.com/jmpsec/osctrl/cache"
"github.com/jmpsec/osctrl/carves"
"github.com/jmpsec/osctrl/environments"
Expand Down Expand Up @@ -172,10 +173,20 @@ func WithAdminConfig(config *types.JSONConfigurationAdmin) HandlersOption {
}
}

func WithDBLogger(dbfile string) HandlersOption {
func WithDBLogger(dbfile string, config *backend.JSONConfigurationDB) HandlersOption {
return func(h *HandlersAdmin) {
if dbfile == "" {
h.DBLogger = nil
if config == nil {
h.DBLogger = nil
return
}
logger, err := logging.CreateLoggerDBConfig(*config)
if err != nil {
log.Printf("error creating DB logger %v", err)
logger.Enabled = false
logger.Database = nil
}
h.DBLogger = logger
return
}
logger, err := logging.CreateLoggerDBFile(dbfile)
Expand Down
20 changes: 16 additions & 4 deletions admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ var (
osqueryTablesFile string
osqueryTablesVersion string
loggerFile string
loggerDbSame bool
staticFilesFolder string
staticOffline bool
carvedFilesFolder string
Expand Down Expand Up @@ -522,6 +523,13 @@ func init() {
EnvVars: []string{"LOGGER_FILE"},
Destination: &loggerFile,
},
&cli.BoolFlag{
Name: "logger-db-same",
Value: false,
Usage: "Use the same DB configuration for the logger",
EnvVars: []string{"LOGGER_DB_SAME"},
Destination: &loggerDbSame,
},
&cli.StringFlag{
Name: "static",
Aliases: []string{"s"},
Expand Down Expand Up @@ -728,9 +736,13 @@ func osctrlAdminService() {
}
}()

// Set the logger configuration file to empty if we are logging to anything but the DB
if adminConfig.Logger != settings.LoggingDB {
loggerFile = ""
var loggerDBConfig *backend.JSONConfigurationDB
loggerFile = ""
// Set the logger configuration file if we have a DB logger
if adminConfig.Logger == settings.LoggingDB {
if loggerDbSame {
loggerDBConfig = &dbConfig
}
}

// Initialize Admin handlers before router
Expand All @@ -753,7 +765,7 @@ func osctrlAdminService() {
handlers.WithOsqueryTables(osqueryTables),
handlers.WithCarvesFolder(carvedFilesFolder),
handlers.WithAdminConfig(&adminConfig),
handlers.WithDBLogger(loggerFile),
handlers.WithDBLogger(loggerFile, loggerDBConfig),
)

// ////////////////////////// ADMIN
Expand Down
1 change: 0 additions & 1 deletion deploy/docker/dockerfiles/Dockerfile-dev-admin
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ RUN go mod verify
### Copy osctrl-admin bin and configs ###
RUN mkdir -p /opt/osctrl/bin
RUN mkdir -p /opt/osctrl/config
RUN mkdir -p /opt/osctrl/script
RUN mkdir -p /opt/osctrl/carved_files

### Copy osctrl-admin web templates ###
Expand Down
3 changes: 1 addition & 2 deletions deploy/docker/dockerfiles/Dockerfile-dev-api
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV GOOS="linux"
ENV CGO_ENABLED=0

# Hot reloading mod
RUN go install github.com/cosmtrek/[email protected]
RUN go install github.com/cosmtrek/[email protected]
RUN go install github.com/go-delve/delve/cmd/[email protected]

# Copy code
Expand All @@ -21,7 +21,6 @@ RUN go mod verify
### Copy osctrl-api bin and configs ###
RUN mkdir -p /opt/osctrl/bin
RUN mkdir -p /opt/osctrl/config
RUN mkdir -p /opt/osctrl/script
RUN go build -o /opt/osctrl/bin/osctrl-api api/*.go

EXPOSE 9002
Expand Down
3 changes: 1 addition & 2 deletions deploy/docker/dockerfiles/Dockerfile-dev-tls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV GOOS="linux"
ENV CGO_ENABLED=0

# Hot reloading mod
RUN go install github.com/cosmtrek/[email protected]
RUN go install github.com/cosmtrek/[email protected]
RUN go install github.com/go-delve/delve/cmd/[email protected]

# Copy code
Expand All @@ -21,7 +21,6 @@ RUN go mod verify
### Copy osctrl-api bin and configs ###
RUN mkdir -p /opt/osctrl/bin
RUN mkdir -p /opt/osctrl/config
RUN mkdir -p /opt/osctrl/script
RUN go build -o /opt/osctrl/bin/osctrl-tls tls/*.go

EXPOSE 9000
Expand Down
24 changes: 13 additions & 11 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ services:
######################################### osctrl-tls #########################################
osctrl-tls:
container_name: 'osctrl-tls-dev'
image: 'osctrl-tls-dev:v${OSCTRL_VERSION}'
image: 'osctrl-tls-dev:${OSCTRL_VERSION}'
restart: unless-stopped
build:
context: .
dockerfile: deploy/docker/dockerfiles/Dockerfile-dev-tls
args:
GOLANG_VERSION: ${GOLANG_VERSION}
environment:
#### TLS settings ####
#### osctrl-tls configuration settings ####
- SERVICE_LISTENER=0.0.0.0
- SERVICE_PORT=9000
- SERVICE_HOST=0.0.0.0
- SERVICE_AUTH=none
- SERVICE_LOGGER=stdout
- SERVICE_LOGGER=db
- LOGGER_DB_SAME=true
#### Database settings ####
- DB_HOST=osctrl-postgres
- DB_NAME=${POSTGRES_DB_NAME}
Expand All @@ -67,7 +68,7 @@ services:
######################################### osctrl-admin #########################################
osctrl-admin:
container_name: 'osctrl-admin-dev'
image: 'osctrl-admin-dev:v${OSCTRL_VERSION}'
image: 'osctrl-admin-dev:${OSCTRL_VERSION}'
restart: unless-stopped
build:
context: .
Expand All @@ -76,13 +77,14 @@ services:
GOLANG_VERSION: ${GOLANG_VERSION}
OSQUERY_VERSION: ${OSQUERY_VERSION}
environment:
#### TLS settings ####
#### osctrl-admin configuration settings ####
- SERVICE_LISTENER=0.0.0.0
- SERVICE_PORT=9001
- SERVICE_HOST=0.0.0.0
- SERVICE_AUTH=db
- JWT_SECRET=${JWT_SECRET}
- SERVICE_LOGGER=stdout
- SERVICE_LOGGER=db
- LOGGER_DB_SAME=true
#### Database settings ####
- DB_HOST=osctrl-postgres
- DB_NAME=${POSTGRES_DB_NAME}
Expand All @@ -104,7 +106,7 @@ services:
######################################### osctrl-api #########################################
osctrl-api:
container_name: 'osctrl-api-dev'
image: 'osctrl-api-dev:v${OSCTRL_VERSION}'
image: 'osctrl-api-dev:${OSCTRL_VERSION}'
restart: unless-stopped
build:
context: .
Expand All @@ -113,13 +115,13 @@ services:
GOLANG_VERSION: ${GOLANG_VERSION}
OSQUERY_VERSION: ${OSQUERY_VERSION}
environment:
#### TLS settings ####
#### osctrl-api configuration settings ####
- SERVICE_LISTENER=0.0.0.0
- SERVICE_PORT=9002
- SERVICE_HOST=0.0.0.0
- SERVICE_AUTH=jwt
- JWT_SECRET=${JWT_SECRET}
- SERVICE_LOGGER=stdout
- SERVICE_LOGGER=db
#### Database settings ####
- DB_HOST=osctrl-postgres
- DB_NAME=${POSTGRES_DB_NAME}
Expand Down Expand Up @@ -183,7 +185,7 @@ services:
##############################################################################################
osctrl-cli:
container_name: 'osctrl-cli-dev'
image: 'osctrl-cli-dev:v${OSCTRL_VERSION}'
image: 'osctrl-cli-dev:${OSCTRL_VERSION}'
restart: unless-stopped
build:
context: .
Expand Down Expand Up @@ -221,7 +223,7 @@ services:
######################################### osquery #########################################
osquery:
container_name: 'osctrl-osquery-dev'
image: 'osctrl-osquery-dev:v${OSCTRL_VERSION}'
image: 'osctrl-osquery-dev:${OSCTRL_VERSION}'
restart: unless-stopped
build:
context: .
Expand Down
21 changes: 15 additions & 6 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type LoggerTLS struct {
}

// CreateLoggerTLS to instantiate a new logger for the TLS endpoint
func CreateLoggerTLS(logging, loggingFile string, s3Conf types.S3Configuration, alwaysLog bool, dbConf backend.JSONConfigurationDB, mgr *settings.Settings, nodes *nodes.NodeManager, queries *queries.Queries) (*LoggerTLS, error) {
func CreateLoggerTLS(logging, loggingFile string, s3Conf types.S3Configuration, loggerSame, alwaysLog bool, dbConf backend.JSONConfigurationDB, mgr *settings.Settings, nodes *nodes.NodeManager, queries *queries.Queries) (*LoggerTLS, error) {
l := &LoggerTLS{
Logging: logging,
Nodes: nodes,
Expand All @@ -47,12 +47,21 @@ func CreateLoggerTLS(logging, loggingFile string, s3Conf types.S3Configuration,
g.Settings(mgr)
l.Logger = g
case settings.LoggingDB:
d, err := CreateLoggerDBFile(loggingFile)
if err != nil {
return nil, err
if loggerSame {
d, err := CreateLoggerDBConfig(dbConf)
if err != nil {
return nil, err
}
d.Settings(mgr)
l.Logger = d
} else {
d, err := CreateLoggerDBFile(loggingFile)
if err != nil {
return nil, err
}
d.Settings(mgr)
l.Logger = d
}
d.Settings(mgr)
l.Logger = d
case settings.LoggingStdout:
d, err := CreateLoggerStdout()
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var (
tlsKeyFile string
loggerFlag bool
loggerFile string
loggerDbSame bool
alwaysLog bool
carverConfigFile string
)
Expand Down Expand Up @@ -421,6 +422,13 @@ func init() {
EnvVars: []string{"LOGGER_FILE"},
Destination: &loggerFile,
},
&cli.BoolFlag{
Name: "logger-db-same",
Value: false,
Usage: "Use the same DB configuration for the logger",
EnvVars: []string{"LOGGER_DB_SAME"},
Destination: &loggerDbSame,
},
&cli.BoolFlag{
Name: "always-log",
Aliases: []string{"a", "always"},
Expand Down Expand Up @@ -569,11 +577,11 @@ func osctrlService() {
ingestedMetrics = metrics.CreateIngested(db.Conn)
// Initialize TLS logger
log.Println("Loading TLS logger")
loggerTLS, err = logging.CreateLoggerTLS(tlsConfig.Logger, loggerFile, s3LogConfig, alwaysLog, dbConfig, settingsmgr, nodesmgr, queriesmgr)
loggerTLS, err = logging.CreateLoggerTLS(
tlsConfig.Logger, loggerFile, s3LogConfig, loggerDbSame, alwaysLog, dbConfig, settingsmgr, nodesmgr, queriesmgr)
if err != nil {
log.Fatalf("Error loading logger - %s: %v", tlsConfig.Logger, err)
}

// Sleep to reload environments
// FIXME Implement Redis cache
// FIXME splay this?
Expand Down