Skip to content
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
60 changes: 52 additions & 8 deletions admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/crewjam/saml/samlsp"
Expand Down Expand Up @@ -257,6 +258,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &adminConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &adminConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &adminConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -642,11 +657,6 @@ func init() {
Destination: &s3CarverConfig.SecretAccessKey,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
}

// Go go!
Expand Down Expand Up @@ -767,10 +777,10 @@ func osctrlAdminService() {
log.Err(err).Msg("Error getting all environments")
}
for _, e := range allEnvs {
if err:= queriesmgr.CleanupExpiredQueries(e.ID); err != nil {
if err := queriesmgr.CleanupExpiredQueries(e.ID); err != nil {
log.Err(err).Msg("Error cleaning up expired queries")
}
if err:= queriesmgr.CleanupExpiredCarves(e.ID); err != nil {
if err := queriesmgr.CleanupExpiredCarves(e.ID); err != nil {
log.Err(err).Msg("Error cleaning up expired carves")
}
}
Expand Down Expand Up @@ -1026,6 +1036,35 @@ func cliAction(c *cli.Context) error {
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -1046,8 +1085,13 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
Copy link
Collaborator

@javuto javuto Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is preferable to use Fatal() than to print the error and call exit, because closing writers and flushing buffers. See the implementation of Fatal() in zerolog:

// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method, which terminates the program immediately.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Fatal() *Event {
	return l.newEvent(FatalLevel, func(msg string) {
		if closer, ok := l.w.(io.Closer); ok {
			// Close the writer to flush any buffered message. Otherwise the message
			// will be lost as os.Exit() terminates the program immediately.
			closer.Close()
		}
		os.Exit(1)
	})
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we cannot use Fatal() because the zerolog is not initialized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will initialize the log after reading all the parameters.

fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}

// Initialize service logger
initializeLogger(adminConfig.LogLevel, adminConfig.LogFormat)

// Service starts!
osctrlAdminService()
}
69 changes: 57 additions & 12 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/jmpsec/osctrl/api/handlers"
Expand Down Expand Up @@ -162,7 +163,7 @@ func loadConfiguration(file, service string) (types.JSONConfigurationAPI, error)
}
// Check if values are valid
if !validAuth[cfg.Auth] {
return cfg, fmt.Errorf("Invalid auth method: '%s'", cfg.Auth)
return cfg, fmt.Errorf("invalid auth method: '%s'", cfg.Auth)
}
// No errors!
return cfg, nil
Expand Down Expand Up @@ -204,6 +205,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &apiConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &apiConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &apiConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -425,11 +440,7 @@ func init() {
Destination: &jwtConfigValues.HoursToExpire,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()

}

// Go go!
Expand Down Expand Up @@ -638,7 +649,7 @@ func cliAction(c *cli.Context) error {
if configFlag {
apiConfig, err = loadConfiguration(serviceConfigFile, settings.ServiceAPI)
if err != nil {
return fmt.Errorf("Failed to load service configuration %s - %s", serviceConfigFile, err)
return fmt.Errorf("failed to load service configuration %s - %s", serviceConfigFile, err.Error())
}
} else {
apiConfig = apiConfigValues
Expand All @@ -647,7 +658,7 @@ func cliAction(c *cli.Context) error {
if dbFlag {
dbConfig, err = backend.LoadConfiguration(dbConfigFile, backend.DBKey)
if err != nil {
return fmt.Errorf("Failed to load DB configuration - %v", err)
return fmt.Errorf("failed to load DB configuration - %s", err.Error())
}
} else {
dbConfig = dbConfigValues
Expand All @@ -656,7 +667,7 @@ func cliAction(c *cli.Context) error {
if redisFlag {
redisConfig, err = cache.LoadConfiguration(redisConfigFile, cache.RedisKey)
if err != nil {
return fmt.Errorf("Failed to load redis configuration - %v", err)
return fmt.Errorf("failed to load redis configuration - %s", err.Error())
}
} else {
redisConfig = redisConfigValues
Expand All @@ -665,14 +676,43 @@ func cliAction(c *cli.Context) error {
if jwtFlag {
jwtConfig, err = loadJWTConfiguration(jwtConfigFile)
if err != nil {
return fmt.Errorf("Failed to load JWT configuration - %v", err)
return fmt.Errorf("failed to load JWT configuration - %s", err.Error())
}
} else {
jwtConfig = jwtConfigValues
}
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -693,8 +733,13 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}
// Service starts!

// Initialize service logger
initializeLogger(apiConfig.LogLevel, apiConfig.LogFormat)

// Run the service
osctrlAPIService()
}
56 changes: 50 additions & 6 deletions tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/jmpsec/osctrl/backend"
Expand Down Expand Up @@ -212,6 +213,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &tlsConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &tlsConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &tlsConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -589,11 +604,7 @@ func init() {
Destination: &kafkaConfiguration.SASL.Password,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()

}

// Go go!
Expand Down Expand Up @@ -839,6 +850,35 @@ func cliAction(c *cli.Context) error {
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -859,8 +899,12 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}

// Initialize service logger
initializeLogger(tlsConfig.LogLevel, tlsConfig.LogFormat)
// Service starts!
osctrlService()
}
28 changes: 23 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ package types

import "time"

const (
// log levels
LogLevelDebug string = "debug"
LogLevelInfo string = "info"
LogLevelWarn string = "warn"
LogLevelError string = "error"

// log formats
LogFormatConsole string = "console"
LogFormatJSON string = "json"
)

// JSONConfigurationTLS to hold TLS service configuration values
type JSONConfigurationTLS struct {
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
MetricsListener string `json:"metricsListener"`
MetricsPort string `json:"metricsPort"`
MetricsEnabled bool `json:"metricsEnabled"`
Expand All @@ -19,6 +33,8 @@ type JSONConfigurationTLS struct {
type JSONConfigurationAdmin struct {
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
Host string `json:"host"`
Auth string `json:"auth"`
Logger string `json:"logger"`
Expand All @@ -28,11 +44,13 @@ type JSONConfigurationAdmin struct {

// JSONConfigurationAPI to hold API service configuration values
type JSONConfigurationAPI struct {
Listener string `json:"listener"`
Port string `json:"port"`
Host string `json:"host"`
Auth string `json:"auth"`
Carver string `json:"carver"`
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
Host string `json:"host"`
Auth string `json:"auth"`
Carver string `json:"carver"`
}

// JSONConfigurationHeaders to keep all headers details for auth
Expand Down